Added image renderer clip space culling
[platform/core/uifw/dali-core.git] / dali / internal / update / render-tasks / scene-graph-render-task.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_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/viewport.h>
23 #include <dali/public-api/render-tasks/render-task.h>
24 #include <dali/internal/common/buffer-index.h>
25 #include <dali/internal/common/event-to-update.h>
26 #include <dali/internal/common/message.h>
27 #include <dali/internal/update/common/double-buffered.h>
28 #include <dali/internal/update/common/property-owner.h>
29 #include <dali/internal/update/common/animatable-property.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36 class FrameBufferTexture;
37 class CompleteStatusManager;
38
39 namespace SceneGraph
40 {
41 class Node;
42 class CameraAttachment;
43 class RenderInstruction;
44
45 /**
46  * RenderTasks describe how the Dali scene should be rendered.
47  */
48 class RenderTask : public PropertyOwner
49 {
50 public:
51   enum State
52   {
53     RENDER_CONTINUOUSLY,               ///< mRefreshRate > 0
54     RENDER_ONCE_WAITING_FOR_RESOURCES, ///< mRefreshRate = REFRESH_ONCE
55     RENDERED_ONCE,                     ///< mRefreshRate = REFRESH_ONCE & rendered
56     RENDERED_ONCE_AND_NOTIFIED         ///< mRefreshRate = REFRESH_ONCE & rendered & notified
57   };
58
59   /**
60    * Create a new RenderTask
61    */
62   static RenderTask* New();
63
64   /**
65    * Virtual destructor
66    */
67   virtual ~RenderTask();
68
69   /**
70    * Set the nodes to be rendered.
71    * @param[in] node This node and its children will be rendered.
72    */
73   void SetSourceNode( Node* node );
74
75   /**
76    * Retrieve the source node.
77    * @return This node and its children will be rendered.
78    */
79   Node* GetSourceNode() const;
80
81   /**
82    * Set whether the RenderTask has exclusive access to the source nodes.
83    * @param[in] exclusive True if the source nodes will only be rendered by this render-task.
84    */
85   void SetExclusive( bool exclusive );
86
87   /**
88    * Query whether the RenderTask has exclusive access to the source actors.
89    * @return True if the source actors will only be rendered by this render-task.
90    */
91   bool IsExclusive() const;
92
93   /**
94    * Set the node from which the scene is viewed.
95    * @param[in] node The scene is viewed from the perspective of this node.
96    */
97   void SetCameraNode( Node* node );
98
99   /**
100    * Retrieve the camera node.
101    * @return The scene is viewed from the perspective of this node.
102    */
103   Node* GetCameraNode() const;
104
105   /**
106    * Set the frame-buffer used as a render target.
107    * @param[in] resourceId The resource ID of the frame-buffer, or zero if not rendering off-screen.
108    */
109   void SetFrameBufferId( unsigned int resourceId );
110
111   /**
112    * Retrieve the resource ID of the frame-buffer.
113    * @return The resource ID, or zero if not rendering off-screen.
114    */
115   unsigned int GetFrameBufferId() const;
116
117   /**
118    * Set the value of property viewport-position
119    * This value will persist only for the current frame.
120    * @param[in] updateBufferIndex The current update buffer index.
121    * @param[in] value The value of the property
122    */
123   void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
124
125   /**
126    * Get the value of property viewport-position
127    * @warning Should only be called from the Update thread
128    * @param[in] bufferIndex The buffer to read from.
129    * @return the value of the property.
130    */
131   const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
132
133   /**
134    * Bake the value of the property viewport-position
135    * This will also set the base value
136    * @param[in] updateBufferIndex The current update buffer index.
137    * @param[in] value The new value for property.
138    */
139   void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
140
141   /**
142    * Set the value of property viewport-size
143    * This value will persist only for the current frame.
144    * @param[in] updateBufferIndex The current update buffer index.
145    * @param[in] value The value of the property
146    */
147   void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
148
149   /**
150    * Get the value of property viewport-size
151    * @warning Should only be called from the Update thread
152    * @param[in] bufferIndex The buffer to read from.
153    * @return the value of the property.
154    */
155   const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
156
157   /**
158    * Bake the value of the property viewport-size
159    * This will also set the base value
160    * @param[in] updateBufferIndex The current update buffer index.
161    * @param[in] value The new value for property.
162    */
163   void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
164
165   /**
166    * Get the value of property viewport-enabled
167    * @warning Should only be called from the Update thread
168    * @param[in] bufferIndex The buffer to read from.
169    * @return the value of the property.
170    */
171   bool GetViewportEnabled( BufferIndex bufferIndex ) const;
172
173   /**
174    * Query whether the optional viewport is set.
175    * @param[in] bufferIndex The buffer to read from.
176    * @param[out] viewport The viewport position and size is populated.
177    * @return true if the viewport has been set
178    */
179   bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
180
181   /**
182    * Set the value of property clear-color
183    * This value will persist only for the current frame.
184    * @param[in] updateBufferIndex The current update buffer index.
185    * @param[in] value The value of the property
186    */
187   void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
188
189   /**
190    * Get the value of property clear-color
191    * @warning Should only be called from the Update thread
192    * @param[in] bufferIndex The buffer to read from.
193    * @return the value of the property.
194    */
195   const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
196
197   /**
198    * Bake the value of the property clear-color
199    * This will also set the base value
200    * @param[in] updateBufferIndex The current update buffer index.
201    * @param[in] value The new value for property.
202    */
203   void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
204
205   /**
206    * @copydoc Dali::RenderTask::SetClearEnabled()
207    */
208   void SetClearEnabled( bool enabled );
209
210   /**
211    * @copydoc Dali::RenderTask::GetClearEnabled()
212    */
213   bool GetClearEnabled() const;
214
215   /**
216    * @copydoc Dali::RenderTask::SetCullMode()
217    */
218   void SetCullMode( bool mode );
219
220   /**
221    * @copydoc Dali::RenderTask::GetCullMode()
222    */
223   bool GetCullMode() const;
224
225   /**
226    * Set the refresh-rate of the RenderTask.
227    * @param[in] refreshRate The new refresh rate.
228    */
229   void SetRefreshRate( unsigned int refreshRate );
230
231   /**
232    * Retrieve the refresh-rate of the RenderTask.
233    * @return The refresh rate.
234    */
235   unsigned int GetRefreshRate() const;
236
237   /**
238    * Check if the render task is ready for rendering.
239    * @param[in] updateBufferIndex The current update buffer index.
240    * @return True if the render-task is ready for rendering.
241    */
242   bool ReadyToRender( BufferIndex updateBufferIndex );
243
244   /**
245    * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
246    * this returns true if the frame count is zero. If the current state is RENDER_ONCE_WAITING_FOR_RESOURCES, then it always returns true. In all other states, it returns false.
247    * @return true if a render is required
248    */
249   bool IsRenderRequired();
250
251   /**
252    * Process a frame. This method is called each frame for every ready render task, regardless
253    * of whether it needs to render (so that the frame counter can be updated).
254    * @param[in] resourcesComplete true if the resources of the source tree are completely loaded.
255    */
256   void UpdateState( bool resourcesComplete );
257
258   /**
259    * Return true only if currently waiting for the render task to
260    * finish rendering and the update thread should be kept alive.
261    * @return true if waiting to be rendered
262    */
263   bool IsWaitingToRender();
264
265   /**
266    * Return true when the render task has finished rendering and a notification
267    * needs sending. (Only one notification is sent per render once request)
268    * @return true if notification is required.
269    */
270   bool HasRendered();
271
272   /**
273    * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
274    */
275   unsigned int GetRenderedOnceCounter() const;
276
277   /**
278    * Retrieve the view-matrix; this is double buffered for input handling.
279    * @pre GetCameraNode() returns a node with valid CameraAttachment.
280    * @param[in] bufferIndex The buffer to read from.
281    * @return The view-matrix.
282    */
283   const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
284
285   /**
286    * Retrieve the projection-matrix; this is double buffered for input handling.
287    * @pre GetCameraNode() returns a node with valid CameraAttachment.
288    * @param[in] bufferIndex The buffer to read from.
289    * @return The projection-matrix.
290    */
291   const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
292
293   /**
294    * Prepares the render-instruction buffer to be populated with instructions.
295    * @param[out] instruction to prepare
296    * @param[in] updateBufferIndex The current update buffer index.
297    */
298   void PrepareRenderInstruction( RenderInstruction& instruction, BufferIndex updateBufferIndex );
299
300   /**
301    * @return true if the view matrix has been updated during this or last frame
302    */
303   bool ViewMatrixUpdated();
304
305   /**
306    * Set the complete status tracker.
307    * @param[in] completeStatusManager The complete status Tracker (not owned)
308    */
309   void SetCompleteStatusManager( CompleteStatusManager* completeStatusManager );
310
311 private:
312
313   /**
314    * Protected constructor.
315    */
316   RenderTask();
317
318   // Undefined
319   RenderTask(const RenderTask&);
320
321   // Undefined
322   RenderTask& operator=(const RenderTask&);
323
324 private: // PropertyOwner
325
326   virtual void ResetDefaultProperties( BufferIndex currentBufferIndex );
327
328 public: // Animatable Properties
329   AnimatableProperty< Vector2 >   mViewportPosition;    ///< viewport-position
330   AnimatableProperty< Vector2 >   mViewportSize;        ///< viewport-size
331   AnimatableProperty< Vector4 >   mClearColor;          ///< clear-color
332
333 private:
334   CompleteStatusManager* mCompleteStatusManager;
335   Node* mSourceNode;
336   Node* mCameraNode;
337   CameraAttachment* mCameraAttachment;
338   unsigned int mFrameBufferResourceId;
339
340   bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
341   bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
342   bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
343   bool mClearEnabled: 1; ///< Whether previous results are cleared.
344   bool mCullMode: 1; ///< Whether renderers should be frustum culled
345
346   FrameBufferTexture* mRenderTarget;
347   Viewport mViewport;
348
349   State mState;                     ///< Render state.
350   unsigned int mRefreshRate;        ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
351   unsigned int mFrameCounter;       ///< counter for rendering every N frames
352
353   unsigned int mRenderedOnceCounter;  ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
354
355 };
356
357 // Messages for RenderTask
358
359 inline void SetFrameBufferIdMessage( EventToUpdate& eventToUpdate, RenderTask& task, unsigned int resourceId )
360 {
361   typedef MessageValue1< RenderTask, unsigned int > LocalType;
362
363   // Reserve some memory inside the message queue
364   unsigned int* slot = eventToUpdate.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( &task, &RenderTask::SetFrameBufferId, resourceId );
368 }
369
370 inline void SetClearColorMessage( EventToUpdate& eventToUpdate, RenderTask& task, const Vector4& value )
371 {
372   typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
373
374   // Reserve some memory inside the message queue
375   unsigned int* slot = eventToUpdate.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( &task, &RenderTask::SetClearColor, value );
379 }
380
381 inline void BakeClearColorMessage( EventToUpdate& eventToUpdate, const RenderTask& task, const Vector4& value )
382 {
383   typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
384
385   // Reserve some memory inside the message queue
386   unsigned int* slot = eventToUpdate.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( &task, &RenderTask::BakeClearColor, value );
390 }
391
392 inline void SetClearEnabledMessage( EventToUpdate& eventToUpdate, RenderTask& task, bool enabled )
393 {
394   typedef MessageValue1< RenderTask, bool > LocalType;
395
396   // Reserve some memory inside the message queue
397   unsigned int* slot = eventToUpdate.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( &task, &RenderTask::SetClearEnabled, enabled );
401 }
402
403 inline void SetCullModeMessage( EventToUpdate& eventToUpdate, RenderTask& task, bool mode )
404 {
405   typedef MessageValue1< RenderTask, bool > LocalType;
406
407   // Reserve some memory inside the message queue
408   unsigned int* slot = eventToUpdate.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( &task, &RenderTask::SetCullMode, mode );
412 }
413
414 inline void SetRefreshRateMessage( EventToUpdate& eventToUpdate, RenderTask& task, unsigned int refreshRate )
415 {
416   typedef MessageValue1< RenderTask, unsigned int > LocalType;
417
418   // Reserve some memory inside the message queue
419   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
420
421   // Construct message in the message queue memory; note that delete should not be called on the return value
422   new (slot) LocalType( &task, &RenderTask::SetRefreshRate, refreshRate );
423 }
424
425 inline void SetSourceNodeMessage( EventToUpdate& eventToUpdate, RenderTask& task, const Node* constNode )
426 {
427   // Scene graph thread can destroy this object.
428   Node* node = const_cast< Node* >( constNode );
429
430   typedef MessageValue1< RenderTask, Node* > LocalType;
431
432   // Reserve some memory inside the message queue
433   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
434
435   // Construct message in the message queue memory; note that delete should not be called on the return value
436   new (slot) LocalType( &task, &RenderTask::SetSourceNode, node );
437 }
438
439 inline void SetCameraNodeMessage( EventToUpdate& eventToUpdate, RenderTask& task, const Node* constNode )
440 {
441   // Scene graph thread can destroy this object.
442   Node* node = const_cast< Node* >( constNode );
443
444   typedef MessageValue1< RenderTask, Node* > LocalType;
445
446   // Reserve some memory inside the message queue
447   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
448
449   // Construct message in the message queue memory; note that delete should not be called on the return value
450   new (slot) LocalType( &task, &RenderTask::SetCameraNode, node );
451 }
452
453 inline void SetExclusiveMessage( EventToUpdate& eventToUpdate, RenderTask& task, bool exclusive )
454 {
455   typedef MessageValue1< RenderTask, bool > LocalType;
456
457   // Reserve some memory inside the message queue
458   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
459
460   // Construct message in the message queue memory; note that delete should not be called on the return value
461   new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
462 }
463
464 inline void BakeViewportPositionMessage( EventToUpdate& eventToUpdate, const RenderTask& task, const Vector2& value )
465 {
466   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
467
468   // Reserve some memory inside the message queue
469   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
470
471   // Construct message in the message queue memory; note that delete should not be called on the return value
472   new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
473 }
474
475 inline void BakeViewportSizeMessage( EventToUpdate& eventToUpdate, const RenderTask& task, const Vector2& value )
476 {
477   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
478
479   // Reserve some memory inside the message queue
480   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
481
482   // Construct message in the message queue memory; note that delete should not be called on the return value
483   new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
484 }
485
486 } // namespace SceneGraph
487
488 } // namespace Internal
489
490 } // namespace Dali
491
492 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__