blob: b06c5dd9ad369ae9a255392b14f95d7ba5720c36 [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 Reck668f0e32014-03-26 15:10:40 -070094
Nader Jawad7612c612022-11-15 10:55:54 -080095 auto hardwareBufferParams = mHardwareBufferParams;
96 mContext->setHardwareBufferRenderParams(hardwareBufferParams);
97 IRenderPipeline* pipeline = mContext->getRenderPipeline();
John Recka5dda642014-05-22 15:43:54 -070098 bool canUnblockUiThread;
99 bool canDrawThisFrame;
100 {
Chris Craike2e53a72015-10-28 15:55:40 -0700101 TreeInfo info(TreeInfo::MODE_FULL, *mContext);
chaviwadba0b12022-03-18 17:42:15 -0500102 info.forceDrawFrame = mForceDrawFrame;
103 mForceDrawFrame = false;
John Recka5dda642014-05-22 15:43:54 -0700104 canUnblockUiThread = syncFrameState(info);
105 canDrawThisFrame = info.out.canDrawThisFrame;
John Reckcc2eee82018-05-17 10:44:00 -0700106
chaviw9c137532021-08-20 12:15:48 -0500107 if (mFrameCommitCallback) {
108 mContext->addFrameCommitListener(std::move(mFrameCommitCallback));
109 mFrameCommitCallback = nullptr;
John Reckcc2eee82018-05-17 10:44:00 -0700110 }
John Recka5dda642014-05-22 15:43:54 -0700111 }
John Reck668f0e32014-03-26 15:10:40 -0700112
113 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -0700114 CanvasContext* context = mContext;
chaviwb6803712021-12-13 15:46:29 -0600115 std::function<std::function<void(bool)>(int32_t, int64_t)> frameCallback =
116 std::move(mFrameCallback);
chaviw9c137532021-08-20 12:15:48 -0500117 std::function<void()> frameCompleteCallback = std::move(mFrameCompleteCallback);
Jorim Jaggi7a5addd2018-04-26 23:23:29 +0200118 mFrameCallback = nullptr;
chaviw9c137532021-08-20 12:15:48 -0500119 mFrameCompleteCallback = nullptr;
John Reck668f0e32014-03-26 15:10:40 -0700120
John Reck668f0e32014-03-26 15:10:40 -0700121 // From this point on anything in "this" is *UNSAFE TO ACCESS*
122 if (canUnblockUiThread) {
123 unblockUiThread();
124 }
125
Mihai Popa95688002018-02-23 16:10:11 +0000126 // Even if we aren't drawing this vsync pulse the next frame number will still be accurate
chaviw9c137532021-08-20 12:15:48 -0500127 if (CC_UNLIKELY(frameCallback)) {
chaviwb6803712021-12-13 15:46:29 -0600128 context->enqueueFrameWork([frameCallback, context, syncResult = mSyncResult,
129 frameNr = context->getFrameNumber()]() {
Matt Buckleye9023cf2022-11-23 22:39:25 +0000130 auto frameCommitCallback = frameCallback(syncResult, frameNr);
chaviwb6803712021-12-13 15:46:29 -0600131 if (frameCommitCallback) {
132 context->addFrameCommitListener(std::move(frameCommitCallback));
133 }
134 });
Mihai Popa95688002018-02-23 16:10:11 +0000135 }
136
John Recka5dda642014-05-22 15:43:54 -0700137 if (CC_LIKELY(canDrawThisFrame)) {
Matt Buckleye9023cf2022-11-23 22:39:25 +0000138 context->draw();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700139 } else {
John Reckcf1170f2021-07-14 15:52:19 -0400140 // Do a flush in case syncFrameState performed any texture uploads. Since we skipped
141 // the draw() call, those uploads (or deletes) will end up sitting in the queue.
142 // Do them now
143 if (GrDirectContext* grContext = mRenderThread->getGrContext()) {
144 grContext->flushAndSubmit();
145 }
Chris Craik06e2e9c2016-08-31 17:32:46 -0700146 // wait on fences so tasks don't overlap next frame
147 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700148 }
John Reck668f0e32014-03-26 15:10:40 -0700149
chaviw9c137532021-08-20 12:15:48 -0500150 if (CC_UNLIKELY(frameCompleteCallback)) {
151 std::invoke(frameCompleteCallback);
152 }
153
John Reck668f0e32014-03-26 15:10:40 -0700154 if (!canUnblockUiThread) {
155 unblockUiThread();
156 }
Nader Jawad7612c612022-11-15 10:55:54 -0800157
158 if (pipeline->hasHardwareBuffer()) {
159 auto fence = pipeline->flush();
160 hardwareBufferParams.invokeRenderCallback(std::move(fence), 0);
161 }
Matt Buckleyd98e8052022-10-21 22:13:23 +0000162}
163
John Recka5dda642014-05-22 15:43:54 -0700164bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700165 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700166 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700167 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
168 int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
Ady Abrahamdfb13982020-10-05 17:59:09 -0700169 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100170 int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)];
171 mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline,
172 frameInterval);
John Reck8afcc762016-04-13 10:24:06 -0700173 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400174 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700175
176 for (size_t i = 0; i < mLayers.size(); i++) {
Jessie Hao10f88672022-05-10 14:47:05 +0800177 if (mLayers[i]) {
178 mLayers[i]->apply();
179 }
John Reckd72e0a32014-05-29 18:56:11 -0700180 }
181 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700182 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700183 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700184
John Reckaa95a882014-11-07 11:02:07 -0800185 // This is after the prepareTree so that any pending operations
186 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
Nader Jawad7612c612022-11-15 10:55:54 -0800187 bool hasTarget = mContext->hasOutputTarget();
188 if (CC_UNLIKELY(!hasTarget || !canDraw)) {
189 if (!hasTarget) {
John Reck9a17da82016-04-18 11:17:52 -0700190 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
191 } else {
192 // If we have a surface but can't draw we must be stopped
193 mSyncResult |= SyncResult::ContextIsStopped;
194 }
John Reck8afcc762016-04-13 10:24:06 -0700195 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800196 }
197
John Reckf9be7792014-05-02 18:21:16 -0700198 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700199 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700200 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700201 }
John Reck52244ff2014-05-01 21:27:37 -0700202 }
John Reckcc2eee82018-05-17 10:44:00 -0700203 if (!info.out.canDrawThisFrame) {
204 mSyncResult |= SyncResult::FrameDropped;
205 }
John Reck860d1552014-04-11 19:15:05 -0700206 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700207 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700208}
209
210void DrawFrameTask::unblockUiThread() {
211 AutoMutex _lock(mLock);
212 mSignal.signal();
213}
214
John Reck668f0e32014-03-26 15:10:40 -0700215} /* namespace renderthread */
216} /* namespace uirenderer */
217} /* namespace android */