Merge "Remove some dead code related to scene graph animator classes" into devel...
[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 PositionInheritanceMode Node::DEFAULT_POSITION_INHERITANCE_MODE( INHERIT_PARENT_POSITION );
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 ( gNodeMemoryPool.AllocateRawThreadSafe() ) Node;
62 }
63
64 void Node::Delete( Node* node )
65 {
66   // check we have a node not a layer
67   if( !node->mIsLayer )
68   {
69     // Manually call the destructor
70     node->~Node();
71
72     // Mark the memory it used as free in the memory pool
73     gNodeMemoryPool.FreeThreadSafe( node );
74   }
75   else
76   {
77     // not in the pool, just delete it.
78     delete node;
79   }
80 }
81
82 Node::Node()
83 : mTransformManager( NULL ),
84   mTransformId( INVALID_TRANSFORM_ID ),
85   mParentOrigin( TRANSFORM_PROPERTY_PARENT_ORIGIN ),
86   mAnchorPoint( TRANSFORM_PROPERTY_ANCHOR_POINT ),
87   mSize( TRANSFORM_PROPERTY_SIZE ),                                               // Zero initialized by default
88   mPosition( TRANSFORM_PROPERTY_POSITION ),                                       // Zero initialized by default
89   mOrientation(),                                                                 // Initialized to identity by default
90   mScale( TRANSFORM_PROPERTY_SCALE ),
91   mVisible( true ),
92   mCulled( false ),
93   mColor( Color::WHITE ),
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( NULL ),
102   mExclusiveRenderTask( NULL ),
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( OwnerPointer< UniformPropertyMapping >& map )
180 {
181   PropertyOwner::AddUniformMapping( map );
182   mRegenerateUniformMap = 2;
183 }
184
185 void Node::RemoveUniformMapping( const std::string& uniformName )
186 {
187   PropertyOwner::RemoveUniformMapping( uniformName );
188   mRegenerateUniformMap = 2;
189 }
190
191 void Node::PrepareRender( BufferIndex bufferIndex )
192 {
193   if( mRegenerateUniformMap != 0 )
194   {
195     if( mRegenerateUniformMap == 2 )
196     {
197       CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
198       localMap.Resize(0);
199
200       for( UniformMap::SizeType i = 0, count=mUniformMaps.Count(); i<count; ++i )
201       {
202         localMap.PushBack( &mUniformMaps[i] );
203       }
204     }
205     else if( mRegenerateUniformMap == 1 )
206     {
207       CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
208       CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-bufferIndex ];
209
210       localMap.Resize( oldMap.Count() );
211
212       CollectedUniformMap::SizeType index = 0;
213       for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
214       {
215         localMap[index] = *iter;
216       }
217     }
218     --mRegenerateUniformMap;
219     mUniformMapChanged[bufferIndex] = 1u;
220   }
221 }
222
223 void Node::ConnectChild( Node* childNode )
224 {
225   DALI_ASSERT_ALWAYS( this != childNode );
226   DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
227   DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
228
229   childNode->SetParent( *this );
230
231   // Everything should be reinherited when reconnected to scene-graph
232   childNode->SetAllDirtyFlags();
233
234   // Add the node to the end of the child list.
235   mChildren.PushBack( childNode );
236
237   // Inform property observers of new connection
238   childNode->ConnectToSceneGraph();
239 }
240
241 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode )
242 {
243   DALI_ASSERT_ALWAYS( this != &childNode );
244   DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
245
246   // Find the childNode and remove it
247   Node* found( NULL );
248
249   const NodeIter endIter = mChildren.End();
250   for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
251   {
252     Node* current = *iter;
253     if ( current == &childNode )
254     {
255       found = current;
256       mChildren.Erase( iter ); // order matters here
257       break; // iter is no longer valid
258     }
259   }
260   DALI_ASSERT_ALWAYS( NULL != found );
261
262   found->RecursiveDisconnectFromSceneGraph( updateBufferIndex );
263 }
264
265 void Node::AddRenderer( Renderer* renderer )
266 {
267   // If it is the first renderer added, make sure the world transform will be calculated
268   // in the next update as world transform is not computed if node has no renderers.
269   if( mRenderer.Empty() )
270   {
271     mDirtyFlags |= NodePropertyFlags::TRANSFORM;
272   }
273   else
274   {
275     // Check that it has not been already added.
276     for( auto&& existingRenderer : mRenderer )
277     {
278       if( existingRenderer == renderer )
279       {
280         // Renderer is already in the list.
281         return;
282       }
283     }
284   }
285
286   mRenderer.PushBack( renderer );
287 }
288
289 void Node::RemoveRenderer( Renderer* renderer )
290 {
291   RendererContainer::SizeType rendererCount( mRenderer.Size() );
292   for( RendererContainer::SizeType i = 0; i < rendererCount; ++i )
293   {
294     if( mRenderer[i] == renderer )
295     {
296       mRenderer.Erase( mRenderer.Begin()+i);
297       return;
298     }
299   }
300 }
301
302 NodePropertyFlags Node::GetDirtyFlags() const
303 {
304   // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
305   NodePropertyFlags flags = mDirtyFlags;
306
307   // Check whether the visible property has changed
308   if ( !mVisible.IsClean() )
309   {
310     flags |= NodePropertyFlags::VISIBLE;
311   }
312
313   // Check whether the color property has changed
314   if ( !mColor.IsClean() )
315   {
316     flags |= NodePropertyFlags::COLOR;
317   }
318
319   return flags;
320 }
321
322 NodePropertyFlags Node::GetInheritedDirtyFlags( NodePropertyFlags parentFlags ) const
323 {
324   // Size is not inherited. VisibleFlag is inherited
325   static const NodePropertyFlags InheritedDirtyFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::VISIBLE | NodePropertyFlags::COLOR;
326   using UnderlyingType = typename std::underlying_type<NodePropertyFlags>::type;
327
328   return static_cast<NodePropertyFlags>( static_cast<UnderlyingType>( mDirtyFlags ) |
329                                          ( static_cast<UnderlyingType>( parentFlags ) & static_cast<UnderlyingType>( InheritedDirtyFlags ) ) );
330 }
331
332 void Node::ResetDirtyFlags( BufferIndex updateBufferIndex )
333 {
334   mDirtyFlags = NodePropertyFlags::NOTHING;
335 }
336
337 void Node::SetParent( Node& parentNode )
338 {
339   DALI_ASSERT_ALWAYS(this != &parentNode);
340   DALI_ASSERT_ALWAYS(!mIsRoot);
341   DALI_ASSERT_ALWAYS(mParent == NULL);
342
343   mParent = &parentNode;
344
345   if( mTransformId != INVALID_TRANSFORM_ID )
346   {
347     mTransformManager->SetParent( mTransformId, parentNode.GetTransformId() );
348   }
349 }
350
351 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex )
352 {
353   DALI_ASSERT_ALWAYS(!mIsRoot);
354   DALI_ASSERT_ALWAYS(mParent != NULL);
355
356   const NodeIter endIter = mChildren.End();
357   for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
358   {
359     (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex );
360   }
361
362   // Animators, Constraints etc. should be disconnected from the child's properties.
363   PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
364
365   // Remove back-pointer to parent
366   mParent = NULL;
367
368   // Remove all child pointers
369   mChildren.Clear();
370
371   if( mTransformId != INVALID_TRANSFORM_ID )
372   {
373     mTransformManager->SetParent( mTransformId, INVALID_TRANSFORM_ID );
374   }
375 }
376
377 } // namespace SceneGraph
378
379 } // namespace Internal
380
381 } // namespace Dali