John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 17 | #pragma once |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 18 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 19 | #include <SkBitmap.h> |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 20 | #include <SkRect.h> |
Leon Scroggins III | 6c5864c0 | 2019-04-03 15:09:25 -0400 | [diff] [blame] | 21 | #include <SkSize.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include <cutils/compiler.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 23 | #include <utils/Functor.h> |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 24 | #include <utils/Mutex.h> |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 25 | |
John Reck | 38f6c03 | 2016-03-17 10:23:49 -0700 | [diff] [blame] | 26 | #include <functional> |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 27 | #include <future> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 28 | #include <set> |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 29 | #include <string> |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 30 | #include <utility> |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 31 | #include <vector> |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 32 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 33 | #include "ColorMode.h" |
| 34 | #include "DamageAccumulator.h" |
| 35 | #include "FrameInfo.h" |
| 36 | #include "FrameInfoVisualizer.h" |
| 37 | #include "FrameMetricsReporter.h" |
| 38 | #include "HintSessionWrapper.h" |
| 39 | #include "IContextFactory.h" |
| 40 | #include "IRenderPipeline.h" |
| 41 | #include "JankTracker.h" |
| 42 | #include "LayerUpdateQueue.h" |
| 43 | #include "Lighting.h" |
| 44 | #include "ReliableSurface.h" |
| 45 | #include "RenderNode.h" |
John Reck | 20a4d68 | 2023-07-11 17:11:51 -0400 | [diff] [blame] | 46 | #include "renderstate/RenderState.h" |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 47 | #include "renderthread/RenderTask.h" |
| 48 | #include "renderthread/RenderThread.h" |
Tyler Freeman | 417accd | 2023-10-31 02:01:06 +0000 | [diff] [blame] | 49 | #include "utils/ForceDark.h" |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 50 | #include "utils/RingBuffer.h" |
| 51 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 52 | namespace android { |
| 53 | namespace uirenderer { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 54 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 55 | class AnimationContext; |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 56 | class DeferredLayerUpdater; |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 57 | class ErrorHandler; |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 58 | class Layer; |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 59 | class Rect; |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 60 | class RenderState; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 61 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 62 | namespace renderthread { |
| 63 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 64 | class Frame; |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 65 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 66 | // This per-renderer class manages the bridge between the global EGL context |
| 67 | // and the render surface. |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 68 | // TODO: Rename to Renderer or some other per-window, top-level manager |
John Reck | 20a4d68 | 2023-07-11 17:11:51 -0400 | [diff] [blame] | 69 | class CanvasContext : public IFrameCallback, public IGpuContextCallback { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 70 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 71 | static CanvasContext* create(RenderThread& thread, bool translucent, RenderNode* rootRenderNode, |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 72 | IContextFactory* contextFactory, pid_t uiThreadId, |
| 73 | pid_t renderThreadId); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 74 | virtual ~CanvasContext(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 75 | |
Derek Sollenberger | 6a21ca5 | 2016-09-28 13:39:55 -0400 | [diff] [blame] | 76 | /** |
| 77 | * Update or create a layer specific for the provided RenderNode. The layer |
| 78 | * attached to the node will be specific to the RenderPipeline used by this |
| 79 | * context |
| 80 | * |
| 81 | * @return true if the layer has been created or updated |
| 82 | */ |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 83 | bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& dmgAccumulator, |
| 84 | ErrorHandler* errorHandler) { |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 85 | return mRenderPipeline->createOrUpdateLayer(node, dmgAccumulator, errorHandler); |
Derek Sollenberger | 6a21ca5 | 2016-09-28 13:39:55 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 89 | * Pin any mutable images to the GPU cache. A pinned images is guaranteed to |
| 90 | * remain in the cache until it has been unpinned. We leverage this feature |
| 91 | * to avoid making a CPU copy of the pixels. |
| 92 | * |
Derek Sollenberger | 189e874 | 2016-11-16 16:00:17 -0500 | [diff] [blame] | 93 | * @return true if all images have been successfully pinned to the GPU cache |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 94 | * and false otherwise (e.g. cache limits have been exceeded). |
| 95 | */ |
| 96 | bool pinImages(std::vector<SkImage*>& mutableImages) { |
Brett Chabot | 856db9e | 2021-05-21 08:41:58 -0700 | [diff] [blame] | 97 | if (!Properties::isDrawingEnabled()) { |
| 98 | return true; |
| 99 | } |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 100 | return mRenderPipeline->pinImages(mutableImages); |
| 101 | } |
Brett Chabot | 856db9e | 2021-05-21 08:41:58 -0700 | [diff] [blame] | 102 | bool pinImages(LsaVector<sk_sp<Bitmap>>& images) { |
| 103 | if (!Properties::isDrawingEnabled()) { |
| 104 | return true; |
| 105 | } |
| 106 | return mRenderPipeline->pinImages(images); |
| 107 | } |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Unpin any image that had be previously pinned to the GPU cache |
| 111 | */ |
| 112 | void unpinImages() { mRenderPipeline->unpinImages(); } |
| 113 | |
Derek Sollenberger | daf7229 | 2016-10-25 12:09:18 -0400 | [diff] [blame] | 114 | static void invokeFunctor(const RenderThread& thread, Functor* functor); |
| 115 | |
| 116 | static void prepareToDraw(const RenderThread& thread, Bitmap* bitmap); |
| 117 | |
Derek Sollenberger | 0df6209 | 2016-09-27 16:04:42 -0400 | [diff] [blame] | 118 | /* |
| 119 | * If Properties::isSkiaEnabled() is true then this will return the Skia |
| 120 | * grContext associated with the current RenderPipeline. |
| 121 | */ |
Adlai Holler | ab76215 | 2020-09-16 10:37:19 -0400 | [diff] [blame] | 122 | GrDirectContext* getGrContext() const { return mRenderThread.getGrContext(); } |
Derek Sollenberger | 0df6209 | 2016-09-27 16:04:42 -0400 | [diff] [blame] | 123 | |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 124 | ASurfaceControl* getSurfaceControl() const { return mSurfaceControl; } |
Huihong Luo | 540fdf8 | 2021-06-25 13:59:39 -0700 | [diff] [blame] | 125 | int32_t getSurfaceControlGenerationId() const { return mSurfaceControlGenerationId; } |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 126 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 127 | // Won't take effect until next EGLSurface creation |
| 128 | void setSwapBehavior(SwapBehavior swapBehavior); |
| 129 | |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 130 | void setHardwareBuffer(AHardwareBuffer* buffer); |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 131 | void setSurface(ANativeWindow* window, bool enableTimeout = true); |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 132 | void setSurfaceControl(ASurfaceControl* surfaceControl); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 133 | bool pauseSurface(); |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 134 | void setStopped(bool stopped); |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 135 | bool isStopped() { return mStopped || !hasOutputTarget(); } |
| 136 | bool hasOutputTarget() const { return mNativeSurface.get() || mHardwareBuffer; } |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 137 | void allocateBuffers(); |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 138 | |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 139 | void setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha); |
| 140 | void setLightGeometry(const Vector3& lightCenter, float lightRadius); |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 141 | void setOpaque(bool opaque); |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 142 | float setColorMode(ColorMode mode); |
| 143 | float targetSdrHdrRatio() const; |
| 144 | void setTargetSdrHdrRatio(float ratio); |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 145 | bool makeCurrent(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 146 | void prepareTree(TreeInfo& info, int64_t* uiFrameInfo, int64_t syncQueued, RenderNode* target); |
Bo Liu | 4d0f1f1 | 2021-05-06 16:49:40 -0400 | [diff] [blame] | 147 | // Returns the DequeueBufferDuration. |
Ady Abraham | cab4afe | 2023-05-09 11:25:22 -0700 | [diff] [blame] | 148 | void draw(bool solelyTextureViewUpdates); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 149 | void destroy(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 150 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 151 | // IFrameCallback, Choreographer-driven frame callback entry point |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 152 | virtual void doFrame() override; |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 153 | void prepareAndDraw(RenderNode* node); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 154 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 155 | void buildLayer(RenderNode* node); |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 156 | void markLayerInUse(RenderNode* node); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 157 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 158 | void destroyHardwareResources(); |
John Reck | 20a4d68 | 2023-07-11 17:11:51 -0400 | [diff] [blame] | 159 | void onContextDestroyed() override; |
John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 160 | |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 161 | DeferredLayerUpdater* createTextureLayer(); |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 162 | |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 163 | void stopDrawing(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 164 | void notifyFramePending(); |
| 165 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 166 | FrameInfoVisualizer& profiler() { return mProfiler; } |
Jiang Tian | e0edc3a | 2024-01-09 11:52:05 +0800 | [diff] [blame] | 167 | std::mutex& profilerLock() { return mFrameInfoMutex; } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 168 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 169 | void dumpFrames(int fd); |
| 170 | void resetFrameStats(); |
| 171 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 172 | void setName(const std::string&& name); |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 173 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 174 | void addRenderNode(RenderNode* node, bool placeFront); |
| 175 | void removeRenderNode(RenderNode* node); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 176 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 177 | void setContentDrawBounds(const Rect& bounds) { mContentDrawBounds = bounds; } |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 178 | |
Pablo Gamito | 88660d7 | 2021-08-09 14:37:56 +0000 | [diff] [blame] | 179 | void addFrameMetricsObserver(FrameMetricsObserver* observer); |
| 180 | void removeFrameMetricsObserver(FrameMetricsObserver* observer); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 181 | |
John Reck | 38f6c03 | 2016-03-17 10:23:49 -0700 | [diff] [blame] | 182 | // Used to queue up work that needs to be completed before this frame completes |
Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 183 | void enqueueFrameWork(std::function<void()>&& func); |
John Reck | 38f6c03 | 2016-03-17 10:23:49 -0700 | [diff] [blame] | 184 | |
Pablo Gamito | 35b80cd | 2021-08-24 11:03:51 +0200 | [diff] [blame] | 185 | uint64_t getFrameNumber(); |
John Reck | 28912a5 | 2016-04-18 14:34:18 -0700 | [diff] [blame] | 186 | |
Chris Craik | 06e2e9c | 2016-08-31 17:32:46 -0700 | [diff] [blame] | 187 | void waitOnFences(); |
| 188 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 189 | IRenderPipeline* getRenderPipeline() { return mRenderPipeline.get(); } |
| 190 | |
chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 191 | void addFrameCommitListener(std::function<void(bool)>&& func) { |
| 192 | mFrameCommitCallbacks.push_back(std::move(func)); |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 193 | } |
| 194 | |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 195 | void setPictureCapturedCallback(const std::function<void(sk_sp<SkPicture>&&)>& callback) { |
| 196 | mRenderPipeline->setPictureCapturedCallback(callback); |
| 197 | } |
| 198 | |
Tyler Freeman | 417accd | 2023-10-31 02:01:06 +0000 | [diff] [blame] | 199 | void setForceDark(ForceDarkType type) { mForceDarkType = type; } |
John Reck | bb3a358 | 2018-09-26 11:21:08 -0700 | [diff] [blame] | 200 | |
Tyler Freeman | 417accd | 2023-10-31 02:01:06 +0000 | [diff] [blame] | 201 | ForceDarkType getForceDarkType() { return mForceDarkType; } |
John Reck | bb3a358 | 2018-09-26 11:21:08 -0700 | [diff] [blame] | 202 | |
Leon Scroggins III | 6c5864c0 | 2019-04-03 15:09:25 -0400 | [diff] [blame] | 203 | SkISize getNextFrameSize() const; |
| 204 | |
John Reck | ee18427 | 2023-03-21 12:29:21 -0400 | [diff] [blame] | 205 | // Returns the matrix to use to nudge non-AA'd points/lines towards the fragment center |
| 206 | const SkM44& getPixelSnapMatrix() const; |
| 207 | |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 208 | // Called when SurfaceStats are available. |
Pablo Gamito | 14b28ce9c | 2021-09-06 16:33:23 +0000 | [diff] [blame] | 209 | static void onSurfaceStatsAvailable(void* context, int32_t surfaceControlId, |
| 210 | ASurfaceControlStats* stats); |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 211 | |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 212 | void setASurfaceTransactionCallback( |
Huihong Luo | 4df4151 | 2021-06-24 10:04:32 -0700 | [diff] [blame] | 213 | const std::function<bool(int64_t, int64_t, int64_t)>& callback) { |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 214 | mASurfaceTransactionCallback = callback; |
| 215 | } |
| 216 | |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 217 | void setHardwareBufferRenderParams(const HardwareBufferRenderParams& params) { |
| 218 | mBufferParams = params; |
| 219 | } |
| 220 | |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 221 | bool mergeTransaction(ASurfaceTransaction* transaction, ASurfaceControl* control); |
| 222 | |
Huihong Luo | 34f42fd | 2021-05-03 14:47:36 -0700 | [diff] [blame] | 223 | void setPrepareSurfaceControlForWebviewCallback(const std::function<void()>& callback) { |
| 224 | mPrepareSurfaceControlForWebviewCallback = callback; |
| 225 | } |
| 226 | |
| 227 | void prepareSurfaceControlForWebview(); |
| 228 | |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 229 | static CanvasContext* getActiveContext(); |
| 230 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 231 | void sendLoadResetHint(); |
| 232 | |
Matt Buckley | ac5f755 | 2022-12-19 22:03:27 +0000 | [diff] [blame] | 233 | void sendLoadIncreaseHint(); |
| 234 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 235 | void setSyncDelayDuration(nsecs_t duration); |
| 236 | |
Matt Buckley | f5f90f1 | 2023-01-28 04:09:14 +0000 | [diff] [blame] | 237 | void startHintSession(); |
| 238 | |
John Reck | d13852e | 2023-05-09 14:33:53 -0400 | [diff] [blame] | 239 | static bool shouldDither(); |
| 240 | |
John Reck | 07f3d91 | 2023-08-17 12:46:44 -0400 | [diff] [blame] | 241 | void visitAllRenderNodes(std::function<void(const RenderNode&)>) const; |
| 242 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 243 | private: |
Stan Iliev | 03de074 | 2016-07-07 12:35:54 -0400 | [diff] [blame] | 244 | CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode, |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 245 | IContextFactory* contextFactory, std::unique_ptr<IRenderPipeline> renderPipeline, |
| 246 | pid_t uiThreadId, pid_t renderThreadId); |
Stan Iliev | 03de074 | 2016-07-07 12:35:54 -0400 | [diff] [blame] | 247 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 248 | friend class RegisterFrameCallbackTask; |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 249 | // TODO: Replace with something better for layer & other GL object |
| 250 | // lifecycle tracking |
| 251 | friend class android::uirenderer::RenderState; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 252 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 253 | void freePrefetchedLayers(); |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 254 | |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 255 | bool isSwapChainStuffed(); |
John Reck | 0b19a73 | 2019-03-07 17:18:25 -0800 | [diff] [blame] | 256 | bool surfaceRequiresRedraw(); |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 257 | void setupPipelineSurface(); |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 258 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 259 | SkRect computeDirtyRect(const Frame& frame, SkRect* dirty); |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 260 | void finishFrame(FrameInfo* frameInfo); |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 261 | |
Siarhei Vishniakou | f0cf18d | 2021-02-26 00:15:04 +0000 | [diff] [blame] | 262 | /** |
Tang Lee | 6ba5125 | 2024-08-13 15:06:28 +0800 | [diff] [blame] | 263 | * Invoke 'reportFrameMetrics' on the last frame stored in 'mLastFrameInfos'. |
Siarhei Vishniakou | f0cf18d | 2021-02-26 00:15:04 +0000 | [diff] [blame] | 264 | * Populate the 'presentTime' field before calling. |
| 265 | */ |
| 266 | void reportMetricsWithPresentTime(); |
| 267 | |
Pablo Gamito | 88660d7 | 2021-08-09 14:37:56 +0000 | [diff] [blame] | 268 | struct FrameMetricsInfo { |
| 269 | FrameInfo* frameInfo; |
| 270 | int64_t frameNumber; |
| 271 | int32_t surfaceId; |
| 272 | }; |
| 273 | |
Tang Lee | 6ba5125 | 2024-08-13 15:06:28 +0800 | [diff] [blame] | 274 | FrameInfo* getFrameInfoFromLastFew(uint64_t frameNumber, uint32_t surfaceControlId); |
Siarhei Vishniakou | 07d35cb | 2021-07-03 02:22:12 +0000 | [diff] [blame] | 275 | |
Nader Jawad | 2461722 | 2023-03-30 12:29:01 -0700 | [diff] [blame] | 276 | Frame getFrame(); |
| 277 | |
Fedor Kudasov | 52abc90 | 2019-06-26 16:59:16 +0100 | [diff] [blame] | 278 | // The same type as Frame.mWidth and Frame.mHeight |
| 279 | int32_t mLastFrameWidth = 0; |
| 280 | int32_t mLastFrameHeight = 0; |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 281 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 282 | RenderThread& mRenderThread; |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 283 | |
| 284 | AHardwareBuffer* mHardwareBuffer = nullptr; |
| 285 | HardwareBufferRenderParams mBufferParams; |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 286 | std::unique_ptr<ReliableSurface> mNativeSurface; |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 287 | // The SurfaceControl reference is passed from ViewRootImpl, can be set to |
| 288 | // NULL to remove the reference |
| 289 | ASurfaceControl* mSurfaceControl = nullptr; |
Huihong Luo | 540fdf8 | 2021-06-25 13:59:39 -0700 | [diff] [blame] | 290 | // id to track surface control changes and WebViewFunctor uses it to determine |
Pablo Gamito | 88660d7 | 2021-08-09 14:37:56 +0000 | [diff] [blame] | 291 | // whether reparenting is needed also used by FrameMetricsReporter to determine |
| 292 | // if a frame is from an "old" surface (i.e. one that existed before the |
| 293 | // observer was attched) and therefore shouldn't be reported. |
Pablo Gamito | bc9e529 | 2021-08-23 17:12:29 +0200 | [diff] [blame] | 294 | // NOTE: It is important that this is an increasing counter. |
Huihong Luo | 540fdf8 | 2021-06-25 13:59:39 -0700 | [diff] [blame] | 295 | int32_t mSurfaceControlGenerationId = 0; |
John Reck | 306f331 | 2016-06-10 16:01:55 -0700 | [diff] [blame] | 296 | // stopped indicates the CanvasContext will reject actual redraw operations, |
| 297 | // and defer repaint until it is un-stopped |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 298 | bool mStopped = false; |
Leon Scroggins III | 4afdd1c | 2018-05-14 14:59:30 -0400 | [diff] [blame] | 299 | // Incremented each time the CanvasContext is stopped. Used to ignore |
| 300 | // delayed messages that are triggered after stopping. |
| 301 | int mGenerationID; |
John Reck | 306f331 | 2016-06-10 16:01:55 -0700 | [diff] [blame] | 302 | // CanvasContext is dirty if it has received an update that it has not |
| 303 | // painted onto its surface. |
| 304 | bool mIsDirty = false; |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 305 | SwapBehavior mSwapBehavior = SwapBehavior::kSwap_default; |
John Reck | e486d93 | 2015-10-28 09:21:19 -0700 | [diff] [blame] | 306 | struct SwapHistory { |
| 307 | SkRect damage; |
| 308 | nsecs_t vsyncTime; |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 309 | nsecs_t swapCompletedTime; |
| 310 | nsecs_t dequeueDuration; |
| 311 | nsecs_t queueDuration; |
John Reck | e486d93 | 2015-10-28 09:21:19 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
Jiang Tian | cd8994f | 2021-12-03 18:18:16 +0800 | [diff] [blame] | 314 | // Need at least 4 because we do quad buffer. Add a few more for good measure. |
| 315 | RingBuffer<SwapHistory, 7> mSwapHistory; |
Pablo Gamito | 7e6a5f2 | 2021-08-31 13:49:19 +0000 | [diff] [blame] | 316 | // Frame numbers start at 1, 0 means uninitialized |
| 317 | uint64_t mFrameNumber = 0; |
John Reck | 4145919 | 2019-10-31 15:04:58 -0700 | [diff] [blame] | 318 | int64_t mDamageId = 0; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 319 | |
Chris Craik | 3163568 | 2016-07-19 17:59:12 -0700 | [diff] [blame] | 320 | // last vsync for a dropped frame due to stuffed queue |
| 321 | nsecs_t mLastDropVsync = 0; |
| 322 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 323 | bool mOpaque; |
Tyler Freeman | 417accd | 2023-10-31 02:01:06 +0000 | [diff] [blame] | 324 | ForceDarkType mForceDarkType = ForceDarkType::NONE; |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 325 | LightInfo mLightInfo; |
| 326 | LightGeometry mLightGeometry = {{0, 0, 0}, 0}; |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 327 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 328 | bool mHaveNewSurface = false; |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 329 | DamageAccumulator mDamageAccumulator; |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 330 | LayerUpdateQueue mLayerUpdateQueue; |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 331 | std::unique_ptr<AnimationContext> mAnimationContext; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 332 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 333 | std::vector<sp<RenderNode>> mRenderNodes; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 334 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 335 | FrameInfo* mCurrentFrameInfo = nullptr; |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 336 | |
Pablo Gamito | 88660d7 | 2021-08-09 14:37:56 +0000 | [diff] [blame] | 337 | // List of data of frames that are awaiting GPU completion reporting. Used to compute frame |
| 338 | // metrics and determine whether or not to report the metrics. |
Tang Lee | 6ba5125 | 2024-08-13 15:06:28 +0800 | [diff] [blame] | 339 | RingBuffer<FrameMetricsInfo, 6> mLastFrameMetricsInfos |
| 340 | GUARDED_BY(mLastFrameMetricsInfosMutex); |
| 341 | std::mutex mLastFrameMetricsInfosMutex; |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 342 | |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 343 | std::string mName; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 344 | JankTracker mJankTracker; |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 345 | FrameInfoVisualizer mProfiler; |
Jiang Tian | e0edc3a | 2024-01-09 11:52:05 +0800 | [diff] [blame] | 346 | std::unique_ptr<FrameMetricsReporter> mFrameMetricsReporter GUARDED_BY(mFrameInfoMutex); |
| 347 | std::mutex mFrameInfoMutex; |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 348 | |
John Reck | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 349 | std::set<RenderNode*> mPrefetchedLayers; |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 350 | |
| 351 | // Stores the bounds of the main content. |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 352 | Rect mContentDrawBounds; |
John Reck | 38f6c03 | 2016-03-17 10:23:49 -0700 | [diff] [blame] | 353 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 354 | std::vector<std::future<void>> mFrameFences; |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 355 | std::unique_ptr<IRenderPipeline> mRenderPipeline; |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 356 | |
chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 357 | std::vector<std::function<void(bool)>> mFrameCommitCallbacks; |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 358 | |
| 359 | // If set to true, we expect that callbacks into onSurfaceStatsAvailable |
| 360 | bool mExpectSurfaceStats = false; |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 361 | |
Huihong Luo | 4df4151 | 2021-06-24 10:04:32 -0700 | [diff] [blame] | 362 | std::function<bool(int64_t, int64_t, int64_t)> mASurfaceTransactionCallback; |
Huihong Luo | 34f42fd | 2021-05-03 14:47:36 -0700 | [diff] [blame] | 363 | std::function<void()> mPrepareSurfaceControlForWebviewCallback; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 364 | |
Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame] | 365 | std::shared_ptr<HintSessionWrapper> mHintSessionWrapper; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 366 | nsecs_t mLastDequeueBufferDuration = 0; |
| 367 | nsecs_t mSyncDelayDuration = 0; |
| 368 | nsecs_t mIdleDuration = 0; |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 369 | |
| 370 | ColorMode mColorMode = ColorMode::Default; |
| 371 | float mTargetSdrHdrRatio = 1.f; |
Ady Abraham | a4a0c7a | 2023-06-07 10:34:32 -0700 | [diff] [blame] | 372 | |
| 373 | struct SkippedFrameInfo { |
| 374 | int64_t vsyncId; |
| 375 | int64_t startTime; |
| 376 | }; |
| 377 | std::optional<SkippedFrameInfo> mSkippedFrameInfo; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | } /* namespace renderthread */ |
| 381 | } /* namespace uirenderer */ |
| 382 | } /* namespace android */ |