blob: 59c914f0198c7e8787c430113dfbc4b64946fe7b [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>
Bo Liu4d0f1f12021-05-06 16:49:40 -040022#include <algorithm>
John Reck668f0e32014-03-26 15:10:40 -070023
John Reckd72e0a32014-05-29 18:56:11 -070024#include "../DeferredLayerUpdater.h"
John Reck668f0e32014-03-26 15:10:40 -070025#include "../DisplayList.h"
Bo Liu027b2182021-03-18 16:50:38 -040026#include "../Properties.h"
John Reck668f0e32014-03-26 15:10:40 -070027#include "../RenderNode.h"
28#include "CanvasContext.h"
29#include "RenderThread.h"
Bo Liu6a3fc602021-07-17 16:42:13 -040030#include "thread/CommonPool.h"
John Reck668f0e32014-03-26 15:10:40 -070031
32namespace android {
33namespace uirenderer {
34namespace renderthread {
35
Bo Liu6a3fc602021-07-17 16:42:13 -040036namespace {
37
38typedef APerformanceHintManager* (*APH_getManager)();
39typedef APerformanceHintSession* (*APH_createSession)(APerformanceHintManager*, const int32_t*,
40 size_t, int64_t);
41typedef void (*APH_updateTargetWorkDuration)(APerformanceHintSession*, int64_t);
42typedef void (*APH_reportActualWorkDuration)(APerformanceHintSession*, int64_t);
43typedef void (*APH_closeSession)(APerformanceHintSession* session);
44
45bool gAPerformanceHintBindingInitialized = false;
46APH_getManager gAPH_getManagerFn = nullptr;
47APH_createSession gAPH_createSessionFn = nullptr;
48APH_updateTargetWorkDuration gAPH_updateTargetWorkDurationFn = nullptr;
49APH_reportActualWorkDuration gAPH_reportActualWorkDurationFn = nullptr;
50APH_closeSession gAPH_closeSessionFn = nullptr;
51
52void 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 Reck18f16e62014-05-02 16:46:41 -070087DrawFrameTask::DrawFrameTask()
Chris Craikd41c4d82015-01-05 15:51:13 -080088 : mRenderThread(nullptr)
89 , mContext(nullptr)
John Reckf138b172017-09-08 11:00:42 -070090 , mContentDrawBounds(0, 0, 0, 0)
John Reck1bcacfd2017-11-03 10:12:19 -070091 , mSyncResult(SyncResult::OK) {}
John Reck668f0e32014-03-26 15:10:40 -070092
John Reck1bcacfd2017-11-03 10:12:19 -070093DrawFrameTask::~DrawFrameTask() {}
John Reck668f0e32014-03-26 15:10:40 -070094
Bo Liu6a3fc602021-07-17 16:42:13 -040095void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context, RenderNode* targetNode,
96 int32_t uiThreadId, int32_t renderThreadId) {
John Reck18f16e62014-05-02 16:46:41 -070097 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070098 mContext = context;
Skuhneea7a7fb2015-08-28 07:10:31 -070099 mTargetNode = targetNode;
Bo Liu6a3fc602021-07-17 16:42:13 -0400100 mUiThreadId = uiThreadId;
101 mRenderThreadId = renderThreadId;
Bo Liu027b2182021-03-18 16:50:38 -0400102}
103
John Reckd72e0a32014-05-29 18:56:11 -0700104void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700105 LOG_ALWAYS_FATAL_IF(!mContext,
106 "Lifecycle violation, there's no context to pushLayerUpdate with!");
John Reck087bc0c2014-04-04 16:20:08 -0700107
John Reckd72e0a32014-05-29 18:56:11 -0700108 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 Reck668f0e32014-03-26 15:10:40 -0700114}
115
John Reckd72e0a32014-05-29 18:56:11 -0700116void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -0700117 for (size_t i = 0; i < mLayers.size(); i++) {
John Reckd72e0a32014-05-29 18:56:11 -0700118 if (mLayers[i].get() == layer) {
119 mLayers.erase(mLayers.begin() + i);
120 return;
John Reck668f0e32014-03-26 15:10:40 -0700121 }
122 }
123}
124
John Reck2de950d2017-01-25 10:58:30 -0800125int DrawFrameTask::drawFrame() {
John Reck668f0e32014-03-26 15:10:40 -0700126 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
127
John Reck9a17da82016-04-18 11:17:52 -0700128 mSyncResult = SyncResult::OK;
Jerome Gaillarde218c692019-06-14 12:58:57 +0100129 mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck18f16e62014-05-02 16:46:41 -0700130 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -0700131
John Reckf9be7792014-05-02 18:21:16 -0700132 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -0700133}
134
John Reck18f16e62014-05-02 16:46:41 -0700135void DrawFrameTask::postAndWait() {
Jimmy Shiu06d91392022-03-15 19:35:45 +0800136 ATRACE_CALL();
John Reck087bc0c2014-04-04 16:20:08 -0700137 AutoMutex _lock(mLock);
John Reckf8441e62017-10-23 13:10:41 -0700138 mRenderThread->queue().post([this]() { run(); });
Chris Craik738ec3a2014-07-25 18:25:02 +0000139 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -0700140}
141
John Reck668f0e32014-03-26 15:10:40 -0700142void DrawFrameTask::run() {
Ady Abraham150816c2021-03-01 10:46:40 -0800143 const int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
144 ATRACE_FORMAT("DrawFrames %" PRId64, vsyncId);
Bo Liu4d0f1f12021-05-06 16:49:40 -0400145 nsecs_t syncDelayDuration = systemTime(SYSTEM_TIME_MONOTONIC) - mSyncQueued;
John Reck668f0e32014-03-26 15:10:40 -0700146
John Recka5dda642014-05-22 15:43:54 -0700147 bool canUnblockUiThread;
148 bool canDrawThisFrame;
149 {
Chris Craike2e53a72015-10-28 15:55:40 -0700150 TreeInfo info(TreeInfo::MODE_FULL, *mContext);
chaviwadba0b12022-03-18 17:42:15 -0500151 info.forceDrawFrame = mForceDrawFrame;
152 mForceDrawFrame = false;
John Recka5dda642014-05-22 15:43:54 -0700153 canUnblockUiThread = syncFrameState(info);
154 canDrawThisFrame = info.out.canDrawThisFrame;
John Reckcc2eee82018-05-17 10:44:00 -0700155
chaviw9c137532021-08-20 12:15:48 -0500156 if (mFrameCommitCallback) {
157 mContext->addFrameCommitListener(std::move(mFrameCommitCallback));
158 mFrameCommitCallback = nullptr;
John Reckcc2eee82018-05-17 10:44:00 -0700159 }
John Recka5dda642014-05-22 15:43:54 -0700160 }
John Reck668f0e32014-03-26 15:10:40 -0700161
162 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -0700163 CanvasContext* context = mContext;
chaviwb6803712021-12-13 15:46:29 -0600164 std::function<std::function<void(bool)>(int32_t, int64_t)> frameCallback =
165 std::move(mFrameCallback);
chaviw9c137532021-08-20 12:15:48 -0500166 std::function<void()> frameCompleteCallback = std::move(mFrameCompleteCallback);
Jorim Jaggi7a5addd2018-04-26 23:23:29 +0200167 mFrameCallback = nullptr;
chaviw9c137532021-08-20 12:15:48 -0500168 mFrameCompleteCallback = nullptr;
Bo Liu027b2182021-03-18 16:50:38 -0400169 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 Reck668f0e32014-03-26 15:10:40 -0700172
John Reck668f0e32014-03-26 15:10:40 -0700173 // From this point on anything in "this" is *UNSAFE TO ACCESS*
174 if (canUnblockUiThread) {
175 unblockUiThread();
176 }
177
Mihai Popa95688002018-02-23 16:10:11 +0000178 // Even if we aren't drawing this vsync pulse the next frame number will still be accurate
chaviw9c137532021-08-20 12:15:48 -0500179 if (CC_UNLIKELY(frameCallback)) {
chaviwb6803712021-12-13 15:46:29 -0600180 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 Popa95688002018-02-23 16:10:11 +0000187 }
188
Bo Liu4d0f1f12021-05-06 16:49:40 -0400189 nsecs_t dequeueBufferDuration = 0;
John Recka5dda642014-05-22 15:43:54 -0700190 if (CC_LIKELY(canDrawThisFrame)) {
Bo Liu4d0f1f12021-05-06 16:49:40 -0400191 dequeueBufferDuration = context->draw();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700192 } else {
John Reckcf1170f2021-07-14 15:52:19 -0400193 // 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 Craik06e2e9c2016-08-31 17:32:46 -0700199 // wait on fences so tasks don't overlap next frame
200 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700201 }
John Reck668f0e32014-03-26 15:10:40 -0700202
chaviw9c137532021-08-20 12:15:48 -0500203 if (CC_UNLIKELY(frameCompleteCallback)) {
204 std::invoke(frameCompleteCallback);
205 }
206
John Reck668f0e32014-03-26 15:10:40 -0700207 if (!canUnblockUiThread) {
208 unblockUiThread();
209 }
Bo Liu027b2182021-03-18 16:50:38 -0400210
Bo Liu6a3fc602021-07-17 16:42:13 -0400211 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 Liu027b2182021-03-18 16:50:38 -0400221 }
Bo Liu6a3fc602021-07-17 16:42:13 -0400222 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 Liu4d0f1f12021-05-06 16:49:40 -0400230 mLastDequeueBufferDuration = dequeueBufferDuration;
John Reck668f0e32014-03-26 15:10:40 -0700231}
232
John Recka5dda642014-05-22 15:43:54 -0700233bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700234 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700235 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700236 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
237 int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
Ady Abrahamdfb13982020-10-05 17:59:09 -0700238 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100239 int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)];
240 mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline,
241 frameInterval);
John Reck8afcc762016-04-13 10:24:06 -0700242 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400243 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700244
245 for (size_t i = 0; i < mLayers.size(); i++) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800246 mLayers[i]->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700247 }
248 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700249 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700250 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700251
John Reckaa95a882014-11-07 11:02:07 -0800252 // This is after the prepareTree so that any pending operations
253 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
John Reck8afcc762016-04-13 10:24:06 -0700254 if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
John Reck9a17da82016-04-18 11:17:52 -0700255 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 Reck8afcc762016-04-13 10:24:06 -0700261 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800262 }
263
John Reckf9be7792014-05-02 18:21:16 -0700264 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700265 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700266 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700267 }
John Reck52244ff2014-05-01 21:27:37 -0700268 }
John Reckcc2eee82018-05-17 10:44:00 -0700269 if (!info.out.canDrawThisFrame) {
270 mSyncResult |= SyncResult::FrameDropped;
271 }
John Reck860d1552014-04-11 19:15:05 -0700272 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700273 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700274}
275
276void DrawFrameTask::unblockUiThread() {
277 AutoMutex _lock(mLock);
278 mSignal.signal();
279}
280
Bo Liu6a3fc602021-07-17 16:42:13 -0400281DrawFrameTask::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
301DrawFrameTask::HintSessionWrapper::~HintSessionWrapper() {
302 if (mHintSession) {
303 gAPH_closeSessionFn(mHintSession);
304 }
305}
306
307void DrawFrameTask::HintSessionWrapper::updateTargetWorkDuration(long targetDurationNanos) {
308 if (mHintSession) {
309 gAPH_updateTargetWorkDurationFn(mHintSession, targetDurationNanos);
310 }
311}
312
313void DrawFrameTask::HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) {
314 if (mHintSession) {
315 gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos);
316 }
317}
318
John Reck668f0e32014-03-26 15:10:40 -0700319} /* namespace renderthread */
320} /* namespace uirenderer */
321} /* namespace android */