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