2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/update/nodes/node.h>
22 #include <dali/internal/common/internal-constants.h>
23 #include <dali/internal/common/memory-pool-object-allocator.h>
24 #include <dali/internal/update/common/discard-queue.h>
25 #include <dali/public-api/common/constants.h>
26 #include <dali/public-api/common/dali-common.h>
30 // Memory pool used to allocate new nodes. Memory used by this pool will be released when process dies
31 // or DALI library is unloaded
32 Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Node>& GetNodeMemoryPool()
34 static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Node> gNodeMemoryPool;
35 return gNodeMemoryPool;
38 // keep track of nodes alive, to ensure we have 0 when the process exits or DALi library is unloaded
39 int32_t gNodeCount = 0;
41 // Called when the process is about to exit, Node count should be zero at this point.
42 void __attribute__((destructor)) ShutDown(void)
44 DALI_ASSERT_DEBUG((gNodeCount == 0) && "Node memory leak");
47 } // Unnamed namespace
55 const ColorMode Node::DEFAULT_COLOR_MODE(USE_OWN_MULTIPLY_PARENT_ALPHA);
57 uint32_t Node::mNodeCounter = 0; ///< A counter to provide unique node ids, up-to 4 billion
61 return new(GetNodeMemoryPool().AllocateRawThreadSafe()) Node;
64 void Node::Delete(Node* node)
66 // check we have a node not a derived node
67 if(!node->mIsLayer && !node->mIsCamera)
69 // Manually call the destructor
72 // Mark the memory it used as free in the memory pool
73 GetNodeMemoryPool().FreeThreadSafe(node);
77 // not in the pool, just delete it.
83 : mOrientation(), // Initialized to identity by default
84 mWorldPosition(TRANSFORM_PROPERTY_WORLD_POSITION, Vector3(0.0f, 0.0f, 0.0f)), // Zero initialized by default
85 mWorldScale(TRANSFORM_PROPERTY_WORLD_SCALE, Vector3(1.0f, 1.0f, 1.0f)),
86 mWorldOrientation(), // Initialized to identity by default
91 mWorldColor(Color::WHITE),
92 mUpdateAreaHint(Vector4::ZERO),
93 mClippingSortModifier(0u),
96 mExclusiveRenderTask(nullptr),
101 mDirtyFlags(NodePropertyFlags::ALL),
102 mDrawMode(DrawMode::NORMAL),
103 mColorMode(DEFAULT_COLOR_MODE),
104 mClippingMode(ClippingMode::DISABLED),
108 mPositionUsesAnchorPoint(true),
110 mUpdateAreaChanged(false)
119 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
121 mTransformManagerData.Manager()->RemoveTransform(mTransformManagerData.Id());
129 void Node::OnDestroy()
131 // Animators, Constraints etc. should be disconnected from the child's properties.
132 PropertyOwner::Destroy();
135 uint32_t Node::GetId() const
140 void Node::CreateTransform(SceneGraph::TransformManager* transformManager)
142 // Create a new transform
143 mTransformManagerData.mManager = transformManager;
144 TransformId createdTransformId = transformManager->CreateTransform();
146 // Set whether the position should use the anchor point
147 transformManager->SetPositionUsesAnchorPoint(createdTransformId, mPositionUsesAnchorPoint);
149 // Set TransformId after initialize done.
150 mTransformManagerData.mId = createdTransformId;
153 void Node::SetRoot(bool isRoot)
155 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
160 bool Node::IsAnimationPossible() const
162 return mIsConnectedToSceneGraph;
165 void Node::ConnectChild(Node* childNode)
167 DALI_ASSERT_ALWAYS(this != childNode);
168 DALI_ASSERT_ALWAYS(IsRoot() || nullptr != mParent); // Parent should be connected first
169 DALI_ASSERT_ALWAYS(!childNode->IsRoot() && nullptr == childNode->GetParent()); // Child should be disconnected
171 childNode->SetParent(*this);
173 // Everything should be reinherited when reconnected to scene-graph
174 childNode->SetAllDirtyFlags();
176 // Add the node to the end of the child list.
177 mChildren.PushBack(childNode);
179 // Inform property observers of new connection
180 childNode->ConnectToSceneGraph();
183 void Node::DisconnectChild(BufferIndex updateBufferIndex, Node& childNode)
185 DALI_ASSERT_ALWAYS(this != &childNode);
186 DALI_ASSERT_ALWAYS(childNode.GetParent() == this);
188 // Find the childNode and remove it
189 Node* found(nullptr);
191 const NodeIter endIter = mChildren.End();
192 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
194 Node* current = *iter;
195 if(current == &childNode)
198 mChildren.Erase(iter); // order matters here
199 break; // iter is no longer valid
202 DALI_ASSERT_ALWAYS(nullptr != found);
204 found->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
207 void Node::AddRenderer(const RendererKey& renderer)
209 // If it is the first renderer added, make sure the world transform will be calculated
210 // in the next update as world transform is not computed if node has no renderers.
211 if(mRenderers.Empty())
213 mDirtyFlags |= NodePropertyFlags::TRANSFORM;
217 // Check that it has not been already added.
218 for(auto&& existingRenderer : mRenderers)
220 if(existingRenderer == renderer)
222 // Renderer is already in the list.
230 mRenderers.PushBack(renderer);
233 void Node::RemoveRenderer(const RendererKey& renderer)
235 RendererContainer::SizeType rendererCount(mRenderers.Size());
236 for(RendererContainer::SizeType i = 0; i < rendererCount; ++i)
238 if(mRenderers[i] == renderer)
241 mRenderers.Erase(mRenderers.Begin() + i);
247 NodePropertyFlags Node::GetDirtyFlags() const
249 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
250 NodePropertyFlags flags = mDirtyFlags;
252 // Check whether the visible property has changed
253 if(!mVisible.IsClean())
255 flags |= NodePropertyFlags::VISIBLE;
258 // Check whether the color property has changed
259 if(!mColor.IsClean())
261 flags |= NodePropertyFlags::COLOR;
267 NodePropertyFlags Node::GetInheritedDirtyFlags(NodePropertyFlags parentFlags) const
269 // Size is not inherited. VisibleFlag is inherited
270 static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
271 using UnderlyingType = typename std::underlying_type<NodePropertyFlags>::type;
273 return static_cast<NodePropertyFlags>(static_cast<UnderlyingType>(mDirtyFlags) |
274 (static_cast<UnderlyingType>(parentFlags) & static_cast<UnderlyingType>(InheritedDirtyFlags)));
277 void Node::ResetDirtyFlags(BufferIndex updateBufferIndex)
279 mDirtyFlags = NodePropertyFlags::NOTHING;
281 mUpdateAreaChanged = false;
284 void Node::SetParent(Node& parentNode)
286 DALI_ASSERT_ALWAYS(this != &parentNode);
287 DALI_ASSERT_ALWAYS(!mIsRoot);
288 DALI_ASSERT_ALWAYS(mParent == nullptr);
290 mParent = &parentNode;
292 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
294 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), parentNode.GetTransformId());
298 void Node::RecursiveDisconnectFromSceneGraph(BufferIndex updateBufferIndex)
300 DALI_ASSERT_ALWAYS(!mIsRoot);
301 DALI_ASSERT_ALWAYS(mParent != nullptr);
303 const NodeIter endIter = mChildren.End();
304 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
306 (*iter)->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
309 // Animators, Constraints etc. should be disconnected from the child's properties.
310 PropertyOwner::DisconnectFromSceneGraph(updateBufferIndex);
312 // Remove back-pointer to parent
315 // Remove all child pointers
318 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
320 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), INVALID_TRANSFORM_ID);
324 uint32_t Node::GetMemoryPoolCapacity()
326 return GetNodeMemoryPool().GetCapacity();
329 } // namespace SceneGraph
331 } // namespace Internal