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