1 /* 2 * Copyright (c) 2025 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_v2.h" 17 18 #include <atomic> 19 20 #include "pipeline/rs_node_map.h" 21 #include "platform/common/rs_log.h" 22 #include "ui/rs_base_node.h" 23 24 namespace OHOS { 25 namespace Rosen { 26 27 namespace { 28 static std::atomic_bool g_valid = false; 29 } 30 RSNodeMapV2()31RSNodeMapV2::RSNodeMapV2() 32 { 33 g_valid.store(true); 34 } 35 ~RSNodeMapV2()36RSNodeMapV2::~RSNodeMapV2() noexcept 37 { 38 std::unique_lock<std::mutex> lock(mutex_); 39 nodeMapNew_.clear(); 40 g_valid.store(false); 41 } 42 RegisterNode(const RSBaseNode::SharedPtr & nodePtr)43bool RSNodeMapV2::RegisterNode(const RSBaseNode::SharedPtr& nodePtr) 44 { 45 std::unique_lock<std::mutex> lock(mutex_); 46 NodeId id = nodePtr->GetId(); 47 auto itr = nodeMapNew_.find(id); 48 if (itr != nodeMapNew_.end()) { 49 ROSEN_LOGW("RSNodeMapV2::RegisterNode: node id %{public}" PRIu64 " already exists", id); 50 return false; 51 } 52 RSBaseNode::WeakPtr ptr(nodePtr); 53 nodeMapNew_.emplace(id, ptr); 54 return true; 55 } 56 UnregisterNode(NodeId id)57void RSNodeMapV2::UnregisterNode(NodeId id) 58 { 59 if (!g_valid.load()) { 60 return; 61 } 62 std::unique_lock<std::mutex> lock(mutex_); 63 auto itr = nodeMapNew_.find(id); 64 if (itr != nodeMapNew_.end()) { 65 nodeMapNew_.erase(itr); 66 } else { 67 ROSEN_LOGW("RSNodeMapV2::UnregisterNode: node id %{public}" PRIu64 " not found", id); 68 } 69 } 70 71 template<> GetNode(NodeId id) const72const std::shared_ptr<RSBaseNode> RSNodeMapV2::GetNode<RSBaseNode>(NodeId id) const 73 { 74 if (!g_valid.load()) { 75 return nullptr; 76 } 77 std::unique_lock<std::mutex> lock(mutex_); 78 auto itr = nodeMapNew_.find(id); 79 if (itr == nodeMapNew_.end()) { 80 return nullptr; 81 } 82 return itr->second.lock(); 83 } 84 } // namespace Rosen 85 } // namespace OHOS 86