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