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_NODE_MAP_H
16 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_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 "ui/rs_base_node.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 class RSNode;
28 
29 class RSC_EXPORT RSNodeMap final {
30 public:
31     static const RSNodeMap& Instance();
32     static RSNodeMap& MutableInstance();
33 
34     bool RegisterNode(const std::shared_ptr<RSBaseNode>& nodePtr);
35     bool RegisterNodeInstanceId(NodeId id, int32_t instanceId);
36     void UnregisterNode(NodeId id);
37     /**
38      * Storing a mapping relationship of an animation with the node it originally belongs to and the instance it
39      * should be played on.
40      * This relationship need to be stored as the ownership of an animation is transferred from an RSNode to the
41      * RootNode in the destructor of RSNode, else this information would have been lost.
42      */
43     bool RegisterAnimationInstanceId(AnimationId animId, NodeId id, int32_t instanceId);
44     // Removing the stored relationship of an animation. Should be used when an RSAnimation object is freed
45     void UnregisterAnimation(AnimationId animId);
46     // Get RSNode with type T, return nullptr if not found or type mismatch
47     template<typename T = RSBaseNode>
GetNode(NodeId id)48     const std::shared_ptr<T> GetNode(NodeId id) const
49     {
50         return RSBaseNode::ReinterpretCast<T>(GetNode<RSBaseNode>(id));
51     }
52     template<>
53     const std::shared_ptr<RSBaseNode> GetNode(NodeId id) const;
54     int32_t GetNodeInstanceId(NodeId id) const;
55     // Used to get instanceId for node which is already freed.
56     int32_t GetInstanceIdForReleasedNode(NodeId id) const;
57 
58     const std::shared_ptr<RSNode> GetAnimationFallbackNode() const;
59 
60 private:
61     explicit RSNodeMap();
62     ~RSNodeMap() noexcept;
63     RSNodeMap(const RSNodeMap&) = delete;
64     RSNodeMap(const RSNodeMap&&) = delete;
65     RSNodeMap& operator=(const RSNodeMap&) = delete;
66     RSNodeMap& operator=(const RSNodeMap&&) = delete;
67 
68 private:
69     mutable std::mutex mutex_;
70     std::unordered_map<NodeId, std::weak_ptr<RSBaseNode>> nodeMap_;
71     std::unordered_map<NodeId, int32_t> nodeIdMap_;
72     std::unordered_map<AnimationId, std::pair<NodeId, int32_t>> animationNodeIdInstanceIdMap_;
73     std::shared_ptr<RSNode> animationFallbackNode_;
74 };
75 } // namespace Rosen
76 } // namespace OHOS
77 
78 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_NODE_MAP_H
79