1 /* 2 * Copyright (c) 2021-2022 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 <atomic> 19 20 #include "ui/rs_base_node.h" 21 #include "ui/rs_canvas_node.h" 22 23 namespace OHOS { 24 namespace Rosen { 25 namespace { 26 static std::atomic_bool g_instance_valid = false; 27 } 28 RSNodeMap()29RSNodeMap::RSNodeMap() 30 { 31 // create animation fallback node 32 auto fallback_node = new RSCanvasNode(false); 33 fallback_node->SetId(0); 34 animationFallbackNode_.reset(fallback_node); 35 nodeMap_.emplace(0, animationFallbackNode_); 36 g_instance_valid.store(true); 37 } 38 ~RSNodeMap()39RSNodeMap::~RSNodeMap() noexcept 40 { 41 animationFallbackNode_ = nullptr; 42 std::unique_lock<std::mutex> lock(mutex_); 43 nodeMap_.clear(); 44 g_instance_valid.store(false); 45 } 46 MutableInstance()47RSNodeMap& RSNodeMap::MutableInstance() 48 { 49 static RSNodeMap nodeMap; 50 return nodeMap; 51 } 52 Instance()53const RSNodeMap& RSNodeMap::Instance() 54 { 55 return MutableInstance(); 56 } 57 RegisterNode(const RSBaseNode::SharedPtr & nodePtr)58bool RSNodeMap::RegisterNode(const RSBaseNode::SharedPtr& nodePtr) 59 { 60 std::unique_lock<std::mutex> lock(mutex_); 61 NodeId id = nodePtr->GetId(); 62 auto itr = nodeMap_.find(id); 63 if (itr != nodeMap_.end()) { 64 ROSEN_LOGW("RSNodeMap::RegisterNode: node id %" PRIu64 " already exists", id); 65 return false; 66 } 67 RSBaseNode::WeakPtr ptr(nodePtr); 68 nodeMap_.emplace(id, ptr); 69 return true; 70 } 71 UnregisterNode(NodeId id)72void RSNodeMap::UnregisterNode(NodeId id) 73 { 74 if (!g_instance_valid.load()) { 75 return; 76 } 77 std::unique_lock<std::mutex> lock(mutex_); 78 auto itr = nodeMap_.find(id); 79 if (itr != nodeMap_.end()) { 80 nodeMap_.erase(itr); 81 } else { 82 ROSEN_LOGW("RSNodeMap::UnregisterNode: node id %" PRIu64 " not found", id); 83 } 84 } 85 86 template<> GetNode(NodeId id) const87const std::shared_ptr<RSBaseNode> RSNodeMap::GetNode<RSBaseNode>(NodeId id) const 88 { 89 if (!g_instance_valid.load()) { 90 return nullptr; 91 } 92 std::unique_lock<std::mutex> lock(mutex_); 93 auto itr = nodeMap_.find(id); 94 if (itr == nodeMap_.end()) { 95 return nullptr; 96 } 97 return itr->second.lock(); 98 } 99 GetAnimationFallbackNode() const100const std::shared_ptr<RSNode> RSNodeMap::GetAnimationFallbackNode() const 101 { 102 return animationFallbackNode_; 103 } 104 105 } // namespace Rosen 106 } // namespace OHOS 107