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