blob: 7ea358fc476e1999b998d00c4e399220e683d270 [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 Reck18f16e62014-05-02 16:46:41 -070033DrawFrameTask::DrawFrameTask()
34 : mRenderThread(NULL)
35 , mContext(NULL)
John Reckf9be7792014-05-02 18:21:16 -070036 , mFrameTimeNanos(0)
John Reckfe5e7b72014-05-23 17:42:28 -070037 , mRecordDurationNanos(0)
38 , mDensity(1.0f) // safe enough default
John Reckf9be7792014-05-02 18:21:16 -070039 , mSyncResult(kSync_OK) {
John Reck668f0e32014-03-26 15:10:40 -070040}
41
42DrawFrameTask::~DrawFrameTask() {
43}
44
John Reck18f16e62014-05-02 16:46:41 -070045void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) {
46 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070047 mContext = context;
48}
49
John Reck668f0e32014-03-26 15:10:40 -070050void DrawFrameTask::addLayer(DeferredLayerUpdater* layer) {
John Reck087bc0c2014-04-04 16:20:08 -070051 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to addLayer with!");
52
John Reck668f0e32014-03-26 15:10:40 -070053 mLayers.push(layer);
54}
55
56void DrawFrameTask::removeLayer(DeferredLayerUpdater* layer) {
57 for (size_t i = 0; i < mLayers.size(); i++) {
58 if (mLayers[i] == layer) {
59 mLayers.removeAt(i);
60 break;
61 }
62 }
63}
64
John Reck668f0e32014-03-26 15:10:40 -070065void DrawFrameTask::setDirty(int left, int top, int right, int bottom) {
66 mDirty.set(left, top, right, bottom);
67}
68
John Reckfe5e7b72014-05-23 17:42:28 -070069int DrawFrameTask::drawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos) {
John Reck668f0e32014-03-26 15:10:40 -070070 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
71
John Reckf9be7792014-05-02 18:21:16 -070072 mSyncResult = kSync_OK;
John Reck18f16e62014-05-02 16:46:41 -070073 mFrameTimeNanos = frameTimeNanos;
John Reckfe5e7b72014-05-23 17:42:28 -070074 mRecordDurationNanos = recordDurationNanos;
John Reck18f16e62014-05-02 16:46:41 -070075 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070076
77 // Reset the single-frame data
John Reck18f16e62014-05-02 16:46:41 -070078 mFrameTimeNanos = 0;
John Reckfe5e7b72014-05-23 17:42:28 -070079 mRecordDurationNanos = 0;
John Reck668f0e32014-03-26 15:10:40 -070080 mDirty.setEmpty();
John Reckf9be7792014-05-02 18:21:16 -070081
82 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070083}
84
John Reck18f16e62014-05-02 16:46:41 -070085void DrawFrameTask::postAndWait() {
John Reck087bc0c2014-04-04 16:20:08 -070086 AutoMutex _lock(mLock);
John Reck18f16e62014-05-02 16:46:41 -070087 mRenderThread->queue(this);
John Reck087bc0c2014-04-04 16:20:08 -070088 mSignal.wait(mLock);
89}
90
John Reck668f0e32014-03-26 15:10:40 -070091void DrawFrameTask::run() {
92 ATRACE_NAME("DrawFrame");
93
John Reckfe5e7b72014-05-23 17:42:28 -070094 mContext->profiler().setDensity(mDensity);
95 mContext->profiler().startFrame(mRecordDurationNanos);
96
John Recka5dda642014-05-22 15:43:54 -070097 bool canUnblockUiThread;
98 bool canDrawThisFrame;
99 {
100 TreeInfo info;
101 canUnblockUiThread = syncFrameState(info);
102 canDrawThisFrame = info.out.canDrawThisFrame;
103 }
John Reck668f0e32014-03-26 15:10:40 -0700104
105 // Grab a copy of everything we need
John Recke45b1fd2014-04-15 09:50:16 -0700106 Rect dirty(mDirty);
John Reck668f0e32014-03-26 15:10:40 -0700107 CanvasContext* context = mContext;
108
John Reck668f0e32014-03-26 15:10:40 -0700109 // From this point on anything in "this" is *UNSAFE TO ACCESS*
110 if (canUnblockUiThread) {
111 unblockUiThread();
112 }
113
John Recka5dda642014-05-22 15:43:54 -0700114 if (CC_LIKELY(canDrawThisFrame)) {
115 context->draw(&dirty);
116 }
John Reck668f0e32014-03-26 15:10:40 -0700117
118 if (!canUnblockUiThread) {
119 unblockUiThread();
120 }
121}
122
John Recke45b1fd2014-04-15 09:50:16 -0700123static void initTreeInfo(TreeInfo& info) {
John Reck860d1552014-04-11 19:15:05 -0700124 info.prepareTextures = true;
John Recke45b1fd2014-04-15 09:50:16 -0700125 info.performStagingPush = true;
126 info.evaluateAnimations = true;
John Reck860d1552014-04-11 19:15:05 -0700127}
128
John Recka5dda642014-05-22 15:43:54 -0700129bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700130 ATRACE_CALL();
John Reck18f16e62014-05-02 16:46:41 -0700131 mRenderThread->timeLord().vsyncReceived(mFrameTimeNanos);
John Reck860d1552014-04-11 19:15:05 -0700132 mContext->makeCurrent();
133 Caches::getInstance().textureCache.resetMarkInUse();
John Recke45b1fd2014-04-15 09:50:16 -0700134 initTreeInfo(info);
John Reckf9be7792014-05-02 18:21:16 -0700135 mContext->prepareDraw(&mLayers, info);
136 if (info.out.hasAnimations) {
John Reck52244ff2014-05-01 21:27:37 -0700137 // TODO: dirty calculations, for now just do a full-screen inval
138 mDirty.setEmpty();
John Reckf9be7792014-05-02 18:21:16 -0700139 if (info.out.requiresUiRedraw) {
140 mSyncResult |= kSync_UIRedrawRequired;
141 }
John Reck52244ff2014-05-01 21:27:37 -0700142 }
John Reck860d1552014-04-11 19:15:05 -0700143 // If prepareTextures is false, we ran out of texture cache space
John Reckf9be7792014-05-02 18:21:16 -0700144 return !info.out.hasFunctors && info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700145}
146
147void DrawFrameTask::unblockUiThread() {
148 AutoMutex _lock(mLock);
149 mSignal.signal();
150}
151
John Reck668f0e32014-03-26 15:10:40 -0700152} /* namespace renderthread */
153} /* namespace uirenderer */
154} /* namespace android */