blob: 2357dfec72a6ed7f03f0315d6b054a0c1ca66668 [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);
John Recka5dda642014-05-22 15:43:54 -0700151 canUnblockUiThread = syncFrameState(info);
152 canDrawThisFrame = info.out.canDrawThisFrame;
John Reckcc2eee82018-05-17 10:44:00 -0700153
chaviw9c137532021-08-20 12:15:48 -0500154 if (mFrameCommitCallback) {
155 mContext->addFrameCommitListener(std::move(mFrameCommitCallback));
156 mFrameCommitCallback = nullptr;
John Reckcc2eee82018-05-17 10:44:00 -0700157 }
John Recka5dda642014-05-22 15:43:54 -0700158 }
John Reck668f0e32014-03-26 15:10:40 -0700159
160 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -0700161 CanvasContext* context = mContext;
chaviwb6803712021-12-13 15:46:29 -0600162 std::function<std::function<void(bool)>(int32_t, int64_t)> frameCallback =
163 std::move(mFrameCallback);
chaviw9c137532021-08-20 12:15:48 -0500164 std::function<void()> frameCompleteCallback = std::move(mFrameCompleteCallback);
Jorim Jaggi7a5addd2018-04-26 23:23:29 +0200165 mFrameCallback = nullptr;
chaviw9c137532021-08-20 12:15:48 -0500166 mFrameCompleteCallback = nullptr;
Bo Liu027b2182021-03-18 16:50:38 -0400167 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
168 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
169 int64_t frameStartTime = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameStartTime)];
John Reck668f0e32014-03-26 15:10:40 -0700170
John Reck668f0e32014-03-26 15:10:40 -0700171 // From this point on anything in "this" is *UNSAFE TO ACCESS*
172 if (canUnblockUiThread) {
173 unblockUiThread();
174 }
175
Mihai Popa95688002018-02-23 16:10:11 +0000176 // Even if we aren't drawing this vsync pulse the next frame number will still be accurate
chaviw9c137532021-08-20 12:15:48 -0500177 if (CC_UNLIKELY(frameCallback)) {
chaviwb6803712021-12-13 15:46:29 -0600178 context->enqueueFrameWork([frameCallback, context, syncResult = mSyncResult,
179 frameNr = context->getFrameNumber()]() {
180 auto frameCommitCallback = std::move(frameCallback(syncResult, frameNr));
181 if (frameCommitCallback) {
182 context->addFrameCommitListener(std::move(frameCommitCallback));
183 }
184 });
Mihai Popa95688002018-02-23 16:10:11 +0000185 }
186
Bo Liu4d0f1f12021-05-06 16:49:40 -0400187 nsecs_t dequeueBufferDuration = 0;
John Recka5dda642014-05-22 15:43:54 -0700188 if (CC_LIKELY(canDrawThisFrame)) {
Bo Liu4d0f1f12021-05-06 16:49:40 -0400189 dequeueBufferDuration = context->draw();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700190 } else {
John Reckcf1170f2021-07-14 15:52:19 -0400191 // Do a flush in case syncFrameState performed any texture uploads. Since we skipped
192 // the draw() call, those uploads (or deletes) will end up sitting in the queue.
193 // Do them now
194 if (GrDirectContext* grContext = mRenderThread->getGrContext()) {
195 grContext->flushAndSubmit();
196 }
Chris Craik06e2e9c2016-08-31 17:32:46 -0700197 // wait on fences so tasks don't overlap next frame
198 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700199 }
John Reck668f0e32014-03-26 15:10:40 -0700200
chaviw9c137532021-08-20 12:15:48 -0500201 if (CC_UNLIKELY(frameCompleteCallback)) {
202 std::invoke(frameCompleteCallback);
203 }
204
John Reck668f0e32014-03-26 15:10:40 -0700205 if (!canUnblockUiThread) {
206 unblockUiThread();
207 }
Bo Liu027b2182021-03-18 16:50:38 -0400208
Bo Liu6a3fc602021-07-17 16:42:13 -0400209 if (!mHintSessionWrapper) mHintSessionWrapper.emplace(mUiThreadId, mRenderThreadId);
210 constexpr int64_t kSanityCheckLowerBound = 100000; // 0.1ms
211 constexpr int64_t kSanityCheckUpperBound = 10000000000; // 10s
212 int64_t targetWorkDuration = frameDeadline - intendedVsync;
213 targetWorkDuration = targetWorkDuration * Properties::targetCpuTimePercentage / 100;
214 if (targetWorkDuration > kSanityCheckLowerBound &&
215 targetWorkDuration < kSanityCheckUpperBound &&
216 targetWorkDuration != mLastTargetWorkDuration) {
217 mLastTargetWorkDuration = targetWorkDuration;
218 mHintSessionWrapper->updateTargetWorkDuration(targetWorkDuration);
Bo Liu027b2182021-03-18 16:50:38 -0400219 }
Bo Liu6a3fc602021-07-17 16:42:13 -0400220 int64_t frameDuration = systemTime(SYSTEM_TIME_MONOTONIC) - frameStartTime;
221 int64_t actualDuration = frameDuration -
222 (std::min(syncDelayDuration, mLastDequeueBufferDuration)) -
223 dequeueBufferDuration;
224 if (actualDuration > kSanityCheckLowerBound && actualDuration < kSanityCheckUpperBound) {
225 mHintSessionWrapper->reportActualWorkDuration(actualDuration);
226 }
227
Bo Liu4d0f1f12021-05-06 16:49:40 -0400228 mLastDequeueBufferDuration = dequeueBufferDuration;
John Reck668f0e32014-03-26 15:10:40 -0700229}
230
John Recka5dda642014-05-22 15:43:54 -0700231bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700232 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700233 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700234 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
235 int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
Ady Abrahamdfb13982020-10-05 17:59:09 -0700236 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100237 int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)];
238 mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline,
239 frameInterval);
John Reck8afcc762016-04-13 10:24:06 -0700240 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400241 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700242
243 for (size_t i = 0; i < mLayers.size(); i++) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800244 mLayers[i]->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700245 }
246 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700247 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700248 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700249
John Reckaa95a882014-11-07 11:02:07 -0800250 // This is after the prepareTree so that any pending operations
251 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
John Reck8afcc762016-04-13 10:24:06 -0700252 if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
John Reck9a17da82016-04-18 11:17:52 -0700253 if (!mContext->hasSurface()) {
254 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
255 } else {
256 // If we have a surface but can't draw we must be stopped
257 mSyncResult |= SyncResult::ContextIsStopped;
258 }
John Reck8afcc762016-04-13 10:24:06 -0700259 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800260 }
261
John Reckf9be7792014-05-02 18:21:16 -0700262 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700263 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700264 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700265 }
John Reck52244ff2014-05-01 21:27:37 -0700266 }
John Reckcc2eee82018-05-17 10:44:00 -0700267 if (!info.out.canDrawThisFrame) {
268 mSyncResult |= SyncResult::FrameDropped;
269 }
John Reck860d1552014-04-11 19:15:05 -0700270 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700271 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700272}
273
274void DrawFrameTask::unblockUiThread() {
275 AutoMutex _lock(mLock);
276 mSignal.signal();
277}
278
Bo Liu6a3fc602021-07-17 16:42:13 -0400279DrawFrameTask::HintSessionWrapper::HintSessionWrapper(int32_t uiThreadId, int32_t renderThreadId) {
280 if (!Properties::useHintManager) return;
281 if (uiThreadId < 0 || renderThreadId < 0) return;
282
283 ensureAPerformanceHintBindingInitialized();
284
285 APerformanceHintManager* manager = gAPH_getManagerFn();
286 if (!manager) return;
287
288 std::vector<int32_t> tids = CommonPool::getThreadIds();
289 tids.push_back(uiThreadId);
290 tids.push_back(renderThreadId);
291
292 // DrawFrameTask code will always set a target duration before reporting actual durations.
293 // So this is just a placeholder value that's never used.
294 int64_t dummyTargetDurationNanos = 16666667;
295 mHintSession =
296 gAPH_createSessionFn(manager, tids.data(), tids.size(), dummyTargetDurationNanos);
297}
298
299DrawFrameTask::HintSessionWrapper::~HintSessionWrapper() {
300 if (mHintSession) {
301 gAPH_closeSessionFn(mHintSession);
302 }
303}
304
305void DrawFrameTask::HintSessionWrapper::updateTargetWorkDuration(long targetDurationNanos) {
306 if (mHintSession) {
307 gAPH_updateTargetWorkDurationFn(mHintSession, targetDurationNanos);
308 }
309}
310
311void DrawFrameTask::HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) {
312 if (mHintSession) {
313 gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos);
314 }
315}
316
John Reck668f0e32014-03-26 15:10:40 -0700317} /* namespace renderthread */
318} /* namespace uirenderer */
319} /* namespace android */