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_PIPELINE_RS_BASE_RENDER_NODE_H 16 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_BASE_RENDER_NODE_H 17 18 #include <atomic> 19 #include <list> 20 #include <memory> 21 22 #include "common/rs_common_def.h" 23 #include "common/rs_macros.h" 24 #include "common/rs_rect.h" 25 namespace OHOS { 26 namespace Rosen { 27 class RSContext; 28 class RSNodeVisitor; 29 class RSCommand; 30 31 class RSB_EXPORT RSBaseRenderNode : public std::enable_shared_from_this<RSBaseRenderNode> { 32 public: 33 using WeakPtr = std::weak_ptr<RSBaseRenderNode>; 34 using SharedPtr = std::shared_ptr<RSBaseRenderNode>; 35 static inline constexpr RSRenderNodeType Type = RSRenderNodeType::BASE_NODE; GetType()36 virtual RSRenderNodeType GetType() const 37 { 38 return Type; 39 } 40 id_(id)41 explicit RSBaseRenderNode(NodeId id, std::weak_ptr<RSContext> context = {}) : id_(id), context_(context) {}; id_(id)42 explicit RSBaseRenderNode(NodeId id, bool isOnTheTree = false, std::weak_ptr<RSContext> context = {}) : id_(id), 43 isOnTheTree_(isOnTheTree), context_(context) {}; 44 virtual ~RSBaseRenderNode() = default; 45 46 void AddChild(SharedPtr child, int index = -1); 47 void MoveChild(SharedPtr child, int index); 48 void RemoveChild(SharedPtr child, bool skipTransition = false); 49 void ClearChildren(); 50 void RemoveFromTree(bool skipTransition = false); 51 52 // Add/RemoveCrossParentChild only used as: the child is under multiple parents(e.g. a window cross multi-screens) 53 void AddCrossParentChild(const SharedPtr& child, int32_t index = -1); 54 void RemoveCrossParentChild(const SharedPtr& child, const WeakPtr& newParent); 55 56 virtual void CollectSurface(const std::shared_ptr<RSBaseRenderNode>& node, 57 std::vector<RSBaseRenderNode::SharedPtr>& vec, 58 bool isUniRender); 59 virtual void Prepare(const std::shared_ptr<RSNodeVisitor>& visitor); 60 virtual void Process(const std::shared_ptr<RSNodeVisitor>& visitor); 61 62 // return if any animation is running Animate(int64_t timestamp)63 virtual std::pair<bool, bool> Animate(int64_t timestamp) 64 { 65 return { false, false }; 66 } 67 68 WeakPtr GetParent() const; 69 virtual void ResetParent(); 70 GetId()71 NodeId GetId() const 72 { 73 return id_; 74 } 75 76 void SetIsOnTheTree(bool flag); IsOnTheTree()77 bool IsOnTheTree() const 78 { 79 return isOnTheTree_; 80 } 81 82 const std::list<SharedPtr>& GetSortedChildren(); 83 ResetSortedChildren()84 void ResetSortedChildren() 85 { 86 sortedChildren_.clear(); 87 } 88 GetChildren()89 const std::list<WeakPtr>& GetChildren() 90 { 91 return children_; 92 } 93 GetChildrenCount()94 uint32_t GetChildrenCount() const 95 { 96 return children_.size(); 97 } 98 99 void DumpTree(int32_t depth, std::string& ou) const; 100 101 virtual bool HasDisappearingTransition(bool recursive = true) const 102 { 103 if (recursive == false) { 104 return false; 105 } else { 106 auto parent = GetParent().lock(); 107 return parent ? parent->HasDisappearingTransition(true) : false; 108 } 109 } 110 SetTunnelHandleChange(bool change)111 void SetTunnelHandleChange(bool change) 112 { 113 isTunnelHandleChange_ = change; 114 } 115 GetTunnelHandleChange()116 bool GetTunnelHandleChange() const 117 { 118 return isTunnelHandleChange_; 119 } 120 121 // type-safe reinterpret_cast 122 template<typename T> IsInstanceOf()123 bool IsInstanceOf() 124 { 125 constexpr uint32_t targetType = static_cast<uint32_t>(T::Type); 126 return (static_cast<uint32_t>(GetType()) & targetType) == targetType; 127 } 128 template<typename T> ReinterpretCast(std::shared_ptr<RSBaseRenderNode> node)129 static std::shared_ptr<T> ReinterpretCast(std::shared_ptr<RSBaseRenderNode> node) 130 { 131 return node ? node->ReinterpretCastTo<T>() : nullptr; 132 } 133 template<typename T> ReinterpretCastTo()134 std::shared_ptr<T> ReinterpretCastTo() 135 { 136 return (IsInstanceOf<T>()) ? std::static_pointer_cast<T>(shared_from_this()) : nullptr; 137 } 138 HasChildrenOutOfRect()139 bool HasChildrenOutOfRect() const 140 { 141 return hasChildrenOutOfRect_; 142 } 143 UpdateChildrenOutOfRectFlag(bool flag)144 void UpdateChildrenOutOfRectFlag(bool flag) 145 { 146 hasChildrenOutOfRect_ = flag; 147 } 148 ClearPaintOutOfParentRect()149 void ClearPaintOutOfParentRect() 150 { 151 paintOutOfParentRect_.Clear(); 152 } 153 GetPaintOutOfParentRect()154 const RectI& GetPaintOutOfParentRect() const 155 { 156 return paintOutOfParentRect_; 157 } 158 UpdatePaintOutOfParentRect(const RectI & r)159 void UpdatePaintOutOfParentRect(const RectI& r) 160 { 161 if (paintOutOfParentRect_.IsEmpty()) { 162 paintOutOfParentRect_ = r; 163 } else { 164 paintOutOfParentRect_ = paintOutOfParentRect_.JoinRect(r); 165 } 166 } 167 ResetHasRemovedChild()168 inline void ResetHasRemovedChild() 169 { 170 hasRemovedChild_ = false; 171 } 172 HasRemovedChild()173 inline bool HasRemovedChild() const 174 { 175 return hasRemovedChild_; 176 } 177 ResetChildrenRect()178 inline void ResetChildrenRect() 179 { 180 childrenRect_ = RectI(); 181 } 182 GetChildrenRect()183 inline RectI GetChildrenRect() const 184 { 185 return childrenRect_; 186 } 187 188 // accumulate all valid children's area 189 void UpdateChildrenRect(const RectI& subRect); 190 protected: 191 enum class NodeDirty { 192 CLEAN = 0, 193 DIRTY, 194 }; 195 virtual bool IsDirty() const; 196 void SetClean(); 197 void SetDirty(); 198 199 void DumpNodeType(std::string& out) const; 200 GetContext()201 const std::weak_ptr<RSContext> GetContext() const 202 { 203 return context_; 204 } 205 206 static void SendCommandFromRT(std::unique_ptr<RSCommand>& command, NodeId nodeId); 207 private: 208 NodeId id_; 209 210 WeakPtr parent_; 211 void SetParent(WeakPtr parent); 212 bool isOnTheTree_ = false; 213 // accumulate all children's region rect for dirty merging when any child has been removed 214 bool hasRemovedChild_ = false; 215 RectI childrenRect_; 216 217 std::list<WeakPtr> children_; 218 std::list<std::pair<SharedPtr, uint32_t>> disappearingChildren_; 219 220 std::list<SharedPtr> sortedChildren_; 221 void GenerateSortedChildren(); 222 223 const std::weak_ptr<RSContext> context_; 224 NodeDirty dirtyStatus_ = NodeDirty::DIRTY; 225 friend class RSRenderPropertyBase; 226 friend class RSRenderTransition; 227 std::atomic<bool> isTunnelHandleChange_ = false; 228 bool hasChildrenOutOfRect_ = false; 229 RectI paintOutOfParentRect_; 230 231 void InternalRemoveSelfFromDisappearingChildren(); 232 }; 233 } // namespace Rosen 234 } // namespace OHOS 235 236 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_BASE_RENDER_NODE_H 237