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