blob: a73d7e4632141690b836bb52a64950d720fd58db [file] [log] [blame]
John Reck23b797a2014-01-03 18:08:34 -08001/*
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 Reck23b797a2014-01-03 18:08:34 -080017#include "CanvasContext.h"
Alec Mouri8d451742019-08-01 19:19:16 -070018
Alec Mouri8d451742019-08-01 19:19:16 -070019#include <apex/window.h>
20#include <fcntl.h>
John Reckc8024fc2021-10-18 19:25:46 -040021#include <gui/TraceUtils.h>
Alec Mouri8d451742019-08-01 19:19:16 -070022#include <strings.h>
23#include <sys/stat.h>
John Reckc8024fc2021-10-18 19:25:46 -040024#include <ui/Fence.h>
Alec Mouri8d451742019-08-01 19:19:16 -070025
26#include <algorithm>
27#include <cstdint>
28#include <cstdlib>
29#include <functional>
John Reck23b797a2014-01-03 18:08:34 -080030
John Reck0fa0cbc2019-04-05 16:57:46 -070031#include "../Properties.h"
John Reckd04794a2015-05-08 10:04:36 -070032#include "AnimationContext.h"
Greg Danielcd558522016-11-17 13:31:40 -050033#include "Frame.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070034#include "LayerUpdateQueue.h"
John Reckd04794a2015-05-08 10:04:36 -070035#include "Properties.h"
John Reck4f02bf42014-01-03 18:09:17 -080036#include "RenderThread.h"
sergeyvdccca442016-03-21 15:38:21 -070037#include "hwui/Canvas.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040038#include "pipeline/skia/SkiaOpenGLPipeline.h"
39#include "pipeline/skia/SkiaPipeline.h"
40#include "pipeline/skia/SkiaVulkanPipeline.h"
John Reck322b8ab2019-03-14 13:15:28 -070041#include "thread/CommonPool.h"
John Reck9372ac32016-01-19 11:46:52 -080042#include "utils/GLUtils.h"
John Recke486d932015-10-28 09:21:19 -070043#include "utils/TimeUtils.h"
John Recke248bd12015-08-05 13:53:53 -070044
John Reck149173d2015-08-10 09:52:29 -070045#define LOG_FRAMETIME_MMA 0
46
47#if LOG_FRAMETIME_MMA
48static float sBenchMma = 0;
49static int sFrameCount = 0;
50static const float NANOS_PER_MILLIS_F = 1000000.0f;
51#endif
52
John Reck23b797a2014-01-03 18:08:34 -080053namespace android {
54namespace uirenderer {
55namespace renderthread {
56
Huihong Luo054b8d32021-02-24 18:48:12 -080057namespace {
58class ScopedActiveContext {
59public:
60 ScopedActiveContext(CanvasContext* context) { sActiveContext = context; }
61
62 ~ScopedActiveContext() { sActiveContext = nullptr; }
63
64 static CanvasContext* getActiveContext() { return sActiveContext; }
65
66private:
67 static CanvasContext* sActiveContext;
68};
69
70CanvasContext* ScopedActiveContext::sActiveContext = nullptr;
71} /* namespace */
72
John Reck1bcacfd2017-11-03 10:12:19 -070073CanvasContext* CanvasContext::create(RenderThread& thread, bool translucent,
Matt Buckleye9023cf2022-11-23 22:39:25 +000074 RenderNode* rootRenderNode, IContextFactory* contextFactory,
75 int32_t uiThreadId, int32_t renderThreadId) {
Stan Iliev03de0742016-07-07 12:35:54 -040076 auto renderType = Properties::getRenderPipelineType();
Stan Iliev768e3932016-07-08 21:34:52 -040077
Stan Iliev03de0742016-07-07 12:35:54 -040078 switch (renderType) {
Stan Iliev03de0742016-07-07 12:35:54 -040079 case RenderPipelineType::SkiaGL:
Stan Iliev500a0c32016-10-26 10:30:09 -040080 return new CanvasContext(thread, translucent, rootRenderNode, contextFactory,
Matt Buckleye9023cf2022-11-23 22:39:25 +000081 std::make_unique<skiapipeline::SkiaOpenGLPipeline>(thread),
82 uiThreadId, renderThreadId);
Stan Iliev8a33e402016-07-08 09:57:49 -040083 case RenderPipelineType::SkiaVulkan:
Stan Iliev500a0c32016-10-26 10:30:09 -040084 return new CanvasContext(thread, translucent, rootRenderNode, contextFactory,
Matt Buckleye9023cf2022-11-23 22:39:25 +000085 std::make_unique<skiapipeline::SkiaVulkanPipeline>(thread),
86 uiThreadId, renderThreadId);
Stan Iliev03de0742016-07-07 12:35:54 -040087 default:
John Reck1bcacfd2017-11-03 10:12:19 -070088 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Stan Iliev03de0742016-07-07 12:35:54 -040089 break;
90 }
91 return nullptr;
92}
93
Derek Sollenbergerdaf72292016-10-25 12:09:18 -040094void CanvasContext::invokeFunctor(const RenderThread& thread, Functor* functor) {
95 ATRACE_CALL();
96 auto renderType = Properties::getRenderPipelineType();
97 switch (renderType) {
Stan Iliev500a0c32016-10-26 10:30:09 -040098 case RenderPipelineType::SkiaGL:
99 skiapipeline::SkiaOpenGLPipeline::invokeFunctor(thread, functor);
100 break;
101 case RenderPipelineType::SkiaVulkan:
102 skiapipeline::SkiaVulkanPipeline::invokeFunctor(thread, functor);
103 break;
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400104 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700105 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400106 break;
107 }
108}
109
110void CanvasContext::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
John Reck18f442e2018-04-09 16:56:34 -0700111 skiapipeline::SkiaPipeline::prepareToDraw(thread, bitmap);
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400112}
113
John Reck1bcacfd2017-11-03 10:12:19 -0700114CanvasContext::CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
115 IContextFactory* contextFactory,
Matt Buckleye9023cf2022-11-23 22:39:25 +0000116 std::unique_ptr<IRenderPipeline> renderPipeline, pid_t uiThreadId,
117 pid_t renderThreadId)
John Reck3b202512014-06-23 13:13:08 -0700118 : mRenderThread(thread)
Leon Scroggins III4afdd1c2018-05-14 14:59:30 -0400119 , mGenerationID(0)
John Reck4f02bf42014-01-03 18:09:17 -0800120 , mOpaque(!translucent)
Chris Craik51d6a3d2014-12-22 17:16:56 -0800121 , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
Alec Mouri22d753f2019-09-05 17:11:45 -0700122 , mJankTracker(&thread.globalProfileData())
John Reck3c0f5632019-03-15 16:36:01 -0700123 , mProfiler(mJankTracker.frames(), thread.timeLord().frameIntervalNanos())
Stan Iliev768e3932016-07-08 21:34:52 -0400124 , mContentDrawBounds(0, 0, 0, 0)
Matt Buckleye9023cf2022-11-23 22:39:25 +0000125 , mRenderPipeline(std::move(renderPipeline))
126 , mHintSessionWrapper(uiThreadId, renderThreadId) {
John Reck5f66fb82022-09-23 17:49:23 -0400127 mRenderThread.cacheManager().registerCanvasContext(this);
John Reck2de950d2017-01-25 10:58:30 -0800128 rootRenderNode->makeRoot();
Skuhneea7a7fb2015-08-28 07:10:31 -0700129 mRenderNodes.emplace_back(rootRenderNode);
Alec Mouri22d753f2019-09-05 17:11:45 -0700130 mProfiler.setDensity(DeviceInfo::getDensity());
John Reck23b797a2014-01-03 18:08:34 -0800131}
132
133CanvasContext::~CanvasContext() {
John Reck2de950d2017-01-25 10:58:30 -0800134 destroy();
John Reck2de950d2017-01-25 10:58:30 -0800135 for (auto& node : mRenderNodes) {
136 node->clearRoot();
137 }
138 mRenderNodes.clear();
John Reck5f66fb82022-09-23 17:49:23 -0400139 mRenderThread.cacheManager().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -0800140}
141
John Reck2de950d2017-01-25 10:58:30 -0800142void CanvasContext::addRenderNode(RenderNode* node, bool placeFront) {
143 int pos = placeFront ? 0 : static_cast<int>(mRenderNodes.size());
144 node->makeRoot();
145 mRenderNodes.emplace(mRenderNodes.begin() + pos, node);
146}
147
148void CanvasContext::removeRenderNode(RenderNode* node) {
149 node->clearRoot();
150 mRenderNodes.erase(std::remove(mRenderNodes.begin(), mRenderNodes.end(), node),
John Reck1bcacfd2017-11-03 10:12:19 -0700151 mRenderNodes.end());
John Reck2de950d2017-01-25 10:58:30 -0800152}
153
154void CanvasContext::destroy() {
John Reck17035b02014-09-03 07:39:53 -0700155 stopDrawing();
Nader Jawada3521852023-01-30 20:23:46 -0800156 setHardwareBuffer(nullptr);
Chris Craikd41c4d82015-01-05 15:51:13 -0800157 setSurface(nullptr);
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800158 setSurfaceControl(nullptr);
John Reck2de950d2017-01-25 10:58:30 -0800159 freePrefetchedLayers();
160 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -0700161 mAnimationContext->destroy();
John Reck5f66fb82022-09-23 17:49:23 -0400162 mRenderThread.cacheManager().onContextStopped(this);
John Reck23b797a2014-01-03 18:08:34 -0800163}
164
John Reckfbe14bb2020-09-21 14:37:41 -0700165static void setBufferCount(ANativeWindow* window) {
John Reck8ddbc592020-05-07 16:11:18 -0700166 int query_value;
167 int err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
168 if (err != 0 || query_value < 0) {
169 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, query_value);
170 return;
171 }
172 auto min_undequeued_buffers = static_cast<uint32_t>(query_value);
173
John Reckfbe14bb2020-09-21 14:37:41 -0700174 // We only need to set min_undequeued + 2 because the renderahead amount was already factored into the
175 // query for min_undequeued
176 int bufferCount = min_undequeued_buffers + 2;
John Reck8ddbc592020-05-07 16:11:18 -0700177 native_window_set_buffer_count(window, bufferCount);
178}
179
Nader Jawada3521852023-01-30 20:23:46 -0800180void CanvasContext::setHardwareBuffer(AHardwareBuffer* buffer) {
181 if (mHardwareBuffer) {
182 AHardwareBuffer_release(mHardwareBuffer);
183 mHardwareBuffer = nullptr;
184 }
185
186 if (buffer) {
187 AHardwareBuffer_acquire(buffer);
188 mHardwareBuffer = buffer;
189 }
190 mRenderPipeline->setHardwareBuffer(mHardwareBuffer);
191}
192
Alec Mouri43fe6fc2019-12-23 07:46:19 -0800193void CanvasContext::setSurface(ANativeWindow* window, bool enableTimeout) {
John Reckfbc8df02014-11-14 16:18:41 -0800194 ATRACE_CALL();
195
John Reck8ddbc592020-05-07 16:11:18 -0700196 if (window) {
197 mNativeSurface = std::make_unique<ReliableSurface>(window);
198 mNativeSurface->init();
199 if (enableTimeout) {
200 // TODO: Fix error handling & re-shorten timeout
201 ANativeWindow_setDequeueTimeout(window, 4000_ms);
202 }
John Reck8ddbc592020-05-07 16:11:18 -0700203 } else {
204 mNativeSurface = nullptr;
205 }
John Reckb36bfdd2020-07-23 13:47:49 -0700206 setupPipelineSurface();
207}
John Reck8ddbc592020-05-07 16:11:18 -0700208
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800209void CanvasContext::setSurfaceControl(ASurfaceControl* surfaceControl) {
210 if (surfaceControl == mSurfaceControl) return;
211
212 auto funcs = mRenderThread.getASurfaceControlFunctions();
Jorim Jaggi71db8892021-02-03 23:19:29 +0100213
Huihong Luoe76f1812021-05-06 10:28:12 -0700214 if (surfaceControl == nullptr) {
215 setASurfaceTransactionCallback(nullptr);
Huihong Luo34f42fd2021-05-03 14:47:36 -0700216 setPrepareSurfaceControlForWebviewCallback(nullptr);
Huihong Luoe76f1812021-05-06 10:28:12 -0700217 }
218
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800219 if (mSurfaceControl != nullptr) {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100220 funcs.unregisterListenerFunc(this, &onSurfaceStatsAvailable);
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800221 funcs.releaseFunc(mSurfaceControl);
222 }
223 mSurfaceControl = surfaceControl;
Huihong Luo540fdf82021-06-25 13:59:39 -0700224 mSurfaceControlGenerationId++;
Jorim Jaggi71db8892021-02-03 23:19:29 +0100225 mExpectSurfaceStats = surfaceControl != nullptr;
Pablo Gamitobc9e5292021-08-23 17:12:29 +0200226 if (mExpectSurfaceStats) {
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800227 funcs.acquireFunc(mSurfaceControl);
Pablo Gamitobc9e5292021-08-23 17:12:29 +0200228 funcs.registerListenerFunc(surfaceControl, mSurfaceControlGenerationId, this,
229 &onSurfaceStatsAvailable);
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800230 }
231}
232
John Reckb36bfdd2020-07-23 13:47:49 -0700233void CanvasContext::setupPipelineSurface() {
Alec Mourif023a322019-11-25 10:02:21 -0800234 bool hasSurface = mRenderPipeline->setSurface(
John Reck8ddbc592020-05-07 16:11:18 -0700235 mNativeSurface ? mNativeSurface->getNativeWindow() : nullptr, mSwapBehavior);
236
237 if (mNativeSurface && !mNativeSurface->didSetExtraBuffers()) {
John Reckfbe14bb2020-09-21 14:37:41 -0700238 setBufferCount(mNativeSurface->getNativeWindow());
John Reck8ddbc592020-05-07 16:11:18 -0700239 }
John Reck23b797a2014-01-03 18:08:34 -0800240
Pablo Gamito7e6a5f22021-08-31 13:49:19 +0000241 mFrameNumber = 0;
John Reck28912a52016-04-18 14:34:18 -0700242
John Reckb36bfdd2020-07-23 13:47:49 -0700243 if (mNativeSurface != nullptr && hasSurface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700244 mHaveNewSurface = true;
245 mSwapHistory.clear();
Stan Iliev10689992019-11-13 10:25:22 -0500246 // Enable frame stats after the surface has been bound to the appropriate graphics API.
247 // Order is important when new and old surfaces are the same, because old surface has
248 // its frame stats disabled automatically.
Alec Mouriff07c8f2019-12-11 16:32:22 -0800249 native_window_enable_frame_timestamps(mNativeSurface->getNativeWindow(), true);
Chavi Weingarten084f9f982023-01-24 21:14:11 +0000250 native_window_set_scaling_mode(mNativeSurface->getNativeWindow(),
251 NATIVE_WINDOW_SCALING_MODE_FREEZE);
John Reck368cdd82014-05-07 13:11:00 -0700252 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700253 mRenderThread.removeFrameCallback(this);
Leon Scroggins III4afdd1c2018-05-14 14:59:30 -0400254 mGenerationID++;
John Reck23b797a2014-01-03 18:08:34 -0800255 }
John Reck23b797a2014-01-03 18:08:34 -0800256}
257
John Reck1125d1f2014-10-23 11:02:19 -0700258void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
259 mSwapBehavior = swapBehavior;
260}
261
John Reckf8441e62017-10-23 13:10:41 -0700262bool CanvasContext::pauseSurface() {
Leon Scroggins III4afdd1c2018-05-14 14:59:30 -0400263 mGenerationID++;
John Reck01a5ea32014-12-03 13:01:07 -0800264 return mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -0800265}
266
John Reck8afcc762016-04-13 10:24:06 -0700267void CanvasContext::setStopped(bool stopped) {
268 if (mStopped != stopped) {
269 mStopped = stopped;
270 if (mStopped) {
Leon Scroggins III4afdd1c2018-05-14 14:59:30 -0400271 mGenerationID++;
John Reck8afcc762016-04-13 10:24:06 -0700272 mRenderThread.removeFrameCallback(this);
Stan Iliev768e3932016-07-08 21:34:52 -0400273 mRenderPipeline->onStop();
John Reck5f66fb82022-09-23 17:49:23 -0400274 mRenderThread.cacheManager().onContextStopped(this);
Nader Jawada3521852023-01-30 20:23:46 -0800275 } else if (mIsDirty && hasOutputTarget()) {
John Reck306f3312016-06-10 16:01:55 -0700276 mRenderThread.postFrameCallback(this);
John Reck8afcc762016-04-13 10:24:06 -0700277 }
278 }
279}
280
John Reck8785ceb2018-10-29 16:45:58 -0700281void CanvasContext::allocateBuffers() {
Brett Chabot856db9e2021-05-21 08:41:58 -0700282 if (mNativeSurface && Properties::isDrawingEnabled()) {
Alec Mouric9d5f3d2020-02-13 13:58:25 -0800283 ANativeWindow_tryAllocateBuffers(mNativeSurface->getNativeWindow());
John Reck8785ceb2018-10-29 16:45:58 -0700284 }
285}
286
287void CanvasContext::setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
Chris Craik98787e62015-11-13 10:55:30 -0800288 mLightInfo.ambientShadowAlpha = ambientShadowAlpha;
289 mLightInfo.spotShadowAlpha = spotShadowAlpha;
Alan Viverette50210d92015-05-14 18:05:36 -0700290}
291
John Reck8785ceb2018-10-29 16:45:58 -0700292void CanvasContext::setLightGeometry(const Vector3& lightCenter, float lightRadius) {
Chris Craik6e068c012016-01-15 16:15:30 -0800293 mLightGeometry.center = lightCenter;
John Reck8785ceb2018-10-29 16:45:58 -0700294 mLightGeometry.radius = lightRadius;
John Reck4f02bf42014-01-03 18:09:17 -0800295}
296
John Reck63a06672014-05-07 13:45:54 -0700297void CanvasContext::setOpaque(bool opaque) {
298 mOpaque = opaque;
299}
300
John Reck55887762023-01-25 16:51:18 -0500301float CanvasContext::setColorMode(ColorMode mode) {
302 if (mode != mColorMode) {
John Reck55887762023-01-25 16:51:18 -0500303 mColorMode = mode;
304 mRenderPipeline->setSurfaceColorProperties(mode);
305 setupPipelineSurface();
306 }
307 switch (mColorMode) {
308 case ColorMode::Hdr:
John Reck342f6212023-03-14 21:58:26 -0400309 return Properties::maxHdrHeadroomOn8bit;
John Reck0b3f3312023-01-31 16:21:28 -0500310 case ColorMode::Hdr10:
311 return 10.f;
John Reck55887762023-01-25 16:51:18 -0500312 default:
313 return 1.f;
314 }
315}
316
317float CanvasContext::targetSdrHdrRatio() const {
John Reck0b3f3312023-01-31 16:21:28 -0500318 if (mColorMode == ColorMode::Hdr || mColorMode == ColorMode::Hdr10) {
John Reck55887762023-01-25 16:51:18 -0500319 return mTargetSdrHdrRatio;
320 } else {
321 return 1.f;
322 }
323}
324
325void CanvasContext::setTargetSdrHdrRatio(float ratio) {
326 if (mTargetSdrHdrRatio == ratio) return;
327
328 mTargetSdrHdrRatio = ratio;
329 mRenderPipeline->setTargetSdrHdrRatio(ratio);
330 // We don't actually but we need to behave as if we do. Specifically we need to ensure
331 // all buffers in the swapchain are fully re-rendered as any partial updates to them will
332 // result in mixed target white points which looks really bad & flickery
333 mHaveNewSurface = true;
Romain Guy26a2b972017-04-17 09:39:51 -0700334}
335
John Reck8afcc762016-04-13 10:24:06 -0700336bool CanvasContext::makeCurrent() {
337 if (mStopped) return false;
338
Stan Iliev768e3932016-07-08 21:34:52 -0400339 auto result = mRenderPipeline->makeCurrent();
340 switch (result) {
341 case MakeCurrentResult::AlreadyCurrent:
342 return true;
343 case MakeCurrentResult::Failed:
344 mHaveNewSurface = true;
345 setSurface(nullptr);
346 return false;
347 case MakeCurrentResult::Succeeded:
348 mHaveNewSurface = true;
349 return true;
350 default:
351 LOG_ALWAYS_FATAL("unexpected result %d from IRenderPipeline::makeCurrent",
John Reck1bcacfd2017-11-03 10:12:19 -0700352 (int32_t)result);
John Reckf2dcc2a2015-07-16 09:17:59 -0700353 }
Stan Iliev768e3932016-07-08 21:34:52 -0400354
355 return true;
John Reck860d1552014-04-11 19:15:05 -0700356}
357
John Reckbf3c6022015-06-02 15:55:00 -0700358static bool wasSkipped(FrameInfo* info) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700359 return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
John Reckbf3c6022015-06-02 15:55:00 -0700360}
361
John Reck0def73a2016-07-01 16:19:13 -0700362bool CanvasContext::isSwapChainStuffed() {
John Recka3d795a2016-07-27 19:28:05 -0700363 static const auto SLOW_THRESHOLD = 6_ms;
364
John Reck0def73a2016-07-01 16:19:13 -0700365 if (mSwapHistory.size() != mSwapHistory.capacity()) {
366 // We want at least 3 frames of history before attempting to
367 // guess if the queue is stuffed
368 return false;
369 }
370 nsecs_t frameInterval = mRenderThread.timeLord().frameIntervalNanos();
371 auto& swapA = mSwapHistory[0];
372
373 // Was there a happy queue & dequeue time? If so, don't
374 // consider it stuffed
John Reck1bcacfd2017-11-03 10:12:19 -0700375 if (swapA.dequeueDuration < SLOW_THRESHOLD && swapA.queueDuration < SLOW_THRESHOLD) {
John Reck0def73a2016-07-01 16:19:13 -0700376 return false;
377 }
378
379 for (size_t i = 1; i < mSwapHistory.size(); i++) {
380 auto& swapB = mSwapHistory[i];
381
Chris Craik31635682016-07-19 17:59:12 -0700382 // If there's a multi-frameInterval gap we effectively already dropped a frame,
John Reck0def73a2016-07-01 16:19:13 -0700383 // so consider the queue healthy.
Yu Jiae57493a2018-05-30 11:18:05 +0800384 if (std::abs(swapA.swapCompletedTime - swapB.swapCompletedTime) > frameInterval * 3) {
John Reck0def73a2016-07-01 16:19:13 -0700385 return false;
386 }
387
388 // Was there a happy queue & dequeue time? If so, don't
389 // consider it stuffed
John Reck1bcacfd2017-11-03 10:12:19 -0700390 if (swapB.dequeueDuration < SLOW_THRESHOLD && swapB.queueDuration < SLOW_THRESHOLD) {
John Reck0def73a2016-07-01 16:19:13 -0700391 return false;
392 }
393
394 swapA = swapB;
395 }
396
397 // All signs point to a stuffed swap chain
Tim Murrayffde62742016-07-18 14:11:28 -0700398 ATRACE_NAME("swap chain stuffed");
John Reck0def73a2016-07-01 16:19:13 -0700399 return true;
400}
401
John Reck1bcacfd2017-11-03 10:12:19 -0700402void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo, int64_t syncQueued,
403 RenderNode* target) {
John Reckf9be7792014-05-02 18:21:16 -0700404 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700405
John Reckbf3c6022015-06-02 15:55:00 -0700406 // If the previous frame was dropped we don't need to hold onto it, so
407 // just keep using the previous frame's structure instead
408 if (!wasSkipped(mCurrentFrameInfo)) {
John Reck34781b22017-07-05 16:39:36 -0700409 mCurrentFrameInfo = mJankTracker.startFrame();
John Reckbf3c6022015-06-02 15:55:00 -0700410 }
Jorim Jaggi71db8892021-02-03 23:19:29 +0100411
John Reckba6adf62015-02-19 14:36:50 -0800412 mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
John Reckbe3fba02015-07-06 13:49:58 -0700413 mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
John Reckba6adf62015-02-19 14:36:50 -0800414 mCurrentFrameInfo->markSyncStart();
415
John Recke4267ea2014-06-03 15:53:15 -0700416 info.damageAccumulator = &mDamageAccumulator;
Chris Craik0b7e8242015-10-28 16:50:44 -0700417 info.layerUpdateQueue = &mLayerUpdateQueue;
John Reck41459192019-10-31 15:04:58 -0700418 info.damageGenerationId = mDamageId++;
John Reck848f6512018-12-03 13:26:43 -0800419 info.out.canDrawThisFrame = true;
John Reck00e79c92015-07-21 10:23:59 -0700420
John Reckec845a22014-09-05 15:23:38 -0700421 mAnimationContext->startFrame(info.mode);
John Reck0fa0cbc2019-04-05 16:57:46 -0700422 for (const sp<RenderNode>& node : mRenderNodes) {
Skuhneea7a7fb2015-08-28 07:10:31 -0700423 // Only the primary target node will be drawn full - all other nodes would get drawn in
424 // real time mode. In case of a window, the primary node is the window content and the other
425 // node(s) are non client / filler nodes.
426 info.mode = (node.get() == target ? TreeInfo::MODE_FULL : TreeInfo::MODE_RT_ONLY);
427 node->prepareTree(info);
John Reck975591a2016-01-22 16:28:07 -0800428 GL_CHECKPOINT(MODERATE);
Skuhneea7a7fb2015-08-28 07:10:31 -0700429 }
John Reck119907c2014-08-14 09:02:01 -0700430 mAnimationContext->runRemainingAnimations(info);
John Reck975591a2016-01-22 16:28:07 -0800431 GL_CHECKPOINT(MODERATE);
John Recke45b1fd2014-04-15 09:50:16 -0700432
John Reck2de950d2017-01-25 10:58:30 -0800433 freePrefetchedLayers();
John Reck975591a2016-01-22 16:28:07 -0800434 GL_CHECKPOINT(MODERATE);
John Reck998a6d82014-08-28 15:35:53 -0700435
John Reck306f3312016-06-10 16:01:55 -0700436 mIsDirty = true;
437
Nader Jawada3521852023-01-30 20:23:46 -0800438 if (CC_UNLIKELY(!hasOutputTarget())) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700439 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaa95a882014-11-07 11:02:07 -0800440 info.out.canDrawThisFrame = false;
441 return;
442 }
443
chaviwadba0b12022-03-18 17:42:15 -0500444 if (CC_LIKELY(mSwapHistory.size() && !info.forceDrawFrame)) {
John Recke486d932015-10-28 09:21:19 -0700445 nsecs_t latestVsync = mRenderThread.timeLord().latestVsync();
John Reck0fa0cbc2019-04-05 16:57:46 -0700446 SwapHistory& lastSwap = mSwapHistory.back();
John Reck52b783f2015-11-24 11:12:55 -0800447 nsecs_t vsyncDelta = std::abs(lastSwap.vsyncTime - latestVsync);
John Recke486d932015-10-28 09:21:19 -0700448 // The slight fudge-factor is to deal with cases where
449 // the vsync was estimated due to being slow handling the signal.
450 // See the logic in TimeLord#computeFrameTimeNanos or in
451 // Choreographer.java for details on when this happens
452 if (vsyncDelta < 2_ms) {
453 // Already drew for this vsync pulse, UI draw request missed
454 // the deadline for RT animations
455 info.out.canDrawThisFrame = false;
John Reck6f75da02018-03-21 14:43:40 -0700456 }
Bo Hudd082242018-12-02 05:22:41 +0000457 } else {
458 info.out.canDrawThisFrame = true;
John Recke486d932015-10-28 09:21:19 -0700459 }
John Recka5dda642014-05-22 15:43:54 -0700460
John Reckcc2eee82018-05-17 10:44:00 -0700461 // TODO: Do we need to abort out if the backdrop is added but not ready? Should that even
462 // be an allowable combination?
463 if (mRenderNodes.size() > 2 && !mRenderNodes[1]->isRenderable()) {
464 info.out.canDrawThisFrame = false;
465 }
466
John Reck848f6512018-12-03 13:26:43 -0800467 if (info.out.canDrawThisFrame) {
468 int err = mNativeSurface->reserveNext();
469 if (err != OK) {
470 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
471 info.out.canDrawThisFrame = false;
472 ALOGW("reserveNext failed, error = %d (%s)", err, strerror(-err));
473 if (err != TIMED_OUT) {
474 // A timed out surface can still recover, but assume others are permanently dead.
475 setSurface(nullptr);
476 return;
477 }
478 }
479 } else {
Chris Craik1b54fb22015-06-02 17:40:58 -0700480 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaef9dc82015-05-08 14:10:57 -0700481 }
482
Leon Scroggins III4afdd1c2018-05-14 14:59:30 -0400483 bool postedFrameCallback = false;
John Recka5dda642014-05-22 15:43:54 -0700484 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reck9f516442017-09-25 10:27:21 -0700485 if (CC_UNLIKELY(!Properties::enableRTAnimations)) {
486 info.out.requiresUiRedraw = true;
487 }
John Reckcd028f32014-06-24 08:44:29 -0700488 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700489 // If animationsNeedsRedraw is set don't bother posting for an RT anim
490 // as we will just end up fighting the UI thread.
491 mRenderThread.postFrameCallback(this);
Leon Scroggins III4afdd1c2018-05-14 14:59:30 -0400492 postedFrameCallback = true;
493 }
494 }
495
496 if (!postedFrameCallback &&
497 info.out.animatedImageDelay != TreeInfo::Out::kNoAnimatedImageDelay) {
498 // Subtract the time of one frame so it can be displayed on time.
499 const nsecs_t kFrameTime = mRenderThread.timeLord().frameIntervalNanos();
500 if (info.out.animatedImageDelay <= kFrameTime) {
501 mRenderThread.postFrameCallback(this);
502 } else {
503 const auto delay = info.out.animatedImageDelay - kFrameTime;
504 int genId = mGenerationID;
505 mRenderThread.queue().postDelayed(delay, [this, genId]() {
506 if (mGenerationID == genId) {
507 mRenderThread.postFrameCallback(this);
508 }
509 });
John Reckf9be7792014-05-02 18:21:16 -0700510 }
John Recke45b1fd2014-04-15 09:50:16 -0700511 }
512}
513
John Reckf47a5942014-06-30 16:20:04 -0700514void CanvasContext::stopDrawing() {
515 mRenderThread.removeFrameCallback(this);
Doris Liuc82e8792016-07-29 16:45:24 -0700516 mAnimationContext->pauseAnimators();
Leon Scroggins III4afdd1c2018-05-14 14:59:30 -0400517 mGenerationID++;
John Reckf47a5942014-06-30 16:20:04 -0700518}
519
John Recka5dda642014-05-22 15:43:54 -0700520void CanvasContext::notifyFramePending() {
521 ATRACE_CALL();
522 mRenderThread.pushBackFrameCallback(this);
Matt Buckley12271682022-12-02 22:04:34 +0000523 sendLoadResetHint();
John Recka5dda642014-05-22 15:43:54 -0700524}
525
Nader Jawad24617222023-03-30 12:29:01 -0700526Frame CanvasContext::getFrame() {
527 if (mHardwareBuffer != nullptr) {
528 return {mBufferParams.getLogicalWidth(), mBufferParams.getLogicalHeight(), 0};
529 } else {
530 return mRenderPipeline->getFrame();
531 }
532}
533
Matt Buckleye9023cf2022-11-23 22:39:25 +0000534void CanvasContext::draw() {
Greg Daniel16d11c62021-04-08 16:17:35 -0400535 if (auto grContext = getGrContext()) {
536 if (grContext->abandoned()) {
537 LOG_ALWAYS_FATAL("GrContext is abandoned/device lost at start of CanvasContext::draw");
Matt Buckleye9023cf2022-11-23 22:39:25 +0000538 return;
Greg Daniel16d11c62021-04-08 16:17:35 -0400539 }
540 }
John Recke4267ea2014-06-03 15:53:15 -0700541 SkRect dirty;
542 mDamageAccumulator.finish(&dirty);
543
Matt Buckleye9023cf2022-11-23 22:39:25 +0000544 // reset syncDelayDuration each time we draw
545 nsecs_t syncDelayDuration = mSyncDelayDuration;
546 nsecs_t idleDuration = mIdleDuration;
547 mSyncDelayDuration = 0;
548 mIdleDuration = 0;
549
Brett Chabot856db9e2021-05-21 08:41:58 -0700550 if (!Properties::isDrawingEnabled() ||
551 (dirty.isEmpty() && Properties::skipEmptyFrames && !surfaceRequiresRedraw())) {
John Reck0b19a732019-03-07 17:18:25 -0800552 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
Leon Scroggins III7fbd0ad2021-08-06 13:42:49 -0400553 if (auto grContext = getGrContext()) {
554 // Submit to ensure that any texture uploads complete and Skia can
555 // free its staging buffers.
556 grContext->flushAndSubmit();
557 }
558
chaviw5fc80e72020-04-30 12:14:35 -0700559 // Notify the callbacks, even if there's nothing to draw so they aren't waiting
560 // indefinitely
Vishnu Nair70a8f0e2020-10-14 18:47:46 -0700561 waitOnFences();
chaviw9c137532021-08-20 12:15:48 -0500562 for (auto& func : mFrameCommitCallbacks) {
563 std::invoke(func, false /* didProduceBuffer */);
chaviw5fc80e72020-04-30 12:14:35 -0700564 }
chaviw9c137532021-08-20 12:15:48 -0500565 mFrameCommitCallbacks.clear();
Matt Buckleye9023cf2022-11-23 22:39:25 +0000566 return;
John Reck0b19a732019-03-07 17:18:25 -0800567 }
John Reck240ff622015-04-28 13:50:00 -0700568
Huihong Luo054b8d32021-02-24 18:48:12 -0800569 ScopedActiveContext activeContext(this);
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100570 mCurrentFrameInfo->set(FrameInfoIndex::FrameInterval) =
571 mRenderThread.timeLord().frameIntervalNanos();
572
John Reck240ff622015-04-28 13:50:00 -0700573 mCurrentFrameInfo->markIssueDrawCommandsStart();
574
Nader Jawad24617222023-03-30 12:29:01 -0700575 Frame frame = getFrame();
576
Stan Iliev768e3932016-07-08 21:34:52 -0400577 SkRect windowDirty = computeDirtyRect(frame, &dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800578
John Reck8a1ddd72021-10-19 12:25:24 -0400579 ATRACE_FORMAT("Drawing " RECT_STRING, SK_RECT_ARGS(dirty));
580
John Reckb013c8d2022-08-15 11:08:41 -0400581 IRenderPipeline::DrawResult drawResult;
582 {
583 // FrameInfoVisualizer accesses the frame events, which cannot be mutated mid-draw
584 // or it can lead to memory corruption.
585 // This lock is overly broad, but it's the quickest fix since this mutex is otherwise
586 // not visible to IRenderPipeline much less FrameInfoVisualizer. And since this is
587 // the thread we're primarily concerned about being responsive, this being too broad
588 // shouldn't pose a performance issue.
589 std::scoped_lock lock(mFrameMetricsReporterMutex);
590 drawResult = mRenderPipeline->draw(frame, windowDirty, dirty, mLightGeometry,
591 &mLayerUpdateQueue, mContentDrawBounds, mOpaque,
Nader Jawada3521852023-01-30 20:23:46 -0800592 mLightInfo, mRenderNodes, &(profiler()), mBufferParams);
John Reckb013c8d2022-08-15 11:08:41 -0400593 }
Chris Craik1dfa0702016-03-04 15:59:24 -0800594
Pablo Gamito35b80cd2021-08-24 11:03:51 +0200595 uint64_t frameCompleteNr = getFrameNumber();
John Reckcc2eee82018-05-17 10:44:00 -0700596
John Reck38f6c032016-03-17 10:23:49 -0700597 waitOnFences();
598
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700599 if (mNativeSurface) {
600 // TODO(b/165985262): measure performance impact
Siarhei Vishniakoud11f38f2021-01-12 20:45:29 -1000601 const auto vsyncId = mCurrentFrameInfo->get(FrameInfoIndex::FrameTimelineVsyncId);
602 if (vsyncId != UiFrameInfoBuilder::INVALID_VSYNC_ID) {
Siarhei Vishniakou4bcbffd2021-02-17 06:19:36 +0000603 const auto inputEventId =
604 static_cast<int32_t>(mCurrentFrameInfo->get(FrameInfoIndex::InputEventId));
Rachel Lee4fa74842021-11-16 13:30:11 -0800605 native_window_set_frame_timeline_info(
Ady Abraham2cf50532023-01-19 17:06:32 -0800606 mNativeSurface->getNativeWindow(), frameCompleteNr, vsyncId, inputEventId,
Rachel Lee4fa74842021-11-16 13:30:11 -0800607 mCurrentFrameInfo->get(FrameInfoIndex::FrameStartTime));
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700608 }
609 }
610
Stan Iliev768e3932016-07-08 21:34:52 -0400611 bool requireSwap = false;
Matt Buckley864ab952022-10-18 23:03:59 +0000612 bool didDraw = false;
613
John Reck59dd2ea2019-07-26 16:51:08 -0700614 int error = OK;
Alec Mouri3afb3972022-05-27 22:03:11 +0000615 bool didSwap = mRenderPipeline->swapBuffers(frame, drawResult.success, windowDirty,
616 mCurrentFrameInfo, &requireSwap);
617
618 mCurrentFrameInfo->set(FrameInfoIndex::CommandSubmissionCompleted) = std::max(
619 drawResult.commandSubmissionTime, mCurrentFrameInfo->get(FrameInfoIndex::SwapBuffers));
John Reck9372ac32016-01-19 11:46:52 -0800620
John Reck306f3312016-06-10 16:01:55 -0700621 mIsDirty = false;
John Reckba6adf62015-02-19 14:36:50 -0800622
Stan Iliev768e3932016-07-08 21:34:52 -0400623 if (requireSwap) {
Matt Buckley864ab952022-10-18 23:03:59 +0000624 didDraw = true;
John Reck59dd2ea2019-07-26 16:51:08 -0700625 // Handle any swapchain errors
626 error = mNativeSurface->getAndClearError();
627 if (error == TIMED_OUT) {
628 // Try again
629 mRenderThread.postFrameCallback(this);
630 // But since this frame didn't happen, we need to mark full damage in the swap
631 // history
632 didDraw = false;
633
634 } else if (error != OK || !didSwap) {
635 // Unknown error, abandon the surface
John Reck149173d2015-08-10 09:52:29 -0700636 setSurface(nullptr);
John Reck59dd2ea2019-07-26 16:51:08 -0700637 didDraw = false;
John Reck149173d2015-08-10 09:52:29 -0700638 }
John Reck59dd2ea2019-07-26 16:51:08 -0700639
John Recke486d932015-10-28 09:21:19 -0700640 SwapHistory& swap = mSwapHistory.next();
John Reck59dd2ea2019-07-26 16:51:08 -0700641 if (didDraw) {
642 swap.damage = windowDirty;
643 } else {
Nick Desaulniersb451d872019-11-04 17:18:51 -0800644 float max = static_cast<float>(INT_MAX);
645 swap.damage = SkRect::MakeWH(max, max);
John Reck59dd2ea2019-07-26 16:51:08 -0700646 }
Jerome Gaillarde218c692019-06-14 12:58:57 +0100647 swap.swapCompletedTime = systemTime(SYSTEM_TIME_MONOTONIC);
John Recke486d932015-10-28 09:21:19 -0700648 swap.vsyncTime = mRenderThread.timeLord().latestVsync();
John Reck59dd2ea2019-07-26 16:51:08 -0700649 if (didDraw) {
Alec Mourif023a322019-11-25 10:02:21 -0800650 nsecs_t dequeueStart =
651 ANativeWindow_getLastDequeueStartTime(mNativeSurface->getNativeWindow());
John Recka67b62e2017-06-01 12:44:58 -0700652 if (dequeueStart < mCurrentFrameInfo->get(FrameInfoIndex::SyncStart)) {
653 // Ignoring dequeue duration as it happened prior to frame render start
John Reck32414ee2017-05-31 14:02:50 -0700654 // and thus is not part of the frame.
655 swap.dequeueDuration = 0;
656 } else {
Alec Mouri8d451742019-08-01 19:19:16 -0700657 swap.dequeueDuration =
Alec Mourif023a322019-11-25 10:02:21 -0800658 ANativeWindow_getLastDequeueDuration(mNativeSurface->getNativeWindow());
John Reck32414ee2017-05-31 14:02:50 -0700659 }
Alec Mourif023a322019-11-25 10:02:21 -0800660 swap.queueDuration =
661 ANativeWindow_getLastQueueDuration(mNativeSurface->getNativeWindow());
John Reck882d5152016-08-01 14:41:08 -0700662 } else {
663 swap.dequeueDuration = 0;
664 swap.queueDuration = 0;
665 }
John Reck1bcacfd2017-11-03 10:12:19 -0700666 mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) = swap.dequeueDuration;
667 mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) = swap.queueDuration;
John Reck149173d2015-08-10 09:52:29 -0700668 mHaveNewSurface = false;
Pablo Gamito7e6a5f22021-08-31 13:49:19 +0000669 mFrameNumber = 0;
John Reck70e89c92016-08-05 10:50:36 -0700670 } else {
671 mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) = 0;
672 mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) = 0;
John Reck4f02bf42014-01-03 18:09:17 -0800673 }
John Reckfe5e7b72014-05-23 17:42:28 -0700674
Jorim Jaggi71db8892021-02-03 23:19:29 +0100675 mCurrentFrameInfo->markSwapBuffersCompleted();
John Reck149173d2015-08-10 09:52:29 -0700676
677#if LOG_FRAMETIME_MMA
John Reck1bcacfd2017-11-03 10:12:19 -0700678 float thisFrame = mCurrentFrameInfo->duration(FrameInfoIndex::IssueDrawCommandsStart,
679 FrameInfoIndex::FrameCompleted) /
680 NANOS_PER_MILLIS_F;
John Reck149173d2015-08-10 09:52:29 -0700681 if (sFrameCount) {
682 sBenchMma = ((9 * sBenchMma) + thisFrame) / 10;
683 } else {
684 sBenchMma = thisFrame;
685 }
686 if (++sFrameCount == 10) {
687 sFrameCount = 1;
688 ALOGD("Average frame time: %.4f", sBenchMma);
689 }
690#endif
691
John Reckcc2eee82018-05-17 10:44:00 -0700692 if (didSwap) {
chaviw9c137532021-08-20 12:15:48 -0500693 for (auto& func : mFrameCommitCallbacks) {
694 std::invoke(func, true /* didProduceBuffer */);
John Reckcc2eee82018-05-17 10:44:00 -0700695 }
chaviw9c137532021-08-20 12:15:48 -0500696 mFrameCommitCallbacks.clear();
John Reckcc2eee82018-05-17 10:44:00 -0700697 }
698
Jorim Jaggi71db8892021-02-03 23:19:29 +0100699 if (requireSwap) {
700 if (mExpectSurfaceStats) {
Siarhei Vishniakouadf50242021-03-11 21:21:05 +0000701 reportMetricsWithPresentTime();
Pablo Gamitobc9e5292021-08-23 17:12:29 +0200702 { // acquire lock
703 std::lock_guard lock(mLast4FrameMetricsInfosMutex);
704 FrameMetricsInfo& next = mLast4FrameMetricsInfos.next();
705 next.frameInfo = mCurrentFrameInfo;
706 next.frameNumber = frameCompleteNr;
707 next.surfaceId = mSurfaceControlGenerationId;
708 } // release lock
Jorim Jaggi71db8892021-02-03 23:19:29 +0100709 } else {
710 mCurrentFrameInfo->markFrameCompleted();
711 mCurrentFrameInfo->set(FrameInfoIndex::GpuCompleted)
712 = mCurrentFrameInfo->get(FrameInfoIndex::FrameCompleted);
Siarhei Vishniakou07d35cb2021-07-03 02:22:12 +0000713 std::scoped_lock lock(mFrameMetricsReporterMutex);
Pablo Gamito88660d72021-08-09 14:37:56 +0000714 mJankTracker.finishFrame(*mCurrentFrameInfo, mFrameMetricsReporter, frameCompleteNr,
715 mSurfaceControlGenerationId);
Alec Mouri026106f2020-03-26 17:17:09 -0700716 }
Stan Iliev7203e1f2019-07-25 13:12:02 -0400717 }
718
Matt Buckleye9023cf2022-11-23 22:39:25 +0000719 int64_t intendedVsync = mCurrentFrameInfo->get(FrameInfoIndex::IntendedVsync);
720 int64_t frameDeadline = mCurrentFrameInfo->get(FrameInfoIndex::FrameDeadline);
721 int64_t dequeueBufferDuration = mCurrentFrameInfo->get(FrameInfoIndex::DequeueBufferDuration);
722
723 mHintSessionWrapper.updateTargetWorkDuration(frameDeadline - intendedVsync);
724
725 if (didDraw) {
726 int64_t frameStartTime = mCurrentFrameInfo->get(FrameInfoIndex::FrameStartTime);
727 int64_t frameDuration = systemTime(SYSTEM_TIME_MONOTONIC) - frameStartTime;
728 int64_t actualDuration = frameDuration -
729 (std::min(syncDelayDuration, mLastDequeueBufferDuration)) -
730 dequeueBufferDuration - idleDuration;
731 mHintSessionWrapper.reportActualWorkDuration(actualDuration);
732 }
733
734 mLastDequeueBufferDuration = dequeueBufferDuration;
735
Stan Ilieve0fae232020-01-07 17:21:49 -0500736 mRenderThread.cacheManager().onFrameCompleted();
Matt Buckleye9023cf2022-11-23 22:39:25 +0000737 return;
John Reck4f02bf42014-01-03 18:09:17 -0800738}
739
Siarhei Vishniakouf0cf18d2021-02-26 00:15:04 +0000740void CanvasContext::reportMetricsWithPresentTime() {
Siarhei Vishniakou07d35cb2021-07-03 02:22:12 +0000741 { // acquire lock
742 std::scoped_lock lock(mFrameMetricsReporterMutex);
743 if (mFrameMetricsReporter == nullptr) {
744 return;
745 }
746 } // release lock
Siarhei Vishniakouf0cf18d2021-02-26 00:15:04 +0000747 if (mNativeSurface == nullptr) {
748 return;
749 }
John Reckb7039c62021-07-07 19:18:34 -0400750 ATRACE_CALL();
Siarhei Vishniakouf0cf18d2021-02-26 00:15:04 +0000751 FrameInfo* forthBehind;
752 int64_t frameNumber;
Pablo Gamito88660d72021-08-09 14:37:56 +0000753 int32_t surfaceControlId;
754
Siarhei Vishniakouf0cf18d2021-02-26 00:15:04 +0000755 { // acquire lock
Pablo Gamito88660d72021-08-09 14:37:56 +0000756 std::scoped_lock lock(mLast4FrameMetricsInfosMutex);
757 if (mLast4FrameMetricsInfos.size() != mLast4FrameMetricsInfos.capacity()) {
Siarhei Vishniakouf0cf18d2021-02-26 00:15:04 +0000758 // Not enough frames yet
759 return;
760 }
Pablo Gamito88660d72021-08-09 14:37:56 +0000761 auto frameMetricsInfo = mLast4FrameMetricsInfos.front();
762 forthBehind = frameMetricsInfo.frameInfo;
763 frameNumber = frameMetricsInfo.frameNumber;
764 surfaceControlId = frameMetricsInfo.surfaceId;
Siarhei Vishniakouf0cf18d2021-02-26 00:15:04 +0000765 } // release lock
766
767 nsecs_t presentTime = 0;
768 native_window_get_frame_timestamps(
769 mNativeSurface->getNativeWindow(), frameNumber, nullptr /*outRequestedPresentTime*/,
770 nullptr /*outAcquireTime*/, nullptr /*outLatchTime*/,
771 nullptr /*outFirstRefreshStartTime*/, nullptr /*outLastRefreshStartTime*/,
772 nullptr /*outGpuCompositionDoneTime*/, &presentTime, nullptr /*outDequeueReadyTime*/,
773 nullptr /*outReleaseTime*/);
774
775 forthBehind->set(FrameInfoIndex::DisplayPresentTime) = presentTime;
Siarhei Vishniakou07d35cb2021-07-03 02:22:12 +0000776 { // acquire lock
777 std::scoped_lock lock(mFrameMetricsReporterMutex);
778 if (mFrameMetricsReporter != nullptr) {
Pablo Gamito88660d72021-08-09 14:37:56 +0000779 mFrameMetricsReporter->reportFrameMetrics(forthBehind->data(), true /*hasPresentTime*/,
780 frameNumber, surfaceControlId);
Siarhei Vishniakou07d35cb2021-07-03 02:22:12 +0000781 }
782 } // release lock
783}
784
Pablo Gamito88660d72021-08-09 14:37:56 +0000785void CanvasContext::addFrameMetricsObserver(FrameMetricsObserver* observer) {
786 std::scoped_lock lock(mFrameMetricsReporterMutex);
787 if (mFrameMetricsReporter.get() == nullptr) {
788 mFrameMetricsReporter.reset(new FrameMetricsReporter());
789 }
790
791 // We want to make sure we aren't reporting frames that have already been queued by the
792 // BufferQueueProducer on the rendner thread but are still pending the callback to report their
793 // their frame metrics.
Pablo Gamito35b80cd2021-08-24 11:03:51 +0200794 uint64_t nextFrameNumber = getFrameNumber();
Pablo Gamito88660d72021-08-09 14:37:56 +0000795 observer->reportMetricsFrom(nextFrameNumber, mSurfaceControlGenerationId);
796 mFrameMetricsReporter->addObserver(observer);
797}
798
799void CanvasContext::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
800 std::scoped_lock lock(mFrameMetricsReporterMutex);
801 if (mFrameMetricsReporter.get() != nullptr) {
802 mFrameMetricsReporter->removeObserver(observer);
803 if (!mFrameMetricsReporter->hasObservers()) {
804 mFrameMetricsReporter.reset(nullptr);
Siarhei Vishniakou07d35cb2021-07-03 02:22:12 +0000805 }
806 }
Pablo Gamito88660d72021-08-09 14:37:56 +0000807}
808
Pablo Gamitobc9e5292021-08-23 17:12:29 +0200809FrameInfo* CanvasContext::getFrameInfoFromLast4(uint64_t frameNumber, uint32_t surfaceControlId) {
Pablo Gamito88660d72021-08-09 14:37:56 +0000810 std::scoped_lock lock(mLast4FrameMetricsInfosMutex);
811 for (size_t i = 0; i < mLast4FrameMetricsInfos.size(); i++) {
Pablo Gamitobc9e5292021-08-23 17:12:29 +0200812 if (mLast4FrameMetricsInfos[i].frameNumber == frameNumber &&
813 mLast4FrameMetricsInfos[i].surfaceId == surfaceControlId) {
814 return mLast4FrameMetricsInfos[i].frameInfo;
Pablo Gamito88660d72021-08-09 14:37:56 +0000815 }
816 }
817
Siarhei Vishniakou07d35cb2021-07-03 02:22:12 +0000818 return nullptr;
Siarhei Vishniakouf0cf18d2021-02-26 00:15:04 +0000819}
820
Pablo Gamito14b28ce9c2021-09-06 16:33:23 +0000821void CanvasContext::onSurfaceStatsAvailable(void* context, int32_t surfaceControlId,
822 ASurfaceControlStats* stats) {
Pablo Gamito88660d72021-08-09 14:37:56 +0000823 auto* instance = static_cast<CanvasContext*>(context);
Jorim Jaggi71db8892021-02-03 23:19:29 +0100824
825 const ASurfaceControlFunctions& functions =
826 instance->mRenderThread.getASurfaceControlFunctions();
827
828 nsecs_t gpuCompleteTime = functions.getAcquireTimeFunc(stats);
John Reckc8024fc2021-10-18 19:25:46 -0400829 if (gpuCompleteTime == Fence::SIGNAL_TIME_PENDING) {
830 gpuCompleteTime = -1;
831 }
Jorim Jaggi71db8892021-02-03 23:19:29 +0100832 uint64_t frameNumber = functions.getFrameNumberFunc(stats);
833
Pablo Gamitobc9e5292021-08-23 17:12:29 +0200834 FrameInfo* frameInfo = instance->getFrameInfoFromLast4(frameNumber, surfaceControlId);
Siarhei Vishniakouf0cf18d2021-02-26 00:15:04 +0000835
Jorim Jaggi71db8892021-02-03 23:19:29 +0100836 if (frameInfo != nullptr) {
John Reckb013c8d2022-08-15 11:08:41 -0400837 std::scoped_lock lock(instance->mFrameMetricsReporterMutex);
Jorim Jaggib891cdf2021-05-11 14:57:25 +0200838 frameInfo->set(FrameInfoIndex::FrameCompleted) = std::max(gpuCompleteTime,
839 frameInfo->get(FrameInfoIndex::SwapBuffersCompleted));
Alec Mouri3afb3972022-05-27 22:03:11 +0000840 frameInfo->set(FrameInfoIndex::GpuCompleted) = std::max(
841 gpuCompleteTime, frameInfo->get(FrameInfoIndex::CommandSubmissionCompleted));
Pablo Gamitobc9e5292021-08-23 17:12:29 +0200842 instance->mJankTracker.finishFrame(*frameInfo, instance->mFrameMetricsReporter, frameNumber,
843 surfaceControlId);
Jorim Jaggi71db8892021-02-03 23:19:29 +0100844 }
845}
846
John Recke45b1fd2014-04-15 09:50:16 -0700847// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700848void CanvasContext::doFrame() {
Stan Iliev768e3932016-07-08 21:34:52 -0400849 if (!mRenderPipeline->isSurfaceReady()) return;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000850 mIdleDuration =
851 systemTime(SYSTEM_TIME_MONOTONIC) - mRenderThread.timeLord().computeFrameTimeNanos();
Skuhneea7a7fb2015-08-28 07:10:31 -0700852 prepareAndDraw(nullptr);
853}
John Reck368cdd82014-05-07 13:11:00 -0700854
Leon Scroggins III6c5864c2019-04-03 15:09:25 -0400855SkISize CanvasContext::getNextFrameSize() const {
Alec Mourif023a322019-11-25 10:02:21 -0800856 static constexpr SkISize defaultFrameSize = {INT32_MAX, INT32_MAX};
857 if (mNativeSurface == nullptr) {
858 return defaultFrameSize;
Leon Scroggins III6c5864c2019-04-03 15:09:25 -0400859 }
Alec Mourif023a322019-11-25 10:02:21 -0800860 ANativeWindow* anw = mNativeSurface->getNativeWindow();
861
862 SkISize size;
863 size.fWidth = ANativeWindow_getWidth(anw);
864 size.fHeight = ANativeWindow_getHeight(anw);
John Reck5f66fb82022-09-23 17:49:23 -0400865 mRenderThread.cacheManager().notifyNextFrameSize(size.fWidth, size.fHeight);
Alec Mourif023a322019-11-25 10:02:21 -0800866 return size;
Leon Scroggins III6c5864c2019-04-03 15:09:25 -0400867}
868
John Reckee184272023-03-21 12:29:21 -0400869const SkM44& CanvasContext::getPixelSnapMatrix() const {
870 return mRenderPipeline->getPixelSnapMatrix();
871}
872
Skuhneea7a7fb2015-08-28 07:10:31 -0700873void CanvasContext::prepareAndDraw(RenderNode* node) {
John Recke45b1fd2014-04-15 09:50:16 -0700874 ATRACE_CALL();
875
Matthew Bouyack7f667e72016-01-12 12:01:48 -0800876 nsecs_t vsync = mRenderThread.timeLord().computeFrameTimeNanos();
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700877 int64_t vsyncId = mRenderThread.timeLord().lastVsyncId();
Ady Abrahamdfb13982020-10-05 17:59:09 -0700878 int64_t frameDeadline = mRenderThread.timeLord().lastFrameDeadline();
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100879 int64_t frameInterval = mRenderThread.timeLord().frameIntervalNanos();
John Reckba6adf62015-02-19 14:36:50 -0800880 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700881 UiFrameInfoBuilder(frameInfo)
882 .addFlag(FrameInfoFlags::RTAnimation)
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100883 .setVsync(vsync, vsync, vsyncId, frameDeadline, frameInterval);
John Reckfe5e7b72014-05-23 17:42:28 -0700884
Chris Craike2e53a72015-10-28 15:55:40 -0700885 TreeInfo info(TreeInfo::MODE_RT_ONLY, *this);
Jerome Gaillarde218c692019-06-14 12:58:57 +0100886 prepareTree(info, frameInfo, systemTime(SYSTEM_TIME_MONOTONIC), node);
John Recka5dda642014-05-22 15:43:54 -0700887 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700888 draw();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700889 } else {
890 // wait on fences so tasks don't overlap next frame
891 waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700892 }
John Recke45b1fd2014-04-15 09:50:16 -0700893}
894
John Reck998a6d82014-08-28 15:35:53 -0700895void CanvasContext::markLayerInUse(RenderNode* node) {
John Reck51f2d602016-04-06 07:50:47 -0700896 if (mPrefetchedLayers.erase(node)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800897 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700898 }
899}
900
John Reck2de950d2017-01-25 10:58:30 -0800901void CanvasContext::freePrefetchedLayers() {
John Reck51f2d602016-04-06 07:50:47 -0700902 if (mPrefetchedLayers.size()) {
903 for (auto& node : mPrefetchedLayers) {
904 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...",
John Reck1bcacfd2017-11-03 10:12:19 -0700905 node->getName());
John Reck2de950d2017-01-25 10:58:30 -0800906 node->destroyLayers();
907 node->decStrong(nullptr);
John Reck51f2d602016-04-06 07:50:47 -0700908 }
909 mPrefetchedLayers.clear();
John Reck998a6d82014-08-28 15:35:53 -0700910 }
911}
912
John Reck2de950d2017-01-25 10:58:30 -0800913void CanvasContext::buildLayer(RenderNode* node) {
John Reck3e824952014-08-20 10:08:39 -0700914 ATRACE_CALL();
Stan Iliev768e3932016-07-08 21:34:52 -0400915 if (!mRenderPipeline->isContextReady()) return;
Chris Craik6246d2782016-03-29 15:01:41 -0700916
John Reck3e824952014-08-20 10:08:39 -0700917 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
918 stopDrawing();
919
Chris Craike2e53a72015-10-28 15:55:40 -0700920 TreeInfo info(TreeInfo::MODE_FULL, *this);
John Reck3e824952014-08-20 10:08:39 -0700921 info.damageAccumulator = &mDamageAccumulator;
Chris Craik0b7e8242015-10-28 16:50:44 -0700922 info.layerUpdateQueue = &mLayerUpdateQueue;
John Reck9eb9f6f2014-08-21 11:23:05 -0700923 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700924 node->prepareTree(info);
925 SkRect ignore;
926 mDamageAccumulator.finish(&ignore);
927 // Tickle the GENERIC property on node to mark it as dirty for damaging
928 // purposes when the frame is actually drawn
929 node->setPropertyFieldsDirty(RenderNode::GENERIC);
930
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700931 mRenderPipeline->renderLayers(mLightGeometry, &mLayerUpdateQueue, mOpaque, mLightInfo);
John Reck998a6d82014-08-28 15:35:53 -0700932
Chris Craikd41c4d82015-01-05 15:51:13 -0800933 node->incStrong(nullptr);
John Reck51f2d602016-04-06 07:50:47 -0700934 mPrefetchedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700935}
936
John Reck2de950d2017-01-25 10:58:30 -0800937void CanvasContext::destroyHardwareResources() {
John Reckf47a5942014-06-30 16:20:04 -0700938 stopDrawing();
Stan Iliev768e3932016-07-08 21:34:52 -0400939 if (mRenderPipeline->isContextReady()) {
John Reck2de950d2017-01-25 10:58:30 -0800940 freePrefetchedLayers();
Skuhneea7a7fb2015-08-28 07:10:31 -0700941 for (const sp<RenderNode>& node : mRenderNodes) {
John Reck2de950d2017-01-25 10:58:30 -0800942 node->destroyHardwareResources();
Skuhneea7a7fb2015-08-28 07:10:31 -0700943 }
Stan Iliev768e3932016-07-08 21:34:52 -0400944 mRenderPipeline->onDestroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700945 }
946}
947
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400948DeferredLayerUpdater* CanvasContext::createTextureLayer() {
Stan Iliev768e3932016-07-08 21:34:52 -0400949 return mRenderPipeline->createTextureLayer();
John Reck1949e792014-04-08 15:18:56 -0700950}
951
John Reckba6adf62015-02-19 14:36:50 -0800952void CanvasContext::dumpFrames(int fd) {
John Reck34781b22017-07-05 16:39:36 -0700953 mJankTracker.dumpStats(fd);
954 mJankTracker.dumpFrames(fd);
John Reckba6adf62015-02-19 14:36:50 -0800955}
956
957void CanvasContext::resetFrameStats() {
John Reck34781b22017-07-05 16:39:36 -0700958 mJankTracker.reset();
John Reckba6adf62015-02-19 14:36:50 -0800959}
960
John Reckdf1742e2017-01-19 15:56:21 -0800961void CanvasContext::setName(const std::string&& name) {
962 mJankTracker.setDescription(JankTrackerType::Window, std::move(name));
963}
964
John Reck38f6c032016-03-17 10:23:49 -0700965void CanvasContext::waitOnFences() {
966 if (mFrameFences.size()) {
967 ATRACE_CALL();
968 for (auto& fence : mFrameFences) {
John Reck322b8ab2019-03-14 13:15:28 -0700969 fence.get();
John Reck38f6c032016-03-17 10:23:49 -0700970 }
971 mFrameFences.clear();
972 }
973}
974
John Reck38f6c032016-03-17 10:23:49 -0700975void CanvasContext::enqueueFrameWork(std::function<void()>&& func) {
John Reck322b8ab2019-03-14 13:15:28 -0700976 mFrameFences.push_back(CommonPool::async(std::move(func)));
John Reck38f6c032016-03-17 10:23:49 -0700977}
978
Pablo Gamito35b80cd2021-08-24 11:03:51 +0200979uint64_t CanvasContext::getFrameNumber() {
Pablo Gamito7e6a5f22021-08-31 13:49:19 +0000980 // mFrameNumber is reset to 0 when the surface changes or we swap buffers
981 if (mFrameNumber == 0 && mNativeSurface.get()) {
Alec Mouri80c7ef12019-12-10 15:09:01 -0800982 mFrameNumber = ANativeWindow_getNextFrameId(mNativeSurface->getNativeWindow());
John Reck28912a52016-04-18 14:34:18 -0700983 }
984 return mFrameNumber;
985}
986
John Reck0b19a732019-03-07 17:18:25 -0800987bool CanvasContext::surfaceRequiresRedraw() {
988 if (!mNativeSurface) return false;
989 if (mHaveNewSurface) return true;
990
Alec Mourif023a322019-11-25 10:02:21 -0800991 ANativeWindow* anw = mNativeSurface->getNativeWindow();
992 const int width = ANativeWindow_getWidth(anw);
993 const int height = ANativeWindow_getHeight(anw);
John Reck0b19a732019-03-07 17:18:25 -0800994
John Reck41459192019-10-31 15:04:58 -0700995 return width != mLastFrameWidth || height != mLastFrameHeight;
John Reck0b19a732019-03-07 17:18:25 -0800996}
997
Stan Iliev768e3932016-07-08 21:34:52 -0400998SkRect CanvasContext::computeDirtyRect(const Frame& frame, SkRect* dirty) {
999 if (frame.width() != mLastFrameWidth || frame.height() != mLastFrameHeight) {
1000 // can't rely on prior content of window if viewport size changes
1001 dirty->setEmpty();
1002 mLastFrameWidth = frame.width();
1003 mLastFrameHeight = frame.height();
1004 } else if (mHaveNewSurface || frame.bufferAge() == 0) {
1005 // New surface needs a full draw
1006 dirty->setEmpty();
1007 } else {
Mike Reed39adc882019-08-22 11:53:05 -04001008 if (!dirty->isEmpty() && !dirty->intersect(SkRect::MakeIWH(frame.width(), frame.height()))) {
John Reck1bcacfd2017-11-03 10:12:19 -07001009 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?", SK_RECT_ARGS(*dirty),
1010 frame.width(), frame.height());
Stan Iliev768e3932016-07-08 21:34:52 -04001011 dirty->setEmpty();
1012 }
1013 profiler().unionDirty(dirty);
1014 }
1015
1016 if (dirty->isEmpty()) {
Mike Reed39adc882019-08-22 11:53:05 -04001017 dirty->setIWH(frame.width(), frame.height());
Stan Iliev768e3932016-07-08 21:34:52 -04001018 }
1019
1020 // At this point dirty is the area of the window to update. However,
1021 // the area of the frame we need to repaint is potentially different, so
1022 // stash the screen area for later
1023 SkRect windowDirty(*dirty);
1024
1025 // If the buffer age is 0 we do a full-screen repaint (handled above)
1026 // If the buffer age is 1 the buffer contents are the same as they were
1027 // last frame so there's nothing to union() against
1028 // Therefore we only care about the > 1 case.
1029 if (frame.bufferAge() > 1) {
John Reck1bcacfd2017-11-03 10:12:19 -07001030 if (frame.bufferAge() > (int)mSwapHistory.size()) {
Stan Iliev768e3932016-07-08 21:34:52 -04001031 // We don't have enough history to handle this old of a buffer
1032 // Just do a full-draw
Mike Reed39adc882019-08-22 11:53:05 -04001033 dirty->setIWH(frame.width(), frame.height());
Stan Iliev768e3932016-07-08 21:34:52 -04001034 } else {
1035 // At this point we haven't yet added the latest frame
1036 // to the damage history (happens below)
1037 // So we need to damage
1038 for (int i = mSwapHistory.size() - 1;
John Reck1bcacfd2017-11-03 10:12:19 -07001039 i > ((int)mSwapHistory.size()) - frame.bufferAge(); i--) {
Stan Iliev768e3932016-07-08 21:34:52 -04001040 dirty->join(mSwapHistory[i].damage);
1041 }
1042 }
1043 }
1044
1045 return windowDirty;
1046}
1047
Huihong Luo054b8d32021-02-24 18:48:12 -08001048CanvasContext* CanvasContext::getActiveContext() {
1049 return ScopedActiveContext::getActiveContext();
1050}
1051
1052bool CanvasContext::mergeTransaction(ASurfaceTransaction* transaction, ASurfaceControl* control) {
1053 if (!mASurfaceTransactionCallback) return false;
Huihong Luo4df41512021-06-24 10:04:32 -07001054 return std::invoke(mASurfaceTransactionCallback, reinterpret_cast<int64_t>(transaction),
1055 reinterpret_cast<int64_t>(control), getFrameNumber());
Huihong Luo054b8d32021-02-24 18:48:12 -08001056}
1057
Huihong Luo34f42fd2021-05-03 14:47:36 -07001058void CanvasContext::prepareSurfaceControlForWebview() {
1059 if (mPrepareSurfaceControlForWebviewCallback) {
1060 std::invoke(mPrepareSurfaceControlForWebviewCallback);
1061 }
1062}
1063
Matt Buckleye9023cf2022-11-23 22:39:25 +00001064void CanvasContext::sendLoadResetHint() {
1065 mHintSessionWrapper.sendLoadResetHint();
1066}
1067
Matt Buckleyac5f7552022-12-19 22:03:27 +00001068void CanvasContext::sendLoadIncreaseHint() {
1069 mHintSessionWrapper.sendLoadIncreaseHint();
1070}
1071
Matt Buckleye9023cf2022-11-23 22:39:25 +00001072void CanvasContext::setSyncDelayDuration(nsecs_t duration) {
1073 mSyncDelayDuration = duration;
1074}
1075
Matt Buckleyf5f90f12023-01-28 04:09:14 +00001076void CanvasContext::startHintSession() {
1077 mHintSessionWrapper.init();
1078}
1079
John Reckd13852e2023-05-09 14:33:53 -04001080bool CanvasContext::shouldDither() {
1081 CanvasContext* self = getActiveContext();
1082 if (!self) return false;
1083 return self->mColorMode != ColorMode::Default;
1084}
1085
John Reck23b797a2014-01-03 18:08:34 -08001086} /* namespace renderthread */
1087} /* namespace uirenderer */
1088} /* namespace android */