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