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