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/rs_base_render_engine.h" 26 #include "pipeline/rs_processor_factory.h" 27 #include "pipeline/rs_render_node.h" 28 #include "pipeline/rs_slr_scale.h" 29 #include "pipeline/rs_surface_handler.h" 30 #include "pipeline/rs_uni_render_virtual_processor.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 = default; 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) const; 47 GetCacheImgForCapture()48 std::shared_ptr<Drawing::Image> GetCacheImgForCapture() const 49 { 50 return cacheImgForCapture_; 51 } 52 SetCacheImgForCapture(std::shared_ptr<Drawing::Image> cacheImgForCapture)53 void SetCacheImgForCapture(std::shared_ptr<Drawing::Image> cacheImgForCapture) 54 { 55 cacheImgForCapture_ = cacheImgForCapture; 56 } GetRSSurfaceHandlerOnDraw()57 const std::shared_ptr<RSSurfaceHandler> GetRSSurfaceHandlerOnDraw() const 58 { 59 return surfaceHandler_; 60 } 61 GetMutableRSSurfaceHandlerOnDraw()62 std::shared_ptr<RSSurfaceHandler> GetMutableRSSurfaceHandlerOnDraw() 63 { 64 return surfaceHandler_; 65 } 66 GetDirtyRects()67 const std::vector<RectI>& GetDirtyRects() const 68 { 69 return dirtyRects_; 70 } 71 SetDirtyRects(const std::vector<RectI> & rects)72 void SetDirtyRects(const std::vector<RectI>& rects) 73 { 74 dirtyRects_ = rects; 75 } 76 GetSyncDirtyManager()77 std::shared_ptr<RSDirtyRegionManager> GetSyncDirtyManager() const override 78 { 79 return syncDirtyManager_; 80 } 81 82 #ifndef ROSEN_CROSS_PLATFORM 83 bool CreateSurface(sptr<IBufferConsumerListener> listener); GetConsumerListener()84 sptr<IBufferConsumerListener> GetConsumerListener() const 85 { 86 return consumerListener_; 87 } 88 #endif IsSurfaceCreated()89 bool IsSurfaceCreated() const 90 { 91 return surfaceCreated_; 92 } 93 94 #ifdef NEW_RENDER_CONTEXT GetRSSurface()95 std::shared_ptr<RSRenderSurface> GetRSSurface() const 96 { 97 return surface_; 98 } SetVirtualSurface(std::shared_ptr<RSRenderSurface> & virtualSurface,uint64_t pSurfaceUniqueId)99 void SetVirtualSurface(std::shared_ptr<RSRenderSurface>& virtualSurface, uint64_t pSurfaceUniqueId) 100 { 101 virtualSurface_ = virtualSurface; 102 virtualSurfaceUniqueId_ = pSurfaceUniqueId; 103 } GetVirtualSurface(uint64_t pSurfaceUniqueId)104 std::shared_ptr<RSRenderSurface> GetVirtualSurface(uint64_t pSurfaceUniqueId) 105 { 106 return virtualSurfaceUniqueId_ != pSurfaceUniqueId ? nullptr : virtualSurface_; 107 } 108 #else GetRSSurface()109 std::shared_ptr<RSSurface> GetRSSurface() const 110 { 111 return surface_; 112 } SetVirtualSurface(std::shared_ptr<RSSurface> & virtualSurface,uint64_t pSurfaceUniqueId)113 void SetVirtualSurface(std::shared_ptr<RSSurface>& virtualSurface, uint64_t pSurfaceUniqueId) 114 { 115 virtualSurface_ = virtualSurface; 116 virtualSurfaceUniqueId_ = pSurfaceUniqueId; 117 } GetVirtualSurface(uint64_t pSurfaceUniqueId)118 std::shared_ptr<RSSurface> GetVirtualSurface(uint64_t pSurfaceUniqueId) 119 { 120 return virtualSurfaceUniqueId_ != pSurfaceUniqueId ? nullptr : virtualSurface_; 121 } 122 #endif 123 IsFirstTimeToProcessor()124 bool IsFirstTimeToProcessor() const 125 { 126 return isFirstTimeToProcessor_; 127 } 128 SetOriginScreenRotation(const ScreenRotation & rotate)129 void SetOriginScreenRotation(const ScreenRotation& rotate) 130 { 131 originScreenRotation_ = rotate; 132 isFirstTimeToProcessor_ = false; 133 } 134 GetOriginScreenRotation()135 ScreenRotation GetOriginScreenRotation() const 136 { 137 return originScreenRotation_; 138 } 139 bool SkipFrame(uint32_t refreshRate, ScreenInfo screenInfo); 140 141 private: 142 explicit RSDisplayRenderNodeDrawable(std::shared_ptr<const RSRenderNode>&& node); 143 bool CheckDisplayNodeSkip(RSDisplayRenderParams& params, std::shared_ptr<RSProcessor> processor); 144 std::unique_ptr<RSRenderFrame> RequestFrame(RSDisplayRenderParams& params, std::shared_ptr<RSProcessor> processor); 145 void FindHardwareEnabledNodes(RSDisplayRenderParams& params); 146 void AdjustZOrderAndDrawSurfaceNode(std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& drawables, 147 Drawing::Canvas& canvas, RSDisplayRenderParams& params) const; 148 void WiredScreenProjection(RSDisplayRenderParams& params, std::shared_ptr<RSProcessor> processor); 149 void ScaleAndRotateMirrorForWiredScreen(RSDisplayRenderNodeDrawable& mirroredDrawable); 150 void DrawWiredMirrorCopy(RSDisplayRenderNodeDrawable& mirroredDrawable); 151 void DrawWiredMirrorOnDraw(RSDisplayRenderNodeDrawable& mirroredDrawable, RSDisplayRenderParams& params); 152 std::vector<RectI> CalculateVirtualDirtyForWiredScreen( 153 std::unique_ptr<RSRenderFrame>& renderFrame, RSDisplayRenderParams& params, Drawing::Matrix canvasMatrix); 154 void DrawWatermarkIfNeed(RSDisplayRenderParams& params, RSPaintFilterCanvas& canvas) const; 155 void RotateMirrorCanvas(ScreenRotation& rotation, float width, float height); 156 157 void DrawMirrorScreen(RSDisplayRenderParams& params, 158 std::shared_ptr<RSProcessor> processor); 159 std::vector<RectI> CalculateVirtualDirty(std::shared_ptr<RSUniRenderVirtualProcessor> virtualProcesser, 160 RSDisplayRenderParams& params, Drawing::Matrix canvasMatrix); 161 using DrawFuncPtr = void(RSDisplayRenderNodeDrawable::*)(Drawing::Canvas&); 162 void DrawMirror(RSDisplayRenderParams& params, std::shared_ptr<RSUniRenderVirtualProcessor> virtualProcesser, 163 DrawFuncPtr drawFunc, RSRenderThreadParams& uniParam); 164 void DrawMirrorCopy(RSDisplayRenderNodeDrawable& mirrorDrawable, RSDisplayRenderParams& params, 165 std::shared_ptr<RSUniRenderVirtualProcessor> virtualProcesser, RSRenderThreadParams& uniParam); 166 void DrawExpandScreen(RSUniRenderVirtualProcessor& processor); 167 void DrawCurtainScreen() const; 168 void RemoveClearMemoryTask() const; 169 void PostClearMemoryTask() const; 170 void SetCanvasBlack(RSProcessor& processor); 171 // Prepare for off-screen render 172 void UpdateSlrScale(ScreenInfo& screenInfo); 173 void ScaleCanvasIfNeeded(const ScreenInfo& screenInfo); 174 void PrepareOffscreenRender(const RSDisplayRenderNodeDrawable& displayDrawable, 175 bool useFixedSize = false, bool useCanvasSize = true); 176 void ClearTransparentBeforeSaveLayer(); 177 void FinishOffscreenRender(const Drawing::SamplingOptions& sampling, 178 bool isSamplingOn = false, float hdrBrightnessRatio = 1.0f); 179 void PrepareHdrDraw(int32_t offscreenWidth, int32_t offscreenHeight); 180 void FinishHdrDraw(Drawing::Brush& paint, float hdrBrightnessRatio); 181 int32_t GetSpecialLayerType(RSDisplayRenderParams& params); 182 void SetDisplayNodeSkipFlag(RSRenderThreadParams& uniParam, bool flag); 183 void UpdateDisplayDirtyManager(int32_t bufferage, bool useAlignedDirtyRegion = false); 184 static void CheckFilterCacheFullyCovered(RSSurfaceRenderParams& surfaceParams, RectI screenRect); 185 static void CheckAndUpdateFilterCacheOcclusion(RSDisplayRenderParams& params, ScreenInfo& screenInfo); 186 bool SkipFrameByInterval(uint32_t refreshRate, uint32_t skipFrameInterval); 187 bool SkipFrameByRefreshRate(uint32_t refreshRate, uint32_t expectedRefreshRate); 188 189 using Registrar = RenderNodeDrawableRegistrar<RSRenderNodeType::DISPLAY_NODE, OnGenerate>; 190 static Registrar instance_; 191 std::shared_ptr<RSSurfaceHandler> surfaceHandler_ = nullptr; 192 mutable std::shared_ptr<RSPaintFilterCanvas> curCanvas_ = nullptr; 193 std::shared_ptr<Drawing::Surface> offscreenSurface_ = nullptr; // temporarily holds offscreen surface 194 std::shared_ptr<RSPaintFilterCanvas> canvasBackup_ = nullptr; // backup current canvas before offscreen render 195 std::unordered_set<NodeId> currentBlackList_; 196 std::unordered_set<NodeId> lastBlackList_; 197 bool curSecExemption_ = false; 198 bool lastSecExemption_ = false; 199 std::shared_ptr<Drawing::Image> cacheImgForCapture_ = nullptr; 200 int32_t specialLayerType_ = 0; 201 bool castScreenEnableSkipWindow_ = false; 202 bool isDisplayNodeSkip_ = false; 203 bool isDisplayNodeSkipStatusChanged_ = false; 204 Drawing::Matrix lastMatrix_; 205 Drawing::Matrix lastMirrorMatrix_; 206 bool useFixedOffscreenSurfaceSize_ = false; 207 std::shared_ptr<RSDisplayRenderNodeDrawable> mirrorSourceDrawable_ = nullptr; 208 uint64_t virtualSurfaceUniqueId_ = 0; 209 bool resetRotate_ = false; 210 bool isFirstTimeToProcessor_ = true; 211 ScreenRotation originScreenRotation_ = ScreenRotation::INVALID_SCREEN_ROTATION; 212 // dirty manager 213 std::shared_ptr<RSDirtyRegionManager> syncDirtyManager_ = nullptr; 214 std::vector<RectI> dirtyRects_; 215 216 // surface create in render thread 217 static constexpr uint32_t BUFFER_SIZE = 4; 218 bool surfaceCreated_ = false; 219 #ifdef NEW_RENDER_CONTEXT 220 std::shared_ptr<RSRenderSurface> surface_ = nullptr; 221 std::shared_ptr<RSRenderSurface> virtualSurface_ = nullptr; 222 #else 223 std::shared_ptr<RSSurface> surface_ = nullptr; 224 std::shared_ptr<RSSurface> virtualSurface_ = nullptr; 225 #endif 226 std::unique_ptr<RSSLRScaleFunction> slrScale_; 227 228 std::shared_ptr<RSSLRScaleFunction> scaleManager_ = nullptr; 229 bool isMirrorSLRCopy_ = false; 230 #ifndef ROSEN_CROSS_PLATFORM 231 sptr<IBufferConsumerListener> consumerListener_ = nullptr; 232 #endif 233 int64_t lastRefreshTime_ = 0; 234 bool virtualDirtyRefresh_ = false; 235 236 bool isRenderSkipIfScreenOff_ = false; 237 }; 238 } // namespace DrawableV2 239 } // namespace OHOS::Rosen 240 #endif // RENDER_SERVICE_DRAWABLE_RS_DISPLAY_RENDER_NODE_DRAWABLE_H