blob: dd34e095738d40dc4f0d1aa438f5dfa67d41302e [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 Reckfe5e7b72014-05-23 17:42:28 -070071int DrawFrameTask::drawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos) {
John Reck668f0e32014-03-26 15:10:40 -070072 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
73
John Reckf9be7792014-05-02 18:21:16 -070074 mSyncResult = kSync_OK;
John Reck18f16e62014-05-02 16:46:41 -070075 mFrameTimeNanos = frameTimeNanos;
John Reckfe5e7b72014-05-23 17:42:28 -070076 mRecordDurationNanos = recordDurationNanos;
John Reck18f16e62014-05-02 16:46:41 -070077 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070078
79 // Reset the single-frame data
John Reck18f16e62014-05-02 16:46:41 -070080 mFrameTimeNanos = 0;
John Reckfe5e7b72014-05-23 17:42:28 -070081 mRecordDurationNanos = 0;
John Reckf9be7792014-05-02 18:21:16 -070082
83 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070084}
85
John Reck18f16e62014-05-02 16:46:41 -070086void DrawFrameTask::postAndWait() {
John Reck087bc0c2014-04-04 16:20:08 -070087 AutoMutex _lock(mLock);
Chris Craik738ec3a2014-07-25 18:25:02 +000088 mRenderThread->queue(this);
89 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -070090}
91
John Reck668f0e32014-03-26 15:10:40 -070092void DrawFrameTask::run() {
93 ATRACE_NAME("DrawFrame");
94
John Reckfe5e7b72014-05-23 17:42:28 -070095 mContext->profiler().setDensity(mDensity);
96 mContext->profiler().startFrame(mRecordDurationNanos);
97
John Recka5dda642014-05-22 15:43:54 -070098 bool canUnblockUiThread;
99 bool canDrawThisFrame;
100 {
John Reck3b202512014-06-23 13:13:08 -0700101 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread->renderState());
John Recka5dda642014-05-22 15:43:54 -0700102 canUnblockUiThread = syncFrameState(info);
103 canDrawThisFrame = info.out.canDrawThisFrame;
104 }
John Reck668f0e32014-03-26 15:10:40 -0700105
106 // Grab a copy of everything we need
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)) {
John Recke4267ea2014-06-03 15:53:15 -0700115 context->draw();
John Recka5dda642014-05-22 15:43:54 -0700116 }
John Reck668f0e32014-03-26 15:10:40 -0700117
118 if (!canUnblockUiThread) {
119 unblockUiThread();
120 }
121}
122
John Recka5dda642014-05-22 15:43:54 -0700123bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700124 ATRACE_CALL();
John Reck18f16e62014-05-02 16:46:41 -0700125 mRenderThread->timeLord().vsyncReceived(mFrameTimeNanos);
John Reck860d1552014-04-11 19:15:05 -0700126 mContext->makeCurrent();
127 Caches::getInstance().textureCache.resetMarkInUse();
John Reckd72e0a32014-05-29 18:56:11 -0700128
129 for (size_t i = 0; i < mLayers.size(); i++) {
John Reck68bfe0a2014-06-24 15:34:58 -0700130 mContext->processLayerUpdate(mLayers[i].get());
John Reckd72e0a32014-05-29 18:56:11 -0700131 }
132 mLayers.clear();
John Reckd72e0a32014-05-29 18:56:11 -0700133 mContext->prepareTree(info);
134
John Reckf9be7792014-05-02 18:21:16 -0700135 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700136 if (info.out.requiresUiRedraw) {
137 mSyncResult |= kSync_UIRedrawRequired;
138 }
John Reck52244ff2014-05-01 21:27:37 -0700139 }
John Reck860d1552014-04-11 19:15:05 -0700140 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700141 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700142}
143
144void DrawFrameTask::unblockUiThread() {
145 AutoMutex _lock(mLock);
146 mSignal.signal();
147}
148
John Reck668f0e32014-03-26 15:10:40 -0700149} /* namespace renderthread */
150} /* namespace uirenderer */
151} /* namespace android */