[dali_2.3.28] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-input-impl.h
1 #ifndef DALI_INTERNAL_PROPERTY_INPUT_IMPL_H
2 #define DALI_INTERNAL_PROPERTY_INPUT_IMPL_H
3
4 /*
5  * Copyright (c) 2023 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 // EXTERNAL INCLUDES
22 #include <iostream>
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/common/buffer-index.h>
26 #include <dali/public-api/math/matrix.h>
27 #include <dali/public-api/math/matrix3.h>
28 #include <dali/public-api/math/quaternion.h>
29 #include <dali/public-api/math/vector2.h>
30 #include <dali/public-api/math/vector3.h>
31 #include <dali/public-api/math/vector4.h>
32 #include <dali/public-api/object/property-input.h>
33
34 #if defined(ANDROID) || defined(WIN32) || defined(__APPLE__)
35 namespace std
36 {
37 uint64_t _Hash_bytes(const void* bytes, uint64_t size, uint64_t seed);
38
39 }
40 #endif
41
42 namespace Dali
43 {
44 namespace Internal
45 {
46 /**
47  * An abstract interface for receiving property values, and for querying whether
48  * a property value has changed i.e. whether a constraint needs to be reapplied.
49  */
50 class PropertyInputImpl
51 {
52 public:
53   /**
54    * Virtual destructor.
55    */
56   virtual ~PropertyInputImpl() = default;
57
58   /**
59    * Query the type of property input.
60    * @return The property type.
61    */
62   virtual Property::Type GetType() const = 0;
63
64   /**
65    * Query the input value (for a constraint) has been initialized.
66    * @return True if initialized, otherwise the constraint should be skipped.
67    */
68   virtual bool InputInitialized() const = 0;
69
70   /**
71    * Query the input value (for a constraint) has changed.
72    * @return True if the input value has changed.
73    */
74   virtual bool InputChanged() const = 0;
75
76   /**
77    * Retrieve a boolean value.
78    * @pre GetType() returns Property::BOOLEAN.
79    * @param[in] bufferIndex The buffer to read from.
80    * @return The boolean value.
81    */
82   virtual const bool& GetBoolean(BufferIndex bufferIndex) const
83   {
84     // the return will never be executed, it's just to keep the compiler happy
85     return reinterpret_cast<const bool&>(*this);
86   }
87
88   /**
89    * Retrieve an integer value.
90    * @pre GetType() returns Property::INTEGER.
91    * @param[in] bufferIndex The buffer to read from.
92    * @return The integer value.
93    */
94   virtual const int& GetInteger(BufferIndex bufferIndex) const
95   {
96     // the return will never be executed, it's just to keep the compiler happy
97     return reinterpret_cast<const int&>(*this);
98   }
99
100   /**
101    * Retrieve a float value.
102    * @pre GetType() returns Property::FLOAT.
103    * @param[in] bufferIndex The buffer to read from.
104    * @return The float value.
105    */
106   virtual const float& GetFloat(BufferIndex bufferIndex) const
107   {
108     // the return will never be executed, it's just to keep the compiler happy
109     return reinterpret_cast<const float&>(*this);
110   }
111
112   /**
113    * Retrieve a Vector2 value.
114    * @pre GetType() returns Property::VECTOR2.
115    * @param[in] bufferIndex The buffer to read from.
116    * @return The Vector2 value.
117    */
118   virtual const Vector2& GetVector2(BufferIndex bufferIndex) const
119   {
120     // the return will never be executed, it's just to keep the compiler happy
121     return reinterpret_cast<const Vector2&>(*this);
122   }
123
124   /**
125    * Retrieve a Vector3 value.
126    * @pre GetType() returns Property::VECTOR3.
127    * @param[in] bufferIndex The buffer to read from.
128    * @return The Vector3 value.
129    */
130   virtual const Vector3& GetVector3(BufferIndex bufferIndex) const
131   {
132     // the return will never be executed, it's just to keep the compiler happy
133     return reinterpret_cast<const Vector3&>(*this);
134   }
135
136   /**
137    * Retrieve a Vector4 value.
138    * @pre GetType() returns Property::VECTOR4.
139    * @param[in] bufferIndex The buffer to read from.
140    * @return The Vector4 value.
141    */
142   virtual const Vector4& GetVector4(BufferIndex bufferIndex) const
143   {
144     // the return will never be executed, it's just to keep the compiler happy
145     return reinterpret_cast<const Vector4&>(*this);
146   }
147
148   /**
149    * Retrieve a Quaternion value.
150    * @pre GetType() returns Property::Quaternion.
151    * @param[in] bufferIndex The buffer to read from.
152    * @return The Quaternion value.
153    */
154   virtual const Quaternion& GetQuaternion(BufferIndex bufferIndex) const
155   {
156     // the return will never be executed, it's just to keep the compiler happy
157     return reinterpret_cast<const Quaternion&>(*this);
158   }
159
160   /**
161    * Retrieve a Matrix value.
162    * @pre GetType() returns Property::Matrix.
163    * @param[in] bufferIndex The buffer to read from.
164    * @return The Matrix value.
165    */
166   virtual const Matrix3& GetMatrix3(BufferIndex bufferIndex) const
167   {
168     // the return will never be executed, it's just to keep the compiler happy
169     return reinterpret_cast<const Matrix3&>(*this);
170   }
171
172   /**
173    * Retrieve a Matrix value.
174    * @pre GetType() returns Property::Matrix.
175    * @param[in] bufferIndex The buffer to read from.
176    * @return The Matrix value.
177    */
178   virtual const Matrix& GetMatrix(BufferIndex bufferIndex) const
179   {
180     // the return will never be executed, it's just to keep the compiler happy
181     return reinterpret_cast<const Matrix&>(*this);
182   }
183
184   /**
185    * Retrieve the address of the property value. Only for use
186    * when writing uniforms.
187    */
188   virtual const void* GetValueAddress(BufferIndex bufferIndex) const = 0;
189
190   /**
191    * Retrieve the size of the property value for use in copying.
192    * Only for use when writing uniforms.
193    */
194   virtual size_t GetValueSize() const = 0;
195
196   // Accessors for Constraint functions
197
198   /**
199    * Retrieve a boolean input for a constraint function.
200    * @note For inherited properties, this method should be overriden to return the value
201    * from the previous frame i.e. not from the current update buffer.
202    * @pre GetType() returns Property::BOOLEAN.
203    * @param[in] updateBufferIndex The current update buffer index.
204    * @return The boolean value.
205    */
206   virtual const bool& GetConstraintInputBoolean(BufferIndex updateBufferIndex) const
207   {
208     return GetBoolean(updateBufferIndex);
209   }
210
211   /**
212    * Retrieve an integer input for a constraint function.
213    * @note For inherited properties, this method should be overriden to return the value
214    * from the previous frame i.e. not from the current update buffer.
215    * @pre GetType() returns Property::INTEGER.
216    * @param[in] updateBufferIndex The current update buffer index.
217    * @return The integer value.
218    */
219   virtual const int& GetConstraintInputInteger(BufferIndex updateBufferIndex) const
220   {
221     return GetInteger(updateBufferIndex);
222   }
223
224   /**
225    * Retrieve a float input for a constraint function.
226    * @note For inherited properties, this method should be overriden to return the value
227    * from the previous frame i.e. not from the current update buffer.
228    * @pre GetType() returns Property::FLOAT.
229    * @param[in] updateBufferIndex The current update buffer index.
230    * @return The float value.
231    */
232   virtual const float& GetConstraintInputFloat(BufferIndex updateBufferIndex) const
233   {
234     return GetFloat(updateBufferIndex);
235   }
236
237   /**
238    * Retrieve a Vector2 input for a constraint function.
239    * @note For inherited properties, this method should be overriden to return the value
240    * from the previous frame i.e. not from the current update buffer.
241    * @pre GetType() returns Property::VECTOR2.
242    * @param[in] updateBufferIndex The buffer to read from.
243    * @return The Vector2 value.
244    */
245   virtual const Vector2& GetConstraintInputVector2(BufferIndex updateBufferIndex) const
246   {
247     return GetVector2(updateBufferIndex);
248   }
249
250   /**
251    * Retrieve a Vector3 input for a constraint function.
252    * @note For inherited properties, this method should be overriden to return the value
253    * from the previous frame i.e. not from the current update buffer.
254    * @pre GetType() returns Property::VECTOR3.
255    * @param[in] updateBufferIndex The buffer to read from.
256    * @return The Vector3 value.
257    */
258   virtual const Vector3& GetConstraintInputVector3(BufferIndex updateBufferIndex) const
259   {
260     return GetVector3(updateBufferIndex);
261   }
262
263   /**
264    * Retrieve a Vector4 input for a constraint function.
265    * @note For inherited properties, this method should be overriden to return the value
266    * from the previous frame i.e. not from the current update buffer.
267    * @pre GetType() returns Property::VECTOR4.
268    * @param[in] updateBufferIndex The buffer to read from.
269    * @return The Vector4 value.
270    */
271   virtual const Vector4& GetConstraintInputVector4(BufferIndex updateBufferIndex) const
272   {
273     return GetVector4(updateBufferIndex);
274   }
275
276   /**
277    * Retrieve a Quaternion input for a constraint function.
278    * @note For inherited properties, this method should be overriden to return the value
279    * from the previous frame i.e. not from the current update buffer.
280    * @pre GetType() returns Property::Quaternion.
281    * @param[in] updateBufferIndex The buffer to read from.
282    * @return The Quaternion value.
283    */
284   virtual const Quaternion& GetConstraintInputQuaternion(BufferIndex updateBufferIndex) const
285   {
286     return GetQuaternion(updateBufferIndex);
287   }
288
289   /**
290    * Retrieve a Matrix3 input for a constraint function.
291    * @note For inherited properties, this method should be overriden to return the value
292    * from the previous frame i.e. not from the current update buffer.
293    * @pre GetType() returns Property::Matrix.
294    * @param[in] updateBufferIndex The buffer to read from.
295    * @return The Matrix value.
296    */
297   virtual const Matrix3& GetConstraintInputMatrix3(BufferIndex updateBufferIndex) const
298   {
299     return GetMatrix3(updateBufferIndex);
300   }
301
302   /**
303    * Retrieve a Matrix input for a constraint function.
304    * @note For inherited properties, this method should be overriden to return the value
305    * from the previous frame i.e. not from the current update buffer.
306    * @pre GetType() returns Property::Matrix.
307    * @param[in] updateBufferIndex The buffer to read from.
308    * @return The Matrix value.
309    */
310   virtual const Matrix& GetConstraintInputMatrix(BufferIndex updateBufferIndex) const
311   {
312     return GetMatrix(updateBufferIndex);
313   }
314
315   /**
316    * Query whether the property belongs to the
317    * transform manager or not.
318    * @return True if it is a transform manager property, false otherwise
319    */
320   virtual bool IsTransformManagerProperty() const
321   {
322     return false;
323   }
324
325   std::uint64_t Hash(BufferIndex bufferIndex, uint64_t seed) const
326   {
327     switch(GetType())
328     {
329       case Property::BOOLEAN:
330       {
331         return std::_Hash_bytes(&GetBoolean(bufferIndex), sizeof(bool), seed);
332       }
333
334       case Property::INTEGER:
335       {
336         return std::_Hash_bytes(&GetInteger(bufferIndex), sizeof(int), seed);
337       }
338
339       case Property::FLOAT:
340       {
341         return std::_Hash_bytes(&GetFloat(bufferIndex), sizeof(float), seed);
342       }
343
344       case Property::VECTOR2:
345       {
346         return std::_Hash_bytes(&GetVector2(bufferIndex), sizeof(Vector2), seed);
347       }
348
349       case Property::VECTOR3:
350       {
351         return std::_Hash_bytes(&GetVector3(bufferIndex), sizeof(Vector3), seed);
352       }
353
354       case Property::VECTOR4:
355       {
356         return std::_Hash_bytes(&GetVector4(bufferIndex), sizeof(Vector4), seed);
357       }
358
359       case Property::ROTATION:
360       {
361         return std::_Hash_bytes(&GetQuaternion(bufferIndex), sizeof(Quaternion), seed);
362       }
363
364       case Property::MATRIX:
365       {
366         return std::_Hash_bytes(&GetMatrix(bufferIndex), sizeof(Matrix), seed);
367       }
368
369       case Property::MATRIX3:
370       {
371         return std::_Hash_bytes(&GetMatrix3(bufferIndex), sizeof(Matrix3), seed);
372       }
373
374       default:
375         break; // print nothing
376     }
377
378     return seed;
379   }
380
381   /**
382    * Print the property value using a stream.
383    * @param[in] debugStream The output stream.
384    * @param[in] bufferIndex The buffer to read from.
385    * @todo Place this far-too-large-to-be-inlined function in a cpp and remove <iostream> header dependency from this file.
386    */
387   void DebugPrint(std::ostream& debugStream, BufferIndex bufferIndex) const
388   {
389     switch(GetType())
390     {
391       case Property::BOOLEAN:
392       {
393         debugStream << GetBoolean(bufferIndex);
394         break;
395       }
396
397       case Property::INTEGER:
398       {
399         debugStream << GetInteger(bufferIndex);
400         break;
401       }
402
403       case Property::FLOAT:
404       {
405         debugStream << GetFloat(bufferIndex);
406         break;
407       }
408
409       case Property::VECTOR2:
410       {
411         debugStream << GetVector2(bufferIndex);
412         break;
413       }
414
415       case Property::VECTOR3:
416       {
417         debugStream << GetVector3(bufferIndex);
418         break;
419       }
420
421       case Property::VECTOR4:
422       {
423         debugStream << GetVector4(bufferIndex);
424         break;
425       }
426
427       case Property::ROTATION:
428       {
429         debugStream << GetQuaternion(bufferIndex);
430         break;
431       }
432
433       case Property::MATRIX:
434       {
435         debugStream << GetMatrix(bufferIndex);
436         break;
437       }
438
439       case Property::MATRIX3:
440       {
441         debugStream << GetMatrix3(bufferIndex);
442         break;
443       }
444
445       default:
446         break; // print nothing
447     }
448   }
449 };
450
451 } // namespace Internal
452
453 } // namespace Dali
454
455 #endif // DALI_INTERNAL_PROPERTY_INPUT_IMPL_H