blob: 8c98c723dbb35e2063f49d1e48d122c5133928c1 [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() {
John Reck087bc0c2014-04-04 16:20:08 -0700136 AutoMutex _lock(mLock);
John Reckf8441e62017-10-23 13:10:41 -0700137 mRenderThread->queue().post([this]() { run(); });
Chris Craik738ec3a2014-07-25 18:25:02 +0000138 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -0700139}
140
John Reck668f0e32014-03-26 15:10:40 -0700141void DrawFrameTask::run() {
Ady Abraham150816c2021-03-01 10:46:40 -0800142 const int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
143 ATRACE_FORMAT("DrawFrames %" PRId64, vsyncId);
Bo Liu4d0f1f12021-05-06 16:49:40 -0400144 nsecs_t syncDelayDuration = systemTime(SYSTEM_TIME_MONOTONIC) - mSyncQueued;
John Reck668f0e32014-03-26 15:10:40 -0700145
John Recka5dda642014-05-22 15:43:54 -0700146 bool canUnblockUiThread;
147 bool canDrawThisFrame;
148 {
Chris Craike2e53a72015-10-28 15:55:40 -0700149 TreeInfo info(TreeInfo::MODE_FULL, *mContext);
John Recka5dda642014-05-22 15:43:54 -0700150 canUnblockUiThread = syncFrameState(info);
151 canDrawThisFrame = info.out.canDrawThisFrame;
John Reckcc2eee82018-05-17 10:44:00 -0700152
chaviw9c137532021-08-20 12:15:48 -0500153 if (mFrameCommitCallback) {
154 mContext->addFrameCommitListener(std::move(mFrameCommitCallback));
155 mFrameCommitCallback = nullptr;
John Reckcc2eee82018-05-17 10:44:00 -0700156 }
John Recka5dda642014-05-22 15:43:54 -0700157 }
John Reck668f0e32014-03-26 15:10:40 -0700158
159 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -0700160 CanvasContext* context = mContext;
chaviwb6803712021-12-13 15:46:29 -0600161 std::function<std::function<void(bool)>(int32_t, int64_t)> frameCallback =
162 std::move(mFrameCallback);
chaviw9c137532021-08-20 12:15:48 -0500163 std::function<void()> frameCompleteCallback = std::move(mFrameCompleteCallback);
Jorim Jaggi7a5addd2018-04-26 23:23:29 +0200164 mFrameCallback = nullptr;
chaviw9c137532021-08-20 12:15:48 -0500165 mFrameCompleteCallback = nullptr;
Bo Liu027b2182021-03-18 16:50:38 -0400166 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
167 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
168 int64_t frameStartTime = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameStartTime)];
John Reck668f0e32014-03-26 15:10:40 -0700169
John Reck668f0e32014-03-26 15:10:40 -0700170 // From this point on anything in "this" is *UNSAFE TO ACCESS*
171 if (canUnblockUiThread) {
172 unblockUiThread();
173 }
174
Mihai Popa95688002018-02-23 16:10:11 +0000175 // Even if we aren't drawing this vsync pulse the next frame number will still be accurate
chaviw9c137532021-08-20 12:15:48 -0500176 if (CC_UNLIKELY(frameCallback)) {
chaviwb6803712021-12-13 15:46:29 -0600177 context->enqueueFrameWork([frameCallback, context, syncResult = mSyncResult,
178 frameNr = context->getFrameNumber()]() {
179 auto frameCommitCallback = std::move(frameCallback(syncResult, frameNr));
180 if (frameCommitCallback) {
181 context->addFrameCommitListener(std::move(frameCommitCallback));
182 }
183 });
Mihai Popa95688002018-02-23 16:10:11 +0000184 }
185
Bo Liu4d0f1f12021-05-06 16:49:40 -0400186 nsecs_t dequeueBufferDuration = 0;
John Recka5dda642014-05-22 15:43:54 -0700187 if (CC_LIKELY(canDrawThisFrame)) {
Bo Liu4d0f1f12021-05-06 16:49:40 -0400188 dequeueBufferDuration = context->draw();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700189 } else {
John Reckcf1170f2021-07-14 15:52:19 -0400190 // Do a flush in case syncFrameState performed any texture uploads. Since we skipped
191 // the draw() call, those uploads (or deletes) will end up sitting in the queue.
192 // Do them now
193 if (GrDirectContext* grContext = mRenderThread->getGrContext()) {
194 grContext->flushAndSubmit();
195 }
Chris Craik06e2e9c2016-08-31 17:32:46 -0700196 // wait on fences so tasks don't overlap next frame
197 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700198 }
John Reck668f0e32014-03-26 15:10:40 -0700199
chaviw9c137532021-08-20 12:15:48 -0500200 if (CC_UNLIKELY(frameCompleteCallback)) {
201 std::invoke(frameCompleteCallback);
202 }
203
John Reck668f0e32014-03-26 15:10:40 -0700204 if (!canUnblockUiThread) {
205 unblockUiThread();
206 }
Bo Liu027b2182021-03-18 16:50:38 -0400207
Bo Liu6a3fc602021-07-17 16:42:13 -0400208 if (!mHintSessionWrapper) mHintSessionWrapper.emplace(mUiThreadId, mRenderThreadId);
209 constexpr int64_t kSanityCheckLowerBound = 100000; // 0.1ms
210 constexpr int64_t kSanityCheckUpperBound = 10000000000; // 10s
211 int64_t targetWorkDuration = frameDeadline - intendedVsync;
212 targetWorkDuration = targetWorkDuration * Properties::targetCpuTimePercentage / 100;
213 if (targetWorkDuration > kSanityCheckLowerBound &&
214 targetWorkDuration < kSanityCheckUpperBound &&
215 targetWorkDuration != mLastTargetWorkDuration) {
216 mLastTargetWorkDuration = targetWorkDuration;
217 mHintSessionWrapper->updateTargetWorkDuration(targetWorkDuration);
Bo Liu027b2182021-03-18 16:50:38 -0400218 }
Bo Liu6a3fc602021-07-17 16:42:13 -0400219 int64_t frameDuration = systemTime(SYSTEM_TIME_MONOTONIC) - frameStartTime;
220 int64_t actualDuration = frameDuration -
221 (std::min(syncDelayDuration, mLastDequeueBufferDuration)) -
222 dequeueBufferDuration;
223 if (actualDuration > kSanityCheckLowerBound && actualDuration < kSanityCheckUpperBound) {
224 mHintSessionWrapper->reportActualWorkDuration(actualDuration);
225 }
226
Bo Liu4d0f1f12021-05-06 16:49:40 -0400227 mLastDequeueBufferDuration = dequeueBufferDuration;
John Reck668f0e32014-03-26 15:10:40 -0700228}
229
John Recka5dda642014-05-22 15:43:54 -0700230bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700231 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700232 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700233 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
234 int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
Ady Abrahamdfb13982020-10-05 17:59:09 -0700235 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100236 int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)];
237 mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline,
238 frameInterval);
John Reck8afcc762016-04-13 10:24:06 -0700239 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400240 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700241
242 for (size_t i = 0; i < mLayers.size(); i++) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800243 mLayers[i]->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700244 }
245 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700246 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700247 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700248
John Reckaa95a882014-11-07 11:02:07 -0800249 // This is after the prepareTree so that any pending operations
250 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
John Reck8afcc762016-04-13 10:24:06 -0700251 if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
John Reck9a17da82016-04-18 11:17:52 -0700252 if (!mContext->hasSurface()) {
253 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
254 } else {
255 // If we have a surface but can't draw we must be stopped
256 mSyncResult |= SyncResult::ContextIsStopped;
257 }
John Reck8afcc762016-04-13 10:24:06 -0700258 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800259 }
260
John Reckf9be7792014-05-02 18:21:16 -0700261 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700262 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700263 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700264 }
John Reck52244ff2014-05-01 21:27:37 -0700265 }
John Reckcc2eee82018-05-17 10:44:00 -0700266 if (!info.out.canDrawThisFrame) {
267 mSyncResult |= SyncResult::FrameDropped;
268 }
John Reck860d1552014-04-11 19:15:05 -0700269 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700270 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700271}
272
273void DrawFrameTask::unblockUiThread() {
274 AutoMutex _lock(mLock);
275 mSignal.signal();
276}
277
Bo Liu6a3fc602021-07-17 16:42:13 -0400278DrawFrameTask::HintSessionWrapper::HintSessionWrapper(int32_t uiThreadId, int32_t renderThreadId) {
279 if (!Properties::useHintManager) return;
280 if (uiThreadId < 0 || renderThreadId < 0) return;
281
282 ensureAPerformanceHintBindingInitialized();
283
284 APerformanceHintManager* manager = gAPH_getManagerFn();
285 if (!manager) return;
286
287 std::vector<int32_t> tids = CommonPool::getThreadIds();
288 tids.push_back(uiThreadId);
289 tids.push_back(renderThreadId);
290
291 // DrawFrameTask code will always set a target duration before reporting actual durations.
292 // So this is just a placeholder value that's never used.
293 int64_t dummyTargetDurationNanos = 16666667;
294 mHintSession =
295 gAPH_createSessionFn(manager, tids.data(), tids.size(), dummyTargetDurationNanos);
296}
297
298DrawFrameTask::HintSessionWrapper::~HintSessionWrapper() {
299 if (mHintSession) {
300 gAPH_closeSessionFn(mHintSession);
301 }
302}
303
304void DrawFrameTask::HintSessionWrapper::updateTargetWorkDuration(long targetDurationNanos) {
305 if (mHintSession) {
306 gAPH_updateTargetWorkDurationFn(mHintSession, targetDurationNanos);
307 }
308}
309
310void DrawFrameTask::HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) {
311 if (mHintSession) {
312 gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos);
313 }
314}
315
John Reck668f0e32014-03-26 15:10:40 -0700316} /* namespace renderthread */
317} /* namespace uirenderer */
318} /* namespace android */