blob: 198906ca845e7d916f49fc074875675f3ae431e5 [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
John Reck668f0e32014-03-26 15:10:40 -070017#include "DrawFrameTask.h"
18
19#include <utils/Log.h>
20#include <utils/Trace.h>
21
John Reckd72e0a32014-05-29 18:56:11 -070022#include "../DeferredLayerUpdater.h"
John Reck668f0e32014-03-26 15:10:40 -070023#include "../DisplayList.h"
24#include "../RenderNode.h"
25#include "CanvasContext.h"
26#include "RenderThread.h"
27
28namespace android {
29namespace uirenderer {
30namespace renderthread {
31
John Reck18f16e62014-05-02 16:46:41 -070032DrawFrameTask::DrawFrameTask()
Chris Craikd41c4d82015-01-05 15:51:13 -080033 : mRenderThread(nullptr)
34 , mContext(nullptr)
John Reckf9be7792014-05-02 18:21:16 -070035 , mSyncResult(kSync_OK) {
John Reck668f0e32014-03-26 15:10:40 -070036}
37
38DrawFrameTask::~DrawFrameTask() {
39}
40
John Reck18f16e62014-05-02 16:46:41 -070041void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) {
42 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070043 mContext = context;
44}
45
John Reckd72e0a32014-05-29 18:56:11 -070046void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
47 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to pushLayerUpdate with!");
John Reck087bc0c2014-04-04 16:20:08 -070048
John Reckd72e0a32014-05-29 18:56:11 -070049 for (size_t i = 0; i < mLayers.size(); i++) {
50 if (mLayers[i].get() == layer) {
51 return;
52 }
53 }
54 mLayers.push_back(layer);
John Reck668f0e32014-03-26 15:10:40 -070055}
56
John Reckd72e0a32014-05-29 18:56:11 -070057void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -070058 for (size_t i = 0; i < mLayers.size(); i++) {
John Reckd72e0a32014-05-29 18:56:11 -070059 if (mLayers[i].get() == layer) {
60 mLayers.erase(mLayers.begin() + i);
61 return;
John Reck668f0e32014-03-26 15:10:40 -070062 }
63 }
64}
65
John Reckba6adf62015-02-19 14:36:50 -080066int DrawFrameTask::drawFrame() {
John Reck668f0e32014-03-26 15:10:40 -070067 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
68
John Reckf9be7792014-05-02 18:21:16 -070069 mSyncResult = kSync_OK;
John Reckbe3fba02015-07-06 13:49:58 -070070 mSyncQueued = systemTime(CLOCK_MONOTONIC);
John Reck18f16e62014-05-02 16:46:41 -070071 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070072
John Reckf9be7792014-05-02 18:21:16 -070073 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070074}
75
John Reck18f16e62014-05-02 16:46:41 -070076void DrawFrameTask::postAndWait() {
John Reck087bc0c2014-04-04 16:20:08 -070077 AutoMutex _lock(mLock);
Chris Craik738ec3a2014-07-25 18:25:02 +000078 mRenderThread->queue(this);
79 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -070080}
81
John Reck668f0e32014-03-26 15:10:40 -070082void DrawFrameTask::run() {
83 ATRACE_NAME("DrawFrame");
84
John Recka5dda642014-05-22 15:43:54 -070085 bool canUnblockUiThread;
86 bool canDrawThisFrame;
87 {
John Reck3b202512014-06-23 13:13:08 -070088 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread->renderState());
John Recka5dda642014-05-22 15:43:54 -070089 canUnblockUiThread = syncFrameState(info);
90 canDrawThisFrame = info.out.canDrawThisFrame;
91 }
John Reck668f0e32014-03-26 15:10:40 -070092
93 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -070094 CanvasContext* context = mContext;
95
John Reck668f0e32014-03-26 15:10:40 -070096 // From this point on anything in "this" is *UNSAFE TO ACCESS*
97 if (canUnblockUiThread) {
98 unblockUiThread();
99 }
100
John Recka5dda642014-05-22 15:43:54 -0700101 if (CC_LIKELY(canDrawThisFrame)) {
John Recke4267ea2014-06-03 15:53:15 -0700102 context->draw();
John Recka5dda642014-05-22 15:43:54 -0700103 }
John Reck668f0e32014-03-26 15:10:40 -0700104
105 if (!canUnblockUiThread) {
106 unblockUiThread();
107 }
108}
109
John Recka5dda642014-05-22 15:43:54 -0700110bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700111 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700112 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
John Reckc87be992015-02-20 10:57:22 -0800113 mRenderThread->timeLord().vsyncReceived(vsync);
John Reck860d1552014-04-11 19:15:05 -0700114 mContext->makeCurrent();
John Reck00e79c92015-07-21 10:23:59 -0700115 Caches::getInstance().textureCache.resetMarkInUse(mContext);
John Reckd72e0a32014-05-29 18:56:11 -0700116
117 for (size_t i = 0; i < mLayers.size(); i++) {
John Reck68bfe0a2014-06-24 15:34:58 -0700118 mContext->processLayerUpdate(mLayers[i].get());
John Reckd72e0a32014-05-29 18:56:11 -0700119 }
120 mLayers.clear();
John Reckbe3fba02015-07-06 13:49:58 -0700121 mContext->prepareTree(info, mFrameInfo, mSyncQueued);
John Reckd72e0a32014-05-29 18:56:11 -0700122
John Reckaa95a882014-11-07 11:02:07 -0800123 // This is after the prepareTree so that any pending operations
124 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
125 if (CC_UNLIKELY(!mContext->hasSurface())) {
126 mSyncResult |= kSync_LostSurfaceRewardIfFound;
127 }
128
John Reckf9be7792014-05-02 18:21:16 -0700129 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700130 if (info.out.requiresUiRedraw) {
131 mSyncResult |= kSync_UIRedrawRequired;
132 }
John Reck52244ff2014-05-01 21:27:37 -0700133 }
John Reck860d1552014-04-11 19:15:05 -0700134 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700135 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700136}
137
138void DrawFrameTask::unblockUiThread() {
139 AutoMutex _lock(mLock);
140 mSignal.signal();
141}
142
John Reck668f0e32014-03-26 15:10:40 -0700143} /* namespace renderthread */
144} /* namespace uirenderer */
145} /* namespace android */