2 * Copyright (c) 2018 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/dali-common.h>
26 #include <dali/public-api/common/constants.h>
28 namespace //Unnamed namespace
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 created / deleted, to ensure we have 0 when the process exits or DALi library is unloaded
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");
54 const PositionInheritanceMode Node::DEFAULT_POSITION_INHERITANCE_MODE( INHERIT_PARENT_POSITION );
55 const ColorMode Node::DEFAULT_COLOR_MODE( USE_OWN_MULTIPLY_PARENT_ALPHA );
58 Node* Node::New( unsigned int id )
60 return new ( gNodeMemoryPool.AllocateRawThreadSafe() ) Node( id );
63 void Node::Delete( Node* node )
65 // check we have a node not a layer
68 // Manually call the destructor
71 // Mark the memory it used as free in the memory pool
72 gNodeMemoryPool.FreeThreadSafe( node );
76 // not in the pool, just delete it.
81 Node::Node( unsigned int id )
82 : mTransformManager( NULL ),
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 ),
92 mColor( Color::WHITE ),
93 mWorldPosition( TRANSFORM_PROPERTY_WORLD_POSITION, Vector3( 0.0f,0.0f,0.0f ) ), // Zero initialized by default
94 mWorldScale( TRANSFORM_PROPERTY_WORLD_SCALE, Vector3( 1.0f,1.0f,1.0f ) ),
95 mWorldOrientation(), // Initialized to identity by default
97 mWorldColor( Color::WHITE ),
98 mClippingSortModifier( 0u ),
101 mExclusiveRenderTask( NULL ),
103 mClippingDepth( 0u ),
106 mRegenerateUniformMap( 0 ),
107 mDirtyFlags( AllFlags ),
108 mDrawMode( DrawMode::NORMAL ),
109 mColorMode( DEFAULT_COLOR_MODE ),
110 mClippingMode( ClippingMode::DISABLED ),
113 mPositionUsesAnchorPoint( true )
115 mUniformMapChanged[0] = 0u;
116 mUniformMapChanged[1] = 0u;
126 if( mTransformId != INVALID_TRANSFORM_ID )
128 mTransformManager->RemoveTransform(mTransformId);
136 void Node::OnDestroy()
138 // Animators, Constraints etc. should be disconnected from the child's properties.
139 PropertyOwner::Destroy();
142 void Node::CreateTransform( SceneGraph::TransformManager* transformManager )
144 //Create a new transform
145 mTransformManager = transformManager;
146 mTransformId = transformManager->CreateTransform();
148 //Initialize all the animatable properties
149 mPosition.Initialize( transformManager, mTransformId );
150 mScale.Initialize( transformManager, mTransformId );
151 mOrientation.Initialize( transformManager, mTransformId );
152 mSize.Initialize( transformManager, mTransformId );
153 mParentOrigin.Initialize( transformManager, mTransformId );
154 mAnchorPoint.Initialize( transformManager, mTransformId );
156 //Initialize all the input properties
157 mWorldPosition.Initialize( transformManager, mTransformId );
158 mWorldScale.Initialize( transformManager, mTransformId );
159 mWorldOrientation.Initialize( transformManager, mTransformId );
160 mWorldMatrix.Initialize( transformManager, mTransformId );
162 //Set whether the position should use the anchor point
163 transformManager->SetPositionUsesAnchorPoint( mTransformId, mPositionUsesAnchorPoint );
166 void Node::SetRoot(bool isRoot)
168 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
173 void Node::AddUniformMapping( OwnerPointer< UniformPropertyMapping >& map )
175 PropertyOwner::AddUniformMapping( map );
176 mRegenerateUniformMap = 2;
179 void Node::RemoveUniformMapping( const std::string& uniformName )
181 PropertyOwner::RemoveUniformMapping( uniformName );
182 mRegenerateUniformMap = 2;
185 void Node::PrepareRender( BufferIndex bufferIndex )
187 if(mRegenerateUniformMap != 0 )
189 if( mRegenerateUniformMap == 2 )
191 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
194 for( unsigned int 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 unsigned int 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() || NULL != mParent ); // Parent should be connected first
221 DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == 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
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( NULL != found );
256 found->RecursiveDisconnectFromSceneGraph( updateBufferIndex );
259 void Node::AddRenderer( Renderer* renderer )
261 // Check that it has not been already added.
262 unsigned int rendererCount( mRenderer.Size() );
263 for( unsigned int i(0); i < rendererCount; ++i )
265 if( mRenderer[i] == renderer )
267 // Renderer is already in the list.
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( rendererCount == 0 )
276 mDirtyFlags |= TransformFlag;
279 mRenderer.PushBack( renderer );
282 void Node::RemoveRenderer( Renderer* renderer )
284 unsigned int rendererCount( mRenderer.Size() );
285 for( unsigned int i(0); i<rendererCount; ++i )
287 if( mRenderer[i] == renderer )
289 mRenderer.Erase( mRenderer.Begin()+i);
295 int Node::GetDirtyFlags() const
297 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
298 int flags = mDirtyFlags;
300 // Check whether the visible property has changed
301 if ( !mVisible.IsClean() )
303 flags |= VisibleFlag;
306 // Check whether the color property has changed
307 if ( !mColor.IsClean() )
315 void Node::ResetDirtyFlags( BufferIndex updateBufferIndex )
317 mDirtyFlags = NothingFlag;
320 void Node::SetParent( Node& parentNode )
322 DALI_ASSERT_ALWAYS(this != &parentNode);
323 DALI_ASSERT_ALWAYS(!mIsRoot);
324 DALI_ASSERT_ALWAYS(mParent == NULL);
326 mParent = &parentNode;
328 if( mTransformId != INVALID_TRANSFORM_ID )
330 mTransformManager->SetParent( mTransformId, parentNode.GetTransformId() );
334 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex )
336 DALI_ASSERT_ALWAYS(!mIsRoot);
337 DALI_ASSERT_ALWAYS(mParent != NULL);
339 const NodeIter endIter = mChildren.End();
340 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
342 (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex );
345 // Animators, Constraints etc. should be disconnected from the child's properties.
346 PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
348 // Remove back-pointer to parent
351 // Remove all child pointers
354 if( mTransformId != INVALID_TRANSFORM_ID )
356 mTransformManager->SetParent( mTransformId, INVALID_TRANSFORM_ID );
360 } // namespace SceneGraph
362 } // namespace Internal