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 ),
76 mDrawMode( DrawMode::NORMAL ),
77 mPositionInheritanceMode( DEFAULT_POSITION_INHERITANCE_MODE ),
78 mColorMode( DEFAULT_COLOR_MODE )
80 mUniformMapChanged[0] = 0u;
81 mUniformMapChanged[1] = 0u;
88 void Node::operator delete( void* ptr )
90 gNodeMemoryPool.FreeThreadSafe( static_cast<Node*>( ptr ) );
93 void Node::OnDestroy()
95 // Node attachments should be notified about the disconnection.
98 mAttachment->OnDestroy();
101 // Animators, Constraints etc. should be disconnected from the child's properties.
102 PropertyOwner::Destroy();
105 void Node::Attach( NodeAttachment& object )
107 DALI_ASSERT_DEBUG(!mAttachment);
109 object.SetParent(*this);
111 mAttachment = &object;
114 mAttachment->ConnectedToSceneGraph();
117 void Node::SetRoot(bool isRoot)
119 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
124 void Node::AddUniformMapping( UniformPropertyMapping* map )
126 PropertyOwner::AddUniformMapping( map );
127 mRegenerateUniformMap = 2;
130 void Node::RemoveUniformMapping( const std::string& uniformName )
132 PropertyOwner::RemoveUniformMapping( uniformName );
133 mRegenerateUniformMap = 2;
136 void Node::PrepareRender( BufferIndex bufferIndex )
138 if(mRegenerateUniformMap != 0 )
140 if( mRegenerateUniformMap == 2 )
142 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
145 for( unsigned int i=0, count=mUniformMaps.Count(); i<count; ++i )
147 localMap.PushBack( &mUniformMaps[i] );
150 else if( mRegenerateUniformMap == 1 )
152 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
153 CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-bufferIndex ];
155 localMap.Resize( oldMap.Count() );
157 unsigned int index=0;
158 for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
160 localMap[index] = *iter;
163 --mRegenerateUniformMap;
164 mUniformMapChanged[bufferIndex] = 1u;
168 void Node::ConnectChild( Node* childNode )
170 DALI_ASSERT_ALWAYS( this != childNode );
171 DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
172 DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
174 childNode->SetParent( *this );
176 // Everything should be reinherited when reconnected to scene-graph
177 childNode->SetAllDirtyFlags();
179 // Add the node to the end of the child list.
180 mChildren.PushBack( childNode );
182 // Inform property observers of new connection
183 childNode->ConnectToSceneGraph();
185 // Inform child node attachment that the node has been added to the stage
186 if( childNode->mAttachment )
188 childNode->mAttachment->ConnectedToSceneGraph();
192 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode )
194 DALI_ASSERT_ALWAYS( this != &childNode );
195 DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
197 // Find the childNode and remove it
200 const NodeIter endIter = mChildren.End();
201 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
203 Node* current = *iter;
204 if ( current == &childNode )
207 mChildren.Erase( iter ); // order matters here
208 break; // iter is no longer valid
211 DALI_ASSERT_ALWAYS( NULL != found );
213 found->RecursiveDisconnectFromSceneGraph( updateBufferIndex );
216 void Node::RemoveRenderer( Renderer* renderer )
218 unsigned int rendererCount( mRenderer.Size() );
219 for( unsigned int i(0); i<rendererCount; ++i )
221 if( mRenderer[i] == renderer )
223 mRenderer.Erase( mRenderer.Begin()+i);
229 int Node::GetDirtyFlags() const
231 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
232 int flags = mDirtyFlags;
233 const bool sizeFlag = mSize.IsClean();
235 if ( !(flags & TransformFlag) )
237 // Check whether the transform related properties have changed
239 !mPosition.IsClean() ||
240 !mOrientation.IsClean() ||
242 mParentOrigin.InputChanged() || // parent origin and anchor point rarely change
243 mAnchorPoint.InputChanged() )
245 flags |= TransformFlag;
249 // Check whether the visible property has changed
250 if ( !mVisible.IsClean() )
252 flags |= VisibleFlag;
255 // Check whether the color property has changed
256 if ( !mColor.IsClean() )
261 // Check whether the size property has changed
270 void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
272 // clear dirty flags in parent origin & anchor point
273 mParentOrigin.Clear();
274 mAnchorPoint.Clear();
275 // Reset default properties
276 mSize.ResetToBaseValue( updateBufferIndex );
277 mPosition.ResetToBaseValue( updateBufferIndex );
278 mOrientation.ResetToBaseValue( updateBufferIndex );
279 mScale.ResetToBaseValue( updateBufferIndex );
280 mVisible.ResetToBaseValue( updateBufferIndex );
281 mColor.ResetToBaseValue( updateBufferIndex );
283 mDirtyFlags = NothingFlag;
286 void Node::SetParent(Node& parentNode)
288 DALI_ASSERT_ALWAYS(this != &parentNode);
289 DALI_ASSERT_ALWAYS(!mIsRoot);
290 DALI_ASSERT_ALWAYS(mParent == NULL);
292 mParent = &parentNode;
293 mDepth = mParent->GetDepth() + 1u;
296 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex )
298 DALI_ASSERT_ALWAYS(!mIsRoot);
299 DALI_ASSERT_ALWAYS(mParent != NULL);
301 const NodeIter endIter = mChildren.End();
302 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
304 (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex );
307 // Animators, Constraints etc. should be disconnected from the child's properties.
308 PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
310 // Remove back-pointer to parent
314 // Remove all child pointers
317 // Inform child node attachment that the node has been removed from the stage
320 mAttachment->DisconnectedFromSceneGraph();
325 } // namespace SceneGraph
327 } // namespace Internal