2 * Copyright (c) 2021 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 mRegenerateUniformMap(0),
99 mDrawMode(DrawMode::NORMAL),
100 mColorMode(DEFAULT_COLOR_MODE),
101 mClippingMode(ClippingMode::DISABLED),
104 mPositionUsesAnchorPoint(true)
106 mUniformMapChanged[0] = 0u;
107 mUniformMapChanged[1] = 0u;
116 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
118 mTransformManagerData.Manager()->RemoveTransform(mTransformManagerData.Id());
126 void Node::OnDestroy()
128 // Animators, Constraints etc. should be disconnected from the child's properties.
129 PropertyOwner::Destroy();
132 uint32_t Node::GetId() const
137 void Node::CreateTransform(SceneGraph::TransformManager* transformManager)
139 //Create a new transform
140 mTransformManagerData.mManager = transformManager;
141 mTransformManagerData.mId = transformManager->CreateTransform();
143 //Initialize all the animatable properties
144 mPosition.Initialize(&mTransformManagerData);
145 mScale.Initialize(&mTransformManagerData);
146 mOrientation.Initialize(&mTransformManagerData);
147 mSize.Initialize(&mTransformManagerData);
148 mParentOrigin.Initialize(&mTransformManagerData);
149 mAnchorPoint.Initialize(&mTransformManagerData);
151 //Initialize all the input properties
152 mWorldPosition.Initialize(&mTransformManagerData);
153 mWorldScale.Initialize(&mTransformManagerData);
154 mWorldOrientation.Initialize(&mTransformManagerData);
155 mWorldMatrix.Initialize(&mTransformManagerData);
157 //Set whether the position should use the anchor point
158 transformManager->SetPositionUsesAnchorPoint(mTransformManagerData.Id(), mPositionUsesAnchorPoint);
161 void Node::SetRoot(bool isRoot)
163 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
168 void Node::AddUniformMapping(const UniformPropertyMapping& map)
170 PropertyOwner::AddUniformMapping(map);
171 mRegenerateUniformMap = 2;
174 void Node::RemoveUniformMapping(const ConstString& uniformName)
176 PropertyOwner::RemoveUniformMapping(uniformName);
177 mRegenerateUniformMap = 2;
180 bool Node::IsAnimationPossible() const
182 return mIsConnectedToSceneGraph;
185 void Node::PrepareRender(BufferIndex bufferIndex)
187 if(mRegenerateUniformMap != 0)
189 if(mRegenerateUniformMap == 2)
191 CollectedUniformMap& localMap = mCollectedUniformMap[bufferIndex];
194 for(UniformMap::SizeType i = 0, count = mUniformMaps.Count(); i < count; ++i)
196 localMap.PushBack(mUniformMaps[i]);
199 else if(mRegenerateUniformMap == 1)
201 CollectedUniformMap& localMap = mCollectedUniformMap[bufferIndex];
202 CollectedUniformMap& oldMap = mCollectedUniformMap[1 - bufferIndex];
204 localMap.Resize(oldMap.Count());
206 CollectedUniformMap::SizeType index = 0;
207 for(CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End(); iter != end; ++iter, ++index)
209 localMap[index] = *iter;
212 --mRegenerateUniformMap;
213 mUniformMapChanged[bufferIndex] = 1u;
217 void Node::ConnectChild(Node* childNode)
219 DALI_ASSERT_ALWAYS(this != childNode);
220 DALI_ASSERT_ALWAYS(IsRoot() || nullptr != mParent); // Parent should be connected first
221 DALI_ASSERT_ALWAYS(!childNode->IsRoot() && nullptr == childNode->GetParent()); // Child should be disconnected
223 childNode->SetParent(*this);
225 // Everything should be reinherited when reconnected to scene-graph
226 childNode->SetAllDirtyFlags();
228 // Add the node to the end of the child list.
229 mChildren.PushBack(childNode);
231 // Inform property observers of new connection
232 childNode->ConnectToSceneGraph();
235 void Node::DisconnectChild(BufferIndex updateBufferIndex, Node& childNode)
237 DALI_ASSERT_ALWAYS(this != &childNode);
238 DALI_ASSERT_ALWAYS(childNode.GetParent() == this);
240 // Find the childNode and remove it
241 Node* found(nullptr);
243 const NodeIter endIter = mChildren.End();
244 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
246 Node* current = *iter;
247 if(current == &childNode)
250 mChildren.Erase(iter); // order matters here
251 break; // iter is no longer valid
254 DALI_ASSERT_ALWAYS(nullptr != found);
256 found->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
259 void Node::AddRenderer(Renderer* renderer)
261 // If it is the first renderer added, make sure the world transform will be calculated
262 // in the next update as world transform is not computed if node has no renderers.
263 if(mRenderer.Empty())
265 mDirtyFlags |= NodePropertyFlags::TRANSFORM;
269 // Check that it has not been already added.
270 for(auto&& existingRenderer : mRenderer)
272 if(existingRenderer == renderer)
274 // Renderer is already in the list.
280 mRenderer.PushBack(renderer);
283 void Node::RemoveRenderer(const Renderer* renderer)
285 RendererContainer::SizeType rendererCount(mRenderer.Size());
286 for(RendererContainer::SizeType i = 0; i < rendererCount; ++i)
288 if(mRenderer[i] == renderer)
290 mRenderer.Erase(mRenderer.Begin() + i);
296 NodePropertyFlags Node::GetDirtyFlags() const
298 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
299 NodePropertyFlags flags = mDirtyFlags;
301 // Check whether the visible property has changed
302 if(!mVisible.IsClean())
304 flags |= NodePropertyFlags::VISIBLE;
307 // Check whether the color property has changed
308 if(!mColor.IsClean())
310 flags |= NodePropertyFlags::COLOR;
316 NodePropertyFlags Node::GetInheritedDirtyFlags(NodePropertyFlags parentFlags) const
318 // Size is not inherited. VisibleFlag is inherited
319 static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
320 using UnderlyingType = typename std::underlying_type<NodePropertyFlags>::type;
322 return static_cast<NodePropertyFlags>(static_cast<UnderlyingType>(mDirtyFlags) |
323 (static_cast<UnderlyingType>(parentFlags) & static_cast<UnderlyingType>(InheritedDirtyFlags)));
326 void Node::ResetDirtyFlags(BufferIndex updateBufferIndex)
328 mDirtyFlags = NodePropertyFlags::NOTHING;
331 void Node::SetParent(Node& parentNode)
333 DALI_ASSERT_ALWAYS(this != &parentNode);
334 DALI_ASSERT_ALWAYS(!mIsRoot);
335 DALI_ASSERT_ALWAYS(mParent == nullptr);
337 mParent = &parentNode;
339 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
341 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), parentNode.GetTransformId());
345 void Node::RecursiveDisconnectFromSceneGraph(BufferIndex updateBufferIndex)
347 DALI_ASSERT_ALWAYS(!mIsRoot);
348 DALI_ASSERT_ALWAYS(mParent != nullptr);
350 const NodeIter endIter = mChildren.End();
351 for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
353 (*iter)->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
356 // Animators, Constraints etc. should be disconnected from the child's properties.
357 PropertyOwner::DisconnectFromSceneGraph(updateBufferIndex);
359 // Remove back-pointer to parent
362 // Remove all child pointers
365 if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
367 mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), INVALID_TRANSFORM_ID);
371 } // namespace SceneGraph
373 } // namespace Internal