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