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