[Tizen] Implement partial update
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / transform-manager.h
1 #ifndef DALI_INTERNAL_TRANSFORM_MANAGER_H
2 #define DALI_INTERNAL_TRANSFORM_MANAGER_H
3
4 /*
5  * Copyright (c) 2018 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/public-api/common/dali-vector.h>
23 #include <dali/public-api/math/matrix.h>
24 #include <dali/public-api/math/quaternion.h>
25 #include <dali/public-api/math/vector3.h>
26 #include <dali/public-api/common/constants.h>
27 #include <dali/internal/update/manager/free-list.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace SceneGraph
36 {
37
38 /**
39  * Struct to store the animatable part of a component (Scale, orientation and position)
40  */
41 struct TransformComponentAnimatable
42 {
43   TransformComponentAnimatable()
44   : mScale( Vector3::ONE ),
45     mOrientation( Quaternion::IDENTITY ),
46     mPosition( Vector3::ZERO )
47   {
48   }
49
50   Vector3 mScale;
51   Quaternion mOrientation;
52   Vector3 mPosition;
53 };
54
55 /**
56  * Struct to store the non-animatable part of a component (AnchorPoint and ParentOrigin)
57  */
58 struct TransformComponentStatic
59 {
60   TransformComponentStatic()
61   : mAnchorPoint( AnchorPoint::DEFAULT ),
62     mParentOrigin( ParentOrigin::DEFAULT ),
63     mPositionUsesAnchorPoint( true )
64   {
65   }
66
67   Vector3 mAnchorPoint;
68   Vector3 mParentOrigin;
69   bool mPositionUsesAnchorPoint;
70 };
71
72 enum InheritanceMode
73 {
74   DONT_INHERIT_TRANSFORM  = 0,
75   INHERIT_POSITION        = 1,
76   INHERIT_SCALE           = 2,
77   INHERIT_ORIENTATION     = 4,
78   INHERIT_ALL             = INHERIT_POSITION | INHERIT_SCALE | INHERIT_ORIENTATION,
79 };
80
81 enum TransformManagerProperty
82 {
83   TRANSFORM_PROPERTY_POSITION = 0,
84   TRANSFORM_PROPERTY_SCALE,
85   TRANSFORM_PROPERTY_ANCHOR_POINT,
86   TRANSFORM_PROPERTY_PARENT_ORIGIN,
87   TRANSFORM_PROPERTY_SIZE,
88   TRANSFORM_PROPERTY_WORLD_POSITION,
89   TRANSFORM_PROPERTY_WORLD_SCALE,
90   TRANSFORM_PROPERTY_WORLD_ORIENTATION,
91   TRANSFORM_PROPERTY_WORLD_MATRIX,
92   TRANSFORM_PROPERTY_UPDATE_SIZE_HINT,
93   TRANSFORM_PROPERTY_COUNT,
94 };
95
96 typedef uint32_t TransformId; // 4,294,967,295 transforms supported
97 static const TransformId INVALID_TRANSFORM_ID = -1;
98
99 } //SceneGraph
100 } //Internal
101
102 // Allow TransformComponentAnimatable to be treated as a POD type
103 template <> struct TypeTraits< Dali::Internal::SceneGraph::TransformComponentAnimatable >: public Dali::BasicTypes< Dali::Internal::SceneGraph::TransformComponentAnimatable > { enum { IS_TRIVIAL_TYPE = true }; };
104
105 namespace Internal
106 {
107
108 namespace SceneGraph
109 {
110
111 /**
112  * Transform manager computes the local to world transformations
113  * of all the nodes in the scene. All the transformation data is stored contiguously
114  * in memory which minimizes cache misses during updates
115  */
116 class TransformManager
117 {
118 public:
119
120   /**
121    * Default constructor
122    */
123   TransformManager();
124
125   /**
126    * Class destructor
127    */
128   ~TransformManager();
129
130   /**
131    * Add a new transform component to the manager
132    * @return A TransformId used to access the component
133    */
134   TransformId CreateTransform();
135
136   /**
137    * Removes an existing transform component
138    * @param[in] id Id of the transform to remove
139    */
140   void RemoveTransform(TransformId id);
141
142   /**
143    * Sets the parent transform of an existing component
144    * @param[in] id Id of the transform
145    * @param[in] parentId Id of the new parent
146    */
147   void SetParent( TransformId id, TransformId parentId );
148
149   /**
150    * Gets the world transform matrix of an exisiting transform component
151    * @param[in] id Id of the transform component
152    * @return The local to world transformation matrix of the component
153    */
154   const Matrix& GetWorldMatrix( TransformId id ) const;
155
156   /**
157    * Gets the world transform matrix of an exisiting transform component
158    * @param[in] id Id of the transform component
159    * @return The local to world transformation matrix of the component
160    */
161   Matrix& GetWorldMatrix( TransformId id );
162
163   /**
164    * Checks if the local transform was updated in the last Update
165    * @param[in] id Id of the transform
166    * @return true if local matrix changed in the last update, false otherwise
167    */
168   bool IsLocalMatrixDirty( TransformId id ) const
169   {
170     return mLocalMatrixDirty[mIds[id]];
171   }
172
173   /**
174    * Sets position inheritance mode.
175    * @param[in] id Id of the transform
176    * @param[in] inherit True if position is inherited from parent, false otherwise
177    */
178   void SetInheritPosition( TransformId id, bool inherit );
179
180   /**
181    * Sets scale inheritance mode.
182    * @param[in] id Id of the transform
183    * @param[in] inherit True if scale is inherited from parent, false otherwise
184    */
185   void SetInheritScale( TransformId id, bool inherit );
186
187   /**
188    * Sets orientation inheritance mode.
189    * @param[in] id Id of the transform
190    * @param[in] inherit True if orientation is inherited from parent, false otherwise
191    */
192   void SetInheritOrientation( TransformId id, bool inherit );
193
194   /**
195    * Recomputes all world transform matrices
196    */
197   void Update();
198
199   /**
200    * Resets all the animatable properties to its base value
201    */
202   void ResetToBaseValue();
203
204   /**
205    * Get the value of a Vector3 property
206    * @param[in] id Id of the transform component
207    * @param[in] property The property
208    */
209   Vector3& GetVector3PropertyValue( TransformId id, TransformManagerProperty property );
210
211   /**
212    * Get the value of a Vector3 property
213    * @param[in] id Id of the transform component
214    * @param[in] property The property
215    */
216   const Vector3& GetVector3PropertyValue( TransformId id, TransformManagerProperty property ) const;
217
218   /**
219    * Get the value of a component of Vector3 property
220    * @param[in] id Id of the transform component
221    * @param[in] property The property
222    * param[in] component The component (0,1,2)
223    */
224   const float& GetVector3PropertyComponentValue( TransformId id, TransformManagerProperty property, uint32_t component ) const;
225
226   /**
227    * Set the value of a Vector3 property
228    * @param[in] id Id of the transform component
229    * @param[in] property The property
230    * @param[in] value The new value
231    */
232   void SetVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
233
234   /**
235    * Set the value of a component of a Vector3 property
236    * @param[in] id Id of the transform component
237    * @param[in] property The property
238    * @param[in] value The new value
239    * param[in] component The component (0,1,2)
240    */
241   void SetVector3PropertyComponentValue( TransformId id, TransformManagerProperty property, float value, uint32_t component );
242
243   /**
244    * Bakes the value of a Vector3 property
245    * @param[in] id Id of the transform component
246    * @param[in] property The property
247    * @param[in] value The new value
248    */
249   void BakeVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
250
251   /**
252    * Bakes the value of a Vector3 property relative to the current value
253    * @param[in] id Id of the transform component
254    * @param[in] property The property
255    * @param[in] value The new value
256    */
257   void BakeRelativeVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
258
259   /**
260    * Bakes the value of a Vector3 property by multiplying the current value and the new value
261    * @param[in] id Id of the transform component
262    * @param[in] property The property
263    * @param[in] value The new value
264    */
265   void BakeMultiplyVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
266
267   /**
268    * Bakes the value of a component of Vector3 property
269    * @param[in] id Id of the transform component
270    * @param[in] property The property
271    * @param[in] value The new value
272    */
273   void BakeVector3PropertyComponentValue( TransformId id, TransformManagerProperty property, float value, uint32_t component );
274
275   /**
276    * Bakes the value of the x component of Vector3 property
277    * @param[in] id Id of the transform component
278    * @param[in] property The property
279    * @param[in] value The new value
280    */
281   void BakeXVector3PropertyValue( TransformId id, TransformManagerProperty property, float value );
282
283   /**
284    * Bakes the value of the y component of Vector3 property
285    * @param[in] id Id of the transform component
286    * @param[in] property The property
287    * @param[in] value The new value
288    */
289   void BakeYVector3PropertyValue( TransformId id, TransformManagerProperty property, float value );
290
291   /**
292    * Bakes the value of the z component of Vector3 property
293    * @param[in] id Id of the transform component
294    * @param[in] property The property
295    * @param[in] value The new value
296    */
297   void BakeZVector3PropertyValue( TransformId id, TransformManagerProperty property, float value );
298
299   /**
300    * Get the value of a quaternion property
301    * @param[in] id Id of the transform component
302    */
303   Quaternion& GetQuaternionPropertyValue( TransformId id );
304
305   /**
306    * Get the value of a quaternion property
307    * @param[in] id Id of the transform component
308    */
309   const Quaternion& GetQuaternionPropertyValue( TransformId id ) const;
310
311   /**
312    * Set the value of a quaternion property
313    * @param[in] id Id of the transform component
314    * @param[in] q The new value
315    */
316   void SetQuaternionPropertyValue( TransformId id, const Quaternion& q );
317
318   /**
319    * Bake the value of a quaternion property
320    * @param[in] id Id of the transform component
321    * @param[in] q The new value
322    */
323   void BakeQuaternionPropertyValue( TransformId id, const Quaternion& q );
324
325   /**
326    * Bake the value of a quaternion property relative to its current value
327    * @param[in] id Id of the transform component
328    * @param[in] q The new value
329    */
330   void BakeRelativeQuaternionPropertyValue( TransformId id, const Quaternion& q );
331
332   /**
333    * Get the bounding sphere, in world coordinates, of a given component
334    * @param[in] id Id of the transform component
335    * @return The world space bounding sphere of the component
336    */
337   const Vector4& GetBoundingSphere( TransformId id ) const;
338
339   /**
340    * Component of node is changed or not
341    * @param[in] id Id of the transform component
342    * @return true if Component box is changed else false.
343    */
344   bool IsComponentChanged( TransformId id );
345
346   /**
347    * Get the world matrix and size of a given component
348    * @param[in] id Id of the transform component
349    * @param[out] The world matrix of the component
350    * @param[out] size size of the component
351    */
352   void GetWorldMatrixAndSize( TransformId id, Matrix& worldMatrix, Vector3& size ) const;
353
354   /**
355    * Get the update size hint,
356    * @param[in] id Id of the transform component
357    * @return The update size hint of the component
358    */
359   const Vector3& GetUpdateSizeHint( TransformId id ) const;
360
361
362   /**
363    * @brief Sets the boolean which states whether the position should use the anchor-point on the given transform component.
364    * @param[in] id Id of the transform component
365    * @param[in] value True if the position should use the anchor-point
366    */
367   void SetPositionUsesAnchorPoint( TransformId id, bool value );
368
369 private:
370
371   //Helper struct to order components
372   struct SOrderItem
373   {
374     bool operator<(const SOrderItem& item) const {return level < item.level;}
375
376     TransformId  id;
377     uint32_t level;
378   };
379
380   /**
381    * Swaps two components in the vectors
382    * @param[in] i Index of a component
383    * @param[in] j Index of a component
384    */
385   void SwapComponents( uint32_t i, uint32_t j );
386
387   /**
388    * Reorders components in hierarchical order so update can iterate sequentially
389    * updating the world transforms
390    */
391   void ReorderComponents();
392
393   uint32_t mComponentCount;                                               ///< Total number of components
394   FreeList mIds;                                                          ///< FreeList of Ids
395   Vector< TransformComponentAnimatable > mTxComponentAnimatable;          ///< Animatable part of the components
396   Vector< TransformComponentStatic > mTxComponentStatic;                  ///< Static part of the components
397   Vector< uint32_t > mInheritanceMode;                                    ///< Inheritance mode of the components
398   Vector< TransformId > mComponentId;                                     ///< Ids of the components
399   Vector< Vector3 > mSize;                                                ///< Size of the components
400   Vector< TransformId > mParent;                                          ///< Parent of the components
401   Vector< Matrix > mWorld;                                                ///< Local to world transform of the components
402   Vector< Matrix > mPrevWorld;                                            ///< Local to world transform of the components in last frame
403   Vector< Matrix > mLocal;                                                ///< Local to parent space transform of the components
404   Vector< Vector4 > mBoundingSpheres;                                     ///< Bounding spheres. xyz is the center and w is the radius
405   Vector< TransformComponentAnimatable > mTxComponentAnimatableBaseValue; ///< Base values for the animatable part of the components
406   Vector< Vector3 > mSizeBase;                                            ///< Base value for the size of the components
407   Vector< bool > mComponentDirty;                                         ///< 1u if some of the parts of the component has changed in this frame, 0 otherwise
408   Vector< bool > mLocalMatrixDirty;                                       ///< 1u if the local matrix has been updated in this frame, 0 otherwise
409   Vector< SOrderItem > mOrderedComponents;                                ///< Used to reorder components when hierarchy changes
410   Vector< bool> mComponentChanged;                                        ///< Component is changed or not
411   Vector< Vector3 > mUpdateSizeHint;                                      ///< BoundingBox of the components
412   Vector< Vector3 > mUpdateSizeHintBase;                                     ///< Base value for the boundingBox of the components
413   bool mReorder;                                                          ///< Flag to determine if the components have to reordered in the next Update
414 };
415
416 } //namespace SceneGraph
417
418 } //namespace Internal
419
420
421 } //namespace Dali
422
423
424 #endif // DALI_INTERNAL_TRANSFORM_MANAGER_H