blob: 008e2977b832333e4a301fc8fc22ca2640e28cca [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 Recka5dda642014-05-22 15:43:54 -070086 bool canUnblockUiThread;
87 bool canDrawThisFrame;
88 {
John Reck3b202512014-06-23 13:13:08 -070089 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread->renderState());
John Recka5dda642014-05-22 15:43:54 -070090 canUnblockUiThread = syncFrameState(info);
91 canDrawThisFrame = info.out.canDrawThisFrame;
92 }
John Reck668f0e32014-03-26 15:10:40 -070093
94 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -070095 CanvasContext* context = mContext;
96
John Reck668f0e32014-03-26 15:10:40 -070097 // From this point on anything in "this" is *UNSAFE TO ACCESS*
98 if (canUnblockUiThread) {
99 unblockUiThread();
100 }
101
John Recka5dda642014-05-22 15:43:54 -0700102 if (CC_LIKELY(canDrawThisFrame)) {
John Recke4267ea2014-06-03 15:53:15 -0700103 context->draw();
John Recka5dda642014-05-22 15:43:54 -0700104 }
John Reck668f0e32014-03-26 15:10:40 -0700105
106 if (!canUnblockUiThread) {
107 unblockUiThread();
108 }
109}
110
John Recka5dda642014-05-22 15:43:54 -0700111bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700112 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700113 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
John Reckc87be992015-02-20 10:57:22 -0800114 mRenderThread->timeLord().vsyncReceived(vsync);
John Reck860d1552014-04-11 19:15:05 -0700115 mContext->makeCurrent();
116 Caches::getInstance().textureCache.resetMarkInUse();
John Reckd72e0a32014-05-29 18:56:11 -0700117
118 for (size_t i = 0; i < mLayers.size(); i++) {
John Reck68bfe0a2014-06-24 15:34:58 -0700119 mContext->processLayerUpdate(mLayers[i].get());
John Reckd72e0a32014-05-29 18:56:11 -0700120 }
121 mLayers.clear();
John Reckba6adf62015-02-19 14:36:50 -0800122 mContext->prepareTree(info, mFrameInfo);
John Reckd72e0a32014-05-29 18:56:11 -0700123
John Reckaa95a882014-11-07 11:02:07 -0800124 // This is after the prepareTree so that any pending operations
125 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
126 if (CC_UNLIKELY(!mContext->hasSurface())) {
127 mSyncResult |= kSync_LostSurfaceRewardIfFound;
128 }
129
John Reckf9be7792014-05-02 18:21:16 -0700130 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700131 if (info.out.requiresUiRedraw) {
132 mSyncResult |= kSync_UIRedrawRequired;
133 }
John Reck52244ff2014-05-01 21:27:37 -0700134 }
John Reck860d1552014-04-11 19:15:05 -0700135 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700136 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700137}
138
139void DrawFrameTask::unblockUiThread() {
140 AutoMutex _lock(mLock);
141 mSignal.signal();
142}
143
John Reck668f0e32014-03-26 15:10:40 -0700144} /* namespace renderthread */
145} /* namespace uirenderer */
146} /* namespace android */