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
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),
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 // Set whether the position should use the anchor point
142 transformManager->SetPositionUsesAnchorPoint(createdTransformId, mPositionUsesAnchorPoint);
144 // Set TransformId after initialize done.
145 mTransformManagerData.mId = createdTransformId;
148 void Node::SetRoot(bool isRoot)
150 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
155 bool Node::IsAnimationPossible() const
157 return mIsConnectedToSceneGraph;
160 void Node::ConnectChild(Node* childNode)
162 DALI_ASSERT_ALWAYS(this != childNode);
163 DALI_ASSERT_ALWAYS(IsRoot() || nullptr != mParent); // Parent should be connected first
164 DALI_ASSERT_ALWAYS(!childNode->IsRoot() && nullptr == childNode->GetParent()); // Child should be disconnected
166 childNode->SetParent(*this);
168 // Everything should be reinherited when reconnected to scene-graph
169 childNode->SetAllDirtyFlags();
171 // Add the node to the end of the child list.
172 mChildren.PushBack(childNode);
174 // Inform property observers of new connection
175 childNode->ConnectToSceneGraph();
178 void Node::DisconnectChild(BufferIndex updateBufferIndex, Node& childNode)
180 DALI_ASSERT_ALWAYS(this != &childNode);
181 DALI_ASSERT_ALWAYS(childNode.GetParent() == this);
183 // Find the childNode and remove it
184 Node* found(nullptr);
186 const NodeIter endIter = mChildren.End();
187 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
189 Node* current = *iter;
190 if(current == &childNode)
193 mChildren.Erase(iter); // order matters here
194 break; // iter is no longer valid
197 DALI_ASSERT_ALWAYS(nullptr != found);
199 found->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
202 void Node::AddRenderer(RendererIndex renderer)
204 // If it is the first renderer added, make sure the world transform will be calculated
205 // in the next update as world transform is not computed if node has no renderers.
206 if(mRenderers.Empty())
208 mDirtyFlags |= NodePropertyFlags::TRANSFORM;
212 // Check that it has not been already added.
213 for(auto&& existingRenderer : mRenderers)
215 if(existingRenderer == renderer)
217 // Renderer is already in the list.
225 mRenderers.PushBack(renderer);
228 void Node::RemoveRenderer(const RendererIndex renderer)
230 RendererContainer::SizeType rendererCount(mRenderers.Size());
231 for(RendererContainer::SizeType i = 0; i < rendererCount; ++i)
233 if(mRenderers[i] == renderer)
236 mRenderers.Erase(mRenderers.Begin() + i);
242 NodePropertyFlags Node::GetDirtyFlags() const
244 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
245 NodePropertyFlags flags = mDirtyFlags;
247 // Check whether the visible property has changed
248 if(!mVisible.IsClean())
250 flags |= NodePropertyFlags::VISIBLE;
253 // Check whether the color property has changed
254 if(!mColor.IsClean())
256 flags |= NodePropertyFlags::COLOR;
259 // Check whether the update area property has changed
260 if(!mUpdateAreaHint.IsClean())
262 flags |= NodePropertyFlags::TRANSFORM;
268 NodePropertyFlags Node::GetInheritedDirtyFlags(NodePropertyFlags parentFlags) const
270 // Size is not inherited. VisibleFlag is inherited
271 static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
272 using UnderlyingType = typename std::underlying_type<NodePropertyFlags>::type;
274 return static_cast<NodePropertyFlags>(static_cast<UnderlyingType>(mDirtyFlags) |
275 (static_cast<UnderlyingType>(parentFlags) & static_cast<UnderlyingType>(InheritedDirtyFlags)));
278 void Node::ResetDirtyFlags(BufferIndex updateBufferIndex)
280 mDirtyFlags = NodePropertyFlags::NOTHING;
283 void Node::UpdateUniformHash(BufferIndex bufferIndex)
285 uint64_t hash = 0xc70f6907UL;
286 for(uint32_t i = 0u, count = mUniformMaps.Count(); i < count; ++i)
288 hash = mUniformMaps[i].propertyPtr->Hash(bufferIndex, hash);
290 if(mUniformsHash != hash)
292 mUniformsHash = hash;
297 void Node::SetParent(Node& parentNode)
299 DALI_ASSERT_ALWAYS(this != &parentNode);
300 DALI_ASSERT_ALWAYS(!mIsRoot);
301 DALI_ASSERT_ALWAYS(mParent == nullptr);
303 mParent = &parentNode;
305 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
307 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), parentNode.GetTransformId());
311 void Node::RecursiveDisconnectFromSceneGraph(BufferIndex updateBufferIndex)
313 DALI_ASSERT_ALWAYS(!mIsRoot);
314 DALI_ASSERT_ALWAYS(mParent != nullptr);
316 const NodeIter endIter = mChildren.End();
317 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
319 (*iter)->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
322 // Animators, Constraints etc. should be disconnected from the child's properties.
323 PropertyOwner::DisconnectFromSceneGraph(updateBufferIndex);
325 // Remove back-pointer to parent
328 // Remove all child pointers
331 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
333 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), INVALID_TRANSFORM_ID);
337 uint32_t Node::GetMemoryPoolCapacity()
339 return gNodeMemoryPool.GetCapacity();
342 } // namespace SceneGraph
344 } // namespace Internal