Protect dereferencing null in Node
[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) 2021 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/integration-api/debug.h>
23 #include <dali/internal/common/message.h>
24 #include <dali/internal/event/common/event-thread-services.h>
25 #include <dali/internal/render/data-providers/node-data-provider.h>
26 #include <dali/internal/update/common/animatable-property.h>
27 #include <dali/internal/update/common/inherited-property.h>
28 #include <dali/internal/update/common/property-owner.h>
29 #include <dali/internal/update/common/scene-graph-buffers.h>
30 #include <dali/internal/update/manager/transform-manager-property.h>
31 #include <dali/internal/update/manager/transform-manager.h>
32 #include <dali/internal/update/nodes/node-declarations.h>
33 #include <dali/internal/update/rendering/scene-graph-renderer.h>
34 #include <dali/public-api/actors/actor-enumerations.h>
35 #include <dali/public-api/actors/draw-mode.h>
36 #include <dali/public-api/math/math-utils.h>
37 #include <dali/public-api/math/quaternion.h>
38 #include <dali/public-api/math/vector3.h>
39
40 namespace Dali
41 {
42 namespace Internal
43 {
44 // Value types used by messages.
45 template<>
46 struct ParameterType<ColorMode> : public BasicType<ColorMode>
47 {
48 };
49 template<>
50 struct ParameterType<ClippingMode::Type> : public BasicType<ClippingMode::Type>
51 {
52 };
53
54 namespace SceneGraph
55 {
56 class DiscardQueue;
57 class Layer;
58 class RenderTask;
59 class UpdateManager;
60
61 // Flags which require the scene renderable lists to be updated
62 static NodePropertyFlags RenderableUpdateFlags = NodePropertyFlags::TRANSFORM | NodePropertyFlags::CHILD_DELETED;
63
64 /**
65  * Node is the base class for all nodes in the Scene Graph.
66  *
67  * Each node provides a transformation which applies to the node and
68  * its children.  Node data is double-buffered. This allows an update
69  * thread to modify node data, without interferring with another
70  * thread reading the values from the previous update traversal.
71  */
72 class Node : public PropertyOwner, public NodeDataProvider
73 {
74 public:
75   // Defaults
76   static const ColorMode DEFAULT_COLOR_MODE;
77
78   // Creation methods
79
80   /**
81    * Construct a new Node.
82    */
83   static Node* New();
84
85   /**
86    * Deletes a Node.
87    */
88   static void Delete(Node* node);
89
90   /**
91    * Called during UpdateManager::DestroyNode shortly before Node is destroyed.
92    */
93   void OnDestroy();
94
95   /**
96    * @return the unique ID of the node
97    */
98   uint32_t GetId() const;
99
100   // Layer interface
101
102   /**
103    * Query whether the node is a layer.
104    * @return True if the node is a layer.
105    */
106   bool IsLayer()
107   {
108     return mIsLayer;
109   }
110
111   /**
112    * Convert a node to a layer.
113    * @return A pointer to the layer, or NULL.
114    */
115   virtual Layer* GetLayer()
116   {
117     return nullptr;
118   }
119
120   /**
121    * Mark an node and its sub tree according to the updated flag.
122    * @param[in] updated The updated flag
123    * (used for partial rendering to mark an animating sub tree for example).
124    */
125   void SetUpdatedTree(bool updated)
126   {
127     mUpdated = updated;
128
129     for(Node* child : mChildren)
130     {
131       child->SetUpdatedTree(updated);
132     }
133   }
134
135   /**
136    * This method sets clipping information on the node based on its hierarchy in the scene-graph.
137    * A value is calculated that can be used during sorting to increase sort speed.
138    * @param[in] clippingId The Clipping ID of the node to set
139    * @param[in] clippingDepth The Clipping Depth of the node to set
140    * @param[in] scissorDepth The Scissor Clipping Depth of the node to set
141    */
142   void SetClippingInformation(const uint32_t clippingId, const uint32_t clippingDepth, const uint32_t scissorDepth)
143   {
144     // We only set up the sort value if we have a stencil clipping depth, IE. At least 1 clipping node has been hit.
145     // If not, if we traverse down a clipping tree and back up, and there is another
146     // node on the parent, this will have a non-zero clipping ID that must be ignored
147     if(clippingDepth > 0u)
148     {
149       mClippingDepth = clippingDepth;
150
151       // Calculate the sort value here on write, as when read (during sort) it may be accessed several times.
152       // The items must be sorted by Clipping ID first (so the ID is kept in the most-significant bits).
153       // For the same ID, the clipping nodes must be first, so we negate the
154       // clipping enabled flag and set it as the least significant bit.
155       mClippingSortModifier = (clippingId << 1u) | (mClippingMode == ClippingMode::DISABLED ? 1u : 0u);
156     }
157     else
158     {
159       // If we do not have a clipping depth, then set this to 0 so we do not have a Clipping ID either.
160       mClippingSortModifier = 0u;
161     }
162
163     // The scissor depth does not modify the clipping sort modifier (as scissor clips are 2D only).
164     // For this reason we can always update the member variable.
165     mScissorDepth = scissorDepth;
166   }
167
168   /**
169    * Gets the Clipping ID for this node.
170    * @return The Clipping ID for this node.
171    */
172   uint32_t GetClippingId() const
173   {
174     return mClippingSortModifier >> 1u;
175   }
176
177   /**
178    * Gets the Clipping Depth for this node.
179    * @return The Clipping Depth for this node.
180    */
181   uint32_t GetClippingDepth() const
182   {
183     return mClippingDepth;
184   }
185
186   /**
187    * Gets the Scissor Clipping Depth for this node.
188    * @return The Scissor Clipping Depth for this node.
189    */
190   uint32_t GetScissorDepth() const
191   {
192     return mScissorDepth;
193   }
194
195   /**
196    * Sets the clipping mode for this node.
197    * @param[in] clippingMode The ClippingMode to set
198    */
199   void SetClippingMode(const ClippingMode::Type clippingMode)
200   {
201     mClippingMode = clippingMode;
202   }
203
204   /**
205    * Gets the Clipping Mode for this node.
206    * @return The ClippingMode of this node
207    */
208   ClippingMode::Type GetClippingMode() const
209   {
210     return mClippingMode;
211   }
212
213   /**
214    * Add a renderer to the node
215    * @param[in] renderer The renderer added to the node
216    */
217   void AddRenderer(Renderer* renderer);
218
219   /**
220    * Remove a renderer from the node
221    * @param[in] renderer The renderer to be removed
222    */
223   void RemoveRenderer(const Renderer* renderer);
224
225   /*
226    * Get the renderer at the given index
227    * @param[in] index
228    */
229   Renderer* GetRendererAt(uint32_t index) const
230   {
231     return mRenderer[index];
232   }
233
234   /**
235    * Retrieve the number of renderers for the node
236    */
237   uint32_t GetRendererCount() const
238   {
239     return static_cast<uint32_t>(mRenderer.Size());
240   }
241
242   // Containment methods
243
244   /**
245    * Query whether a node is the root node. Root nodes cannot have a parent node.
246    * A node becomes a root node, when it is installed by UpdateManager.
247    * @return True if the node is a root node.
248    */
249   bool IsRoot() const
250   {
251     return mIsRoot;
252   }
253
254   /**
255    * Set whether a node is the root node. Root nodes cannot have a parent node.
256    * This method should only be called by UpdateManager.
257    * @pre When isRoot is true, the node must not have a parent.
258    * @param[in] isRoot Whether the node is now a root node.
259    */
260   void SetRoot(bool isRoot);
261
262   /**
263    * Retrieve the parent of a Node.
264    * @return The parent node, or NULL if the Node has not been added to the scene-graph.
265    */
266   Node* GetParent()
267   {
268     return mParent;
269   }
270
271   /**
272    * Retrieve the parent of a Node.
273    * @return The parent node, or NULL if the Node has not been added to the scene-graph.
274    */
275   const Node* GetParent() const
276   {
277     return mParent;
278   }
279
280   /**
281    * @return true if the node is connected to SceneGraph
282    */
283   bool ConnectedToScene()
284   {
285     return IsRoot() || GetParent();
286   }
287
288   /**
289    * Connect a node to the scene-graph.
290    * @pre A node cannot be added to itself.
291    * @pre The parent node is connected to the scene-graph.
292    * @pre The childNode does not already have a parent.
293    * @pre The childNode is not a root node.
294    * @param[in] childNode The child to add.
295    */
296   void ConnectChild(Node* childNode);
297
298   /**
299    * Disconnect a child (& its children) from the scene-graph.
300    * @pre childNode is a child of this Node.
301    * @param[in] updateBufferIndex The current update buffer index.
302    * @param[in] childNode The node to disconnect.
303    */
304   void DisconnectChild(BufferIndex updateBufferIndex, Node& childNode);
305
306   /**
307    * Retrieve the children a Node.
308    * @return The container of children.
309    */
310   const NodeContainer& GetChildren() const
311   {
312     return mChildren;
313   }
314
315   /**
316    * Retrieve the children a Node.
317    * @return The container of children.
318    */
319   NodeContainer& GetChildren()
320   {
321     return mChildren;
322   }
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 = NodePropertyFlags::ALL;
341   }
342
343   /**
344    * Query whether a node is dirty.
345    * @return The dirty flags
346    */
347   NodePropertyFlags GetDirtyFlags() const;
348
349   /**
350    * Query inherited dirty flags.
351    *
352    * @param The parentFlags to or with
353    * @return The inherited dirty flags
354    */
355   NodePropertyFlags GetInheritedDirtyFlags(NodePropertyFlags parentFlags) const;
356
357   /**
358    * Retrieve the parent-origin of the node.
359    * @return The parent-origin.
360    */
361   const Vector3& GetParentOrigin() const
362   {
363     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID && mParentOrigin.mTxManagerData)
364     {
365       return mParentOrigin.Get(0);
366     }
367
368     return Vector3::ZERO;
369   }
370
371   /**
372    * Sets both the local & base parent-origins of the node.
373    * @param[in] origin The new local & base parent-origins.
374    */
375   void SetParentOrigin(const Vector3& origin)
376   {
377     mParentOrigin.Set(0, origin);
378   }
379
380   /**
381    * Retrieve the anchor-point of the node.
382    * @return The anchor-point.
383    */
384   const Vector3& GetAnchorPoint() const
385   {
386     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID && mAnchorPoint.mTxManagerData)
387     {
388       return mAnchorPoint.Get(0);
389     }
390
391     return Vector3::ZERO;
392   }
393
394   /**
395    * Sets both the local & base anchor-points of the node.
396    * @param[in] anchor The new local & base anchor-points.
397    */
398   void SetAnchorPoint(const Vector3& anchor)
399   {
400     mAnchorPoint.Set(0, anchor);
401   }
402
403   /**
404    * Retrieve the local position of the node, relative to its parent.
405    * @param[in] bufferIndex The buffer to read from.
406    * @return The local position.
407    */
408   const Vector3& GetPosition(BufferIndex bufferIndex) const
409   {
410     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID && mPosition.mTxManagerData)
411     {
412       return mPosition.Get(bufferIndex);
413     }
414
415     return Vector3::ZERO;
416   }
417
418   /**
419    * Retrieve the position of the node derived from the position of all its parents.
420    * @return The world position.
421    */
422   const Vector3& GetWorldPosition(BufferIndex bufferIndex) const
423   {
424     return mWorldPosition.Get(bufferIndex);
425   }
426
427   /**
428    * Set whether the Node inherits position.
429    * @param[in] inherit True if the parent position is inherited.
430    */
431   void SetInheritPosition(bool inherit)
432   {
433     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
434     {
435       mTransformManagerData.Manager()->SetInheritPosition(mTransformManagerData.Id(), inherit);
436     }
437   }
438
439   /**
440    * Retrieve the local orientation of the node, relative to its parent.
441    * @param[in] bufferIndex The buffer to read from.
442    * @return The local orientation.
443    */
444   const Quaternion& GetOrientation(BufferIndex bufferIndex) const
445   {
446     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID && mOrientation.mTxManagerData)
447     {
448       return mOrientation.Get(0);
449     }
450
451     return Quaternion::IDENTITY;
452   }
453
454   /**
455    * Retrieve the orientation of the node derived from the rotation of all its parents.
456    * @param[in] bufferIndex The buffer to read from.
457    * @return The world rotation.
458    */
459   const Quaternion& GetWorldOrientation(BufferIndex bufferIndex) const
460   {
461     return mWorldOrientation.Get(0);
462   }
463
464   /**
465    * Set whether the Node inherits orientation.
466    * @param[in] inherit True if the parent orientation is inherited.
467    */
468   void SetInheritOrientation(bool inherit)
469   {
470     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
471     {
472       mTransformManagerData.Manager()->SetInheritOrientation(mTransformManagerData.Id(), inherit);
473     }
474   }
475
476   /**
477    * Retrieve the local scale of the node, relative to its parent.
478    * @param[in] bufferIndex The buffer to read from.
479    * @return The local scale.
480    */
481   const Vector3& GetScale(BufferIndex bufferIndex) const
482   {
483     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID && mScale.mTxManagerData)
484     {
485       return mScale.Get(0);
486     }
487
488     return Vector3::ONE;
489   }
490
491   /**
492    * Retrieve the scale of the node derived from the scale of all its parents.
493    * @param[in] bufferIndex The buffer to read from.
494    * @return The world scale.
495    */
496   const Vector3& GetWorldScale(BufferIndex bufferIndex) const
497   {
498     return mWorldScale.Get(0);
499   }
500
501   /**
502    * Set whether the Node inherits scale.
503    * @param inherit True if the Node inherits scale.
504    */
505   void SetInheritScale(bool inherit)
506   {
507     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
508     {
509       mTransformManagerData.Manager()->SetInheritScale(mTransformManagerData.Id(), inherit);
510     }
511   }
512
513   /**
514    * Retrieve the visibility of the node.
515    * @param[in] bufferIndex The buffer to read from.
516    * @return True if the node is visible.
517    */
518   bool IsVisible(BufferIndex bufferIndex) const
519   {
520     return mVisible[bufferIndex];
521   }
522
523   /**
524    * Retrieve the opacity of the node.
525    * @param[in] bufferIndex The buffer to read from.
526    * @return The opacity.
527    */
528   float GetOpacity(BufferIndex bufferIndex) const
529   {
530     return mColor[bufferIndex].a;
531   }
532
533   /**
534    * Retrieve the color of the node.
535    * @param[in] bufferIndex The buffer to read from.
536    * @return The color.
537    */
538   const Vector4& GetColor(BufferIndex bufferIndex) const
539   {
540     return mColor[bufferIndex];
541   }
542
543   /**
544    * Sets the color of the node derived from the color of all its parents.
545    * @param[in] color The world color.
546    * @param[in] updateBufferIndex The current update buffer index.
547    */
548   void SetWorldColor(const Vector4& color, BufferIndex updateBufferIndex)
549   {
550     mWorldColor.Set(updateBufferIndex, color);
551   }
552
553   /**
554    * Sets the color of the node derived from the color of all its parents.
555    * This method should only be called when the parents world color is up-to-date.
556    * @pre The node has a parent.
557    * @param[in] updateBufferIndex The current update buffer index.
558    */
559   void InheritWorldColor(BufferIndex updateBufferIndex)
560   {
561     DALI_ASSERT_DEBUG(mParent != NULL);
562
563     // default first
564     if(mColorMode == USE_OWN_MULTIPLY_PARENT_ALPHA)
565     {
566       const Vector4& ownColor = mColor[updateBufferIndex];
567       mWorldColor.Set(updateBufferIndex, ownColor.r, ownColor.g, ownColor.b, ownColor.a * mParent->GetWorldColor(updateBufferIndex).a);
568     }
569     else if(mColorMode == USE_OWN_MULTIPLY_PARENT_COLOR)
570     {
571       mWorldColor.Set(updateBufferIndex, mParent->GetWorldColor(updateBufferIndex) * mColor[updateBufferIndex]);
572     }
573     else if(mColorMode == USE_PARENT_COLOR)
574     {
575       mWorldColor.Set(updateBufferIndex, mParent->GetWorldColor(updateBufferIndex));
576     }
577     else // USE_OWN_COLOR
578     {
579       mWorldColor.Set(updateBufferIndex, mColor[updateBufferIndex]);
580     }
581   }
582
583   /**
584    * Copies the previous inherited scale, if this changed in the previous frame.
585    * This method should be called instead of InheritWorldScale i.e. if the inherited scale
586    * does not need to be recalculated in the current frame.
587    * @param[in] updateBufferIndex The current update buffer index.
588    */
589   void CopyPreviousWorldColor(BufferIndex updateBufferIndex)
590   {
591     mWorldColor.CopyPrevious(updateBufferIndex);
592   }
593
594   /**
595    * Retrieve the color of the node, possibly derived from the color
596    * of all its parents, depending on the value of mColorMode.
597    * @param[in] bufferIndex The buffer to read from.
598    * @return The world color.
599    */
600   const Vector4& GetWorldColor(BufferIndex bufferIndex) const
601   {
602     return mWorldColor[bufferIndex];
603   }
604
605   /**
606    * Set the color mode. This specifies whether the Node uses its own color,
607    * or inherits its parent color.
608    * @param[in] colorMode The new color mode.
609    */
610   void SetColorMode(ColorMode colorMode)
611   {
612     mColorMode = colorMode;
613
614     SetDirtyFlag(NodePropertyFlags::COLOR);
615   }
616
617   /**
618    * Retrieve the color mode.
619    * @return The color mode.
620    */
621   ColorMode GetColorMode() const
622   {
623     return mColorMode;
624   }
625
626   /**
627    * Retrieve the size of the node.
628    * @param[in] bufferIndex The buffer to read from.
629    * @return The size.
630    */
631   const Vector3& GetSize(BufferIndex bufferIndex) const
632   {
633     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID && mSize.mTxManagerData)
634     {
635       return mSize.Get(0);
636     }
637
638     return Vector3::ZERO;
639   }
640
641   /**
642    * Retrieve the update size hint of the node.
643    * @return The update size hint.
644    */
645   const Vector3& GetUpdateSizeHint() const
646   {
647     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
648     {
649       return mUpdateSizeHint.Get(0);
650     }
651
652     return Vector3::ZERO;
653   }
654
655   /**
656    * Retrieve the bounding sphere of the node
657    * @return A vector4 describing the bounding sphere. XYZ is the center and W is the radius
658    */
659   const Vector4& GetBoundingSphere() const
660   {
661     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
662     {
663       return mTransformManagerData.Manager()->GetBoundingSphere(mTransformManagerData.Id());
664     }
665
666     return Vector4::ZERO;
667   }
668
669   /**
670    * Retrieve world matrix and size of the node
671    * @param[out] The local to world matrix of the node
672    * @param[out] size The current size of the node
673    */
674   void GetWorldMatrixAndSize(Matrix& worldMatrix, Vector3& size) const
675   {
676     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID)
677     {
678       mTransformManagerData.Manager()->GetWorldMatrixAndSize(mTransformManagerData.Id(), worldMatrix, size);
679     }
680   }
681
682   /**
683    * Checks if local matrix has changed since last update
684    * @return true if local matrix has changed, false otherwise
685    */
686   bool IsLocalMatrixDirty() const
687   {
688     return (mTransformManagerData.Id() != INVALID_TRANSFORM_ID) &&
689            (mTransformManagerData.Manager()->IsLocalMatrixDirty(mTransformManagerData.Id()));
690   }
691
692   /**
693    * Retrieve the cached world-matrix of a node.
694    * @param[in] bufferIndex The buffer to read from.
695    * @return The world-matrix.
696    */
697   const Matrix& GetWorldMatrix(BufferIndex bufferIndex) const
698   {
699     return mWorldMatrix.Get(bufferIndex);
700   }
701
702   /**
703    * Mark the node as exclusive to a single RenderTask.
704    * @param[in] renderTask The render-task, or NULL if the Node is not exclusive to a single RenderTask.
705    */
706   void SetExclusiveRenderTask(RenderTask* renderTask)
707   {
708     mExclusiveRenderTask = renderTask;
709   }
710
711   /**
712    * Query whether the node is exclusive to a single RenderTask.
713    * @return The render-task, or NULL if the Node is not exclusive to a single RenderTask.
714    */
715   RenderTask* GetExclusiveRenderTask() const
716   {
717     return mExclusiveRenderTask;
718   }
719
720   /**
721    * Set how the Node and its children should be drawn; see Dali::Actor::SetDrawMode() for more details.
722    * @param[in] drawMode The new draw-mode to use.
723    */
724   void SetDrawMode(const DrawMode::Type& drawMode)
725   {
726     mDrawMode = drawMode;
727   }
728
729   /**
730    * Returns whether node is an overlay or not.
731    * @return True if node is an overlay, false otherwise.
732    */
733   DrawMode::Type GetDrawMode() const
734   {
735     return mDrawMode;
736   }
737
738   void SetTransparent(bool transparent)
739   {
740     mTransparent = transparent;
741   }
742
743   bool IsTransparent() const
744   {
745     return mTransparent;
746   }
747
748   /*
749    * Returns the transform id of the node
750    * @return The transform component id of the node
751    */
752   TransformId GetTransformId() const
753   {
754     return mTransformManagerData.Id();
755   }
756
757   /**
758    * Equality operator, checks for identity, not values.
759    * @param[in]
760    */
761   bool operator==(const Node* rhs) const
762   {
763     return (this == rhs);
764   }
765
766   /**
767    * @brief Sets the sibling order of the node
768    * @param[in] order The new order
769    */
770   void SetDepthIndex(uint32_t depthIndex)
771   {
772     mDepthIndex = depthIndex;
773   }
774
775   /**
776    * @brief Get the depth index of the node
777    * @return Current depth index
778    */
779   uint32_t GetDepthIndex() const
780   {
781     return mDepthIndex;
782   }
783
784   /**
785    * @brief Sets the boolean which states whether the position should use the anchor-point.
786    * @param[in] positionUsesAnchorPoint True if the position should use the anchor-point
787    */
788   void SetPositionUsesAnchorPoint(bool positionUsesAnchorPoint)
789   {
790     if(mTransformManagerData.Id() != INVALID_TRANSFORM_ID && mPositionUsesAnchorPoint != positionUsesAnchorPoint)
791     {
792       mPositionUsesAnchorPoint = positionUsesAnchorPoint;
793       mTransformManagerData.Manager()->SetPositionUsesAnchorPoint(mTransformManagerData.Id(), mPositionUsesAnchorPoint);
794     }
795   }
796
797   /**
798    * @brief Sets whether the node is culled or not.
799    * @param[in] bufferIndex The buffer to read from.
800    * @param[in] culled True if the node is culled.
801    */
802   void SetCulled(BufferIndex bufferIndex, bool culled)
803   {
804     mCulled[bufferIndex] = culled;
805   }
806
807   /**
808    * @brief Retrieves whether the node is culled or not.
809    * @param[in] bufferIndex The buffer to read from.
810    * @return True if the node is culled.
811    */
812   bool IsCulled(BufferIndex bufferIndex) const
813   {
814     return mCulled[bufferIndex];
815   }
816
817 public:
818   /**
819    * @copydoc UniformMap::Add
820    */
821   void AddUniformMapping(const UniformPropertyMapping& map) override;
822
823   /**
824    * @copydoc UniformMap::Remove
825    */
826   void RemoveUniformMapping(const ConstString& uniformName) override;
827
828   /**
829    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::IsAnimationPossible
830    */
831   bool IsAnimationPossible() const override;
832
833   /**
834    * Prepare the node for rendering.
835    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
836    * @param[in] updateBufferIndex The current update buffer index.
837    */
838   void PrepareRender(BufferIndex bufferIndex);
839
840   /**
841    * Called by UpdateManager when the node is added.
842    * Creates a new transform component in the transform manager and initialize all the properties
843    * related to the transformation
844    * @param[in] transformManager A pointer to the trnasform manager (Owned by UpdateManager)
845    */
846   void CreateTransform(SceneGraph::TransformManager* transformManager);
847
848   /**
849    * Reset dirty flags
850    */
851   void ResetDirtyFlags(BufferIndex updateBufferIndex);
852
853 protected:
854   /**
855    * Set the parent of a Node.
856    * @param[in] parentNode the new parent.
857    */
858   void SetParent(Node& parentNode);
859
860 protected:
861   /**
862    * Protected constructor; See also Node::New()
863    */
864   Node();
865
866   /**
867    * Protected virtual destructor; See also Node::Delete( Node* )
868    * Kept protected to allow destructor chaining from layer
869    */
870   ~Node() override;
871
872 private: // from NodeDataProvider
873   /**
874    * @copydoc NodeDataProvider::GetModelMatrix
875    */
876   const Matrix& GetModelMatrix(BufferIndex bufferIndex) const override
877   {
878     return GetWorldMatrix(bufferIndex);
879   }
880
881   /**
882    * @copydoc NodeDataProvider::GetRenderColor
883    */
884   const Vector4& GetRenderColor(BufferIndex bufferIndex) const override
885   {
886     return GetWorldColor(bufferIndex);
887   }
888
889 public: // From UniformMapDataProvider
890   /**
891    * @copydoc UniformMapDataProvider::GetUniformMapChanged
892    */
893   bool GetUniformMapChanged(BufferIndex bufferIndex) const override
894   {
895     return mUniformMapChanged[bufferIndex];
896   }
897
898   /**
899    * @copydoc UniformMapDataProvider::GetUniformMap
900    */
901   const CollectedUniformMap& GetUniformMap(BufferIndex bufferIndex) const override
902   {
903     return mCollectedUniformMap[bufferIndex];
904   }
905
906 private:
907   // Undefined
908   Node(const Node&);
909
910   // Undefined
911   Node& operator=(const Node& rhs);
912
913   /**
914    * Recursive helper to disconnect a Node and its children.
915    * Disconnected Nodes have no parent or children.
916    * @param[in] updateBufferIndex The current update buffer index.
917    */
918   void RecursiveDisconnectFromSceneGraph(BufferIndex updateBufferIndex);
919
920 public: // Default properties
921   using TransformManagerParentsOrigin = TransformManagerPropertyVector3<TRANSFORM_PROPERTY_PARENT_ORIGIN>;
922   using TransformManagerAnchorPoint   = TransformManagerPropertyVector3<TRANSFORM_PROPERTY_ANCHOR_POINT>;
923   using TransformManagerSize          = TransformManagerPropertyVector3<TRANSFORM_PROPERTY_SIZE>;
924   using TransformManagerPosition      = TransformManagerPropertyVector3<TRANSFORM_PROPERTY_POSITION>;
925   using TransformManagerScale         = TransformManagerPropertyVector3<TRANSFORM_PROPERTY_SCALE>;
926
927   TransformManagerData               mTransformManagerData;
928   TransformManagerParentsOrigin      mParentOrigin; ///< Local transform; the position is relative to this. Sets the Transform flag dirty when changed
929   TransformManagerAnchorPoint        mAnchorPoint;  ///< Local transform; local center of rotation. Sets the Transform flag dirty when changed
930   TransformManagerSize               mSize;         ///< Size is provided for layouting
931   TransformManagerPosition           mPosition;     ///< Local transform; distance between parent-origin & anchor-point
932   TransformManagerScale              mScale;        ///< Local transform; scale relative to parent node
933   TransformManagerPropertyQuaternion mOrientation;  ///< Local transform; rotation relative to parent node
934
935   AnimatableProperty<bool>    mVisible;        ///< Visibility can be inherited from the Node hierachy
936   AnimatableProperty<bool>    mCulled;         ///< True if the node is culled. This is not animatable. It is just double-buffered.
937   AnimatableProperty<Vector4> mColor;          ///< Color can be inherited from the Node hierarchy
938   AnimatableProperty<Vector3> mUpdateSizeHint; ///< Update size hint is provided for damaged area calculation. This is not animatable. It is just double-buffered. (Because all these bloody properties are).
939
940   // Inherited properties; read-only from public API
941
942   TransformManagerVector3Input    mWorldPosition; ///< Full inherited position
943   TransformManagerVector3Input    mWorldScale;
944   TransformManagerQuaternionInput mWorldOrientation; ///< Full inherited orientation
945   TransformManagerMatrixInput     mWorldMatrix;      ///< Full inherited world matrix
946   InheritedColor                  mWorldColor;       ///< Full inherited color
947
948   uint32_t       mClippingSortModifier; ///< Contains bit-packed clipping information for quick access when sorting
949   const uint32_t mId;                   ///< The Unique ID of the node.
950
951 protected:
952   static uint32_t mNodeCounter; ///< count of total nodes, used for unique ids
953
954   Node*       mParent;              ///< Pointer to parent node (a child is owned by its parent)
955   RenderTask* mExclusiveRenderTask; ///< Nodes can be marked as exclusive to a single RenderTask
956
957   RendererContainer mRenderer; ///< Container of renderers; not owned
958
959   NodeContainer mChildren; ///< Container of children; not owned
960
961   CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps of the node
962   uint32_t            mUniformMapChanged[2];   ///< Records if the uniform map has been altered this frame
963   uint32_t            mClippingDepth;          ///< The number of stencil clipping nodes deep this node is
964   uint32_t            mScissorDepth;           ///< The number of scissor clipping nodes deep this node is
965
966   uint32_t mDepthIndex; ///< Depth index of the node
967
968   // flags, compressed to bitfield
969   NodePropertyFlags  mDirtyFlags;                  ///< Dirty flags for each of the Node properties
970   uint32_t           mRegenerateUniformMap : 2;    ///< Indicate if the uniform map has to be regenerated this frame
971   DrawMode::Type     mDrawMode : 3;                ///< How the Node and its children should be drawn
972   ColorMode          mColorMode : 3;               ///< Determines whether mWorldColor is inherited, 2 bits is enough
973   ClippingMode::Type mClippingMode : 3;            ///< The clipping mode of this node
974   bool               mIsRoot : 1;                  ///< True if the node cannot have a parent
975   bool               mIsLayer : 1;                 ///< True if the node is a layer
976   bool               mPositionUsesAnchorPoint : 1; ///< True if the node should use the anchor-point when calculating the position
977   bool               mTransparent : 1;             ///< True if this node is transparent. This value do not affect children.
978
979   // Changes scope, should be at end of class
980   DALI_LOG_OBJECT_STRING_DECLARATION;
981 };
982
983 // Messages for Node
984
985 inline void SetInheritOrientationMessage(EventThreadServices& eventThreadServices, const Node& node, bool inherit)
986 {
987   using LocalType = MessageValue1<Node, bool>;
988
989   // Reserve some memory inside the message queue
990   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
991
992   // Construct message in the message queue memory; note that delete should not be called on the return value
993   new(slot) LocalType(&node, &Node::SetInheritOrientation, inherit);
994 }
995
996 inline void SetParentOriginMessage(EventThreadServices& eventThreadServices, const Node& node, const Vector3& origin)
997 {
998   using LocalType = MessageValue1<Node, Vector3>;
999
1000   // Reserve some memory inside the message queue
1001   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
1002
1003   // Construct message in the message queue memory; note that delete should not be called on the return value
1004   new(slot) LocalType(&node, &Node::SetParentOrigin, origin);
1005 }
1006
1007 inline void SetAnchorPointMessage(EventThreadServices& eventThreadServices, const Node& node, const Vector3& anchor)
1008 {
1009   using LocalType = MessageValue1<Node, Vector3>;
1010
1011   // Reserve some memory inside the message queue
1012   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
1013
1014   // Construct message in the message queue memory; note that delete should not be called on the return value
1015   new(slot) LocalType(&node, &Node::SetAnchorPoint, anchor);
1016 }
1017
1018 inline void SetInheritPositionMessage(EventThreadServices& eventThreadServices, const Node& node, bool inherit)
1019 {
1020   using LocalType = MessageValue1<Node, bool>;
1021
1022   // Reserve some memory inside the message queue
1023   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
1024
1025   // Construct message in the message queue memory; note that delete should not be called on the return value
1026   new(slot) LocalType(&node, &Node::SetInheritPosition, inherit);
1027 }
1028
1029 inline void SetInheritScaleMessage(EventThreadServices& eventThreadServices, const Node& node, bool inherit)
1030 {
1031   using LocalType = MessageValue1<Node, bool>;
1032
1033   // Reserve some memory inside the message queue
1034   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
1035
1036   // Construct message in the message queue memory; note that delete should not be called on the return value
1037   new(slot) LocalType(&node, &Node::SetInheritScale, inherit);
1038 }
1039
1040 inline void SetColorModeMessage(EventThreadServices& eventThreadServices, const Node& node, ColorMode colorMode)
1041 {
1042   using LocalType = MessageValue1<Node, ColorMode>;
1043
1044   // Reserve some memory inside the message queue
1045   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
1046
1047   // Construct message in the message queue memory; note that delete should not be called on the return value
1048   new(slot) LocalType(&node, &Node::SetColorMode, colorMode);
1049 }
1050
1051 inline void SetDrawModeMessage(EventThreadServices& eventThreadServices, const Node& node, DrawMode::Type drawMode)
1052 {
1053   using LocalType = MessageValue1<Node, DrawMode::Type>;
1054
1055   // Reserve some memory inside the message queue
1056   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
1057
1058   // Construct message in the message queue memory; note that delete should not be called on the return value
1059   new(slot) LocalType(&node, &Node::SetDrawMode, drawMode);
1060 }
1061
1062 inline void SetTransparentMessage(EventThreadServices& eventThreadServices, const Node& node, bool transparent)
1063 {
1064   using LocalType = MessageValue1<Node, bool>;
1065
1066   // Reserve some memory inside the message queue
1067   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
1068
1069   // Construct message in the message queue memory; note that delete should not be called on the return value
1070   new(slot) LocalType(&node, &Node::SetTransparent, transparent);
1071 }
1072
1073 inline void DetachRendererMessage(EventThreadServices& eventThreadServices, const Node& node, const Renderer& renderer)
1074 {
1075   using LocalType = MessageValue1<Node, const Renderer*>;
1076
1077   // Reserve some memory inside the message queue
1078   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
1079
1080   // Construct message in the message queue memory; note that delete should not be called on the return value
1081   new(slot) LocalType(&node, &Node::RemoveRenderer, &renderer);
1082 }
1083
1084 inline void SetDepthIndexMessage(EventThreadServices& eventThreadServices, const Node& node, uint32_t depthIndex)
1085 {
1086   using LocalType = MessageValue1<Node, uint32_t>;
1087
1088   // Reserve some memory inside the message queue
1089   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
1090
1091   // Construct message in the message queue memory; note that delete should not be called on the return value
1092   new(slot) LocalType(&node, &Node::SetDepthIndex, depthIndex);
1093 }
1094
1095 inline void SetClippingModeMessage(EventThreadServices& eventThreadServices, const Node& node, ClippingMode::Type clippingMode)
1096 {
1097   using LocalType = MessageValue1<Node, ClippingMode::Type>;
1098
1099   // Reserve some memory inside the message queue
1100   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
1101
1102   // Construct message in the message queue memory; note that delete should not be called on the return value
1103   new(slot) LocalType(&node, &Node::SetClippingMode, clippingMode);
1104 }
1105
1106 inline void SetPositionUsesAnchorPointMessage(EventThreadServices& eventThreadServices, const Node& node, bool positionUsesAnchorPoint)
1107 {
1108   using LocalType = MessageValue1<Node, bool>;
1109
1110   // Reserve some memory inside the message queue
1111   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
1112
1113   // Construct message in the message queue memory; note that delete should not be called on the return value
1114   new(slot) LocalType(&node, &Node::SetPositionUsesAnchorPoint, positionUsesAnchorPoint);
1115 }
1116
1117 } // namespace SceneGraph
1118
1119 // Template specialisation for OwnerPointer<Node>, because delete is protected
1120 template<>
1121 inline void OwnerPointer<Dali::Internal::SceneGraph::Node>::Reset()
1122 {
1123   if(mObject != nullptr)
1124   {
1125     Dali::Internal::SceneGraph::Node::Delete(mObject);
1126     mObject = nullptr;
1127   }
1128 }
1129 } // namespace Internal
1130
1131 // Template specialisations for OwnerContainer<Node*>, because delete is protected
1132 template<>
1133 inline void OwnerContainer<Dali::Internal::SceneGraph::Node*>::Delete(Dali::Internal::SceneGraph::Node* pointer)
1134 {
1135   Dali::Internal::SceneGraph::Node::Delete(pointer);
1136 }
1137 } // namespace Dali
1138
1139 #endif // DALI_INTERNAL_SCENE_GRAPH_NODE_H