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_BASE_PIPELINE_RS_RENDER_NODE_MAP_H
16 #define RENDER_SERVICE_BASE_PIPELINE_RS_RENDER_NODE_MAP_H
17 
18 #include <mutex>
19 #include <unordered_map>
20 
21 #include "common/rs_common_def.h"
22 #include "common/rs_macros.h"
23 #include "pipeline/rs_base_render_node.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 class RSRenderNode;
28 class RSSurfaceRenderNode;
29 class RSDisplayRenderNode;
30 class RSCanvasDrawingRenderNode;
31 class RSB_EXPORT RSRenderNodeMap final {
32 public:
33     bool RegisterRenderNode(const std::shared_ptr<RSBaseRenderNode>& nodePtr);
34     bool RegisterDisplayRenderNode(const std::shared_ptr<RSDisplayRenderNode>& nodePtr);
35     void UnregisterRenderNode(NodeId id);
36 
37     // Get RenderNode with type T, return nullptr if not found or type mismatch
38     template<typename T = RSBaseRenderNode>
GetRenderNode(NodeId id)39     const std::shared_ptr<T> GetRenderNode(NodeId id) const
40     {
41         auto renderNode = GetRenderNode<RSBaseRenderNode>(id);
42         return RSBaseRenderNode::ReinterpretCast<T>(renderNode);
43     }
44     template<>
45     const std::shared_ptr<RSBaseRenderNode> GetRenderNode(NodeId id) const;
46 
47     const std::shared_ptr<RSRenderNode> GetAnimationFallbackNode() const;
48 
49     const std::string GetSelfDrawSurfaceNameByPid(pid_t nodePid) const;
50 
51     bool ContainPid(pid_t pid) const;
52     void FilterNodeByPid(pid_t pid);
53     void MoveRenderNodeMap(
54         std::shared_ptr<std::unordered_map<NodeId, std::shared_ptr<RSBaseRenderNode>>> subRenderNodeMap, pid_t pid);
55     void TraversalNodes(std::function<void (const std::shared_ptr<RSBaseRenderNode>&)> func) const;
56     void TraverseSurfaceNodes(std::function<void (const std::shared_ptr<RSSurfaceRenderNode>&)> func) const;
57     void TraverseDisplayNodes(std::function<void (const std::shared_ptr<RSDisplayRenderNode>&)> func) const;
58     void TraverseCanvasDrawingNodes(std::function<void (const std::shared_ptr<RSCanvasDrawingRenderNode>&)> func) const;
59     const std::unordered_map<NodeId, std::shared_ptr<RSSurfaceRenderNode>>& GetResidentSurfaceNodeMap() const;
60     bool IsResidentProcessNode(NodeId id) const;
61     bool IsUIExtensionSurfaceNode(NodeId id) const;
62 
63     NodeId GetEntryViewNodeId() const;
64     NodeId GetWallPaperViewNodeId() const;
65     NodeId GetScreenLockWindowNodeId() const;
66     NodeId GetNegativeScreenNodeId() const;
67     void ObtainScreenLockWindowNodeId(const std::shared_ptr<RSSurfaceRenderNode> surfaceNode);
68     void ObtainLauncherNodeId(const std::shared_ptr<RSSurfaceRenderNode> surfaceNode);
69 
70     uint32_t GetVisibleLeashWindowCount() const;
71     uint64_t GetSize() const;
72 
73     // call from main thread
74     void AddOffTreeNode(NodeId nodeId);
75     void RemoveOffTreeNode(NodeId nodeId);
76     std::unordered_map<NodeId, bool>&& GetAndClearPurgeableNodeIds();
77 private:
78     explicit RSRenderNodeMap();
79     ~RSRenderNodeMap() = default;
80     RSRenderNodeMap(const RSRenderNodeMap&) = delete;
81     RSRenderNodeMap(const RSRenderNodeMap&&) = delete;
82     RSRenderNodeMap& operator=(const RSRenderNodeMap&) = delete;
83     RSRenderNodeMap& operator=(const RSRenderNodeMap&&) = delete;
84 
85 private:
86     std::unordered_map<pid_t, std::unordered_map<NodeId, std::shared_ptr<RSBaseRenderNode>>> renderNodeMap_;
87     std::unordered_map<NodeId, std::shared_ptr<RSSurfaceRenderNode>> surfaceNodeMap_;
88     std::unordered_map<NodeId, std::shared_ptr<RSSurfaceRenderNode>> residentSurfaceNodeMap_;
89     std::unordered_map<NodeId, std::shared_ptr<RSDisplayRenderNode>> displayNodeMap_;
90     std::unordered_map<NodeId, std::shared_ptr<RSCanvasDrawingRenderNode>> canvasDrawingNodeMap_;
91     std::unordered_map<NodeId, bool> purgeableNodeMap_;
92 
93     NodeId entryViewNodeId_ = 0;
94     NodeId negativeScreenNodeId_ = 0;
95     NodeId wallpaperViewNodeId_ = 0;
96     NodeId screenLockWindowNodeId_ = 0;
97 
98     void Initialize(const std::weak_ptr<RSContext>& context);
99     std::weak_ptr<RSContext> context_;
100 
101     void AddUIExtensionSurfaceNode(const std::shared_ptr<RSSurfaceRenderNode> surfaceNode);
102     void RemoveUIExtensionSurfaceNode(const std::shared_ptr<RSSurfaceRenderNode> surfaceNode);
103     std::unordered_set<NodeId> uiExtensionSurfaceNodes_;
104     mutable std::mutex uiExtensionSurfaceNodesMutex_;
105 
106     friend class RSContext;
107     friend class RSMainThread;
108 #ifdef RS_PROFILER_ENABLED
109     friend class RSProfiler;
110 #endif
111 };
112 } // namespace Rosen
113 } // namespace OHOS
114 
115 #endif // RENDER_SERVICE_BASE_PIPELINE_RS_RENDER_NODE_MAP_H
116