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