[Tizen] Add screen and client rotation itself function
[platform/core/uifw/dali-core.git] / dali / internal / update / render-tasks / scene-graph-camera.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_CAMERA_H
2 #define DALI_INTERNAL_SCENE_GRAPH_CAMERA_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/math/rect.h>
23 #include <dali/public-api/actors/camera-actor.h>
24 #include <dali/internal/common/message.h>
25 #include <dali/internal/event/common/event-thread-services.h>
26 #include <dali/internal/update/common/double-buffered.h>
27 #include <dali/internal/update/common/inherited-property.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 // value types used by messages
36 template <> struct ParameterType< Dali::Camera::Type >
37 : public BasicType< Dali::Camera::Type > {};
38 template <> struct ParameterType< Dali::Camera::ProjectionMode >
39 : public BasicType< Dali::Camera::ProjectionMode > {};
40
41 namespace SceneGraph
42 {
43
44 class Node;
45 class SceneController;
46
47 /**
48  * Scene-graph camera object
49  */
50 class Camera
51 {
52 public:
53   static const Dali::Camera::Type DEFAULT_TYPE;
54   static const Dali::Camera::ProjectionMode DEFAULT_MODE;
55   static const bool  DEFAULT_INVERT_Y_AXIS;
56   static const float DEFAULT_FIELD_OF_VIEW;
57   static const float DEFAULT_ASPECT_RATIO;
58   static const float DEFAULT_LEFT_CLIPPING_PLANE;
59   static const float DEFAULT_RIGHT_CLIPPING_PLANE;
60   static const float DEFAULT_TOP_CLIPPING_PLANE;
61   static const float DEFAULT_BOTTOM_CLIPPING_PLANE;
62   static const float DEFAULT_NEAR_CLIPPING_PLANE;
63   static const float DEFAULT_FAR_CLIPPING_PLANE;
64   static const Vector2 DEFAULT_STEREO_BIAS;
65   static const Vector3 DEFAULT_TARGET_POSITION;
66
67   /**
68    * Plane equation container for a plane of the view frustum
69    */
70   struct Plane
71   {
72     Vector3 mNormal;
73     float mDistance;
74   };
75
76   /**
77    * @brief Container for six planes in a view frustum
78    */
79   struct FrustumPlanes
80   {
81     Plane mPlanes[ 6 ];
82     Vector3 mSign[ 6 ];
83   };
84
85   /**
86    * Construct a new Camera.
87    * @return a new camera.
88    */
89   static Camera* New();
90
91   /**
92    * Destructor
93    */
94   ~Camera();
95
96   /**
97    * Set the node this scene graph camera belongs to.
98    * @param[in] node The owning node.
99    */
100   void SetNode( const Node* node );
101
102   /**
103    * @copydoc Dali::Internal::CameraActor::SetType
104    */
105   void SetType( Dali::Camera::Type type );
106
107   /**
108    * @copydoc Dali::Internal::CameraActor::SetInvertYAxis
109    */
110   void SetInvertYAxis( bool invertYAxis );
111
112   /**
113    * @copydoc Dali::Internal::CameraActor::SetProjectionMode
114    */
115   void SetProjectionMode( Dali::Camera::ProjectionMode projectionMode );
116
117   /**
118    * @copydoc Dali::Internal::CameraActor::SetFieldOfView
119    */
120   void SetFieldOfView( float fieldOfView );
121
122   /**
123    * @copydoc Dali::Internal::CameraActor::SetAspectRatio
124    */
125   void SetAspectRatio( float aspectRatio );
126
127    /**
128    * @copydoc Dali::Internal::CameraActor::SetLeftClippingPlane
129    */
130   void SetLeftClippingPlane( float leftClippingPlane );
131
132   /**
133    * @copydoc Dali::Internal::CameraActor::SetRightClippingPlane
134    */
135   void SetRightClippingPlane( float rightClippingPlane );
136
137   /**
138    * @copydoc Dali::Internal::CameraActor::SetTopClippingPlane
139    */
140   void SetTopClippingPlane( float topClippingPlane );
141
142   /**
143    * @copydoc Dali::Internal::CameraActor::SetBottomClippingPlane
144    */
145   void SetBottomClippingPlane( float bottomClippingPlane );
146
147   /**
148    * @copydoc Dali::Internal::CameraActor::SetNearClippingPlane
149    */
150   void SetNearClippingPlane( float nearClippingPlane );
151
152   /**
153    * @copydoc Dali::Internal::CameraActor::SetFarClippingPlane
154    */
155   void SetFarClippingPlane( float farClippingPlane );
156
157   /**
158    * @copydoc Dali::Internal::CameraActor::RotateProjection
159    */
160   void RotateProjection( int rotationAngle );
161
162   /**
163    * @copydoc Dali::Internal::CameraActor::SetTarget
164    */
165   void SetTargetPosition( const Vector3& targetPosition );
166
167   /**
168    * Sets the reflection plane
169    * @param[in] plane reflection plane
170    */
171   void SetReflectByPlane( const Vector4& plane );
172
173   /**
174    * Tests whether reflection is used
175    * @return True if used, False otherwise
176    */
177   bool GetReflectionUsed() const
178   {
179     return mUseReflection;
180   }
181
182   /**
183    * Retrieve the view-matrix; this is double buffered for input handling.
184    * @param[in] bufferIndex The buffer to read from.
185    * @return The view-matrix.
186    */
187   const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
188
189   /**
190    * @brief Check to see if a sphere lies within the view frustum.
191    *
192    * @param bufferIndex The buffer to read from.
193    * @param origin The world position center of the sphere to check.
194    * @param radius The length of the sphere radius in world scale.
195    *
196    * @return false if the sphere lies outside of the frustum.
197    */
198   bool CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius );
199
200   /**
201    * @brief Check to see if a bounding box lies within the view frustum.
202    *
203    * @param bufferIndex The buffer to read from.
204    * @param origin the world position center of the cubeoid to check.
205    * @param halfExtents The half length of the cubeoid in world co-ordinates in each axis.
206    *
207    * @return false if the cubeoid lies completely outside of the frustum, true otherwise
208    */
209   bool CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents );
210
211   /**
212    * Retrieve the projection-matrix; this is double buffered for input handling.
213    * @param[in] bufferIndex The buffer to read from.
214    * @return The projection-matrix.
215    */
216   const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
217
218   /**
219    * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
220    * @param[in] bufferIndex The buffer to read from.
221    * @return The inverse view-projection-matrix.
222    */
223   const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
224
225   /**
226    * Retrieve the final projection-matrix; this is double buffered for input handling.
227    * @param[in] bufferIndex The buffer to read from.
228    * @return The projection-matrix that should be used to render.
229    */
230   const Matrix& GetFinalProjectionMatrix( BufferIndex bufferIndex ) const;
231
232   /**
233    * Retrieve the projection-matrix property querying interface.
234    * @pre The camera is on-stage.
235    * @return The projection-matrix property querying interface.
236    */
237   const PropertyInputImpl* GetProjectionMatrix() const;
238
239   /**
240    * Retrieve the viewMatrix property querying interface.
241    * @pre The camera is on-stage.
242    * @return The viewMatrix property querying interface.
243    */
244   const PropertyInputImpl* GetViewMatrix() const;
245
246   /**
247    * Updates view and projection matrices.
248    * Called by the render task using the camera
249    * @param[in] updateBufferIndex The buffer to read from.
250    */
251   void Update( BufferIndex updateBufferIndex );
252
253   /**
254    * @return true if the view matrix of camera is updated this or the previous frame
255    */
256   bool ViewMatrixUpdated();
257
258 private:
259
260   /**
261    * Constructor
262    */
263   Camera();
264
265   // Non copyable
266   // Undefined
267   Camera(const Camera&);
268   // Undefined
269   Camera& operator=(const Camera& rhs);
270
271   /**
272    * Recalculates the view matrix.
273    * @param[in] bufferIndex The current update buffer index.
274    * @return count how many frames ago the matrix was changed.
275    */
276   uint32_t UpdateViewMatrix( BufferIndex updateBufferIndex );
277
278   /**
279    * Recalculates the projection matrix.
280    * @param[in] bufferIndex The current update buffer index.
281    * @return count how many frames ago the matrix was changed.
282    */
283   uint32_t UpdateProjection( BufferIndex updateBufferIndex );
284
285 private:
286
287   /**
288    * @brief Extracts the frustum planes.
289    *
290    * @param[in] bufferIndex The current update buffer index.
291    * @param[in] normalize will normalize plane equation coefficients by default.
292    */
293   void UpdateFrustum( BufferIndex updateBufferIndex, bool normalize = true );
294
295   /**
296    * Adjust near plane for reflection
297    * @param perspective Perspective matrix
298    * @param clipPlane Clipping plane
299    */
300   void AdjustNearPlaneForPerspective( Matrix& perspective, const Vector4& clipPlane );
301
302   uint32_t                  mUpdateViewFlag;       ///< This is non-zero if the view matrix requires an update
303   uint32_t                  mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
304   int                       mProjectionRotation;   ///< The rotaion angle of the projection
305   const Node*                   mNode;                 ///< The node this scene graph camera belongs to
306
307 public:  // PROPERTIES
308   Dali::Camera::Type            mType;                 // Non-animatable
309   Dali::Camera::ProjectionMode  mProjectionMode;       // Non-animatable
310   bool                          mInvertYAxis;          // Non-animatable
311
312   float                         mFieldOfView;
313   float                         mAspectRatio;
314   float                         mLeftClippingPlane;
315   float                         mRightClippingPlane;
316   float                         mTopClippingPlane;
317   float                         mBottomClippingPlane;
318   float                         mNearClippingPlane;
319   float                         mFarClippingPlane;
320   Vector3                       mTargetPosition;
321
322   Dali::Matrix                  mReflectionMtx;
323   Dali::Vector4                 mReflectionPlane;
324   Dali::Vector4                 mReflectionEye;
325   bool                          mUseReflection{ false };
326   bool                          mUseReflectionClip{ false };
327
328   InheritedMatrix mViewMatrix;           ///< The viewMatrix; this is double buffered for input handling.
329   InheritedMatrix mProjectionMatrix;     ///< The projectionMatrix; this is double buffered for input handling.
330
331   DoubleBuffered< FrustumPlanes > mFrustum;               ///< Clipping frustum; double buffered for input handling
332   DoubleBuffered< Matrix >        mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
333   DoubleBuffered< Matrix >        mFinalProjection;       ///< Final projection matrix; double buffered for input handling
334
335 };
336
337 // Messages for Camera
338
339 inline void SetTypeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::Type parameter )
340 {
341   typedef MessageValue1< Camera, Dali::Camera::Type > LocalType;
342
343   // Reserve some memory inside the message queue
344   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
345
346   // Construct message in the message queue memory; note that delete should not be called on the return value
347   new (slot) LocalType( &camera, &Camera::SetType, parameter );
348 }
349
350 inline void SetProjectionModeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::ProjectionMode parameter )
351 {
352   typedef MessageValue1< Camera, Dali::Camera::ProjectionMode > LocalProjectionMode;
353
354   // Reserve some memory inside the message queue
355   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
356
357   // Construct message in the message queue memory; note that delete should not be called on the return value
358   new (slot) LocalProjectionMode( &camera, &Camera::SetProjectionMode, parameter );
359 }
360
361 inline void SetFieldOfViewMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
362 {
363   typedef MessageValue1< Camera, float > LocalType;
364
365   // Reserve some memory inside the message queue
366   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
367
368   // Construct message in the message queue memory; note that delete should not be called on the return value
369   new (slot) LocalType( &camera, &Camera::SetFieldOfView, parameter );
370 }
371
372 inline void SetAspectRatioMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
373 {
374   typedef MessageValue1< Camera, float > LocalType;
375
376   // Reserve some memory inside the message queue
377   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
378
379   // Construct message in the message queue memory; note that delete should not be called on the return value
380   new (slot) LocalType( &camera, &Camera::SetAspectRatio, parameter );
381 }
382
383 inline void SetLeftClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
384 {
385   typedef MessageValue1< Camera, float > LocalType;
386
387   // Reserve some memory inside the message queue
388   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
389
390   // Construct message in the message queue memory; note that delete should not be called on the return value
391   new (slot) LocalType( &camera, &Camera::SetLeftClippingPlane, parameter );
392 }
393
394 inline void SetRightClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
395 {
396   typedef MessageValue1< Camera, float > LocalType;
397
398   // Reserve some memory inside the message queue
399   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
400
401   // Construct message in the message queue memory; note that delete should not be called on the return value
402   new (slot) LocalType( &camera, &Camera::SetRightClippingPlane, parameter );
403 }
404
405 inline void SetTopClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
406 {
407   typedef MessageValue1< Camera, float > LocalType;
408
409   // Reserve some memory inside the message queue
410   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
411
412   // Construct message in the message queue memory; note that delete should not be called on the return value
413   new (slot) LocalType( &camera, &Camera::SetTopClippingPlane, parameter );
414 }
415
416 inline void SetBottomClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
417 {
418   typedef MessageValue1< Camera, float > LocalType;
419
420   // Reserve some memory inside the message queue
421   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
422
423   // Construct message in the message queue memory; note that delete should not be called on the return value
424   new (slot) LocalType( &camera, &Camera::SetBottomClippingPlane, parameter );
425 }
426
427 inline void SetNearClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
428 {
429   typedef MessageValue1< Camera, float > LocalType;
430
431   // Reserve some memory inside the message queue
432   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
433
434   // Construct message in the message queue memory; note that delete should not be called on the return value
435   new (slot) LocalType( &camera, &Camera::SetNearClippingPlane, parameter );
436 }
437
438 inline void SetFarClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
439 {
440   typedef MessageValue1< Camera, float > LocalType;
441
442   // Reserve some memory inside the message queue
443   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
444
445   // Construct message in the message queue memory; note that delete should not be called on the return value
446   new (slot) LocalType( &camera, &Camera::SetFarClippingPlane, parameter );
447 }
448
449 inline void SetTargetPositionMessage( EventThreadServices& eventThreadServices, const Camera& camera, const Vector3& parameter )
450 {
451   typedef MessageValue1< Camera, Vector3 > LocalType;
452
453   // Reserve some memory inside the message queue
454   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
455
456   // Construct message in the message queue memory; note that delete should not be called on the return value
457   new (slot) LocalType( &camera, &Camera::SetTargetPosition, parameter );
458 }
459
460 inline void SetInvertYAxisMessage( EventThreadServices& eventThreadServices, const Camera& camera, bool parameter )
461 {
462   typedef MessageValue1< Camera, bool > LocalType;
463
464   // Reserve some memory inside the message queue
465   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
466
467   // Construct message in the message queue memory; note that delete should not be called on the return value
468   new (slot) LocalType( &camera, &Camera::SetInvertYAxis, parameter );
469 }
470
471 inline void RotateProjectionMessage( EventThreadServices& eventThreadServices, const Camera& camera, int parameter )
472 {
473   typedef MessageValue1< Camera, int > LocalType;
474
475   // Reserve some memory inside the message queue
476   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
477
478   // Construct message in the message queue memory; note that delete should not be called on the return value
479   new (slot) LocalType( &camera, &Camera::RotateProjection, parameter );
480 }
481
482 } // namespace SceneGraph
483
484 } // namespace Internal
485
486 } // namespace Dali
487
488 #endif // DALI_INTERNAL_SCENE_GRAPH_CAMERA_H