Refactored EventToUpdate into EventThreadServices
[platform/core/uifw/dali-core.git] / dali / internal / update / node-attachments / scene-graph-camera-attachment.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_CAMERA_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_CAMERA_ATTACHMENT_H__
3
4 /*
5  * Copyright (c) 2014 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/node-attachments/node-attachment.h>
28 #include <dali/internal/update/common/animatable-property.h>
29 #include <dali/internal/update/common/inherited-property.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 // value types used by messages
38 template <> struct ParameterType< Dali::Camera::Type >
39 : public BasicType< Dali::Camera::Type > {};
40 template <> struct ParameterType< Dali::Camera::ProjectionMode >
41 : public BasicType< Dali::Camera::ProjectionMode > {};
42
43 namespace SceneGraph
44 {
45
46 class RenderableAttachment;
47 class SceneController;
48
49 /**
50  * An attachment for camera objects and their properties.
51  */
52 class CameraAttachment : public NodeAttachment
53 {
54 public:
55   static const Dali::Camera::Type DEFAULT_TYPE;
56   static const Dali::Camera::ProjectionMode DEFAULT_MODE;
57   static const bool  DEFAULT_INVERT_Y_AXIS;
58   static const float DEFAULT_FIELD_OF_VIEW;
59   static const float DEFAULT_ASPECT_RATIO;
60   static const float DEFAULT_LEFT_CLIPPING_PLANE;
61   static const float DEFAULT_RIGHT_CLIPPING_PLANE;
62   static const float DEFAULT_TOP_CLIPPING_PLANE;
63   static const float DEFAULT_BOTTOM_CLIPPING_PLANE;
64   static const float DEFAULT_NEAR_CLIPPING_PLANE;
65   static const float DEFAULT_FAR_CLIPPING_PLANE;
66   static const Vector2 DEFAULT_STEREO_BIAS;
67   static const Vector3 DEFAULT_TARGET_POSITION;
68
69   /**
70    * Construct a new CameraAttachment.
71    * @return a new camera.
72    */
73   static CameraAttachment* New();
74
75   /**
76    * @copydoc NodeAttachment::ConnectToSceneGraph().
77    */
78   virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
79
80   /**
81    * @copydoc NodeAttachment::OnDestroy().
82    */
83   virtual void OnDestroy();
84
85   /**
86    * Virtual destructor
87    */
88   virtual ~CameraAttachment();
89
90   /**
91    * @copydoc NodeAttachment::GetRenderable.
92    * @return Always NULL.
93    */
94   virtual RenderableAttachment* GetRenderable();
95
96   /**
97    * @copydoc Dali::Internal::CameraAttachment::SetType
98    */
99   void SetType( Dali::Camera::Type type );
100
101   /**
102    * @copydoc Dali::Internal::CameraAttachment::SetInvertYAxis
103    */
104   void SetInvertYAxis( bool invertYAxis );
105
106   /**
107    * @copydoc Dali::Internal::CameraAttachment::SetProjectionMode
108    */
109   void SetProjectionMode( Dali::Camera::ProjectionMode projectionMode );
110
111   /**
112    * @copydoc Dali::Internal::CameraAttachment::SetFieldOfView
113    */
114   void SetFieldOfView( float fieldOfView );
115
116   /**
117    * @copydoc Dali::Internal::CameraAttachment::SetAspectRatio
118    */
119   void SetAspectRatio( float aspectRatio );
120
121   /**
122    * @copydoc Dali::Internal::CameraAttachment::SetStereoBias
123    */
124   void SetStereoBias(const Vector2& stereoBias);
125
126    /**
127    * @copydoc Dali::Internal::CameraAttachment::SetLeftClippingPlane
128    */
129   void SetLeftClippingPlane( float leftClippingPlane );
130
131   /**
132    * @copydoc Dali::Internal::CameraAttachment::SetRightClippingPlane
133    */
134   void SetRightClippingPlane( float rightClippingPlane );
135
136   /**
137    * @copydoc Dali::Internal::CameraAttachment::SetTopClippingPlane
138    */
139   void SetTopClippingPlane( float topClippingPlane );
140
141   /**
142    * @copydoc Dali::Internal::CameraAttachment::SetBottomClippingPlane
143    */
144   void SetBottomClippingPlane( float bottomClippingPlane );
145
146   /**
147    * @copydoc Dali::Internal::CameraAttachment::SetNearClippingPlane
148    */
149   void SetNearClippingPlane( float nearClippingPlane );
150
151   /**
152    * @copydoc Dali::Internal::CameraAttachment::SetFarClippingPlane
153    */
154   void SetFarClippingPlane( float farClippingPlane );
155
156   /**
157    * @copydoc Dali::Internal::CameraAttachment::SetTarget
158    */
159   void SetTargetPosition( const Vector3& targetPosition );
160
161   /**
162    * Get Camera Target
163    */
164   Vector3 GetTargetPosition() const;
165
166   /**
167    * Retrieve the view-matrix; this is double buffered for input handling.
168    * @param[in] bufferIndex The buffer to read from.
169    * @return The view-matrix.
170    */
171   const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
172
173   /**
174    * Retrieve the projection-matrix; this is double buffered for input handling.
175    * @param[in] bufferIndex The buffer to read from.
176    * @return The projection-matrix.
177    */
178   const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
179
180   /**
181    * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
182    * @param[in] bufferIndex The buffer to read from.
183    * @return The inverse view-projection-matrix.
184    */
185   const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
186
187   /**
188    * Retrieve the projection-matrix property querying interface.
189    * @pre The attachment is on-stage.
190    * @return The projection-matrix property querying interface.
191    */
192   const PropertyInputImpl* GetProjectionMatrix() const;
193
194   /**
195    * Retrieve the view-matrix property querying interface.
196    * @pre The attachment is on-stage.
197    * @return The view-matrix property querying interface.
198    */
199   const PropertyInputImpl* GetViewMatrix() const;
200
201   /**
202    * @copydoc NodeAttachment::Update
203    */
204   virtual void Update( BufferIndex updateBufferIndex, const Node& owningNode, int nodeDirtyFlags );
205
206   /**
207    * @return true if the view matrix of camera is updated this or the previous frame
208    */
209   bool ViewMatrixUpdated();
210
211 protected:
212
213   /**
214    * Protected constructor, see New().
215    */
216   CameraAttachment();
217
218 private:
219
220   // Undefined
221   CameraAttachment(const CameraAttachment&);
222
223   // Undefined
224   CameraAttachment& operator=(const CameraAttachment& rhs);
225
226   /**
227    * Recalculates the view matrix.
228    * @param[in] bufferIndex The current update buffer index.
229    * @param[in] owningNode to use for calculations.
230    * @return count how many frames ago the matrix was changed.
231    */
232   unsigned int UpdateViewMatrix( BufferIndex updateBufferIndex, const Node& owningNode );
233
234   /**
235    * Recalculates the projection matrix.
236    * @param[in] bufferIndex The current update buffer index.
237    * @return count how many frames ago the matrix was changed.
238    */
239   unsigned int UpdateProjection( BufferIndex updateBufferIndex );
240
241 private:
242
243   unsigned int                  mUpdateViewFlag;       ///< This is non-zero if the view matrix requires an update
244   unsigned int                  mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
245
246 public:  // PROPERTIES
247   Dali::Camera::Type            mType;                 // Non-animatable
248   Dali::Camera::ProjectionMode  mProjectionMode;       // Non-animatable
249   bool                          mInvertYAxis;          // Non-animatable
250
251   float                         mFieldOfView;
252   float                         mAspectRatio;
253   float                         mLeftClippingPlane;
254   float                         mRightClippingPlane;
255   float                         mTopClippingPlane;
256   float                         mBottomClippingPlane;
257   float                         mNearClippingPlane;
258   float                         mFarClippingPlane;
259   Vector2                       mStereoBias;
260   Vector3                       mTargetPosition;
261
262   InheritedMatrix mViewMatrix;           ///< The view-matrix; this is double buffered for input handling.
263   InheritedMatrix mProjectionMatrix;     ///< The projection-matrix; this is double buffered for input handling.
264
265   DoubleBuffered< Matrix >      mInverseViewProjection;///< Inverted viewprojection; double buffered for input handling
266
267 };
268
269 // Messages for CameraAttachment
270
271 inline void SetTypeMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, Dali::Camera::Type parameter )
272 {
273   typedef MessageValue1< CameraAttachment, Dali::Camera::Type > LocalType;
274
275   // Reserve some memory inside the message queue
276   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
277
278   // Construct message in the message queue memory; note that delete should not be called on the return value
279   new (slot) LocalType( &attachment, &CameraAttachment::SetType, parameter );
280 }
281
282 inline void SetProjectionModeMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, Dali::Camera::ProjectionMode parameter )
283 {
284   typedef MessageValue1< CameraAttachment, Dali::Camera::ProjectionMode > LocalProjectionMode;
285
286   // Reserve some memory inside the message queue
287   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
288
289   // Construct message in the message queue memory; note that delete should not be called on the return value
290   new (slot) LocalProjectionMode( &attachment, &CameraAttachment::SetProjectionMode, parameter );
291 }
292
293
294 inline void SetFieldOfViewMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
295 {
296   typedef MessageValue1< CameraAttachment, float > LocalType;
297
298   // Reserve some memory inside the message queue
299   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
300
301   // Construct message in the message queue memory; note that delete should not be called on the return value
302   new (slot) LocalType( &attachment, &CameraAttachment::SetFieldOfView, parameter );
303 }
304
305 inline void SetAspectRatioMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
306 {
307   typedef MessageValue1< CameraAttachment, float > LocalType;
308
309   // Reserve some memory inside the message queue
310   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
311
312   // Construct message in the message queue memory; note that delete should not be called on the return value
313   new (slot) LocalType( &attachment, &CameraAttachment::SetAspectRatio, parameter );
314 }
315
316 inline void SetStereoBiasMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, const Vector2& parameter )
317 {
318   typedef MessageValue1< CameraAttachment, Vector2 > LocalType;
319
320   // Reserve some memory inside the message queue
321   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
322
323   // Construct message in the message queue memory; note that delete should not be called on the return value
324   new (slot) LocalType( &attachment, &CameraAttachment::SetStereoBias, parameter );
325 }
326
327 inline void SetLeftClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
328 {
329   typedef MessageValue1< CameraAttachment, float > LocalType;
330
331   // Reserve some memory inside the message queue
332   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
333
334   // Construct message in the message queue memory; note that delete should not be called on the return value
335   new (slot) LocalType( &attachment, &CameraAttachment::SetLeftClippingPlane, parameter );
336 }
337
338 inline void SetRightClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
339 {
340   typedef MessageValue1< CameraAttachment, float > LocalType;
341
342   // Reserve some memory inside the message queue
343   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
344
345   // Construct message in the message queue memory; note that delete should not be called on the return value
346   new (slot) LocalType( &attachment, &CameraAttachment::SetRightClippingPlane, parameter );
347 }
348
349 inline void SetTopClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
350 {
351   typedef MessageValue1< CameraAttachment, float > LocalType;
352
353   // Reserve some memory inside the message queue
354   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
355
356   // Construct message in the message queue memory; note that delete should not be called on the return value
357   new (slot) LocalType( &attachment, &CameraAttachment::SetTopClippingPlane, parameter );
358 }
359
360 inline void SetBottomClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
361 {
362   typedef MessageValue1< CameraAttachment, float > LocalType;
363
364   // Reserve some memory inside the message queue
365   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
366
367   // Construct message in the message queue memory; note that delete should not be called on the return value
368   new (slot) LocalType( &attachment, &CameraAttachment::SetBottomClippingPlane, parameter );
369 }
370
371 inline void SetNearClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
372 {
373   typedef MessageValue1< CameraAttachment, float > LocalType;
374
375   // Reserve some memory inside the message queue
376   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
377
378   // Construct message in the message queue memory; note that delete should not be called on the return value
379   new (slot) LocalType( &attachment, &CameraAttachment::SetNearClippingPlane, parameter );
380 }
381
382 inline void SetFarClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
383 {
384   typedef MessageValue1< CameraAttachment, float > LocalType;
385
386   // Reserve some memory inside the message queue
387   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
388
389   // Construct message in the message queue memory; note that delete should not be called on the return value
390   new (slot) LocalType( &attachment, &CameraAttachment::SetFarClippingPlane, parameter );
391 }
392
393 inline void SetTargetPositionMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, const Vector3& parameter )
394 {
395   typedef MessageValue1< CameraAttachment, Vector3 > LocalType;
396
397   // Reserve some memory inside the message queue
398   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
399
400   // Construct message in the message queue memory; note that delete should not be called on the return value
401   new (slot) LocalType( &attachment, &CameraAttachment::SetTargetPosition, parameter );
402 }
403
404
405 inline void SetInvertYAxisMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, bool parameter )
406 {
407   typedef MessageValue1< CameraAttachment, bool > LocalType;
408
409   // Reserve some memory inside the message queue
410   unsigned int* 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( &attachment, &CameraAttachment::SetInvertYAxis, parameter );
414 }
415
416
417 } // namespace SceneGraph
418
419 } // namespace Internal
420
421 } // namespace Dali
422
423 #endif // __DALI_INTERNAL_SCENE_GRAPH_CAMERA_ATTACHMENT_H__