blob: af1ebb288f2142ee87cc9454e9ec2768e762d5ca [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
Chris Craik5e00c7c2016-07-06 16:10:09 -070017#pragma once
John Reck23b797a2014-01-03 18:08:34 -080018
John Reckba6adf62015-02-19 14:36:50 -080019#include "DamageAccumulator.h"
John Reckba6adf62015-02-19 14:36:50 -080020#include "FrameInfo.h"
John Reck4c9e59d2015-05-12 07:17:50 -070021#include "FrameInfoVisualizer.h"
Andres Morales910beb82016-02-02 16:19:40 -080022#include "FrameMetricsReporter.h"
Chris Craik0b7e8242015-10-28 16:50:44 -070023#include "IContextFactory.h"
Stan Iliev768e3932016-07-08 21:34:52 -040024#include "IRenderPipeline.h"
Fedor Kudasov85d6f102019-07-31 15:58:16 +010025#include "JankTracker.h"
Chris Craik0b7e8242015-10-28 16:50:44 -070026#include "LayerUpdateQueue.h"
John Reck0fa0cbc2019-04-05 16:57:46 -070027#include "Lighting.h"
John Reck848f6512018-12-03 13:26:43 -080028#include "ReliableSurface.h"
John Reck0fa0cbc2019-04-05 16:57:46 -070029#include "RenderNode.h"
John Reckba6adf62015-02-19 14:36:50 -080030#include "renderthread/RenderTask.h"
31#include "renderthread/RenderThread.h"
Stan Iliev7203e1f2019-07-25 13:12:02 -040032#include "utils/RingBuffer.h"
John Reckb36bfdd2020-07-23 13:47:49 -070033#include "ColorMode.h"
John Reck998a6d82014-08-28 15:35:53 -070034
John Reck19b6bcf2014-02-14 20:03:38 -080035#include <SkBitmap.h>
John Reckd04794a2015-05-08 10:04:36 -070036#include <SkRect.h>
Leon Scroggins III6c5864c2019-04-03 15:09:25 -040037#include <SkSize.h>
John Reck1bcacfd2017-11-03 10:12:19 -070038#include <cutils/compiler.h>
John Reck1bcacfd2017-11-03 10:12:19 -070039#include <utils/Functor.h>
Jorim Jaggi71db8892021-02-03 23:19:29 +010040#include <utils/Mutex.h>
John Reck4f02bf42014-01-03 18:09:17 -080041
John Reck38f6c032016-03-17 10:23:49 -070042#include <functional>
John Reck0fa0cbc2019-04-05 16:57:46 -070043#include <future>
John Reckba6adf62015-02-19 14:36:50 -080044#include <set>
John Reckb36016c2015-03-11 08:50:53 -070045#include <string>
Stan Iliev7203e1f2019-07-25 13:12:02 -040046#include <utility>
Skuhneea7a7fb2015-08-28 07:10:31 -070047#include <vector>
John Reck23b797a2014-01-03 18:08:34 -080048
49namespace android {
50namespace uirenderer {
John Reck4f02bf42014-01-03 18:09:17 -080051
John Reck119907c2014-08-14 09:02:01 -070052class AnimationContext;
John Reck19b6bcf2014-02-14 20:03:38 -080053class DeferredLayerUpdater;
Stan Iliev216b1572018-03-26 14:29:50 -040054class ErrorHandler;
John Reck1949e792014-04-08 15:18:56 -070055class Layer;
Chris Craik5e00c7c2016-07-06 16:10:09 -070056class Rect;
John Reck443a7142014-09-04 17:40:05 -070057class RenderState;
John Reck4f02bf42014-01-03 18:09:17 -080058
John Reck23b797a2014-01-03 18:08:34 -080059namespace renderthread {
60
Stan Iliev768e3932016-07-08 21:34:52 -040061class Frame;
John Reck1125d1f2014-10-23 11:02:19 -070062
John Reck23b797a2014-01-03 18:08:34 -080063// This per-renderer class manages the bridge between the global EGL context
64// and the render surface.
John Reck119907c2014-08-14 09:02:01 -070065// TODO: Rename to Renderer or some other per-window, top-level manager
John Recke45b1fd2014-04-15 09:50:16 -070066class CanvasContext : public IFrameCallback {
John Reck23b797a2014-01-03 18:08:34 -080067public:
John Reck1bcacfd2017-11-03 10:12:19 -070068 static CanvasContext* create(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
69 IContextFactory* contextFactory);
John Recke45b1fd2014-04-15 09:50:16 -070070 virtual ~CanvasContext();
John Reck23b797a2014-01-03 18:08:34 -080071
Derek Sollenberger6a21ca52016-09-28 13:39:55 -040072 /**
73 * Update or create a layer specific for the provided RenderNode. The layer
74 * attached to the node will be specific to the RenderPipeline used by this
75 * context
76 *
77 * @return true if the layer has been created or updated
78 */
Stan Iliev216b1572018-03-26 14:29:50 -040079 bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& dmgAccumulator,
80 ErrorHandler* errorHandler) {
Peiyong Lin1f6aa122018-09-10 16:28:08 -070081 return mRenderPipeline->createOrUpdateLayer(node, dmgAccumulator, errorHandler);
Derek Sollenberger6a21ca52016-09-28 13:39:55 -040082 }
83
84 /**
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040085 * Pin any mutable images to the GPU cache. A pinned images is guaranteed to
86 * remain in the cache until it has been unpinned. We leverage this feature
87 * to avoid making a CPU copy of the pixels.
88 *
Derek Sollenberger189e8742016-11-16 16:00:17 -050089 * @return true if all images have been successfully pinned to the GPU cache
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040090 * and false otherwise (e.g. cache limits have been exceeded).
91 */
92 bool pinImages(std::vector<SkImage*>& mutableImages) {
93 return mRenderPipeline->pinImages(mutableImages);
94 }
John Reck1bcacfd2017-11-03 10:12:19 -070095 bool pinImages(LsaVector<sk_sp<Bitmap>>& images) { return mRenderPipeline->pinImages(images); }
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040096
97 /**
98 * Unpin any image that had be previously pinned to the GPU cache
99 */
100 void unpinImages() { mRenderPipeline->unpinImages(); }
101
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400102 static void invokeFunctor(const RenderThread& thread, Functor* functor);
103
104 static void prepareToDraw(const RenderThread& thread, Bitmap* bitmap);
105
Derek Sollenberger0df62092016-09-27 16:04:42 -0400106 /*
107 * If Properties::isSkiaEnabled() is true then this will return the Skia
108 * grContext associated with the current RenderPipeline.
109 */
Adlai Hollerab762152020-09-16 10:37:19 -0400110 GrDirectContext* getGrContext() const { return mRenderThread.getGrContext(); }
Derek Sollenberger0df62092016-09-27 16:04:42 -0400111
Huihong Luo054b8d32021-02-24 18:48:12 -0800112 ASurfaceControl* getSurfaceControl() const { return mSurfaceControl; }
113
John Reck1125d1f2014-10-23 11:02:19 -0700114 // Won't take effect until next EGLSurface creation
115 void setSwapBehavior(SwapBehavior swapBehavior);
116
Alec Mouri43fe6fc2019-12-23 07:46:19 -0800117 void setSurface(ANativeWindow* window, bool enableTimeout = true);
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800118 void setSurfaceControl(ASurfaceControl* surfaceControl);
John Reckf8441e62017-10-23 13:10:41 -0700119 bool pauseSurface();
John Reck8afcc762016-04-13 10:24:06 -0700120 void setStopped(bool stopped);
Leon Scroggins III6c5864c2019-04-03 15:09:25 -0400121 bool hasSurface() const { return mNativeSurface.get(); }
John Reck8785ceb2018-10-29 16:45:58 -0700122 void allocateBuffers();
John Reckaa95a882014-11-07 11:02:07 -0800123
John Reck8785ceb2018-10-29 16:45:58 -0700124 void setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
125 void setLightGeometry(const Vector3& lightCenter, float lightRadius);
John Reck63a06672014-05-07 13:45:54 -0700126 void setOpaque(bool opaque);
John Reckb36bfdd2020-07-23 13:47:49 -0700127 void setColorMode(ColorMode mode);
John Reck8afcc762016-04-13 10:24:06 -0700128 bool makeCurrent();
John Reck1bcacfd2017-11-03 10:12:19 -0700129 void prepareTree(TreeInfo& info, int64_t* uiFrameInfo, int64_t syncQueued, RenderNode* target);
John Recke4267ea2014-06-03 15:53:15 -0700130 void draw();
John Reck2de950d2017-01-25 10:58:30 -0800131 void destroy();
John Reck23b797a2014-01-03 18:08:34 -0800132
Chris Craik0b7e8242015-10-28 16:50:44 -0700133 // IFrameCallback, Choreographer-driven frame callback entry point
Chris Craikd41c4d82015-01-05 15:51:13 -0800134 virtual void doFrame() override;
Skuhneea7a7fb2015-08-28 07:10:31 -0700135 void prepareAndDraw(RenderNode* node);
John Recke45b1fd2014-04-15 09:50:16 -0700136
John Reck2de950d2017-01-25 10:58:30 -0800137 void buildLayer(RenderNode* node);
John Reck998a6d82014-08-28 15:35:53 -0700138 void markLayerInUse(RenderNode* node);
John Reck19b6bcf2014-02-14 20:03:38 -0800139
John Reck2de950d2017-01-25 10:58:30 -0800140 void destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700141 static void trimMemory(RenderThread& thread, int level);
John Recke1628b72014-05-23 15:11:19 -0700142
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400143 DeferredLayerUpdater* createTextureLayer();
John Reck1949e792014-04-08 15:18:56 -0700144
John Reckf47a5942014-06-30 16:20:04 -0700145 void stopDrawing();
John Recka5dda642014-05-22 15:43:54 -0700146 void notifyFramePending();
147
John Reck4c9e59d2015-05-12 07:17:50 -0700148 FrameInfoVisualizer& profiler() { return mProfiler; }
John Reckfe5e7b72014-05-23 17:42:28 -0700149
John Reckba6adf62015-02-19 14:36:50 -0800150 void dumpFrames(int fd);
151 void resetFrameStats();
152
John Reckdf1742e2017-01-19 15:56:21 -0800153 void setName(const std::string&& name);
John Reckb36016c2015-03-11 08:50:53 -0700154
John Reck2de950d2017-01-25 10:58:30 -0800155 void addRenderNode(RenderNode* node, bool placeFront);
156 void removeRenderNode(RenderNode* node);
Skuhneea7a7fb2015-08-28 07:10:31 -0700157
John Reck1bcacfd2017-11-03 10:12:19 -0700158 void setContentDrawBounds(const Rect& bounds) { mContentDrawBounds = bounds; }
Skuhneea7a7fb2015-08-28 07:10:31 -0700159
Andres Morales910beb82016-02-02 16:19:40 -0800160 void addFrameMetricsObserver(FrameMetricsObserver* observer) {
161 if (mFrameMetricsReporter.get() == nullptr) {
162 mFrameMetricsReporter.reset(new FrameMetricsReporter());
Andres Morales06f5bc72015-12-15 15:21:31 -0800163 }
164
Andres Morales910beb82016-02-02 16:19:40 -0800165 mFrameMetricsReporter->addObserver(observer);
Andres Morales06f5bc72015-12-15 15:21:31 -0800166 }
167
Andres Morales910beb82016-02-02 16:19:40 -0800168 void removeFrameMetricsObserver(FrameMetricsObserver* observer) {
169 if (mFrameMetricsReporter.get() != nullptr) {
170 mFrameMetricsReporter->removeObserver(observer);
171 if (!mFrameMetricsReporter->hasObservers()) {
172 mFrameMetricsReporter.reset(nullptr);
Andres Morales06f5bc72015-12-15 15:21:31 -0800173 }
174 }
175 }
176
John Reck38f6c032016-03-17 10:23:49 -0700177 // Used to queue up work that needs to be completed before this frame completes
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -0500178 void enqueueFrameWork(std::function<void()>&& func);
John Reck38f6c032016-03-17 10:23:49 -0700179
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -0500180 int64_t getFrameNumber();
John Reck28912a52016-04-18 14:34:18 -0700181
Chris Craik06e2e9c2016-08-31 17:32:46 -0700182 void waitOnFences();
183
Stan Iliev23c38a92017-03-23 00:12:50 -0400184 IRenderPipeline* getRenderPipeline() { return mRenderPipeline.get(); }
185
John Reckcc2eee82018-05-17 10:44:00 -0700186 void addFrameCompleteListener(std::function<void(int64_t)>&& func) {
187 mFrameCompleteCallbacks.push_back(std::move(func));
188 }
189
John Reck5cca8f22018-12-10 17:06:22 -0800190 void setPictureCapturedCallback(const std::function<void(sk_sp<SkPicture>&&)>& callback) {
191 mRenderPipeline->setPictureCapturedCallback(callback);
192 }
193
John Reck0fa0cbc2019-04-05 16:57:46 -0700194 void setForceDark(bool enable) { mUseForceDark = enable; }
John Reckbb3a3582018-09-26 11:21:08 -0700195
196 bool useForceDark() {
John Reckbb3a3582018-09-26 11:21:08 -0700197 return mUseForceDark;
198 }
199
Leon Scroggins III6c5864c2019-04-03 15:09:25 -0400200 SkISize getNextFrameSize() const;
201
Jorim Jaggi71db8892021-02-03 23:19:29 +0100202 // Called when SurfaceStats are available.
203 static void onSurfaceStatsAvailable(void* context, ASurfaceControl* control,
204 ASurfaceControlStats* stats);
205
Huihong Luo054b8d32021-02-24 18:48:12 -0800206 void setASurfaceTransactionCallback(
207 const std::function<void(int64_t, int64_t, int64_t)>& callback) {
208 mASurfaceTransactionCallback = callback;
209 }
210
211 bool mergeTransaction(ASurfaceTransaction* transaction, ASurfaceControl* control);
212
213 static CanvasContext* getActiveContext();
214
John Reck23b797a2014-01-03 18:08:34 -0800215private:
Stan Iliev03de0742016-07-07 12:35:54 -0400216 CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
John Reck1bcacfd2017-11-03 10:12:19 -0700217 IContextFactory* contextFactory, std::unique_ptr<IRenderPipeline> renderPipeline);
Stan Iliev03de0742016-07-07 12:35:54 -0400218
John Recka5dda642014-05-22 15:43:54 -0700219 friend class RegisterFrameCallbackTask;
John Reck443a7142014-09-04 17:40:05 -0700220 // TODO: Replace with something better for layer & other GL object
221 // lifecycle tracking
222 friend class android::uirenderer::RenderState;
John Recka5dda642014-05-22 15:43:54 -0700223
John Reck2de950d2017-01-25 10:58:30 -0800224 void freePrefetchedLayers();
John Reck998a6d82014-08-28 15:35:53 -0700225
John Reck0def73a2016-07-01 16:19:13 -0700226 bool isSwapChainStuffed();
John Reck0b19a732019-03-07 17:18:25 -0800227 bool surfaceRequiresRedraw();
John Reckb36bfdd2020-07-23 13:47:49 -0700228 void setupPipelineSurface();
John Reck0def73a2016-07-01 16:19:13 -0700229
Stan Iliev768e3932016-07-08 21:34:52 -0400230 SkRect computeDirtyRect(const Frame& frame, SkRect* dirty);
Jorim Jaggi71db8892021-02-03 23:19:29 +0100231 void finishFrame(FrameInfo* frameInfo);
Stan Iliev768e3932016-07-08 21:34:52 -0400232
Siarhei Vishniakouf0cf18d2021-02-26 00:15:04 +0000233 /**
234 * Invoke 'reportFrameMetrics' on the last frame stored in 'mLast4FrameInfos'.
235 * Populate the 'presentTime' field before calling.
236 */
237 void reportMetricsWithPresentTime();
238
Fedor Kudasov52abc902019-06-26 16:59:16 +0100239 // The same type as Frame.mWidth and Frame.mHeight
240 int32_t mLastFrameWidth = 0;
241 int32_t mLastFrameHeight = 0;
Chris Craikddf22152015-10-14 17:42:47 -0700242
John Reck4f02bf42014-01-03 18:09:17 -0800243 RenderThread& mRenderThread;
Alec Mourif023a322019-11-25 10:02:21 -0800244 std::unique_ptr<ReliableSurface> mNativeSurface;
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800245 // The SurfaceControl reference is passed from ViewRootImpl, can be set to
246 // NULL to remove the reference
247 ASurfaceControl* mSurfaceControl = nullptr;
John Reck306f3312016-06-10 16:01:55 -0700248 // stopped indicates the CanvasContext will reject actual redraw operations,
249 // and defer repaint until it is un-stopped
John Reck8afcc762016-04-13 10:24:06 -0700250 bool mStopped = false;
Leon Scroggins III4afdd1c2018-05-14 14:59:30 -0400251 // Incremented each time the CanvasContext is stopped. Used to ignore
252 // delayed messages that are triggered after stopping.
253 int mGenerationID;
John Reck306f3312016-06-10 16:01:55 -0700254 // CanvasContext is dirty if it has received an update that it has not
255 // painted onto its surface.
256 bool mIsDirty = false;
Stan Iliev768e3932016-07-08 21:34:52 -0400257 SwapBehavior mSwapBehavior = SwapBehavior::kSwap_default;
John Recke486d932015-10-28 09:21:19 -0700258 struct SwapHistory {
259 SkRect damage;
260 nsecs_t vsyncTime;
John Reck0def73a2016-07-01 16:19:13 -0700261 nsecs_t swapCompletedTime;
262 nsecs_t dequeueDuration;
263 nsecs_t queueDuration;
John Recke486d932015-10-28 09:21:19 -0700264 };
265
John Reckca718a02019-10-21 10:14:58 -0700266 // Need at least 4 because we do quad buffer. Add a 5th for good measure.
267 RingBuffer<SwapHistory, 5> mSwapHistory;
John Reck28912a52016-04-18 14:34:18 -0700268 int64_t mFrameNumber = -1;
John Reck41459192019-10-31 15:04:58 -0700269 int64_t mDamageId = 0;
John Reck4f02bf42014-01-03 18:09:17 -0800270
Chris Craik31635682016-07-19 17:59:12 -0700271 // last vsync for a dropped frame due to stuffed queue
272 nsecs_t mLastDropVsync = 0;
273
John Reck4f02bf42014-01-03 18:09:17 -0800274 bool mOpaque;
John Reckbb3a3582018-09-26 11:21:08 -0700275 bool mUseForceDark = false;
John Reckd9d7f122018-05-03 14:40:56 -0700276 LightInfo mLightInfo;
277 LightGeometry mLightGeometry = {{0, 0, 0}, 0};
Chris Craik98787e62015-11-13 10:55:30 -0800278
John Reckedc524c2015-03-18 15:24:33 -0700279 bool mHaveNewSurface = false;
John Recke4267ea2014-06-03 15:53:15 -0700280 DamageAccumulator mDamageAccumulator;
Chris Craik0b7e8242015-10-28 16:50:44 -0700281 LayerUpdateQueue mLayerUpdateQueue;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800282 std::unique_ptr<AnimationContext> mAnimationContext;
John Recke45b1fd2014-04-15 09:50:16 -0700283
John Reck1bcacfd2017-11-03 10:12:19 -0700284 std::vector<sp<RenderNode>> mRenderNodes;
John Reckfe5e7b72014-05-23 17:42:28 -0700285
John Reckedc524c2015-03-18 15:24:33 -0700286 FrameInfo* mCurrentFrameInfo = nullptr;
Jorim Jaggi71db8892021-02-03 23:19:29 +0100287
288 // List of frames that are awaiting GPU completion reporting
289 RingBuffer<std::pair<FrameInfo*, int64_t>, 4> mLast4FrameInfos
290 GUARDED_BY(mLast4FrameInfosMutex);
291 std::mutex mLast4FrameInfosMutex;
292
John Reckb36016c2015-03-11 08:50:53 -0700293 std::string mName;
John Reckedc524c2015-03-18 15:24:33 -0700294 JankTracker mJankTracker;
John Reck4c9e59d2015-05-12 07:17:50 -0700295 FrameInfoVisualizer mProfiler;
Andres Morales910beb82016-02-02 16:19:40 -0800296 std::unique_ptr<FrameMetricsReporter> mFrameMetricsReporter;
John Reck998a6d82014-08-28 15:35:53 -0700297
John Reck51f2d602016-04-06 07:50:47 -0700298 std::set<RenderNode*> mPrefetchedLayers;
Skuhneea7a7fb2015-08-28 07:10:31 -0700299
300 // Stores the bounds of the main content.
Skuhneb8160872015-09-22 09:51:39 -0700301 Rect mContentDrawBounds;
John Reck38f6c032016-03-17 10:23:49 -0700302
John Reck322b8ab2019-03-14 13:15:28 -0700303 std::vector<std::future<void>> mFrameFences;
Stan Iliev768e3932016-07-08 21:34:52 -0400304 std::unique_ptr<IRenderPipeline> mRenderPipeline;
John Reckcc2eee82018-05-17 10:44:00 -0700305
306 std::vector<std::function<void(int64_t)>> mFrameCompleteCallbacks;
Jorim Jaggi71db8892021-02-03 23:19:29 +0100307
308 // If set to true, we expect that callbacks into onSurfaceStatsAvailable
309 bool mExpectSurfaceStats = false;
Huihong Luo054b8d32021-02-24 18:48:12 -0800310
311 std::function<void(int64_t, int64_t, int64_t)> mASurfaceTransactionCallback;
John Reck23b797a2014-01-03 18:08:34 -0800312};
313
314} /* namespace renderthread */
315} /* namespace uirenderer */
316} /* namespace android */