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