blob: e7081df2b558db4249930a37ed429beb1a6a7aed [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
153 if (mFrameCompleteCallback) {
154 mContext->addFrameCompleteListener(std::move(mFrameCompleteCallback));
155 mFrameCompleteCallback = nullptr;
156 }
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;
Mihai Popa95688002018-02-23 16:10:11 +0000161 std::function<void(int64_t)> callback = std::move(mFrameCallback);
Jorim Jaggi7a5addd2018-04-26 23:23:29 +0200162 mFrameCallback = nullptr;
Bo Liu027b2182021-03-18 16:50:38 -0400163 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
164 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
165 int64_t frameStartTime = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameStartTime)];
John Reck668f0e32014-03-26 15:10:40 -0700166
John Reck668f0e32014-03-26 15:10:40 -0700167 // From this point on anything in "this" is *UNSAFE TO ACCESS*
168 if (canUnblockUiThread) {
169 unblockUiThread();
170 }
171
Mihai Popa95688002018-02-23 16:10:11 +0000172 // Even if we aren't drawing this vsync pulse the next frame number will still be accurate
173 if (CC_UNLIKELY(callback)) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700174 context->enqueueFrameWork(
175 [callback, frameNr = context->getFrameNumber()]() { callback(frameNr); });
Mihai Popa95688002018-02-23 16:10:11 +0000176 }
177
Bo Liu4d0f1f12021-05-06 16:49:40 -0400178 nsecs_t dequeueBufferDuration = 0;
John Recka5dda642014-05-22 15:43:54 -0700179 if (CC_LIKELY(canDrawThisFrame)) {
Bo Liu4d0f1f12021-05-06 16:49:40 -0400180 dequeueBufferDuration = context->draw();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700181 } else {
John Reckcf1170f2021-07-14 15:52:19 -0400182 // Do a flush in case syncFrameState performed any texture uploads. Since we skipped
183 // the draw() call, those uploads (or deletes) will end up sitting in the queue.
184 // Do them now
185 if (GrDirectContext* grContext = mRenderThread->getGrContext()) {
186 grContext->flushAndSubmit();
187 }
Chris Craik06e2e9c2016-08-31 17:32:46 -0700188 // wait on fences so tasks don't overlap next frame
189 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700190 }
John Reck668f0e32014-03-26 15:10:40 -0700191
192 if (!canUnblockUiThread) {
193 unblockUiThread();
194 }
Bo Liu027b2182021-03-18 16:50:38 -0400195
Bo Liu6a3fc602021-07-17 16:42:13 -0400196 if (!mHintSessionWrapper) mHintSessionWrapper.emplace(mUiThreadId, mRenderThreadId);
197 constexpr int64_t kSanityCheckLowerBound = 100000; // 0.1ms
198 constexpr int64_t kSanityCheckUpperBound = 10000000000; // 10s
199 int64_t targetWorkDuration = frameDeadline - intendedVsync;
200 targetWorkDuration = targetWorkDuration * Properties::targetCpuTimePercentage / 100;
201 if (targetWorkDuration > kSanityCheckLowerBound &&
202 targetWorkDuration < kSanityCheckUpperBound &&
203 targetWorkDuration != mLastTargetWorkDuration) {
204 mLastTargetWorkDuration = targetWorkDuration;
205 mHintSessionWrapper->updateTargetWorkDuration(targetWorkDuration);
Bo Liu027b2182021-03-18 16:50:38 -0400206 }
Bo Liu6a3fc602021-07-17 16:42:13 -0400207 int64_t frameDuration = systemTime(SYSTEM_TIME_MONOTONIC) - frameStartTime;
208 int64_t actualDuration = frameDuration -
209 (std::min(syncDelayDuration, mLastDequeueBufferDuration)) -
210 dequeueBufferDuration;
211 if (actualDuration > kSanityCheckLowerBound && actualDuration < kSanityCheckUpperBound) {
212 mHintSessionWrapper->reportActualWorkDuration(actualDuration);
213 }
214
Bo Liu4d0f1f12021-05-06 16:49:40 -0400215 mLastDequeueBufferDuration = dequeueBufferDuration;
John Reck668f0e32014-03-26 15:10:40 -0700216}
217
John Recka5dda642014-05-22 15:43:54 -0700218bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700219 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700220 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700221 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
222 int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
Ady Abrahamdfb13982020-10-05 17:59:09 -0700223 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100224 int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)];
225 mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline,
226 frameInterval);
John Reck8afcc762016-04-13 10:24:06 -0700227 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400228 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700229
230 for (size_t i = 0; i < mLayers.size(); i++) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800231 mLayers[i]->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700232 }
233 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700234 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700235 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700236
John Reckaa95a882014-11-07 11:02:07 -0800237 // This is after the prepareTree so that any pending operations
238 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
John Reck8afcc762016-04-13 10:24:06 -0700239 if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
John Reck9a17da82016-04-18 11:17:52 -0700240 if (!mContext->hasSurface()) {
241 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
242 } else {
243 // If we have a surface but can't draw we must be stopped
244 mSyncResult |= SyncResult::ContextIsStopped;
245 }
John Reck8afcc762016-04-13 10:24:06 -0700246 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800247 }
248
John Reckf9be7792014-05-02 18:21:16 -0700249 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700250 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700251 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700252 }
John Reck52244ff2014-05-01 21:27:37 -0700253 }
John Reckcc2eee82018-05-17 10:44:00 -0700254 if (!info.out.canDrawThisFrame) {
255 mSyncResult |= SyncResult::FrameDropped;
256 }
John Reck860d1552014-04-11 19:15:05 -0700257 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700258 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700259}
260
261void DrawFrameTask::unblockUiThread() {
262 AutoMutex _lock(mLock);
263 mSignal.signal();
264}
265
Bo Liu6a3fc602021-07-17 16:42:13 -0400266DrawFrameTask::HintSessionWrapper::HintSessionWrapper(int32_t uiThreadId, int32_t renderThreadId) {
267 if (!Properties::useHintManager) return;
268 if (uiThreadId < 0 || renderThreadId < 0) return;
269
270 ensureAPerformanceHintBindingInitialized();
271
272 APerformanceHintManager* manager = gAPH_getManagerFn();
273 if (!manager) return;
274
275 std::vector<int32_t> tids = CommonPool::getThreadIds();
276 tids.push_back(uiThreadId);
277 tids.push_back(renderThreadId);
278
279 // DrawFrameTask code will always set a target duration before reporting actual durations.
280 // So this is just a placeholder value that's never used.
281 int64_t dummyTargetDurationNanos = 16666667;
282 mHintSession =
283 gAPH_createSessionFn(manager, tids.data(), tids.size(), dummyTargetDurationNanos);
284}
285
286DrawFrameTask::HintSessionWrapper::~HintSessionWrapper() {
287 if (mHintSession) {
288 gAPH_closeSessionFn(mHintSession);
289 }
290}
291
292void DrawFrameTask::HintSessionWrapper::updateTargetWorkDuration(long targetDurationNanos) {
293 if (mHintSession) {
294 gAPH_updateTargetWorkDurationFn(mHintSession, targetDurationNanos);
295 }
296}
297
298void DrawFrameTask::HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) {
299 if (mHintSession) {
300 gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos);
301 }
302}
303
John Reck668f0e32014-03-26 15:10:40 -0700304} /* namespace renderthread */
305} /* namespace uirenderer */
306} /* namespace android */