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