blob: 61d67ca3f608a9a1c59645e79bc05ed6a4e87fe3 [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
John Reckd72e0a32014-05-29 18:56:11 -070024#include "../DeferredLayerUpdater.h"
John Reck668f0e32014-03-26 15:10:40 -070025#include "../DisplayList.h"
26#include "../RenderNode.h"
27#include "CanvasContext.h"
28#include "RenderThread.h"
29
30namespace android {
31namespace uirenderer {
32namespace renderthread {
33
John Reck18f16e62014-05-02 16:46:41 -070034DrawFrameTask::DrawFrameTask()
35 : mRenderThread(NULL)
36 , mContext(NULL)
John Reckf9be7792014-05-02 18:21:16 -070037 , mFrameTimeNanos(0)
John Reckfe5e7b72014-05-23 17:42:28 -070038 , mRecordDurationNanos(0)
39 , mDensity(1.0f) // safe enough default
John Reckf9be7792014-05-02 18:21:16 -070040 , mSyncResult(kSync_OK) {
John Reck668f0e32014-03-26 15:10:40 -070041}
42
43DrawFrameTask::~DrawFrameTask() {
44}
45
John Reck18f16e62014-05-02 16:46:41 -070046void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) {
47 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070048 mContext = context;
49}
50
John Reckd72e0a32014-05-29 18:56:11 -070051void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
52 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to pushLayerUpdate with!");
John Reck087bc0c2014-04-04 16:20:08 -070053
John Reckd72e0a32014-05-29 18:56:11 -070054 for (size_t i = 0; i < mLayers.size(); i++) {
55 if (mLayers[i].get() == layer) {
56 return;
57 }
58 }
59 mLayers.push_back(layer);
John Reck668f0e32014-03-26 15:10:40 -070060}
61
John Reckd72e0a32014-05-29 18:56:11 -070062void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -070063 for (size_t i = 0; i < mLayers.size(); i++) {
John Reckd72e0a32014-05-29 18:56:11 -070064 if (mLayers[i].get() == layer) {
65 mLayers.erase(mLayers.begin() + i);
66 return;
John Reck668f0e32014-03-26 15:10:40 -070067 }
68 }
69}
70
John Reck668f0e32014-03-26 15:10:40 -070071void DrawFrameTask::setDirty(int left, int top, int right, int bottom) {
72 mDirty.set(left, top, right, bottom);
73}
74
John Reckfe5e7b72014-05-23 17:42:28 -070075int DrawFrameTask::drawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos) {
John Reck668f0e32014-03-26 15:10:40 -070076 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
77
John Reckf9be7792014-05-02 18:21:16 -070078 mSyncResult = kSync_OK;
John Reck18f16e62014-05-02 16:46:41 -070079 mFrameTimeNanos = frameTimeNanos;
John Reckfe5e7b72014-05-23 17:42:28 -070080 mRecordDurationNanos = recordDurationNanos;
John Reck18f16e62014-05-02 16:46:41 -070081 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070082
83 // Reset the single-frame data
John Reck18f16e62014-05-02 16:46:41 -070084 mFrameTimeNanos = 0;
John Reckfe5e7b72014-05-23 17:42:28 -070085 mRecordDurationNanos = 0;
John Reck668f0e32014-03-26 15:10:40 -070086 mDirty.setEmpty();
John Reckf9be7792014-05-02 18:21:16 -070087
88 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070089}
90
John Reck18f16e62014-05-02 16:46:41 -070091void DrawFrameTask::postAndWait() {
John Reck087bc0c2014-04-04 16:20:08 -070092 AutoMutex _lock(mLock);
John Reck18f16e62014-05-02 16:46:41 -070093 mRenderThread->queue(this);
John Reck087bc0c2014-04-04 16:20:08 -070094 mSignal.wait(mLock);
95}
96
John Reck668f0e32014-03-26 15:10:40 -070097void DrawFrameTask::run() {
98 ATRACE_NAME("DrawFrame");
99
John Reckfe5e7b72014-05-23 17:42:28 -0700100 mContext->profiler().setDensity(mDensity);
101 mContext->profiler().startFrame(mRecordDurationNanos);
102
John Recka5dda642014-05-22 15:43:54 -0700103 bool canUnblockUiThread;
104 bool canDrawThisFrame;
105 {
106 TreeInfo info;
107 canUnblockUiThread = syncFrameState(info);
108 canDrawThisFrame = info.out.canDrawThisFrame;
109 }
John Reck668f0e32014-03-26 15:10:40 -0700110
111 // Grab a copy of everything we need
John Recke45b1fd2014-04-15 09:50:16 -0700112 Rect dirty(mDirty);
John Reck668f0e32014-03-26 15:10:40 -0700113 CanvasContext* context = mContext;
114
John Reck668f0e32014-03-26 15:10:40 -0700115 // From this point on anything in "this" is *UNSAFE TO ACCESS*
116 if (canUnblockUiThread) {
117 unblockUiThread();
118 }
119
John Recka5dda642014-05-22 15:43:54 -0700120 if (CC_LIKELY(canDrawThisFrame)) {
121 context->draw(&dirty);
122 }
John Reck668f0e32014-03-26 15:10:40 -0700123
124 if (!canUnblockUiThread) {
125 unblockUiThread();
126 }
127}
128
John Recke45b1fd2014-04-15 09:50:16 -0700129static void initTreeInfo(TreeInfo& info) {
John Reck860d1552014-04-11 19:15:05 -0700130 info.prepareTextures = true;
John Recke45b1fd2014-04-15 09:50:16 -0700131 info.performStagingPush = true;
132 info.evaluateAnimations = true;
John Reck860d1552014-04-11 19:15:05 -0700133}
134
John Recka5dda642014-05-22 15:43:54 -0700135bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700136 ATRACE_CALL();
John Reck18f16e62014-05-02 16:46:41 -0700137 mRenderThread->timeLord().vsyncReceived(mFrameTimeNanos);
John Reck860d1552014-04-11 19:15:05 -0700138 mContext->makeCurrent();
139 Caches::getInstance().textureCache.resetMarkInUse();
John Recke45b1fd2014-04-15 09:50:16 -0700140 initTreeInfo(info);
John Reckd72e0a32014-05-29 18:56:11 -0700141
142 for (size_t i = 0; i < mLayers.size(); i++) {
143 mContext->processLayerUpdate(mLayers[i].get(), info);
144 }
145 mLayers.clear();
146 if (info.out.hasAnimations) {
147 // TODO: Uh... crap?
148 }
149 mContext->prepareTree(info);
150
John Reckf9be7792014-05-02 18:21:16 -0700151 if (info.out.hasAnimations) {
John Reck52244ff2014-05-01 21:27:37 -0700152 // TODO: dirty calculations, for now just do a full-screen inval
153 mDirty.setEmpty();
John Reckf9be7792014-05-02 18:21:16 -0700154 if (info.out.requiresUiRedraw) {
155 mSyncResult |= kSync_UIRedrawRequired;
156 }
John Reck52244ff2014-05-01 21:27:37 -0700157 }
John Reck860d1552014-04-11 19:15:05 -0700158 // If prepareTextures is false, we ran out of texture cache space
John Reckf9be7792014-05-02 18:21:16 -0700159 return !info.out.hasFunctors && info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700160}
161
162void DrawFrameTask::unblockUiThread() {
163 AutoMutex _lock(mLock);
164 mSignal.signal();
165}
166
John Reck668f0e32014-03-26 15:10:40 -0700167} /* namespace renderthread */
168} /* namespace uirenderer */
169} /* namespace android */