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