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