1 /* 2 * Copyright (c) 2021-2023 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_UI_RS_BASE_NODE_H 16 #define RENDER_SERVICE_CLIENT_CORE_UI_RS_BASE_NODE_H 17 18 #include <memory> 19 #include <unistd.h> 20 #include <vector> 21 22 #include "common/rs_common_def.h" 23 #include "platform/common/rs_system_properties.h" 24 25 namespace OHOS { 26 namespace Rosen { 27 class RSTransitionEffect; 28 29 class RSC_EXPORT RSBaseNode : public std::enable_shared_from_this<RSBaseNode> { 30 public: 31 using WeakPtr = std::weak_ptr<RSBaseNode>; 32 using SharedPtr = std::shared_ptr<RSBaseNode>; 33 static inline constexpr RSUINodeType Type = RSUINodeType::BASE_NODE; GetType()34 virtual RSUINodeType GetType() const 35 { 36 return Type; 37 } 38 39 virtual ~RSBaseNode(); 40 41 virtual void AddChild(SharedPtr child, int index = -1); 42 void MoveChild(SharedPtr child, int index); 43 virtual void RemoveChild(SharedPtr child); 44 void RemoveFromTree(); 45 virtual void ClearChildren(); GetChildren()46 const std::vector<NodeId>& GetChildren() const 47 { 48 return children_; 49 } 50 51 // Add/RemoveCrossParentChild only used as: the child is under multiple parents(e.g. a window cross multi-screens) 52 void AddCrossParentChild(SharedPtr child, int index); 53 void RemoveCrossParentChild(SharedPtr child, NodeId newParentId); 54 GetId()55 NodeId GetId() const 56 { 57 return id_; 58 } 59 GetFollowType()60 virtual FollowType GetFollowType() const 61 { 62 return FollowType::NONE; 63 } 64 65 bool IsInstanceOf(RSUINodeType type) const; 66 template<typename T> 67 RSC_EXPORT bool IsInstanceOf() const; 68 69 // type-safe reinterpret_cast 70 template<typename T> ReinterpretCast(const std::shared_ptr<RSBaseNode> & node)71 static std::shared_ptr<T> ReinterpretCast(const std::shared_ptr<RSBaseNode>& node) 72 { 73 return node ? node->ReinterpretCastTo<T>() : nullptr; 74 } 75 template<typename T> ReinterpretCastTo()76 std::shared_ptr<T> ReinterpretCastTo() 77 { 78 return (IsInstanceOf<T>()) ? std::static_pointer_cast<T>(shared_from_this()) : nullptr; 79 } 80 virtual std::string DumpNode(int depth) const; 81 82 protected: 83 bool isRenderServiceNode_; 84 bool skipDestroyCommandInDestructor_ = false; 85 86 explicit RSBaseNode(bool isRenderServiceNode); 87 explicit RSBaseNode(bool isRenderServiceNode, NodeId id); 88 RSBaseNode(const RSBaseNode&) = delete; 89 RSBaseNode(const RSBaseNode&&) = delete; 90 RSBaseNode& operator=(const RSBaseNode&) = delete; 91 RSBaseNode& operator=(const RSBaseNode&&) = delete; 92 93 // this id is ONLY used in hierarchy operation commands, this may differ from id_ when the node is a proxy node. GetHierarchyCommandNodeId()94 virtual NodeId GetHierarchyCommandNodeId() const 95 { 96 return id_; 97 } 98 OnAddChildren()99 virtual void OnAddChildren() {} OnRemoveChildren()100 virtual void OnRemoveChildren() {} 101 SharedPtr GetParent(); 102 SetId(const NodeId & id)103 void SetId(const NodeId& id) 104 { 105 id_ = id; 106 } 107 108 bool IsUniRenderEnabled() const; 109 bool IsRenderServiceNode() const; 110 bool NeedSendExtraCommand() const; 111 112 private: 113 static NodeId GenerateId(); 114 static void InitUniRenderEnabled(); 115 NodeId id_; 116 NodeId parent_ = 0; 117 std::vector<NodeId> children_; 118 void SetParent(NodeId parent); 119 void RemoveChildById(NodeId childId); 120 121 friend class RSUIDirector; 122 }; 123 } // namespace Rosen 124 } // namespace OHOS 125 126 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_BASE_NODE_H 127