blob: 1cc82fd0ff64bf23cab5d9e4fee12686f1e96c3d [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>
Matt Buckley864ab952022-10-18 23:03:59 +000021
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"
30
31namespace android {
32namespace uirenderer {
33namespace renderthread {
34
John Reck18f16e62014-05-02 16:46:41 -070035DrawFrameTask::DrawFrameTask()
Chris Craikd41c4d82015-01-05 15:51:13 -080036 : mRenderThread(nullptr)
37 , mContext(nullptr)
John Reckf138b172017-09-08 11:00:42 -070038 , mContentDrawBounds(0, 0, 0, 0)
John Reck1bcacfd2017-11-03 10:12:19 -070039 , mSyncResult(SyncResult::OK) {}
John Reck668f0e32014-03-26 15:10:40 -070040
John Reck1bcacfd2017-11-03 10:12:19 -070041DrawFrameTask::~DrawFrameTask() {}
John Reck668f0e32014-03-26 15:10:40 -070042
Matt Buckleye9023cf2022-11-23 22:39:25 +000043void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context,
44 RenderNode* targetNode) {
John Reck18f16e62014-05-02 16:46:41 -070045 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070046 mContext = context;
Skuhneea7a7fb2015-08-28 07:10:31 -070047 mTargetNode = targetNode;
Bo Liu027b2182021-03-18 16:50:38 -040048}
49
John Reckd72e0a32014-05-29 18:56:11 -070050void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -070051 LOG_ALWAYS_FATAL_IF(!mContext,
52 "Lifecycle violation, there's no context to pushLayerUpdate with!");
John Reck087bc0c2014-04-04 16:20:08 -070053
John Reckd72e0a32014-05-29 18:56:11 -070054 for (size_t i = 0; i < mLayers.size(); i++) {
55 if (mLayers[i].get() == layer) {
56 return;
57 }
58 }
59 mLayers.push_back(layer);
John Reck668f0e32014-03-26 15:10:40 -070060}
61
John Reckd72e0a32014-05-29 18:56:11 -070062void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -070063 for (size_t i = 0; i < mLayers.size(); i++) {
John Reckd72e0a32014-05-29 18:56:11 -070064 if (mLayers[i].get() == layer) {
65 mLayers.erase(mLayers.begin() + i);
66 return;
John Reck668f0e32014-03-26 15:10:40 -070067 }
68 }
69}
70
John Reck2de950d2017-01-25 10:58:30 -080071int DrawFrameTask::drawFrame() {
John Reck668f0e32014-03-26 15:10:40 -070072 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
73
John Reck9a17da82016-04-18 11:17:52 -070074 mSyncResult = SyncResult::OK;
Jerome Gaillarde218c692019-06-14 12:58:57 +010075 mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck18f16e62014-05-02 16:46:41 -070076 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070077
John Reckf9be7792014-05-02 18:21:16 -070078 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070079}
80
John Reck18f16e62014-05-02 16:46:41 -070081void DrawFrameTask::postAndWait() {
Jimmy Shiu06d91392022-03-15 19:35:45 +080082 ATRACE_CALL();
John Reck087bc0c2014-04-04 16:20:08 -070083 AutoMutex _lock(mLock);
John Reckf8441e62017-10-23 13:10:41 -070084 mRenderThread->queue().post([this]() { run(); });
Chris Craik738ec3a2014-07-25 18:25:02 +000085 mSignal.wait(mLock);
John Reck087bc0c2014-04-04 16:20:08 -070086}
87
John Reck668f0e32014-03-26 15:10:40 -070088void DrawFrameTask::run() {
Ady Abraham150816c2021-03-01 10:46:40 -080089 const int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
90 ATRACE_FORMAT("DrawFrames %" PRId64, vsyncId);
Matt Buckleye9023cf2022-11-23 22:39:25 +000091
92 mContext->setSyncDelayDuration(systemTime(SYSTEM_TIME_MONOTONIC) - mSyncQueued);
John Reck668f0e32014-03-26 15:10:40 -070093
John Recka5dda642014-05-22 15:43:54 -070094 bool canUnblockUiThread;
95 bool canDrawThisFrame;
96 {
Chris Craike2e53a72015-10-28 15:55:40 -070097 TreeInfo info(TreeInfo::MODE_FULL, *mContext);
chaviwadba0b12022-03-18 17:42:15 -050098 info.forceDrawFrame = mForceDrawFrame;
99 mForceDrawFrame = false;
John Recka5dda642014-05-22 15:43:54 -0700100 canUnblockUiThread = syncFrameState(info);
101 canDrawThisFrame = info.out.canDrawThisFrame;
John Reckcc2eee82018-05-17 10:44:00 -0700102
chaviw9c137532021-08-20 12:15:48 -0500103 if (mFrameCommitCallback) {
104 mContext->addFrameCommitListener(std::move(mFrameCommitCallback));
105 mFrameCommitCallback = nullptr;
John Reckcc2eee82018-05-17 10:44:00 -0700106 }
John Recka5dda642014-05-22 15:43:54 -0700107 }
John Reck668f0e32014-03-26 15:10:40 -0700108
109 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -0700110 CanvasContext* context = mContext;
chaviwb6803712021-12-13 15:46:29 -0600111 std::function<std::function<void(bool)>(int32_t, int64_t)> frameCallback =
112 std::move(mFrameCallback);
chaviw9c137532021-08-20 12:15:48 -0500113 std::function<void()> frameCompleteCallback = std::move(mFrameCompleteCallback);
Jorim Jaggi7a5addd2018-04-26 23:23:29 +0200114 mFrameCallback = nullptr;
chaviw9c137532021-08-20 12:15:48 -0500115 mFrameCompleteCallback = nullptr;
John Reck668f0e32014-03-26 15:10:40 -0700116
John Reck668f0e32014-03-26 15:10:40 -0700117 // From this point on anything in "this" is *UNSAFE TO ACCESS*
118 if (canUnblockUiThread) {
119 unblockUiThread();
120 }
121
Mihai Popa95688002018-02-23 16:10:11 +0000122 // Even if we aren't drawing this vsync pulse the next frame number will still be accurate
chaviw9c137532021-08-20 12:15:48 -0500123 if (CC_UNLIKELY(frameCallback)) {
chaviwb6803712021-12-13 15:46:29 -0600124 context->enqueueFrameWork([frameCallback, context, syncResult = mSyncResult,
125 frameNr = context->getFrameNumber()]() {
Matt Buckleye9023cf2022-11-23 22:39:25 +0000126 auto frameCommitCallback = frameCallback(syncResult, frameNr);
chaviwb6803712021-12-13 15:46:29 -0600127 if (frameCommitCallback) {
128 context->addFrameCommitListener(std::move(frameCommitCallback));
129 }
130 });
Mihai Popa95688002018-02-23 16:10:11 +0000131 }
132
John Recka5dda642014-05-22 15:43:54 -0700133 if (CC_LIKELY(canDrawThisFrame)) {
Matt Buckleye9023cf2022-11-23 22:39:25 +0000134 context->draw();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700135 } else {
John Reckcf1170f2021-07-14 15:52:19 -0400136 // Do a flush in case syncFrameState performed any texture uploads. Since we skipped
137 // the draw() call, those uploads (or deletes) will end up sitting in the queue.
138 // Do them now
139 if (GrDirectContext* grContext = mRenderThread->getGrContext()) {
140 grContext->flushAndSubmit();
141 }
Chris Craik06e2e9c2016-08-31 17:32:46 -0700142 // wait on fences so tasks don't overlap next frame
143 context->waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700144 }
John Reck668f0e32014-03-26 15:10:40 -0700145
chaviw9c137532021-08-20 12:15:48 -0500146 if (CC_UNLIKELY(frameCompleteCallback)) {
147 std::invoke(frameCompleteCallback);
148 }
149
John Reck668f0e32014-03-26 15:10:40 -0700150 if (!canUnblockUiThread) {
151 unblockUiThread();
152 }
Matt Buckleyd98e8052022-10-21 22:13:23 +0000153}
154
John Recka5dda642014-05-22 15:43:54 -0700155bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700156 ATRACE_CALL();
Chris Craik1b54fb22015-06-02 17:40:58 -0700157 int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700158 int64_t intendedVsync = mFrameInfo[static_cast<int>(FrameInfoIndex::IntendedVsync)];
159 int64_t vsyncId = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameTimelineVsyncId)];
Ady Abrahamdfb13982020-10-05 17:59:09 -0700160 int64_t frameDeadline = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameDeadline)];
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100161 int64_t frameInterval = mFrameInfo[static_cast<int>(FrameInfoIndex::FrameInterval)];
162 mRenderThread->timeLord().vsyncReceived(vsync, intendedVsync, vsyncId, frameDeadline,
163 frameInterval);
John Reck8afcc762016-04-13 10:24:06 -0700164 bool canDraw = mContext->makeCurrent();
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400165 mContext->unpinImages();
John Reckd72e0a32014-05-29 18:56:11 -0700166
167 for (size_t i = 0; i < mLayers.size(); i++) {
Jessie Hao10f88672022-05-10 14:47:05 +0800168 if (mLayers[i]) {
169 mLayers[i]->apply();
170 }
John Reckd72e0a32014-05-29 18:56:11 -0700171 }
172 mLayers.clear();
John Reckf138b172017-09-08 11:00:42 -0700173 mContext->setContentDrawBounds(mContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700174 mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
John Reckd72e0a32014-05-29 18:56:11 -0700175
John Reckaa95a882014-11-07 11:02:07 -0800176 // This is after the prepareTree so that any pending operations
177 // (RenderNode tree state, prefetched layers, etc...) will be flushed.
John Reck8afcc762016-04-13 10:24:06 -0700178 if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
John Reck9a17da82016-04-18 11:17:52 -0700179 if (!mContext->hasSurface()) {
180 mSyncResult |= SyncResult::LostSurfaceRewardIfFound;
181 } else {
182 // If we have a surface but can't draw we must be stopped
183 mSyncResult |= SyncResult::ContextIsStopped;
184 }
John Reck8afcc762016-04-13 10:24:06 -0700185 info.out.canDrawThisFrame = false;
John Reckaa95a882014-11-07 11:02:07 -0800186 }
187
John Reckf9be7792014-05-02 18:21:16 -0700188 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700189 if (info.out.requiresUiRedraw) {
John Reck9a17da82016-04-18 11:17:52 -0700190 mSyncResult |= SyncResult::UIRedrawRequired;
John Reckf9be7792014-05-02 18:21:16 -0700191 }
John Reck52244ff2014-05-01 21:27:37 -0700192 }
John Reckcc2eee82018-05-17 10:44:00 -0700193 if (!info.out.canDrawThisFrame) {
194 mSyncResult |= SyncResult::FrameDropped;
195 }
John Reck860d1552014-04-11 19:15:05 -0700196 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700197 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700198}
199
200void DrawFrameTask::unblockUiThread() {
201 AutoMutex _lock(mLock);
202 mSignal.signal();
203}
204
John Reck668f0e32014-03-26 15:10:40 -0700205} /* namespace renderthread */
206} /* namespace uirenderer */
207} /* namespace android */