blob: dc7676c08bd7c0482705e35962bc5e4e1574e003 [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);
45typedef void (*APH_closeSession)(APerformanceHintSession* session);
46
47bool gAPerformanceHintBindingInitialized = false;
48APH_getManager gAPH_getManagerFn = nullptr;
49APH_createSession gAPH_createSessionFn = nullptr;
50APH_updateTargetWorkDuration gAPH_updateTargetWorkDurationFn = nullptr;
51APH_reportActualWorkDuration gAPH_reportActualWorkDurationFn = nullptr;
52APH_closeSession gAPH_closeSessionFn = nullptr;
53
54void ensureAPerformanceHintBindingInitialized() {
55 if (gAPerformanceHintBindingInitialized) return;
56
57 void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
58 LOG_ALWAYS_FATAL_IF(handle_ == nullptr, "Failed to dlopen libandroid.so!");
59
60 gAPH_getManagerFn = (APH_getManager)dlsym(handle_, "APerformanceHint_getManager");
61 LOG_ALWAYS_FATAL_IF(gAPH_getManagerFn == nullptr,
62 "Failed to find required symbol APerformanceHint_getManager!");
63
64 gAPH_createSessionFn = (APH_createSession)dlsym(handle_, "APerformanceHint_createSession");
65 LOG_ALWAYS_FATAL_IF(gAPH_createSessionFn == nullptr,
66 "Failed to find required symbol APerformanceHint_createSession!");
67
68 gAPH_updateTargetWorkDurationFn = (APH_updateTargetWorkDuration)dlsym(
69 handle_, "APerformanceHint_updateTargetWorkDuration");
70 LOG_ALWAYS_FATAL_IF(
71 gAPH_updateTargetWorkDurationFn == nullptr,
72 "Failed to find required symbol APerformanceHint_updateTargetWorkDuration!");
73
74 gAPH_reportActualWorkDurationFn = (APH_reportActualWorkDuration)dlsym(
75 handle_, "APerformanceHint_reportActualWorkDuration");
76 LOG_ALWAYS_FATAL_IF(
77 gAPH_reportActualWorkDurationFn == nullptr,
78 "Failed to find required symbol APerformanceHint_reportActualWorkDuration!");
79
80 gAPH_closeSessionFn = (APH_closeSession)dlsym(handle_, "APerformanceHint_closeSession");
81 LOG_ALWAYS_FATAL_IF(gAPH_closeSessionFn == nullptr,
82 "Failed to find required symbol APerformanceHint_closeSession!");
83
84 gAPerformanceHintBindingInitialized = true;
85}
86
87} // namespace
88
John Reck18f16e62014-05-02 16:46:41 -070089DrawFrameTask::DrawFrameTask()
Chris Craikd41c4d82015-01-05 15:51:13 -080090 : mRenderThread(nullptr)
91 , mContext(nullptr)
John Reckf138b172017-09-08 11:00:42 -070092 , mContentDrawBounds(0, 0, 0, 0)
John Reck1bcacfd2017-11-03 10:12:19 -070093 , mSyncResult(SyncResult::OK) {}
John Reck668f0e32014-03-26 15:10:40 -070094
John Reck1bcacfd2017-11-03 10:12:19 -070095DrawFrameTask::~DrawFrameTask() {}
John Reck668f0e32014-03-26 15:10:40 -070096
Bo Liu6a3fc602021-07-17 16:42:13 -040097void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context, RenderNode* targetNode,
98 int32_t uiThreadId, int32_t renderThreadId) {
John Reck18f16e62014-05-02 16:46:41 -070099 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -0700100 mContext = context;
Skuhneea7a7fb2015-08-28 07:10:31 -0700101 mTargetNode = targetNode;
Bo Liu6a3fc602021-07-17 16:42:13 -0400102 mUiThreadId = uiThreadId;
103 mRenderThreadId = renderThreadId;
Bo Liu027b2182021-03-18 16:50:38 -0400104}
105
John Reckd72e0a32014-05-29 18:56:11 -0700106void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700107 LOG_ALWAYS_FATAL_IF(!mContext,
108 "Lifecycle violation, there's no context to pushLayerUpdate with!");
John Reck087bc0c2014-04-04 16:20:08 -0700109
John Reckd72e0a32014-05-29 18:56:11 -0700110 for (size_t i = 0; i < mLayers.size(); i++) {
111 if (mLayers[i].get() == layer) {
112 return;
113 }
114 }
115 mLayers.push_back(layer);
John Reck668f0e32014-03-26 15:10:40 -0700116}
117
John Reckd72e0a32014-05-29 18:56:11 -0700118void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -0700119 for (size_t i = 0; i < mLayers.size(); i++) {
John Reckd72e0a32014-05-29 18:56:11 -0700120 if (mLayers[i].get() == layer) {
121 mLayers.erase(mLayers.begin() + i);
122 return;
John Reck668f0e32014-03-26 15:10:40 -0700123 }
124 }
125}
126
John Reck2de950d2017-01-25 10:58:30 -0800127int DrawFrameTask::drawFrame() {
John Reck668f0e32014-03-26 15:10:40 -0700128 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
129
John Reck9a17da82016-04-18 11:17:52 -0700130 mSyncResult = SyncResult::OK;
Jerome Gaillarde218c692019-06-14 12:58:57 +0100131 mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck18f16e62014-05-02 16:46:41 -0700132 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -0700133
John Reckf9be7792014-05-02 18:21:16 -0700134 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -0700135}
136
John Reck18f16e62014-05-02 16:46:41 -0700137void DrawFrameTask::postAndWait() {
Jimmy Shiu06d91392022-03-15 19:35:45 +0800138 ATRACE_CALL();
John Reck087bc0c2014-04-04 16:20:08 -0700139 AutoMutex _lock(mLock);
John Reckf8441e62017-10-23 13:10:41 -0700140 mRenderThread->queue().post([this]() { run(); });
Chris Craik738ec3a2014-07-25 18:25:02 +0000141 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -0700142}
143
John Reck668f0e32014-03-26 15:10:40 -0700144void DrawFrameTask::run() {
Ady Abraham150816c2021-03-01 10:46:40 -0800145 const int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
146 ATRACE_FORMAT("DrawFrames %" PRId64, vsyncId);
Bo Liu4d0f1f12021-05-06 16:49:40 -0400147 nsecs_t syncDelayDuration = systemTime(SYSTEM_TIME_MONOTONIC) - mSyncQueued;
John Reck668f0e32014-03-26 15:10:40 -0700148
John Recka5dda642014-05-22 15:43:54 -0700149 bool canUnblockUiThread;
150 bool canDrawThisFrame;
Matt Buckley864ab952022-10-18 23:03:59 +0000151 bool didDraw = false;
John Recka5dda642014-05-22 15:43:54 -0700152 {
Chris Craike2e53a72015-10-28 15:55:40 -0700153 TreeInfo info(TreeInfo::MODE_FULL, *mContext);
chaviwadba0b12022-03-18 17:42:15 -0500154 info.forceDrawFrame = mForceDrawFrame;
155 mForceDrawFrame = false;
John Recka5dda642014-05-22 15:43:54 -0700156 canUnblockUiThread = syncFrameState(info);
157 canDrawThisFrame = info.out.canDrawThisFrame;
John Reckcc2eee82018-05-17 10:44:00 -0700158
chaviw9c137532021-08-20 12:15:48 -0500159 if (mFrameCommitCallback) {
160 mContext->addFrameCommitListener(std::move(mFrameCommitCallback));
161 mFrameCommitCallback = nullptr;
John Reckcc2eee82018-05-17 10:44:00 -0700162 }
John Recka5dda642014-05-22 15:43:54 -0700163 }
John Reck668f0e32014-03-26 15:10:40 -0700164
165 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -0700166 CanvasContext* context = mContext;
chaviwb6803712021-12-13 15:46:29 -0600167 std::function<std::function<void(bool)>(int32_t, int64_t)> frameCallback =
168 std::move(mFrameCallback);
chaviw9c137532021-08-20 12:15:48 -0500169 std::function<void()> frameCompleteCallback = std::move(mFrameCompleteCallback);
Jorim Jaggi7a5addd2018-04-26 23:23:29 +0200170 mFrameCallback = nullptr;
chaviw9c137532021-08-20 12:15:48 -0500171 mFrameCompleteCallback = nullptr;
Bo Liu027b2182021-03-18 16:50:38 -0400172 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
173 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
174 int64_t frameStartTime = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameStartTime)];
John Reck668f0e32014-03-26 15:10:40 -0700175
John Reck668f0e32014-03-26 15:10:40 -0700176 // From this point on anything in "this" is *UNSAFE TO ACCESS*
177 if (canUnblockUiThread) {
178 unblockUiThread();
179 }
180
Mihai Popa95688002018-02-23 16:10:11 +0000181 // Even if we aren't drawing this vsync pulse the next frame number will still be accurate
chaviw9c137532021-08-20 12:15:48 -0500182 if (CC_UNLIKELY(frameCallback)) {
chaviwb6803712021-12-13 15:46:29 -0600183 context->enqueueFrameWork([frameCallback, context, syncResult = mSyncResult,
184 frameNr = context->getFrameNumber()]() {
185 auto frameCommitCallback = std::move(frameCallback(syncResult, frameNr));
186 if (frameCommitCallback) {
187 context->addFrameCommitListener(std::move(frameCommitCallback));
188 }
189 });
Mihai Popa95688002018-02-23 16:10:11 +0000190 }
191
Bo Liu4d0f1f12021-05-06 16:49:40 -0400192 nsecs_t dequeueBufferDuration = 0;
John Recka5dda642014-05-22 15:43:54 -0700193 if (CC_LIKELY(canDrawThisFrame)) {
Matt Buckley864ab952022-10-18 23:03:59 +0000194 std::optional<nsecs_t> drawResult = context->draw();
195 didDraw = drawResult.has_value();
196 dequeueBufferDuration = drawResult.value_or(0);
Chris Craik06e2e9c2016-08-31 17:32:46 -0700197 } else {
John Reckcf1170f2021-07-14 15:52:19 -0400198 // Do a flush in case syncFrameState performed any texture uploads. Since we skipped
199 // the draw() call, those uploads (or deletes) will end up sitting in the queue.
200 // Do them now
201 if (GrDirectContext* grContext = mRenderThread->getGrContext()) {
202 grContext->flushAndSubmit();
203 }
Chris Craik06e2e9c2016-08-31 17:32:46 -0700204 // wait on fences so tasks don't overlap next frame
205 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700206 }
John Reck668f0e32014-03-26 15:10:40 -0700207
chaviw9c137532021-08-20 12:15:48 -0500208 if (CC_UNLIKELY(frameCompleteCallback)) {
209 std::invoke(frameCompleteCallback);
210 }
211
John Reck668f0e32014-03-26 15:10:40 -0700212 if (!canUnblockUiThread) {
213 unblockUiThread();
214 }
Bo Liu027b2182021-03-18 16:50:38 -0400215
Bo Liu6a3fc602021-07-17 16:42:13 -0400216 if (!mHintSessionWrapper) mHintSessionWrapper.emplace(mUiThreadId, mRenderThreadId);
Matt Buckley864ab952022-10-18 23:03:59 +0000217
218 constexpr int64_t kSanityCheckLowerBound = 100_us;
219 constexpr int64_t kSanityCheckUpperBound = 10_s;
Bo Liu6a3fc602021-07-17 16:42:13 -0400220 int64_t targetWorkDuration = frameDeadline - intendedVsync;
221 targetWorkDuration = targetWorkDuration * Properties::targetCpuTimePercentage / 100;
222 if (targetWorkDuration > kSanityCheckLowerBound &&
223 targetWorkDuration < kSanityCheckUpperBound &&
224 targetWorkDuration != mLastTargetWorkDuration) {
225 mLastTargetWorkDuration = targetWorkDuration;
226 mHintSessionWrapper->updateTargetWorkDuration(targetWorkDuration);
Bo Liu027b2182021-03-18 16:50:38 -0400227 }
Matt Buckley864ab952022-10-18 23:03:59 +0000228
229 if (didDraw) {
230 int64_t frameDuration = systemTime(SYSTEM_TIME_MONOTONIC) - frameStartTime;
231 int64_t actualDuration = frameDuration -
232 (std::min(syncDelayDuration, mLastDequeueBufferDuration)) -
233 dequeueBufferDuration;
234 if (actualDuration > kSanityCheckLowerBound && actualDuration < kSanityCheckUpperBound) {
235 mHintSessionWrapper->reportActualWorkDuration(actualDuration);
236 }
Bo Liu6a3fc602021-07-17 16:42:13 -0400237 }
238
Bo Liu4d0f1f12021-05-06 16:49:40 -0400239 mLastDequeueBufferDuration = dequeueBufferDuration;
John Reck668f0e32014-03-26 15:10:40 -0700240}
241
John Recka5dda642014-05-22 15:43:54 -0700242bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700243 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700244 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700245 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
246 int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
Ady Abrahamdfb13982020-10-05 17:59:09 -0700247 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100248 int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)];
249 mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline,
250 frameInterval);
John Reck8afcc762016-04-13 10:24:06 -0700251 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400252 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700253
254 for (size_t i = 0; i < mLayers.size(); i++) {
Jessie Hao10f88672022-05-10 14:47:05 +0800255 if (mLayers[i]) {
256 mLayers[i]->apply();
257 }
John Reckd72e0a32014-05-29 18:56:11 -0700258 }
259 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700260 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700261 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700262
John Reckaa95a882014-11-07 11:02:07 -0800263 // This is after the prepareTree so that any pending operations
264 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
John Reck8afcc762016-04-13 10:24:06 -0700265 if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
John Reck9a17da82016-04-18 11:17:52 -0700266 if (!mContext->hasSurface()) {
267 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
268 } else {
269 // If we have a surface but can't draw we must be stopped
270 mSyncResult |= SyncResult::ContextIsStopped;
271 }
John Reck8afcc762016-04-13 10:24:06 -0700272 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800273 }
274
John Reckf9be7792014-05-02 18:21:16 -0700275 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700276 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700277 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700278 }
John Reck52244ff2014-05-01 21:27:37 -0700279 }
John Reckcc2eee82018-05-17 10:44:00 -0700280 if (!info.out.canDrawThisFrame) {
281 mSyncResult |= SyncResult::FrameDropped;
282 }
John Reck860d1552014-04-11 19:15:05 -0700283 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700284 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700285}
286
287void DrawFrameTask::unblockUiThread() {
288 AutoMutex _lock(mLock);
289 mSignal.signal();
290}
291
Bo Liu6a3fc602021-07-17 16:42:13 -0400292DrawFrameTask::HintSessionWrapper::HintSessionWrapper(int32_t uiThreadId, int32_t renderThreadId) {
293 if (!Properties::useHintManager) return;
294 if (uiThreadId < 0 || renderThreadId < 0) return;
295
296 ensureAPerformanceHintBindingInitialized();
297
298 APerformanceHintManager* manager = gAPH_getManagerFn();
299 if (!manager) return;
300
301 std::vector<int32_t> tids = CommonPool::getThreadIds();
302 tids.push_back(uiThreadId);
303 tids.push_back(renderThreadId);
304
305 // DrawFrameTask code will always set a target duration before reporting actual durations.
306 // So this is just a placeholder value that's never used.
307 int64_t dummyTargetDurationNanos = 16666667;
308 mHintSession =
309 gAPH_createSessionFn(manager, tids.data(), tids.size(), dummyTargetDurationNanos);
310}
311
312DrawFrameTask::HintSessionWrapper::~HintSessionWrapper() {
313 if (mHintSession) {
314 gAPH_closeSessionFn(mHintSession);
315 }
316}
317
318void DrawFrameTask::HintSessionWrapper::updateTargetWorkDuration(long targetDurationNanos) {
319 if (mHintSession) {
320 gAPH_updateTargetWorkDurationFn(mHintSession, targetDurationNanos);
321 }
322}
323
324void DrawFrameTask::HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) {
325 if (mHintSession) {
326 gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos);
327 }
328}
329
John Reck668f0e32014-03-26 15:10:40 -0700330} /* namespace renderthread */
331} /* namespace uirenderer */
332} /* namespace android */