Avoid Heap allocation in UniformMap.
[platform/core/uifw/dali-core.git] / dali / internal / update / nodes / node.cpp
1 /*
2  * Copyright (c) 2018 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/dali-common.h>
26 #include <dali/public-api/common/constants.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> gNodeMemoryPool;
33 #ifdef DEBUG_ENABLED
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;
36
37 // Called when the process is about to exit, Node count should be zero at this point.
38 void __attribute__ ((destructor)) ShutDown(void)
39 {
40 DALI_ASSERT_DEBUG( (gNodeCount == 0) && "Node memory leak");
41 }
42 #endif
43 } // Unnamed namespace
44
45 namespace Dali
46 {
47
48 namespace Internal
49 {
50
51 namespace SceneGraph
52 {
53
54 const ColorMode Node::DEFAULT_COLOR_MODE( USE_OWN_MULTIPLY_PARENT_ALPHA );
55
56 uint32_t Node::mNodeCounter = 0;        ///< A counter to provide unique node ids, up-to 4 billion
57
58 Node* Node::New()
59 {
60   return new ( gNodeMemoryPool.AllocateRawThreadSafe() ) Node;
61 }
62
63 void Node::Delete( Node* node )
64 {
65   // check we have a node not a layer
66   if( !node->mIsLayer )
67   {
68     // Manually call the destructor
69     node->~Node();
70
71     // Mark the memory it used as free in the memory pool
72     gNodeMemoryPool.FreeThreadSafe( node );
73   }
74   else
75   {
76     // not in the pool, just delete it.
77     delete node;
78   }
79 }
80
81 Node::Node()
82 : mTransformManager( nullptr ),
83   mTransformId( INVALID_TRANSFORM_ID ),
84   mParentOrigin( TRANSFORM_PROPERTY_PARENT_ORIGIN ),
85   mAnchorPoint( TRANSFORM_PROPERTY_ANCHOR_POINT ),
86   mSize( TRANSFORM_PROPERTY_SIZE ),                                               // Zero initialized by default
87   mPosition( TRANSFORM_PROPERTY_POSITION ),                                       // Zero initialized by default
88   mOrientation(),                                                                 // Initialized to identity by default
89   mScale( TRANSFORM_PROPERTY_SCALE ),
90   mVisible( true ),
91   mCulled( false ),
92   mColor( Color::WHITE ),
93   mUpdateSizeHint( Vector3::ZERO ),
94   mWorldPosition( TRANSFORM_PROPERTY_WORLD_POSITION, Vector3( 0.0f,0.0f,0.0f ) ), // Zero initialized by default
95   mWorldScale( TRANSFORM_PROPERTY_WORLD_SCALE, Vector3( 1.0f,1.0f,1.0f ) ),
96   mWorldOrientation(),                                                            // Initialized to identity by default
97   mWorldMatrix(),
98   mWorldColor( Color::WHITE ),
99   mClippingSortModifier( 0u ),
100   mId( ++mNodeCounter ),
101   mParent( nullptr ),
102   mExclusiveRenderTask( nullptr ),
103   mChildren(),
104   mClippingDepth( 0u ),
105   mScissorDepth( 0u ),
106   mDepthIndex( 0u ),
107   mDirtyFlags( NodePropertyFlags::ALL ),
108   mRegenerateUniformMap( 0 ),
109   mDrawMode( DrawMode::NORMAL ),
110   mColorMode( DEFAULT_COLOR_MODE ),
111   mClippingMode( ClippingMode::DISABLED ),
112   mIsRoot( false ),
113   mIsLayer( false ),
114   mPositionUsesAnchorPoint( true )
115 {
116   mUniformMapChanged[0] = 0u;
117   mUniformMapChanged[1] = 0u;
118
119 #ifdef DEBUG_ENABLED
120   gNodeCount++;
121 #endif
122
123 }
124
125 Node::~Node()
126 {
127   if( mTransformId != INVALID_TRANSFORM_ID )
128   {
129     mTransformManager->RemoveTransform(mTransformId);
130   }
131
132 #ifdef DEBUG_ENABLED
133   gNodeCount--;
134 #endif
135 }
136
137 void Node::OnDestroy()
138 {
139   // Animators, Constraints etc. should be disconnected from the child's properties.
140   PropertyOwner::Destroy();
141 }
142
143 uint32_t Node::GetId() const
144 {
145   return mId;
146 }
147
148 void Node::CreateTransform( SceneGraph::TransformManager* transformManager )
149 {
150   //Create a new transform
151   mTransformManager = transformManager;
152   mTransformId = transformManager->CreateTransform();
153
154   //Initialize all the animatable properties
155   mPosition.Initialize( transformManager, mTransformId );
156   mScale.Initialize( transformManager, mTransformId );
157   mOrientation.Initialize( transformManager, mTransformId );
158   mSize.Initialize( transformManager, mTransformId );
159   mParentOrigin.Initialize( transformManager, mTransformId );
160   mAnchorPoint.Initialize( transformManager, mTransformId );
161
162   //Initialize all the input properties
163   mWorldPosition.Initialize( transformManager, mTransformId );
164   mWorldScale.Initialize( transformManager, mTransformId );
165   mWorldOrientation.Initialize( transformManager, mTransformId );
166   mWorldMatrix.Initialize( transformManager, mTransformId );
167
168   //Set whether the position should use the anchor point
169   transformManager->SetPositionUsesAnchorPoint( mTransformId, mPositionUsesAnchorPoint );
170 }
171
172 void Node::SetRoot(bool isRoot)
173 {
174   DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
175
176   mIsRoot = isRoot;
177 }
178
179 void Node::AddUniformMapping(const UniformPropertyMapping& map)
180 {
181   PropertyOwner::AddUniformMapping( map );
182   mRegenerateUniformMap = 2;
183 }
184
185 void Node::RemoveUniformMapping( const ConstString& uniformName )
186 {
187   PropertyOwner::RemoveUniformMapping( uniformName );
188   mRegenerateUniformMap = 2;
189 }
190
191 bool Node::IsAnimationPossible() const
192 {
193   return mIsConnectedToSceneGraph;
194 }
195
196 void Node::PrepareRender( BufferIndex bufferIndex )
197 {
198   if( mRegenerateUniformMap != 0 )
199   {
200     if( mRegenerateUniformMap == 2 )
201     {
202       CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
203       localMap.Resize(0);
204
205       for( UniformMap::SizeType i = 0, count=mUniformMaps.Count(); i<count; ++i )
206       {
207         localMap.PushBack(mUniformMaps[i]);
208       }
209     }
210     else if( mRegenerateUniformMap == 1 )
211     {
212       CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
213       CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-bufferIndex ];
214
215       localMap.Resize( oldMap.Count() );
216
217       CollectedUniformMap::SizeType index = 0;
218       for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
219       {
220         localMap[index] = *iter;
221       }
222     }
223     --mRegenerateUniformMap;
224     mUniformMapChanged[bufferIndex] = 1u;
225   }
226 }
227
228 void Node::ConnectChild( Node* childNode )
229 {
230   DALI_ASSERT_ALWAYS( this != childNode );
231   DALI_ASSERT_ALWAYS( IsRoot() || nullptr != mParent ); // Parent should be connected first
232   DALI_ASSERT_ALWAYS( !childNode->IsRoot() && nullptr == childNode->GetParent() ); // Child should be disconnected
233
234   childNode->SetParent( *this );
235
236   // Everything should be reinherited when reconnected to scene-graph
237   childNode->SetAllDirtyFlags();
238
239   // Add the node to the end of the child list.
240   mChildren.PushBack( childNode );
241
242   // Inform property observers of new connection
243   childNode->ConnectToSceneGraph();
244 }
245
246 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode )
247 {
248   DALI_ASSERT_ALWAYS( this != &childNode );
249   DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
250
251   // Find the childNode and remove it
252   Node* found( nullptr );
253
254   const NodeIter endIter = mChildren.End();
255   for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
256   {
257     Node* current = *iter;
258     if ( current == &childNode )
259     {
260       found = current;
261       mChildren.Erase( iter ); // order matters here
262       break; // iter is no longer valid
263     }
264   }
265   DALI_ASSERT_ALWAYS( nullptr != found );
266
267   found->RecursiveDisconnectFromSceneGraph( updateBufferIndex );
268 }
269
270 void Node::AddRenderer( Renderer* renderer )
271 {
272   // If it is the first renderer added, make sure the world transform will be calculated
273   // in the next update as world transform is not computed if node has no renderers.
274   if( mRenderer.Empty() )
275   {
276     mDirtyFlags |= NodePropertyFlags::TRANSFORM;
277   }
278   else
279   {
280     // Check that it has not been already added.
281     for( auto&& existingRenderer : mRenderer )
282     {
283       if( existingRenderer == renderer )
284       {
285         // Renderer is already in the list.
286         return;
287       }
288     }
289   }
290
291   mRenderer.PushBack( renderer );
292 }
293
294 void Node::RemoveRenderer( const Renderer* renderer )
295 {
296   RendererContainer::SizeType rendererCount( mRenderer.Size() );
297   for( RendererContainer::SizeType i = 0; i < rendererCount; ++i )
298   {
299     if( mRenderer[i] == renderer )
300     {
301       mRenderer.Erase( mRenderer.Begin()+i);
302       return;
303     }
304   }
305 }
306
307 NodePropertyFlags Node::GetDirtyFlags() const
308 {
309   // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
310   NodePropertyFlags flags = mDirtyFlags;
311
312   // Check whether the visible property has changed
313   if ( !mVisible.IsClean() )
314   {
315     flags |= NodePropertyFlags::VISIBLE;
316   }
317
318   // Check whether the color property has changed
319   if ( !mColor.IsClean() )
320   {
321     flags |= NodePropertyFlags::COLOR;
322   }
323
324   return flags;
325 }
326
327 NodePropertyFlags Node::GetInheritedDirtyFlags( NodePropertyFlags parentFlags ) const
328 {
329   // Size is not inherited. VisibleFlag is inherited
330   static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
331   using UnderlyingType = typename std::underlying_type<NodePropertyFlags>::type;
332
333   return static_cast<NodePropertyFlags>( static_cast<UnderlyingType>( mDirtyFlags ) |
334                                          ( static_cast<UnderlyingType>( parentFlags ) & static_cast<UnderlyingType>( InheritedDirtyFlags ) ) );
335 }
336
337 void Node::ResetDirtyFlags( BufferIndex updateBufferIndex )
338 {
339   mDirtyFlags = NodePropertyFlags::NOTHING;
340 }
341
342 void Node::SetParent( Node& parentNode )
343 {
344   DALI_ASSERT_ALWAYS(this != &parentNode);
345   DALI_ASSERT_ALWAYS(!mIsRoot);
346   DALI_ASSERT_ALWAYS(mParent == nullptr);
347
348   mParent = &parentNode;
349
350   if( mTransformId != INVALID_TRANSFORM_ID )
351   {
352     mTransformManager->SetParent( mTransformId, parentNode.GetTransformId() );
353   }
354 }
355
356 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex )
357 {
358   DALI_ASSERT_ALWAYS(!mIsRoot);
359   DALI_ASSERT_ALWAYS(mParent != nullptr);
360
361   const NodeIter endIter = mChildren.End();
362   for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
363   {
364     (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex );
365   }
366
367   // Animators, Constraints etc. should be disconnected from the child's properties.
368   PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
369
370   // Remove back-pointer to parent
371   mParent = nullptr;
372
373   // Remove all child pointers
374   mChildren.Clear();
375
376   if( mTransformId != INVALID_TRANSFORM_ID )
377   {
378     mTransformManager->SetParent( mTransformId, INVALID_TRANSFORM_ID );
379   }
380 }
381
382 } // namespace SceneGraph
383
384 } // namespace Internal
385
386 } // namespace Dali