blob: f48ee414871fe0e13b65e79fd994a4025099612c [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()
Chris Craikd41c4d82015-01-05 15:51:13 -080035 : mRenderThread(nullptr)
36 , mContext(nullptr)
John Reckfe5e7b72014-05-23 17:42:28 -070037 , mDensity(1.0f) // safe enough default
John Reckf9be7792014-05-02 18:21:16 -070038 , mSyncResult(kSync_OK) {
John Reck668f0e32014-03-26 15:10:40 -070039}
40
41DrawFrameTask::~DrawFrameTask() {
42}
43
John Reck18f16e62014-05-02 16:46:41 -070044void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) {
45 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070046 mContext = context;
47}
48
John Reckd72e0a32014-05-29 18:56:11 -070049void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
50 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to pushLayerUpdate with!");
John Reck087bc0c2014-04-04 16:20:08 -070051
John Reckd72e0a32014-05-29 18:56:11 -070052 for (size_t i = 0; i < mLayers.size(); i++) {
53 if (mLayers[i].get() == layer) {
54 return;
55 }
56 }
57 mLayers.push_back(layer);
John Reck668f0e32014-03-26 15:10:40 -070058}
59
John Reckd72e0a32014-05-29 18:56:11 -070060void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -070061 for (size_t i = 0; i < mLayers.size(); i++) {
John Reckd72e0a32014-05-29 18:56:11 -070062 if (mLayers[i].get() == layer) {
63 mLayers.erase(mLayers.begin() + i);
64 return;
John Reck668f0e32014-03-26 15:10:40 -070065 }
66 }
67}
68
John Reckba6adf62015-02-19 14:36:50 -080069int DrawFrameTask::drawFrame() {
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 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070074
John Reckf9be7792014-05-02 18:21:16 -070075 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070076}
77
John Reck18f16e62014-05-02 16:46:41 -070078void DrawFrameTask::postAndWait() {
John Reck087bc0c2014-04-04 16:20:08 -070079 AutoMutex _lock(mLock);
Chris Craik738ec3a2014-07-25 18:25:02 +000080 mRenderThread->queue(this);
81 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -070082}
83
John Reck668f0e32014-03-26 15:10:40 -070084void DrawFrameTask::run() {
85 ATRACE_NAME("DrawFrame");
86
John Reckfe5e7b72014-05-23 17:42:28 -070087 mContext->profiler().setDensity(mDensity);
John Reckba6adf62015-02-19 14:36:50 -080088 mContext->profiler().startFrame();
John Reckfe5e7b72014-05-23 17:42:28 -070089
John Recka5dda642014-05-22 15:43:54 -070090 bool canUnblockUiThread;
91 bool canDrawThisFrame;
92 {
John Reck3b202512014-06-23 13:13:08 -070093 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread->renderState());
John Recka5dda642014-05-22 15:43:54 -070094 canUnblockUiThread = syncFrameState(info);
95 canDrawThisFrame = info.out.canDrawThisFrame;
96 }
John Reck668f0e32014-03-26 15:10:40 -070097
98 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -070099 CanvasContext* context = mContext;
100
John Reck668f0e32014-03-26 15:10:40 -0700101 // From this point on anything in "this" is *UNSAFE TO ACCESS*
102 if (canUnblockUiThread) {
103 unblockUiThread();
104 }
105
John Recka5dda642014-05-22 15:43:54 -0700106 if (CC_LIKELY(canDrawThisFrame)) {
John Recke4267ea2014-06-03 15:53:15 -0700107 context->draw();
John Recka5dda642014-05-22 15:43:54 -0700108 }
John Reck668f0e32014-03-26 15:10:40 -0700109
110 if (!canUnblockUiThread) {
111 unblockUiThread();
112 }
113}
114
John Recka5dda642014-05-22 15:43:54 -0700115bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700116 ATRACE_CALL();
John Reckc87be992015-02-20 10:57:22 -0800117 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::kVsync)];
118 mRenderThread->timeLord().vsyncReceived(vsync);
John Reck860d1552014-04-11 19:15:05 -0700119 mContext->makeCurrent();
120 Caches::getInstance().textureCache.resetMarkInUse();
John Reckd72e0a32014-05-29 18:56:11 -0700121
122 for (size_t i = 0; i < mLayers.size(); i++) {
John Reck68bfe0a2014-06-24 15:34:58 -0700123 mContext->processLayerUpdate(mLayers[i].get());
John Reckd72e0a32014-05-29 18:56:11 -0700124 }
125 mLayers.clear();
John Reckba6adf62015-02-19 14:36:50 -0800126 mContext->prepareTree(info, mFrameInfo);
John Reckd72e0a32014-05-29 18:56:11 -0700127
John Reckaa95a882014-11-07 11:02:07 -0800128 // This is after the prepareTree so that any pending operations
129 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
130 if (CC_UNLIKELY(!mContext->hasSurface())) {
131 mSyncResult |= kSync_LostSurfaceRewardIfFound;
132 }
133
John Reckf9be7792014-05-02 18:21:16 -0700134 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700135 if (info.out.requiresUiRedraw) {
136 mSyncResult |= kSync_UIRedrawRequired;
137 }
John Reck52244ff2014-05-01 21:27:37 -0700138 }
John Reck860d1552014-04-11 19:15:05 -0700139 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700140 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700141}
142
143void DrawFrameTask::unblockUiThread() {
144 AutoMutex _lock(mLock);
145 mSignal.signal();
146}
147
John Reck668f0e32014-03-26 15:10:40 -0700148} /* namespace renderthread */
149} /* namespace uirenderer */
150} /* namespace android */