1 /*
2  * Copyright (c) 2021-2023 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 #ifndef RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_DISPLAY_RENDER_NODE_H
16 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_DISPLAY_RENDER_NODE_H
17 
18 #include <memory>
19 #include <mutex>
20 #include "common/rs_common_def.h"
21 
22 #ifndef ROSEN_CROSS_PLATFORM
23 #include <ibuffer_consumer_listener.h>
24 #include <iconsumer_surface.h>
25 #include <surface.h>
26 #include "sync_fence.h"
27 #endif
28 
29 #include "common/rs_macros.h"
30 #include "common/rs_occlusion_region.h"
31 #include "memory/rs_memory_track.h"
32 #include "pipeline/rs_render_node.h"
33 #include "pipeline/rs_surface_handler.h"
34 #include <screen_manager/screen_types.h>
35 #include "screen_manager/rs_screen_info.h"
36 #ifdef NEW_RENDER_CONTEXT
37 #include "rs_render_surface.h"
38 #else
39 #include "platform/drawing/rs_surface.h"
40 #endif
41 
42 namespace OHOS {
43 namespace Rosen {
44 class RSSurfaceRenderNode;
45 class RSB_EXPORT RSDisplayRenderNode : public RSRenderNode {
46 public:
47     struct ScreenRenderParams
48     {
49         ScreenInfo screenInfo;
50         std::map<ScreenId, bool> displayHasSecSurface;
51         std::map<ScreenId, bool> displayHasSkipSurface;
52         std::map<ScreenId, bool> displayHasProtectedSurface;
53         std::map<ScreenId, bool> displaySpecailSurfaceChanged;
54         std::map<ScreenId, bool> hasCaptureWindow;
55     };
56 
57     enum CompositeType {
58         UNI_RENDER_COMPOSITE = 0,
59         UNI_RENDER_MIRROR_COMPOSITE,
60         UNI_RENDER_EXPAND_COMPOSITE,
61         HARDWARE_COMPOSITE,
62         SOFTWARE_COMPOSITE
63     };
64     using WeakPtr = std::weak_ptr<RSDisplayRenderNode>;
65     using SharedPtr = std::shared_ptr<RSDisplayRenderNode>;
66     static inline constexpr RSRenderNodeType Type = RSRenderNodeType::DISPLAY_NODE;
67 
68     ~RSDisplayRenderNode() override;
69     void SetIsOnTheTree(bool flag, NodeId instanceRootNodeId = INVALID_NODEID,
70         NodeId firstLevelNodeId = INVALID_NODEID, NodeId cacheNodeId = INVALID_NODEID,
71         NodeId uifirstRootNodeId = INVALID_NODEID, NodeId displayNodeId = INVALID_NODEID) override;
72 
SetScreenId(uint64_t screenId)73     void SetScreenId(uint64_t screenId)
74     {
75         screenId_ = screenId;
76     }
77 
GetScreenId()78     uint64_t GetScreenId() const
79     {
80         return screenId_;
81     }
82 
SetRogSize(uint32_t rogWidth,uint32_t rogHeight)83     void SetRogSize(uint32_t rogWidth, uint32_t rogHeight)
84     {
85         rogWidth_ = rogWidth;
86         rogHeight_ = rogHeight;
87     }
88 
GetRogWidth()89     uint32_t GetRogWidth() const
90     {
91         return rogWidth_;
92     }
93 
GetRogHeight()94     uint32_t GetRogHeight() const
95     {
96         return rogHeight_;
97     }
98 
SetDisplayOffset(int32_t offsetX,int32_t offsetY)99     void SetDisplayOffset(int32_t offsetX, int32_t offsetY)
100     {
101         offsetX_ = offsetX;
102         offsetY_ = offsetY;
103     }
104 
GetDisplayOffsetX()105     int32_t GetDisplayOffsetX() const
106     {
107         return offsetX_;
108     }
109 
GetDisplayOffsetY()110     int32_t GetDisplayOffsetY() const
111     {
112         return offsetY_;
113     }
114 
GetFingerprint()115     bool GetFingerprint() const
116     {
117         return hasFingerprint_;
118     }
119 
SetFingerprint(bool hasFingerprint)120     void SetFingerprint(bool hasFingerprint)
121     {
122         hasFingerprint_ = hasFingerprint;
123     }
124 
SetScreenRotation(const ScreenRotation & screenRotation)125     void SetScreenRotation(const ScreenRotation& screenRotation)
126     {
127         screenRotation_ = screenRotation;
128     }
129 
GetScreenRotation()130     ScreenRotation GetScreenRotation()
131     {
132         return screenRotation_;
133     }
134 
IsMirrorScreen()135     bool IsMirrorScreen() const
136     {
137         return isMirrorScreen_;
138     }
139 
SetIsMirrorScreen(bool isMirrorScreen)140     void SetIsMirrorScreen(bool isMirrorScreen)
141     {
142         isMirrorScreen_ = isMirrorScreen;
143     }
144 
145     void CollectSurface(
146         const std::shared_ptr<RSBaseRenderNode>& node, std::vector<RSBaseRenderNode::SharedPtr>& vec,
147         bool isUniRender, bool onlyFirstLevel) override;
148     void QuickPrepare(const std::shared_ptr<RSNodeVisitor>& visitor) override;
149     void Prepare(const std::shared_ptr<RSNodeVisitor>& visitor) override;
150     void Process(const std::shared_ptr<RSNodeVisitor>& visitor) override;
151 
GetType()152     RSRenderNodeType GetType() const override
153     {
154         return RSRenderNodeType::DISPLAY_NODE;
155     }
156 
157     bool IsMirrorDisplay() const;
158 
159     void SetCompositeType(CompositeType type);
160     CompositeType GetCompositeType() const;
161     void SetForceSoftComposite(bool flag);
162     bool IsForceSoftComposite() const;
163     void SetMirrorSource(SharedPtr node);
164     void ResetMirrorSource();
165     void SetIsMirrorDisplay(bool isMirror);
166     void SetSecurityDisplay(bool isSecurityDisplay);
167     bool GetSecurityDisplay() const;
168     void SetDisplayGlobalZOrder(float zOrder);
169     bool SkipFrame(uint32_t refreshRate, uint32_t skipFrameInterval) override;
170     void SetBootAnimation(bool isBootAnimation) override;
171     bool GetBootAnimation() const override;
GetMirrorSource()172     WeakPtr GetMirrorSource() const
173     {
174         return mirrorSource_;
175     }
176 
HasDisappearingTransition(bool)177     bool HasDisappearingTransition(bool) const override
178     {
179         return false;
180     }
181     // Use in vulkan parallel rendering
SetIsParallelDisplayNode(bool isParallelDisplayNode)182     void SetIsParallelDisplayNode(bool isParallelDisplayNode)
183     {
184         isParallelDisplayNode_ = isParallelDisplayNode;
185     }
186 
IsParallelDisplayNode()187     bool IsParallelDisplayNode() const
188     {
189         return isParallelDisplayNode_;
190     }
191 
192     ScreenRotation GetRotation() const;
193 
GetDirtyManager()194     std::shared_ptr<RSDirtyRegionManager> GetDirtyManager() const
195     {
196         return dirtyManager_;
197     }
198     void UpdateDisplayDirtyManager(int32_t bufferage, bool useAlignedDirtyRegion = false);
199     void ClearCurrentSurfacePos();
UpdateSurfaceNodePos(NodeId id,RectI rect)200     void UpdateSurfaceNodePos(NodeId id, RectI rect)
201     {
202         currentFrameSurfacePos_[id] = rect;
203     }
204 
AddSurfaceNodePosByDescZOrder(NodeId id,RectI rect)205     void AddSurfaceNodePosByDescZOrder(NodeId id, RectI rect)
206     {
207         currentFrameSurfacesByDescZOrder_.emplace_back(id, rect);
208     }
209 
AddSecurityLayer(NodeId id)210     void AddSecurityLayer(NodeId id)
211     {
212         securityLayerList_.emplace_back(id);
213     }
214 
ClearSecurityLayerList()215     void ClearSecurityLayerList()
216     {
217         securityLayerList_.clear();
218     }
219 
GetSecurityLayerList()220     const std::vector<NodeId>& GetSecurityLayerList()
221     {
222         return securityLayerList_;
223     }
224 
SetSecurityExemption(bool isSecurityExemption)225     void SetSecurityExemption(bool isSecurityExemption)
226     {
227         isSecurityExemption_ = isSecurityExemption;
228     }
229 
GetSecurityExemption()230     bool GetSecurityExemption() const
231     {
232         return isSecurityExemption_;
233     }
234 
GetLastFrameSurfacePos(NodeId id)235     RectI GetLastFrameSurfacePos(NodeId id)
236     {
237         if (lastFrameSurfacePos_.count(id) == 0) {
238             return {};
239         }
240         return lastFrameSurfacePos_[id];
241     }
242 
GetCurrentFrameSurfacePos(NodeId id)243     RectI GetCurrentFrameSurfacePos(NodeId id)
244     {
245         if (currentFrameSurfacePos_.count(id) == 0) {
246             return {};
247         }
248         return currentFrameSurfacePos_[id];
249     }
250 
GetSurfaceChangedRects()251     const std::vector<RectI> GetSurfaceChangedRects() const
252     {
253         std::vector<RectI> rects;
254         for (const auto& lastFrameSurfacePo : lastFrameSurfacePos_) {
255             if (currentFrameSurfacePos_.find(lastFrameSurfacePo.first) == currentFrameSurfacePos_.end()) {
256                 rects.emplace_back(lastFrameSurfacePo.second);
257             }
258         }
259         for (const auto& currentFrameSurfacePo : currentFrameSurfacePos_) {
260             if (lastFrameSurfacePos_.find(currentFrameSurfacePo.first) == lastFrameSurfacePos_.end()) {
261                 rects.emplace_back(currentFrameSurfacePo.second);
262             }
263         }
264         return rects;
265     }
266 
GetCurAllSurfaces()267     std::vector<RSBaseRenderNode::SharedPtr>& GetCurAllSurfaces()
268     {
269         return curAllSurfaces_;
270     }
271 
272     void UpdateRenderParams() override;
273     void UpdatePartialRenderParams();
274     void UpdateScreenRenderParams(ScreenRenderParams& screenRenderParams);
275     void UpdateOffscreenRenderParams(bool needOffscreen);
276     void RecordMainAndLeashSurfaces(RSBaseRenderNode::SharedPtr surface);
GetAllMainAndLeashSurfaces()277     std::vector<RSBaseRenderNode::SharedPtr>& GetAllMainAndLeashSurfaces() { return curMainAndLeashSurfaceNodes_;}
GetCurAllSurfaces(bool onlyFirstLevel)278     std::vector<RSBaseRenderNode::SharedPtr>& GetCurAllSurfaces(bool onlyFirstLevel)
279     {
280         return onlyFirstLevel ? curAllFirstLevelSurfaces_ : curAllSurfaces_;
281     }
282 
283     void UpdateRotation();
284     bool IsRotationChanged() const;
IsLastRotationChanged()285     bool IsLastRotationChanged() const {
286         return lastRotationChanged;
287     }
GetPreRotationStatus()288     bool GetPreRotationStatus() const {
289         return preRotationStatus_;
290     }
GetCurRotationStatus()291     bool GetCurRotationStatus() const {
292         return curRotationStatus_;
293     }
IsFirstTimeToProcessor()294     bool IsFirstTimeToProcessor() const {
295         return isFirstTimeToProcessor_;
296     }
297 
SetOriginScreenRotation(const ScreenRotation & rotate)298     void SetOriginScreenRotation(const ScreenRotation& rotate) {
299         originScreenRotation_ = rotate;
300         isFirstTimeToProcessor_ = false;
301     }
GetOriginScreenRotation()302     ScreenRotation GetOriginScreenRotation() const {
303         return originScreenRotation_;
304     }
305 
SetInitMatrix(const Drawing::Matrix & matrix)306     void SetInitMatrix(const Drawing::Matrix& matrix) {
307         initMatrix_ = matrix;
308         isFirstTimeToProcessor_ = false;
309     }
310 
GetInitMatrix()311     const Drawing::Matrix& GetInitMatrix() const {
312         return initMatrix_;
313     }
314 
GetOffScreenCacheImgForCapture()315     std::shared_ptr<Drawing::Image> GetOffScreenCacheImgForCapture() {
316         return offScreenCacheImgForCapture_;
317     }
SetOffScreenCacheImgForCapture(std::shared_ptr<Drawing::Image> offScreenCacheImgForCapture)318     void SetOffScreenCacheImgForCapture(std::shared_ptr<Drawing::Image> offScreenCacheImgForCapture) {
319         offScreenCacheImgForCapture_ = offScreenCacheImgForCapture;
320     }
321 
322     void SetHDRPresent(bool hdrPresent);
323 
324     void SetBrightnessRatio(float brightnessRatio);
325 
326     void SetColorSpace(const GraphicColorGamut& newColorSpace);
327     GraphicColorGamut GetColorSpace() const;
328 
GetDirtySurfaceNodeMap()329     std::map<NodeId, std::shared_ptr<RSSurfaceRenderNode>>& GetDirtySurfaceNodeMap()
330     {
331         return dirtySurfaceNodeMap_;
332     }
333 
334     void SetMainAndLeashSurfaceDirty(bool isDirty);
335 
336     // Use in MultiLayersPerf
GetSurfaceCountForMultiLayersPerf()337     size_t GetSurfaceCountForMultiLayersPerf() const
338     {
339         return surfaceCountForMultiLayersPerf_;
340     }
341 
SetScbNodePid(const std::vector<int32_t> & oldScbPids,int32_t currentScbPid)342     void SetScbNodePid(const std::vector<int32_t>& oldScbPids, int32_t currentScbPid)
343     {
344         oldScbPids_ = oldScbPids;
345         currentScbPid_ = currentScbPid;
346         isNeedWaitNewScbPid_ = true;
347         isFullChildrenListValid_ = false;
348     }
349 
GetOldScbPids()350     std::vector<int32_t> GetOldScbPids() const
351     {
352         return oldScbPids_;
353     }
354 
GetCurrentScbPid()355     int32_t GetCurrentScbPid() const
356     {
357         return currentScbPid_;
358     }
359 
360     ChildrenListSharedPtr GetSortedChildren() const override;
361 
362     Occlusion::Region GetDisappearedSurfaceRegionBelowCurrent(NodeId currentSurface) const;
363 
UpdateZoomState(bool state)364     void UpdateZoomState(bool state)
365     {
366         preZoomState_ = curZoomState_;
367         curZoomState_ = state;
368     }
369 
370     bool IsZoomStateChange() const;
371 protected:
372     void OnSync() override;
373 private:
374     explicit RSDisplayRenderNode(
375         NodeId id, const RSDisplayNodeConfig& config, const std::weak_ptr<RSContext>& context = {});
376     void InitRenderParams() override;
377     void HandleCurMainAndLeashSurfaceNodes();
378     // vector of sufacenodes will records dirtyregions by itself
379     std::vector<RSBaseRenderNode::SharedPtr> curMainAndLeashSurfaceNodes_;
380     CompositeType compositeType_ { HARDWARE_COMPOSITE };
381     bool isMirrorScreen_ = false;
382     ScreenRotation screenRotation_ = ScreenRotation::ROTATION_0;
383     ScreenRotation originScreenRotation_ = ScreenRotation::ROTATION_0;
384     uint64_t screenId_ = 0;
385     int32_t offsetX_ = 0;
386     int32_t offsetY_ = 0;
387     uint32_t rogWidth_ = 0;
388     uint32_t rogHeight_ = 0;
389     bool forceSoftComposite_ { false };
390     bool isMirroredDisplay_ = false;
391     bool isSecurityDisplay_ = false;
392     WeakPtr mirrorSource_;
393     float lastRotation_ = 0.f;
394     bool preRotationStatus_ = false;
395     bool curRotationStatus_ = false;
396     bool lastRotationChanged = false;
397     Drawing::Matrix initMatrix_;
398     bool isFirstTimeToProcessor_ = true;
399     bool hasFingerprint_ = false;
400     GraphicColorGamut colorSpace_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
401 
402     std::map<NodeId, RectI> lastFrameSurfacePos_;
403     std::map<NodeId, RectI> currentFrameSurfacePos_;
404     std::vector<std::pair<NodeId, RectI>> lastFrameSurfacesByDescZOrder_;
405     std::vector<std::pair<NodeId, RectI>> currentFrameSurfacesByDescZOrder_;
406     std::shared_ptr<RSDirtyRegionManager> dirtyManager_ = nullptr;
407     std::vector<std::string> windowsName_;
408 
409     std::vector<NodeId> securityLayerList_;
410     bool isSecurityExemption_ = false;
411 
412     std::vector<RSBaseRenderNode::SharedPtr> curAllSurfaces_;
413     std::vector<RSBaseRenderNode::SharedPtr> curAllFirstLevelSurfaces_;
414     std::mutex mtx_;
415 
416     // Use in screen recording optimization
417     std::shared_ptr<Drawing::Image> offScreenCacheImgForCapture_ = nullptr;
418 
419     // Use in vulkan parallel rendering
420     bool isParallelDisplayNode_ = false;
421 
422     // Use in MultiLayersPerf
423     size_t surfaceCountForMultiLayersPerf_ = 0;
424 
425     std::map<NodeId, std::shared_ptr<RSSurfaceRenderNode>> dirtySurfaceNodeMap_;
426 
427     std::vector<int32_t> oldScbPids_ {};
428     int32_t currentScbPid_ = -1;
429     mutable bool isNeedWaitNewScbPid_ = false;
430     mutable std::shared_ptr<std::vector<std::shared_ptr<RSRenderNode>>> currentChildrenList_ =
431         std::make_shared<std::vector<std::shared_ptr<RSRenderNode>>>();
432 
433     friend class DisplayNodeCommandHelper;
434     int64_t lastRefreshTime_ = 0;
435 
436     bool curZoomState_ = false;
437     bool preZoomState_ = false;
438 };
439 } // namespace Rosen
440 } // namespace OHOS
441 
442 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_DISPLAY_RENDER_NODE_H
443