blob: db29e342855bb5b41d2caf9bd11da95f2eede291 [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
rnleece9762b2021-05-21 15:40:53 -070019#include <gui/TraceUtils.h>
John Reck668f0e32014-03-26 15:10:40 -070020#include <utils/Log.h>
Bo Liu4d0f1f12021-05-06 16:49:40 -040021#include <algorithm>
John Reck668f0e32014-03-26 15:10:40 -070022
John Reckd72e0a32014-05-29 18:56:11 -070023#include "../DeferredLayerUpdater.h"
John Reck668f0e32014-03-26 15:10:40 -070024#include "../DisplayList.h"
Bo Liu027b2182021-03-18 16:50:38 -040025#include "../Properties.h"
John Reck668f0e32014-03-26 15:10:40 -070026#include "../RenderNode.h"
27#include "CanvasContext.h"
28#include "RenderThread.h"
29
30namespace android {
31namespace uirenderer {
32namespace renderthread {
33
John Reck18f16e62014-05-02 16:46:41 -070034DrawFrameTask::DrawFrameTask()
Chris Craikd41c4d82015-01-05 15:51:13 -080035 : mRenderThread(nullptr)
36 , mContext(nullptr)
John Reckf138b172017-09-08 11:00:42 -070037 , mContentDrawBounds(0, 0, 0, 0)
John Reck1bcacfd2017-11-03 10:12:19 -070038 , mSyncResult(SyncResult::OK) {}
John Reck668f0e32014-03-26 15:10:40 -070039
John Reck1bcacfd2017-11-03 10:12:19 -070040DrawFrameTask::~DrawFrameTask() {}
John Reck668f0e32014-03-26 15:10:40 -070041
Skuhneea7a7fb2015-08-28 07:10:31 -070042void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context,
John Reck1bcacfd2017-11-03 10:12:19 -070043 RenderNode* targetNode) {
John Reck18f16e62014-05-02 16:46:41 -070044 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070045 mContext = context;
Skuhneea7a7fb2015-08-28 07:10:31 -070046 mTargetNode = targetNode;
John Reck668f0e32014-03-26 15:10:40 -070047}
48
Bo Liu027b2182021-03-18 16:50:38 -040049void DrawFrameTask::setHintSessionCallbacks(std::function<void(int64_t)> updateTargetWorkDuration,
50 std::function<void(int64_t)> reportActualWorkDuration) {
51 mUpdateTargetWorkDuration = std::move(updateTargetWorkDuration);
52 mReportActualWorkDuration = std::move(reportActualWorkDuration);
53}
54
John Reckd72e0a32014-05-29 18:56:11 -070055void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -070056 LOG_ALWAYS_FATAL_IF(!mContext,
57 "Lifecycle violation, there's no context to pushLayerUpdate with!");
John Reck087bc0c2014-04-04 16:20:08 -070058
John Reckd72e0a32014-05-29 18:56:11 -070059 for (size_t i = 0; i < mLayers.size(); i++) {
60 if (mLayers[i].get() == layer) {
61 return;
62 }
63 }
64 mLayers.push_back(layer);
John Reck668f0e32014-03-26 15:10:40 -070065}
66
John Reckd72e0a32014-05-29 18:56:11 -070067void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -070068 for (size_t i = 0; i < mLayers.size(); i++) {
John Reckd72e0a32014-05-29 18:56:11 -070069 if (mLayers[i].get() == layer) {
70 mLayers.erase(mLayers.begin() + i);
71 return;
John Reck668f0e32014-03-26 15:10:40 -070072 }
73 }
74}
75
John Reck2de950d2017-01-25 10:58:30 -080076int DrawFrameTask::drawFrame() {
John Reck668f0e32014-03-26 15:10:40 -070077 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
78
John Reck9a17da82016-04-18 11:17:52 -070079 mSyncResult = SyncResult::OK;
Jerome Gaillarde218c692019-06-14 12:58:57 +010080 mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck18f16e62014-05-02 16:46:41 -070081 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070082
John Reckf9be7792014-05-02 18:21:16 -070083 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070084}
85
John Reck18f16e62014-05-02 16:46:41 -070086void DrawFrameTask::postAndWait() {
John Reck087bc0c2014-04-04 16:20:08 -070087 AutoMutex _lock(mLock);
John Reckf8441e62017-10-23 13:10:41 -070088 mRenderThread->queue().post([this]() { run(); });
Chris Craik738ec3a2014-07-25 18:25:02 +000089 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -070090}
91
John Reck668f0e32014-03-26 15:10:40 -070092void DrawFrameTask::run() {
Ady Abraham150816c2021-03-01 10:46:40 -080093 const int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
94 ATRACE_FORMAT("DrawFrames %" PRId64, vsyncId);
Bo Liu4d0f1f12021-05-06 16:49:40 -040095 nsecs_t syncDelayDuration = systemTime(SYSTEM_TIME_MONOTONIC) - mSyncQueued;
John Reck668f0e32014-03-26 15:10:40 -070096
John Recka5dda642014-05-22 15:43:54 -070097 bool canUnblockUiThread;
98 bool canDrawThisFrame;
99 {
Chris Craike2e53a72015-10-28 15:55:40 -0700100 TreeInfo info(TreeInfo::MODE_FULL, *mContext);
John Recka5dda642014-05-22 15:43:54 -0700101 canUnblockUiThread = syncFrameState(info);
102 canDrawThisFrame = info.out.canDrawThisFrame;
John Reckcc2eee82018-05-17 10:44:00 -0700103
104 if (mFrameCompleteCallback) {
105 mContext->addFrameCompleteListener(std::move(mFrameCompleteCallback));
106 mFrameCompleteCallback = nullptr;
107 }
John Recka5dda642014-05-22 15:43:54 -0700108 }
John Reck668f0e32014-03-26 15:10:40 -0700109
110 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -0700111 CanvasContext* context = mContext;
Mihai Popa95688002018-02-23 16:10:11 +0000112 std::function<void(int64_t)> callback = std::move(mFrameCallback);
Jorim Jaggi7a5addd2018-04-26 23:23:29 +0200113 mFrameCallback = nullptr;
Bo Liu027b2182021-03-18 16:50:38 -0400114 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
115 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
116 int64_t frameStartTime = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameStartTime)];
John Reck668f0e32014-03-26 15:10:40 -0700117
John Reck668f0e32014-03-26 15:10:40 -0700118 // From this point on anything in "this" is *UNSAFE TO ACCESS*
119 if (canUnblockUiThread) {
120 unblockUiThread();
121 }
122
Mihai Popa95688002018-02-23 16:10:11 +0000123 // Even if we aren't drawing this vsync pulse the next frame number will still be accurate
124 if (CC_UNLIKELY(callback)) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700125 context->enqueueFrameWork(
126 [callback, frameNr = context->getFrameNumber()]() { callback(frameNr); });
Mihai Popa95688002018-02-23 16:10:11 +0000127 }
128
Bo Liu4d0f1f12021-05-06 16:49:40 -0400129 nsecs_t dequeueBufferDuration = 0;
John Recka5dda642014-05-22 15:43:54 -0700130 if (CC_LIKELY(canDrawThisFrame)) {
Bo Liu4d0f1f12021-05-06 16:49:40 -0400131 dequeueBufferDuration = context->draw();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700132 } else {
John Reckcf1170f2021-07-14 15:52:19 -0400133 // Do a flush in case syncFrameState performed any texture uploads. Since we skipped
134 // the draw() call, those uploads (or deletes) will end up sitting in the queue.
135 // Do them now
136 if (GrDirectContext* grContext = mRenderThread->getGrContext()) {
137 grContext->flushAndSubmit();
138 }
Chris Craik06e2e9c2016-08-31 17:32:46 -0700139 // wait on fences so tasks don't overlap next frame
140 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700141 }
John Reck668f0e32014-03-26 15:10:40 -0700142
143 if (!canUnblockUiThread) {
144 unblockUiThread();
145 }
Bo Liu027b2182021-03-18 16:50:38 -0400146
147 // These member callbacks are effectively const as they are set once during init, so it's safe
148 // to use these directly instead of making local copies.
149 if (mUpdateTargetWorkDuration && mReportActualWorkDuration) {
150 constexpr int64_t kSanityCheckLowerBound = 100000; // 0.1ms
151 constexpr int64_t kSanityCheckUpperBound = 10000000000; // 10s
152 int64_t targetWorkDuration = frameDeadline - intendedVsync;
153 targetWorkDuration = targetWorkDuration * Properties::targetCpuTimePercentage / 100;
154 if (targetWorkDuration > kSanityCheckLowerBound &&
155 targetWorkDuration < kSanityCheckUpperBound &&
156 targetWorkDuration != mLastTargetWorkDuration) {
157 mLastTargetWorkDuration = targetWorkDuration;
158 mUpdateTargetWorkDuration(targetWorkDuration);
159 }
160 int64_t frameDuration = systemTime(SYSTEM_TIME_MONOTONIC) - frameStartTime;
Bo Liu4d0f1f12021-05-06 16:49:40 -0400161 int64_t actualDuration = frameDuration -
162 (std::min(syncDelayDuration, mLastDequeueBufferDuration)) -
163 dequeueBufferDuration;
164 if (actualDuration > kSanityCheckLowerBound && actualDuration < kSanityCheckUpperBound) {
165 mReportActualWorkDuration(actualDuration);
Bo Liu027b2182021-03-18 16:50:38 -0400166 }
167 }
Bo Liu4d0f1f12021-05-06 16:49:40 -0400168 mLastDequeueBufferDuration = dequeueBufferDuration;
John Reck668f0e32014-03-26 15:10:40 -0700169}
170
John Recka5dda642014-05-22 15:43:54 -0700171bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700172 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700173 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700174 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
175 int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
Ady Abrahamdfb13982020-10-05 17:59:09 -0700176 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100177 int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)];
178 mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline,
179 frameInterval);
John Reck8afcc762016-04-13 10:24:06 -0700180 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400181 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700182
183 for (size_t i = 0; i < mLayers.size(); i++) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800184 mLayers[i]->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700185 }
186 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700187 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700188 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700189
John Reckaa95a882014-11-07 11:02:07 -0800190 // This is after the prepareTree so that any pending operations
191 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
John Reck8afcc762016-04-13 10:24:06 -0700192 if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
John Reck9a17da82016-04-18 11:17:52 -0700193 if (!mContext->hasSurface()) {
194 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
195 } else {
196 // If we have a surface but can't draw we must be stopped
197 mSyncResult |= SyncResult::ContextIsStopped;
198 }
John Reck8afcc762016-04-13 10:24:06 -0700199 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800200 }
201
John Reckf9be7792014-05-02 18:21:16 -0700202 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700203 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700204 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700205 }
John Reck52244ff2014-05-01 21:27:37 -0700206 }
John Reckcc2eee82018-05-17 10:44:00 -0700207 if (!info.out.canDrawThisFrame) {
208 mSyncResult |= SyncResult::FrameDropped;
209 }
John Reck860d1552014-04-11 19:15:05 -0700210 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700211 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700212}
213
214void DrawFrameTask::unblockUiThread() {
215 AutoMutex _lock(mLock);
216 mSignal.signal();
217}
218
John Reck668f0e32014-03-26 15:10:40 -0700219} /* namespace renderthread */
220} /* namespace uirenderer */
221} /* namespace android */