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 mInheritOrientation( true ),
75 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;
117 mAttachment->ConnectedToSceneGraph();
121 void Node::SetRoot(bool isRoot)
123 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
128 void Node::AddUniformMapping( UniformPropertyMapping* map )
130 PropertyOwner::AddUniformMapping( map );
131 mRegenerateUniformMap = 2;
134 void Node::RemoveUniformMapping( const std::string& uniformName )
136 PropertyOwner::RemoveUniformMapping( uniformName );
137 mRegenerateUniformMap = 2;
140 void Node::PrepareRender( BufferIndex bufferIndex )
142 if(mRegenerateUniformMap != 0 )
144 if( mRegenerateUniformMap == 2 )
146 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
149 for( unsigned int i=0, count=mUniformMaps.Count(); i<count; ++i )
151 localMap.PushBack( &mUniformMaps[i] );
154 else if( mRegenerateUniformMap == 1 )
156 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
157 CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-bufferIndex ];
159 localMap.Resize( oldMap.Count() );
161 unsigned int index=0;
162 for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
164 localMap[index] = *iter;
167 --mRegenerateUniformMap;
168 mUniformMapChanged[bufferIndex] = 1u;
172 void Node::ConnectChild( Node* childNode )
174 DALI_ASSERT_ALWAYS( this != childNode );
175 DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
176 DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
178 childNode->SetParent( *this );
180 // Everything should be reinherited when reconnected to scene-graph
181 childNode->SetAllDirtyFlags();
183 // Add the node to the end of the child list.
184 mChildren.PushBack( childNode );
186 // Inform property observers of new connection
187 childNode->ConnectToSceneGraph();
189 // Inform child node attachment that the node has been added to the stage
190 if( childNode->mAttachment )
192 childNode->mAttachment->ConnectedToSceneGraph();
196 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
198 DALI_ASSERT_ALWAYS( this != &childNode );
199 DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
201 // Find the childNode and remove it
204 const NodeIter endIter = mChildren.End();
205 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
207 Node* current = *iter;
208 if ( current == &childNode )
211 mChildren.Erase( iter ); // order matters here
212 break; // iter is no longer valid
215 DALI_ASSERT_ALWAYS( NULL != found );
217 found->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
220 void Node::RemoveRenderer( Renderer* renderer )
222 unsigned int rendererCount( mRenderer.Size() );
223 for( unsigned int i(0); i<rendererCount; ++i )
225 if( mRenderer[i] == renderer )
227 mRenderer.Erase( mRenderer.Begin()+i);
233 int Node::GetDirtyFlags() const
235 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
236 int flags = mDirtyFlags;
237 const bool sizeFlag = mSize.IsClean();
239 if ( !(flags & TransformFlag) )
241 // Check whether the transform related properties have changed
243 !mPosition.IsClean() ||
244 !mOrientation.IsClean() ||
246 mParentOrigin.InputChanged() || // parent origin and anchor point rarely change
247 mAnchorPoint.InputChanged() )
249 flags |= TransformFlag;
253 // Check whether the visible property has changed
254 if ( !mVisible.IsClean() )
256 flags |= VisibleFlag;
259 // Check whether the color property has changed
260 if ( !mColor.IsClean() )
265 // Check whether the size property has changed
274 void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
276 // clear dirty flags in parent origin & anchor point
277 mParentOrigin.Clear();
278 mAnchorPoint.Clear();
279 // Reset default properties
280 mSize.ResetToBaseValue( updateBufferIndex );
281 mPosition.ResetToBaseValue( updateBufferIndex );
282 mOrientation.ResetToBaseValue( updateBufferIndex );
283 mScale.ResetToBaseValue( updateBufferIndex );
284 mVisible.ResetToBaseValue( updateBufferIndex );
285 mColor.ResetToBaseValue( updateBufferIndex );
287 mDirtyFlags = NothingFlag;
290 void Node::SetParent(Node& parentNode)
292 DALI_ASSERT_ALWAYS(this != &parentNode);
293 DALI_ASSERT_ALWAYS(!mIsRoot);
294 DALI_ASSERT_ALWAYS(mParent == NULL);
296 mParent = &parentNode;
297 mDepth = mParent->GetDepth() + 1u;
300 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
302 DALI_ASSERT_ALWAYS(!mIsRoot);
303 DALI_ASSERT_ALWAYS(mParent != NULL);
305 const NodeIter endIter = mChildren.End();
306 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
308 (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
311 // Animators, Constraints etc. should be disconnected from the child's properties.
312 PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
314 // Remove back-pointer to parent
318 // Remove all child pointers
321 // Inform child node attachment that the node has been removed from the stage
324 mAttachment->DisconnectedFromSceneGraph();
327 // Move into disconnectedNodes
328 std::set<Node*>::size_type removed = connectedNodes.erase( this );
329 DALI_ASSERT_ALWAYS( removed );
330 disconnectedNodes.insert( this );
333 } // namespace SceneGraph
335 } // namespace Internal