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