John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 1 | /* |
| 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 Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 17 | #include "DrawFrameTask.h" |
| 18 | |
rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 19 | #include <gui/TraceUtils.h> |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 20 | #include <utils/Log.h> |
Matt Buckley | 864ab95 | 2022-10-18 23:03:59 +0000 | [diff] [blame] | 21 | |
Bo Liu | 4d0f1f1 | 2021-05-06 16:49:40 -0400 | [diff] [blame] | 22 | #include <algorithm> |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 23 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 24 | #include "../DeferredLayerUpdater.h" |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 25 | #include "../DisplayList.h" |
Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 26 | #include "../Properties.h" |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 27 | #include "../RenderNode.h" |
| 28 | #include "CanvasContext.h" |
Nader Jawad | 7612c61 | 2022-11-15 10:55:54 -0800 | [diff] [blame] | 29 | #include "HardwareBufferRenderParams.h" |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 30 | #include "RenderThread.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace uirenderer { |
| 34 | namespace renderthread { |
| 35 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 36 | DrawFrameTask::DrawFrameTask() |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 37 | : mRenderThread(nullptr) |
| 38 | , mContext(nullptr) |
John Reck | f138b17 | 2017-09-08 11:00:42 -0700 | [diff] [blame] | 39 | , mContentDrawBounds(0, 0, 0, 0) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 40 | , mSyncResult(SyncResult::OK) {} |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 41 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 42 | DrawFrameTask::~DrawFrameTask() {} |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 43 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 44 | void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context, |
| 45 | RenderNode* targetNode) { |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 46 | mRenderThread = thread; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 47 | mContext = context; |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 48 | mTargetNode = targetNode; |
Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 49 | } |
| 50 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 51 | void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 52 | LOG_ALWAYS_FATAL_IF(!mContext, |
| 53 | "Lifecycle violation, there's no context to pushLayerUpdate with!"); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 54 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 55 | 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 Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 61 | } |
| 62 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 63 | void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 64 | for (size_t i = 0; i < mLayers.size(); i++) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 65 | if (mLayers[i].get() == layer) { |
| 66 | mLayers.erase(mLayers.begin() + i); |
| 67 | return; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 72 | int DrawFrameTask::drawFrame() { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 73 | LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!"); |
| 74 | |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 75 | mSyncResult = SyncResult::OK; |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 76 | mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 77 | postAndWait(); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 78 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 79 | return mSyncResult; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 80 | } |
| 81 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 82 | void DrawFrameTask::postAndWait() { |
Jimmy Shiu | 06d9139 | 2022-03-15 19:35:45 +0800 | [diff] [blame] | 83 | ATRACE_CALL(); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 84 | AutoMutex _lock(mLock); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 85 | mRenderThread->queue().post([this]() { run(); }); |
Chris Craik | 738ec3a | 2014-07-25 18:25:02 +0000 | [diff] [blame] | 86 | mSignal.wait(mLock); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 87 | } |
| 88 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 89 | void DrawFrameTask::run() { |
Ady Abraham | 150816c | 2021-03-01 10:46:40 -0800 | [diff] [blame] | 90 | const int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)]; |
| 91 | ATRACE_FORMAT("DrawFrames %" PRId64, vsyncId); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 92 | |
| 93 | mContext->setSyncDelayDuration(systemTime(SYSTEM_TIME_MONOTONIC) - mSyncQueued); |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 94 | mContext->setTargetSdrHdrRatio(mRenderSdrHdrRatio); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 95 | |
Nader Jawad | 7612c61 | 2022-11-15 10:55:54 -0800 | [diff] [blame] | 96 | auto hardwareBufferParams = mHardwareBufferParams; |
| 97 | mContext->setHardwareBufferRenderParams(hardwareBufferParams); |
| 98 | IRenderPipeline* pipeline = mContext->getRenderPipeline(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 99 | bool canUnblockUiThread; |
| 100 | bool canDrawThisFrame; |
| 101 | { |
Chris Craik | e2e53a7 | 2015-10-28 15:55:40 -0700 | [diff] [blame] | 102 | TreeInfo info(TreeInfo::MODE_FULL, *mContext); |
chaviw | adba0b1 | 2022-03-18 17:42:15 -0500 | [diff] [blame] | 103 | info.forceDrawFrame = mForceDrawFrame; |
| 104 | mForceDrawFrame = false; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 105 | canUnblockUiThread = syncFrameState(info); |
| 106 | canDrawThisFrame = info.out.canDrawThisFrame; |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 107 | |
chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 108 | if (mFrameCommitCallback) { |
| 109 | mContext->addFrameCommitListener(std::move(mFrameCommitCallback)); |
| 110 | mFrameCommitCallback = nullptr; |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 111 | } |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 112 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 113 | |
| 114 | // Grab a copy of everything we need |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 115 | CanvasContext* context = mContext; |
chaviw | b680371 | 2021-12-13 15:46:29 -0600 | [diff] [blame] | 116 | std::function<std::function<void(bool)>(int32_t, int64_t)> frameCallback = |
| 117 | std::move(mFrameCallback); |
chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 118 | std::function<void()> frameCompleteCallback = std::move(mFrameCompleteCallback); |
Jorim Jaggi | 7a5addd | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 119 | mFrameCallback = nullptr; |
chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 120 | mFrameCompleteCallback = nullptr; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 121 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 122 | // From this point on anything in "this" is *UNSAFE TO ACCESS* |
| 123 | if (canUnblockUiThread) { |
| 124 | unblockUiThread(); |
| 125 | } |
| 126 | |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 127 | // Even if we aren't drawing this vsync pulse the next frame number will still be accurate |
chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 128 | if (CC_UNLIKELY(frameCallback)) { |
chaviw | b680371 | 2021-12-13 15:46:29 -0600 | [diff] [blame] | 129 | context->enqueueFrameWork([frameCallback, context, syncResult = mSyncResult, |
| 130 | frameNr = context->getFrameNumber()]() { |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 131 | auto frameCommitCallback = frameCallback(syncResult, frameNr); |
chaviw | b680371 | 2021-12-13 15:46:29 -0600 | [diff] [blame] | 132 | if (frameCommitCallback) { |
| 133 | context->addFrameCommitListener(std::move(frameCommitCallback)); |
| 134 | } |
| 135 | }); |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 136 | } |
| 137 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 138 | if (CC_LIKELY(canDrawThisFrame)) { |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 139 | context->draw(); |
Chris Craik | 06e2e9c | 2016-08-31 17:32:46 -0700 | [diff] [blame] | 140 | } else { |
John Reck | cf1170f | 2021-07-14 15:52:19 -0400 | [diff] [blame] | 141 | // 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 Craik | 06e2e9c | 2016-08-31 17:32:46 -0700 | [diff] [blame] | 147 | // wait on fences so tasks don't overlap next frame |
| 148 | context->waitOnFences(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 149 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 150 | |
chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 151 | if (CC_UNLIKELY(frameCompleteCallback)) { |
| 152 | std::invoke(frameCompleteCallback); |
| 153 | } |
| 154 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 155 | if (!canUnblockUiThread) { |
| 156 | unblockUiThread(); |
| 157 | } |
Nader Jawad | 7612c61 | 2022-11-15 10:55:54 -0800 | [diff] [blame] | 158 | |
| 159 | if (pipeline->hasHardwareBuffer()) { |
| 160 | auto fence = pipeline->flush(); |
| 161 | hardwareBufferParams.invokeRenderCallback(std::move(fence), 0); |
| 162 | } |
Matt Buckley | d98e805 | 2022-10-21 22:13:23 +0000 | [diff] [blame] | 163 | } |
| 164 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 165 | bool DrawFrameTask::syncFrameState(TreeInfo& info) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 166 | ATRACE_CALL(); |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 167 | int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)]; |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 168 | int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)]; |
| 169 | int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)]; |
Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 170 | int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)]; |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 171 | int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)]; |
| 172 | mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline, |
| 173 | frameInterval); |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 174 | bool canDraw = mContext->makeCurrent(); |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 175 | mContext->unpinImages(); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 176 | |
| 177 | for (size_t i = 0; i < mLayers.size(); i++) { |
Jessie Hao | 10f8867 | 2022-05-10 14:47:05 +0800 | [diff] [blame] | 178 | if (mLayers[i]) { |
| 179 | mLayers[i]->apply(); |
| 180 | } |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 181 | } |
| 182 | mLayers.clear(); |
John Reck | f138b17 | 2017-09-08 11:00:42 -0700 | [diff] [blame] | 183 | mContext->setContentDrawBounds(mContentDrawBounds); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 184 | mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 185 | |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 186 | // This is after the prepareTree so that any pending operations |
| 187 | // (RenderNode tree state, prefetched layers, etc...) will be flushed. |
Nader Jawad | 7612c61 | 2022-11-15 10:55:54 -0800 | [diff] [blame] | 188 | bool hasTarget = mContext->hasOutputTarget(); |
| 189 | if (CC_UNLIKELY(!hasTarget || !canDraw)) { |
| 190 | if (!hasTarget) { |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 191 | 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 Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 196 | info.out.canDrawThisFrame = false; |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 197 | } |
| 198 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 199 | if (info.out.hasAnimations) { |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 200 | if (info.out.requiresUiRedraw) { |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 201 | mSyncResult |= SyncResult::UIRedrawRequired; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 202 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 203 | } |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 204 | if (!info.out.canDrawThisFrame) { |
| 205 | mSyncResult |= SyncResult::FrameDropped; |
| 206 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 207 | // If prepareTextures is false, we ran out of texture cache space |
Bo Liu | 826b564 | 2014-05-13 16:47:27 -0700 | [diff] [blame] | 208 | return info.prepareTextures; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void DrawFrameTask::unblockUiThread() { |
| 212 | AutoMutex _lock(mLock); |
| 213 | mSignal.signal(); |
| 214 | } |
| 215 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 216 | } /* namespace renderthread */ |
| 217 | } /* namespace uirenderer */ |
| 218 | } /* namespace android */ |