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