Fix regression by previous optimization for mDirtyFlags
[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 Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21 #include <dali/public-api/actors/actor-enumerations.h>
22 #include <dali/public-api/actors/draw-mode.h>
23 #include <dali/public-api/math/quaternion.h>
24 #include <dali/public-api/math/math-utils.h>
25 #include <dali/public-api/math/vector3.h>
26 #include <dali/internal/common/message.h>
27 #include <dali/internal/common/event-to-update.h>
28 #include <dali/internal/update/common/animatable-property.h>
29 #include <dali/internal/update/common/double-buffered.h>
30 #include <dali/internal/update/common/property-owner.h>
31 #include <dali/internal/update/common/property-vector3.h>
32 #include <dali/internal/update/common/scene-graph-buffers.h>
33 #include <dali/internal/update/common/inherited-property.h>
34 #include <dali/integration-api/debug.h>
35 #include <dali/internal/update/nodes/node-declarations.h>
36 #include <dali/internal/update/node-attachments/node-attachment-declarations.h>
37 #include <dali/internal/render/renderers/render-data-provider.h>
38
39 namespace Dali
40 {
41
42 namespace Internal
43 {
44
45 // value types used by messages
46 template <> struct ParameterType< ColorMode > : public BasicType< ColorMode > {};
47 template <> struct ParameterType< PositionInheritanceMode > : public BasicType< PositionInheritanceMode > {};
48
49 namespace SceneGraph
50 {
51
52 class DiscardQueue;
53 class Layer;
54 class Shader;
55 class NodeAttachment;
56 class RenderTask;
57 class UpdateManager;
58
59 /**
60  * Flag whether property has changed, during the Update phase.
61  */
62 enum NodePropertyFlags
63 {
64   NothingFlag          = 0x000,
65   TransformFlag        = 0x001,
66   VisibleFlag          = 0x002,
67   ColorFlag            = 0x004,
68   SizeFlag             = 0x008,
69   ShaderFlag           = 0x010,
70   OverlayFlag          = 0x020,
71   SortModifierFlag     = 0x040,
72   ChildDeletedFlag     = 0x080
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 | ShaderFlag | 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  * Each node provides a transformation which applies to the node and its children.
89  * Node data is double-buffered. This allows an update thread to modify node data, without interferring
90  * with another thread reading the values from the previous update traversal.
91  */
92 class Node : public PropertyOwner, public RenderDataProvider
93 {
94 public:
95
96   // Defaults
97   static const PositionInheritanceMode DEFAULT_POSITION_INHERITANCE_MODE;
98   static const ColorMode DEFAULT_COLOR_MODE;
99
100   // Creation methods
101
102   /**
103    * Construct a new Node.
104    */
105   static Node* New();
106
107   /**
108    * Virtual destructor
109    */
110   virtual ~Node();
111
112   /**
113    * When a Node is marked "active" it has been disconnected, but its properties have been modified.
114    * @note An inactive Node will be skipped during the UpdateManager ResetProperties stage.
115    * @param[in] isActive True if the Node is active.
116    */
117   void SetActive( bool isActive )
118   {
119     mIsActive = isActive;
120   }
121
122   /**
123    * Query whether the Node is active.
124    * @return True if the Node is active.
125    */
126   bool IsActive() const
127   {
128     return mIsActive;
129   }
130
131   /**
132    * Called during UpdateManager::DestroyNode shortly before Node is destroyed.
133    */
134   void OnDestroy();
135
136   // Layer interface
137
138   /**
139    * Query whether the node is a layer.
140    * @return True if the node is a layer.
141    */
142   bool IsLayer()
143   {
144     return (GetLayer() != NULL);
145   }
146
147   /**
148    * Convert a node to a layer.
149    * @return A pointer to the layer, or NULL.
150    */
151   virtual Layer* GetLayer()
152   {
153     return NULL;
154   }
155
156   // Attachments
157
158   /**
159    * Attach an object to this Node; This should only be done by UpdateManager::AttachToNode.
160    * @pre The Node does not already have an attachment.
161    * @param[in] attachment The object to attach.
162    */
163   void Attach( NodeAttachment& attachment );
164
165   /**
166    * Query the node if it has an attachment.
167    * @return True if it has an attachment.
168    */
169   bool HasAttachment() const
170   {
171     return mAttachment;
172   }
173
174   /**
175    * Retreive the object attached to this node.
176    * @return The attachment.
177    */
178   NodeAttachment& GetAttachment() const
179   {
180     return *mAttachment;
181   }
182
183   // Containment methods
184
185   /**
186    * Query whether a node is the root node. Root nodes cannot have a parent node.
187    * A node becomes a root node, when it is installed by UpdateManager.
188    * @return True if the node is a root node.
189    */
190   bool IsRoot() const
191   {
192     return mIsRoot;
193   }
194
195   /**
196    * Set whether a node is the root node. Root nodes cannot have a parent node.
197    * This method should only be called by UpdateManager.
198    * @pre When isRoot is true, the node must not have a parent.
199    * @param[in] isRoot Whether the node is now a root node.
200    */
201   void SetRoot(bool isRoot);
202
203   /**
204    * Retrieve the parent of a Node.
205    * @return The parent node, or NULL if the Node has not been added to the scene-graph.
206    */
207   Node* GetParent()
208   {
209     return mParent;
210   }
211
212   /**
213    * Retrieve the parent of a Node.
214    * @return The parent node, or NULL if the Node has not been added to the scene-graph.
215    */
216   const Node* GetParent() const
217   {
218     return mParent;
219   }
220
221   /**
222    * Connect a node to the scene-graph.
223    * @pre A node cannot be added to itself.
224    * @pre The parent node is connected to the scene-graph.
225    * @pre The childNode does not already have a parent.
226    * @pre The childNode is not a root node.
227    * @param[in] childNode The child to add.
228    */
229   void ConnectChild( Node* childNode );
230
231   /**
232    * Disconnect a child (& its children) from the scene-graph.
233    * @pre childNode is a child of this Node.
234    * @param[in] updateBufferIndex The current update buffer index.
235    * @param[in] childNode The node to disconnect.
236    * @param[in] connectedNodes Disconnected Node attachments should be removed from here.
237    * @param[in] disconnectedNodes Disconnected Node attachments should be added here.
238    */
239   void DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes,  std::set<Node*>& disconnectedNodes );
240
241   /**
242    * Retrieve the children a Node.
243    * @return The container of children.
244    */
245   const NodeContainer& GetChildren() const
246   {
247     return mChildren;
248   }
249
250   /**
251    * Retrieve the children a Node.
252    * @return The container of children.
253    */
254   NodeContainer& GetChildren()
255   {
256     return mChildren;
257   }
258
259   // Shaders
260
261   /**
262    * Set whether the node inherits a shader effect from its parent.
263    * The inherited effect can be overriden using ApplyShader()
264    * @param [in] inherit True if the parent effect is inherited.
265    */
266   void SetInheritShader(bool inherit)
267   {
268     if (inherit != mInheritShader)
269     {
270       mInheritShader = inherit;
271
272       SetDirtyFlag(ShaderFlag);
273     }
274   }
275
276   /**
277    * Query whether the node inherits a shader from its parent.
278    * @return True if the parent effect is inherited.
279    */
280   bool GetInheritShader() const
281   {
282     return mInheritShader;
283   }
284
285   /**
286    * Apply a shader object to this Node.
287    * Shader effects are weakly referenced, potentially by multiple nodes & node attachments.
288    * @param[in] shader The shader to apply.
289    */
290   void ApplyShader( Shader* shader );
291
292   /**
293    * Remove the shader object from this Node (if any).
294    */
295   void RemoveShader();
296
297   /**
298    * Retrieve the applied shader.
299    * @return The applied shader.
300    */
301   Shader* GetAppliedShader() const;
302
303   /**
304    * Sets the inherited shader of the node.
305    * @param[in] shader The new inherited shader.
306    */
307   void SetInheritedShader(Shader* shader);
308
309   /**
310    * Retrieve the inherited shader.
311    * @return The inherited shader.
312    */
313   Shader* GetInheritedShader() const;
314
315   /**
316    * Inherit a shader (if any) applied to the parent node.
317    * This method should only be called when the parents inherited shader is up-to-date.
318    * @param defaultShader pointer to the default shader, used if inherit shader is set to false
319    * @pre The node has a parent.
320    */
321   void InheritShader(Shader* defaultShader);
322
323   // Update methods
324
325   /**
326    * Flag that one of the node values has changed in the current frame.
327    * @param[in] flag The flag to set.
328    */
329   void SetDirtyFlag(NodePropertyFlags flag)
330   {
331     mDirtyFlags |= flag;
332   }
333
334   /**
335    * Flag that all of the node values are dirty.
336    */
337   void SetAllDirtyFlags()
338   {
339     mDirtyFlags = AllFlags;
340   }
341
342   /**
343    * Query whether a node is dirty.
344    * @return The dirty flags
345    */
346   int GetDirtyFlags() const;
347
348   /**
349    * Query whether a node is clean.
350    * @return True if the node is clean.
351    */
352   bool IsClean() const
353   {
354     return ( NothingFlag == GetDirtyFlags() );
355   }
356
357   /**
358    * Retrieve the parent-origin of the node.
359    * @return The parent-origin.
360    */
361   const Vector3& GetParentOrigin() const
362   {
363     return mParentOrigin.mValue;
364   }
365
366   /**
367    * Sets both the local & base parent-origins of the node.
368    * @param[in] origin The new local & base parent-origins.
369    */
370   void SetParentOrigin(const Vector3& origin)
371   {
372     mParentOrigin.mValue = origin;
373     mParentOrigin.OnSet();
374   }
375
376   /**
377    * Retrieve the anchor-point of the node.
378    * @return The anchor-point.
379    */
380   const Vector3& GetAnchorPoint() const
381   {
382     return mAnchorPoint.mValue;
383   }
384
385   /**
386    * Sets both the local & base anchor-points of the node.
387    * @param[in] anchor The new local & base anchor-points.
388    */
389   void SetAnchorPoint(const Vector3& anchor)
390   {
391     mAnchorPoint.mValue = anchor;
392     mAnchorPoint.OnSet();
393   }
394
395   /**
396    * Retrieve the local position of the node, relative to its parent.
397    * @param[in] bufferIndex The buffer to read from.
398    * @return The local position.
399    */
400   const Vector3& GetPosition(BufferIndex bufferIndex) const
401   {
402     return mPosition[bufferIndex];
403   }
404
405   /**
406    * Sets both the local & base positions of the node.
407    * @param[in] updateBufferIndex The current update buffer index.
408    * @param[in] position The new local & base position.
409    */
410   void BakePosition(BufferIndex updateBufferIndex, const Vector3& position)
411   {
412     mPosition.Bake( updateBufferIndex, position );
413   }
414
415   /**
416    * Sets the world of the node derived from the position of all its parents.
417    * @param[in] updateBufferIndex The current update buffer index.
418    * @param[in] position The world position.
419    */
420   void SetWorldPosition( BufferIndex updateBufferIndex, const Vector3& position )
421   {
422     mWorldPosition.Set( updateBufferIndex, position );
423   }
424
425   /**
426    * Sets the position of the node derived from the position of all its parents.
427    * This method should only be called when the parent's world position is up-to-date.
428    * With a non-central anchor-point, the local rotation and scale affects the world position.
429    * Therefore the world rotation & scale must be updated before the world position.
430    * @pre The node has a parent.
431    * @param[in] updateBufferIndex The current update buffer index.
432    */
433   void InheritWorldPosition(BufferIndex updateBufferIndex)
434   {
435     DALI_ASSERT_DEBUG(mParent != NULL);
436
437     switch( mPositionInheritanceMode )
438     {
439       case INHERIT_PARENT_POSITION  : ///@see Dali::PositionInheritanceMode for how these modes are expected to work
440       {
441         Vector3 finalPosition(-0.5f, -0.5f, -0.5f);
442
443         finalPosition += mParentOrigin.mValue;
444         finalPosition *= mParent->GetSize(updateBufferIndex);
445         finalPosition += mPosition[updateBufferIndex];
446         finalPosition *= mParent->GetWorldScale(updateBufferIndex);
447         const Quaternion& parentWorldRotation = mParent->GetWorldRotation(updateBufferIndex);
448         if(!parentWorldRotation.IsIdentity())
449         {
450           finalPosition *= parentWorldRotation;
451         }
452
453         // check if a node needs to be offsetted locally (only applies when AnchorPoint is not central)
454         // dont use operator== as that does a slower comparison (and involves function calls)
455         Vector3 localOffset(0.5f, 0.5f, 0.5f);    // AnchorPoint::CENTER
456         localOffset -= mAnchorPoint.mValue;
457
458         if( ( fabsf( localOffset.x ) >= Math::MACHINE_EPSILON_0 ) ||
459             ( fabsf( localOffset.y ) >= Math::MACHINE_EPSILON_0 ) ||
460             ( fabsf( localOffset.z ) >= Math::MACHINE_EPSILON_0 ) )
461         {
462           localOffset *= mSize[updateBufferIndex];
463
464           Vector3 scale = mWorldScale[updateBufferIndex];
465           if(GetTransmitGeometryScaling())
466           {
467             // Remove geometry scaling to get back to actor scale
468             scale /= mGeometryScale;
469           }
470           // Also pick up sign of local scale
471           if (mScale[updateBufferIndex].x < 0.0f)
472           {
473             scale.x = -scale.x;
474           }
475           if (mScale[updateBufferIndex].y < 0.0f)
476           {
477             scale.y = -scale.y;
478           }
479           if (mScale[updateBufferIndex].z < 0.0f)
480           {
481             scale.z = -scale.z;
482           }
483
484           // If the anchor-point is not central, then position is affected by the local rotation & scale
485           localOffset *= scale;
486           const Quaternion& localWorldRotation = mWorldRotation[updateBufferIndex];
487           if(!localWorldRotation.IsIdentity())
488           {
489             localOffset *= localWorldRotation;
490           }
491           finalPosition += localOffset;
492         }
493
494         finalPosition += mParent->GetWorldPosition(updateBufferIndex);
495         mWorldPosition.Set( updateBufferIndex, finalPosition );
496         break;
497       }
498       case USE_PARENT_POSITION_PLUS_LOCAL_POSITION :
499       {
500         // copy parents position plus local transform
501         mWorldPosition.Set( updateBufferIndex, mParent->GetWorldPosition(updateBufferIndex) + mPosition[updateBufferIndex] );
502         break;
503       }
504       case USE_PARENT_POSITION :
505       {
506         // copy parents position
507         mWorldPosition.Set( updateBufferIndex, mParent->GetWorldPosition(updateBufferIndex) );
508         break;
509       }
510       case DONT_INHERIT_POSITION :
511       {
512         // use local position as world position
513         mWorldPosition.Set( updateBufferIndex, mPosition[updateBufferIndex] );
514         break;
515       }
516     }
517   }
518
519   /**
520    * Copies the previous inherited position, if this changed in the previous frame.
521    * This method should be called instead of InheritWorldPosition i.e. if the inherited position
522    * does not need to be recalculated in the current frame.
523    * @param[in] updateBufferIndex The current update buffer index.
524    */
525   void CopyPreviousWorldPosition( BufferIndex updateBufferIndex )
526   {
527     mWorldPosition.CopyPrevious( updateBufferIndex );
528   }
529
530   /**
531    * Retrieve the position of the node derived from the position of all its parents.
532    * @return The world position.
533    */
534   const Vector3& GetWorldPosition( BufferIndex bufferIndex ) const
535   {
536     return mWorldPosition[bufferIndex];
537   }
538
539   /**
540    * Set the position inheritance mode.
541    * @see Dali::Actor::PositionInheritanceMode
542    * @param[in] mode The new position inheritance mode.
543    */
544   void SetPositionInheritanceMode( PositionInheritanceMode mode )
545   {
546     mPositionInheritanceMode = mode;
547
548     SetDirtyFlag(TransformFlag);
549   }
550
551   /**
552    * @return The position inheritance mode.
553    */
554   PositionInheritanceMode GetPositionInheritanceMode() const
555   {
556     return mPositionInheritanceMode;
557   }
558
559   /**
560    * Retrieve the local rotation of the node, relative to its parent.
561    * @param[in] bufferIndex The buffer to read from.
562    * @return The local rotation.
563    */
564   const Quaternion& GetRotation(BufferIndex bufferIndex) const
565   {
566     return mRotation[bufferIndex];
567   }
568
569   /**
570    * Sets both the local & base rotations of the node.
571    * @param[in] updateBufferIndex The current update buffer index.
572    * @param[in] rotation The new local & base rotation.
573    */
574   void BakeRotation(BufferIndex updateBufferIndex, const Quaternion& rotation)
575   {
576     mRotation.Bake( updateBufferIndex, rotation );
577   }
578
579   /**
580    * Sets the rotation of the node derived from the rotation of all its parents.
581    * @param[in] updateBufferIndex The current update buffer index.
582    * @param[in] rotation The world rotation.
583    */
584   void SetWorldRotation( BufferIndex updateBufferIndex, const Quaternion& rotation )
585   {
586     mWorldRotation.Set( updateBufferIndex, rotation );
587   }
588
589   /**
590    * Sets the rotation of the node derived from the rotation of all its parents.
591    * This method should only be called when the parents world rotation is up-to-date.
592    * @pre The node has a parent.
593    * @param[in] updateBufferIndex The current update buffer index.
594    */
595   void InheritWorldRotation( BufferIndex updateBufferIndex )
596   {
597     DALI_ASSERT_DEBUG(mParent != NULL);
598
599     const Quaternion& localRotation = mRotation[updateBufferIndex];
600
601     if(localRotation.IsIdentity())
602     {
603       mWorldRotation.Set( updateBufferIndex, mParent->GetWorldRotation(updateBufferIndex) );
604     }
605     else
606     {
607       Quaternion finalRotation( mParent->GetWorldRotation(updateBufferIndex) );
608       finalRotation *= localRotation;
609       mWorldRotation.Set( updateBufferIndex, finalRotation );
610     }
611   }
612
613   /**
614    * Copies the previous inherited rotation, if this changed in the previous frame.
615    * This method should be called instead of InheritWorldRotation i.e. if the inherited rotation
616    * does not need to be recalculated in the current frame.
617    * @param[in] updateBufferIndex The current update buffer index.
618    */
619   void CopyPreviousWorldRotation( BufferIndex updateBufferIndex )
620   {
621     mWorldRotation.CopyPrevious( updateBufferIndex );
622   }
623
624   /**
625    * Retrieve the rotation of the node derived from the rotation of all its parents.
626    * @param[in] bufferIndex The buffer to read from.
627    * @return The world rotation.
628    */
629   const Quaternion& GetWorldRotation( BufferIndex bufferIndex ) const
630   {
631     return mWorldRotation[bufferIndex];
632   }
633
634   /**
635    * Set whether the Node inherits rotation.
636    * @param[in] inherit True if the parent rotation is inherited.
637    */
638   void SetInheritRotation(bool inherit)
639   {
640     if (inherit != mInheritRotation)
641     {
642       mInheritRotation = inherit;
643
644       SetDirtyFlag(TransformFlag);
645     }
646   }
647
648   /**
649    * Query whether the node inherits rotation from its parent.
650    * @return True if the parent rotation is inherited.
651    */
652   bool IsRotationInherited() const
653   {
654     return mInheritRotation;
655   }
656
657   /**
658    * Set the initial volume of the node. Used for calculating geometry scaling
659    * as the node size is changed  when transmitGeometryScaling is set to true.
660    *
661    * This property is not animatable.
662    *
663    * @param[in] volume The initial volume of this nodes meshes & children
664    */
665   void SetInitialVolume( const Vector3& volume)
666   {
667     mInitialVolume = volume;
668     SetDirtyFlag(SizeFlag);
669   }
670
671   /**
672    * Get the initial volume.  Used for calculating geometry scaling
673    * when TransmitGeometryScaling is true (i.e., the scaling is baked
674    * into the node tranform)
675    *
676    * @return The initial volume of this node and children.
677    */
678   Vector3 GetInitialVolume()
679   {
680     return mInitialVolume;
681   }
682
683   /**
684    * Sets whether the geometry scaling should be applied to the node
685    * (In which case, set the initial scale using SetInitialVolume()).
686    *
687    * If it is applied to the node, then the attachments are not scaled,
688    * as the scaling is then already baked into the node transform.
689    *
690    * @param[in] transmitGeometryScaling true if scaling is to be applied
691    * to the node.
692    */
693   void SetTransmitGeometryScaling(bool transmitGeometryScaling)
694   {
695     mTransmitGeometryScaling = transmitGeometryScaling;
696     SetDirtyFlag(SizeFlag);
697   }
698
699   /**
700    * Find out whether the node allows geometry scaling to be transmitted to its children.
701    * @return true if transmitted.
702    */
703   bool GetTransmitGeometryScaling() const
704   {
705     return mTransmitGeometryScaling;
706   }
707
708   /**
709    * Retrieve the local scale of the node, relative to its parent.
710    * @param[in] bufferIndex The buffer to read from.
711    * @return The local scale.
712    */
713   const Vector3& GetScale(BufferIndex bufferIndex) const
714   {
715     return mScale[bufferIndex];
716   }
717
718   /**
719    * Sets the scale of the node derived from the scale of all its parents and a pre-scale
720    * @param[in] updateBufferIndex The current update buffer index.
721    * @param[in] scale The world scale.
722    */
723   void SetWorldScale(BufferIndex updateBufferIndex, const Vector3& scale)
724   {
725     mWorldScale.Set( updateBufferIndex, mGeometryScale * scale );
726   }
727
728   /**
729    * Sets the scale of the node derived from the scale of all its parents and a pre-scale.
730    * This method should only be called when the parents world scale is up-to-date.
731    * @pre The node has a parent.
732    * @param[in] updateBufferIndex The current update buffer index.
733    */
734   void InheritWorldScale(BufferIndex updateBufferIndex)
735   {
736     DALI_ASSERT_DEBUG(mParent != NULL);
737
738     mWorldScale.Set( updateBufferIndex, mParent->GetWorldScale(updateBufferIndex) * mGeometryScale * mScale[updateBufferIndex] );
739   }
740
741   /**
742    * Copies the previous inherited scale, if this changed in the previous frame.
743    * This method should be called instead of InheritWorldScale i.e. if the inherited scale
744    * does not need to be recalculated in the current frame.
745    * @param[in] updateBufferIndex The current update buffer index.
746    */
747   void CopyPreviousWorldScale( BufferIndex updateBufferIndex )
748   {
749     mWorldScale.CopyPrevious( updateBufferIndex );
750   }
751
752   /**
753    * Retrieve the scale of the node derived from the scale of all its parents.
754    * @param[in] bufferIndex The buffer to read from.
755    * @return The world scale.
756    */
757   const Vector3& GetWorldScale( BufferIndex bufferIndex ) const
758   {
759     return mWorldScale[bufferIndex];
760   }
761
762   /**
763    * Set whether the Node inherits scale.
764    * @param inherit True if the Node inherits scale.
765    */
766   void SetInheritScale( bool inherit )
767   {
768     if( inherit != mInheritScale )
769     {
770       mInheritScale = inherit;
771
772       SetDirtyFlag( TransformFlag );
773     }
774   }
775
776   /**
777    * Query whether the Node inherits scale.
778    * @return if scale is inherited
779    */
780   bool IsScaleInherited() const
781   {
782     return mInheritScale;
783   }
784
785   /**
786    * Sets a geometry scale, calculated when TransmitGeometryScaling is true.
787    * Must only be used from render thread.
788    * @param[in] geometryScale The geometry scale value
789    */
790   void SetGeometryScale(Vector3 geometryScale)
791   {
792     mGeometryScale = geometryScale;
793
794     SetDirtyFlag( TransformFlag );
795   }
796
797   /**
798    * Retrieve the geometry scale, calculated when TransmitGeometryScaling is true.
799    * @return The geometry scale value.
800    */
801   const Vector3& GetGeometryScale() const
802   {
803     return mGeometryScale;
804   }
805
806   /**
807    * Retrieve the visibility of the node.
808    * @param[in] bufferIndex The buffer to read from.
809    * @return True if the node is visible.
810    */
811   bool IsVisible(BufferIndex bufferIndex) const
812   {
813     return mVisible[bufferIndex];
814   }
815
816   /**
817    * Retrieves whether a node is fully visible.
818    * A node is fully visible if is visible and all its ancestors are visible.
819    * @param[in] updateBufferIndex The current update buffer index.
820    * @return True if the node is fully visible.
821    */
822   bool IsFullyVisible( BufferIndex updateBufferIndex ) const;
823
824   /**
825    * Retrieve the opacity of the node.
826    * @param[in] bufferIndex The buffer to read from.
827    * @return The opacity.
828    */
829   float GetOpacity(BufferIndex bufferIndex) const
830   {
831     return mColor[bufferIndex].a;
832   }
833
834   /**
835    * Retrieve the color of the node.
836    * @param[in] bufferIndex The buffer to read from.
837    * @return The color.
838    */
839   const Vector4& GetColor(BufferIndex bufferIndex) const
840   {
841     return mColor[bufferIndex];
842   }
843
844   /**
845    * Sets the color of the node derived from the color of all its parents.
846    * @param[in] color The world color.
847    * @param[in] updateBufferIndex The current update buffer index.
848    */
849   void SetWorldColor(const Vector4& color, BufferIndex updateBufferIndex)
850   {
851     mWorldColor.Set( updateBufferIndex, color );
852   }
853
854   /**
855    * Sets the color of the node derived from the color of all its parents.
856    * This method should only be called when the parents world color is up-to-date.
857    * @pre The node has a parent.
858    * @param[in] updateBufferIndex The current update buffer index.
859    */
860   void InheritWorldColor( BufferIndex updateBufferIndex )
861   {
862     DALI_ASSERT_DEBUG(mParent != NULL);
863
864     // default first
865     if( mColorMode == USE_OWN_MULTIPLY_PARENT_ALPHA )
866     {
867       const Vector4& ownColor = mColor[updateBufferIndex];
868       mWorldColor.Set( updateBufferIndex, ownColor.r, ownColor.g, ownColor.b, ownColor.a * mParent->GetWorldColor(updateBufferIndex).a );
869     }
870     else if( mColorMode == USE_OWN_MULTIPLY_PARENT_COLOR )
871     {
872       mWorldColor.Set( updateBufferIndex, mParent->GetWorldColor(updateBufferIndex) * mColor[updateBufferIndex] );
873     }
874     else if( mColorMode == USE_PARENT_COLOR )
875     {
876       mWorldColor.Set( updateBufferIndex, mParent->GetWorldColor(updateBufferIndex) );
877     }
878     else // USE_OWN_COLOR
879     {
880       mWorldColor.Set( updateBufferIndex, mColor[updateBufferIndex] );
881     }
882   }
883
884   /**
885    * Copies the previous inherited scale, if this changed in the previous frame.
886    * This method should be called instead of InheritWorldScale i.e. if the inherited scale
887    * does not need to be recalculated in the current frame.
888    * @param[in] updateBufferIndex The current update buffer index.
889    */
890   void CopyPreviousWorldColor( BufferIndex updateBufferIndex )
891   {
892     mWorldColor.CopyPrevious( updateBufferIndex );
893   }
894
895   /**
896    * Retrieve the color of the node, possibly derived from the color
897    * of all its parents, depending on the value of mColorMode.
898    * @param[in] bufferIndex The buffer to read from.
899    * @return The world color.
900    */
901   const Vector4& GetWorldColor(BufferIndex bufferIndex) const
902   {
903     return mWorldColor[bufferIndex];
904   }
905
906   /**
907    * Set the color mode. This specifies whether the Node uses its own color,
908    * or inherits its parent color.
909    * @param[in] colorMode The new color mode.
910    */
911   void SetColorMode(ColorMode colorMode)
912   {
913     mColorMode = colorMode;
914
915     SetDirtyFlag(ColorFlag);
916   }
917
918   /**
919    * Retrieve the color mode.
920    * @return The color mode.
921    */
922   ColorMode GetColorMode() const
923   {
924     return mColorMode;
925   }
926
927   /**
928    * Retrieve the size of the node.
929    * @param[in] bufferIndex The buffer to read from.
930    * @return The size.
931    */
932   const Vector3& GetSize(BufferIndex bufferIndex) const
933   {
934     return mSize[bufferIndex];
935   }
936
937   /**
938    * Set the world-matrix of a node, with scale + rotation + translation.
939    * Scale and rotation are centered at the origin.
940    * Translation is applied independently of the scale or rotatation axis.
941    * @param[in] updateBufferIndex The current update buffer index.
942    * @param[in] scale The scale.
943    * @param[in] rotation The rotation.
944    * @param[in] translation The translation.
945    */
946   void SetWorldMatrix( BufferIndex updateBufferIndex, const Vector3& scale, const Quaternion& rotation, const Vector3& translation )
947   {
948     mWorldMatrix.Get( updateBufferIndex ).SetTransformComponents( scale, rotation, translation );
949     mWorldMatrix.SetDirty( updateBufferIndex );
950   }
951
952   /**
953    * Retrieve the cached world-matrix of a node.
954    * @param[in] bufferIndex The buffer to read from.
955    * @return The world-matrix.
956    */
957   const Matrix& GetWorldMatrix( BufferIndex bufferIndex ) const
958   {
959     return mWorldMatrix[ bufferIndex ];
960   }
961
962   /**
963    * Copy previous frames world matrix
964    * @param[in] updateBufferIndex The current update buffer index.
965    */
966   void CopyPreviousWorldMatrix( BufferIndex updateBufferIndex )
967   {
968     mWorldMatrix.CopyPrevious( updateBufferIndex );
969   }
970
971   /**
972    * Mark the node as exclusive to a single RenderTask.
973    * @param[in] renderTask The render-task, or NULL if the Node is not exclusive to a single RenderTask.
974    */
975   void SetExclusiveRenderTask( RenderTask* renderTask )
976   {
977     mExclusiveRenderTask = renderTask;
978   }
979
980   /**
981    * Query whether the node is exclusive to a single RenderTask.
982    * @return The render-task, or NULL if the Node is not exclusive to a single RenderTask.
983    */
984   RenderTask* GetExclusiveRenderTask() const
985   {
986     return mExclusiveRenderTask;
987   }
988
989   /**
990    * Set how the Node and its children should be drawn; see Dali::Actor::SetDrawMode() for more details.
991    * @param[in] drawMode The new draw-mode to use.
992    */
993   void SetDrawMode( const DrawMode::Type& drawMode )
994   {
995     mDrawMode = drawMode;
996   }
997
998   /**
999    * Returns whether node is an overlay or not.
1000    * @return True if node is an overlay, false otherwise.
1001    */
1002   DrawMode::Type GetDrawMode() const
1003   {
1004     return mDrawMode;
1005   }
1006
1007   /**
1008    * Equality operator, checks for identity, not values.
1009    *
1010    */
1011   bool operator==( const Node* rhs ) const
1012   {
1013     if ( this == rhs )
1014     {
1015       return true;
1016     }
1017     return false;
1018   }
1019
1020   /**
1021    * Set the inhibit local transform flag.@n
1022    * Setting this flag will stop the node's local transform (position, scale and orientation)
1023    * being applied on top of its parents transformation.
1024    * @param[in] flag When true, local transformation is inhibited when calculating the world matrix.
1025    */
1026   void SetInhibitLocalTransform( bool flag )
1027   {
1028     SetDirtyFlag( TransformFlag );
1029     mInhibitLocalTransform = flag;
1030   }
1031
1032   /**
1033    * Get the inhibit local transform flag.@n
1034    * See @ref SetInhibitLocalTransform
1035    * @result A flag, when true, local transformation is inhibited when calculating the world matrix.
1036    */
1037   bool GetInhibitLocalTransform() const
1038   {
1039     return mInhibitLocalTransform;
1040   }
1041
1042 protected:
1043
1044   /**
1045    * Set the parent of a Node.
1046    * @param[in] parentNode the new parent.
1047    */
1048   void SetParent(Node& parentNode);
1049
1050   /**
1051    * Protected constructor; See also Node::New()
1052    */
1053   Node();
1054
1055 private: // from RenderDataProvider
1056
1057   /**
1058    * @copydoc RenderDataProvider::GetModelMatrix
1059    */
1060   virtual const Matrix& GetModelMatrix( unsigned int bufferId )
1061   {
1062     return GetWorldMatrix( bufferId );
1063   }
1064
1065   /**
1066    * @copydoc RenderDataProvider::GetRenderColor
1067    */
1068   virtual const Vector4& GetRenderColor( unsigned int bufferId )
1069   {
1070     return GetWorldColor( bufferId );
1071   }
1072
1073 private:
1074
1075   // Undefined
1076   Node(const Node&);
1077
1078   // Undefined
1079   Node& operator=(const Node& rhs);
1080
1081   /**
1082    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
1083    */
1084   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
1085
1086   /**
1087    * Recursive helper to disconnect a Node and its children.
1088    * Disconnected Nodes have no parent or children.
1089    * @param[in] updateBufferIndex The current update buffer index.
1090    * @param[in] connectedNodes Disconnected Node attachments should be removed from here.
1091    * @param[in] disconnectedNodes Disconnected Node attachments should be added here.
1092    */
1093   void RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes );
1094
1095 public: // Default properties
1096
1097   PropertyVector3                mParentOrigin;  ///< Local transform; the position is relative to this. Sets the TransformFlag dirty when changed
1098   PropertyVector3                mAnchorPoint;   ///< Local transform; local center of rotation. Sets the TransformFlag dirty when changed
1099
1100   AnimatableProperty<Vector3>    mSize;          ///< Size is provided for layouting
1101   AnimatableProperty<Vector3>    mPosition;      ///< Local transform; distance between parent-origin & anchor-point
1102   AnimatableProperty<Quaternion> mRotation;      ///< Local transform; rotation relative to parent node
1103   AnimatableProperty<Vector3>    mScale;         ///< Local transform; scale relative to parent node
1104   AnimatableProperty<bool>       mVisible;       ///< Visibility can be inherited from the Node hierachy
1105   AnimatableProperty<Vector4>    mColor;         ///< Color can be inherited from the Node hierarchy
1106
1107   // Inherited properties; read-only from public API
1108
1109   InheritedProperty<Vector3>    mWorldPosition; ///< Full inherited position
1110   InheritedProperty<Quaternion> mWorldRotation; ///< Full inherited rotation
1111   InheritedProperty<Vector3>    mWorldScale;    ///< Full inherited scale
1112   InheritedProperty<Matrix>     mWorldMatrix;   ///< Full inherited world matrix
1113   InheritedColor                mWorldColor;    ///< Full inherited color
1114
1115 protected:
1116
1117   Node*               mParent;                       ///< Pointer to parent node (a child is owned by its parent)
1118   Shader*             mAppliedShader;                ///< A pointer to an applied shader
1119   Shader*             mInheritedShader;              ///< A pointer to an inherited shader
1120   RenderTask*         mExclusiveRenderTask;          ///< Nodes can be marked as exclusive to a single RenderTask
1121
1122   NodeAttachmentOwner mAttachment;                   ///< Optional owned attachment
1123   NodeContainer       mChildren;                     ///< Container of children; not owned
1124
1125   Vector3             mGeometryScale;                ///< Applied before calculating world transform.
1126   Vector3             mInitialVolume;                ///< Initial volume... TODO - need a better name
1127
1128   // flags, compressed to bitfield
1129   int  mDirtyFlags:10;                               ///< A composite set of flags for each of the Node properties
1130
1131   bool mIsRoot:1;                                    ///< True if the node cannot have a parent
1132   bool mInheritShader:1;                             ///< Whether the parent's shader should be inherited.
1133   bool mInheritRotation:1;                           ///< Whether the parent's rotation should be inherited.
1134   bool mInheritScale:1;                              ///< Whether the parent's scale should be inherited.
1135   bool mTransmitGeometryScaling:1;                   ///< Whether geometry scaling should be applied to world transform.
1136   bool mInhibitLocalTransform:1;                     ///< whether local transform should be applied.
1137   bool mIsActive:1;                                  ///< When a Node is marked "active" it has been disconnected, and its properties have not been modified
1138
1139   DrawMode::Type          mDrawMode:2;               ///< How the Node and its children should be drawn
1140   PositionInheritanceMode mPositionInheritanceMode:2;///< Determines how position is inherited, 2 bits is enough
1141   ColorMode               mColorMode:2;              ///< Determines whether mWorldColor is inherited, 2 bits is enough
1142
1143   // Changes scope, should be at end of class
1144   DALI_LOG_OBJECT_STRING_DECLARATION;
1145 };
1146
1147 // Messages for Node
1148
1149 inline void SetInheritShaderMessage( EventToUpdate& eventToUpdate, const Node& node, bool inherit )
1150 {
1151   typedef MessageValue1< Node, bool > LocalType;
1152
1153   // Reserve some memory inside the message queue
1154   unsigned int* slot = eventToUpdate.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::SetInheritShader, inherit );
1158 }
1159
1160 inline void SetInheritRotationMessage( EventToUpdate& eventToUpdate, const Node& node, bool inherit )
1161 {
1162   typedef MessageValue1< Node, bool > LocalType;
1163
1164   // Reserve some memory inside the message queue
1165   unsigned int* slot = eventToUpdate.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::SetInheritRotation, inherit );
1169 }
1170
1171 inline void SetInitialVolumeMessage( EventToUpdate& eventToUpdate, const Node& node, const Vector3& initialVolume )
1172 {
1173   typedef MessageValue1< Node, Vector3 > LocalType;
1174
1175   // Reserve some memory inside the message queue
1176   unsigned int* slot = eventToUpdate.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::SetInitialVolume, initialVolume );
1180 }
1181
1182 inline void SetTransmitGeometryScalingMessage( EventToUpdate& eventToUpdate, const Node& node, bool transmitGeometryScaling )
1183 {
1184   typedef MessageValue1< Node, bool > LocalType;
1185
1186   // Reserve some memory inside the message queue
1187   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1188
1189   // Construct message in the message queue memory; note that delete should not be called on the return value
1190   new (slot) LocalType( &node, &Node::SetTransmitGeometryScaling, transmitGeometryScaling );
1191 }
1192
1193 inline void ApplyShaderMessage( EventToUpdate& eventToUpdate, const Node& node, const Shader& constShader )
1194 {
1195   // Update thread can edit the object
1196   Shader& shader = const_cast< Shader& >( constShader );
1197
1198   typedef MessageValue1< Node, Shader* > LocalType;
1199
1200   // Reserve some memory inside the message queue
1201   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1202
1203   // Construct message in the message queue memory; note that delete should not be called on the return value
1204   new (slot) LocalType( &node, &Node::ApplyShader, &shader );
1205 }
1206
1207 inline void RemoveShaderMessage( EventToUpdate& eventToUpdate, const Node& node )
1208 {
1209   typedef Message< Node > LocalType;
1210
1211   // Reserve some memory inside the message queue
1212   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1213
1214   // Construct message in the message queue memory; note that delete should not be called on the return value
1215   new (slot) LocalType( &node, &Node::RemoveShader );
1216 }
1217
1218 inline void SetParentOriginMessage( EventToUpdate& eventToUpdate, const Node& node, const Vector3& origin )
1219 {
1220   typedef MessageValue1< Node, Vector3 > LocalType;
1221
1222   // Reserve some memory inside the message queue
1223   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1224
1225   // Construct message in the message queue memory; note that delete should not be called on the return value
1226   new (slot) LocalType( &node, &Node::SetParentOrigin, origin );
1227 }
1228
1229 inline void SetAnchorPointMessage( EventToUpdate& eventToUpdate, const Node& node, const Vector3& anchor )
1230 {
1231   typedef MessageValue1< Node, Vector3 > LocalType;
1232
1233   // Reserve some memory inside the message queue
1234   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1235
1236   // Construct message in the message queue memory; note that delete should not be called on the return value
1237   new (slot) LocalType( &node, &Node::SetAnchorPoint, anchor );
1238 }
1239
1240 inline void SetPositionInheritanceModeMessage( EventToUpdate& eventToUpdate, const Node& node, PositionInheritanceMode mode )
1241 {
1242   typedef MessageValue1< Node, PositionInheritanceMode > LocalType;
1243
1244   // Reserve some memory inside the message queue
1245   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1246
1247   // Construct message in the message queue memory; note that delete should not be called on the return value
1248   new (slot) LocalType( &node, &Node::SetPositionInheritanceMode, mode );
1249 }
1250
1251 inline void SetInheritScaleMessage( EventToUpdate& eventToUpdate, const Node& node, bool inherit )
1252 {
1253   typedef MessageValue1< Node, bool > LocalType;
1254
1255   // Reserve some memory inside the message queue
1256   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1257
1258   // Construct message in the message queue memory; note that delete should not be called on the return value
1259   new (slot) LocalType( &node, &Node::SetInheritScale, inherit );
1260 }
1261
1262 inline void SetColorModeMessage( EventToUpdate& eventToUpdate, const Node& node, ColorMode colorMode )
1263 {
1264   typedef MessageValue1< Node, ColorMode > LocalType;
1265
1266   // Reserve some memory inside the message queue
1267   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1268
1269   // Construct message in the message queue memory; note that delete should not be called on the return value
1270   new (slot) LocalType( &node, &Node::SetColorMode, colorMode );
1271 }
1272
1273 inline void SetDrawModeMessage( EventToUpdate& eventToUpdate, const Node& node, DrawMode::Type drawMode )
1274 {
1275   typedef MessageValue1< Node, DrawMode::Type > LocalType;
1276
1277   // Reserve some memory inside the message queue
1278   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1279
1280   // Construct message in the message queue memory; note that delete should not be called on the return value
1281   new (slot) LocalType( &node, &Node::SetDrawMode, drawMode );
1282 }
1283
1284 } // namespace SceneGraph
1285
1286 } // namespace Internal
1287
1288 } // namespace Dali
1289
1290 #endif // __DALI_INTERNAL_SCENE_GRAPH_NODE_H_