1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef RENDER_SERVICE_DRAWABLE_RS_DISPLAY_RENDER_NODE_DRAWABLE_H 17 #define RENDER_SERVICE_DRAWABLE_RS_DISPLAY_RENDER_NODE_DRAWABLE_H 18 19 #include <memory> 20 21 #include "common/rs_common_def.h" 22 #include "common/rs_occlusion_region.h" 23 #include "drawable/rs_render_node_drawable.h" 24 #include "params/rs_render_thread_params.h" 25 #include "pipeline/render_thread/rs_base_render_engine.h" 26 #include "pipeline/render_thread/rs_uni_render_virtual_processor.h" 27 #include "pipeline/rs_processor_factory.h" 28 #include "pipeline/rs_render_node.h" 29 #include "pipeline/rs_surface_handler.h" 30 #include "pipeline/slr_scale/rs_slr_scale.h" 31 #include "screen_manager/rs_screen_manager.h" 32 33 namespace OHOS::Rosen { 34 namespace DrawableV2 { 35 class RSDisplayRenderNodeDrawable : public RSRenderNodeDrawable { 36 public: 37 ~RSDisplayRenderNodeDrawable() override; 38 39 static RSRenderNodeDrawable::Ptr OnGenerate(std::shared_ptr<const RSRenderNode> node); 40 void OnDraw(Drawing::Canvas& canvas) override; 41 void OnCapture(Drawing::Canvas& canvas) override; 42 void DrawHardwareEnabledNodes(Drawing::Canvas& canvas, RSDisplayRenderParams& params); 43 void DrawHardwareEnabledNodes(Drawing::Canvas& canvas); 44 void DrawHardwareEnabledNodesMissedInCacheImage(Drawing::Canvas& canvas); 45 void DrawHardwareEnabledTopNodesMissedInCacheImage(Drawing::Canvas& canvas); 46 void SwitchColorFilter(RSPaintFilterCanvas& canvas, float hdrBrightnessRatio = 1.f, 47 bool displayP3Enable = false) const; 48 void SwitchColorFilterWithP3(RSPaintFilterCanvas& canvas, ColorFilterMode colorFilterMode, 49 float hdrBrightnessRatio = 1.f) const; 50 void DrawHardCursorNodesMissedInCacheImage(Drawing::Canvas& canvas); 51 GetCacheImgForCapture()52 std::shared_ptr<Drawing::Image> GetCacheImgForCapture() const 53 { 54 return cacheImgForCapture_; 55 } 56 SetCacheImgForCapture(std::shared_ptr<Drawing::Image> cacheImgForCapture)57 void SetCacheImgForCapture(std::shared_ptr<Drawing::Image> cacheImgForCapture) 58 { 59 cacheImgForCapture_ = cacheImgForCapture; 60 } 61 GetCacheImgForMultiScreenView()62 std::shared_ptr<Drawing::Image> GetCacheImgForMultiScreenView() const 63 { 64 return cacheImgForMultiScreenView_; 65 } 66 GetRSSurfaceHandlerOnDraw()67 const std::shared_ptr<RSSurfaceHandler> GetRSSurfaceHandlerOnDraw() const 68 { 69 return surfaceHandler_; 70 } 71 GetMutableRSSurfaceHandlerOnDraw()72 std::shared_ptr<RSSurfaceHandler> GetMutableRSSurfaceHandlerOnDraw() 73 { 74 return surfaceHandler_; 75 } 76 GetDirtyRects()77 const std::vector<RectI>& GetDirtyRects() const 78 { 79 return dirtyRects_; 80 } 81 SetDirtyRects(const std::vector<RectI> & rects)82 void SetDirtyRects(const std::vector<RectI>& rects) 83 { 84 dirtyRects_ = rects; 85 } 86 GetSyncDirtyManager()87 std::shared_ptr<RSDirtyRegionManager> GetSyncDirtyManager() const override 88 { 89 return syncDirtyManager_; 90 } 91 92 #ifndef ROSEN_CROSS_PLATFORM 93 bool CreateSurface(sptr<IBufferConsumerListener> listener); GetConsumerListener()94 sptr<IBufferConsumerListener> GetConsumerListener() const 95 { 96 return consumerListener_; 97 } 98 #endif IsSurfaceCreated()99 bool IsSurfaceCreated() const 100 { 101 return surfaceCreated_; 102 } GetRSSurface()103 std::shared_ptr<RSSurface> GetRSSurface() const 104 { 105 return surface_; 106 } SetVirtualSurface(std::shared_ptr<RSSurface> & virtualSurface,uint64_t pSurfaceUniqueId)107 void SetVirtualSurface(std::shared_ptr<RSSurface>& virtualSurface, uint64_t pSurfaceUniqueId) 108 { 109 virtualSurface_ = virtualSurface; 110 virtualSurfaceUniqueId_ = pSurfaceUniqueId; 111 } GetVirtualSurface(uint64_t pSurfaceUniqueId)112 std::shared_ptr<RSSurface> GetVirtualSurface(uint64_t pSurfaceUniqueId) 113 { 114 return virtualSurfaceUniqueId_ != pSurfaceUniqueId ? nullptr : virtualSurface_; 115 } 116 IsFirstTimeToProcessor()117 bool IsFirstTimeToProcessor() const 118 { 119 return isFirstTimeToProcessor_; 120 } 121 SetOriginScreenRotation(const ScreenRotation & rotate)122 void SetOriginScreenRotation(const ScreenRotation& rotate) 123 { 124 originScreenRotation_ = rotate; 125 isFirstTimeToProcessor_ = false; 126 } 127 GetOriginScreenRotation()128 ScreenRotation GetOriginScreenRotation() const 129 { 130 return originScreenRotation_; 131 } 132 bool SkipFrame(uint32_t refreshRate, ScreenInfo screenInfo); 133 134 private: 135 explicit RSDisplayRenderNodeDrawable(std::shared_ptr<const RSRenderNode>&& node); 136 bool CheckDisplayNodeSkip(RSDisplayRenderParams& params, std::shared_ptr<RSProcessor> processor); 137 void ClearCanvasStencil(RSPaintFilterCanvas& canvas, RSDisplayRenderParams& params, 138 RSRenderThreadParams& uniParam); 139 std::unique_ptr<RSRenderFrame> RequestFrame(RSDisplayRenderParams& params, std::shared_ptr<RSProcessor> processor); 140 void FindHardwareEnabledNodes(RSDisplayRenderParams& params); 141 void AdjustZOrderAndDrawSurfaceNode(std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& drawables, 142 Drawing::Canvas& canvas, RSDisplayRenderParams& params) const; 143 void WiredScreenProjection(RSDisplayRenderParams& params, std::shared_ptr<RSProcessor> processor); 144 void ScaleAndRotateMirrorForWiredScreen(RSDisplayRenderNodeDrawable& mirroredDrawable); 145 void DrawWiredMirrorCopy(RSDisplayRenderNodeDrawable& mirroredDrawable); 146 void DrawWiredMirrorOnDraw(RSDisplayRenderNodeDrawable& mirroredDrawable, RSDisplayRenderParams& params); 147 std::vector<RectI> CalculateVirtualDirtyForWiredScreen( 148 std::unique_ptr<RSRenderFrame>& renderFrame, RSDisplayRenderParams& params, Drawing::Matrix canvasMatrix); 149 void DrawWatermarkIfNeed(RSDisplayRenderParams& params, RSPaintFilterCanvas& canvas) const; 150 void RotateMirrorCanvas(ScreenRotation& rotation, float width, float height); 151 152 void DrawMirrorScreen(RSDisplayRenderParams& params, 153 std::shared_ptr<RSProcessor> processor); 154 std::vector<RectI> CalculateVirtualDirty(std::shared_ptr<RSUniRenderVirtualProcessor> virtualProcesser, 155 RSDisplayRenderParams& params, Drawing::Matrix canvasMatrix); 156 using DrawFuncPtr = void(RSDisplayRenderNodeDrawable::*)(Drawing::Canvas&); 157 void DrawMirror(RSDisplayRenderParams& params, std::shared_ptr<RSUniRenderVirtualProcessor> virtualProcesser, 158 DrawFuncPtr drawFunc, RSRenderThreadParams& uniParam); 159 void DrawMirrorCopy(RSDisplayRenderNodeDrawable& mirrorDrawable, RSDisplayRenderParams& params, 160 std::shared_ptr<RSUniRenderVirtualProcessor> virtualProcesser, RSRenderThreadParams& uniParam); 161 void DrawExpandScreen(RSDisplayRenderParams& params, RSUniRenderVirtualProcessor& processor); 162 void DrawCurtainScreen() const; 163 void InitTranslateForWallpaper(); 164 void CalculateTranslationForWallpaper(); 165 void RemoveClearMemoryTask() const; 166 void PostClearMemoryTask() const; 167 void SetCanvasBlack(RSProcessor& processor); 168 void SetSecurityMask(RSProcessor& processor); 169 void SetScreenRotationForPointLight(RSDisplayRenderParams ¶ms); 170 // Prepare for off-screen render 171 void UpdateSlrScale(ScreenInfo& screenInfo); 172 void ScaleCanvasIfNeeded(const ScreenInfo& screenInfo); 173 void PrepareOffscreenRender(const RSDisplayRenderNodeDrawable& displayDrawable, 174 bool useFixedSize = false, bool useCanvasSize = true); 175 void ClearTransparentBeforeSaveLayer(); 176 static std::shared_ptr<Drawing::ShaderEffect> MakeBrightnessAdjustmentShader( 177 const std::shared_ptr<Drawing::Image>& image, const Drawing::SamplingOptions& sampling, 178 float hdrBrightnessRatio); 179 void FinishOffscreenRender(const Drawing::SamplingOptions& sampling, 180 bool isSamplingOn = false, float hdrBrightnessRatio = 1.0f); 181 void PrepareHdrDraw(int32_t offscreenWidth, int32_t offscreenHeight); 182 void FinishHdrDraw(Drawing::Brush& paint, float hdrBrightnessRatio); 183 int32_t GetSpecialLayerType(RSDisplayRenderParams& params, bool isSecLayerInVisivleRect = true); 184 void SetDisplayNodeSkipFlag(RSRenderThreadParams& uniParam, bool flag); 185 void UpdateDisplayDirtyManager(int32_t bufferage, bool useAlignedDirtyRegion = false); 186 static void CheckFilterCacheFullyCovered(RSSurfaceRenderParams& surfaceParams, RectI screenRect); 187 static void CheckAndUpdateFilterCacheOcclusion(RSDisplayRenderParams& params, ScreenInfo& screenInfo); 188 bool HardCursorCreateLayer(std::shared_ptr<RSProcessor> processor); 189 void FindHardCursorNodes(RSDisplayRenderParams& params); 190 // For P3-scRGB Control 191 bool EnablescRGBForP3AndUiFirst(const GraphicColorGamut& currentGamut); 192 void RenderOverDraw(); 193 bool SkipFrameByInterval(uint32_t refreshRate, uint32_t skipFrameInterval); 194 bool SkipFrameByRefreshRate(uint32_t refreshRate, uint32_t expectedRefreshRate); 195 196 using Registrar = RenderNodeDrawableRegistrar<RSRenderNodeType::DISPLAY_NODE, OnGenerate>; 197 static Registrar instance_; 198 std::shared_ptr<RSSurfaceHandler> surfaceHandler_ = nullptr; 199 mutable std::shared_ptr<RSPaintFilterCanvas> curCanvas_ = nullptr; 200 std::shared_ptr<Drawing::Surface> offscreenSurface_ = nullptr; // temporarily holds offscreen surface 201 std::shared_ptr<RSPaintFilterCanvas> canvasBackup_ = nullptr; // backup current canvas before offscreen render 202 std::unordered_set<NodeId> currentBlackList_ = {}; 203 std::unordered_set<NodeId> lastBlackList_ = {}; 204 bool curSecExemption_ = false; 205 bool lastSecExemption_ = false; 206 std::shared_ptr<Drawing::Image> cacheImgForCapture_ = nullptr; 207 std::shared_ptr<Drawing::Image> cacheImgForMultiScreenView_ = nullptr; 208 int32_t specialLayerType_ = 0; 209 bool castScreenEnableSkipWindow_ = false; 210 bool isDisplayNodeSkip_ = false; 211 bool isDisplayNodeSkipStatusChanged_ = false; 212 Drawing::Matrix lastMatrix_; 213 Drawing::Matrix lastMirrorMatrix_; 214 bool useFixedOffscreenSurfaceSize_ = false; 215 std::shared_ptr<RSDisplayRenderNodeDrawable> mirrorSourceDrawable_ = nullptr; 216 uint64_t virtualSurfaceUniqueId_ = 0; 217 bool resetRotate_ = false; 218 bool isFirstTimeToProcessor_ = true; 219 ScreenRotation originScreenRotation_ = ScreenRotation::INVALID_SCREEN_ROTATION; 220 // dirty manager 221 std::shared_ptr<RSDirtyRegionManager> syncDirtyManager_ = nullptr; 222 std::vector<RectI> dirtyRects_; 223 224 // surface create in render thread 225 static constexpr uint32_t BUFFER_SIZE = 4; 226 bool surfaceCreated_ = false; 227 std::shared_ptr<RSSurface> surface_ = nullptr; 228 std::shared_ptr<RSSurface> virtualSurface_ = nullptr; 229 std::unique_ptr<RSSLRScaleFunction> slrScale_; 230 231 static std::shared_ptr<Drawing::RuntimeEffect> brightnessAdjustmentShaderEffect_; 232 233 std::shared_ptr<RSSLRScaleFunction> scaleManager_ = nullptr; 234 bool isMirrorSLRCopy_ = false; 235 #ifndef ROSEN_CROSS_PLATFORM 236 sptr<IBufferConsumerListener> consumerListener_ = nullptr; 237 #endif 238 int64_t lastRefreshTime_ = 0; 239 bool virtualDirtyRefresh_ = false; 240 bool enableVisibleRect_ = false; 241 Drawing::RectI curVisibleRect_; 242 Drawing::RectI lastVisibleRect_; 243 int32_t offscreenTranslateX_ = 0; 244 int32_t offscreenTranslateY_ = 0; 245 246 bool isRenderSkipIfScreenOff_ = false; 247 }; 248 } // namespace DrawableV2 249 } // namespace OHOS::Rosen 250 #endif // RENDER_SERVICE_DRAWABLE_RS_DISPLAY_RENDER_NODE_DRAWABLE_H