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_BASE_PARAMS_RS_RENDER_PARAMS_H
17 #define RENDER_SERVICE_BASE_PARAMS_RS_RENDER_PARAMS_H
18 
19 #include "common/rs_common_def.h"
20 #include "common/rs_occlusion_region.h"
21 #include "common/rs_rect.h"
22 #include "drawable/rs_render_node_drawable_adapter.h"
23 #include "pipeline/rs_render_node.h"
24 #include "property/rs_properties.h"
25 #include "utils/matrix.h"
26 #include "utils/region.h"
27 
28 #ifndef ROSEN_CROSS_PLATFORM
29 #include <iconsumer_surface.h>
30 #include <surface.h>
31 #include "sync_fence.h"
32 #endif
33 
34 namespace OHOS::Rosen {
35 #define RENDER_BASIC_PARAM_TO_STRING(basicType) (std::string(#basicType "[") + std::to_string(basicType) + "] ")
36 #define RENDER_RECT_PARAM_TO_STRING(rect) (std::string(#rect "[") + (rect).ToString() + "] ")
37 #define RENDER_PARAM_TO_STRING(param) (std::string(#param "[") + (param).ToString() + "] ")
38 
39 struct DirtyRegionInfoForDFX {
40     RectI oldDirty;
41     RectI oldDirtyInSurface;
42     bool operator==(const DirtyRegionInfoForDFX& rhs) const
43     {
44         return oldDirty == rhs.oldDirty && oldDirtyInSurface == rhs.oldDirtyInSurface;
45     }
46 };
47 struct RSLayerInfo;
48 struct ScreenInfo;
49 
50 typedef enum {
51     RS_PARAM_DEFAULT,
52     RS_PARAM_OWNED_BY_NODE,
53     RS_PARAM_OWNED_BY_DRAWABLE,
54     RS_PARAM_OWNED_BY_DRAWABLE_UIFIRST,
55     RS_PARAM_INVALID,
56 } RSRenderParamsType;
57 
58 class RSB_EXPORT RSRenderParams {
59 public:
RSRenderParams(NodeId id)60     RSRenderParams(NodeId id) : id_(id) {}
61     virtual ~RSRenderParams() = default;
62 
63     struct SurfaceParam {
64         int width = 0;
65         int height = 0;
66     };
67 
68     void SetDirtyType(RSRenderParamsDirtyType dirtyType);
69 
70     void SetAlpha(float alpha);
71     float GetAlpha() const;
72 
73     void SetAlphaOffScreen(bool alphaOffScreen);
74     bool GetAlphaOffScreen() const;
75 
76     void SetMatrix(const Drawing::Matrix& matrix);
77     const Drawing::Matrix& GetMatrix() const;
78 
79     void ApplyAlphaAndMatrixToCanvas(RSPaintFilterCanvas& canvas, bool applyMatrix = true) const;
80 
81     void SetBoundsRect(const Drawing::RectF& boundsRect);
82     const Drawing::Rect& GetBounds() const;
83 
84     void SetFrameRect(const Drawing::RectF& frameRect);
85     const Drawing::Rect& GetFrameRect() const;
86 
87     // return to add some dirtynode does not mark pending
88     bool SetLocalDrawRect(const RectF& localDrawRect);
89     const RectF& GetLocalDrawRect() const;
90 
91     void SetHasSandBox(bool hasSandBox);
92     bool HasSandBox() const;
93 
94     bool GetShouldPaint() const;
95     void SetShouldPaint(bool shouldPaint);
96     void SetContentEmpty(bool empty);
97 
98     bool NeedSync() const;
99     void SetNeedSync(bool needSync);
100 
101     const std::shared_ptr<RSFilter>& GetForegroundFilterCache() const;
102     void SetForegroundFilterCache(const std::shared_ptr<RSFilter>& foregroundFilterCache);
103 
GetId()104     inline NodeId GetId() const
105     {
106         return id_;
107     }
108 
SetParamsType(RSRenderParamsType paramsType)109     void SetParamsType(RSRenderParamsType paramsType)
110     {
111         paramsType_ = paramsType;
112     }
113 
GetParamsType()114     inline RSRenderParamsType GetParamsType() const
115     {
116         return paramsType_;
117     }
118 
GetFrameGravity()119     Gravity GetFrameGravity() const
120     {
121         return frameGravity_;
122     }
123 
124     void SetFrameGravity(Gravity gravity);
125 
126     void SetNeedFilter(bool needFilter);
127 
NeedFilter()128     inline bool NeedFilter() const
129     {
130         return needFilter_;
131     }
132 
133     void SetNodeType(RSRenderNodeType type);
GetType()134     inline RSRenderNodeType GetType() const
135     {
136         return renderNodeType_;
137     }
138 
139     void SetEffectNodeShouldPaint(bool effectNodeShouldPaint);
GetEffectNodeShouldPaint()140     inline bool GetEffectNodeShouldPaint() const
141     {
142         return effectNodeShouldPaint_;
143     }
144 
145     void SetHasGlobalCorner(bool hasGlobalCorner);
HasGlobalCorner()146     inline bool HasGlobalCorner() const
147     {
148         return hasGlobalCorner_;
149     }
150 
151     void SetHasBlurFilter(bool hasBlurFilter);
HasBlurFilter()152     inline bool HasBlurFilter() const
153     {
154         return hasBlurFilter_;
155     }
156 
157     void SetGlobalAlpha(float alpha);
GetGlobalAlpha()158     inline float GetGlobalAlpha() const
159     {
160         return globalAlpha_;
161     }
162 
IsSecurityLayer()163     inline bool IsSecurityLayer() const
164     {
165         return isSecurityLayer_;
166     }
167 
IsSkipLayer()168     inline bool IsSkipLayer() const
169     {
170         return isSkipLayer_;
171     }
172 
IsLayerDirty()173     inline bool IsLayerDirty() const
174     {
175         return dirtyType_.test(RSRenderParamsDirtyType::LAYER_INFO_DIRTY);
176     }
177 
IsBufferDirty()178     inline bool IsBufferDirty() const
179     {
180         return dirtyType_.test(RSRenderParamsDirtyType::BUFFER_INFO_DIRTY);
181     }
182 
183     void SetChildHasVisibleFilter(bool val);
184     bool ChildHasVisibleFilter() const;
185     void SetChildHasVisibleEffect(bool val);
186     bool ChildHasVisibleEffect() const;
187 
188     void SetCacheSize(Vector2f size);
GetCacheSize()189     inline Vector2f GetCacheSize() const
190     {
191         return cacheSize_;
192     }
193 
194     void SetDrawingCacheChanged(bool isChanged, bool lastFrameSynced);
195     bool GetDrawingCacheChanged() const;
196 
197     void SetDrawingCacheType(RSDrawingCacheType cacheType);
198     RSDrawingCacheType GetDrawingCacheType() const;
199 
200     void OpincUpdateRootFlag(bool suggestFlag);
201     bool OpincGetRootFlag() const;
202     void OpincSetCacheChangeFlag(bool state, bool lastFrameSynced);
203     bool OpincGetCacheChangeState();
204 
205     void SetDrawingCacheIncludeProperty(bool includeProperty);
206     bool GetDrawingCacheIncludeProperty() const;
207 
208     void SetRSFreezeFlag(bool freezeFlag);
209     bool GetRSFreezeFlag() const;
210     void SetShadowRect(Drawing::Rect rect);
211     Drawing::Rect GetShadowRect() const;
212 
213     void SetDirtyRegionInfoForDFX(DirtyRegionInfoForDFX dirtyRegionInfo);
214     DirtyRegionInfoForDFX GetDirtyRegionInfoForDFX() const;
215 
216     // One-time trigger, needs to be manually reset false in main/RT thread after each sync operation
217     void OnCanvasDrawingSurfaceChange(const std::unique_ptr<RSRenderParams>& target);
218     bool GetCanvasDrawingSurfaceChanged() const;
219     void SetCanvasDrawingSurfaceChanged(bool changeFlag);
220     SurfaceParam GetCanvasDrawingSurfaceParams();
221     void SetCanvasDrawingSurfaceParams(int width, int height);
222 
SetStartingWindowFlag(bool b)223     void SetStartingWindowFlag(bool b)
224     {
225         if (startingWindowFlag_ == b) {
226             return;
227         }
228         startingWindowFlag_ = b;
229         needSync_ = true;
230     }
231 
GetStartingWindowFlag()232     bool GetStartingWindowFlag() const
233     {
234         return startingWindowFlag_;
235     }
236 
237     bool SetFirstLevelNode(NodeId firstLevelNodeId);
238     NodeId GetFirstLevelNodeId() const;
239     bool SetUiFirstRootNode(NodeId uifirstRootNodeId);
240     NodeId GetUifirstRootNodeId() const;
241 
242     // disable copy and move
243     RSRenderParams(const RSRenderParams&) = delete;
244     RSRenderParams(RSRenderParams&&) = delete;
245     RSRenderParams& operator=(const RSRenderParams&) = delete;
246     RSRenderParams& operator=(RSRenderParams&&) = delete;
247 
248     virtual void OnSync(const std::unique_ptr<RSRenderParams>& target);
249 
250     // dfx
251     virtual std::string ToString() const;
252 
253     static void SetParentSurfaceMatrix(const Drawing::Matrix& parentSurfaceMatrix);
254     static const Drawing::Matrix& GetParentSurfaceMatrix();
255 
256     // overrided surface params
257 #ifndef ROSEN_CROSS_PLATFORM
SetBuffer(const sptr<SurfaceBuffer> & buffer,const Rect & damageRect)258     virtual void SetBuffer(const sptr<SurfaceBuffer>& buffer, const Rect& damageRect) {}
GetBuffer()259     virtual sptr<SurfaceBuffer> GetBuffer() const { return nullptr; }
SetPreBuffer(const sptr<SurfaceBuffer> & preBuffer)260     virtual void SetPreBuffer(const sptr<SurfaceBuffer>& preBuffer) {}
GetPreBuffer()261     virtual sptr<SurfaceBuffer> GetPreBuffer() { return nullptr; }
SetAcquireFence(const sptr<SyncFence> & acquireFence)262     virtual void SetAcquireFence(const sptr<SyncFence>& acquireFence) {}
GetAcquireFence()263     virtual sptr<SyncFence> GetAcquireFence() const { return nullptr; }
GetBufferDamage()264     virtual const Rect& GetBufferDamage() const
265     {
266         static const Rect defaultRect = {};
267         return defaultRect;
268     }
269 #endif
270     virtual const RSLayerInfo& GetLayerInfo() const;
GetAbsDrawRect()271     virtual const RectI& GetAbsDrawRect() const
272     {
273         return absDrawRect_;
274     }
275 
SetAbsDrawRect(RectI & absRect)276     void SetAbsDrawRect(RectI& absRect)
277     {
278         absDrawRect_ = absRect;
279     }
280 
281     // surface params
GetOcclusionVisible()282     virtual bool GetOcclusionVisible() const { return true; }
IsLeashWindow()283     virtual bool IsLeashWindow() const { return true; }
IsAppWindow()284     virtual bool IsAppWindow() const { return false; }
GetHardwareEnabled()285     virtual bool GetHardwareEnabled() const { return false; }
GetHardCursorStatus()286     virtual bool GetHardCursorStatus() const { return false; }
GetLayerCreated()287     virtual bool GetLayerCreated() const { return false; }
GetLastFrameHardwareEnabled()288     virtual bool GetLastFrameHardwareEnabled() const { return false; }
SetLayerCreated(bool layerCreated)289     virtual void SetLayerCreated(bool layerCreated) {}
SetOldDirtyInSurface(const RectI & oldDirtyInSurface)290     virtual void SetOldDirtyInSurface(const RectI& oldDirtyInSurface) {}
GetOldDirtyInSurface()291     virtual RectI GetOldDirtyInSurface() const { return {}; }
292 
293     // overrided by displayNode
GetAllMainAndLeashSurfaceDrawables()294     virtual std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& GetAllMainAndLeashSurfaceDrawables()
295     {
296         static std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr> defaultSurfaceVector;
297         return defaultSurfaceVector;
298     }
299 
GetVisibleRegion()300     virtual Occlusion::Region GetVisibleRegion() const
301     {
302         return {};
303     }
304 
SetRotationChanged(bool changed)305     virtual void SetRotationChanged(bool changed) {}
IsRotationChanged()306     virtual bool IsRotationChanged() const
307     {
308         return false;
309     }
310 
311     virtual const ScreenInfo& GetScreenInfo() const;
GetScreenId()312     virtual uint64_t GetScreenId() const
313     {
314         return 0;
315     }
GetScreenRotation()316     virtual ScreenRotation GetScreenRotation() const
317     {
318         return ScreenRotation::ROTATION_0;
319     }
SetTotalMatrix(const Drawing::Matrix & totalMatrix)320     virtual void SetTotalMatrix(const Drawing::Matrix& totalMatrix) {}
321     virtual const Drawing::Matrix& GetTotalMatrix();
SetNeedClient(bool needClient)322     virtual void SetNeedClient(bool needClient) {}
GetNeedClient()323     virtual bool GetNeedClient() const { return false; }
GetFingerprint()324     virtual bool GetFingerprint() { return false; }
SetFingerprint(bool hasFingerprint)325     virtual void SetFingerprint(bool hasFingerprint) {}
326     // virtual display params
327     virtual DrawableV2::RSRenderNodeDrawableAdapter::WeakPtr GetMirrorSourceDrawable();
GetSecurityDisplay()328     virtual bool GetSecurityDisplay() const { return true; }
329     // canvas drawing node
IsNeedProcess()330     virtual bool IsNeedProcess() const { return false; }
SetNeedProcess(bool isNeedProcess)331     virtual void SetNeedProcess(bool isNeedProcess) {}
332 
SetAbsRotation(float degree)333     void SetAbsRotation(float degree)
334     {
335         absRotation_ = degree;
336     }
337 
GetAbsRotation()338     float GetAbsRotation() const
339     {
340         return absRotation_;
341     }
342 
343 protected:
344     bool needSync_ = false;
345     std::bitset<RSRenderParamsDirtyType::MAX_DIRTY_TYPE> dirtyType_;
346 
347 private:
348     NodeId id_;
349     RSRenderParamsType paramsType_ = RSRenderParamsType::RS_PARAM_DEFAULT;
350     RSRenderNodeType renderNodeType_ = RSRenderNodeType::RS_NODE;
351     Drawing::Matrix matrix_;
352     Drawing::RectF boundsRect_;
353     Drawing::RectF frameRect_;
354     float alpha_ = 1.0f;
355     float globalAlpha_ = 1.0f;
356     // this rect should map display coordination
357     RectF localDrawRect_;
358     RectI absDrawRect_;
359     Vector2f cacheSize_;
360     Gravity frameGravity_ = Gravity::CENTER;
361     bool freezeFlag_ = false;
362     bool childHasVisibleEffect_ = false;
363     bool childHasVisibleFilter_ = false;
364     bool hasSandBox_ = false;
365     bool isDrawingCacheChanged_ = false;
366     bool drawingCacheIncludeProperty_ = false;
367     bool isSecurityLayer_ = false;
368     bool isSkipLayer_ = false;
369     bool shouldPaint_ = false;
370     bool contentEmpty_  = false;
371     std::atomic_bool canvasDrawingNodeSurfaceChanged_ = false;
372     bool alphaOffScreen_ = false;
373     Drawing::Rect shadowRect_;
374     RSDrawingCacheType drawingCacheType_ = RSDrawingCacheType::DISABLED_CACHE;
375     DirtyRegionInfoForDFX dirtyRegionInfoForDFX_;
376     std::shared_ptr<RSFilter> foregroundFilterCache_ = nullptr;
377     bool isOpincRootFlag_ = false;
378     bool isOpincStateChanged_ = false;
379     bool startingWindowFlag_ = false;
380     bool needFilter_ = false;
381     bool effectNodeShouldPaint_ = false;
382     bool hasGlobalCorner_ = false;
383     bool hasBlurFilter_ = false;
384     SurfaceParam surfaceParams_;
385     NodeId firstLevelNodeId_ = INVALID_NODEID;
386     NodeId uifirstRootNodeId_ = INVALID_NODEID;
387     // The angle at which the node rotates about the Z-axis
388     float absRotation_ = 0.f;
389 };
390 } // namespace OHOS::Rosen
391 #endif // RENDER_SERVICE_BASE_PARAMS_RS_RENDER_PARAMS_H
392