blob: 35391b27cc961fa4ff6a8ab40c4ae658b407f090 [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 Reckf9be7792014-05-02 18:21:16 -070037 , mSyncResult(kSync_OK) {
John Reck668f0e32014-03-26 15:10:40 -070038}
39
40DrawFrameTask::~DrawFrameTask() {
41}
42
John Reck18f16e62014-05-02 16:46:41 -070043void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) {
44 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070045 mContext = context;
46}
47
John Reckd72e0a32014-05-29 18:56:11 -070048void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
49 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to pushLayerUpdate with!");
John Reck087bc0c2014-04-04 16:20:08 -070050
John Reckd72e0a32014-05-29 18:56:11 -070051 for (size_t i = 0; i < mLayers.size(); i++) {
52 if (mLayers[i].get() == layer) {
53 return;
54 }
55 }
56 mLayers.push_back(layer);
John Reck668f0e32014-03-26 15:10:40 -070057}
58
John Reckd72e0a32014-05-29 18:56:11 -070059void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -070060 for (size_t i = 0; i < mLayers.size(); i++) {
John Reckd72e0a32014-05-29 18:56:11 -070061 if (mLayers[i].get() == layer) {
62 mLayers.erase(mLayers.begin() + i);
63 return;
John Reck668f0e32014-03-26 15:10:40 -070064 }
65 }
66}
67
John Reckba6adf62015-02-19 14:36:50 -080068int DrawFrameTask::drawFrame() {
John Reck668f0e32014-03-26 15:10:40 -070069 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
70
John Reckf9be7792014-05-02 18:21:16 -070071 mSyncResult = kSync_OK;
John Reck18f16e62014-05-02 16:46:41 -070072 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070073
John Reckf9be7792014-05-02 18:21:16 -070074 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070075}
76
John Reck18f16e62014-05-02 16:46:41 -070077void DrawFrameTask::postAndWait() {
John Reck087bc0c2014-04-04 16:20:08 -070078 AutoMutex _lock(mLock);
Chris Craik738ec3a2014-07-25 18:25:02 +000079 mRenderThread->queue(this);
80 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -070081}
82
John Reck668f0e32014-03-26 15:10:40 -070083void DrawFrameTask::run() {
84 ATRACE_NAME("DrawFrame");
85
John Reckba6adf62015-02-19 14:36:50 -080086 mContext->profiler().startFrame();
John Reckfe5e7b72014-05-23 17:42:28 -070087
John Recka5dda642014-05-22 15:43:54 -070088 bool canUnblockUiThread;
89 bool canDrawThisFrame;
90 {
John Reck3b202512014-06-23 13:13:08 -070091 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread->renderState());
John Recka5dda642014-05-22 15:43:54 -070092 canUnblockUiThread = syncFrameState(info);
93 canDrawThisFrame = info.out.canDrawThisFrame;
94 }
John Reck668f0e32014-03-26 15:10:40 -070095
96 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -070097 CanvasContext* context = mContext;
98
John Reck668f0e32014-03-26 15:10:40 -070099 // From this point on anything in "this" is *UNSAFE TO ACCESS*
100 if (canUnblockUiThread) {
101 unblockUiThread();
102 }
103
John Recka5dda642014-05-22 15:43:54 -0700104 if (CC_LIKELY(canDrawThisFrame)) {
John Recke4267ea2014-06-03 15:53:15 -0700105 context->draw();
John Recka5dda642014-05-22 15:43:54 -0700106 }
John Reck668f0e32014-03-26 15:10:40 -0700107
108 if (!canUnblockUiThread) {
109 unblockUiThread();
110 }
111}
112
John Recka5dda642014-05-22 15:43:54 -0700113bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700114 ATRACE_CALL();
John Reckc87be992015-02-20 10:57:22 -0800115 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::kVsync)];
116 mRenderThread->timeLord().vsyncReceived(vsync);
John Reck860d1552014-04-11 19:15:05 -0700117 mContext->makeCurrent();
118 Caches::getInstance().textureCache.resetMarkInUse();
John Reckd72e0a32014-05-29 18:56:11 -0700119
120 for (size_t i = 0; i < mLayers.size(); i++) {
John Reck68bfe0a2014-06-24 15:34:58 -0700121 mContext->processLayerUpdate(mLayers[i].get());
John Reckd72e0a32014-05-29 18:56:11 -0700122 }
123 mLayers.clear();
John Reckba6adf62015-02-19 14:36:50 -0800124 mContext->prepareTree(info, mFrameInfo);
John Reckd72e0a32014-05-29 18:56:11 -0700125
John Reckaa95a882014-11-07 11:02:07 -0800126 // This is after the prepareTree so that any pending operations
127 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
128 if (CC_UNLIKELY(!mContext->hasSurface())) {
129 mSyncResult |= kSync_LostSurfaceRewardIfFound;
130 }
131
John Reckf9be7792014-05-02 18:21:16 -0700132 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700133 if (info.out.requiresUiRedraw) {
134 mSyncResult |= kSync_UIRedrawRequired;
135 }
John Reck52244ff2014-05-01 21:27:37 -0700136 }
John Reck860d1552014-04-11 19:15:05 -0700137 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700138 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700139}
140
141void DrawFrameTask::unblockUiThread() {
142 AutoMutex _lock(mLock);
143 mSignal.signal();
144}
145
John Reck668f0e32014-03-26 15:10:40 -0700146} /* namespace renderthread */
147} /* namespace uirenderer */
148} /* namespace android */