2 * Copyright (c) 2014 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/node-attachments/node-attachment.h>
25 #include <dali/internal/update/common/discard-queue.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/common/constants.h>
29 namespace //Unnamed namespace
31 //Memory pool used to allocate new nodes. Memory used by this pool will be released when shutting down DALi
32 Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Node> gNodeMemoryPool;
44 const PositionInheritanceMode Node::DEFAULT_POSITION_INHERITANCE_MODE( INHERIT_PARENT_POSITION );
45 const ColorMode Node::DEFAULT_COLOR_MODE( USE_OWN_MULTIPLY_PARENT_ALPHA );
49 return new ( gNodeMemoryPool.AllocateRawThreadSafe() ) Node();
53 : mParentOrigin( ParentOrigin::DEFAULT ),
54 mAnchorPoint( AnchorPoint::DEFAULT ),
55 mSize(), // zero initialized by default
56 mPosition(), // zero initialized by default
57 mOrientation(), // initialized to identity by default
58 mScale( Vector3::ONE ),
60 mColor( Color::WHITE ),
61 mWorldPosition(), // zero initialized by default
62 mWorldOrientation(), // initialized to identity by default
63 mWorldScale( Vector3::ONE ),
65 mWorldColor( Color::WHITE ),
67 mExclusiveRenderTask( NULL ),
70 mRegenerateUniformMap( 0 ),
72 mDirtyFlags(AllFlags),
74 mInheritPosition( true ),
75 mInheritOrientation( true ),
76 mInheritScale( true ),
77 mDrawMode( DrawMode::NORMAL ),
78 mPositionInheritanceMode( DEFAULT_POSITION_INHERITANCE_MODE ),
79 mColorMode( DEFAULT_COLOR_MODE )
81 mUniformMapChanged[0] = 0u;
82 mUniformMapChanged[1] = 0u;
89 void Node::operator delete( void* ptr )
91 gNodeMemoryPool.FreeThreadSafe( static_cast<Node*>( ptr ) );
94 void Node::OnDestroy()
96 // Node attachments should be notified about the disconnection.
99 mAttachment->OnDestroy();
102 // Animators, Constraints etc. should be disconnected from the child's properties.
103 PropertyOwner::Destroy();
106 void Node::Attach( NodeAttachment& object )
108 DALI_ASSERT_DEBUG(!mAttachment);
110 object.SetParent(*this);
112 mAttachment = &object;
115 mAttachment->ConnectedToSceneGraph();
118 void Node::SetRoot(bool isRoot)
120 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
125 void Node::AddUniformMapping( UniformPropertyMapping* map )
127 PropertyOwner::AddUniformMapping( map );
128 mRegenerateUniformMap = 2;
131 void Node::RemoveUniformMapping( const std::string& uniformName )
133 PropertyOwner::RemoveUniformMapping( uniformName );
134 mRegenerateUniformMap = 2;
137 void Node::PrepareRender( BufferIndex bufferIndex )
139 if(mRegenerateUniformMap != 0 )
141 if( mRegenerateUniformMap == 2 )
143 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
146 for( unsigned int i=0, count=mUniformMaps.Count(); i<count; ++i )
148 localMap.PushBack( &mUniformMaps[i] );
151 else if( mRegenerateUniformMap == 1 )
153 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
154 CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-bufferIndex ];
156 localMap.Resize( oldMap.Count() );
158 unsigned int index=0;
159 for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
161 localMap[index] = *iter;
164 --mRegenerateUniformMap;
165 mUniformMapChanged[bufferIndex] = 1u;
169 void Node::ConnectChild( Node* childNode )
171 DALI_ASSERT_ALWAYS( this != childNode );
172 DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
173 DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
175 childNode->SetParent( *this );
177 // Everything should be reinherited when reconnected to scene-graph
178 childNode->SetAllDirtyFlags();
180 // Add the node to the end of the child list.
181 mChildren.PushBack( childNode );
183 // Inform property observers of new connection
184 childNode->ConnectToSceneGraph();
186 // Inform child node attachment that the node has been added to the stage
187 if( childNode->mAttachment )
189 childNode->mAttachment->ConnectedToSceneGraph();
193 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode )
195 DALI_ASSERT_ALWAYS( this != &childNode );
196 DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
198 // Find the childNode and remove it
201 const NodeIter endIter = mChildren.End();
202 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
204 Node* current = *iter;
205 if ( current == &childNode )
208 mChildren.Erase( iter ); // order matters here
209 break; // iter is no longer valid
212 DALI_ASSERT_ALWAYS( NULL != found );
214 found->RecursiveDisconnectFromSceneGraph( updateBufferIndex );
217 void Node::RemoveRenderer( Renderer* renderer )
219 unsigned int rendererCount( mRenderer.Size() );
220 for( unsigned int i(0); i<rendererCount; ++i )
222 if( mRenderer[i] == renderer )
224 mRenderer.Erase( mRenderer.Begin()+i);
230 int Node::GetDirtyFlags() const
232 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
233 int flags = mDirtyFlags;
234 const bool sizeFlag = mSize.IsClean();
236 if ( !(flags & TransformFlag) )
238 // Check whether the transform related properties have changed
240 !mPosition.IsClean() ||
241 !mOrientation.IsClean() ||
243 mParentOrigin.InputChanged() || // parent origin and anchor point rarely change
244 mAnchorPoint.InputChanged() )
246 flags |= TransformFlag;
250 // Check whether the visible property has changed
251 if ( !mVisible.IsClean() )
253 flags |= VisibleFlag;
256 // Check whether the color property has changed
257 if ( !mColor.IsClean() )
262 // Check whether the size property has changed
271 void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
273 // clear dirty flags in parent origin & anchor point
274 mParentOrigin.Clear();
275 mAnchorPoint.Clear();
276 // Reset default properties
277 mSize.ResetToBaseValue( updateBufferIndex );
278 mPosition.ResetToBaseValue( updateBufferIndex );
279 mOrientation.ResetToBaseValue( updateBufferIndex );
280 mScale.ResetToBaseValue( updateBufferIndex );
281 mVisible.ResetToBaseValue( updateBufferIndex );
282 mColor.ResetToBaseValue( updateBufferIndex );
284 mDirtyFlags = NothingFlag;
287 void Node::SetParent(Node& parentNode)
289 DALI_ASSERT_ALWAYS(this != &parentNode);
290 DALI_ASSERT_ALWAYS(!mIsRoot);
291 DALI_ASSERT_ALWAYS(mParent == NULL);
293 mParent = &parentNode;
294 mDepth = mParent->GetDepth() + 1u;
297 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex )
299 DALI_ASSERT_ALWAYS(!mIsRoot);
300 DALI_ASSERT_ALWAYS(mParent != NULL);
302 const NodeIter endIter = mChildren.End();
303 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
305 (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex );
308 // Animators, Constraints etc. should be disconnected from the child's properties.
309 PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
311 // Remove back-pointer to parent
315 // Remove all child pointers
318 // Inform child node attachment that the node has been removed from the stage
321 mAttachment->DisconnectedFromSceneGraph();
326 } // namespace SceneGraph
328 } // namespace Internal