blob: cb306144ffd6b70f4f236e79e8c22b4937061df0 [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
Bo Liu6a3fc602021-07-17 16:42:13 -040019#include <dlfcn.h>
rnleece9762b2021-05-21 15:40:53 -070020#include <gui/TraceUtils.h>
John Reck668f0e32014-03-26 15:10:40 -070021#include <utils/Log.h>
Matt Buckley864ab952022-10-18 23:03:59 +000022
Bo Liu4d0f1f12021-05-06 16:49:40 -040023#include <algorithm>
John Reck668f0e32014-03-26 15:10:40 -070024
John Reckd72e0a32014-05-29 18:56:11 -070025#include "../DeferredLayerUpdater.h"
John Reck668f0e32014-03-26 15:10:40 -070026#include "../DisplayList.h"
Bo Liu027b2182021-03-18 16:50:38 -040027#include "../Properties.h"
John Reck668f0e32014-03-26 15:10:40 -070028#include "../RenderNode.h"
29#include "CanvasContext.h"
30#include "RenderThread.h"
Bo Liu6a3fc602021-07-17 16:42:13 -040031#include "thread/CommonPool.h"
Matt Buckley864ab952022-10-18 23:03:59 +000032#include "utils/TimeUtils.h"
John Reck668f0e32014-03-26 15:10:40 -070033
34namespace android {
35namespace uirenderer {
36namespace renderthread {
37
Bo Liu6a3fc602021-07-17 16:42:13 -040038namespace {
39
40typedef APerformanceHintManager* (*APH_getManager)();
41typedef APerformanceHintSession* (*APH_createSession)(APerformanceHintManager*, const int32_t*,
42 size_t, int64_t);
43typedef void (*APH_updateTargetWorkDuration)(APerformanceHintSession*, int64_t);
44typedef void (*APH_reportActualWorkDuration)(APerformanceHintSession*, int64_t);
Matt Buckleyd98e8052022-10-21 22:13:23 +000045typedef void (*APH_sendHint)(APerformanceHintSession* session, int32_t);
Bo Liu6a3fc602021-07-17 16:42:13 -040046typedef void (*APH_closeSession)(APerformanceHintSession* session);
47
48bool gAPerformanceHintBindingInitialized = false;
49APH_getManager gAPH_getManagerFn = nullptr;
50APH_createSession gAPH_createSessionFn = nullptr;
51APH_updateTargetWorkDuration gAPH_updateTargetWorkDurationFn = nullptr;
52APH_reportActualWorkDuration gAPH_reportActualWorkDurationFn = nullptr;
Matt Buckleyd98e8052022-10-21 22:13:23 +000053APH_sendHint gAPH_sendHintFn = nullptr;
Bo Liu6a3fc602021-07-17 16:42:13 -040054APH_closeSession gAPH_closeSessionFn = nullptr;
55
56void ensureAPerformanceHintBindingInitialized() {
57 if (gAPerformanceHintBindingInitialized) return;
58
59 void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
60 LOG_ALWAYS_FATAL_IF(handle_ == nullptr, "Failed to dlopen libandroid.so!");
61
62 gAPH_getManagerFn = (APH_getManager)dlsym(handle_, "APerformanceHint_getManager");
63 LOG_ALWAYS_FATAL_IF(gAPH_getManagerFn == nullptr,
64 "Failed to find required symbol APerformanceHint_getManager!");
65
66 gAPH_createSessionFn = (APH_createSession)dlsym(handle_, "APerformanceHint_createSession");
67 LOG_ALWAYS_FATAL_IF(gAPH_createSessionFn == nullptr,
68 "Failed to find required symbol APerformanceHint_createSession!");
69
70 gAPH_updateTargetWorkDurationFn = (APH_updateTargetWorkDuration)dlsym(
71 handle_, "APerformanceHint_updateTargetWorkDuration");
72 LOG_ALWAYS_FATAL_IF(
73 gAPH_updateTargetWorkDurationFn == nullptr,
74 "Failed to find required symbol APerformanceHint_updateTargetWorkDuration!");
75
76 gAPH_reportActualWorkDurationFn = (APH_reportActualWorkDuration)dlsym(
77 handle_, "APerformanceHint_reportActualWorkDuration");
78 LOG_ALWAYS_FATAL_IF(
79 gAPH_reportActualWorkDurationFn == nullptr,
80 "Failed to find required symbol APerformanceHint_reportActualWorkDuration!");
81
Matt Buckleyd98e8052022-10-21 22:13:23 +000082 gAPH_sendHintFn = (APH_sendHint)dlsym(handle_, "APerformanceHint_sendHint");
83 LOG_ALWAYS_FATAL_IF(gAPH_sendHintFn == nullptr,
84 "Failed to find required symbol APerformanceHint_sendHint!");
85
Bo Liu6a3fc602021-07-17 16:42:13 -040086 gAPH_closeSessionFn = (APH_closeSession)dlsym(handle_, "APerformanceHint_closeSession");
87 LOG_ALWAYS_FATAL_IF(gAPH_closeSessionFn == nullptr,
88 "Failed to find required symbol APerformanceHint_closeSession!");
89
90 gAPerformanceHintBindingInitialized = true;
91}
92
93} // namespace
94
John Reck18f16e62014-05-02 16:46:41 -070095DrawFrameTask::DrawFrameTask()
Chris Craikd41c4d82015-01-05 15:51:13 -080096 : mRenderThread(nullptr)
97 , mContext(nullptr)
John Reckf138b172017-09-08 11:00:42 -070098 , mContentDrawBounds(0, 0, 0, 0)
John Reck1bcacfd2017-11-03 10:12:19 -070099 , mSyncResult(SyncResult::OK) {}
John Reck668f0e32014-03-26 15:10:40 -0700100
John Reck1bcacfd2017-11-03 10:12:19 -0700101DrawFrameTask::~DrawFrameTask() {}
John Reck668f0e32014-03-26 15:10:40 -0700102
Bo Liu6a3fc602021-07-17 16:42:13 -0400103void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context, RenderNode* targetNode,
104 int32_t uiThreadId, int32_t renderThreadId) {
John Reck18f16e62014-05-02 16:46:41 -0700105 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -0700106 mContext = context;
Skuhneea7a7fb2015-08-28 07:10:31 -0700107 mTargetNode = targetNode;
Bo Liu6a3fc602021-07-17 16:42:13 -0400108 mUiThreadId = uiThreadId;
109 mRenderThreadId = renderThreadId;
Bo Liu027b2182021-03-18 16:50:38 -0400110}
111
John Reckd72e0a32014-05-29 18:56:11 -0700112void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700113 LOG_ALWAYS_FATAL_IF(!mContext,
114 "Lifecycle violation, there's no context to pushLayerUpdate with!");
John Reck087bc0c2014-04-04 16:20:08 -0700115
John Reckd72e0a32014-05-29 18:56:11 -0700116 for (size_t i = 0; i < mLayers.size(); i++) {
117 if (mLayers[i].get() == layer) {
118 return;
119 }
120 }
121 mLayers.push_back(layer);
John Reck668f0e32014-03-26 15:10:40 -0700122}
123
John Reckd72e0a32014-05-29 18:56:11 -0700124void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -0700125 for (size_t i = 0; i < mLayers.size(); i++) {
John Reckd72e0a32014-05-29 18:56:11 -0700126 if (mLayers[i].get() == layer) {
127 mLayers.erase(mLayers.begin() + i);
128 return;
John Reck668f0e32014-03-26 15:10:40 -0700129 }
130 }
131}
132
John Reck2de950d2017-01-25 10:58:30 -0800133int DrawFrameTask::drawFrame() {
John Reck668f0e32014-03-26 15:10:40 -0700134 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
135
John Reck9a17da82016-04-18 11:17:52 -0700136 mSyncResult = SyncResult::OK;
Jerome Gaillarde218c692019-06-14 12:58:57 +0100137 mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck18f16e62014-05-02 16:46:41 -0700138 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -0700139
John Reckf9be7792014-05-02 18:21:16 -0700140 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -0700141}
142
John Reck18f16e62014-05-02 16:46:41 -0700143void DrawFrameTask::postAndWait() {
Jimmy Shiu06d91392022-03-15 19:35:45 +0800144 ATRACE_CALL();
John Reck087bc0c2014-04-04 16:20:08 -0700145 AutoMutex _lock(mLock);
John Reckf8441e62017-10-23 13:10:41 -0700146 mRenderThread->queue().post([this]() { run(); });
Chris Craik738ec3a2014-07-25 18:25:02 +0000147 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -0700148}
149
John Reck668f0e32014-03-26 15:10:40 -0700150void DrawFrameTask::run() {
Ady Abraham150816c2021-03-01 10:46:40 -0800151 const int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
152 ATRACE_FORMAT("DrawFrames %" PRId64, vsyncId);
Bo Liu4d0f1f12021-05-06 16:49:40 -0400153 nsecs_t syncDelayDuration = systemTime(SYSTEM_TIME_MONOTONIC) - mSyncQueued;
John Reck668f0e32014-03-26 15:10:40 -0700154
John Recka5dda642014-05-22 15:43:54 -0700155 bool canUnblockUiThread;
156 bool canDrawThisFrame;
Matt Buckley864ab952022-10-18 23:03:59 +0000157 bool didDraw = false;
John Recka5dda642014-05-22 15:43:54 -0700158 {
Chris Craike2e53a72015-10-28 15:55:40 -0700159 TreeInfo info(TreeInfo::MODE_FULL, *mContext);
chaviwadba0b12022-03-18 17:42:15 -0500160 info.forceDrawFrame = mForceDrawFrame;
161 mForceDrawFrame = false;
John Recka5dda642014-05-22 15:43:54 -0700162 canUnblockUiThread = syncFrameState(info);
163 canDrawThisFrame = info.out.canDrawThisFrame;
John Reckcc2eee82018-05-17 10:44:00 -0700164
chaviw9c137532021-08-20 12:15:48 -0500165 if (mFrameCommitCallback) {
166 mContext->addFrameCommitListener(std::move(mFrameCommitCallback));
167 mFrameCommitCallback = nullptr;
John Reckcc2eee82018-05-17 10:44:00 -0700168 }
John Recka5dda642014-05-22 15:43:54 -0700169 }
John Reck668f0e32014-03-26 15:10:40 -0700170
171 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -0700172 CanvasContext* context = mContext;
chaviwb6803712021-12-13 15:46:29 -0600173 std::function<std::function<void(bool)>(int32_t, int64_t)> frameCallback =
174 std::move(mFrameCallback);
chaviw9c137532021-08-20 12:15:48 -0500175 std::function<void()> frameCompleteCallback = std::move(mFrameCompleteCallback);
Jorim Jaggi7a5addd2018-04-26 23:23:29 +0200176 mFrameCallback = nullptr;
chaviw9c137532021-08-20 12:15:48 -0500177 mFrameCompleteCallback = nullptr;
Bo Liu027b2182021-03-18 16:50:38 -0400178 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
179 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
180 int64_t frameStartTime = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameStartTime)];
John Reck668f0e32014-03-26 15:10:40 -0700181
John Reck668f0e32014-03-26 15:10:40 -0700182 // From this point on anything in "this" is *UNSAFE TO ACCESS*
183 if (canUnblockUiThread) {
184 unblockUiThread();
185 }
186
Mihai Popa95688002018-02-23 16:10:11 +0000187 // Even if we aren't drawing this vsync pulse the next frame number will still be accurate
chaviw9c137532021-08-20 12:15:48 -0500188 if (CC_UNLIKELY(frameCallback)) {
chaviwb6803712021-12-13 15:46:29 -0600189 context->enqueueFrameWork([frameCallback, context, syncResult = mSyncResult,
190 frameNr = context->getFrameNumber()]() {
191 auto frameCommitCallback = std::move(frameCallback(syncResult, frameNr));
192 if (frameCommitCallback) {
193 context->addFrameCommitListener(std::move(frameCommitCallback));
194 }
195 });
Mihai Popa95688002018-02-23 16:10:11 +0000196 }
197
Bo Liu4d0f1f12021-05-06 16:49:40 -0400198 nsecs_t dequeueBufferDuration = 0;
John Recka5dda642014-05-22 15:43:54 -0700199 if (CC_LIKELY(canDrawThisFrame)) {
Matt Buckley864ab952022-10-18 23:03:59 +0000200 std::optional<nsecs_t> drawResult = context->draw();
201 didDraw = drawResult.has_value();
202 dequeueBufferDuration = drawResult.value_or(0);
Chris Craik06e2e9c2016-08-31 17:32:46 -0700203 } else {
John Reckcf1170f2021-07-14 15:52:19 -0400204 // Do a flush in case syncFrameState performed any texture uploads. Since we skipped
205 // the draw() call, those uploads (or deletes) will end up sitting in the queue.
206 // Do them now
207 if (GrDirectContext* grContext = mRenderThread->getGrContext()) {
208 grContext->flushAndSubmit();
209 }
Chris Craik06e2e9c2016-08-31 17:32:46 -0700210 // wait on fences so tasks don't overlap next frame
211 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700212 }
John Reck668f0e32014-03-26 15:10:40 -0700213
chaviw9c137532021-08-20 12:15:48 -0500214 if (CC_UNLIKELY(frameCompleteCallback)) {
215 std::invoke(frameCompleteCallback);
216 }
217
John Reck668f0e32014-03-26 15:10:40 -0700218 if (!canUnblockUiThread) {
219 unblockUiThread();
220 }
Bo Liu027b2182021-03-18 16:50:38 -0400221
Bo Liu6a3fc602021-07-17 16:42:13 -0400222 if (!mHintSessionWrapper) mHintSessionWrapper.emplace(mUiThreadId, mRenderThreadId);
Matt Buckley864ab952022-10-18 23:03:59 +0000223
224 constexpr int64_t kSanityCheckLowerBound = 100_us;
225 constexpr int64_t kSanityCheckUpperBound = 10_s;
Bo Liu6a3fc602021-07-17 16:42:13 -0400226 int64_t targetWorkDuration = frameDeadline - intendedVsync;
227 targetWorkDuration = targetWorkDuration * Properties::targetCpuTimePercentage / 100;
228 if (targetWorkDuration > kSanityCheckLowerBound &&
229 targetWorkDuration < kSanityCheckUpperBound &&
230 targetWorkDuration != mLastTargetWorkDuration) {
231 mLastTargetWorkDuration = targetWorkDuration;
232 mHintSessionWrapper->updateTargetWorkDuration(targetWorkDuration);
Bo Liu027b2182021-03-18 16:50:38 -0400233 }
Matt Buckley864ab952022-10-18 23:03:59 +0000234
235 if (didDraw) {
236 int64_t frameDuration = systemTime(SYSTEM_TIME_MONOTONIC) - frameStartTime;
237 int64_t actualDuration = frameDuration -
238 (std::min(syncDelayDuration, mLastDequeueBufferDuration)) -
239 dequeueBufferDuration;
240 if (actualDuration > kSanityCheckLowerBound && actualDuration < kSanityCheckUpperBound) {
241 mHintSessionWrapper->reportActualWorkDuration(actualDuration);
242 }
Bo Liu6a3fc602021-07-17 16:42:13 -0400243 }
244
Bo Liu4d0f1f12021-05-06 16:49:40 -0400245 mLastDequeueBufferDuration = dequeueBufferDuration;
John Reck668f0e32014-03-26 15:10:40 -0700246}
247
Matt Buckleyd98e8052022-10-21 22:13:23 +0000248void DrawFrameTask::sendLoadResetHint() {
249 if (!(Properties::useHintManager && Properties::isDrawingEnabled())) return;
250 if (!mHintSessionWrapper) mHintSessionWrapper.emplace(mUiThreadId, mRenderThreadId);
251 nsecs_t now = systemTime();
252 if (now - mLastFrameNotification > kResetHintTimeout) {
253 mHintSessionWrapper->sendHint(SessionHint::CPU_LOAD_RESET);
254 }
255 mLastFrameNotification = now;
256}
257
John Recka5dda642014-05-22 15:43:54 -0700258bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700259 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700260 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700261 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
262 int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
Ady Abrahamdfb13982020-10-05 17:59:09 -0700263 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100264 int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)];
265 mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline,
266 frameInterval);
John Reck8afcc762016-04-13 10:24:06 -0700267 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400268 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700269
270 for (size_t i = 0; i < mLayers.size(); i++) {
Jessie Hao10f88672022-05-10 14:47:05 +0800271 if (mLayers[i]) {
272 mLayers[i]->apply();
273 }
John Reckd72e0a32014-05-29 18:56:11 -0700274 }
275 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700276 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700277 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700278
John Reckaa95a882014-11-07 11:02:07 -0800279 // This is after the prepareTree so that any pending operations
280 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
John Reck8afcc762016-04-13 10:24:06 -0700281 if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
John Reck9a17da82016-04-18 11:17:52 -0700282 if (!mContext->hasSurface()) {
283 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
284 } else {
285 // If we have a surface but can't draw we must be stopped
286 mSyncResult |= SyncResult::ContextIsStopped;
287 }
John Reck8afcc762016-04-13 10:24:06 -0700288 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800289 }
290
John Reckf9be7792014-05-02 18:21:16 -0700291 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700292 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700293 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700294 }
John Reck52244ff2014-05-01 21:27:37 -0700295 }
John Reckcc2eee82018-05-17 10:44:00 -0700296 if (!info.out.canDrawThisFrame) {
297 mSyncResult |= SyncResult::FrameDropped;
298 }
John Reck860d1552014-04-11 19:15:05 -0700299 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700300 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700301}
302
303void DrawFrameTask::unblockUiThread() {
304 AutoMutex _lock(mLock);
305 mSignal.signal();
306}
307
Bo Liu6a3fc602021-07-17 16:42:13 -0400308DrawFrameTask::HintSessionWrapper::HintSessionWrapper(int32_t uiThreadId, int32_t renderThreadId) {
309 if (!Properties::useHintManager) return;
310 if (uiThreadId < 0 || renderThreadId < 0) return;
311
312 ensureAPerformanceHintBindingInitialized();
313
314 APerformanceHintManager* manager = gAPH_getManagerFn();
315 if (!manager) return;
316
317 std::vector<int32_t> tids = CommonPool::getThreadIds();
318 tids.push_back(uiThreadId);
319 tids.push_back(renderThreadId);
320
321 // DrawFrameTask code will always set a target duration before reporting actual durations.
322 // So this is just a placeholder value that's never used.
323 int64_t dummyTargetDurationNanos = 16666667;
324 mHintSession =
325 gAPH_createSessionFn(manager, tids.data(), tids.size(), dummyTargetDurationNanos);
326}
327
328DrawFrameTask::HintSessionWrapper::~HintSessionWrapper() {
329 if (mHintSession) {
330 gAPH_closeSessionFn(mHintSession);
331 }
332}
333
334void DrawFrameTask::HintSessionWrapper::updateTargetWorkDuration(long targetDurationNanos) {
335 if (mHintSession) {
336 gAPH_updateTargetWorkDurationFn(mHintSession, targetDurationNanos);
337 }
338}
339
340void DrawFrameTask::HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) {
341 if (mHintSession) {
342 gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos);
343 }
344}
345
Matt Buckleyd98e8052022-10-21 22:13:23 +0000346void DrawFrameTask::HintSessionWrapper::sendHint(SessionHint hint) {
347 if (mHintSession && Properties::isDrawingEnabled()) {
348 gAPH_sendHintFn(mHintSession, static_cast<int>(hint));
349 }
350}
351
John Reck668f0e32014-03-26 15:10:40 -0700352} /* namespace renderthread */
353} /* namespace uirenderer */
354} /* namespace android */