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