(Partial Update) Change damaged rect calcutation
[platform/core/uifw/dali-core.git] / dali / internal / update / common / inherited-property.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H
2 #define DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_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/internal/event/common/property-input-impl.h>
23 #include <dali/internal/update/common/double-buffered.h>
24 #include <dali/internal/update/common/property-base.h>
25 #include <dali/internal/update/common/scene-graph-buffers.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/math/math-utils.h> // Clamp
28 #include <dali/public-api/object/property-input.h>
29 #include <dali/public-api/object/property-types.h>
30 #include <dali/public-api/object/property.h>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace SceneGraph
37 {
38 /**
39  * An inherited Vector3 property.
40  */
41 class InheritedVector3 : public PropertyInputImpl
42 {
43 public:
44   /**
45    * Create an inherited Vector3.
46    */
47   InheritedVector3()
48   : mValue(),
49     mInheritedFlag(false),
50     mReinheritedFlag(true)
51   {
52   }
53
54   /**
55    * Create an inherited Vector3.
56    * @param [in] initialValue The initial value of the property.
57    */
58   InheritedVector3(const Vector3& initialValue)
59   : mValue(initialValue),
60     mInheritedFlag(false),
61     mReinheritedFlag(true)
62   {
63   }
64   /**
65    * Virtual destructor.
66    */
67   ~InheritedVector3() override = default;
68
69   /**
70    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
71    */
72   Dali::Property::Type GetType() const override
73   {
74     return Dali::PropertyTypes::Get<Vector3>();
75   }
76
77   /**
78    * Called once per Update (only) if the property did not need to be re-inherited.
79    * @param[in] updateBufferIndex The current update buffer index.
80    */
81   void CopyPrevious(BufferIndex updateBufferIndex)
82   {
83     if(mReinheritedFlag)
84     {
85       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
86
87       mReinheritedFlag = false;
88     }
89   }
90
91   /**
92    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
93    */
94   virtual bool IsClean() const
95   {
96     return (false == mReinheritedFlag);
97   }
98
99   /**
100    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
101    */
102   bool InputInitialized() const override
103   {
104     // A constraint cannot use the property until it has been inherited (at least once).
105     return mInheritedFlag;
106   }
107
108   /**
109    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
110    * @note A constraint can only receive the inherited property from the previous frame.
111    */
112   bool InputChanged() const override
113   {
114     return !IsClean();
115   }
116
117   /**
118    * @copydoc Dali::PropertyInput::GetVector3()
119    */
120   const Vector3& GetVector3(BufferIndex bufferIndex) const override
121   {
122     return mValue[bufferIndex];
123   }
124
125   /**
126    * @copydoc Dali::PropertyInput::GetConstraintInputVector3()
127    */
128   const Vector3& GetConstraintInputVector3(BufferIndex bufferIndex) const override
129   {
130     // For inherited properties, constraints work with the value from the previous frame.
131     // This is because constraints are applied to position etc, before world-position is calculated.
132     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
133
134     return mValue[eventBufferIndex];
135   }
136
137   /**
138    * Set the property value. This will only persist for the current frame; the property
139    * will be reset with the base value, at the beginning of the next frame.
140    * @param[in] bufferIndex The buffer to write.
141    * @param[in] value The new property value.
142    */
143   void Set(BufferIndex bufferIndex, const Vector3& value)
144   {
145     mValue[bufferIndex] = value;
146
147     // The value has been inherited for the first time
148     mInheritedFlag = true;
149
150     mReinheritedFlag = true;
151   }
152
153   /**
154    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
155    */
156   Vector3& Get(BufferIndex bufferIndex)
157   {
158     return mValue[bufferIndex];
159   }
160
161   /**
162    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
163    */
164   const Vector3& Get(BufferIndex bufferIndex) const
165   {
166     return mValue[bufferIndex];
167   }
168
169   /**
170    * Retrieve the property value.
171    * @param[in] bufferIndex The buffer to read.
172    * @return The property value.
173    */
174   const Vector3& operator[](BufferIndex bufferIndex) const
175   {
176     return mValue[bufferIndex];
177   }
178
179 private:
180   // Undefined
181   InheritedVector3(const InheritedVector3& property);
182
183   // Undefined
184   InheritedVector3& operator=(const InheritedVector3& rhs);
185
186 private:
187   DoubleBuffered<Vector3> mValue; ///< The double-buffered property value
188
189   bool mInheritedFlag : 1;   ///< Flag whether the value has ever been inherited
190   bool mReinheritedFlag : 1; ///< Flag whether value was re-inherited in previous frame
191 };
192
193 /**
194  * An inherited Color property.
195  */
196 class InheritedColor : public PropertyInputImpl
197 {
198 public:
199   /**
200    * Create an inherited property.
201    * @param [in] initialValue The initial value of the property.
202    */
203   InheritedColor(const Vector4& initialValue)
204   : mValue(initialValue),
205     mInheritedFlag(false),
206     mReinheritedFlag(true)
207   {
208   }
209
210   /**
211    * Virtual destructor.
212    */
213   ~InheritedColor() override = default;
214
215   /**
216    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
217    */
218   Dali::Property::Type GetType() const override
219   {
220     return Dali::PropertyTypes::Get<Vector4>();
221   }
222
223   /**
224    * Called once per Update (only) if the property did not need to be re-inherited.
225    * @param[in] updateBufferIndex The current update buffer index.
226    */
227   void CopyPrevious(BufferIndex updateBufferIndex)
228   {
229     if(mReinheritedFlag)
230     {
231       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
232
233       mReinheritedFlag = false;
234     }
235   }
236
237   /**
238    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
239    */
240   virtual bool IsClean() const
241   {
242     return (false == mReinheritedFlag);
243   }
244
245   /**
246    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
247    */
248   bool InputInitialized() const override
249   {
250     // A constraint cannot use the property until it has been inherited (at least once).
251     return mInheritedFlag;
252   }
253
254   /**
255    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
256    * @note A constraint can only receive the inherited property from the previous frame.
257    */
258   bool InputChanged() const override
259   {
260     return !IsClean();
261   }
262
263   /**
264    * @copydoc Dali::PropertyInput::GetVector4()
265    */
266   const Vector4& GetVector4(BufferIndex bufferIndex) const override
267   {
268     return mValue[bufferIndex];
269   }
270
271   /**
272    * @copydoc Dali::PropertyInput::GetConstraintInputVector4()
273    */
274   const Vector4& GetConstraintInputVector4(BufferIndex bufferIndex) const override
275   {
276     // For inherited properties, constraints work with the value from the previous frame.
277     // This is because constraints are applied to position etc, before world-position is calculated.
278     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
279
280     return mValue[eventBufferIndex];
281   }
282
283   /**
284    * Set the property value. This will only persist for the current frame; the property
285    * will be reset with the base value, at the beginning of the next frame.
286    * @param[in] bufferIndex The buffer to write.
287    * @param[in] value The new property value.
288    */
289   void Set(BufferIndex bufferIndex, const Vector4& value)
290   {
291     mValue[bufferIndex] = Clamp(value, 0.0f, 1.0f); // color values are clamped between 0 and 1
292
293     // The value has been inherited for the first time
294     mInheritedFlag   = true;
295     mReinheritedFlag = true;
296   }
297
298   /**
299    * Set the property value. This will only persist for the current frame; the property
300    * will be reset with the base value, at the beginning of the next frame.
301    * @param[in] bufferIndex The buffer to write.
302    * @param[in] r The new red value.
303    * @param[in] g The new green value.
304    * @param[in] b The new blue value.
305    * @param[in] a The new alpha value.
306    */
307   void Set(BufferIndex bufferIndex, float r, float g, float b, float a)
308   {
309     mValue[bufferIndex].r = Clamp(r, 0.0f, 1.0f); // color values are clamped between 0 and 1
310     mValue[bufferIndex].g = Clamp(g, 0.0f, 1.0f); // color values are clamped between 0 and 1
311     mValue[bufferIndex].b = Clamp(b, 0.0f, 1.0f); // color values are clamped between 0 and 1
312     mValue[bufferIndex].a = Clamp(a, 0.0f, 1.0f); // color values are clamped between 0 and 1
313
314     // The value has been inherited for the first time
315     mInheritedFlag   = true;
316     mReinheritedFlag = true;
317   }
318
319   /**
320    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
321    */
322   Vector4& Get(BufferIndex bufferIndex)
323   {
324     return mValue[bufferIndex];
325   }
326
327   /**
328    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
329    */
330   const Vector4& Get(BufferIndex bufferIndex) const
331   {
332     return mValue[bufferIndex];
333   }
334
335   /**
336    * Retrieve the property value.
337    * @param[in] bufferIndex The buffer to read.
338    * @return The property value.
339    */
340   const Vector4& operator[](BufferIndex bufferIndex) const
341   {
342     return mValue[bufferIndex];
343   }
344
345 private:
346   // Undefined
347   InheritedColor(const InheritedColor& property);
348   // Undefined
349   InheritedColor& operator=(const InheritedColor& rhs);
350
351 private:
352   DoubleBuffered<Vector4> mValue; ///< The double-buffered property value
353
354   bool mInheritedFlag : 1;   ///< Flag whether the value has ever been inherited
355   bool mReinheritedFlag : 1; ///< Flag whether value was re-inherited in previous frame
356 };
357
358 /**
359  * An inherited Quaternion property.
360  */
361 class InheritedQuaternion : public PropertyInputImpl
362 {
363 public:
364   /**
365    * Create an inherited property.
366    */
367   InheritedQuaternion()
368   : mValue(),
369     mInheritedFlag(false),
370     mReinheritedFlag(true)
371   {
372   }
373
374   /**
375    * Virtual destructor.
376    */
377   ~InheritedQuaternion() override = default;
378
379   /**
380    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
381    */
382   Dali::Property::Type GetType() const override
383   {
384     return Dali::PropertyTypes::Get<Quaternion>();
385   }
386
387   /**
388    * Called once per Update (only) if the property did not need to be re-inherited.
389    * @param[in] updateBufferIndex The current update buffer index.
390    */
391   void CopyPrevious(BufferIndex updateBufferIndex)
392   {
393     if(mReinheritedFlag)
394     {
395       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
396
397       mReinheritedFlag = false;
398     }
399   }
400
401   /**
402    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
403    */
404   virtual bool IsClean() const
405   {
406     return (false == mReinheritedFlag);
407   }
408
409   /**
410    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
411    */
412   bool InputInitialized() const override
413   {
414     // A constraint cannot use the property until it has been inherited (at least once).
415     return mInheritedFlag;
416   }
417
418   /**
419    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
420    * @note A constraint can only receive the inherited property from the previous frame.
421    */
422   bool InputChanged() const override
423   {
424     return !IsClean();
425   }
426
427   /**
428    * @copydoc Dali::PropertyInput::GetQuaternion()
429    */
430   const Quaternion& GetQuaternion(BufferIndex bufferIndex) const override
431   {
432     return mValue[bufferIndex];
433   }
434
435   /**
436    * @copydoc Dali::PropertyInput::GetConstraintInputQuaternion()
437    */
438   const Quaternion& GetConstraintInputQuaternion(BufferIndex bufferIndex) const override
439   {
440     // For inherited properties, constraints work with the value from the previous frame.
441     // This is because constraints are applied to position etc, before world-position is calculated.
442     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
443
444     return mValue[eventBufferIndex];
445   }
446
447   /**
448    * Set the property value. This will only persist for the current frame; the property
449    * will be reset with the base value, at the beginning of the next frame.
450    * @param[in] bufferIndex The buffer to write.
451    * @param[in] value The new property value.
452    */
453   void Set(BufferIndex bufferIndex, const Quaternion& value)
454   {
455     mValue[bufferIndex] = value;
456
457     // The value has been inherited for the first time
458     mInheritedFlag = true;
459
460     mReinheritedFlag = true;
461   }
462
463   /**
464    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
465    */
466   Quaternion& Get(BufferIndex bufferIndex)
467   {
468     return mValue[bufferIndex];
469   }
470
471   /**
472    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
473    */
474   const Quaternion& Get(BufferIndex bufferIndex) const
475   {
476     return mValue[bufferIndex];
477   }
478
479   /**
480    * Retrieve the property value.
481    * @param[in] bufferIndex The buffer to read.
482    * @return The property value.
483    */
484   const Quaternion& operator[](BufferIndex bufferIndex) const
485   {
486     return mValue[bufferIndex];
487   }
488
489 private:
490   // Undefined
491   InheritedQuaternion(const InheritedQuaternion& property);
492
493   // Undefined
494   InheritedQuaternion& operator=(const InheritedQuaternion& rhs);
495
496 private:
497   DoubleBuffered<Quaternion> mValue; ///< The double-buffered property value
498
499   bool mInheritedFlag : 1;   ///< Flag whether the value has ever been inherited
500   bool mReinheritedFlag : 1; ///< Flag whether value was re-inherited in previous frame
501 };
502
503 /**
504  * An inherited Matrix property.
505  */
506 class InheritedMatrix : public PropertyInputImpl
507 {
508 public:
509   /**
510    * Create an inherited property.
511    */
512   InheritedMatrix()
513   : mValue(),
514     mInheritedFlag(false),
515     mReinheritedFlag(true)
516   {
517   }
518
519   /**
520    * Virtual destructor.
521    */
522   ~InheritedMatrix() override = default;
523
524   /**
525    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
526    */
527   Dali::Property::Type GetType() const override
528   {
529     return Dali::PropertyTypes::Get<Matrix>();
530   }
531
532   /**
533    * Called once per Update (only) if the property did not need to be re-inherited.
534    * @param[in] updateBufferIndex The current update buffer index.
535    */
536   void CopyPrevious(BufferIndex updateBufferIndex)
537   {
538     if(mReinheritedFlag)
539     {
540       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
541
542       mReinheritedFlag = false;
543     }
544   }
545
546   /**
547    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
548    */
549   virtual bool IsClean() const
550   {
551     return (false == mReinheritedFlag);
552   }
553
554   /**
555    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
556    */
557   bool InputInitialized() const override
558   {
559     // A constraint cannot use the property until it has been inherited (at least once).
560     return mInheritedFlag;
561   }
562
563   /**
564    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
565    * @note A constraint can only receive the inherited property from the previous frame.
566    */
567   bool InputChanged() const override
568   {
569     return !IsClean();
570   }
571
572   /**
573    * @copydoc Dali::Internal::PropertyInputImpl::GetMatrix()
574    */
575   const Matrix& GetMatrix(BufferIndex bufferIndex) const override
576   {
577     return mValue[bufferIndex];
578   }
579
580   /**
581    * @copydoc Dali::Internal::PropertyInputImpl::GetConstraintInputMatrix()
582    */
583   const Matrix& GetConstraintInputMatrix(BufferIndex bufferIndex) const override
584   {
585     // For inherited properties, constraints work with the value from the previous frame.
586     // This is because constraints are applied to position etc, before world-position is calculated.
587     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
588
589     return mValue[eventBufferIndex];
590   }
591
592   /**
593    * Set the property value. This will only persist for the current frame; the property
594    * will be reset with the base value, at the beginning of the next frame.
595    * @param[in] bufferIndex The buffer to write.
596    * @param[in] value The new property value.
597    */
598   void Set(BufferIndex bufferIndex, const Matrix& value)
599   {
600     mValue[bufferIndex] = value;
601
602     // The value has been inherited for the first time
603     mInheritedFlag = true;
604
605     mReinheritedFlag = true;
606   }
607
608   /**
609    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
610    */
611   Matrix& Get(BufferIndex bufferIndex)
612   {
613     return mValue[bufferIndex];
614   }
615
616   /**
617    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
618    */
619   const Matrix& Get(BufferIndex bufferIndex) const
620   {
621     return mValue[bufferIndex];
622   }
623
624   /**
625    * Retrieve the property value.
626    * @param[in] bufferIndex The buffer to read.
627    * @return The property value.
628    */
629   const Matrix& operator[](BufferIndex bufferIndex) const
630   {
631     return mValue[bufferIndex];
632   }
633
634   void SetDirty(BufferIndex bufferIndex)
635   {
636     mReinheritedFlag = true;
637
638     // The value has been inherited for the first time
639     mInheritedFlag = true;
640   }
641
642 private:
643   // Undefined
644   InheritedMatrix(const InheritedMatrix& property);
645
646   // Undefined
647   InheritedMatrix& operator=(const InheritedMatrix& rhs);
648
649 private:
650   DoubleBuffered<Matrix> mValue; ///< The double-buffered property value
651
652   bool mInheritedFlag : 1;   ///< Flag whether the value has ever been inherited
653   bool mReinheritedFlag : 1; ///< Flag whether value was re-inherited in previous frame
654 };
655
656 } // namespace SceneGraph
657
658 } // namespace Internal
659
660 } // namespace Dali
661
662 #endif // DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H