blob: 3408ffda3f9de37f48101d640211fa65e9637e65 [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>
Ady Abraham150816c2021-03-01 10:46:40 -080020#include <utils/TraceUtils.h>
John Reck668f0e32014-03-26 15:10:40 -070021
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 Reck1bcacfd2017-11-03 10:12:19 -070036 , mSyncResult(SyncResult::OK) {}
John Reck668f0e32014-03-26 15:10:40 -070037
John Reck1bcacfd2017-11-03 10:12:19 -070038DrawFrameTask::~DrawFrameTask() {}
John Reck668f0e32014-03-26 15:10:40 -070039
Skuhneea7a7fb2015-08-28 07:10:31 -070040void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context,
John Reck1bcacfd2017-11-03 10:12:19 -070041 RenderNode* targetNode) {
John Reck18f16e62014-05-02 16:46:41 -070042 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070043 mContext = context;
Skuhneea7a7fb2015-08-28 07:10:31 -070044 mTargetNode = targetNode;
John Reck668f0e32014-03-26 15:10:40 -070045}
46
John Reckd72e0a32014-05-29 18:56:11 -070047void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -070048 LOG_ALWAYS_FATAL_IF(!mContext,
49 "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 Reck2de950d2017-01-25 10:58:30 -080068int DrawFrameTask::drawFrame() {
John Reck668f0e32014-03-26 15:10:40 -070069 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
70
John Reck9a17da82016-04-18 11:17:52 -070071 mSyncResult = SyncResult::OK;
Jerome Gaillarde218c692019-06-14 12:58:57 +010072 mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC);
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);
John Reckf8441e62017-10-23 13:10:41 -070080 mRenderThread->queue().post([this]() { run(); });
Chris Craik738ec3a2014-07-25 18:25:02 +000081 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -070082}
83
John Reck668f0e32014-03-26 15:10:40 -070084void DrawFrameTask::run() {
Ady Abraham150816c2021-03-01 10:46:40 -080085 const int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
86 ATRACE_FORMAT("DrawFrames %" PRId64, vsyncId);
John Reck668f0e32014-03-26 15:10:40 -070087
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;
John Reckcc2eee82018-05-17 10:44:00 -070094
95 if (mFrameCompleteCallback) {
96 mContext->addFrameCompleteListener(std::move(mFrameCompleteCallback));
97 mFrameCompleteCallback = nullptr;
98 }
John Recka5dda642014-05-22 15:43:54 -070099 }
John Reck668f0e32014-03-26 15:10:40 -0700100
101 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -0700102 CanvasContext* context = mContext;
Mihai Popa95688002018-02-23 16:10:11 +0000103 std::function<void(int64_t)> callback = std::move(mFrameCallback);
Jorim Jaggi7a5addd2018-04-26 23:23:29 +0200104 mFrameCallback = nullptr;
John Reck668f0e32014-03-26 15:10:40 -0700105
John Reck668f0e32014-03-26 15:10:40 -0700106 // From this point on anything in "this" is *UNSAFE TO ACCESS*
107 if (canUnblockUiThread) {
108 unblockUiThread();
109 }
110
Mihai Popa95688002018-02-23 16:10:11 +0000111 // Even if we aren't drawing this vsync pulse the next frame number will still be accurate
112 if (CC_UNLIKELY(callback)) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700113 context->enqueueFrameWork(
114 [callback, frameNr = context->getFrameNumber()]() { callback(frameNr); });
Mihai Popa95688002018-02-23 16:10:11 +0000115 }
116
John Recka5dda642014-05-22 15:43:54 -0700117 if (CC_LIKELY(canDrawThisFrame)) {
John Recke4267ea2014-06-03 15:53:15 -0700118 context->draw();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700119 } else {
120 // wait on fences so tasks don't overlap next frame
121 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700122 }
John Reck668f0e32014-03-26 15:10:40 -0700123
124 if (!canUnblockUiThread) {
125 unblockUiThread();
126 }
127}
128
John Recka5dda642014-05-22 15:43:54 -0700129bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700130 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700131 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700132 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
133 int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
Ady Abrahamdfb13982020-10-05 17:59:09 -0700134 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
135 mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline);
John Reck8afcc762016-04-13 10:24:06 -0700136 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400137 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700138
139 for (size_t i = 0; i < mLayers.size(); i++) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800140 mLayers[i]->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700141 }
142 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700143 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700144 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700145
John Reckaa95a882014-11-07 11:02:07 -0800146 // This is after the prepareTree so that any pending operations
147 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
John Reck8afcc762016-04-13 10:24:06 -0700148 if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
John Reck9a17da82016-04-18 11:17:52 -0700149 if (!mContext->hasSurface()) {
150 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
151 } else {
152 // If we have a surface but can't draw we must be stopped
153 mSyncResult |= SyncResult::ContextIsStopped;
154 }
John Reck8afcc762016-04-13 10:24:06 -0700155 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800156 }
157
John Reckf9be7792014-05-02 18:21:16 -0700158 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700159 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700160 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700161 }
John Reck52244ff2014-05-01 21:27:37 -0700162 }
John Reckcc2eee82018-05-17 10:44:00 -0700163 if (!info.out.canDrawThisFrame) {
164 mSyncResult |= SyncResult::FrameDropped;
165 }
John Reck860d1552014-04-11 19:15:05 -0700166 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700167 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700168}
169
170void DrawFrameTask::unblockUiThread() {
171 AutoMutex _lock(mLock);
172 mSignal.signal();
173}
174
John Reck668f0e32014-03-26 15:10:40 -0700175} /* namespace renderthread */
176} /* namespace uirenderer */
177} /* namespace android */