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),
118 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
120 mTransformManagerData.Manager()->RemoveTransform(mTransformManagerData.Id());
128 void Node::OnDestroy()
130 // Animators, Constraints etc. should be disconnected from the child's properties.
131 PropertyOwner::Destroy();
134 uint32_t Node::GetId() const
139 void Node::CreateTransform(SceneGraph::TransformManager* transformManager)
141 // Create a new transform
142 mTransformManagerData.mManager = transformManager;
143 TransformId createdTransformId = transformManager->CreateTransform();
145 // Set whether the position should use the anchor point
146 transformManager->SetPositionUsesAnchorPoint(createdTransformId, mPositionUsesAnchorPoint);
148 // Set TransformId after initialize done.
149 mTransformManagerData.mId = createdTransformId;
152 void Node::SetRoot(bool isRoot)
154 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
159 bool Node::IsAnimationPossible() const
161 return mIsConnectedToSceneGraph;
164 void Node::ConnectChild(Node* childNode)
166 DALI_ASSERT_ALWAYS(this != childNode);
167 DALI_ASSERT_ALWAYS(IsRoot() || nullptr != mParent); // Parent should be connected first
168 DALI_ASSERT_ALWAYS(!childNode->IsRoot() && nullptr == childNode->GetParent()); // Child should be disconnected
170 childNode->SetParent(*this);
172 // Everything should be reinherited when reconnected to scene-graph
173 childNode->SetAllDirtyFlags();
175 // Add the node to the end of the child list.
176 mChildren.PushBack(childNode);
178 // Inform property observers of new connection
179 childNode->ConnectToSceneGraph();
182 void Node::DisconnectChild(BufferIndex updateBufferIndex, Node& childNode)
184 DALI_ASSERT_ALWAYS(this != &childNode);
185 DALI_ASSERT_ALWAYS(childNode.GetParent() == this);
187 // Find the childNode and remove it
188 Node* found(nullptr);
190 const NodeIter endIter = mChildren.End();
191 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
193 Node* current = *iter;
194 if(current == &childNode)
197 mChildren.Erase(iter); // order matters here
198 break; // iter is no longer valid
201 DALI_ASSERT_ALWAYS(nullptr != found);
203 found->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
206 void Node::AddRenderer(const RendererKey& renderer)
208 // If it is the first renderer added, make sure the world transform will be calculated
209 // in the next update as world transform is not computed if node has no renderers.
210 if(mRenderers.Empty())
212 mDirtyFlags |= NodePropertyFlags::TRANSFORM;
216 // Check that it has not been already added.
217 for(auto&& existingRenderer : mRenderers)
219 if(existingRenderer == renderer)
221 // Renderer is already in the list.
229 mRenderers.PushBack(renderer);
232 void Node::RemoveRenderer(const RendererKey& renderer)
234 RendererContainer::SizeType rendererCount(mRenderers.Size());
235 for(RendererContainer::SizeType i = 0; i < rendererCount; ++i)
237 if(mRenderers[i] == renderer)
240 mRenderers.Erase(mRenderers.Begin() + i);
246 NodePropertyFlags Node::GetDirtyFlags() const
248 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
249 NodePropertyFlags flags = mDirtyFlags;
251 // Check whether the visible property has changed
252 if(!mVisible.IsClean())
254 flags |= NodePropertyFlags::VISIBLE;
257 // Check whether the color property has changed
258 if(!mColor.IsClean())
260 flags |= NodePropertyFlags::COLOR;
263 // Check whether the update area property has changed
264 if(!mUpdateAreaHint.IsClean())
266 flags |= NodePropertyFlags::TRANSFORM;
272 NodePropertyFlags Node::GetInheritedDirtyFlags(NodePropertyFlags parentFlags) const
274 // Size is not inherited. VisibleFlag is inherited
275 static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
276 using UnderlyingType = typename std::underlying_type<NodePropertyFlags>::type;
278 return static_cast<NodePropertyFlags>(static_cast<UnderlyingType>(mDirtyFlags) |
279 (static_cast<UnderlyingType>(parentFlags) & static_cast<UnderlyingType>(InheritedDirtyFlags)));
282 void Node::ResetDirtyFlags(BufferIndex updateBufferIndex)
284 mDirtyFlags = NodePropertyFlags::NOTHING;
287 void Node::UpdateUniformHash(BufferIndex bufferIndex)
289 uint64_t hash = 0xc70f6907UL;
290 for(uint32_t i = 0u, count = mUniformMaps.Count(); i < count; ++i)
292 hash = mUniformMaps[i].propertyPtr->Hash(bufferIndex, hash);
294 if(mUniformsHash != hash)
296 mUniformsHash = hash;
301 void Node::SetParent(Node& parentNode)
303 DALI_ASSERT_ALWAYS(this != &parentNode);
304 DALI_ASSERT_ALWAYS(!mIsRoot);
305 DALI_ASSERT_ALWAYS(mParent == nullptr);
307 mParent = &parentNode;
309 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
311 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), parentNode.GetTransformId());
315 void Node::RecursiveDisconnectFromSceneGraph(BufferIndex updateBufferIndex)
317 DALI_ASSERT_ALWAYS(!mIsRoot);
318 DALI_ASSERT_ALWAYS(mParent != nullptr);
320 const NodeIter endIter = mChildren.End();
321 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
323 (*iter)->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
326 // Animators, Constraints etc. should be disconnected from the child's properties.
327 PropertyOwner::DisconnectFromSceneGraph(updateBufferIndex);
329 // Remove back-pointer to parent
332 // Remove all child pointers
335 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
337 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), INVALID_TRANSFORM_ID);
341 uint32_t Node::GetMemoryPoolCapacity()
343 return GetNodeMemoryPool().GetCapacity();
346 } // namespace SceneGraph
348 } // namespace Internal