99ed6c192a35ae96f8c1ebf054d16c97b24682ec
[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) 2017 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
53 {
54 public:
55
56   enum State
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] resourceId The resource ID of the frame-buffer, or zero if not rendering off-screen.
114    * @param[in] isNativeFBO if this render task is targeting a native FBO
115    */
116   void SetFrameBufferId( unsigned int resourceId, bool isNativeFBO );
117
118   /**
119    * Retrieve the resource ID of the frame-buffer.
120    * @return The resource ID, or zero if not rendering off-screen.
121    */
122   unsigned int GetFrameBufferId() const;
123
124   /**
125    * Set the frame-buffer used as a render target.
126    * @param[in] frameBuffer The framebuffer
127    */
128   void SetFrameBuffer( Render::FrameBuffer* frameBuffer );
129
130   /**
131    * Retrieve the resource ID of the frame-buffer.
132    * @return The resource ID, or zero if not rendering off-screen.
133    */
134   Render::FrameBuffer* GetFrameBuffer();
135
136   /**
137    * Set the value of property viewportPosition
138    * This value will persist only for the current frame.
139    * @param[in] updateBufferIndex The current update buffer index.
140    * @param[in] value The value of the property
141    */
142   void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
143
144   /**
145    * Get the value of property viewportPosition
146    * @warning Should only be called from the Update thread
147    * @param[in] bufferIndex The buffer to read from.
148    * @return the value of the property.
149    */
150   const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
151
152   /**
153    * Bake the value of the property viewportPosition
154    * This will also set the base value
155    * @param[in] updateBufferIndex The current update buffer index.
156    * @param[in] value The new value for property.
157    */
158   void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
159
160   /**
161    * Set the value of property viewportSize
162    * This value will persist only for the current frame.
163    * @param[in] updateBufferIndex The current update buffer index.
164    * @param[in] value The value of the property
165    */
166   void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
167
168   /**
169    * Get the value of property viewportSize
170    * @warning Should only be called from the Update thread
171    * @param[in] bufferIndex The buffer to read from.
172    * @return the value of the property.
173    */
174   const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
175
176   /**
177    * Bake the value of the property viewportSize
178    * This will also set the base value
179    * @param[in] updateBufferIndex The current update buffer index.
180    * @param[in] value The new value for property.
181    */
182   void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
183
184   /**
185    * Get the value of property viewportEnabled
186    * @warning Should only be called from the Update thread
187    * @param[in] bufferIndex The buffer to read from.
188    * @return the value of the property.
189    */
190   bool GetViewportEnabled( BufferIndex bufferIndex ) const;
191
192   /**
193    * Query whether the optional viewport is set.
194    * @param[in] bufferIndex The buffer to read from.
195    * @param[out] viewport The viewport position and size is populated.
196    * @return true if the viewport has been set
197    */
198   bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
199
200   /**
201    * Set the value of property clearColor
202    * This value will persist only for the current frame.
203    * @param[in] updateBufferIndex The current update buffer index.
204    * @param[in] value The value of the property
205    */
206   void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
207
208   /**
209    * Get the value of property clearColor
210    * @warning Should only be called from the Update thread
211    * @param[in] bufferIndex The buffer to read from.
212    * @return the value of the property.
213    */
214   const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
215
216   /**
217    * Bake the value of the property clearColor
218    * This will also set the base value
219    * @param[in] updateBufferIndex The current update buffer index.
220    * @param[in] value The new value for property.
221    */
222   void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
223
224   /**
225    * @copydoc Dali::RenderTask::SetClearEnabled()
226    */
227   void SetClearEnabled( bool enabled );
228
229   /**
230    * @copydoc Dali::RenderTask::GetClearEnabled()
231    */
232   bool GetClearEnabled() const;
233
234   /**
235    * @copydoc Dali::RenderTask::SetCullMode()
236    */
237   void SetCullMode( bool mode );
238
239   /**
240    * @copydoc Dali::RenderTask::GetCullMode()
241    */
242   bool GetCullMode() const;
243
244   /**
245    * Set the refresh-rate of the RenderTask.
246    * @param[in] refreshRate The new refresh rate.
247    */
248   void SetRefreshRate( unsigned int refreshRate );
249
250   /**
251    * Retrieve the refresh-rate of the RenderTask.
252    * @return The refresh rate.
253    */
254   unsigned int GetRefreshRate() const;
255
256   /**
257    * Check if the render task is ready for rendering.
258    * @param[in] updateBufferIndex The current update buffer index.
259    * @return True if the render-task is ready for rendering.
260    */
261   bool ReadyToRender( BufferIndex updateBufferIndex );
262
263   /**
264    * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
265    * 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.
266    * @return true if a render is required
267    */
268   bool IsRenderRequired();
269
270   /**
271    * Process a frame. This method is called each frame for every ready render task, regardless
272    * of whether it needs to render (so that the frame counter can be updated).
273    */
274   void UpdateState();
275
276   /**
277    * Return true only if currently waiting for the render task to
278    * finish rendering and the update thread should be kept alive.
279    * @return true if waiting to be rendered
280    */
281   bool IsWaitingToRender();
282
283   /**
284    * Return true when the render task has finished rendering and a notification
285    * needs sending. (Only one notification is sent per render once request)
286    * @return true if notification is required.
287    */
288   bool HasRendered();
289
290   /**
291    * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
292    */
293   unsigned int GetRenderedOnceCounter() const;
294
295   /**
296    * Retrieve the view-matrix; this is double buffered for input handling.
297    * @pre GetCameraNode() returns a node with valid Camera.
298    * @param[in] bufferIndex The buffer to read from.
299    * @return The view-matrix.
300    */
301   const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
302
303   /**
304    * @brief Retrieve the camera.
305    * @pre GetCameraNode() returns a node with valid Camera.
306    *
307    * @return The camera.
308    */
309   SceneGraph::Camera& GetCamera() const;
310
311   /**
312    * Retrieve the projection-matrix; this is double buffered for input handling.
313    * @pre GetCameraNode() returns a node with valid Camera.
314    * @param[in] bufferIndex The buffer to read from.
315    * @return The projection-matrix.
316    */
317   const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
318
319   /**
320    * Prepares the render-instruction buffer to be populated with instructions.
321    *
322    * If the render task is a render-once framebuffer backed by a native image,
323    * then this method will ensure that a GL sync object is created to track
324    * when the rendering has finished.
325    *
326    * @param[out] instruction to prepare
327    * @param[in] updateBufferIndex The current update buffer index.
328    */
329   void PrepareRenderInstruction( RenderInstruction& instruction, BufferIndex updateBufferIndex );
330
331   /**
332    * @return true if the view matrix has been updated during this or last frame
333    */
334   bool ViewMatrixUpdated();
335
336   /**
337    * Indicate whether GL sync is required for native render target.
338    * @param[in] requiresSync whether GL sync is required for native render target
339    */
340   void SetSyncRequired( bool requiresSync );
341
342 private:
343
344   /**
345    * Protected constructor.
346    */
347   RenderTask();
348
349   // Undefined
350   RenderTask(const RenderTask&);
351
352   // Undefined
353   RenderTask& operator=(const RenderTask&);
354
355 private: // PropertyOwner
356
357   virtual void ResetDefaultProperties( BufferIndex currentBufferIndex );
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   bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
373   bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
374   bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
375   bool mClearEnabled: 1; ///< Whether previous results are cleared.
376   bool mCullMode: 1; ///< Whether renderers should be frustum culled
377
378   State mState;                     ///< Render state.
379   unsigned int mRefreshRate;        ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
380   unsigned int mFrameCounter;       ///< counter for rendering every N frames
381
382   unsigned int mRenderedOnceCounter;  ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
383   bool mRequiresSync;              ///< Whether sync is needed to track the render
384
385 };
386
387 // Messages for RenderTask
388 inline void SetFrameBufferMessage( EventThreadServices& eventThreadServices, RenderTask& task, Render::FrameBuffer* frameBuffer )
389 {
390   typedef MessageValue1< RenderTask, Render::FrameBuffer*> 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::SetFrameBuffer, frameBuffer );
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 SetCameraMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode, const Camera* constCamera )
469 {
470   typedef MessageValue2< RenderTask, Node*, Camera* > LocalType;
471
472   Node* node = const_cast< Node* >( constNode );
473   Camera* camera = const_cast< Camera* >( constCamera );
474   // Reserve memory inside the message queue
475   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
476
477   // Construct message in the message queue memory; note that delete should not be called on the return value
478   new (slot) LocalType( &task, &RenderTask::SetCamera, node, camera );
479 }
480
481 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool exclusive )
482 {
483   typedef MessageValue1< RenderTask, bool > LocalType;
484
485   // Reserve some memory inside the message queue
486   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
487
488   // Construct message in the message queue memory; note that delete should not be called on the return value
489   new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
490 }
491
492 inline void SetSyncRequiredMessage(EventThreadServices& eventThreadServices, RenderTask& task, bool requiresSync )
493 {
494   typedef MessageValue1< RenderTask, bool > LocalType;
495
496   // Reserve some memory inside the message queue
497   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
498
499   // Construct message in the message queue memory; note that delete should not be called on the return value
500   new (slot) LocalType( &task, &RenderTask::SetSyncRequired, requiresSync );
501 }
502
503 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
504 {
505   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
506
507   // Reserve some memory inside the message queue
508   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
509
510   // Construct message in the message queue memory; note that delete should not be called on the return value
511   new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
512 }
513
514 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
515 {
516   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
517
518   // Reserve some memory inside the message queue
519   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
520
521   // Construct message in the message queue memory; note that delete should not be called on the return value
522   new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
523 }
524
525 } // namespace SceneGraph
526
527 } // namespace Internal
528
529 } // namespace Dali
530
531 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__