blob: 7b509a2040a1c90fcabed038a75ac0baf7299b1b [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 -070033SetDisplayListData::SetDisplayListData() : mNewData(0) {}
34
35SetDisplayListData::SetDisplayListData(RenderNode* node, DisplayListData* newData)
36 : mTargetNode(node), mNewData(newData) {
37}
38
39SetDisplayListData::~SetDisplayListData() {}
40
41void SetDisplayListData::apply() const {
42 mTargetNode->setData(mNewData);
43}
44
45DrawFrameTask::DrawFrameTask() : mContext(0), mTaskMode(MODE_INVALID), mRenderNode(0) {
John Reck668f0e32014-03-26 15:10:40 -070046}
47
48DrawFrameTask::~DrawFrameTask() {
49}
50
51void DrawFrameTask::setContext(CanvasContext* context) {
52 mContext = context;
53}
54
55void DrawFrameTask::setDisplayListData(RenderNode* renderNode, DisplayListData* newData) {
John Reck087bc0c2014-04-04 16:20:08 -070056 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to setDisplayListData with!");
57
58 SetDisplayListData setter(renderNode, newData);
John Reck668f0e32014-03-26 15:10:40 -070059 mDisplayListDataUpdates.push(setter);
60}
61
62void DrawFrameTask::addLayer(DeferredLayerUpdater* layer) {
John Reck087bc0c2014-04-04 16:20:08 -070063 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to addLayer with!");
64
John Reck668f0e32014-03-26 15:10:40 -070065 mLayers.push(layer);
66}
67
68void DrawFrameTask::removeLayer(DeferredLayerUpdater* layer) {
69 for (size_t i = 0; i < mLayers.size(); i++) {
70 if (mLayers[i] == layer) {
71 mLayers.removeAt(i);
72 break;
73 }
74 }
75}
76
77void DrawFrameTask::setRenderNode(RenderNode* renderNode) {
John Reck087bc0c2014-04-04 16:20:08 -070078 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to setRenderNode with!");
79
John Reck668f0e32014-03-26 15:10:40 -070080 mRenderNode = renderNode;
81}
82
83void DrawFrameTask::setDirty(int left, int top, int right, int bottom) {
84 mDirty.set(left, top, right, bottom);
85}
86
87void DrawFrameTask::drawFrame(RenderThread* renderThread) {
John Reck087bc0c2014-04-04 16:20:08 -070088 LOG_ALWAYS_FATAL_IF(!mRenderNode.get(), "Cannot drawFrame with no render node!");
John Reck668f0e32014-03-26 15:10:40 -070089 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
90
John Reck087bc0c2014-04-04 16:20:08 -070091 postAndWait(renderThread, MODE_FULL);
John Reck668f0e32014-03-26 15:10:40 -070092
93 // Reset the single-frame data
94 mDirty.setEmpty();
95 mRenderNode = 0;
96}
97
John Reck087bc0c2014-04-04 16:20:08 -070098void DrawFrameTask::flushStateChanges(RenderThread* renderThread) {
99 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
100
101 postAndWait(renderThread, MODE_STATE_ONLY);
102}
103
104void DrawFrameTask::postAndWait(RenderThread* renderThread, TaskMode mode) {
105 LOG_ALWAYS_FATAL_IF(mode == MODE_INVALID, "That's not a real mode, silly!");
106
107 mTaskMode = mode;
108 AutoMutex _lock(mLock);
109 renderThread->queue(this);
110 mSignal.wait(mLock);
111}
112
John Reck668f0e32014-03-26 15:10:40 -0700113void DrawFrameTask::run() {
114 ATRACE_NAME("DrawFrame");
115
116 syncFrameState();
117
John Reck087bc0c2014-04-04 16:20:08 -0700118 if (mTaskMode == MODE_STATE_ONLY) {
119 unblockUiThread();
120 return;
121 }
122
John Reck668f0e32014-03-26 15:10:40 -0700123 // Grab a copy of everything we need
124 Rect dirtyCopy(mDirty);
John Reck087bc0c2014-04-04 16:20:08 -0700125 sp<RenderNode> renderNode = mRenderNode;
John Reck668f0e32014-03-26 15:10:40 -0700126 CanvasContext* context = mContext;
127
128 // This is temporary until WebView has a solution for syncing frame state
John Reck087bc0c2014-04-04 16:20:08 -0700129 bool canUnblockUiThread = !requiresSynchronousDraw(renderNode.get());
John Reck668f0e32014-03-26 15:10:40 -0700130
131 // From this point on anything in "this" is *UNSAFE TO ACCESS*
132 if (canUnblockUiThread) {
133 unblockUiThread();
134 }
135
John Reck087bc0c2014-04-04 16:20:08 -0700136 drawRenderNode(context, renderNode.get(), &dirtyCopy);
John Reck668f0e32014-03-26 15:10:40 -0700137
138 if (!canUnblockUiThread) {
139 unblockUiThread();
140 }
141}
142
143void DrawFrameTask::syncFrameState() {
144 ATRACE_CALL();
145
146 for (size_t i = 0; i < mDisplayListDataUpdates.size(); i++) {
147 const SetDisplayListData& setter = mDisplayListDataUpdates[i];
John Reck087bc0c2014-04-04 16:20:08 -0700148 setter.apply();
John Reck668f0e32014-03-26 15:10:40 -0700149 }
150 mDisplayListDataUpdates.clear();
151
152 mContext->processLayerUpdates(&mLayers);
John Reck087bc0c2014-04-04 16:20:08 -0700153
154 // If we don't have an mRenderNode this is a state flush only
155 if (mRenderNode.get()) {
156 mRenderNode->updateProperties();
157 }
John Reck668f0e32014-03-26 15:10:40 -0700158}
159
160void DrawFrameTask::unblockUiThread() {
161 AutoMutex _lock(mLock);
162 mSignal.signal();
163}
164
165void DrawFrameTask::drawRenderNode(CanvasContext* context, RenderNode* renderNode, Rect* dirty) {
166 ATRACE_CALL();
167
168 if (dirty->bottom == -1 && dirty->left == -1
169 && dirty->top == -1 && dirty->right == -1) {
170 dirty = 0;
171 }
172 context->drawDisplayList(renderNode, dirty);
173}
174
175bool DrawFrameTask::requiresSynchronousDraw(RenderNode* renderNode) {
176 return renderNode->hasFunctors();
177}
178
179} /* namespace renderthread */
180} /* namespace uirenderer */
181} /* namespace android */