blob: 6e7ec9b42ef9f9974d78abbbe9a8be5eebd81ef1 [file] [log] [blame]
John Reck668f0e32014-03-26 15:10:40 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define ATRACE_TAG ATRACE_TAG_VIEW
18
19#include "DrawFrameTask.h"
20
21#include <utils/Log.h>
22#include <utils/Trace.h>
23
24#include "../DisplayList.h"
25#include "../RenderNode.h"
26#include "CanvasContext.h"
27#include "RenderThread.h"
28
29namespace android {
30namespace uirenderer {
31namespace renderthread {
32
John Reck087bc0c2014-04-04 16:20:08 -070033DrawFrameTask::DrawFrameTask() : mContext(0), mTaskMode(MODE_INVALID), mRenderNode(0) {
John Reck668f0e32014-03-26 15:10:40 -070034}
35
36DrawFrameTask::~DrawFrameTask() {
37}
38
39void DrawFrameTask::setContext(CanvasContext* context) {
40 mContext = context;
41}
42
John Reck668f0e32014-03-26 15:10:40 -070043void DrawFrameTask::addLayer(DeferredLayerUpdater* layer) {
John Reck087bc0c2014-04-04 16:20:08 -070044 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to addLayer with!");
45
John Reck668f0e32014-03-26 15:10:40 -070046 mLayers.push(layer);
47}
48
49void DrawFrameTask::removeLayer(DeferredLayerUpdater* layer) {
50 for (size_t i = 0; i < mLayers.size(); i++) {
51 if (mLayers[i] == layer) {
52 mLayers.removeAt(i);
53 break;
54 }
55 }
56}
57
58void DrawFrameTask::setRenderNode(RenderNode* renderNode) {
John Reck087bc0c2014-04-04 16:20:08 -070059 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to setRenderNode with!");
60
John Reck668f0e32014-03-26 15:10:40 -070061 mRenderNode = renderNode;
62}
63
64void DrawFrameTask::setDirty(int left, int top, int right, int bottom) {
65 mDirty.set(left, top, right, bottom);
66}
67
68void DrawFrameTask::drawFrame(RenderThread* renderThread) {
John Reck087bc0c2014-04-04 16:20:08 -070069 LOG_ALWAYS_FATAL_IF(!mRenderNode.get(), "Cannot drawFrame with no render node!");
John Reck668f0e32014-03-26 15:10:40 -070070 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
71
John Reck087bc0c2014-04-04 16:20:08 -070072 postAndWait(renderThread, MODE_FULL);
John Reck668f0e32014-03-26 15:10:40 -070073
74 // Reset the single-frame data
75 mDirty.setEmpty();
76 mRenderNode = 0;
77}
78
John Reck087bc0c2014-04-04 16:20:08 -070079void DrawFrameTask::flushStateChanges(RenderThread* renderThread) {
80 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
81
82 postAndWait(renderThread, MODE_STATE_ONLY);
83}
84
85void DrawFrameTask::postAndWait(RenderThread* renderThread, TaskMode mode) {
86 LOG_ALWAYS_FATAL_IF(mode == MODE_INVALID, "That's not a real mode, silly!");
87
88 mTaskMode = mode;
89 AutoMutex _lock(mLock);
90 renderThread->queue(this);
91 mSignal.wait(mLock);
92}
93
John Reck668f0e32014-03-26 15:10:40 -070094void DrawFrameTask::run() {
95 ATRACE_NAME("DrawFrame");
96
97 syncFrameState();
98
John Reck087bc0c2014-04-04 16:20:08 -070099 if (mTaskMode == MODE_STATE_ONLY) {
100 unblockUiThread();
101 return;
102 }
103
John Reck668f0e32014-03-26 15:10:40 -0700104 // Grab a copy of everything we need
105 Rect dirtyCopy(mDirty);
John Reck087bc0c2014-04-04 16:20:08 -0700106 sp<RenderNode> renderNode = mRenderNode;
John Reck668f0e32014-03-26 15:10:40 -0700107 CanvasContext* context = mContext;
108
109 // This is temporary until WebView has a solution for syncing frame state
John Reck087bc0c2014-04-04 16:20:08 -0700110 bool canUnblockUiThread = !requiresSynchronousDraw(renderNode.get());
John Reck668f0e32014-03-26 15:10:40 -0700111
112 // From this point on anything in "this" is *UNSAFE TO ACCESS*
113 if (canUnblockUiThread) {
114 unblockUiThread();
115 }
116
John Reck087bc0c2014-04-04 16:20:08 -0700117 drawRenderNode(context, renderNode.get(), &dirtyCopy);
John Reck668f0e32014-03-26 15:10:40 -0700118
119 if (!canUnblockUiThread) {
120 unblockUiThread();
121 }
122}
123
124void DrawFrameTask::syncFrameState() {
125 ATRACE_CALL();
126
John Reck668f0e32014-03-26 15:10:40 -0700127 mContext->processLayerUpdates(&mLayers);
John Reck087bc0c2014-04-04 16:20:08 -0700128
129 // If we don't have an mRenderNode this is a state flush only
130 if (mRenderNode.get()) {
John Reck8de65a82014-04-09 15:23:38 -0700131 mRenderNode->pushStagingChanges();
John Reck087bc0c2014-04-04 16:20:08 -0700132 }
John Reck668f0e32014-03-26 15:10:40 -0700133}
134
135void DrawFrameTask::unblockUiThread() {
136 AutoMutex _lock(mLock);
137 mSignal.signal();
138}
139
140void DrawFrameTask::drawRenderNode(CanvasContext* context, RenderNode* renderNode, Rect* dirty) {
141 ATRACE_CALL();
142
143 if (dirty->bottom == -1 && dirty->left == -1
144 && dirty->top == -1 && dirty->right == -1) {
145 dirty = 0;
146 }
147 context->drawDisplayList(renderNode, dirty);
148}
149
150bool DrawFrameTask::requiresSynchronousDraw(RenderNode* renderNode) {
151 return renderNode->hasFunctors();
152}
153
154} /* namespace renderthread */
155} /* namespace uirenderer */
156} /* namespace android */