Make sure that global variables are initialized lazily.
[platform/core/uifw/dali-core.git] / dali / internal / update / nodes / node.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/nodes/node.h>
20
21 // INTERNAL INCLUDES
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>
27
28 namespace
29 {
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>& GetNodeMemoryPool()
33 {
34   static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Node> gNodeMemoryPool;
35   return gNodeMemoryPool;
36 }
37 #ifdef DEBUG_ENABLED
38 // keep track of nodes alive, to ensure we have 0 when the process exits or DALi library is unloaded
39 int32_t gNodeCount = 0;
40
41 // Called when the process is about to exit, Node count should be zero at this point.
42 void __attribute__((destructor)) ShutDown(void)
43 {
44   DALI_ASSERT_DEBUG((gNodeCount == 0) && "Node memory leak");
45 }
46 #endif
47 } // Unnamed namespace
48
49 namespace Dali
50 {
51 namespace Internal
52 {
53 namespace SceneGraph
54 {
55 const ColorMode Node::DEFAULT_COLOR_MODE(USE_OWN_MULTIPLY_PARENT_ALPHA);
56
57 uint32_t Node::mNodeCounter = 0; ///< A counter to provide unique node ids, up-to 4 billion
58
59 Node* Node::New()
60 {
61   return new(GetNodeMemoryPool().AllocateRawThreadSafe()) Node;
62 }
63
64 void Node::Delete(Node* node)
65 {
66   // check we have a node not a derived node
67   if(!node->mIsLayer && !node->mIsCamera)
68   {
69     // Manually call the destructor
70     node->~Node();
71
72     // Mark the memory it used as free in the memory pool
73     GetNodeMemoryPool().FreeThreadSafe(node);
74   }
75   else
76   {
77     // not in the pool, just delete it.
78     delete node;
79   }
80 }
81
82 Node::Node()
83 : mOrientation(),                                                               // Initialized to identity by default
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
87   mWorldMatrix(),
88   mVisible(true),
89   mCulled(false),
90   mColor(Color::WHITE),
91   mWorldColor(Color::WHITE),
92   mUpdateAreaHint(Vector4::ZERO),
93   mClippingSortModifier(0u),
94   mId(++mNodeCounter),
95   mParent(nullptr),
96   mExclusiveRenderTask(nullptr),
97   mChildren(),
98   mClippingDepth(0u),
99   mScissorDepth(0u),
100   mDepthIndex(0u),
101   mDirtyFlags(NodePropertyFlags::ALL),
102   mDrawMode(DrawMode::NORMAL),
103   mColorMode(DEFAULT_COLOR_MODE),
104   mClippingMode(ClippingMode::DISABLED),
105   mIsRoot(false),
106   mIsLayer(false),
107   mIsCamera(false),
108   mPositionUsesAnchorPoint(true),
109   mTransparent(false)
110 {
111 #ifdef DEBUG_ENABLED
112   gNodeCount++;
113 #endif
114 }
115
116 Node::~Node()
117 {
118   if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
119   {
120     mTransformManagerData.Manager()->RemoveTransform(mTransformManagerData.Id());
121   }
122
123 #ifdef DEBUG_ENABLED
124   gNodeCount--;
125 #endif
126 }
127
128 void Node::OnDestroy()
129 {
130   // Animators, Constraints etc. should be disconnected from the child's properties.
131   PropertyOwner::Destroy();
132 }
133
134 uint32_t Node::GetId() const
135 {
136   return mId;
137 }
138
139 void Node::CreateTransform(SceneGraph::TransformManager* transformManager)
140 {
141   // Create a new transform
142   mTransformManagerData.mManager = transformManager;
143   TransformId createdTransformId = transformManager->CreateTransform();
144
145   // Set whether the position should use the anchor point
146   transformManager->SetPositionUsesAnchorPoint(createdTransformId, mPositionUsesAnchorPoint);
147
148   // Set TransformId after initialize done.
149   mTransformManagerData.mId = createdTransformId;
150 }
151
152 void Node::SetRoot(bool isRoot)
153 {
154   DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
155
156   mIsRoot = isRoot;
157 }
158
159 bool Node::IsAnimationPossible() const
160 {
161   return mIsConnectedToSceneGraph;
162 }
163
164 void Node::ConnectChild(Node* childNode)
165 {
166   DALI_ASSERT_ALWAYS(this != childNode);
167   DALI_ASSERT_ALWAYS(IsRoot() || nullptr != mParent);                            // Parent should be connected first
168   DALI_ASSERT_ALWAYS(!childNode->IsRoot() && nullptr == childNode->GetParent()); // Child should be disconnected
169
170   childNode->SetParent(*this);
171
172   // Everything should be reinherited when reconnected to scene-graph
173   childNode->SetAllDirtyFlags();
174
175   // Add the node to the end of the child list.
176   mChildren.PushBack(childNode);
177
178   // Inform property observers of new connection
179   childNode->ConnectToSceneGraph();
180 }
181
182 void Node::DisconnectChild(BufferIndex updateBufferIndex, Node& childNode)
183 {
184   DALI_ASSERT_ALWAYS(this != &childNode);
185   DALI_ASSERT_ALWAYS(childNode.GetParent() == this);
186
187   // Find the childNode and remove it
188   Node* found(nullptr);
189
190   const NodeIter endIter = mChildren.End();
191   for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
192   {
193     Node* current = *iter;
194     if(current == &childNode)
195     {
196       found = current;
197       mChildren.Erase(iter); // order matters here
198       break;                 // iter is no longer valid
199     }
200   }
201   DALI_ASSERT_ALWAYS(nullptr != found);
202
203   found->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
204 }
205
206 void Node::AddRenderer(const RendererKey& renderer)
207 {
208   // If it is the first renderer added, make sure the world transform will be calculated
209   // in the next update as world transform is not computed if node has no renderers.
210   if(mRenderers.Empty())
211   {
212     mDirtyFlags |= NodePropertyFlags::TRANSFORM;
213   }
214   else
215   {
216     // Check that it has not been already added.
217     for(auto&& existingRenderer : mRenderers)
218     {
219       if(existingRenderer == renderer)
220       {
221         // Renderer is already in the list.
222         return;
223       }
224     }
225   }
226
227   SetUpdated(true);
228
229   mRenderers.PushBack(renderer);
230 }
231
232 void Node::RemoveRenderer(const RendererKey& renderer)
233 {
234   RendererContainer::SizeType rendererCount(mRenderers.Size());
235   for(RendererContainer::SizeType i = 0; i < rendererCount; ++i)
236   {
237     if(mRenderers[i] == renderer)
238     {
239       SetUpdated(true);
240       mRenderers.Erase(mRenderers.Begin() + i);
241       return;
242     }
243   }
244 }
245
246 NodePropertyFlags Node::GetDirtyFlags() const
247 {
248   // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
249   NodePropertyFlags flags = mDirtyFlags;
250
251   // Check whether the visible property has changed
252   if(!mVisible.IsClean())
253   {
254     flags |= NodePropertyFlags::VISIBLE;
255   }
256
257   // Check whether the color property has changed
258   if(!mColor.IsClean())
259   {
260     flags |= NodePropertyFlags::COLOR;
261   }
262
263   // Check whether the update area property has changed
264   if(!mUpdateAreaHint.IsClean())
265   {
266     flags |= NodePropertyFlags::TRANSFORM;
267   }
268
269   return flags;
270 }
271
272 NodePropertyFlags Node::GetInheritedDirtyFlags(NodePropertyFlags parentFlags) const
273 {
274   // Size is not inherited. VisibleFlag is inherited
275   static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
276   using UnderlyingType                               = typename std::underlying_type<NodePropertyFlags>::type;
277
278   return static_cast<NodePropertyFlags>(static_cast<UnderlyingType>(mDirtyFlags) |
279                                         (static_cast<UnderlyingType>(parentFlags) & static_cast<UnderlyingType>(InheritedDirtyFlags)));
280 }
281
282 void Node::ResetDirtyFlags(BufferIndex updateBufferIndex)
283 {
284   mDirtyFlags = NodePropertyFlags::NOTHING;
285 }
286
287 void Node::UpdateUniformHash(BufferIndex bufferIndex)
288 {
289   uint64_t hash = 0xc70f6907UL;
290   for(uint32_t i = 0u, count = mUniformMaps.Count(); i < count; ++i)
291   {
292     hash = mUniformMaps[i].propertyPtr->Hash(bufferIndex, hash);
293   }
294   if(mUniformsHash != hash)
295   {
296     mUniformsHash = hash;
297     SetUpdated(true);
298   }
299 }
300
301 void Node::SetParent(Node& parentNode)
302 {
303   DALI_ASSERT_ALWAYS(this != &parentNode);
304   DALI_ASSERT_ALWAYS(!mIsRoot);
305   DALI_ASSERT_ALWAYS(mParent == nullptr);
306
307   mParent = &parentNode;
308
309   if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
310   {
311     mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), parentNode.GetTransformId());
312   }
313 }
314
315 void Node::RecursiveDisconnectFromSceneGraph(BufferIndex updateBufferIndex)
316 {
317   DALI_ASSERT_ALWAYS(!mIsRoot);
318   DALI_ASSERT_ALWAYS(mParent != nullptr);
319
320   const NodeIter endIter = mChildren.End();
321   for(NodeIter iter = mChildren.Begin(); iter != endIter; ++iter)
322   {
323     (*iter)->RecursiveDisconnectFromSceneGraph(updateBufferIndex);
324   }
325
326   // Animators, Constraints etc. should be disconnected from the child's properties.
327   PropertyOwner::DisconnectFromSceneGraph(updateBufferIndex);
328
329   // Remove back-pointer to parent
330   mParent = nullptr;
331
332   // Remove all child pointers
333   mChildren.Clear();
334
335   if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
336   {
337     mTransformManagerData.Manager()->SetParent(mTransformManagerData.Id(), INVALID_TRANSFORM_ID);
338   }
339 }
340
341 uint32_t Node::GetMemoryPoolCapacity()
342 {
343   return GetNodeMemoryPool().GetCapacity();
344 }
345
346 } // namespace SceneGraph
347
348 } // namespace Internal
349
350 } // namespace Dali