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>
28 #include <dali/internal/update/common/resetter-manager.h> ///< For AddInitializeResetter
32 // Memory pool used to allocate new nodes. Memory used by this pool will be released when process dies
33 // or DALI library is unloaded
34 Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Node>& GetNodeMemoryPool()
36 static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Node> gNodeMemoryPool;
37 return gNodeMemoryPool;
40 // keep track of nodes alive, to ensure we have 0 when the process exits or DALi library is unloaded
41 int32_t gNodeCount = 0;
43 // Called when the process is about to exit, Node count should be zero at this point.
44 void __attribute__((destructor)) ShutDown(void)
46 DALI_ASSERT_DEBUG((gNodeCount == 0) && "Node memory leak");
49 } // Unnamed namespace
57 const ColorMode Node::DEFAULT_COLOR_MODE(USE_OWN_MULTIPLY_PARENT_ALPHA);
59 uint32_t Node::mNodeCounter = 0; ///< A counter to provide unique node ids, up-to 4 billion
63 return new(GetNodeMemoryPool().AllocateRawThreadSafe()) Node;
66 void Node::Delete(Node* node)
68 // check we have a node not a derived node
69 if(!node->mIsLayer && !node->mIsCamera)
71 // Manually call the destructor
74 // Mark the memory it used as free in the memory pool
75 GetNodeMemoryPool().FreeThreadSafe(node);
79 // not in the pool, just delete it.
85 : mOrientation(), // Initialized to identity by default
86 mWorldPosition(TRANSFORM_PROPERTY_WORLD_POSITION, Vector3(0.0f, 0.0f, 0.0f)), // Zero initialized by default
87 mWorldScale(TRANSFORM_PROPERTY_WORLD_SCALE, Vector3(1.0f, 1.0f, 1.0f)),
88 mWorldOrientation(), // Initialized to identity by default
93 mWorldColor(Color::WHITE),
94 mUpdateAreaHint(Vector4::ZERO),
95 mClippingSortModifier(0u),
98 mExclusiveRenderTask(nullptr),
103 mDirtyFlags(NodePropertyFlags::ALL),
104 mDrawMode(DrawMode::NORMAL),
105 mColorMode(DEFAULT_COLOR_MODE),
106 mClippingMode(ClippingMode::DISABLED),
110 mPositionUsesAnchorPoint(true),
112 mUpdateAreaChanged(false)
121 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
123 mTransformManagerData.Manager()->RemoveTransform(mTransformManagerData.Id());
131 void Node::OnDestroy()
133 // Animators, Constraints etc. should be disconnected from the child's properties.
134 PropertyOwner::Destroy();
137 uint32_t Node::GetId() const
142 void Node::CreateTransform(SceneGraph::TransformManager* transformManager)
144 // Create a new transform
145 mTransformManagerData.mManager = transformManager;
146 TransformId createdTransformId = transformManager->CreateTransform();
148 // Set whether the position should use the anchor point
149 transformManager->SetPositionUsesAnchorPoint(createdTransformId, mPositionUsesAnchorPoint);
151 // Set TransformId after initialize done.
152 mTransformManagerData.mId = createdTransformId;
155 void Node::SetRoot(bool isRoot)
157 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
162 bool Node::IsAnimationPossible() const
164 return mIsConnectedToSceneGraph;
167 void Node::AddInitializeResetter(ResetterManager& manager) const
169 manager.AddNodeResetter(*this);
172 void Node::ConnectChild(Node* childNode)
174 DALI_ASSERT_ALWAYS(this != childNode);
175 DALI_ASSERT_ALWAYS(IsRoot() || nullptr != mParent); // Parent should be connected first
176 DALI_ASSERT_ALWAYS(!childNode->IsRoot() && nullptr == childNode->GetParent()); // Child should be disconnected
178 childNode->SetParent(*this);
180 // Everything should be reinherited when reconnected to scene-graph
181 childNode->SetAllDirtyFlags();
183 // Add the node to the end of the child list.
184 mChildren.PushBack(childNode);
186 // Inform property observers of new connection
187 childNode->ConnectToSceneGraph();
190 void Node::DisconnectChild(BufferIndex updateBufferIndex, Node& childNode)
192 DALI_ASSERT_ALWAYS(this != &childNode);
193 DALI_ASSERT_ALWAYS(childNode.GetParent() == this);
195 // Find the childNode and remove it
196 Node* found(nullptr);
198 const NodeIter endIter = mChildren.End();
199 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
201 Node* current = *iter;
202 if(current == &childNode)
205 mChildren.Erase(iter); // order matters here
206 break; // iter is no longer valid
209 DALI_ASSERT_ALWAYS(nullptr != found);
211 found->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
214 void Node::AddRenderer(const RendererKey& renderer)
216 // If it is the first renderer added, make sure the world transform will be calculated
217 // in the next update as world transform is not computed if node has no renderers.
218 if(mRenderers.Empty())
220 mDirtyFlags |= NodePropertyFlags::TRANSFORM;
224 // Check that it has not been already added.
225 for(auto&& existingRenderer : mRenderers)
227 if(existingRenderer == renderer)
229 // Renderer is already in the list.
237 mRenderers.PushBack(renderer);
240 void Node::RemoveRenderer(const RendererKey& renderer)
242 RendererContainer::SizeType rendererCount(mRenderers.Size());
243 for(RendererContainer::SizeType i = 0; i < rendererCount; ++i)
245 if(mRenderers[i] == renderer)
247 renderer->DetachFromNodeDataProvider(*this);
250 mRenderers.Erase(mRenderers.Begin() + i);
256 NodePropertyFlags Node::GetDirtyFlags() const
258 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
259 NodePropertyFlags flags = mDirtyFlags;
261 // Check whether the visible property has changed
262 if(!mVisible.IsClean())
264 flags |= NodePropertyFlags::VISIBLE;
267 // Check whether the color property has changed
268 if(!mColor.IsClean())
270 flags |= NodePropertyFlags::COLOR;
276 NodePropertyFlags Node::GetInheritedDirtyFlags(NodePropertyFlags parentFlags) const
278 // Size is not inherited. VisibleFlag is inherited
279 static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
280 using UnderlyingType = typename std::underlying_type<NodePropertyFlags>::type;
282 return static_cast<NodePropertyFlags>(static_cast<UnderlyingType>(mDirtyFlags) |
283 (static_cast<UnderlyingType>(parentFlags) & static_cast<UnderlyingType>(InheritedDirtyFlags)));
286 void Node::ResetDirtyFlags(BufferIndex updateBufferIndex)
288 mDirtyFlags = NodePropertyFlags::NOTHING;
290 mUpdateAreaChanged = false;
293 void Node::SetParent(Node& parentNode)
295 DALI_ASSERT_ALWAYS(this != &parentNode);
296 DALI_ASSERT_ALWAYS(!mIsRoot);
297 DALI_ASSERT_ALWAYS(mParent == nullptr);
299 mParent = &parentNode;
301 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
303 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), parentNode.GetTransformId());
307 void Node::RecursiveDisconnectFromSceneGraph(BufferIndex updateBufferIndex)
309 DALI_ASSERT_ALWAYS(!mIsRoot);
310 DALI_ASSERT_ALWAYS(mParent != nullptr);
312 const NodeIter endIter = mChildren.End();
313 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
315 (*iter)->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
318 // Animators, Constraints etc. should be disconnected from the child's properties.
319 PropertyOwner::DisconnectFromSceneGraph(updateBufferIndex);
321 // Remove back-pointer to parent
324 // Remove all child pointers
327 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
329 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), INVALID_TRANSFORM_ID);
333 uint32_t Node::GetMemoryPoolCapacity()
335 return GetNodeMemoryPool().GetCapacity();
338 } // namespace SceneGraph
340 } // namespace Internal