Merge "Sync UTC harness" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / transform-manager-property.h
1 #ifndef TRANSFORM_MANAGER_PROPERTY_H_
2 #define TRANSFORM_MANAGER_PROPERTY_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 #include <dali/internal/update/common/animatable-property.h>
22 #include <dali/internal/update/manager/transform-manager.h>
23
24 namespace Dali
25 {
26 namespace Internal
27 {
28 namespace SceneGraph
29 {
30 template<typename T>
31 struct TransformManagerPropertyHandler : public AnimatablePropertyBase
32 {
33   /**
34    * Constructor
35    */
36   TransformManagerPropertyHandler() = default;
37
38   /**
39    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
40    */
41   Dali::Property::Type GetType() const override = 0;
42
43   /**
44    * @copydoc Dali::SceneGraph::AnimatableProperty::Get()
45    */
46   virtual T& Get(BufferIndex bufferIndex) = 0;
47
48   /**
49    * @copydoc Dali::SceneGraph::AnimatableProperty::Get()
50    */
51   virtual const T& Get(BufferIndex bufferIndex) const = 0;
52
53   /**
54    * @copydoc Dali::PropertyInput::GetVector3()
55    */
56   const Vector3& GetVector3(BufferIndex bufferIndex) const override
57   {
58     return Vector3::ZERO;
59   }
60
61   /**
62    * Retrieve a component of property
63    * @param[in] component The component of the property
64    */
65   virtual const float& GetFloatComponent(uint32_t component) const
66   {
67     DALI_ASSERT_DEBUG(0 && "Invalid call");
68
69     static const float value = 0.0f;
70     return value;
71   }
72
73   /**
74    * Set the property value. This will only persist for the current frame; the property
75    * will be reset with the base value, at the beginning of the next frame.
76    * @param[in] bufferIndex Not used
77    * @param[in] value The new property value.
78    */
79   virtual void Set(BufferIndex bufferIndex, const T& value) = 0;
80
81   /**
82    * Change a component of property
83    * @param[in] value The new value of the component
84    * @param[in] component The component of the property
85    */
86   virtual void SetFloatComponent(float value, uint32_t component)
87   {
88   }
89
90   /**
91    * @copydoc Dali::AnimatableProperty::Bake()
92    */
93   virtual void Bake(BufferIndex bufferIndex, const T& value) = 0;
94
95   /**
96    * Bake a component of a property
97    * @param[in] value The new value of the component
98    * @param[in] component The component of the property
99    */
100   virtual void BakeFloatComponent(float value, uint32_t component)
101   {
102   }
103
104   /**
105    * @copydoc Dali::AnimatableProperty::BakeX()
106    */
107   virtual void BakeX(BufferIndex bufferIndex, float value)
108   {
109   }
110
111   /**
112    * @copydoc Dali::AnimatableProperty::BakeY()
113    */
114   virtual void BakeY(BufferIndex bufferIndex, float value)
115   {
116   }
117
118   /**
119    * @copydoc Dali::AnimatableProperty::BakeZ()
120    */
121   virtual void BakeZ(BufferIndex bufferIndex, float value)
122   {
123   }
124
125   /**
126    * @copydoc Dali::AnimatableProperty::BakeRelative()
127    */
128   virtual void BakeRelative(BufferIndex bufferIndex, const T& value) = 0;
129
130   /**
131    * @copydoc Dali::AnimatableProperty::BakeRelativeMultiply()
132    */
133   virtual void BakeRelativeMultiply(BufferIndex bufferIndex, const T& value){};
134
135   /**
136    * @copydoc Dali::AnimatableProperty::ResetToBaseValue()
137    */
138   void ResetToBaseValue(BufferIndex updateBufferIndex) override
139   {
140   }
141
142   /**
143    * @copydoc Dali::AnimatableProperty::IsClean()
144    */
145   bool IsClean() const override
146   {
147     return false;
148   }
149
150   /**
151    * @copydoc Dali::Internal::PropertyInputImpl::IsTransformManagerProperty()
152    */
153   bool IsTransformManagerProperty() const override
154   {
155     return true;
156   }
157 };
158
159 template<TransformManagerProperty PropertyT, size_t TxMgrDataOffset>
160 struct TransformManagerPropertyVector3 final : public TransformManagerPropertyHandler<Vector3>
161 {
162   enum
163   {
164     TRANSFORM_MANAGER_DATA_OFFSET = TxMgrDataOffset
165   };
166   TransformManagerData* GetTxManagerData()
167   {
168     return reinterpret_cast<TransformManagerData*>(
169       reinterpret_cast<uint8_t*>(this) - TRANSFORM_MANAGER_DATA_OFFSET);
170   }
171   const TransformManagerData* GetTxManagerData() const
172   {
173     return reinterpret_cast<const TransformManagerData*>(
174       reinterpret_cast<const uint8_t*>(this) - TRANSFORM_MANAGER_DATA_OFFSET);
175   }
176
177   TransformManagerPropertyVector3() = default;
178
179   Dali::Property::Type GetType() const override
180   {
181     return Dali::PropertyTypes::Get<Vector3>();
182   }
183
184   Vector3& Get(BufferIndex bufferIndex) override
185   {
186     return GetTxManagerData()->Manager()->GetVector3PropertyValue(GetTxManagerData()->Id(), PropertyT);
187   }
188
189   const Vector3& Get(BufferIndex bufferIndex) const override
190   {
191     return GetTxManagerData()->Manager()->GetVector3PropertyValue(GetTxManagerData()->Id(), PropertyT);
192   }
193
194   const Vector3& GetVector3(BufferIndex bufferIndex) const override
195   {
196     return Get(bufferIndex);
197   }
198
199   const float& GetFloatComponent(uint32_t component) const override
200   {
201     return GetTxManagerData()->Manager()->GetVector3PropertyComponentValue(GetTxManagerData()->Id(), PropertyT, component);
202   }
203
204   void Set(BufferIndex bufferIndex, const Vector3& value) override
205   {
206     GetTxManagerData()->Manager()->SetVector3PropertyValue(GetTxManagerData()->Id(), PropertyT, value);
207   }
208
209   void SetComponent(BufferIndex bufferIndex, float value, uint32_t component)
210   {
211     GetTxManagerData()->Manager()->SetVector3PropertyComponentValue(GetTxManagerData()->Id(), PropertyT, value, component);
212   }
213
214   void BakeComponent(BufferIndex bufferIndex, float value, uint32_t component)
215   {
216     GetTxManagerData()->Manager()->BakeVector3PropertyComponentValue(GetTxManagerData()->Id(), PropertyT, value, component);
217   }
218
219   void Bake(BufferIndex bufferIndex, const Vector3& value) override
220   {
221     GetTxManagerData()->Manager()->BakeVector3PropertyValue(GetTxManagerData()->Id(), PropertyT, value);
222   }
223
224   void BakeX(BufferIndex bufferIndex, float value) override
225   {
226     GetTxManagerData()->Manager()->BakeXVector3PropertyValue(GetTxManagerData()->Id(), PropertyT, value);
227   }
228
229   void BakeY(BufferIndex bufferIndex, float value) override
230   {
231     GetTxManagerData()->Manager()->BakeYVector3PropertyValue(GetTxManagerData()->Id(), PropertyT, value);
232   }
233
234   void BakeZ(BufferIndex bufferIndex, float value) override
235   {
236     GetTxManagerData()->Manager()->BakeZVector3PropertyValue(GetTxManagerData()->Id(), PropertyT, value);
237   }
238
239   void SetFloatComponent(float value, uint32_t component) override
240   {
241     GetTxManagerData()->Manager()->SetVector3PropertyComponentValue(GetTxManagerData()->Id(), PropertyT, value, component);
242   }
243
244   void BakeFloatComponent(float value, uint32_t component) override
245   {
246     GetTxManagerData()->Manager()->BakeVector3PropertyComponentValue(GetTxManagerData()->Id(), PropertyT, value, component);
247   }
248
249   void BakeRelative(BufferIndex bufferIndex, const Vector3& value) override
250   {
251     GetTxManagerData()->Manager()->BakeRelativeVector3PropertyValue(GetTxManagerData()->Id(), PropertyT, value);
252   }
253
254   void BakeRelativeMultiply(BufferIndex bufferIndex, const Vector3& value) override
255   {
256     GetTxManagerData()->Manager()->BakeMultiplyVector3PropertyValue(GetTxManagerData()->Id(), PropertyT, value);
257   }
258 };
259
260 template<size_t TxMgrDataOffset>
261 class TransformManagerPropertyQuaternion final : public TransformManagerPropertyHandler<Quaternion>
262 {
263 public:
264   enum
265   {
266     TRANSFORM_MANAGER_DATA_OFFSET = TxMgrDataOffset
267   };
268   TransformManagerData* GetTxManagerData()
269   {
270     return reinterpret_cast<TransformManagerData*>(
271       reinterpret_cast<uint8_t*>(this) - TRANSFORM_MANAGER_DATA_OFFSET);
272   }
273   const TransformManagerData* GetTxManagerData() const
274   {
275     return reinterpret_cast<const TransformManagerData*>(
276       reinterpret_cast<const uint8_t*>(this) - TRANSFORM_MANAGER_DATA_OFFSET);
277   }
278
279   TransformManagerPropertyQuaternion() = default;
280
281   Dali::Property::Type GetType() const override
282   {
283     return Dali::PropertyTypes::Get<Quaternion>();
284   }
285
286   Quaternion& Get(BufferIndex bufferIndex) override
287   {
288     return GetTxManagerData()->Manager()->GetQuaternionPropertyValue(GetTxManagerData()->Id());
289   }
290
291   const Quaternion& Get(BufferIndex bufferIndex) const override
292   {
293     return GetTxManagerData()->Manager()->GetQuaternionPropertyValue(GetTxManagerData()->Id());
294   }
295
296   void Set(BufferIndex bufferIndex, const Quaternion& value) override
297   {
298     return GetTxManagerData()->Manager()->SetQuaternionPropertyValue(GetTxManagerData()->Id(), value);
299   }
300
301   void Bake(BufferIndex bufferIndex, const Quaternion& value) override
302   {
303     return GetTxManagerData()->Manager()->BakeQuaternionPropertyValue(GetTxManagerData()->Id(), value);
304   }
305
306   void BakeRelative(BufferIndex bufferIndex, const Quaternion& value) override
307   {
308     return GetTxManagerData()->Manager()->BakeRelativeQuaternionPropertyValue(GetTxManagerData()->Id(), value);
309   }
310
311   const Quaternion& GetQuaternion(BufferIndex bufferIndex) const override
312   {
313     return Get(bufferIndex);
314   }
315 };
316
317 /**
318  * A Vector3 property used as input.
319  */
320 template<size_t TxMgrDataOffset>
321 class TransformManagerVector3Input : public PropertyInputImpl
322 {
323 public:
324   enum
325   {
326     TRANSFORM_MANAGER_DATA_OFFSET = TxMgrDataOffset
327   };
328   TransformManagerData* GetTxManagerData()
329   {
330     return reinterpret_cast<TransformManagerData*>(
331       reinterpret_cast<uint8_t*>(this) - TRANSFORM_MANAGER_DATA_OFFSET);
332   }
333   const TransformManagerData* GetTxManagerData() const
334   {
335     return reinterpret_cast<const TransformManagerData*>(
336       reinterpret_cast<const uint8_t*>(this) - TRANSFORM_MANAGER_DATA_OFFSET);
337   }
338
339   /**
340    * Create an TransformManagerVector3Input
341    */
342   TransformManagerVector3Input(TransformManagerProperty property, const Vector3& initialValue)
343   : mProperty(property),
344     mValue(initialValue)
345   {
346   }
347
348   /**
349    * Virtual destructor.
350    */
351   ~TransformManagerVector3Input() override = default;
352
353   /**
354    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
355    */
356   Dali::Property::Type GetType() const override
357   {
358     return Dali::PropertyTypes::Get<Vector3>();
359   }
360
361   /**
362    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
363    */
364   virtual bool IsClean() const
365   {
366     return false;
367   }
368
369   /**
370    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
371    */
372   bool InputInitialized() const override
373   {
374     return true;
375   }
376
377   /**
378    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
379    * @note A constraint can only receive the inherited property from the previous frame.
380    */
381   bool InputChanged() const override
382   {
383     return true;
384   }
385
386   /**
387    * Helper function to get the transform components out of the world matrix.
388    * It stores the value in the mValue member variable
389    */
390   void ComputeTransformComponent() const
391   {
392     auto transformManagerData = GetTxManagerData();
393     auto id                   = transformManagerData->Id();
394     if(id != INVALID_TRANSFORM_ID)
395     {
396       const Matrix& worldMatrix = transformManagerData->Manager()->GetWorldMatrix(id);
397       if(mProperty == TRANSFORM_PROPERTY_WORLD_POSITION)
398       {
399         mValue = worldMatrix.GetTranslation3();
400       }
401       else if(mProperty == TRANSFORM_PROPERTY_WORLD_SCALE)
402       {
403         Vector3    position;
404         Quaternion orientation;
405         worldMatrix.GetTransformComponents(position, orientation, mValue);
406       }
407     }
408   }
409
410   /**
411    * @copydoc Dali::PropertyInput::GetVector3()
412    */
413   const Vector3& GetVector3(BufferIndex bufferIndex) const override
414   {
415     ComputeTransformComponent();
416     return mValue;
417   }
418
419   /**
420    * @copydoc Dali::PropertyInput::GetConstraintInputVector3()
421    */
422   const Vector3& GetConstraintInputVector3(BufferIndex bufferIndex) const override
423   {
424     ComputeTransformComponent();
425     return mValue;
426   }
427
428   /**
429    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
430    */
431   Vector3& Get(BufferIndex bufferIndex)
432   {
433     ComputeTransformComponent();
434     return mValue;
435   }
436
437   /**
438    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
439    */
440   const Vector3& Get(BufferIndex bufferIndex) const
441   {
442     ComputeTransformComponent();
443     return mValue;
444   }
445
446   /**
447    * Retrieve the property value.
448    * @param[in] bufferIndex The buffer to read.
449    * @return The property value.
450    */
451   const Vector3& operator[](BufferIndex bufferIndex) const
452   {
453     ComputeTransformComponent();
454     return mValue;
455   }
456
457   /**
458    * @copydoc Dali::Internal::PropertyInputImpl::IsTransformManagerProperty()
459    */
460   bool IsTransformManagerProperty() const override
461   {
462     return true;
463   }
464
465 private:
466   TransformManagerVector3Input(const TransformManagerVector3Input& property)       = delete;
467   TransformManagerVector3Input& operator=(const TransformManagerVector3Input& rhs) = delete;
468
469 public:
470   TransformManagerProperty mProperty;
471   mutable Vector3          mValue;
472 };
473
474 /**
475  * A Quaternion property used as input.
476  */
477 template<size_t TxMgrDataOffset>
478 class TransformManagerQuaternionInput final : public PropertyInputImpl
479 {
480 public:
481   enum
482   {
483     TRANSFORM_MANAGER_DATA_OFFSET = TxMgrDataOffset
484   };
485   TransformManagerData* GetTxManagerData()
486   {
487     return reinterpret_cast<TransformManagerData*>(
488       reinterpret_cast<uint8_t*>(this) - TRANSFORM_MANAGER_DATA_OFFSET);
489   }
490   const TransformManagerData* GetTxManagerData() const
491   {
492     return reinterpret_cast<const TransformManagerData*>(
493       reinterpret_cast<const uint8_t*>(this) - TRANSFORM_MANAGER_DATA_OFFSET);
494   }
495
496   /**
497    * Constructor
498    */
499   TransformManagerQuaternionInput() = default;
500
501   /**
502    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
503    */
504   Dali::Property::Type GetType() const override
505   {
506     return Dali::PropertyTypes::Get<Quaternion>();
507   }
508
509   /**
510    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
511    */
512   virtual bool IsClean() const
513   {
514     return false;
515   }
516
517   /**
518    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
519    */
520   bool InputInitialized() const override
521   {
522     return true;
523   }
524
525   /**
526    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
527    * @note A constraint can only receive the inherited property from the previous frame.
528    */
529   bool InputChanged() const override
530   {
531     return true;
532   }
533
534   /**
535    * Helper function to get the orientation out of the world matrix.
536    * It stores the result in the mValue member variable
537    */
538   void ComputeTransformComponent() const
539   {
540     auto transformManagerData = GetTxManagerData();
541     auto id                   = transformManagerData->Id();
542     if(id != INVALID_TRANSFORM_ID)
543     {
544       const Matrix& worldMatrix = transformManagerData->Manager()->GetWorldMatrix(id);
545       Vector3       position, scale;
546       worldMatrix.GetTransformComponents(position, mValue, scale);
547     }
548   }
549
550   /**
551    * @copydoc Dali::PropertyInput::GetQuaternion()
552    */
553   const Quaternion& GetQuaternion(BufferIndex bufferIndex) const override
554   {
555     ComputeTransformComponent();
556     return mValue;
557   }
558
559   /**
560    * @copydoc Dali::PropertyInput::GetConstraintInputQuaternion()
561    */
562   const Quaternion& GetConstraintInputQuaternion(BufferIndex bufferIndex) const override
563   {
564     ComputeTransformComponent();
565     return mValue;
566   }
567
568   /**
569    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
570    */
571   Quaternion& Get(BufferIndex bufferIndex)
572   {
573     ComputeTransformComponent();
574     return mValue;
575   }
576
577   /**
578    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
579    */
580   const Quaternion& Get(BufferIndex bufferIndex) const
581   {
582     ComputeTransformComponent();
583     return mValue;
584   }
585
586   /**
587    * Retrieve the property value.
588    * @param[in] bufferIndex The buffer to read.
589    * @return The property value.
590    */
591   const Quaternion& operator[](BufferIndex bufferIndex) const
592   {
593     ComputeTransformComponent();
594     return mValue;
595   }
596
597   /**
598    * @copydoc Dali::Internal::PropertyInputImpl::IsTransformManagerProperty()
599    */
600   bool IsTransformManagerProperty() const override
601   {
602     return true;
603   }
604
605 private:
606   TransformManagerQuaternionInput(const TransformManagerQuaternionInput& property)       = delete;
607   TransformManagerQuaternionInput& operator=(const TransformManagerQuaternionInput& rhs) = delete;
608
609 public:
610   mutable Quaternion mValue;
611 };
612
613 /**
614  * A Matrix property used as input.
615  */
616 template<size_t TxMgrDataOffset>
617 class TransformManagerMatrixInput final : public PropertyInputImpl
618 {
619 public:
620   enum
621   {
622     TRANSFORM_MANAGER_DATA_OFFSET = TxMgrDataOffset
623   };
624   TransformManagerData* GetTxManagerData()
625   {
626     return reinterpret_cast<TransformManagerData*>(
627       reinterpret_cast<uint8_t*>(this) - TRANSFORM_MANAGER_DATA_OFFSET);
628   }
629   const TransformManagerData* GetTxManagerData() const
630   {
631     return reinterpret_cast<const TransformManagerData*>(
632       reinterpret_cast<const uint8_t*>(this) - TRANSFORM_MANAGER_DATA_OFFSET);
633   }
634
635   /**
636    * Constructor
637    */
638   TransformManagerMatrixInput() = default;
639
640   /**
641    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
642    */
643   Dali::Property::Type GetType() const override
644   {
645     return Dali::PropertyTypes::Get<Matrix>();
646   }
647
648   /**
649    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
650    */
651   virtual bool IsClean() const
652   {
653     return false;
654   }
655
656   /**
657    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
658    */
659   bool InputInitialized() const override
660   {
661     return true;
662   }
663
664   /**
665    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
666    * @note A constraint can only receive the inherited property from the previous frame.
667    */
668   bool InputChanged() const override
669   {
670     return true;
671   }
672
673   /**
674    * @copydoc Dali::PropertyInput::GetMatrix()
675    */
676   const Matrix& GetMatrix(BufferIndex bufferIndex) const override
677   {
678     auto transformManagerData = GetTxManagerData();
679     auto id                   = transformManagerData->Id();
680     if(id != INVALID_TRANSFORM_ID)
681     {
682       return transformManagerData->Manager()->GetWorldMatrix(id);
683     }
684     return Matrix::IDENTITY;
685   }
686
687   /**
688    * @copydoc Dali::PropertyInput::GetConstraintInputMatrix()
689    */
690   const Matrix& GetConstraintInputMatrix(BufferIndex bufferIndex) const override
691   {
692     auto transformManagerData = GetTxManagerData();
693     auto id                   = transformManagerData->Id();
694     if(id != INVALID_TRANSFORM_ID)
695     {
696       return transformManagerData->Manager()->GetWorldMatrix(id);
697     }
698     return Matrix::IDENTITY;
699   }
700
701   /**
702    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
703    */
704   Matrix& Get(BufferIndex bufferIndex)
705   {
706     auto transformManagerData = GetTxManagerData();
707     auto id                   = transformManagerData->Id();
708     DALI_ASSERT_ALWAYS(id != INVALID_TRANSFORM_ID);
709     return transformManagerData->Manager()->GetWorldMatrix(id);
710   }
711
712   /**
713    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
714    */
715   const Matrix& Get(BufferIndex bufferIndex) const
716   {
717     return GetMatrix(bufferIndex);
718   }
719
720   /**
721    * Retrieve the property value.
722    * @param[in] bufferIndex The buffer to read.
723    * @return The property value.
724    */
725   const Matrix& operator[](BufferIndex bufferIndex) const
726   {
727     return GetMatrix(bufferIndex);
728   }
729
730   /**
731    * @copydoc Dali::Internal::PropertyInputImpl::IsTransformManagerProperty()
732    */
733   bool IsTransformManagerProperty() const override
734   {
735     return true;
736   }
737
738 private:
739   TransformManagerMatrixInput(const TransformManagerMatrixInput& property)       = delete;
740   TransformManagerMatrixInput& operator=(const TransformManagerMatrixInput& rhs) = delete;
741 };
742
743 } // namespace SceneGraph
744 } // namespace Internal
745 } // namespace Dali
746
747 #endif // TRANSFORM_MANAGER_PROPERTY_H_