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