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 | |
| 19 | #include <utils/Log.h> |
Ady Abraham | 150816c | 2021-03-01 10:46:40 -0800 | [diff] [blame] | 20 | #include <utils/TraceUtils.h> |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 21 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 22 | #include "../DeferredLayerUpdater.h" |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 23 | #include "../DisplayList.h" |
Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 24 | #include "../Properties.h" |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 25 | #include "../RenderNode.h" |
| 26 | #include "CanvasContext.h" |
| 27 | #include "RenderThread.h" |
| 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | namespace renderthread { |
| 32 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 33 | DrawFrameTask::DrawFrameTask() |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 34 | : mRenderThread(nullptr) |
| 35 | , mContext(nullptr) |
John Reck | f138b17 | 2017-09-08 11:00:42 -0700 | [diff] [blame] | 36 | , mContentDrawBounds(0, 0, 0, 0) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 37 | , mSyncResult(SyncResult::OK) {} |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 38 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 39 | DrawFrameTask::~DrawFrameTask() {} |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 40 | |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 41 | void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 42 | RenderNode* targetNode) { |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 43 | mRenderThread = thread; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 44 | mContext = context; |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 45 | mTargetNode = targetNode; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 48 | void DrawFrameTask::setHintSessionCallbacks(std::function<void(int64_t)> updateTargetWorkDuration, |
| 49 | std::function<void(int64_t)> reportActualWorkDuration) { |
| 50 | mUpdateTargetWorkDuration = std::move(updateTargetWorkDuration); |
| 51 | mReportActualWorkDuration = std::move(reportActualWorkDuration); |
| 52 | } |
| 53 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 54 | void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 55 | LOG_ALWAYS_FATAL_IF(!mContext, |
| 56 | "Lifecycle violation, there's no context to pushLayerUpdate with!"); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 57 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 58 | for (size_t i = 0; i < mLayers.size(); i++) { |
| 59 | if (mLayers[i].get() == layer) { |
| 60 | return; |
| 61 | } |
| 62 | } |
| 63 | mLayers.push_back(layer); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 64 | } |
| 65 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 66 | void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 67 | for (size_t i = 0; i < mLayers.size(); i++) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 68 | if (mLayers[i].get() == layer) { |
| 69 | mLayers.erase(mLayers.begin() + i); |
| 70 | return; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 75 | int DrawFrameTask::drawFrame() { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 76 | LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!"); |
| 77 | |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 78 | mSyncResult = SyncResult::OK; |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 79 | mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 80 | postAndWait(); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 81 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 82 | return mSyncResult; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 83 | } |
| 84 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 85 | void DrawFrameTask::postAndWait() { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 86 | AutoMutex _lock(mLock); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 87 | mRenderThread->queue().post([this]() { run(); }); |
Chris Craik | 738ec3a | 2014-07-25 18:25:02 +0000 | [diff] [blame] | 88 | mSignal.wait(mLock); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 89 | } |
| 90 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 91 | void DrawFrameTask::run() { |
Ady Abraham | 150816c | 2021-03-01 10:46:40 -0800 | [diff] [blame] | 92 | const int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)]; |
| 93 | ATRACE_FORMAT("DrawFrames %" PRId64, vsyncId); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 94 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 95 | bool canUnblockUiThread; |
| 96 | bool canDrawThisFrame; |
| 97 | { |
Chris Craik | e2e53a7 | 2015-10-28 15:55:40 -0700 | [diff] [blame] | 98 | TreeInfo info(TreeInfo::MODE_FULL, *mContext); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 99 | canUnblockUiThread = syncFrameState(info); |
| 100 | canDrawThisFrame = info.out.canDrawThisFrame; |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 101 | |
| 102 | if (mFrameCompleteCallback) { |
| 103 | mContext->addFrameCompleteListener(std::move(mFrameCompleteCallback)); |
| 104 | mFrameCompleteCallback = nullptr; |
| 105 | } |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 106 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 107 | |
| 108 | // Grab a copy of everything we need |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 109 | CanvasContext* context = mContext; |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 110 | std::function<void(int64_t)> callback = std::move(mFrameCallback); |
Jorim Jaggi | 7a5addd | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 111 | mFrameCallback = nullptr; |
Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 112 | int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)]; |
| 113 | int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)]; |
| 114 | int64_t frameStartTime = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameStartTime)]; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 115 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 116 | // From this point on anything in "this" is *UNSAFE TO ACCESS* |
| 117 | if (canUnblockUiThread) { |
| 118 | unblockUiThread(); |
| 119 | } |
| 120 | |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 121 | // Even if we aren't drawing this vsync pulse the next frame number will still be accurate |
| 122 | if (CC_UNLIKELY(callback)) { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 123 | context->enqueueFrameWork( |
| 124 | [callback, frameNr = context->getFrameNumber()]() { callback(frameNr); }); |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 125 | } |
| 126 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 127 | if (CC_LIKELY(canDrawThisFrame)) { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 128 | context->draw(); |
Chris Craik | 06e2e9c | 2016-08-31 17:32:46 -0700 | [diff] [blame] | 129 | } else { |
| 130 | // wait on fences so tasks don't overlap next frame |
| 131 | context->waitOnFences(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 132 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 133 | |
| 134 | if (!canUnblockUiThread) { |
| 135 | unblockUiThread(); |
| 136 | } |
Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 137 | |
| 138 | // These member callbacks are effectively const as they are set once during init, so it's safe |
| 139 | // to use these directly instead of making local copies. |
| 140 | if (mUpdateTargetWorkDuration && mReportActualWorkDuration) { |
| 141 | constexpr int64_t kSanityCheckLowerBound = 100000; // 0.1ms |
| 142 | constexpr int64_t kSanityCheckUpperBound = 10000000000; // 10s |
| 143 | int64_t targetWorkDuration = frameDeadline - intendedVsync; |
| 144 | targetWorkDuration = targetWorkDuration * Properties::targetCpuTimePercentage / 100; |
| 145 | if (targetWorkDuration > kSanityCheckLowerBound && |
| 146 | targetWorkDuration < kSanityCheckUpperBound && |
| 147 | targetWorkDuration != mLastTargetWorkDuration) { |
| 148 | mLastTargetWorkDuration = targetWorkDuration; |
| 149 | mUpdateTargetWorkDuration(targetWorkDuration); |
| 150 | } |
| 151 | int64_t frameDuration = systemTime(SYSTEM_TIME_MONOTONIC) - frameStartTime; |
| 152 | if (frameDuration > kSanityCheckLowerBound && frameDuration < kSanityCheckUpperBound) { |
| 153 | mReportActualWorkDuration(frameDuration); |
| 154 | } |
| 155 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 156 | } |
| 157 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 158 | bool DrawFrameTask::syncFrameState(TreeInfo& info) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 159 | ATRACE_CALL(); |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 160 | int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)]; |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 161 | int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)]; |
| 162 | int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)]; |
Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 163 | int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)]; |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame^] | 164 | int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)]; |
| 165 | mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline, |
| 166 | frameInterval); |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 167 | bool canDraw = mContext->makeCurrent(); |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 168 | mContext->unpinImages(); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 169 | |
| 170 | for (size_t i = 0; i < mLayers.size(); i++) { |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 171 | mLayers[i]->apply(); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 172 | } |
| 173 | mLayers.clear(); |
John Reck | f138b17 | 2017-09-08 11:00:42 -0700 | [diff] [blame] | 174 | mContext->setContentDrawBounds(mContentDrawBounds); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 175 | mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 176 | |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 177 | // This is after the prepareTree so that any pending operations |
| 178 | // (RenderNode tree state, prefetched layers, etc...) will be flushed. |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 179 | if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) { |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 180 | if (!mContext->hasSurface()) { |
| 181 | mSyncResult |= SyncResult::LostSurfaceRewardIfFound; |
| 182 | } else { |
| 183 | // If we have a surface but can't draw we must be stopped |
| 184 | mSyncResult |= SyncResult::ContextIsStopped; |
| 185 | } |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 186 | info.out.canDrawThisFrame = false; |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 187 | } |
| 188 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 189 | if (info.out.hasAnimations) { |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 190 | if (info.out.requiresUiRedraw) { |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 191 | mSyncResult |= SyncResult::UIRedrawRequired; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 192 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 193 | } |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 194 | if (!info.out.canDrawThisFrame) { |
| 195 | mSyncResult |= SyncResult::FrameDropped; |
| 196 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 197 | // If prepareTextures is false, we ran out of texture cache space |
Bo Liu | 826b564 | 2014-05-13 16:47:27 -0700 | [diff] [blame] | 198 | return info.prepareTextures; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void DrawFrameTask::unblockUiThread() { |
| 202 | AutoMutex _lock(mLock); |
| 203 | mSignal.signal(); |
| 204 | } |
| 205 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 206 | } /* namespace renderthread */ |
| 207 | } /* namespace uirenderer */ |
| 208 | } /* namespace android */ |