blob: fab2f46e91c3fad5209fbaf17929136dde3de19c [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
rnleece9762b2021-05-21 15:40:53 -070019#include <gui/TraceUtils.h>
John Reck668f0e32014-03-26 15:10:40 -070020#include <utils/Log.h>
Matt Buckley864ab952022-10-18 23:03:59 +000021
Bo Liu4d0f1f12021-05-06 16:49:40 -040022#include <algorithm>
John Reck668f0e32014-03-26 15:10:40 -070023
John Reckd72e0a32014-05-29 18:56:11 -070024#include "../DeferredLayerUpdater.h"
John Reck668f0e32014-03-26 15:10:40 -070025#include "../DisplayList.h"
Bo Liu027b2182021-03-18 16:50:38 -040026#include "../Properties.h"
John Reck668f0e32014-03-26 15:10:40 -070027#include "../RenderNode.h"
28#include "CanvasContext.h"
Nader Jawad7612c612022-11-15 10:55:54 -080029#include "HardwareBufferRenderParams.h"
John Reck668f0e32014-03-26 15:10:40 -070030#include "RenderThread.h"
31
32namespace android {
33namespace uirenderer {
34namespace renderthread {
35
John Reck18f16e62014-05-02 16:46:41 -070036DrawFrameTask::DrawFrameTask()
Chris Craikd41c4d82015-01-05 15:51:13 -080037 : mRenderThread(nullptr)
38 , mContext(nullptr)
John Reckf138b172017-09-08 11:00:42 -070039 , mContentDrawBounds(0, 0, 0, 0)
John Reck1bcacfd2017-11-03 10:12:19 -070040 , mSyncResult(SyncResult::OK) {}
John Reck668f0e32014-03-26 15:10:40 -070041
John Reck1bcacfd2017-11-03 10:12:19 -070042DrawFrameTask::~DrawFrameTask() {}
John Reck668f0e32014-03-26 15:10:40 -070043
Matt Buckleye9023cf2022-11-23 22:39:25 +000044void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context,
45 RenderNode* targetNode) {
John Reck18f16e62014-05-02 16:46:41 -070046 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070047 mContext = context;
Skuhneea7a7fb2015-08-28 07:10:31 -070048 mTargetNode = targetNode;
Bo Liu027b2182021-03-18 16:50:38 -040049}
50
John Reckd72e0a32014-05-29 18:56:11 -070051void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -070052 LOG_ALWAYS_FATAL_IF(!mContext,
53 "Lifecycle violation, there's no context to pushLayerUpdate with!");
John Reck087bc0c2014-04-04 16:20:08 -070054
John Reckd72e0a32014-05-29 18:56:11 -070055 for (size_t i = 0; i < mLayers.size(); i++) {
56 if (mLayers[i].get() == layer) {
57 return;
58 }
59 }
60 mLayers.push_back(layer);
John Reck668f0e32014-03-26 15:10:40 -070061}
62
John Reckd72e0a32014-05-29 18:56:11 -070063void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -070064 for (size_t i = 0; i < mLayers.size(); i++) {
John Reckd72e0a32014-05-29 18:56:11 -070065 if (mLayers[i].get() == layer) {
66 mLayers.erase(mLayers.begin() + i);
67 return;
John Reck668f0e32014-03-26 15:10:40 -070068 }
69 }
70}
71
John Reck2de950d2017-01-25 10:58:30 -080072int DrawFrameTask::drawFrame() {
John Reck668f0e32014-03-26 15:10:40 -070073 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
74
John Reck9a17da82016-04-18 11:17:52 -070075 mSyncResult = SyncResult::OK;
Jerome Gaillarde218c692019-06-14 12:58:57 +010076 mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck18f16e62014-05-02 16:46:41 -070077 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070078
John Reckf9be7792014-05-02 18:21:16 -070079 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070080}
81
John Reck18f16e62014-05-02 16:46:41 -070082void DrawFrameTask::postAndWait() {
Jimmy Shiu06d91392022-03-15 19:35:45 +080083 ATRACE_CALL();
John Reck087bc0c2014-04-04 16:20:08 -070084 AutoMutex _lock(mLock);
John Reckf8441e62017-10-23 13:10:41 -070085 mRenderThread->queue().post([this]() { run(); });
Chris Craik738ec3a2014-07-25 18:25:02 +000086 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -070087}
88
John Reck668f0e32014-03-26 15:10:40 -070089void DrawFrameTask::run() {
Ady Abraham150816c2021-03-01 10:46:40 -080090 const int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
91 ATRACE_FORMAT("DrawFrames %" PRId64, vsyncId);
Matt Buckleye9023cf2022-11-23 22:39:25 +000092
93 mContext->setSyncDelayDuration(systemTime(SYSTEM_TIME_MONOTONIC) - mSyncQueued);
John Reck55887762023-01-25 16:51:18 -050094 mContext->setTargetSdrHdrRatio(mRenderSdrHdrRatio);
John Reck668f0e32014-03-26 15:10:40 -070095
Nader Jawad7612c612022-11-15 10:55:54 -080096 auto hardwareBufferParams = mHardwareBufferParams;
97 mContext->setHardwareBufferRenderParams(hardwareBufferParams);
98 IRenderPipeline* pipeline = mContext->getRenderPipeline();
John Recka5dda642014-05-22 15:43:54 -070099 bool canUnblockUiThread;
100 bool canDrawThisFrame;
101 {
Chris Craike2e53a72015-10-28 15:55:40 -0700102 TreeInfo info(TreeInfo::MODE_FULL, *mContext);
chaviwadba0b12022-03-18 17:42:15 -0500103 info.forceDrawFrame = mForceDrawFrame;
104 mForceDrawFrame = false;
John Recka5dda642014-05-22 15:43:54 -0700105 canUnblockUiThread = syncFrameState(info);
106 canDrawThisFrame = info.out.canDrawThisFrame;
John Reckcc2eee82018-05-17 10:44:00 -0700107
chaviw9c137532021-08-20 12:15:48 -0500108 if (mFrameCommitCallback) {
109 mContext->addFrameCommitListener(std::move(mFrameCommitCallback));
110 mFrameCommitCallback = nullptr;
John Reckcc2eee82018-05-17 10:44:00 -0700111 }
John Recka5dda642014-05-22 15:43:54 -0700112 }
John Reck668f0e32014-03-26 15:10:40 -0700113
114 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -0700115 CanvasContext* context = mContext;
chaviwb6803712021-12-13 15:46:29 -0600116 std::function<std::function<void(bool)>(int32_t, int64_t)> frameCallback =
117 std::move(mFrameCallback);
chaviw9c137532021-08-20 12:15:48 -0500118 std::function<void()> frameCompleteCallback = std::move(mFrameCompleteCallback);
Jorim Jaggi7a5addd2018-04-26 23:23:29 +0200119 mFrameCallback = nullptr;
chaviw9c137532021-08-20 12:15:48 -0500120 mFrameCompleteCallback = nullptr;
John Reck668f0e32014-03-26 15:10:40 -0700121
John Reck668f0e32014-03-26 15:10:40 -0700122 // From this point on anything in "this" is *UNSAFE TO ACCESS*
123 if (canUnblockUiThread) {
124 unblockUiThread();
125 }
126
Mihai Popa95688002018-02-23 16:10:11 +0000127 // Even if we aren't drawing this vsync pulse the next frame number will still be accurate
chaviw9c137532021-08-20 12:15:48 -0500128 if (CC_UNLIKELY(frameCallback)) {
chaviwb6803712021-12-13 15:46:29 -0600129 context->enqueueFrameWork([frameCallback, context, syncResult = mSyncResult,
130 frameNr = context->getFrameNumber()]() {
Matt Buckleye9023cf2022-11-23 22:39:25 +0000131 auto frameCommitCallback = frameCallback(syncResult, frameNr);
chaviwb6803712021-12-13 15:46:29 -0600132 if (frameCommitCallback) {
133 context->addFrameCommitListener(std::move(frameCommitCallback));
134 }
135 });
Mihai Popa95688002018-02-23 16:10:11 +0000136 }
137
John Recka5dda642014-05-22 15:43:54 -0700138 if (CC_LIKELY(canDrawThisFrame)) {
Matt Buckleye9023cf2022-11-23 22:39:25 +0000139 context->draw();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700140 } else {
John Reckcf1170f2021-07-14 15:52:19 -0400141 // Do a flush in case syncFrameState performed any texture uploads. Since we skipped
142 // the draw() call, those uploads (or deletes) will end up sitting in the queue.
143 // Do them now
144 if (GrDirectContext* grContext = mRenderThread->getGrContext()) {
145 grContext->flushAndSubmit();
146 }
Chris Craik06e2e9c2016-08-31 17:32:46 -0700147 // wait on fences so tasks don't overlap next frame
148 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700149 }
John Reck668f0e32014-03-26 15:10:40 -0700150
chaviw9c137532021-08-20 12:15:48 -0500151 if (CC_UNLIKELY(frameCompleteCallback)) {
152 std::invoke(frameCompleteCallback);
153 }
154
John Reck668f0e32014-03-26 15:10:40 -0700155 if (!canUnblockUiThread) {
156 unblockUiThread();
157 }
Nader Jawad7612c612022-11-15 10:55:54 -0800158
159 if (pipeline->hasHardwareBuffer()) {
160 auto fence = pipeline->flush();
161 hardwareBufferParams.invokeRenderCallback(std::move(fence), 0);
162 }
Matt Buckleyd98e8052022-10-21 22:13:23 +0000163}
164
John Recka5dda642014-05-22 15:43:54 -0700165bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700166 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700167 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700168 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
169 int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
Ady Abrahamdfb13982020-10-05 17:59:09 -0700170 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100171 int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)];
172 mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline,
173 frameInterval);
John Reck8afcc762016-04-13 10:24:06 -0700174 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400175 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700176
177 for (size_t i = 0; i < mLayers.size(); i++) {
Jessie Hao10f88672022-05-10 14:47:05 +0800178 if (mLayers[i]) {
179 mLayers[i]->apply();
180 }
John Reckd72e0a32014-05-29 18:56:11 -0700181 }
182 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700183 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700184 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700185
John Reckaa95a882014-11-07 11:02:07 -0800186 // This is after the prepareTree so that any pending operations
187 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
Nader Jawad7612c612022-11-15 10:55:54 -0800188 bool hasTarget = mContext->hasOutputTarget();
189 if (CC_UNLIKELY(!hasTarget || !canDraw)) {
190 if (!hasTarget) {
John Reck9a17da82016-04-18 11:17:52 -0700191 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
192 } else {
193 // If we have a surface but can't draw we must be stopped
194 mSyncResult |= SyncResult::ContextIsStopped;
195 }
John Reck8afcc762016-04-13 10:24:06 -0700196 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800197 }
198
John Reckf9be7792014-05-02 18:21:16 -0700199 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700200 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700201 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700202 }
John Reck52244ff2014-05-01 21:27:37 -0700203 }
John Reckcc2eee82018-05-17 10:44:00 -0700204 if (!info.out.canDrawThisFrame) {
205 mSyncResult |= SyncResult::FrameDropped;
206 }
John Reck860d1552014-04-11 19:15:05 -0700207 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700208 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700209}
210
211void DrawFrameTask::unblockUiThread() {
212 AutoMutex _lock(mLock);
213 mSignal.signal();
214}
215
John Reck668f0e32014-03-26 15:10:40 -0700216} /* namespace renderthread */
217} /* namespace uirenderer */
218} /* namespace android */