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> gNodeMemoryPool;
34 // keep track of nodes alive, to ensure we have 0 when the process exits or DALi library is unloaded
35 int32_t gNodeCount = 0;
37 // Called when the process is about to exit, Node count should be zero at this point.
38 void __attribute__((destructor)) ShutDown(void)
40 DALI_ASSERT_DEBUG((gNodeCount == 0) && "Node memory leak");
43 } // Unnamed namespace
51 const ColorMode Node::DEFAULT_COLOR_MODE(USE_OWN_MULTIPLY_PARENT_ALPHA);
53 uint32_t Node::mNodeCounter = 0; ///< A counter to provide unique node ids, up-to 4 billion
57 return new(gNodeMemoryPool.AllocateRawThreadSafe()) Node;
60 void Node::Delete(Node* node)
62 // check we have a node not a derived node
63 if(!node->mIsLayer && !node->mIsCamera)
65 // Manually call the destructor
68 // Mark the memory it used as free in the memory pool
69 gNodeMemoryPool.FreeThreadSafe(node);
73 // not in the pool, just delete it.
79 : mOrientation(), // Initialized to identity by default
80 mWorldPosition(TRANSFORM_PROPERTY_WORLD_POSITION, Vector3(0.0f, 0.0f, 0.0f)), // Zero initialized by default
81 mWorldScale(TRANSFORM_PROPERTY_WORLD_SCALE, Vector3(1.0f, 1.0f, 1.0f)),
82 mWorldOrientation(), // Initialized to identity by default
87 mWorldColor(Color::WHITE),
88 mUpdateAreaHint(Vector4::ZERO),
89 mClippingSortModifier(0u),
92 mExclusiveRenderTask(nullptr),
97 mDirtyFlags(NodePropertyFlags::ALL),
98 mDrawMode(DrawMode::NORMAL),
99 mColorMode(DEFAULT_COLOR_MODE),
100 mClippingMode(ClippingMode::DISABLED),
104 mPositionUsesAnchorPoint(true),
106 mUpdateAreaChanged(false)
115 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
117 mTransformManagerData.Manager()->RemoveTransform(mTransformManagerData.Id());
125 void Node::OnDestroy()
127 // Animators, Constraints etc. should be disconnected from the child's properties.
128 PropertyOwner::Destroy();
131 uint32_t Node::GetId() const
136 void Node::CreateTransform(SceneGraph::TransformManager* transformManager)
138 // Create a new transform
139 mTransformManagerData.mManager = transformManager;
140 TransformId createdTransformId = transformManager->CreateTransform();
142 // Set whether the position should use the anchor point
143 transformManager->SetPositionUsesAnchorPoint(createdTransformId, mPositionUsesAnchorPoint);
145 // Set TransformId after initialize done.
146 mTransformManagerData.mId = createdTransformId;
149 void Node::SetRoot(bool isRoot)
151 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
156 bool Node::IsAnimationPossible() const
158 return mIsConnectedToSceneGraph;
161 void Node::ConnectChild(Node* childNode)
163 DALI_ASSERT_ALWAYS(this != childNode);
164 DALI_ASSERT_ALWAYS(IsRoot() || nullptr != mParent); // Parent should be connected first
165 DALI_ASSERT_ALWAYS(!childNode->IsRoot() && nullptr == childNode->GetParent()); // Child should be disconnected
167 childNode->SetParent(*this);
169 // Everything should be reinherited when reconnected to scene-graph
170 childNode->SetAllDirtyFlags();
172 // Add the node to the end of the child list.
173 mChildren.PushBack(childNode);
175 // Inform property observers of new connection
176 childNode->ConnectToSceneGraph();
179 void Node::DisconnectChild(BufferIndex updateBufferIndex, Node& childNode)
181 DALI_ASSERT_ALWAYS(this != &childNode);
182 DALI_ASSERT_ALWAYS(childNode.GetParent() == this);
184 // Find the childNode and remove it
185 Node* found(nullptr);
187 const NodeIter endIter = mChildren.End();
188 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
190 Node* current = *iter;
191 if(current == &childNode)
194 mChildren.Erase(iter); // order matters here
195 break; // iter is no longer valid
198 DALI_ASSERT_ALWAYS(nullptr != found);
200 found->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
203 void Node::AddRenderer(const RendererKey& renderer)
205 // If it is the first renderer added, make sure the world transform will be calculated
206 // in the next update as world transform is not computed if node has no renderers.
207 if(mRenderers.Empty())
209 mDirtyFlags |= NodePropertyFlags::TRANSFORM;
213 // Check that it has not been already added.
214 for(auto&& existingRenderer : mRenderers)
216 if(existingRenderer == renderer)
218 // Renderer is already in the list.
226 mRenderers.PushBack(renderer);
229 void Node::RemoveRenderer(const RendererKey& renderer)
231 RendererContainer::SizeType rendererCount(mRenderers.Size());
232 for(RendererContainer::SizeType i = 0; i < rendererCount; ++i)
234 if(mRenderers[i] == renderer)
237 mRenderers.Erase(mRenderers.Begin() + i);
243 NodePropertyFlags Node::GetDirtyFlags() const
245 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
246 NodePropertyFlags flags = mDirtyFlags;
248 // Check whether the visible property has changed
249 if(!mVisible.IsClean())
251 flags |= NodePropertyFlags::VISIBLE;
254 // Check whether the color property has changed
255 if(!mColor.IsClean())
257 flags |= NodePropertyFlags::COLOR;
263 NodePropertyFlags Node::GetInheritedDirtyFlags(NodePropertyFlags parentFlags) const
265 // Size is not inherited. VisibleFlag is inherited
266 static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
267 using UnderlyingType = typename std::underlying_type<NodePropertyFlags>::type;
269 return static_cast<NodePropertyFlags>(static_cast<UnderlyingType>(mDirtyFlags) |
270 (static_cast<UnderlyingType>(parentFlags) & static_cast<UnderlyingType>(InheritedDirtyFlags)));
273 void Node::ResetDirtyFlags(BufferIndex updateBufferIndex)
275 mDirtyFlags = NodePropertyFlags::NOTHING;
277 mUpdateAreaChanged = false;
280 void Node::UpdateUniformHash(BufferIndex bufferIndex)
282 uint64_t hash = 0xc70f6907UL;
283 for(uint32_t i = 0u, count = mUniformMaps.Count(); i < count; ++i)
285 hash = mUniformMaps[i].propertyPtr->Hash(bufferIndex, hash);
287 if(mUniformsHash != hash)
289 mUniformsHash = hash;
294 void Node::SetParent(Node& parentNode)
296 DALI_ASSERT_ALWAYS(this != &parentNode);
297 DALI_ASSERT_ALWAYS(!mIsRoot);
298 DALI_ASSERT_ALWAYS(mParent == nullptr);
300 mParent = &parentNode;
302 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
304 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), parentNode.GetTransformId());
308 void Node::RecursiveDisconnectFromSceneGraph(BufferIndex updateBufferIndex)
310 DALI_ASSERT_ALWAYS(!mIsRoot);
311 DALI_ASSERT_ALWAYS(mParent != nullptr);
313 const NodeIter endIter = mChildren.End();
314 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
316 (*iter)->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
319 // Animators, Constraints etc. should be disconnected from the child's properties.
320 PropertyOwner::DisconnectFromSceneGraph(updateBufferIndex);
322 // Remove back-pointer to parent
325 // Remove all child pointers
328 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
330 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), INVALID_TRANSFORM_ID);
334 uint32_t Node::GetMemoryPoolCapacity()
336 return gNodeMemoryPool.GetCapacity();
339 } // namespace SceneGraph
341 } // namespace Internal