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 #include "DrawFrameTask.h"
18
19 #include <utils/Log.h>
20 #include <utils/Trace.h>
21
22 #include "../DeferredLayerUpdater.h"
23 #include "../DisplayList.h"
24 #include "../RenderNode.h"
25 #include "CanvasContext.h"
26 #include "RenderThread.h"
27
28 namespace android {
29 namespace uirenderer {
30 namespace renderthread {
31
DrawFrameTask()32 DrawFrameTask::DrawFrameTask()
33 : mRenderThread(nullptr)
34 , mContext(nullptr)
35 , mSyncResult(SyncResult::OK) {
36 }
37
~DrawFrameTask()38 DrawFrameTask::~DrawFrameTask() {
39 }
40
setContext(RenderThread * thread,CanvasContext * context,RenderNode * targetNode)41 void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context,
42 RenderNode* targetNode) {
43 mRenderThread = thread;
44 mContext = context;
45 mTargetNode = targetNode;
46 }
47
pushLayerUpdate(DeferredLayerUpdater * layer)48 void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
49 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to pushLayerUpdate with!");
50
51 for (size_t i = 0; i < mLayers.size(); i++) {
52 if (mLayers[i].get() == layer) {
53 return;
54 }
55 }
56 mLayers.push_back(layer);
57 }
58
removeLayerUpdate(DeferredLayerUpdater * layer)59 void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
60 for (size_t i = 0; i < mLayers.size(); i++) {
61 if (mLayers[i].get() == layer) {
62 mLayers.erase(mLayers.begin() + i);
63 return;
64 }
65 }
66 }
67
drawFrame(TreeObserver * observer)68 int DrawFrameTask::drawFrame(TreeObserver* observer) {
69 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
70
71 mSyncResult = SyncResult::OK;
72 mSyncQueued = systemTime(CLOCK_MONOTONIC);
73 mObserver = observer;
74 postAndWait();
75
76 return mSyncResult;
77 }
78
postAndWait()79 void DrawFrameTask::postAndWait() {
80 AutoMutex _lock(mLock);
81 mRenderThread->queue(this);
82 mSignal.wait(mLock);
83 }
84
run()85 void DrawFrameTask::run() {
86 ATRACE_NAME("DrawFrame");
87
88 bool canUnblockUiThread;
89 bool canDrawThisFrame;
90 {
91 TreeInfo info(TreeInfo::MODE_FULL, *mContext);
92 info.observer = mObserver;
93 canUnblockUiThread = syncFrameState(info);
94 canDrawThisFrame = info.out.canDrawThisFrame;
95 }
96
97 // Grab a copy of everything we need
98 CanvasContext* context = mContext;
99
100 // From this point on anything in "this" is *UNSAFE TO ACCESS*
101 if (canUnblockUiThread) {
102 unblockUiThread();
103 }
104
105 if (CC_LIKELY(canDrawThisFrame)) {
106 context->draw();
107 }
108
109 if (!canUnblockUiThread) {
110 unblockUiThread();
111 }
112 }
113
syncFrameState(TreeInfo & info)114 bool DrawFrameTask::syncFrameState(TreeInfo& info) {
115 ATRACE_CALL();
116 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
117 mRenderThread->timeLord().vsyncReceived(vsync);
118 bool canDraw = mContext->makeCurrent();
119 Caches::getInstance().textureCache.resetMarkInUse(mContext);
120
121 for (size_t i = 0; i < mLayers.size(); i++) {
122 mLayers[i]->apply();
123 }
124 mLayers.clear();
125 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
126
127 // This is after the prepareTree so that any pending operations
128 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
129 if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
130 if (!mContext->hasSurface()) {
131 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
132 } else {
133 // If we have a surface but can't draw we must be stopped
134 mSyncResult |= SyncResult::ContextIsStopped;
135 }
136 info.out.canDrawThisFrame = false;
137 }
138
139 if (info.out.hasAnimations) {
140 if (info.out.requiresUiRedraw) {
141 mSyncResult |= SyncResult::UIRedrawRequired;
142 }
143 }
144 // If prepareTextures is false, we ran out of texture cache space
145 return info.prepareTextures;
146 }
147
unblockUiThread()148 void DrawFrameTask::unblockUiThread() {
149 AutoMutex _lock(mLock);
150 mSignal.signal();
151 }
152
153 } /* namespace renderthread */
154 } /* namespace uirenderer */
155 } /* namespace android */
156