blob: 0a94678a5c83b06094968377c3a2fd53bd4a2f6f [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 Reckf138b172017-09-08 11:00:42 -070035 , mContentDrawBounds(0, 0, 0, 0)
John Reck9a17da82016-04-18 11:17:52 -070036 , mSyncResult(SyncResult::OK) {
John Reck668f0e32014-03-26 15:10:40 -070037}
38
39DrawFrameTask::~DrawFrameTask() {
40}
41
Skuhneea7a7fb2015-08-28 07:10:31 -070042void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context,
43 RenderNode* targetNode) {
John Reck18f16e62014-05-02 16:46:41 -070044 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070045 mContext = context;
Skuhneea7a7fb2015-08-28 07:10:31 -070046 mTargetNode = targetNode;
John Reck668f0e32014-03-26 15:10:40 -070047}
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 Reck2de950d2017-01-25 10:58:30 -080069int DrawFrameTask::drawFrame() {
John Reck668f0e32014-03-26 15:10:40 -070070 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
71
John Reck9a17da82016-04-18 11:17:52 -070072 mSyncResult = SyncResult::OK;
John Reckbe3fba02015-07-06 13:49:58 -070073 mSyncQueued = systemTime(CLOCK_MONOTONIC);
John Reck18f16e62014-05-02 16:46:41 -070074 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070075
John Reckf9be7792014-05-02 18:21:16 -070076 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070077}
78
John Reck18f16e62014-05-02 16:46:41 -070079void DrawFrameTask::postAndWait() {
John Reck087bc0c2014-04-04 16:20:08 -070080 AutoMutex _lock(mLock);
John Reckf8441e62017-10-23 13:10:41 -070081 mRenderThread->queue().post([this]() { run(); });
Chris Craik738ec3a2014-07-25 18:25:02 +000082 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -070083}
84
John Reck668f0e32014-03-26 15:10:40 -070085void DrawFrameTask::run() {
86 ATRACE_NAME("DrawFrame");
87
John Recka5dda642014-05-22 15:43:54 -070088 bool canUnblockUiThread;
89 bool canDrawThisFrame;
90 {
Chris Craike2e53a72015-10-28 15:55:40 -070091 TreeInfo info(TreeInfo::MODE_FULL, *mContext);
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();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700106 } else {
107 // wait on fences so tasks don't overlap next frame
108 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700109 }
John Reck668f0e32014-03-26 15:10:40 -0700110
111 if (!canUnblockUiThread) {
112 unblockUiThread();
113 }
114}
115
John Recka5dda642014-05-22 15:43:54 -0700116bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700117 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700118 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
John Reckc87be992015-02-20 10:57:22 -0800119 mRenderThread->timeLord().vsyncReceived(vsync);
John Reck8afcc762016-04-13 10:24:06 -0700120 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400121 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700122
123 for (size_t i = 0; i < mLayers.size(); i++) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800124 mLayers[i]->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700125 }
126 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700127 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700128 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700129
John Reckaa95a882014-11-07 11:02:07 -0800130 // This is after the prepareTree so that any pending operations
131 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
John Reck8afcc762016-04-13 10:24:06 -0700132 if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
John Reck9a17da82016-04-18 11:17:52 -0700133 if (!mContext->hasSurface()) {
134 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
135 } else {
136 // If we have a surface but can't draw we must be stopped
137 mSyncResult |= SyncResult::ContextIsStopped;
138 }
John Reck8afcc762016-04-13 10:24:06 -0700139 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800140 }
141
John Reckf9be7792014-05-02 18:21:16 -0700142 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700143 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700144 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700145 }
John Reck52244ff2014-05-01 21:27:37 -0700146 }
John Reck860d1552014-04-11 19:15:05 -0700147 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700148 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700149}
150
151void DrawFrameTask::unblockUiThread() {
152 AutoMutex _lock(mLock);
153 mSignal.signal();
154}
155
John Reck668f0e32014-03-26 15:10:40 -0700156} /* namespace renderthread */
157} /* namespace uirenderer */
158} /* namespace android */