Merge "Changed Update to reset only target properties each frame" 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) 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
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] 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( unsigned int refreshRate );
236
237   /**
238    * Retrieve the refresh-rate of the RenderTask.
239    * @return The refresh rate.
240    */
241   unsigned int 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   unsigned int 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:
330
331   /**
332    * Protected constructor.
333    */
334   RenderTask();
335
336   // Undefined
337   RenderTask(const RenderTask&);
338
339   // Undefined
340   RenderTask& operator=(const RenderTask&);
341
342
343 public: // Animatable Properties
344   AnimatableProperty< Vector2 >   mViewportPosition;    ///< viewportPosition
345   AnimatableProperty< Vector2 >   mViewportSize;        ///< viewportSize
346   AnimatableProperty< Vector4 >   mClearColor;          ///< clearColor
347
348 private:
349   RenderMessageDispatcher* mRenderMessageDispatcher;
350   Render::RenderTracker* mRenderSyncTracker;
351   Node* mSourceNode;
352   Node* mCameraNode;
353   SceneGraph::Camera* mCamera;
354   Render::FrameBuffer* mFrameBuffer;
355
356   bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
357   bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
358   bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
359   bool mClearEnabled: 1; ///< Whether previous results are cleared.
360   bool mCullMode: 1; ///< Whether renderers should be frustum culled
361
362   State mState;                     ///< Render state.
363   unsigned int mRefreshRate;        ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
364   unsigned int mFrameCounter;       ///< counter for rendering every N frames
365
366   unsigned int mRenderedOnceCounter;  ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
367   bool mRequiresSync;              ///< Whether sync is needed to track the render
368
369 };
370
371 // Messages for RenderTask
372 inline void SetFrameBufferMessage( EventThreadServices& eventThreadServices, RenderTask& task, Render::FrameBuffer* frameBuffer )
373 {
374   typedef MessageValue1< RenderTask, Render::FrameBuffer*> LocalType;
375
376   // Reserve some memory inside the message queue
377   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
378
379   // Construct message in the message queue memory; note that delete should not be called on the return value
380   new (slot) LocalType( &task, &RenderTask::SetFrameBuffer, frameBuffer );
381 }
382
383 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Vector4& value )
384 {
385   typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
386
387   // Reserve some memory inside the message queue
388   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
389
390   // Construct message in the message queue memory; note that delete should not be called on the return value
391   new (slot) LocalType( &task, &RenderTask::SetClearColor, value );
392 }
393
394 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
395 {
396   typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
397
398   // Reserve some memory inside the message queue
399   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
400
401   // Construct message in the message queue memory; note that delete should not be called on the return value
402   new (slot) LocalType( &task, &RenderTask::BakeClearColor, value );
403 }
404
405 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool enabled )
406 {
407   typedef MessageValue1< RenderTask, 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::SetClearEnabled, enabled );
414 }
415
416 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool mode )
417 {
418   typedef MessageValue1< RenderTask, bool > 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::SetCullMode, mode );
425 }
426
427 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int refreshRate )
428 {
429   typedef MessageValue1< RenderTask, unsigned int > 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::SetRefreshRate, refreshRate );
436 }
437
438 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
439 {
440   // Scene graph thread can destroy this object.
441   Node* node = const_cast< Node* >( constNode );
442
443   typedef MessageValue1< RenderTask, Node* > LocalType;
444
445   // Reserve some memory inside the message queue
446   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
447
448   // Construct message in the message queue memory; note that delete should not be called on the return value
449   new (slot) LocalType( &task, &RenderTask::SetSourceNode, node );
450 }
451
452 inline void SetCameraMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode, const Camera* constCamera )
453 {
454   typedef MessageValue2< RenderTask, Node*, Camera* > LocalType;
455
456   Node* node = const_cast< Node* >( constNode );
457   Camera* camera = const_cast< Camera* >( constCamera );
458   // Reserve memory inside the message queue
459   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
460
461   // Construct message in the message queue memory; note that delete should not be called on the return value
462   new (slot) LocalType( &task, &RenderTask::SetCamera, node, camera );
463 }
464
465 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool exclusive )
466 {
467   typedef MessageValue1< RenderTask, bool > LocalType;
468
469   // Reserve some memory inside the message queue
470   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
471
472   // Construct message in the message queue memory; note that delete should not be called on the return value
473   new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
474 }
475
476 inline void SetSyncRequiredMessage(EventThreadServices& eventThreadServices, RenderTask& task, bool requiresSync )
477 {
478   typedef MessageValue1< RenderTask, bool > LocalType;
479
480   // Reserve some memory inside the message queue
481   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
482
483   // Construct message in the message queue memory; note that delete should not be called on the return value
484   new (slot) LocalType( &task, &RenderTask::SetSyncRequired, requiresSync );
485 }
486
487 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
488 {
489   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
490
491   // Reserve some memory inside the message queue
492   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
493
494   // Construct message in the message queue memory; note that delete should not be called on the return value
495   new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
496 }
497
498 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
499 {
500   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
501
502   // Reserve some 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::BakeViewportSize, value );
507 }
508
509 } // namespace SceneGraph
510
511 } // namespace Internal
512
513 } // namespace Dali
514
515 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H