2 * Copyright (c) 2022 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
83 mUpdateAreaHint(Vector4::ZERO),
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
88 mWorldColor(Color::WHITE),
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),
114 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
116 mTransformManagerData.Manager()->RemoveTransform(mTransformManagerData.Id());
124 void Node::OnDestroy()
126 // Animators, Constraints etc. should be disconnected from the child's properties.
127 PropertyOwner::Destroy();
130 uint32_t Node::GetId() const
135 void Node::CreateTransform(SceneGraph::TransformManager* transformManager)
137 //Create a new transform
138 mTransformManagerData.mManager = transformManager;
139 TransformId createdTransformId = transformManager->CreateTransform();
141 //Initialize all the animatable properties
142 mPosition.Initialize(&mTransformManagerData);
143 mScale.Initialize(&mTransformManagerData);
144 mOrientation.Initialize(&mTransformManagerData);
145 mSize.Initialize(&mTransformManagerData);
146 mParentOrigin.Initialize(&mTransformManagerData);
147 mAnchorPoint.Initialize(&mTransformManagerData);
149 //Initialize all the input properties
150 mWorldPosition.Initialize(&mTransformManagerData);
151 mWorldScale.Initialize(&mTransformManagerData);
152 mWorldOrientation.Initialize(&mTransformManagerData);
153 mWorldMatrix.Initialize(&mTransformManagerData);
155 //Set whether the position should use the anchor point
156 transformManager->SetPositionUsesAnchorPoint(createdTransformId, mPositionUsesAnchorPoint);
158 //Set TransformId after initialize done.
159 mTransformManagerData.mId = createdTransformId;
162 void Node::SetRoot(bool isRoot)
164 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
169 bool Node::IsAnimationPossible() const
171 return mIsConnectedToSceneGraph;
174 void Node::ConnectChild(Node* childNode)
176 DALI_ASSERT_ALWAYS(this != childNode);
177 DALI_ASSERT_ALWAYS(IsRoot() || nullptr != mParent); // Parent should be connected first
178 DALI_ASSERT_ALWAYS(!childNode->IsRoot() && nullptr == childNode->GetParent()); // Child should be disconnected
180 childNode->SetParent(*this);
182 // Everything should be reinherited when reconnected to scene-graph
183 childNode->SetAllDirtyFlags();
185 // Add the node to the end of the child list.
186 mChildren.PushBack(childNode);
188 // Inform property observers of new connection
189 childNode->ConnectToSceneGraph();
192 void Node::DisconnectChild(BufferIndex updateBufferIndex, Node& childNode)
194 DALI_ASSERT_ALWAYS(this != &childNode);
195 DALI_ASSERT_ALWAYS(childNode.GetParent() == this);
197 // Find the childNode and remove it
198 Node* found(nullptr);
200 const NodeIter endIter = mChildren.End();
201 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
203 Node* current = *iter;
204 if(current == &childNode)
207 mChildren.Erase(iter); // order matters here
208 break; // iter is no longer valid
211 DALI_ASSERT_ALWAYS(nullptr != found);
213 found->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
216 void Node::AddRenderer(Renderer* renderer)
218 // If it is the first renderer added, make sure the world transform will be calculated
219 // in the next update as world transform is not computed if node has no renderers.
220 if(mRenderer.Empty())
222 mDirtyFlags |= NodePropertyFlags::TRANSFORM;
226 // Check that it has not been already added.
227 for(auto&& existingRenderer : mRenderer)
229 if(existingRenderer == renderer)
231 // Renderer is already in the list.
239 mRenderer.PushBack(renderer);
242 void Node::RemoveRenderer(const Renderer* renderer)
244 RendererContainer::SizeType rendererCount(mRenderer.Size());
245 for(RendererContainer::SizeType i = 0; i < rendererCount; ++i)
247 if(mRenderer[i] == renderer)
250 mRenderer.Erase(mRenderer.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;
273 // Check whether the update area property has changed
274 if(!mUpdateAreaHint.IsClean())
276 flags |= NodePropertyFlags::TRANSFORM;
282 NodePropertyFlags Node::GetInheritedDirtyFlags(NodePropertyFlags parentFlags) const
284 // Size is not inherited. VisibleFlag is inherited
285 static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
286 using UnderlyingType = typename std::underlying_type<NodePropertyFlags>::type;
288 return static_cast<NodePropertyFlags>(static_cast<UnderlyingType>(mDirtyFlags) |
289 (static_cast<UnderlyingType>(parentFlags) & static_cast<UnderlyingType>(InheritedDirtyFlags)));
292 void Node::ResetDirtyFlags(BufferIndex updateBufferIndex)
294 mDirtyFlags = NodePropertyFlags::NOTHING;
297 void Node::UpdateUniformHash(BufferIndex bufferIndex)
299 uint64_t hash = 0xc70f6907UL;
300 for(uint32_t i = 0u, count = mUniformMaps.Count(); i < count; ++i)
302 hash = mUniformMaps[i].propertyPtr->Hash(bufferIndex, hash);
304 if(mUniformsHash != hash)
306 mUniformsHash = hash;
311 void Node::SetParent(Node& parentNode)
313 DALI_ASSERT_ALWAYS(this != &parentNode);
314 DALI_ASSERT_ALWAYS(!mIsRoot);
315 DALI_ASSERT_ALWAYS(mParent == nullptr);
317 mParent = &parentNode;
319 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
321 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), parentNode.GetTransformId());
325 void Node::RecursiveDisconnectFromSceneGraph(BufferIndex updateBufferIndex)
327 DALI_ASSERT_ALWAYS(!mIsRoot);
328 DALI_ASSERT_ALWAYS(mParent != nullptr);
330 const NodeIter endIter = mChildren.End();
331 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
333 (*iter)->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
336 // Animators, Constraints etc. should be disconnected from the child's properties.
337 PropertyOwner::DisconnectFromSceneGraph(updateBufferIndex);
339 // Remove back-pointer to parent
342 // Remove all child pointers
345 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
347 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), INVALID_TRANSFORM_ID);
351 uint32_t Node::GetMemoryPoolCapacity()
353 return gNodeMemoryPool.GetCapacity();
356 } // namespace SceneGraph
358 } // namespace Internal