| 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 |  | 
| Bo Liu | 6a3fc60 | 2021-07-17 16:42:13 -0400 | [diff] [blame] | 19 | #include <dlfcn.h> | 
| rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 20 | #include <gui/TraceUtils.h> | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> | 
| 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" | 
|  | 29 | #include "RenderThread.h" | 
| Bo Liu | 6a3fc60 | 2021-07-17 16:42:13 -0400 | [diff] [blame] | 30 | #include "thread/CommonPool.h" | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | namespace android { | 
|  | 33 | namespace uirenderer { | 
|  | 34 | namespace renderthread { | 
|  | 35 |  | 
| Bo Liu | 6a3fc60 | 2021-07-17 16:42:13 -0400 | [diff] [blame] | 36 | namespace { | 
|  | 37 |  | 
|  | 38 | typedef APerformanceHintManager* (*APH_getManager)(); | 
|  | 39 | typedef APerformanceHintSession* (*APH_createSession)(APerformanceHintManager*, const int32_t*, | 
|  | 40 | size_t, int64_t); | 
|  | 41 | typedef void (*APH_updateTargetWorkDuration)(APerformanceHintSession*, int64_t); | 
|  | 42 | typedef void (*APH_reportActualWorkDuration)(APerformanceHintSession*, int64_t); | 
|  | 43 | typedef void (*APH_closeSession)(APerformanceHintSession* session); | 
|  | 44 |  | 
|  | 45 | bool gAPerformanceHintBindingInitialized = false; | 
|  | 46 | APH_getManager gAPH_getManagerFn = nullptr; | 
|  | 47 | APH_createSession gAPH_createSessionFn = nullptr; | 
|  | 48 | APH_updateTargetWorkDuration gAPH_updateTargetWorkDurationFn = nullptr; | 
|  | 49 | APH_reportActualWorkDuration gAPH_reportActualWorkDurationFn = nullptr; | 
|  | 50 | APH_closeSession gAPH_closeSessionFn = nullptr; | 
|  | 51 |  | 
|  | 52 | void ensureAPerformanceHintBindingInitialized() { | 
|  | 53 | if (gAPerformanceHintBindingInitialized) return; | 
|  | 54 |  | 
|  | 55 | void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE); | 
|  | 56 | LOG_ALWAYS_FATAL_IF(handle_ == nullptr, "Failed to dlopen libandroid.so!"); | 
|  | 57 |  | 
|  | 58 | gAPH_getManagerFn = (APH_getManager)dlsym(handle_, "APerformanceHint_getManager"); | 
|  | 59 | LOG_ALWAYS_FATAL_IF(gAPH_getManagerFn == nullptr, | 
|  | 60 | "Failed to find required symbol APerformanceHint_getManager!"); | 
|  | 61 |  | 
|  | 62 | gAPH_createSessionFn = (APH_createSession)dlsym(handle_, "APerformanceHint_createSession"); | 
|  | 63 | LOG_ALWAYS_FATAL_IF(gAPH_createSessionFn == nullptr, | 
|  | 64 | "Failed to find required symbol APerformanceHint_createSession!"); | 
|  | 65 |  | 
|  | 66 | gAPH_updateTargetWorkDurationFn = (APH_updateTargetWorkDuration)dlsym( | 
|  | 67 | handle_, "APerformanceHint_updateTargetWorkDuration"); | 
|  | 68 | LOG_ALWAYS_FATAL_IF( | 
|  | 69 | gAPH_updateTargetWorkDurationFn == nullptr, | 
|  | 70 | "Failed to find required symbol APerformanceHint_updateTargetWorkDuration!"); | 
|  | 71 |  | 
|  | 72 | gAPH_reportActualWorkDurationFn = (APH_reportActualWorkDuration)dlsym( | 
|  | 73 | handle_, "APerformanceHint_reportActualWorkDuration"); | 
|  | 74 | LOG_ALWAYS_FATAL_IF( | 
|  | 75 | gAPH_reportActualWorkDurationFn == nullptr, | 
|  | 76 | "Failed to find required symbol APerformanceHint_reportActualWorkDuration!"); | 
|  | 77 |  | 
|  | 78 | gAPH_closeSessionFn = (APH_closeSession)dlsym(handle_, "APerformanceHint_closeSession"); | 
|  | 79 | LOG_ALWAYS_FATAL_IF(gAPH_closeSessionFn == nullptr, | 
|  | 80 | "Failed to find required symbol APerformanceHint_closeSession!"); | 
|  | 81 |  | 
|  | 82 | gAPerformanceHintBindingInitialized = true; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | }  // namespace | 
|  | 86 |  | 
| John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 87 | DrawFrameTask::DrawFrameTask() | 
| Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 88 | : mRenderThread(nullptr) | 
|  | 89 | , mContext(nullptr) | 
| John Reck | f138b17 | 2017-09-08 11:00:42 -0700 | [diff] [blame] | 90 | , mContentDrawBounds(0, 0, 0, 0) | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 91 | , mSyncResult(SyncResult::OK) {} | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 92 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 93 | DrawFrameTask::~DrawFrameTask() {} | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 94 |  | 
| Bo Liu | 6a3fc60 | 2021-07-17 16:42:13 -0400 | [diff] [blame] | 95 | void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context, RenderNode* targetNode, | 
|  | 96 | int32_t uiThreadId, int32_t renderThreadId) { | 
| John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 97 | mRenderThread = thread; | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 98 | mContext = context; | 
| Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 99 | mTargetNode = targetNode; | 
| Bo Liu | 6a3fc60 | 2021-07-17 16:42:13 -0400 | [diff] [blame] | 100 | mUiThreadId = uiThreadId; | 
|  | 101 | mRenderThreadId = renderThreadId; | 
| Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 104 | void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 105 | LOG_ALWAYS_FATAL_IF(!mContext, | 
|  | 106 | "Lifecycle violation, there's no context to pushLayerUpdate with!"); | 
| John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 107 |  | 
| John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 108 | for (size_t i = 0; i < mLayers.size(); i++) { | 
|  | 109 | if (mLayers[i].get() == layer) { | 
|  | 110 | return; | 
|  | 111 | } | 
|  | 112 | } | 
|  | 113 | mLayers.push_back(layer); | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 114 | } | 
|  | 115 |  | 
| John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 116 | void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) { | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 117 | for (size_t i = 0; i < mLayers.size(); i++) { | 
| John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 118 | if (mLayers[i].get() == layer) { | 
|  | 119 | mLayers.erase(mLayers.begin() + i); | 
|  | 120 | return; | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 121 | } | 
|  | 122 | } | 
|  | 123 | } | 
|  | 124 |  | 
| John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 125 | int DrawFrameTask::drawFrame() { | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 126 | LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!"); | 
|  | 127 |  | 
| John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 128 | mSyncResult = SyncResult::OK; | 
| Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 129 | mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC); | 
| John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 130 | postAndWait(); | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 131 |  | 
| John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 132 | return mSyncResult; | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
| John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 135 | void DrawFrameTask::postAndWait() { | 
| Jimmy Shiu | 06d9139 | 2022-03-15 19:35:45 +0800 | [diff] [blame] | 136 | ATRACE_CALL(); | 
| John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 137 | AutoMutex _lock(mLock); | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 138 | mRenderThread->queue().post([this]() { run(); }); | 
| Chris Craik | 738ec3a | 2014-07-25 18:25:02 +0000 | [diff] [blame] | 139 | mSignal.wait(mLock); | 
| John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 142 | void DrawFrameTask::run() { | 
| Ady Abraham | 150816c | 2021-03-01 10:46:40 -0800 | [diff] [blame] | 143 | const int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)]; | 
|  | 144 | ATRACE_FORMAT("DrawFrames %" PRId64, vsyncId); | 
| Bo Liu | 4d0f1f1 | 2021-05-06 16:49:40 -0400 | [diff] [blame] | 145 | nsecs_t syncDelayDuration = systemTime(SYSTEM_TIME_MONOTONIC) - mSyncQueued; | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 146 |  | 
| John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 147 | bool canUnblockUiThread; | 
|  | 148 | bool canDrawThisFrame; | 
|  | 149 | { | 
| Chris Craik | e2e53a7 | 2015-10-28 15:55:40 -0700 | [diff] [blame] | 150 | TreeInfo info(TreeInfo::MODE_FULL, *mContext); | 
| chaviw | adba0b1 | 2022-03-18 17:42:15 -0500 | [diff] [blame] | 151 | info.forceDrawFrame = mForceDrawFrame; | 
|  | 152 | mForceDrawFrame = false; | 
| John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 153 | canUnblockUiThread = syncFrameState(info); | 
|  | 154 | canDrawThisFrame = info.out.canDrawThisFrame; | 
| John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 155 |  | 
| chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 156 | if (mFrameCommitCallback) { | 
|  | 157 | mContext->addFrameCommitListener(std::move(mFrameCommitCallback)); | 
|  | 158 | mFrameCommitCallback = nullptr; | 
| John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 159 | } | 
| John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 160 | } | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 161 |  | 
|  | 162 | // Grab a copy of everything we need | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 163 | CanvasContext* context = mContext; | 
| chaviw | b680371 | 2021-12-13 15:46:29 -0600 | [diff] [blame] | 164 | std::function<std::function<void(bool)>(int32_t, int64_t)> frameCallback = | 
|  | 165 | std::move(mFrameCallback); | 
| chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 166 | std::function<void()> frameCompleteCallback = std::move(mFrameCompleteCallback); | 
| Jorim Jaggi | 7a5addd | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 167 | mFrameCallback = nullptr; | 
| chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 168 | mFrameCompleteCallback = nullptr; | 
| Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 169 | int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)]; | 
|  | 170 | int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)]; | 
|  | 171 | int64_t frameStartTime = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameStartTime)]; | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 172 |  | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 173 | // From this point on anything in "this" is *UNSAFE TO ACCESS* | 
|  | 174 | if (canUnblockUiThread) { | 
|  | 175 | unblockUiThread(); | 
|  | 176 | } | 
|  | 177 |  | 
| Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 178 | // 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] | 179 | if (CC_UNLIKELY(frameCallback)) { | 
| chaviw | b680371 | 2021-12-13 15:46:29 -0600 | [diff] [blame] | 180 | context->enqueueFrameWork([frameCallback, context, syncResult = mSyncResult, | 
|  | 181 | frameNr = context->getFrameNumber()]() { | 
|  | 182 | auto frameCommitCallback = std::move(frameCallback(syncResult, frameNr)); | 
|  | 183 | if (frameCommitCallback) { | 
|  | 184 | context->addFrameCommitListener(std::move(frameCommitCallback)); | 
|  | 185 | } | 
|  | 186 | }); | 
| Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
| Bo Liu | 4d0f1f1 | 2021-05-06 16:49:40 -0400 | [diff] [blame] | 189 | nsecs_t dequeueBufferDuration = 0; | 
| John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 190 | if (CC_LIKELY(canDrawThisFrame)) { | 
| Bo Liu | 4d0f1f1 | 2021-05-06 16:49:40 -0400 | [diff] [blame] | 191 | dequeueBufferDuration = context->draw(); | 
| Chris Craik | 06e2e9c | 2016-08-31 17:32:46 -0700 | [diff] [blame] | 192 | } else { | 
| John Reck | cf1170f | 2021-07-14 15:52:19 -0400 | [diff] [blame] | 193 | // Do a flush in case syncFrameState performed any texture uploads. Since we skipped | 
|  | 194 | // the draw() call, those uploads (or deletes) will end up sitting in the queue. | 
|  | 195 | // Do them now | 
|  | 196 | if (GrDirectContext* grContext = mRenderThread->getGrContext()) { | 
|  | 197 | grContext->flushAndSubmit(); | 
|  | 198 | } | 
| Chris Craik | 06e2e9c | 2016-08-31 17:32:46 -0700 | [diff] [blame] | 199 | // wait on fences so tasks don't overlap next frame | 
|  | 200 | context->waitOnFences(); | 
| John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 201 | } | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 202 |  | 
| chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 203 | if (CC_UNLIKELY(frameCompleteCallback)) { | 
|  | 204 | std::invoke(frameCompleteCallback); | 
|  | 205 | } | 
|  | 206 |  | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 207 | if (!canUnblockUiThread) { | 
|  | 208 | unblockUiThread(); | 
|  | 209 | } | 
| Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 210 |  | 
| Bo Liu | 6a3fc60 | 2021-07-17 16:42:13 -0400 | [diff] [blame] | 211 | if (!mHintSessionWrapper) mHintSessionWrapper.emplace(mUiThreadId, mRenderThreadId); | 
|  | 212 | constexpr int64_t kSanityCheckLowerBound = 100000;       // 0.1ms | 
|  | 213 | constexpr int64_t kSanityCheckUpperBound = 10000000000;  // 10s | 
|  | 214 | int64_t targetWorkDuration = frameDeadline - intendedVsync; | 
|  | 215 | targetWorkDuration = targetWorkDuration * Properties::targetCpuTimePercentage / 100; | 
|  | 216 | if (targetWorkDuration > kSanityCheckLowerBound && | 
|  | 217 | targetWorkDuration < kSanityCheckUpperBound && | 
|  | 218 | targetWorkDuration != mLastTargetWorkDuration) { | 
|  | 219 | mLastTargetWorkDuration = targetWorkDuration; | 
|  | 220 | mHintSessionWrapper->updateTargetWorkDuration(targetWorkDuration); | 
| Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 221 | } | 
| Bo Liu | 6a3fc60 | 2021-07-17 16:42:13 -0400 | [diff] [blame] | 222 | int64_t frameDuration = systemTime(SYSTEM_TIME_MONOTONIC) - frameStartTime; | 
|  | 223 | int64_t actualDuration = frameDuration - | 
|  | 224 | (std::min(syncDelayDuration, mLastDequeueBufferDuration)) - | 
|  | 225 | dequeueBufferDuration; | 
|  | 226 | if (actualDuration > kSanityCheckLowerBound && actualDuration < kSanityCheckUpperBound) { | 
|  | 227 | mHintSessionWrapper->reportActualWorkDuration(actualDuration); | 
|  | 228 | } | 
|  | 229 |  | 
| Bo Liu | 4d0f1f1 | 2021-05-06 16:49:40 -0400 | [diff] [blame] | 230 | mLastDequeueBufferDuration = dequeueBufferDuration; | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 231 | } | 
|  | 232 |  | 
| John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 233 | bool DrawFrameTask::syncFrameState(TreeInfo& info) { | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 234 | ATRACE_CALL(); | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 235 | int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)]; | 
| Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 236 | int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)]; | 
|  | 237 | int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)]; | 
| Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 238 | int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)]; | 
| Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 239 | int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)]; | 
|  | 240 | mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline, | 
|  | 241 | frameInterval); | 
| John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 242 | bool canDraw = mContext->makeCurrent(); | 
| Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 243 | mContext->unpinImages(); | 
| John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 244 |  | 
|  | 245 | for (size_t i = 0; i < mLayers.size(); i++) { | 
| Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 246 | mLayers[i]->apply(); | 
| John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 247 | } | 
|  | 248 | mLayers.clear(); | 
| John Reck | f138b17 | 2017-09-08 11:00:42 -0700 | [diff] [blame] | 249 | mContext->setContentDrawBounds(mContentDrawBounds); | 
| Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 250 | mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode); | 
| John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 251 |  | 
| John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 252 | // This is after the prepareTree so that any pending operations | 
|  | 253 | // (RenderNode tree state, prefetched layers, etc...) will be flushed. | 
| John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 254 | if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) { | 
| John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 255 | if (!mContext->hasSurface()) { | 
|  | 256 | mSyncResult |= SyncResult::LostSurfaceRewardIfFound; | 
|  | 257 | } else { | 
|  | 258 | // If we have a surface but can't draw we must be stopped | 
|  | 259 | mSyncResult |= SyncResult::ContextIsStopped; | 
|  | 260 | } | 
| John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 261 | info.out.canDrawThisFrame = false; | 
| John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
| John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 264 | if (info.out.hasAnimations) { | 
| John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 265 | if (info.out.requiresUiRedraw) { | 
| John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 266 | mSyncResult |= SyncResult::UIRedrawRequired; | 
| John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 267 | } | 
| John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 268 | } | 
| John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 269 | if (!info.out.canDrawThisFrame) { | 
|  | 270 | mSyncResult |= SyncResult::FrameDropped; | 
|  | 271 | } | 
| John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 272 | // If prepareTextures is false, we ran out of texture cache space | 
| Bo Liu | 826b564 | 2014-05-13 16:47:27 -0700 | [diff] [blame] | 273 | return info.prepareTextures; | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 274 | } | 
|  | 275 |  | 
|  | 276 | void DrawFrameTask::unblockUiThread() { | 
|  | 277 | AutoMutex _lock(mLock); | 
|  | 278 | mSignal.signal(); | 
|  | 279 | } | 
|  | 280 |  | 
| Bo Liu | 6a3fc60 | 2021-07-17 16:42:13 -0400 | [diff] [blame] | 281 | DrawFrameTask::HintSessionWrapper::HintSessionWrapper(int32_t uiThreadId, int32_t renderThreadId) { | 
|  | 282 | if (!Properties::useHintManager) return; | 
|  | 283 | if (uiThreadId < 0 || renderThreadId < 0) return; | 
|  | 284 |  | 
|  | 285 | ensureAPerformanceHintBindingInitialized(); | 
|  | 286 |  | 
|  | 287 | APerformanceHintManager* manager = gAPH_getManagerFn(); | 
|  | 288 | if (!manager) return; | 
|  | 289 |  | 
|  | 290 | std::vector<int32_t> tids = CommonPool::getThreadIds(); | 
|  | 291 | tids.push_back(uiThreadId); | 
|  | 292 | tids.push_back(renderThreadId); | 
|  | 293 |  | 
|  | 294 | // DrawFrameTask code will always set a target duration before reporting actual durations. | 
|  | 295 | // So this is just a placeholder value that's never used. | 
|  | 296 | int64_t dummyTargetDurationNanos = 16666667; | 
|  | 297 | mHintSession = | 
|  | 298 | gAPH_createSessionFn(manager, tids.data(), tids.size(), dummyTargetDurationNanos); | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | DrawFrameTask::HintSessionWrapper::~HintSessionWrapper() { | 
|  | 302 | if (mHintSession) { | 
|  | 303 | gAPH_closeSessionFn(mHintSession); | 
|  | 304 | } | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | void DrawFrameTask::HintSessionWrapper::updateTargetWorkDuration(long targetDurationNanos) { | 
|  | 308 | if (mHintSession) { | 
|  | 309 | gAPH_updateTargetWorkDurationFn(mHintSession, targetDurationNanos); | 
|  | 310 | } | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | void DrawFrameTask::HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) { | 
|  | 314 | if (mHintSession) { | 
|  | 315 | gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos); | 
|  | 316 | } | 
|  | 317 | } | 
|  | 318 |  | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 319 | } /* namespace renderthread */ | 
|  | 320 | } /* namespace uirenderer */ | 
|  | 321 | } /* namespace android */ |