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 17 #pragma once 18 19 #include <SkBitmap.h> 20 #include <SkRect.h> 21 #include <SkSize.h> 22 #include <cutils/compiler.h> 23 #include <utils/Functor.h> 24 #include <utils/Mutex.h> 25 26 #include <functional> 27 #include <future> 28 #include <set> 29 #include <string> 30 #include <utility> 31 #include <vector> 32 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" 46 #include "renderthread/RenderTask.h" 47 #include "renderthread/RenderThread.h" 48 #include "utils/RingBuffer.h" 49 50 namespace android { 51 namespace uirenderer { 52 53 class AnimationContext; 54 class DeferredLayerUpdater; 55 class ErrorHandler; 56 class Layer; 57 class Rect; 58 class RenderState; 59 60 namespace renderthread { 61 62 class Frame; 63 64 // This per-renderer class manages the bridge between the global EGL context 65 // and the render surface. 66 // TODO: Rename to Renderer or some other per-window, top-level manager 67 class CanvasContext : public IFrameCallback { 68 public: 69 static CanvasContext* create(RenderThread& thread, bool translucent, RenderNode* rootRenderNode, 70 IContextFactory* contextFactory, pid_t uiThreadId, 71 pid_t renderThreadId); 72 virtual ~CanvasContext(); 73 74 /** 75 * Update or create a layer specific for the provided RenderNode. The layer 76 * attached to the node will be specific to the RenderPipeline used by this 77 * context 78 * 79 * @return true if the layer has been created or updated 80 */ createOrUpdateLayer(RenderNode * node,const DamageAccumulator & dmgAccumulator,ErrorHandler * errorHandler)81 bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& dmgAccumulator, 82 ErrorHandler* errorHandler) { 83 return mRenderPipeline->createOrUpdateLayer(node, dmgAccumulator, errorHandler); 84 } 85 86 /** 87 * Pin any mutable images to the GPU cache. A pinned images is guaranteed to 88 * remain in the cache until it has been unpinned. We leverage this feature 89 * to avoid making a CPU copy of the pixels. 90 * 91 * @return true if all images have been successfully pinned to the GPU cache 92 * and false otherwise (e.g. cache limits have been exceeded). 93 */ pinImages(std::vector<SkImage * > & mutableImages)94 bool pinImages(std::vector<SkImage*>& mutableImages) { 95 if (!Properties::isDrawingEnabled()) { 96 return true; 97 } 98 return mRenderPipeline->pinImages(mutableImages); 99 } pinImages(LsaVector<sk_sp<Bitmap>> & images)100 bool pinImages(LsaVector<sk_sp<Bitmap>>& images) { 101 if (!Properties::isDrawingEnabled()) { 102 return true; 103 } 104 return mRenderPipeline->pinImages(images); 105 } 106 107 /** 108 * Unpin any image that had be previously pinned to the GPU cache 109 */ unpinImages()110 void unpinImages() { mRenderPipeline->unpinImages(); } 111 112 static void invokeFunctor(const RenderThread& thread, Functor* functor); 113 114 static void prepareToDraw(const RenderThread& thread, Bitmap* bitmap); 115 116 /* 117 * If Properties::isSkiaEnabled() is true then this will return the Skia 118 * grContext associated with the current RenderPipeline. 119 */ getGrContext()120 GrDirectContext* getGrContext() const { return mRenderThread.getGrContext(); } 121 getSurfaceControl()122 ASurfaceControl* getSurfaceControl() const { return mSurfaceControl; } getSurfaceControlGenerationId()123 int32_t getSurfaceControlGenerationId() const { return mSurfaceControlGenerationId; } 124 125 // Won't take effect until next EGLSurface creation 126 void setSwapBehavior(SwapBehavior swapBehavior); 127 128 void setHardwareBuffer(AHardwareBuffer* buffer); 129 void setSurface(ANativeWindow* window, bool enableTimeout = true); 130 void setSurfaceControl(ASurfaceControl* surfaceControl); 131 bool pauseSurface(); 132 void setStopped(bool stopped); isStopped()133 bool isStopped() { return mStopped || !hasOutputTarget(); } hasOutputTarget()134 bool hasOutputTarget() const { return mNativeSurface.get() || mHardwareBuffer; } 135 void allocateBuffers(); 136 137 void setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha); 138 void setLightGeometry(const Vector3& lightCenter, float lightRadius); 139 void setOpaque(bool opaque); 140 float setColorMode(ColorMode mode); 141 float targetSdrHdrRatio() const; 142 void setTargetSdrHdrRatio(float ratio); 143 bool makeCurrent(); 144 void prepareTree(TreeInfo& info, int64_t* uiFrameInfo, int64_t syncQueued, RenderNode* target); 145 // Returns the DequeueBufferDuration. 146 void draw(bool solelyTextureViewUpdates); 147 void destroy(); 148 149 // IFrameCallback, Choreographer-driven frame callback entry point 150 virtual void doFrame() override; 151 void prepareAndDraw(RenderNode* node); 152 153 void buildLayer(RenderNode* node); 154 void markLayerInUse(RenderNode* node); 155 156 void destroyHardwareResources(); 157 158 DeferredLayerUpdater* createTextureLayer(); 159 160 void stopDrawing(); 161 void notifyFramePending(); 162 profiler()163 FrameInfoVisualizer& profiler() { return mProfiler; } 164 165 void dumpFrames(int fd); 166 void resetFrameStats(); 167 168 void setName(const std::string&& name); 169 170 void addRenderNode(RenderNode* node, bool placeFront); 171 void removeRenderNode(RenderNode* node); 172 setContentDrawBounds(const Rect & bounds)173 void setContentDrawBounds(const Rect& bounds) { mContentDrawBounds = bounds; } 174 175 void addFrameMetricsObserver(FrameMetricsObserver* observer); 176 void removeFrameMetricsObserver(FrameMetricsObserver* observer); 177 178 // Used to queue up work that needs to be completed before this frame completes 179 void enqueueFrameWork(std::function<void()>&& func); 180 181 uint64_t getFrameNumber(); 182 183 void waitOnFences(); 184 getRenderPipeline()185 IRenderPipeline* getRenderPipeline() { return mRenderPipeline.get(); } 186 addFrameCommitListener(std::function<void (bool)> && func)187 void addFrameCommitListener(std::function<void(bool)>&& func) { 188 mFrameCommitCallbacks.push_back(std::move(func)); 189 } 190 setPictureCapturedCallback(const std::function<void (sk_sp<SkPicture> &&)> & callback)191 void setPictureCapturedCallback(const std::function<void(sk_sp<SkPicture>&&)>& callback) { 192 mRenderPipeline->setPictureCapturedCallback(callback); 193 } 194 setForceDark(bool enable)195 void setForceDark(bool enable) { mUseForceDark = enable; } 196 useForceDark()197 bool useForceDark() { 198 return mUseForceDark; 199 } 200 201 SkISize getNextFrameSize() const; 202 203 // Returns the matrix to use to nudge non-AA'd points/lines towards the fragment center 204 const SkM44& getPixelSnapMatrix() const; 205 206 // Called when SurfaceStats are available. 207 static void onSurfaceStatsAvailable(void* context, int32_t surfaceControlId, 208 ASurfaceControlStats* stats); 209 setASurfaceTransactionCallback(const std::function<bool (int64_t,int64_t,int64_t)> & callback)210 void setASurfaceTransactionCallback( 211 const std::function<bool(int64_t, int64_t, int64_t)>& callback) { 212 mASurfaceTransactionCallback = callback; 213 } 214 setHardwareBufferRenderParams(const HardwareBufferRenderParams & params)215 void setHardwareBufferRenderParams(const HardwareBufferRenderParams& params) { 216 mBufferParams = params; 217 } 218 219 bool mergeTransaction(ASurfaceTransaction* transaction, ASurfaceControl* control); 220 setPrepareSurfaceControlForWebviewCallback(const std::function<void ()> & callback)221 void setPrepareSurfaceControlForWebviewCallback(const std::function<void()>& callback) { 222 mPrepareSurfaceControlForWebviewCallback = callback; 223 } 224 225 void prepareSurfaceControlForWebview(); 226 227 static CanvasContext* getActiveContext(); 228 229 void sendLoadResetHint(); 230 231 void sendLoadIncreaseHint(); 232 233 void setSyncDelayDuration(nsecs_t duration); 234 235 void startHintSession(); 236 237 static bool shouldDither(); 238 239 private: 240 CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode, 241 IContextFactory* contextFactory, std::unique_ptr<IRenderPipeline> renderPipeline, 242 pid_t uiThreadId, pid_t renderThreadId); 243 244 friend class RegisterFrameCallbackTask; 245 // TODO: Replace with something better for layer & other GL object 246 // lifecycle tracking 247 friend class android::uirenderer::RenderState; 248 249 void freePrefetchedLayers(); 250 251 bool isSwapChainStuffed(); 252 bool surfaceRequiresRedraw(); 253 void setupPipelineSurface(); 254 255 SkRect computeDirtyRect(const Frame& frame, SkRect* dirty); 256 void finishFrame(FrameInfo* frameInfo); 257 258 /** 259 * Invoke 'reportFrameMetrics' on the last frame stored in 'mLast4FrameInfos'. 260 * Populate the 'presentTime' field before calling. 261 */ 262 void reportMetricsWithPresentTime(); 263 264 struct FrameMetricsInfo { 265 FrameInfo* frameInfo; 266 int64_t frameNumber; 267 int32_t surfaceId; 268 }; 269 270 FrameInfo* getFrameInfoFromLast4(uint64_t frameNumber, uint32_t surfaceControlId); 271 272 Frame getFrame(); 273 274 // The same type as Frame.mWidth and Frame.mHeight 275 int32_t mLastFrameWidth = 0; 276 int32_t mLastFrameHeight = 0; 277 278 RenderThread& mRenderThread; 279 280 AHardwareBuffer* mHardwareBuffer = nullptr; 281 HardwareBufferRenderParams mBufferParams; 282 std::unique_ptr<ReliableSurface> mNativeSurface; 283 // The SurfaceControl reference is passed from ViewRootImpl, can be set to 284 // NULL to remove the reference 285 ASurfaceControl* mSurfaceControl = nullptr; 286 // id to track surface control changes and WebViewFunctor uses it to determine 287 // whether reparenting is needed also used by FrameMetricsReporter to determine 288 // if a frame is from an "old" surface (i.e. one that existed before the 289 // observer was attched) and therefore shouldn't be reported. 290 // NOTE: It is important that this is an increasing counter. 291 int32_t mSurfaceControlGenerationId = 0; 292 // stopped indicates the CanvasContext will reject actual redraw operations, 293 // and defer repaint until it is un-stopped 294 bool mStopped = false; 295 // Incremented each time the CanvasContext is stopped. Used to ignore 296 // delayed messages that are triggered after stopping. 297 int mGenerationID; 298 // CanvasContext is dirty if it has received an update that it has not 299 // painted onto its surface. 300 bool mIsDirty = false; 301 SwapBehavior mSwapBehavior = SwapBehavior::kSwap_default; 302 struct SwapHistory { 303 SkRect damage; 304 nsecs_t vsyncTime; 305 nsecs_t swapCompletedTime; 306 nsecs_t dequeueDuration; 307 nsecs_t queueDuration; 308 }; 309 310 // Need at least 4 because we do quad buffer. Add a few more for good measure. 311 RingBuffer<SwapHistory, 7> mSwapHistory; 312 // Frame numbers start at 1, 0 means uninitialized 313 uint64_t mFrameNumber = 0; 314 int64_t mDamageId = 0; 315 316 // last vsync for a dropped frame due to stuffed queue 317 nsecs_t mLastDropVsync = 0; 318 319 bool mOpaque; 320 bool mUseForceDark = false; 321 LightInfo mLightInfo; 322 LightGeometry mLightGeometry = {{0, 0, 0}, 0}; 323 324 bool mHaveNewSurface = false; 325 DamageAccumulator mDamageAccumulator; 326 LayerUpdateQueue mLayerUpdateQueue; 327 std::unique_ptr<AnimationContext> mAnimationContext; 328 329 std::vector<sp<RenderNode>> mRenderNodes; 330 331 FrameInfo* mCurrentFrameInfo = nullptr; 332 333 // List of data of frames that are awaiting GPU completion reporting. Used to compute frame 334 // metrics and determine whether or not to report the metrics. 335 RingBuffer<FrameMetricsInfo, 4> mLast4FrameMetricsInfos 336 GUARDED_BY(mLast4FrameMetricsInfosMutex); 337 std::mutex mLast4FrameMetricsInfosMutex; 338 339 std::string mName; 340 JankTracker mJankTracker; 341 FrameInfoVisualizer mProfiler; 342 std::unique_ptr<FrameMetricsReporter> mFrameMetricsReporter 343 GUARDED_BY(mFrameMetricsReporterMutex); 344 std::mutex mFrameMetricsReporterMutex; 345 346 std::set<RenderNode*> mPrefetchedLayers; 347 348 // Stores the bounds of the main content. 349 Rect mContentDrawBounds; 350 351 std::vector<std::future<void>> mFrameFences; 352 std::unique_ptr<IRenderPipeline> mRenderPipeline; 353 354 std::vector<std::function<void(bool)>> mFrameCommitCallbacks; 355 356 // If set to true, we expect that callbacks into onSurfaceStatsAvailable 357 bool mExpectSurfaceStats = false; 358 359 std::function<bool(int64_t, int64_t, int64_t)> mASurfaceTransactionCallback; 360 std::function<void()> mPrepareSurfaceControlForWebviewCallback; 361 362 HintSessionWrapper mHintSessionWrapper; 363 nsecs_t mLastDequeueBufferDuration = 0; 364 nsecs_t mSyncDelayDuration = 0; 365 nsecs_t mIdleDuration = 0; 366 367 ColorMode mColorMode = ColorMode::Default; 368 float mTargetSdrHdrRatio = 1.f; 369 370 struct SkippedFrameInfo { 371 int64_t vsyncId; 372 int64_t startTime; 373 }; 374 std::optional<SkippedFrameInfo> mSkippedFrameInfo; 375 }; 376 377 } /* namespace renderthread */ 378 } /* namespace uirenderer */ 379 } /* namespace android */ 380