Remove some redundant&dead code from node
[platform/core/uifw/dali-core.git] / dali / internal / update / nodes / node.cpp
1 /*
2  * Copyright (c) 2014 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/update/node-attachments/node-attachment.h>
23 #include <dali/internal/update/common/discard-queue.h>
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/public-api/common/constants.h>
26 #include <dali/internal/common/internal-constants.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace SceneGraph
35 {
36
37 const PositionInheritanceMode Node::DEFAULT_POSITION_INHERITANCE_MODE( INHERIT_PARENT_POSITION );
38 const ColorMode Node::DEFAULT_COLOR_MODE( USE_OWN_MULTIPLY_PARENT_ALPHA );
39
40 Node* Node::New()
41 {
42   return new Node();
43 }
44
45 Node::Node()
46 : mParentOrigin( ParentOrigin::DEFAULT ),
47   mAnchorPoint( AnchorPoint::DEFAULT ),
48   mSize(),     // zero initialized by default
49   mPosition(), // zero initialized by default
50   mOrientation(), // initialized to identity by default
51   mScale( Vector3::ONE ),
52   mVisible( true ),
53   mColor( Color::WHITE ),
54   mWorldPosition(), // zero initialized by default
55   mWorldOrientation(), // initialized to identity by default
56   mWorldScale( Vector3::ONE ),
57   mWorldMatrix(),
58   mWorldColor( Color::WHITE ),
59   mParent( NULL ),
60   mExclusiveRenderTask( NULL ),
61   mAttachment( NULL ),
62   mChildren(),
63   mRegenerateUniformMap( 0 ),
64   mDepth(0u),
65   mDirtyFlags(AllFlags),
66   mIsRoot( false ),
67   mInheritOrientation( true ),
68   mInheritScale( true ),
69   mIsActive( true ),
70   mDrawMode( DrawMode::NORMAL ),
71   mPositionInheritanceMode( DEFAULT_POSITION_INHERITANCE_MODE ),
72   mColorMode( DEFAULT_COLOR_MODE )
73 {
74   mUniformMapChanged[0] = 0u;
75   mUniformMapChanged[1] = 0u;
76 }
77
78 Node::~Node()
79 {
80 }
81
82 void Node::OnDestroy()
83 {
84   // Node attachments should be notified about the disconnection.
85   if ( mAttachment )
86   {
87     mAttachment->OnDestroy();
88   }
89
90   // Animators, Constraints etc. should be disconnected from the child's properties.
91   PropertyOwner::Destroy();
92 }
93
94 void Node::Attach( NodeAttachment& object )
95 {
96   DALI_ASSERT_DEBUG(!mAttachment);
97
98   object.SetParent(*this);
99
100   mAttachment = &object;
101   SetAllDirtyFlags();
102
103   if( mIsActive )
104   {
105     mAttachment->ConnectedToSceneGraph();
106   }
107 }
108
109 void Node::SetRoot(bool isRoot)
110 {
111   DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
112
113   mIsRoot = isRoot;
114 }
115
116 bool Node::ResolveVisibility( BufferIndex updateBufferIndex )
117 {
118   bool result = false;
119   const Vector4& color = GetWorldColor( updateBufferIndex );
120   if( color.a > FULLY_TRANSPARENT )               // not fully transparent
121   {
122     const float MAX_NODE_SIZE = float(1u<<30);
123     const Vector3& size = GetSize( updateBufferIndex );
124     if( ( size.width > Math::MACHINE_EPSILON_1000 ) &&  // width is greater than a very small number
125         ( size.height > Math::MACHINE_EPSILON_1000 ) )  // height is greater than a very small number
126     {
127       if( ( size.width < MAX_NODE_SIZE ) &&             // width is smaller than the maximum allowed size
128           ( size.height < MAX_NODE_SIZE ) )             // height is smaller than the maximum allowed size
129       {
130         result = true;
131       }
132       else
133       {
134         DALI_LOG_ERROR("Actor size should not be bigger than %f.\n", MAX_NODE_SIZE );
135         DALI_LOG_ACTOR_TREE( mParent );
136       }
137     }
138   }
139   return result;
140 }
141
142 void Node::AddUniformMapping( UniformPropertyMapping* map )
143 {
144   PropertyOwner::AddUniformMapping( map );
145   mRegenerateUniformMap = 2;
146 }
147
148 void Node::RemoveUniformMapping( const std::string& uniformName )
149 {
150   PropertyOwner::RemoveUniformMapping( uniformName );
151   mRegenerateUniformMap = 2;
152 }
153
154 void Node::PrepareRender( BufferIndex bufferIndex )
155 {
156   if(mRegenerateUniformMap != 0 )
157   {
158     if( mRegenerateUniformMap == 2 )
159     {
160       CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
161       localMap.Resize(0);
162
163       for( unsigned int i=0, count=mUniformMaps.Count(); i<count; ++i )
164       {
165         localMap.PushBack( &mUniformMaps[i] );
166       }
167     }
168     else if( mRegenerateUniformMap == 1 )
169     {
170       CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
171       CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-bufferIndex ];
172
173       localMap.Resize( oldMap.Count() );
174
175       unsigned int index=0;
176       for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
177       {
178         localMap[index] = *iter;
179       }
180     }
181     --mRegenerateUniformMap;
182     mUniformMapChanged[bufferIndex] = 1u;
183   }
184 }
185
186 void Node::ConnectChild( Node* childNode )
187 {
188   DALI_ASSERT_ALWAYS( this != childNode );
189   DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
190   DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
191
192   childNode->SetParent( *this );
193
194   // Everything should be reinherited when reconnected to scene-graph
195   childNode->SetAllDirtyFlags();
196
197   // Add the node to the end of the child list.
198   mChildren.PushBack( childNode );
199
200   // Inform property observers of new connection
201   childNode->ConnectToSceneGraph();
202
203   // Inform child node attachment that the node has been added to the stage
204   if( childNode->mAttachment )
205   {
206     childNode->mAttachment->ConnectedToSceneGraph();
207   }
208 }
209
210 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes,  std::set<Node*>& disconnectedNodes )
211 {
212   DALI_ASSERT_ALWAYS( this != &childNode );
213   DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
214
215   // Find the childNode and remove it
216   Node* found( NULL );
217
218   const NodeIter endIter = mChildren.End();
219   for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
220   {
221     Node* current = *iter;
222     if ( current == &childNode )
223     {
224       found = current;
225       mChildren.Erase( iter ); // order matters here
226       break; // iter is no longer valid
227     }
228   }
229   DALI_ASSERT_ALWAYS( NULL != found );
230
231   found->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
232 }
233
234 void Node::RemoveRenderer( Renderer* renderer )
235 {
236   unsigned int rendererCount( mRenderer.Size() );
237   for( unsigned int i(0); i<rendererCount; ++i )
238   {
239     if( mRenderer[i] == renderer )
240     {
241       mRenderer.Erase( mRenderer.Begin()+i);
242       return;
243     }
244   }
245 }
246
247 int Node::GetDirtyFlags() const
248 {
249   // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
250   int flags = mDirtyFlags;
251   const bool sizeFlag = mSize.IsClean();
252
253   if ( !(flags & TransformFlag) )
254   {
255     // Check whether the transform related properties have changed
256     if( !sizeFlag            ||
257         !mPosition.IsClean() ||
258         !mOrientation.IsClean() ||
259         !mScale.IsClean()    ||
260         mParentOrigin.InputChanged() || // parent origin and anchor point rarely change
261         mAnchorPoint.InputChanged() )
262     {
263       flags |= TransformFlag;
264     }
265   }
266
267   // Check whether the visible property has changed
268   if ( !mVisible.IsClean() )
269   {
270     flags |= VisibleFlag;
271   }
272
273   // Check whether the color property has changed
274   if ( !mColor.IsClean() )
275   {
276     flags |= ColorFlag;
277   }
278
279   // Check whether the size property has changed
280   if ( !sizeFlag )
281   {
282     flags |= SizeFlag;
283    }
284
285   return flags;
286 }
287
288 void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
289 {
290   // clear dirty flags in parent origin & anchor point
291   mParentOrigin.Clear();
292   mAnchorPoint.Clear();
293   // Reset default properties
294   mSize.ResetToBaseValue( updateBufferIndex );
295   mPosition.ResetToBaseValue( updateBufferIndex );
296   mOrientation.ResetToBaseValue( updateBufferIndex );
297   mScale.ResetToBaseValue( updateBufferIndex );
298   mVisible.ResetToBaseValue( updateBufferIndex );
299   mColor.ResetToBaseValue( updateBufferIndex );
300
301   mDirtyFlags = NothingFlag;
302 }
303
304 void Node::SetParent(Node& parentNode)
305 {
306   DALI_ASSERT_ALWAYS(this != &parentNode);
307   DALI_ASSERT_ALWAYS(!mIsRoot);
308   DALI_ASSERT_ALWAYS(mParent == NULL);
309
310   mParent = &parentNode;
311   mDepth = mParent->GetDepth() + 1u;
312 }
313
314 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex, std::set<Node*>& connectedNodes,  std::set<Node*>& disconnectedNodes )
315 {
316   DALI_ASSERT_ALWAYS(!mIsRoot);
317   DALI_ASSERT_ALWAYS(mParent != NULL);
318
319   const NodeIter endIter = mChildren.End();
320   for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
321   {
322     (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
323   }
324
325   // Animators, Constraints etc. should be disconnected from the child's properties.
326   PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
327
328   // Remove back-pointer to parent
329   mParent = NULL;
330   mDepth = 0u;
331
332   // Remove all child pointers
333   mChildren.Clear();
334
335   // Inform child node attachment that the node has been removed from the stage
336   if( mAttachment )
337   {
338     mAttachment->DisconnectedFromSceneGraph();
339   }
340
341   // Move into disconnectedNodes
342   std::set<Node*>::size_type removed = connectedNodes.erase( this );
343   DALI_ASSERT_ALWAYS( removed );
344   disconnectedNodes.insert( this );
345 }
346
347 } // namespace SceneGraph
348
349 } // namespace Internal
350
351 } // namespace Dali