DrawFrameTask.cpp revision 087bc0c14bdccf7c258dce0cdef46a69a839b427
1/*
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
33SetDisplayListData::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) {
46}
47
48DrawFrameTask::~DrawFrameTask() {
49}
50
51void DrawFrameTask::setContext(CanvasContext* context) {
52    mContext = context;
53}
54
55void DrawFrameTask::setDisplayListData(RenderNode* renderNode, DisplayListData* newData) {
56    LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to setDisplayListData with!");
57
58    SetDisplayListData setter(renderNode, newData);
59    mDisplayListDataUpdates.push(setter);
60}
61
62void DrawFrameTask::addLayer(DeferredLayerUpdater* layer) {
63    LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to addLayer with!");
64
65    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) {
78    LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to setRenderNode with!");
79
80    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) {
88    LOG_ALWAYS_FATAL_IF(!mRenderNode.get(), "Cannot drawFrame with no render node!");
89    LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
90
91    postAndWait(renderThread, MODE_FULL);
92
93    // Reset the single-frame data
94    mDirty.setEmpty();
95    mRenderNode = 0;
96}
97
98void 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
113void DrawFrameTask::run() {
114    ATRACE_NAME("DrawFrame");
115
116    syncFrameState();
117
118    if (mTaskMode == MODE_STATE_ONLY) {
119        unblockUiThread();
120        return;
121    }
122
123    // Grab a copy of everything we need
124    Rect dirtyCopy(mDirty);
125    sp<RenderNode> renderNode = mRenderNode;
126    CanvasContext* context = mContext;
127
128    // This is temporary until WebView has a solution for syncing frame state
129    bool canUnblockUiThread = !requiresSynchronousDraw(renderNode.get());
130
131    // From this point on anything in "this" is *UNSAFE TO ACCESS*
132    if (canUnblockUiThread) {
133        unblockUiThread();
134    }
135
136    drawRenderNode(context, renderNode.get(), &dirtyCopy);
137
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];
148        setter.apply();
149    }
150    mDisplayListDataUpdates.clear();
151
152    mContext->processLayerUpdates(&mLayers);
153
154    // If we don't have an mRenderNode this is a state flush only
155    if (mRenderNode.get()) {
156        mRenderNode->updateProperties();
157    }
158}
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 */
182