7e24335f13d27ac49b7f90dd7c5c943c6a10b9d4
[platform/core/uifw/dali-core.git] / dali / internal / update / nodes / node.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_NODE_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_NODE_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/actors/actor-enumerations.h>
23 #include <dali/public-api/actors/draw-mode.h>
24 #include <dali/devel-api/common/set-wrapper.h>
25 #include <dali/public-api/math/quaternion.h>
26 #include <dali/public-api/math/math-utils.h>
27 #include <dali/public-api/math/vector3.h>
28 #include <dali/internal/common/message.h>
29 #include <dali/internal/event/common/event-thread-services.h>
30 #include <dali/internal/update/common/animatable-property.h>
31 #include <dali/internal/update/common/property-owner.h>
32 #include <dali/internal/update/common/property-vector3.h>
33 #include <dali/internal/update/common/scene-graph-buffers.h>
34 #include <dali/internal/update/common/inherited-property.h>
35 #include <dali/integration-api/debug.h>
36 #include <dali/internal/update/nodes/node-declarations.h>
37 #include <dali/internal/update/node-attachments/node-attachment-declarations.h>
38 #include <dali/internal/render/data-providers/node-data-provider.h>
39 #include <dali/internal/update/rendering/scene-graph-renderer.h>
40
41 namespace Dali
42 {
43
44 namespace Internal
45 {
46
47 // value types used by messages
48 template <> struct ParameterType< ColorMode > : public BasicType< ColorMode > {};
49 template <> struct ParameterType< PositionInheritanceMode > : public BasicType< PositionInheritanceMode > {};
50
51 namespace SceneGraph
52 {
53
54 class DiscardQueue;
55 class Layer;
56 class NodeAttachment;
57 class RenderTask;
58 class UpdateManager;
59
60 /**
61  * Flag whether property has changed, during the Update phase.
62  */
63 enum NodePropertyFlags
64 {
65   NothingFlag          = 0x000,
66   TransformFlag        = 0x001,
67   VisibleFlag          = 0x002,
68   ColorFlag            = 0x004,
69   SizeFlag             = 0x008,
70   OverlayFlag          = 0x010,
71   SortModifierFlag     = 0x020,
72   ChildDeletedFlag     = 0x040
73 };
74
75 static const int AllFlags = ( ChildDeletedFlag << 1 ) - 1; // all the flags
76
77 /**
78  * Size is not inherited.
79  * VisibleFlag is inherited so that attachments can be synchronized with nodes after they become visible
80  */
81 static const int InheritedDirtyFlags = TransformFlag | VisibleFlag | ColorFlag | OverlayFlag;
82
83 // Flags which require the scene renderable lists to be updated
84 static const int RenderableUpdateFlags = TransformFlag | SortModifierFlag | ChildDeletedFlag;
85
86 /**
87  * Node is the base class for all nodes in the Scene Graph.
88  *
89  * Each node provides a transformation which applies to the node and
90  * its children.  Node data is double-buffered. This allows an update
91  * thread to modify node data, without interferring with another
92  * thread reading the values from the previous update traversal.
93  */
94 class Node : public PropertyOwner, public NodeDataProvider
95 {
96 public:
97
98   // Defaults
99   static const PositionInheritanceMode DEFAULT_POSITION_INHERITANCE_MODE;
100   static const ColorMode DEFAULT_COLOR_MODE;
101
102   // Creation methods
103
104   /**
105    * Construct a new Node.
106    */
107   static Node* New();
108
109   /**
110    * Virtual destructor
111    */
112   virtual ~Node();
113
114   /**
115    * Overriden delete operator
116    * Deletes the node from its global memory pool
117    */
118   void operator delete( void* ptr );
119
120   /**
121    * Called during UpdateManager::DestroyNode shortly before Node is destroyed.
122    */
123   void OnDestroy();
124
125   // Layer interface
126
127   /**
128    * Query whether the node is a layer.
129    * @return True if the node is a layer.
130    */
131   bool IsLayer()
132   {
133     return (GetLayer() != NULL);
134   }
135
136   /**
137    * Convert a node to a layer.
138    * @return A pointer to the layer, or NULL.
139    */
140   virtual Layer* GetLayer()
141   {
142     return NULL;
143   }
144
145   // Attachments
146
147   /**
148    * Attach an object to this Node; This should only be done by UpdateManager::AttachToNode.
149    * @pre The Node does not already have an attachment.
150    * @param[in] attachment The object to attach.
151    */
152   void Attach( NodeAttachment& attachment );
153
154   /**
155    * Query the node if it has an attachment.
156    * @return True if it has an attachment.
157    */
158   bool HasAttachment() const
159   {
160     return mAttachment;
161   }
162
163   /**
164    * Add a renderer to the node
165    * @param[in] renderer The renderer added to the node
166    */
167   void AddRenderer( Renderer* renderer )
168   {
169     //Check that it has not been already added
170     unsigned int rendererCount( mRenderer.Size() );
171     for( unsigned int i(0); i<rendererCount; ++i )
172     {
173       if( mRenderer[i] == renderer )
174       {
175         //Renderer already in the list
176         return;
177       }
178     }
179
180     //If it is the first renderer added, make sure the world transform will be calculated
181     //in the next update as world transform is not computed if node has no renderers
182     if( rendererCount == 0 )
183     {
184       mDirtyFlags |= TransformFlag;
185     }
186
187     mRenderer.PushBack( renderer );
188   }
189
190   /**
191    * Remove a renderer from the node
192    * @param[in] renderer The renderer to be removed
193    */
194   void RemoveRenderer( Renderer* renderer );
195
196   /*
197    * Get the renderer at the given index
198    * @param[in] index
199    */
200   Renderer* GetRendererAt( unsigned int index )
201   {
202     return mRenderer[index];
203   }
204
205   /**
206    * Retrieve the number of renderers for the node
207    */
208   unsigned int GetRendererCount()
209   {
210     return mRenderer.Size();
211   }
212
213   /**
214    * Retreive the object attached to this node.
215    * @return The attachment.
216    */
217   NodeAttachment& GetAttachment() const
218   {
219     return *mAttachment;
220   }
221
222   // Containment methods
223
224   /**
225    * Query whether a node is the root node. Root nodes cannot have a parent node.
226    * A node becomes a root node, when it is installed by UpdateManager.
227    * @return True if the node is a root node.
228    */
229   bool IsRoot() const
230   {
231     return mIsRoot;
232   }
233
234   /**
235    * Set whether a node is the root node. Root nodes cannot have a parent node.
236    * This method should only be called by UpdateManager.
237    * @pre When isRoot is true, the node must not have a parent.
238    * @param[in] isRoot Whether the node is now a root node.
239    */
240   void SetRoot(bool isRoot);
241
242   /**
243    * Retrieve the parent of a Node.
244    * @return The parent node, or NULL if the Node has not been added to the scene-graph.
245    */
246   Node* GetParent()
247   {
248     return mParent;
249   }
250
251   /**
252    * Retrieve the parent of a Node.
253    * @return The parent node, or NULL if the Node has not been added to the scene-graph.
254    */
255   const Node* GetParent() const
256   {
257     return mParent;
258   }
259
260   /**
261    * Connect a node to the scene-graph.
262    * @pre A node cannot be added to itself.
263    * @pre The parent node is connected to the scene-graph.
264    * @pre The childNode does not already have a parent.
265    * @pre The childNode is not a root node.
266    * @param[in] childNode The child to add.
267    */
268   void ConnectChild( Node* childNode );
269
270   /**
271    * Disconnect a child (& its children) from the scene-graph.
272    * @pre childNode is a child of this Node.
273    * @param[in] updateBufferIndex The current update buffer index.
274    * @param[in] childNode The node to disconnect.
275    */
276   void DisconnectChild( BufferIndex updateBufferIndex, Node& childNode );
277
278   /**
279    * Retrieve the children a Node.
280    * @return The container of children.
281    */
282   const NodeContainer& GetChildren() const
283   {
284     return mChildren;
285   }
286
287   /**
288    * Retrieve the children a Node.
289    * @return The container of children.
290    */
291   NodeContainer& GetChildren()
292   {
293     return mChildren;
294   }
295
296   // Update methods
297
298   /**
299    * Flag that one of the node values has changed in the current frame.
300    * @param[in] flag The flag to set.
301    */
302   void SetDirtyFlag(NodePropertyFlags flag)
303   {
304     mDirtyFlags |= flag;
305   }
306
307   /**
308    * Flag that all of the node values are dirty.
309    */
310   void SetAllDirtyFlags()
311   {
312     mDirtyFlags = AllFlags;
313   }
314
315   /**
316    * Query whether a node is dirty.
317    * @return The dirty flags
318    */
319   int GetDirtyFlags() const;
320
321   /**
322    * Query whether a node is clean.
323    * @return True if the node is clean.
324    */
325   bool IsClean() const
326   {
327     return ( NothingFlag == GetDirtyFlags() );
328   }
329
330   /**
331    * Retrieve the parent-origin of the node.
332    * @return The parent-origin.
333    */
334   const Vector3& GetParentOrigin() const
335   {
336     return mParentOrigin.mValue;
337   }
338
339   /**
340    * Sets both the local & base parent-origins of the node.
341    * @param[in] origin The new local & base parent-origins.
342    */
343   void SetParentOrigin(const Vector3& origin)
344   {
345     mParentOrigin.mValue = origin;
346     mParentOrigin.OnSet();
347   }
348
349   /**
350    * Retrieve the anchor-point of the node.
351    * @return The anchor-point.
352    */
353   const Vector3& GetAnchorPoint() const
354   {
355     return mAnchorPoint.mValue;
356   }
357
358   /**
359    * Sets both the local & base anchor-points of the node.
360    * @param[in] anchor The new local & base anchor-points.
361    */
362   void SetAnchorPoint(const Vector3& anchor)
363   {
364     mAnchorPoint.mValue = anchor;
365     mAnchorPoint.OnSet();
366   }
367
368   /**
369    * Retrieve the local position of the node, relative to its parent.
370    * @param[in] bufferIndex The buffer to read from.
371    * @return The local position.
372    */
373   const Vector3& GetPosition(BufferIndex bufferIndex) const
374   {
375     return mPosition[bufferIndex];
376   }
377
378   /**
379    * Sets both the local & base positions of the node.
380    * @param[in] updateBufferIndex The current update buffer index.
381    * @param[in] position The new local & base position.
382    */
383   void BakePosition(BufferIndex updateBufferIndex, const Vector3& position)
384   {
385     mPosition.Bake( updateBufferIndex, position );
386   }
387
388   /**
389    * Sets the world of the node derived from the position of all its parents.
390    * @param[in] updateBufferIndex The current update buffer index.
391    * @param[in] position The world position.
392    */
393   void SetWorldPosition( BufferIndex updateBufferIndex, const Vector3& position )
394   {
395     mWorldPosition.Set( updateBufferIndex, position );
396   }
397
398   /**
399    * Sets the position of the node derived from the position of all its parents.
400    * This method should only be called when the parent's world position is up-to-date.
401    * With a non-central anchor-point, the local orientation and scale affects the world position.
402    * Therefore the world orientation & scale must be updated before the world position.
403    * @pre The node has a parent.
404    * @param[in] updateBufferIndex The current update buffer index.
405    */
406   void InheritWorldPosition(BufferIndex updateBufferIndex)
407   {
408     DALI_ASSERT_DEBUG(mParent != NULL);
409
410     switch( mPositionInheritanceMode )
411     {
412       case INHERIT_PARENT_POSITION  : ///@see Dali::PositionInheritanceMode for how these modes are expected to work
413       {
414         Vector3 finalPosition(-0.5f, -0.5f, -0.5f);
415
416         finalPosition += mParentOrigin.mValue;
417         finalPosition *= mParent->GetSize(updateBufferIndex);
418         finalPosition += mPosition[updateBufferIndex];
419         finalPosition *= mParent->GetWorldScale(updateBufferIndex);
420         const Quaternion& parentWorldOrientation = mParent->GetWorldOrientation(updateBufferIndex);
421         if(!parentWorldOrientation.IsIdentity())
422         {
423           finalPosition *= parentWorldOrientation;
424         }
425
426         // check if a node needs to be offsetted locally (only applies when AnchorPoint is not central)
427         // dont use operator== as that does a slower comparison (and involves function calls)
428         Vector3 localOffset(0.5f, 0.5f, 0.5f);    // AnchorPoint::CENTER
429         localOffset -= mAnchorPoint.mValue;
430
431         if( ( fabsf( localOffset.x ) >= Math::MACHINE_EPSILON_0 ) ||
432             ( fabsf( localOffset.y ) >= Math::MACHINE_EPSILON_0 ) ||
433             ( fabsf( localOffset.z ) >= Math::MACHINE_EPSILON_0 ) )
434         {
435           localOffset *= mSize[updateBufferIndex];
436
437           Vector3 scale = mWorldScale[updateBufferIndex];
438
439           // Pick up sign of local scale
440           if (mScale[updateBufferIndex].x < 0.0f)
441           {
442             scale.x = -scale.x;
443           }
444           if (mScale[updateBufferIndex].y < 0.0f)
445           {
446             scale.y = -scale.y;
447           }
448           if (mScale[updateBufferIndex].z < 0.0f)
449           {
450             scale.z = -scale.z;
451           }
452
453           // If the anchor-point is not central, then position is affected by the local orientation & scale
454           localOffset *= scale;
455           const Quaternion& localWorldOrientation = mWorldOrientation[updateBufferIndex];
456           if(!localWorldOrientation.IsIdentity())
457           {
458             localOffset *= localWorldOrientation;
459           }
460           finalPosition += localOffset;
461         }
462
463         finalPosition += mParent->GetWorldPosition(updateBufferIndex);
464         mWorldPosition.Set( updateBufferIndex, finalPosition );
465         break;
466       }
467       case USE_PARENT_POSITION_PLUS_LOCAL_POSITION :
468       {
469         // copy parents position plus local transform
470         mWorldPosition.Set( updateBufferIndex, mParent->GetWorldPosition(updateBufferIndex) + mPosition[updateBufferIndex] );
471         break;
472       }
473       case USE_PARENT_POSITION :
474       {
475         // copy parents position
476         mWorldPosition.Set( updateBufferIndex, mParent->GetWorldPosition(updateBufferIndex) );
477         break;
478       }
479       case DONT_INHERIT_POSITION :
480       {
481         // use local position as world position
482         mWorldPosition.Set( updateBufferIndex, mPosition[updateBufferIndex] );
483         break;
484       }
485     }
486   }
487
488   /**
489    * Copies the previous inherited position, if this changed in the previous frame.
490    * This method should be called instead of InheritWorldPosition i.e. if the inherited position
491    * does not need to be recalculated in the current frame.
492    * @param[in] updateBufferIndex The current update buffer index.
493    */
494   void CopyPreviousWorldPosition( BufferIndex updateBufferIndex )
495   {
496     mWorldPosition.CopyPrevious( updateBufferIndex );
497   }
498
499   /**
500    * Retrieve the position of the node derived from the position of all its parents.
501    * @return The world position.
502    */
503   const Vector3& GetWorldPosition( BufferIndex bufferIndex ) const
504   {
505     return mWorldPosition[bufferIndex];
506   }
507
508   /**
509    * Set whether the Node inherits position.
510    * @param[in] inherit True if the parent position is inherited.
511    */
512   void SetInheritPosition(bool inherit)
513   {
514     if (inherit != mInheritPosition)
515     {
516       mInheritPosition = inherit;
517       mPositionInheritanceMode = inherit ?  INHERIT_PARENT_POSITION : DONT_INHERIT_POSITION;
518       SetDirtyFlag(TransformFlag);
519     }
520   }
521
522   /**
523    * Set the position inheritance mode.
524    * @see Dali::Actor::PositionInheritanceMode
525    * @param[in] mode The new position inheritance mode.
526    */
527   void SetPositionInheritanceMode( PositionInheritanceMode mode )
528   {
529     mPositionInheritanceMode = mode;
530
531     SetDirtyFlag(TransformFlag);
532   }
533
534   /**
535    * @return The position inheritance mode.
536    */
537   PositionInheritanceMode GetPositionInheritanceMode() const
538   {
539     return mPositionInheritanceMode;
540   }
541
542   /**
543    * Retrieve the local orientation of the node, relative to its parent.
544    * @param[in] bufferIndex The buffer to read from.
545    * @return The local orientation.
546    */
547   const Quaternion& GetOrientation(BufferIndex bufferIndex) const
548   {
549     return mOrientation[bufferIndex];
550   }
551
552   /**
553    * Sets both the local & base orientations of the node.
554    * @param[in] updateBufferIndex The current update buffer index.
555    * @param[in] orientation The new local & base orientation.
556    */
557   void BakeOrientation(BufferIndex updateBufferIndex, const Quaternion& orientation)
558   {
559     mOrientation.Bake( updateBufferIndex, orientation );
560   }
561
562   /**
563    * Sets the orientation of the node derived from the rotation of all its parents.
564    * @param[in] updateBufferIndex The current update buffer index.
565    * @param[in] orientation The world orientation.
566    */
567   void SetWorldOrientation( BufferIndex updateBufferIndex, const Quaternion& orientation )
568   {
569     mWorldOrientation.Set( updateBufferIndex, orientation );
570   }
571
572   /**
573    * Sets the orientation of the node derived from the rotation of all its parents.
574    * This method should only be called when the parents world orientation is up-to-date.
575    * @pre The node has a parent.
576    * @param[in] updateBufferIndex The current update buffer index.
577    */
578   void InheritWorldOrientation( BufferIndex updateBufferIndex )
579   {
580     DALI_ASSERT_DEBUG(mParent != NULL);
581
582     const Quaternion& localOrientation = mOrientation[updateBufferIndex];
583
584     if(localOrientation.IsIdentity())
585     {
586       mWorldOrientation.Set( updateBufferIndex, mParent->GetWorldOrientation(updateBufferIndex) );
587     }
588     else
589     {
590       Quaternion finalOrientation( mParent->GetWorldOrientation(updateBufferIndex) );
591       finalOrientation *= localOrientation;
592       mWorldOrientation.Set( updateBufferIndex, finalOrientation );
593     }
594   }
595
596   /**
597    * Copies the previous inherited orientation, if this changed in the previous frame.
598    * This method should be called instead of InheritWorldOrientation i.e. if the inherited orientation
599    * does not need to be recalculated in the current frame.
600    * @param[in] updateBufferIndex The current update buffer index.
601    */
602   void CopyPreviousWorldOrientation( BufferIndex updateBufferIndex )
603   {
604     mWorldOrientation.CopyPrevious( updateBufferIndex );
605   }
606
607   /**
608    * Retrieve the orientation of the node derived from the rotation of all its parents.
609    * @param[in] bufferIndex The buffer to read from.
610    * @return The world rotation.
611    */
612   const Quaternion& GetWorldOrientation( BufferIndex bufferIndex ) const
613   {
614     return mWorldOrientation[bufferIndex];
615   }
616
617   /**
618    * Set whether the Node inherits orientation.
619    * @param[in] inherit True if the parent orientation is inherited.
620    */
621   void SetInheritOrientation(bool inherit)
622   {
623     if (inherit != mInheritOrientation)
624     {
625       mInheritOrientation = inherit;
626
627       SetDirtyFlag(TransformFlag);
628     }
629   }
630
631   /**
632    * Query whether the node inherits orientation from its parent.
633    * @return True if the parent orientation is inherited.
634    */
635   bool IsOrientationInherited() const
636   {
637     return mInheritOrientation;
638   }
639
640   /**
641    * Retrieve the local scale of the node, relative to its parent.
642    * @param[in] bufferIndex The buffer to read from.
643    * @return The local scale.
644    */
645   const Vector3& GetScale(BufferIndex bufferIndex) const
646   {
647     return mScale[bufferIndex];
648   }
649
650   /**
651    * Sets the scale of the node derived from the scale of all its parents and a pre-scale
652    * @param[in] updateBufferIndex The current update buffer index.
653    * @param[in] scale The world scale.
654    */
655   void SetWorldScale(BufferIndex updateBufferIndex, const Vector3& scale)
656   {
657     mWorldScale.Set( updateBufferIndex, scale );
658   }
659
660   /**
661    * Sets the scale of the node derived from the scale of all its parents and a pre-scale.
662    * This method should only be called when the parents world scale is up-to-date.
663    * @pre The node has a parent.
664    * @param[in] updateBufferIndex The current update buffer index.
665    */
666   void InheritWorldScale(BufferIndex updateBufferIndex)
667   {
668     DALI_ASSERT_DEBUG(mParent != NULL);
669
670     mWorldScale.Set( updateBufferIndex, mParent->GetWorldScale(updateBufferIndex) * mScale[updateBufferIndex] );
671   }
672
673   /**
674    * Copies the previous inherited scale, if this changed in the previous frame.
675    * This method should be called instead of InheritWorldScale i.e. if the inherited scale
676    * does not need to be recalculated in the current frame.
677    * @param[in] updateBufferIndex The current update buffer index.
678    */
679   void CopyPreviousWorldScale( BufferIndex updateBufferIndex )
680   {
681     mWorldScale.CopyPrevious( updateBufferIndex );
682   }
683
684   /**
685    * Retrieve the scale of the node derived from the scale of all its parents.
686    * @param[in] bufferIndex The buffer to read from.
687    * @return The world scale.
688    */
689   const Vector3& GetWorldScale( BufferIndex bufferIndex ) const
690   {
691     return mWorldScale[bufferIndex];
692   }
693
694   /**
695    * Set whether the Node inherits scale.
696    * @param inherit True if the Node inherits scale.
697    */
698   void SetInheritScale( bool inherit )
699   {
700     if( inherit != mInheritScale )
701     {
702       mInheritScale = inherit;
703
704       SetDirtyFlag( TransformFlag );
705     }
706   }
707
708   /**
709    * Query whether the Node inherits scale.
710    * @return if scale is inherited
711    */
712   bool IsScaleInherited() const
713   {
714     return mInheritScale;
715   }
716
717   /**
718    * Retrieve the visibility of the node.
719    * @param[in] bufferIndex The buffer to read from.
720    * @return True if the node is visible.
721    */
722   bool IsVisible(BufferIndex bufferIndex) const
723   {
724     return mVisible[bufferIndex];
725   }
726
727   /**
728    * Retrieve the opacity of the node.
729    * @param[in] bufferIndex The buffer to read from.
730    * @return The opacity.
731    */
732   float GetOpacity(BufferIndex bufferIndex) const
733   {
734     return mColor[bufferIndex].a;
735   }
736
737   /**
738    * Retrieve the color of the node.
739    * @param[in] bufferIndex The buffer to read from.
740    * @return The color.
741    */
742   const Vector4& GetColor(BufferIndex bufferIndex) const
743   {
744     return mColor[bufferIndex];
745   }
746
747   /**
748    * Sets the color of the node derived from the color of all its parents.
749    * @param[in] color The world color.
750    * @param[in] updateBufferIndex The current update buffer index.
751    */
752   void SetWorldColor(const Vector4& color, BufferIndex updateBufferIndex)
753   {
754     mWorldColor.Set( updateBufferIndex, color );
755   }
756
757   /**
758    * Sets the color of the node derived from the color of all its parents.
759    * This method should only be called when the parents world color is up-to-date.
760    * @pre The node has a parent.
761    * @param[in] updateBufferIndex The current update buffer index.
762    */
763   void InheritWorldColor( BufferIndex updateBufferIndex )
764   {
765     DALI_ASSERT_DEBUG(mParent != NULL);
766
767     // default first
768     if( mColorMode == USE_OWN_MULTIPLY_PARENT_ALPHA )
769     {
770       const Vector4& ownColor = mColor[updateBufferIndex];
771       mWorldColor.Set( updateBufferIndex, ownColor.r, ownColor.g, ownColor.b, ownColor.a * mParent->GetWorldColor(updateBufferIndex).a );
772     }
773     else if( mColorMode == USE_OWN_MULTIPLY_PARENT_COLOR )
774     {
775       mWorldColor.Set( updateBufferIndex, mParent->GetWorldColor(updateBufferIndex) * mColor[updateBufferIndex] );
776     }
777     else if( mColorMode == USE_PARENT_COLOR )
778     {
779       mWorldColor.Set( updateBufferIndex, mParent->GetWorldColor(updateBufferIndex) );
780     }
781     else // USE_OWN_COLOR
782     {
783       mWorldColor.Set( updateBufferIndex, mColor[updateBufferIndex] );
784     }
785   }
786
787   /**
788    * Copies the previous inherited scale, if this changed in the previous frame.
789    * This method should be called instead of InheritWorldScale i.e. if the inherited scale
790    * does not need to be recalculated in the current frame.
791    * @param[in] updateBufferIndex The current update buffer index.
792    */
793   void CopyPreviousWorldColor( BufferIndex updateBufferIndex )
794   {
795     mWorldColor.CopyPrevious( updateBufferIndex );
796   }
797
798   /**
799    * Retrieve the color of the node, possibly derived from the color
800    * of all its parents, depending on the value of mColorMode.
801    * @param[in] bufferIndex The buffer to read from.
802    * @return The world color.
803    */
804   const Vector4& GetWorldColor(BufferIndex bufferIndex) const
805   {
806     return mWorldColor[bufferIndex];
807   }
808
809   /**
810    * Set the color mode. This specifies whether the Node uses its own color,
811    * or inherits its parent color.
812    * @param[in] colorMode The new color mode.
813    */
814   void SetColorMode(ColorMode colorMode)
815   {
816     mColorMode = colorMode;
817
818     SetDirtyFlag(ColorFlag);
819   }
820
821   /**
822    * Retrieve the color mode.
823    * @return The color mode.
824    */
825   ColorMode GetColorMode() const
826   {
827     return mColorMode;
828   }
829
830   /**
831    * Retrieve the size of the node.
832    * @param[in] bufferIndex The buffer to read from.
833    * @return The size.
834    */
835   const Vector3& GetSize(BufferIndex bufferIndex) const
836   {
837     return mSize[bufferIndex];
838   }
839
840   /**
841    * Set the world-matrix of a node, with scale + rotation + translation.
842    * Scale and rotation are centered at the origin.
843    * Translation is applied independently of the scale or rotatation axis.
844    * @param[in] updateBufferIndex The current update buffer index.
845    * @param[in] scale The scale.
846    * @param[in] rotation The rotation.
847    * @param[in] translation The translation.
848    */
849   void SetWorldMatrix( BufferIndex updateBufferIndex, const Vector3& scale, const Quaternion& rotation, const Vector3& translation )
850   {
851     mWorldMatrix.Get( updateBufferIndex ).SetTransformComponents( scale, rotation, translation );
852     mWorldMatrix.SetDirty( updateBufferIndex );
853   }
854
855   /**
856    * Retrieve the cached world-matrix of a node.
857    * @param[in] bufferIndex The buffer to read from.
858    * @return The world-matrix.
859    */
860   const Matrix& GetWorldMatrix( BufferIndex bufferIndex ) const
861   {
862     return mWorldMatrix[ bufferIndex ];
863   }
864
865   /**
866    * Copy previous frames world matrix
867    * @param[in] updateBufferIndex The current update buffer index.
868    */
869   void CopyPreviousWorldMatrix( BufferIndex updateBufferIndex )
870   {
871     mWorldMatrix.CopyPrevious( updateBufferIndex );
872   }
873
874   /**
875    * Mark the node as exclusive to a single RenderTask.
876    * @param[in] renderTask The render-task, or NULL if the Node is not exclusive to a single RenderTask.
877    */
878   void SetExclusiveRenderTask( RenderTask* renderTask )
879   {
880     mExclusiveRenderTask = renderTask;
881   }
882
883   /**
884    * Query whether the node is exclusive to a single RenderTask.
885    * @return The render-task, or NULL if the Node is not exclusive to a single RenderTask.
886    */
887   RenderTask* GetExclusiveRenderTask() const
888   {
889     return mExclusiveRenderTask;
890   }
891
892   /**
893    * Set how the Node and its children should be drawn; see Dali::Actor::SetDrawMode() for more details.
894    * @param[in] drawMode The new draw-mode to use.
895    */
896   void SetDrawMode( const DrawMode::Type& drawMode )
897   {
898     mDrawMode = drawMode;
899   }
900
901   /**
902    * Returns whether node is an overlay or not.
903    * @return True if node is an overlay, false otherwise.
904    */
905   DrawMode::Type GetDrawMode() const
906   {
907     return mDrawMode;
908   }
909
910   /**
911    * Equality operator, checks for identity, not values.
912    *
913    */
914   bool operator==( const Node* rhs ) const
915   {
916     if ( this == rhs )
917     {
918       return true;
919     }
920     return false;
921   }
922
923   unsigned short GetDepth() const
924   {
925     return mDepth;
926   }
927
928 public:
929   /**
930    * @copydoc UniformMap::Add
931    */
932   void AddUniformMapping( UniformPropertyMapping* map );
933
934   /**
935    * @copydoc UniformMap::Remove
936    */
937   void RemoveUniformMapping( const std::string& uniformName );
938
939   /**
940    * Prepare the node for rendering.
941    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
942    * @param[in] updateBufferIndex The current update buffer index.
943    */
944   void PrepareRender( BufferIndex bufferIndex );
945
946 protected:
947
948   /**
949    * Set the parent of a Node.
950    * @param[in] parentNode the new parent.
951    */
952   void SetParent(Node& parentNode);
953
954   /**
955    * Protected constructor; See also Node::New()
956    */
957   Node();
958
959 private: // from NodeDataProvider
960
961   /**
962    * @copydoc NodeDataProvider::GetModelMatrix
963    */
964   virtual const Matrix& GetModelMatrix( unsigned int bufferId ) const
965   {
966     return GetWorldMatrix( bufferId );
967   }
968
969   /**
970    * @copydoc NodeDataProvider::GetRenderColor
971    */
972   virtual const Vector4& GetRenderColor( unsigned int bufferId ) const
973   {
974     return GetWorldColor( bufferId );
975   }
976
977   virtual const Vector3& GetRenderSize( unsigned int bufferId ) const
978   {
979     return GetSize( bufferId );
980   }
981
982 public: // From UniformMapDataProvider
983   /**
984    * @copydoc UniformMapDataProvider::GetUniformMapChanged
985    */
986   virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const
987   {
988     return mUniformMapChanged[bufferIndex];
989   }
990
991   /**
992    * @copydoc UniformMapDataProvider::GetUniformMap
993    */
994   virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const
995   {
996     return mCollectedUniformMap[bufferIndex];
997   }
998
999 private:
1000
1001   // Undefined
1002   Node(const Node&);
1003
1004   // Undefined
1005   Node& operator=(const Node& rhs);
1006
1007   /**
1008    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
1009    */
1010   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
1011
1012   /**
1013    * Recursive helper to disconnect a Node and its children.
1014    * Disconnected Nodes have no parent or children.
1015    * @param[in] updateBufferIndex The current update buffer index.
1016    */
1017   void RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex );
1018
1019 public: // Default properties
1020
1021   PropertyVector3                mParentOrigin;  ///< Local transform; the position is relative to this. Sets the TransformFlag dirty when changed
1022   PropertyVector3                mAnchorPoint;   ///< Local transform; local center of rotation. Sets the TransformFlag dirty when changed
1023
1024   AnimatableProperty<Vector3>    mSize;          ///< Size is provided for layouting
1025   AnimatableProperty<Vector3>    mPosition;      ///< Local transform; distance between parent-origin & anchor-point
1026   AnimatableProperty<Quaternion> mOrientation;   ///< Local transform; rotation relative to parent node
1027   AnimatableProperty<Vector3>    mScale;         ///< Local transform; scale relative to parent node
1028   AnimatableProperty<bool>       mVisible;       ///< Visibility can be inherited from the Node hierachy
1029   AnimatableProperty<Vector4>    mColor;         ///< Color can be inherited from the Node hierarchy
1030
1031   // Inherited properties; read-only from public API
1032
1033   InheritedVector3    mWorldPosition;     ///< Full inherited position
1034   InheritedQuaternion mWorldOrientation;  ///< Full inherited orientation
1035   InheritedVector3    mWorldScale;        ///< Full inherited scale
1036   InheritedMatrix     mWorldMatrix;       ///< Full inherited world matrix
1037   InheritedColor      mWorldColor;        ///< Full inherited color
1038
1039 protected:
1040
1041   Node*               mParent;                       ///< Pointer to parent node (a child is owned by its parent)
1042   RenderTask*         mExclusiveRenderTask;          ///< Nodes can be marked as exclusive to a single RenderTask
1043
1044   NodeAttachmentOwner mAttachment;                   ///< Optional owned attachment
1045   RendererContainer   mRenderer;                     ///< Container of renderers; not owned
1046
1047   NodeContainer       mChildren;                     ///< Container of children; not owned
1048
1049   CollectedUniformMap mCollectedUniformMap[2];      ///< Uniform maps of the node
1050   unsigned int        mUniformMapChanged[2];        ///< Records if the uniform map has been altered this frame
1051   unsigned int        mRegenerateUniformMap : 2;    ///< Indicate if the uniform map has to be regenerated this frame
1052
1053   // flags, compressed to bitfield
1054   unsigned short mDepth: 12;                        ///< Depth in the hierarchy
1055   int  mDirtyFlags:8;                               ///< A composite set of flags for each of the Node properties
1056
1057   bool mIsRoot:1;                                    ///< True if the node cannot have a parent
1058   bool mInheritPosition:1;                           ///< Whether the parent's position should be inherited.
1059   bool mInheritOrientation:1;                        ///< Whether the parent's orientation should be inherited.
1060   bool mInheritScale:1;                              ///< Whether the parent's scale should be inherited.
1061
1062   DrawMode::Type          mDrawMode:2;               ///< How the Node and its children should be drawn
1063   PositionInheritanceMode mPositionInheritanceMode:2;///< Determines how position is inherited, 2 bits is enough.
1064   ColorMode               mColorMode:2;              ///< Determines whether mWorldColor is inherited, 2 bits is enough
1065
1066   // Changes scope, should be at end of class
1067   DALI_LOG_OBJECT_STRING_DECLARATION;
1068 };
1069
1070 // Messages for Node
1071
1072 inline void SetInheritOrientationMessage( EventThreadServices& eventThreadServices, const Node& node, bool inherit )
1073 {
1074   typedef MessageValue1< Node, bool > LocalType;
1075
1076   // Reserve some memory inside the message queue
1077   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1078
1079   // Construct message in the message queue memory; note that delete should not be called on the return value
1080   new (slot) LocalType( &node, &Node::SetInheritOrientation, inherit );
1081 }
1082
1083 inline void SetParentOriginMessage( EventThreadServices& eventThreadServices, const Node& node, const Vector3& origin )
1084 {
1085   typedef MessageValue1< Node, Vector3 > LocalType;
1086
1087   // Reserve some memory inside the message queue
1088   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1089
1090   // Construct message in the message queue memory; note that delete should not be called on the return value
1091   new (slot) LocalType( &node, &Node::SetParentOrigin, origin );
1092 }
1093
1094 inline void SetAnchorPointMessage( EventThreadServices& eventThreadServices, const Node& node, const Vector3& anchor )
1095 {
1096   typedef MessageValue1< Node, Vector3 > LocalType;
1097
1098   // Reserve some memory inside the message queue
1099   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1100
1101   // Construct message in the message queue memory; note that delete should not be called on the return value
1102   new (slot) LocalType( &node, &Node::SetAnchorPoint, anchor );
1103 }
1104
1105 inline void SetPositionInheritanceModeMessage( EventThreadServices& eventThreadServices, const Node& node, PositionInheritanceMode mode )
1106 {
1107   typedef MessageValue1< Node, PositionInheritanceMode > LocalType;
1108
1109   // Reserve some memory inside the message queue
1110   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1111
1112   // Construct message in the message queue memory; note that delete should not be called on the return value
1113   new (slot) LocalType( &node, &Node::SetPositionInheritanceMode, mode );
1114 }
1115
1116 inline void SetInheritPositionMessage( EventThreadServices& eventThreadServices, const Node& node, bool inherit )
1117 {
1118   typedef MessageValue1< Node, bool > LocalType;
1119
1120   // Reserve some memory inside the message queue
1121   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1122
1123   // Construct message in the message queue memory; note that delete should not be called on the return value
1124   new (slot) LocalType( &node, &Node::SetInheritPosition, inherit );
1125 }
1126
1127 inline void SetInheritScaleMessage( EventThreadServices& eventThreadServices, const Node& node, bool inherit )
1128 {
1129   typedef MessageValue1< Node, bool > LocalType;
1130
1131   // Reserve some memory inside the message queue
1132   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1133
1134   // Construct message in the message queue memory; note that delete should not be called on the return value
1135   new (slot) LocalType( &node, &Node::SetInheritScale, inherit );
1136 }
1137
1138 inline void SetColorModeMessage( EventThreadServices& eventThreadServices, const Node& node, ColorMode colorMode )
1139 {
1140   typedef MessageValue1< Node, ColorMode > LocalType;
1141
1142   // Reserve some memory inside the message queue
1143   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1144
1145   // Construct message in the message queue memory; note that delete should not be called on the return value
1146   new (slot) LocalType( &node, &Node::SetColorMode, colorMode );
1147 }
1148
1149 inline void SetDrawModeMessage( EventThreadServices& eventThreadServices, const Node& node, DrawMode::Type drawMode )
1150 {
1151   typedef MessageValue1< Node, DrawMode::Type > LocalType;
1152
1153   // Reserve some memory inside the message queue
1154   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1155
1156   // Construct message in the message queue memory; note that delete should not be called on the return value
1157   new (slot) LocalType( &node, &Node::SetDrawMode, drawMode );
1158 }
1159
1160 inline void AddRendererMessage( EventThreadServices& eventThreadServices, const Node& node, Renderer* renderer )
1161 {
1162   typedef MessageValue1< Node, Renderer* > LocalType;
1163
1164   // Reserve some memory inside the message queue
1165   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1166
1167   // Construct message in the message queue memory; note that delete should not be called on the return value
1168   new (slot) LocalType( &node, &Node::AddRenderer, renderer );
1169 }
1170
1171 inline void RemoveRendererMessage( EventThreadServices& eventThreadServices, const Node& node, Renderer* renderer )
1172 {
1173   typedef MessageValue1< Node, Renderer* > LocalType;
1174
1175   // Reserve some memory inside the message queue
1176   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1177
1178   // Construct message in the message queue memory; note that delete should not be called on the return value
1179   new (slot) LocalType( &node, &Node::RemoveRenderer, renderer );
1180 }
1181 } // namespace SceneGraph
1182
1183 } // namespace Internal
1184
1185 } // namespace Dali
1186
1187 #endif // __DALI_INTERNAL_SCENE_GRAPH_NODE_H_