• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "pipeline/rs_node_map.h"
17 
18 #include "ui/rs_base_node.h"
19 #include "ui/rs_canvas_node.h"
20 
21 namespace OHOS {
22 namespace Rosen {
RSNodeMap()23 RSNodeMap::RSNodeMap()
24 {
25     // create animation fallback node
26     auto fallback_node = new RSCanvasNode(false);
27     fallback_node->SetId(0);
28     animationFallbackNode_.reset(fallback_node);
29     nodeMap_.emplace(0, animationFallbackNode_);
30 }
31 
~RSNodeMap()32 RSNodeMap::~RSNodeMap()  noexcept
33 {
34     nodeMap_.clear();
35     animationFallbackNode_ = nullptr;
36 }
37 
MutableInstance()38 RSNodeMap& RSNodeMap::MutableInstance()
39 {
40     static RSNodeMap nodeMap;
41     return nodeMap;
42 }
43 
Instance()44 const RSNodeMap& RSNodeMap::Instance()
45 {
46     return MutableInstance();
47 }
48 
RegisterNode(const RSBaseNode::SharedPtr & nodePtr)49 bool RSNodeMap::RegisterNode(const RSBaseNode::SharedPtr& nodePtr)
50 {
51     std::unique_lock<std::mutex> lock(mutex_);
52     NodeId id = nodePtr->GetId();
53     auto itr = nodeMap_.find(id);
54     if (itr != nodeMap_.end()) {
55         return false;
56     }
57     RSBaseNode::WeakPtr ptr(nodePtr);
58     nodeMap_.emplace(id, ptr);
59     return true;
60 }
61 
UnregisterNode(NodeId id)62 void RSNodeMap::UnregisterNode(NodeId id)
63 {
64     std::unique_lock<std::mutex> lock(mutex_);
65     auto itr = nodeMap_.find(id);
66     if (itr != nodeMap_.end()) {
67         nodeMap_.erase(itr);
68     }
69 }
70 
71 template<>
GetNode(NodeId id) const72 const std::shared_ptr<RSBaseNode> RSNodeMap::GetNode<RSBaseNode>(NodeId id) const
73 {
74     std::unique_lock<std::mutex> lock(mutex_);
75     auto itr = nodeMap_.find(id);
76     if (itr == nodeMap_.end()) {
77         return nullptr;
78     }
79     return itr->second.lock();
80 }
81 
GetAnimationFallbackNode() const82 const std::shared_ptr<RSNode> RSNodeMap::GetAnimationFallbackNode() const
83 {
84     return animationFallbackNode_;
85 }
86 
87 } // namespace Rosen
88 } // namespace OHOS
89