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 layer
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 mUpdateSizeHint(Vector3::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),
103 mPositionUsesAnchorPoint(true),
113 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
115 mTransformManagerData.Manager()->RemoveTransform(mTransformManagerData.Id());
123 void Node::OnDestroy()
125 // Animators, Constraints etc. should be disconnected from the child's properties.
126 PropertyOwner::Destroy();
129 uint32_t Node::GetId() const
134 void Node::CreateTransform(SceneGraph::TransformManager* transformManager)
136 //Create a new transform
137 mTransformManagerData.mManager = transformManager;
138 TransformId createdTransformId = transformManager->CreateTransform();
140 //Initialize all the animatable properties
141 mPosition.Initialize(&mTransformManagerData);
142 mScale.Initialize(&mTransformManagerData);
143 mOrientation.Initialize(&mTransformManagerData);
144 mSize.Initialize(&mTransformManagerData);
145 mParentOrigin.Initialize(&mTransformManagerData);
146 mAnchorPoint.Initialize(&mTransformManagerData);
148 //Initialize all the input properties
149 mWorldPosition.Initialize(&mTransformManagerData);
150 mWorldScale.Initialize(&mTransformManagerData);
151 mWorldOrientation.Initialize(&mTransformManagerData);
152 mWorldMatrix.Initialize(&mTransformManagerData);
154 //Set whether the position should use the anchor point
155 transformManager->SetPositionUsesAnchorPoint(createdTransformId, mPositionUsesAnchorPoint);
157 //Set TransformId after initialize done.
158 mTransformManagerData.mId = createdTransformId;
161 void Node::SetRoot(bool isRoot)
163 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
168 bool Node::IsAnimationPossible() const
170 return mIsConnectedToSceneGraph;
173 void Node::ConnectChild(Node* childNode)
175 DALI_ASSERT_ALWAYS(this != childNode);
176 DALI_ASSERT_ALWAYS(IsRoot() || nullptr != mParent); // Parent should be connected first
177 DALI_ASSERT_ALWAYS(!childNode->IsRoot() && nullptr == childNode->GetParent()); // Child should be disconnected
179 childNode->SetParent(*this);
181 // Everything should be reinherited when reconnected to scene-graph
182 childNode->SetAllDirtyFlags();
184 // Add the node to the end of the child list.
185 mChildren.PushBack(childNode);
187 // Inform property observers of new connection
188 childNode->ConnectToSceneGraph();
191 void Node::DisconnectChild(BufferIndex updateBufferIndex, Node& childNode)
193 DALI_ASSERT_ALWAYS(this != &childNode);
194 DALI_ASSERT_ALWAYS(childNode.GetParent() == this);
196 // Find the childNode and remove it
197 Node* found(nullptr);
199 const NodeIter endIter = mChildren.End();
200 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
202 Node* current = *iter;
203 if(current == &childNode)
206 mChildren.Erase(iter); // order matters here
207 break; // iter is no longer valid
210 DALI_ASSERT_ALWAYS(nullptr != found);
212 found->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
215 void Node::AddRenderer(Renderer* renderer)
217 // If it is the first renderer added, make sure the world transform will be calculated
218 // in the next update as world transform is not computed if node has no renderers.
219 if(mRenderer.Empty())
221 mDirtyFlags |= NodePropertyFlags::TRANSFORM;
225 // Check that it has not been already added.
226 for(auto&& existingRenderer : mRenderer)
228 if(existingRenderer == renderer)
230 // Renderer is already in the list.
238 mRenderer.PushBack(renderer);
241 void Node::RemoveRenderer(const Renderer* renderer)
243 RendererContainer::SizeType rendererCount(mRenderer.Size());
244 for(RendererContainer::SizeType i = 0; i < rendererCount; ++i)
246 if(mRenderer[i] == renderer)
249 mRenderer.Erase(mRenderer.Begin() + i);
255 NodePropertyFlags Node::GetDirtyFlags() const
257 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
258 NodePropertyFlags flags = mDirtyFlags;
260 // Check whether the visible property has changed
261 if(!mVisible.IsClean())
263 flags |= NodePropertyFlags::VISIBLE;
266 // Check whether the color property has changed
267 if(!mColor.IsClean())
269 flags |= NodePropertyFlags::COLOR;
275 NodePropertyFlags Node::GetInheritedDirtyFlags(NodePropertyFlags parentFlags) const
277 // Size is not inherited. VisibleFlag is inherited
278 static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
279 using UnderlyingType = typename std::underlying_type<NodePropertyFlags>::type;
281 return static_cast<NodePropertyFlags>(static_cast<UnderlyingType>(mDirtyFlags) |
282 (static_cast<UnderlyingType>(parentFlags) & static_cast<UnderlyingType>(InheritedDirtyFlags)));
285 void Node::ResetDirtyFlags(BufferIndex updateBufferIndex)
287 mDirtyFlags = NodePropertyFlags::NOTHING;
290 void Node::SetParent(Node& parentNode)
292 DALI_ASSERT_ALWAYS(this != &parentNode);
293 DALI_ASSERT_ALWAYS(!mIsRoot);
294 DALI_ASSERT_ALWAYS(mParent == nullptr);
296 mParent = &parentNode;
298 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
300 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), parentNode.GetTransformId());
304 void Node::RecursiveDisconnectFromSceneGraph(BufferIndex updateBufferIndex)
306 DALI_ASSERT_ALWAYS(!mIsRoot);
307 DALI_ASSERT_ALWAYS(mParent != nullptr);
309 const NodeIter endIter = mChildren.End();
310 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
312 (*iter)->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
315 // Animators, Constraints etc. should be disconnected from the child's properties.
316 PropertyOwner::DisconnectFromSceneGraph(updateBufferIndex);
318 // Remove back-pointer to parent
321 // Remove all child pointers
324 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
326 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), INVALID_TRANSFORM_ID);
330 } // namespace SceneGraph
332 } // namespace Internal