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_surface_handler.h"
29 #include "pipeline/rs_uni_render_virtual_processor.h"
30 #include "screen_manager/rs_screen_manager.h"
31 
32 namespace OHOS::Rosen {
33 namespace DrawableV2 {
34 class RSDisplayRenderNodeDrawable : public RSRenderNodeDrawable {
35 public:
36     ~RSDisplayRenderNodeDrawable() override = default;
37 
38     static RSRenderNodeDrawable::Ptr OnGenerate(std::shared_ptr<const RSRenderNode> node);
39     void OnDraw(Drawing::Canvas& canvas) override;
40     void OnCapture(Drawing::Canvas& canvas) override;
41     void DrawHardwareEnabledNodes(Drawing::Canvas& canvas, RSDisplayRenderParams& params);
42     void DrawHardwareEnabledNodes(Drawing::Canvas& canvas);
43     void DrawHardwareEnabledNodesMissedInCacheImage(Drawing::Canvas& canvas);
44     void DrawHardwareEnabledTopNodesMissedInCacheImage(Drawing::Canvas& canvas);
45     void SwitchColorFilter(RSPaintFilterCanvas& canvas, float hdrBrightnessRatio = 1.f) const;
46     void DrawHardCursorNodesMissedInCacheImage(Drawing::Canvas& canvas);
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 mainWidth, float mainHeight);
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 ClearTransparentBeforeSaveLayer();
173     void PrepareOffscreenRender(const RSDisplayRenderNodeDrawable& displayDrawable, bool useFixedSize = false);
174     void FinishOffscreenRender(const Drawing::SamplingOptions& sampling, float hdrBrightnessRatio = 1.0f);
175     void PrepareHdrDraw(int32_t offscreenWidth, int32_t offscreenHeight);
176     void FinishHdrDraw(Drawing::Brush& paint, float hdrBrightnessRatio);
177     int32_t GetSpecialLayerType(RSDisplayRenderParams& params);
178     void SetDisplayNodeSkipFlag(RSRenderThreadParams& uniParam, bool flag);
179     void UpdateDisplayDirtyManager(int32_t bufferage, bool useAlignedDirtyRegion = false);
180     static void CheckFilterCacheFullyCovered(RSSurfaceRenderParams& surfaceParams, RectI screenRect);
181     static void CheckAndUpdateFilterCacheOcclusion(RSDisplayRenderParams& params, ScreenInfo& screenInfo);
182     bool HardCursorCreateLayer(std::shared_ptr<RSProcessor> processor);
183     void FindHardCursorNodes(RSDisplayRenderParams& params);
184     bool SkipFrameByInterval(uint32_t refreshRate, uint32_t skipFrameInterval);
185     bool SkipFrameByRefreshRate(uint32_t refreshRate, uint32_t expectedRefreshRate);
186 
187     using Registrar = RenderNodeDrawableRegistrar<RSRenderNodeType::DISPLAY_NODE, OnGenerate>;
188     static Registrar instance_;
189     std::shared_ptr<RSSurfaceHandler> surfaceHandler_ = nullptr;
190     mutable std::shared_ptr<RSPaintFilterCanvas> curCanvas_ = nullptr;
191     std::shared_ptr<Drawing::Surface> offscreenSurface_ = nullptr; // temporary holds offscreen surface
192     std::shared_ptr<RSPaintFilterCanvas> canvasBackup_ = nullptr; // backup current canvas before offscreen rende
193     std::unordered_set<NodeId> currentBlackList_;
194     std::unordered_set<NodeId> lastBlackList_;
195     bool curSecExemption_ = false;
196     bool lastSecExemption_ = false;
197     std::shared_ptr<Drawing::Image> cacheImgForCapture_ = nullptr;
198     int32_t specialLayerType_ = 0;
199     bool castScreenEnableSkipWindow_ = false;
200     bool isDisplayNodeSkip_ = false;
201     bool isDisplayNodeSkipStatusChanged_ = false;
202     Drawing::Matrix lastMatrix_;
203     Drawing::Matrix lastMirrorMatrix_;
204     bool useFixedOffscreenSurfaceSize_ = false;
205     std::shared_ptr<RSDisplayRenderNodeDrawable> mirrorSourceDrawable_ = nullptr;
206     uint64_t virtualSurfaceUniqueId_ = 0;
207     bool resetRotate_ = false;
208     bool isFirstTimeToProcessor_ = true;
209     ScreenRotation originScreenRotation_ = ScreenRotation::INVALID_SCREEN_ROTATION;
210     // dirty manager
211     std::shared_ptr<RSDirtyRegionManager> syncDirtyManager_ = nullptr;
212     std::vector<RectI> dirtyRects_;
213 
214     // surface create in render thread
215     static constexpr uint32_t BUFFER_SIZE = 4;
216     bool surfaceCreated_ = false;
217 #ifdef NEW_RENDER_CONTEXT
218     std::shared_ptr<RSRenderSurface> surface_ = nullptr;
219     std::shared_ptr<RSRenderSurface> virtualSurface_ = nullptr;
220 #else
221     std::shared_ptr<RSSurface> surface_ = nullptr;
222     std::shared_ptr<RSSurface> virtualSurface_ = nullptr;
223 #endif
224 
225 #ifndef ROSEN_CROSS_PLATFORM
226     sptr<IBufferConsumerListener> consumerListener_ = nullptr;
227 #endif
228     int64_t lastRefreshTime_ = 0;
229     bool virtualDirtyRefresh_ = false;
230 
231     bool isRenderSkipIfScreenOff_ = false;
232 };
233 } // namespace DrawableV2
234 } // namespace OHOS::Rosen
235 #endif // RENDER_SERVICE_DRAWABLE_RS_DISPLAY_RENDER_NODE_DRAWABLE_H