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 #ifndef RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_NODE_MAP_H 16 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_NODE_MAP_H 17 18 #include <mutex> 19 #include <unordered_map> 20 21 #include "common/rs_common_def.h" 22 #include "ui/rs_base_node.h" 23 24 namespace OHOS { 25 namespace Rosen { 26 class RSNode; 27 28 class RSNodeMap final { 29 public: 30 static const RSNodeMap& Instance(); 31 static RSNodeMap& MutableInstance(); 32 33 bool RegisterNode(const std::shared_ptr<RSBaseNode>& nodePtr); 34 void UnregisterNode(NodeId id); 35 36 // Get RSNode with type T, return nullptr if not found or type mismatch 37 template<typename T = RSBaseNode> GetNode(NodeId id)38 const std::shared_ptr<T> GetNode(NodeId id) const 39 { 40 return RSBaseNode::ReinterpretCast<T>(GetNode<RSBaseNode>(id)); 41 } 42 template<> 43 const std::shared_ptr<RSBaseNode> GetNode(NodeId id) const; 44 45 const std::shared_ptr<RSNode> GetAnimationFallbackNode() const; 46 47 private: 48 explicit RSNodeMap(); 49 ~RSNodeMap() noexcept; 50 RSNodeMap(const RSNodeMap&) = delete; 51 RSNodeMap(const RSNodeMap&&) = delete; 52 RSNodeMap& operator=(const RSNodeMap&) = delete; 53 RSNodeMap& operator=(const RSNodeMap&&) = delete; 54 55 private: 56 mutable std::mutex mutex_; 57 std::unordered_map<NodeId, std::weak_ptr<RSBaseNode>> nodeMap_; 58 std::shared_ptr<RSNode> animationFallbackNode_; 59 }; 60 } // namespace Rosen 61 } // namespace OHOS 62 63 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_NODE_MAP_H 64