[dali_2.3.23] Merge branch '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) 2023 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/event/common/notifier-interface.h>
23 #include <dali/internal/render/common/render-instruction.h>
24 #include <dali/internal/render/renderers/render-frame-buffer.h>
25 #include <dali/internal/update/common/animatable-property.h>
26 #include <dali/internal/update/common/property-owner.h>
27 #include <dali/public-api/math/viewport.h>
28 #include <dali/public-api/render-tasks/render-task.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 namespace Render
35 {
36 class RenderTracker;
37 }
38
39 namespace SceneGraph
40 {
41 class Node;
42 class Camera;
43 class RenderInstruction;
44 class RenderMessageDispatcher;
45 class ResetterManager;
46
47 /**
48  * RenderTasks describe how the Dali scene should be rendered.
49  */
50 class RenderTask : public PropertyOwner, public PropertyOwner::Observer, public NotifierInterface
51 {
52 public:
53   enum State : uint8_t
54   {
55     RENDER_CONTINUOUSLY,               ///< mRefreshRate > 0
56     RENDER_ONCE_WAITING_FOR_RESOURCES, ///< mRefreshRate = REFRESH_ONCE
57     RENDERED_ONCE,                     ///< mRefreshRate = REFRESH_ONCE & rendered
58     RENDERED_ONCE_AND_NOTIFIED         ///< mRefreshRate = REFRESH_ONCE & rendered & notified
59   };
60
61   /**
62    * Create a new RenderTask
63    */
64   static RenderTask* New();
65
66   /**
67    * Virtual destructor
68    */
69   ~RenderTask() override;
70
71   /**
72    * Initialize the render task. Called in update thread
73    * @param[in] resetterManager to send resetter
74    * @param[in] renderMessageDispatcher to send messages to render thread
75    */
76   void Initialize(ResetterManager& resetterManager, 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   void SetStopperNode(Node* node);
91   Node* GetStopperNode() const;
92
93   /**
94    * Set the ViewportGuideNode.
95    * @param[in] node This node is used to compute viewport of the render task.
96    */
97   void SetViewportGuideNode(Node* node);
98
99   /**
100    * Retrieve the ViewportGuideNode.
101    * @return This node is used to compute viewport of the render task.
102    */
103   Node* GetViewportGuideNode() const;
104
105   /**
106    * Set whether the RenderTask has exclusive access to the source nodes.
107    * @param[in] exclusive True if the source nodes will only be rendered by this render-task.
108    */
109   void SetExclusive(bool exclusive);
110
111   /**
112    * Query whether the RenderTask has exclusive access to the source actors.
113    * @return True if the source actors will only be rendered by this render-task.
114    */
115   bool IsExclusive() const;
116
117   /**
118    * Set the camera from which the scene is viewed.
119    * @param[in] cameraNode that camera is connected with
120    */
121   void SetCamera(Camera* cameraNode);
122
123   /**
124    * Set the frame-buffer used as a render target.
125    * @param[in] frameBuffer The framebuffer
126    */
127   void SetFrameBuffer(Render::FrameBuffer* frameBuffer);
128
129   /**
130    * Retrieve the resource ID of the frame-buffer.
131    * @return The resource ID, or zero if not rendering off-screen.
132    */
133   Render::FrameBuffer* GetFrameBuffer();
134
135   /**
136    * Update viewport by using viewport guide node
137    * @param[in] updateBufferIndex The current update buffer index.
138    * @param[in] sceneSize The size of scene.
139    * @param[in] cameraPosition The position of default camera of the scene.
140    */
141   void UpdateViewport(BufferIndex updateBufferIndex, Vector2 sceneSize, Vector3 cameraPosition);
142
143   /**
144    * Set the value of property viewportPosition
145    * This value will persist only for the current frame.
146    * @param[in] updateBufferIndex The current update buffer index.
147    * @param[in] value The value of the property
148    */
149   void SetViewportPosition(BufferIndex updateBufferIndex, const Vector2& value);
150
151   /**
152    * Get the value of property viewportPosition
153    * @warning Should only be called from the Update thread
154    * @param[in] bufferIndex The buffer to read from.
155    * @return the value of the property.
156    */
157   const Vector2& GetViewportPosition(BufferIndex bufferIndex) const;
158
159   /**
160    * Bake the value of the property viewportPosition
161    * This will also set the base value
162    * @param[in] updateBufferIndex The current update buffer index.
163    * @param[in] value The new value for property.
164    */
165   void BakeViewportPosition(BufferIndex updateBufferIndex, const Vector2& value);
166
167   /**
168    * Set the value of property viewportSize
169    * This value will persist only for the current frame.
170    * @param[in] updateBufferIndex The current update buffer index.
171    * @param[in] value The value of the property
172    */
173   void SetViewportSize(BufferIndex updateBufferIndex, const Vector2& value);
174
175   /**
176    * Get the value of property viewportSize
177    * @warning Should only be called from the Update thread
178    * @param[in] bufferIndex The buffer to read from.
179    * @return the value of the property.
180    */
181   const Vector2& GetViewportSize(BufferIndex bufferIndex) const;
182
183   /**
184    * Bake the value of the property viewportSize
185    * This will also set the base value
186    * @param[in] updateBufferIndex The current update buffer index.
187    * @param[in] value The new value for property.
188    */
189   void BakeViewportSize(BufferIndex updateBufferIndex, const Vector2& value);
190
191   /**
192    * Get the value of property viewportEnabled
193    * @warning Should only be called from the Update thread
194    * @param[in] bufferIndex The buffer to read from.
195    * @return the value of the property.
196    */
197   bool GetViewportEnabled(BufferIndex bufferIndex) const;
198
199   /**
200    * Query whether the optional viewport is set.
201    * @param[in] bufferIndex The buffer to read from.
202    * @param[out] viewport The viewport position and size is populated.
203    * @return true if the viewport has been set
204    */
205   bool QueryViewport(BufferIndex bufferIndex, Viewport& viewport) const;
206
207   /**
208    * Set the value of property clearColor
209    * This value will persist only for the current frame.
210    * @param[in] updateBufferIndex The current update buffer index.
211    * @param[in] value The value of the property
212    */
213   void SetClearColor(BufferIndex updateBufferIndex, const Vector4& value);
214
215   /**
216    * Get the value of property clearColor
217    * @warning Should only be called from the Update thread
218    * @param[in] bufferIndex The buffer to read from.
219    * @return the value of the property.
220    */
221   const Vector4& GetClearColor(BufferIndex bufferIndex) const;
222
223   /**
224    * Bake the value of the property clearColor
225    * This will also set the base value
226    * @param[in] updateBufferIndex The current update buffer index.
227    * @param[in] value The new value for property.
228    */
229   void BakeClearColor(BufferIndex updateBufferIndex, const Vector4& value);
230
231   /**
232    * @copydoc Dali::RenderTask::SetClearEnabled()
233    */
234   void SetClearEnabled(bool enabled);
235
236   /**
237    * @copydoc Dali::RenderTask::GetClearEnabled()
238    */
239   bool GetClearEnabled() const;
240
241   /**
242    * @copydoc Dali::RenderTask::SetCullMode()
243    */
244   void SetCullMode(bool mode);
245
246   /**
247    * @copydoc Dali::RenderTask::GetCullMode()
248    */
249   bool GetCullMode() const;
250
251   /**
252    * Set the refresh-rate of the RenderTask.
253    * @param[in] refreshRate The new refresh rate.
254    */
255   void SetRefreshRate(uint32_t refreshRate);
256
257   /**
258    * Retrieve the refresh-rate of the RenderTask.
259    * @return The refresh rate.
260    */
261   uint32_t GetRefreshRate() const;
262
263   /**
264    * Check if the render task is ready for rendering.
265    * @param[in] updateBufferIndex The current update buffer index.
266    * @return True if the render-task is ready for rendering.
267    */
268   bool ReadyToRender(BufferIndex updateBufferIndex);
269
270   /**
271    * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
272    * 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.
273    * @return true if a render is required
274    */
275   bool IsRenderRequired();
276
277   /**
278    * Process a frame. This method is called each frame for every ready render task, regardless
279    * of whether it needs to render (so that the frame counter can be updated).
280    */
281   void UpdateState();
282
283   /**
284    * Return true only if currently waiting for the render task to
285    * finish rendering and the update thread should be kept alive.
286    * @return true if waiting to be rendered
287    */
288   bool IsWaitingToRender();
289
290   /**
291    * Return true when the render task has finished rendering and a notification
292    * needs sending. (Only one notification is sent per render once request)
293    * @return true if notification is required.
294    */
295   bool HasRendered();
296
297   /**
298    * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
299    */
300   uint32_t GetRenderedOnceCounter() const;
301
302   /**
303    * Retrieve the view-matrix; this is double buffered for input handling.
304    * @pre GetCameraNode() returns a node with valid Camera.
305    * @param[in] bufferIndex The buffer to read from.
306    * @return The view-matrix.
307    */
308   const Matrix& GetViewMatrix(BufferIndex bufferIndex) const;
309
310   /**
311    * @brief Retrieve the camera.
312    * @pre GetCameraNode() returns a node with valid Camera.
313    *
314    * @return The camera.
315    */
316   const SceneGraph::Camera& GetCamera() const;
317
318   /**
319    * Retrieve the projection-matrix; this is double buffered for input handling.
320    * @pre GetCameraNode() returns a node with valid Camera.
321    * @param[in] bufferIndex The buffer to read from.
322    * @return The projection-matrix.
323    */
324   const Matrix& GetProjectionMatrix(BufferIndex bufferIndex) const;
325
326   /**
327    * Prepares the render-instruction buffer to be populated with instructions.
328    *
329    * If the render task is a render-once framebuffer backed by a native image,
330    * then this method will ensure that a GL sync object is created to track
331    * when the rendering has finished.
332    *
333    * @param[in] updateBufferIndex The current update buffer index.
334    * @return instruction to prepare
335    */
336   RenderInstruction& PrepareRenderInstruction(BufferIndex updateBufferIndex);
337
338   /**
339    * @return true if the view matrix has been updated during this or last frame
340    */
341   bool ViewMatrixUpdated();
342
343   /**
344    * Indicate whether GL sync is required for native render target.
345    * @param[in] requiresSync whether GL sync is required for native render target
346    */
347   void SetSyncRequired(bool requiresSync);
348
349   /**
350    * Retrieve the render instruction.
351    * @param[in] updateBufferIndex The current update buffer index.
352    * @return The render instruction
353    */
354   RenderInstruction& GetRenderInstruction(BufferIndex updateBufferIndex)
355   {
356     return mRenderInstruction[updateBufferIndex];
357   }
358
359   /**
360    * Sets Render Pass key for this RenderTask.
361    * Shader code that matches this render pass is used for rendering.
362    * If no matching shader is found, the code with a render pass of 0 is used.
363    * In other cases, operation is not guaranteed.
364    * @param[in] renderPassTag RenderPassTag value for this render task.
365    */
366   void SetRenderPassTag(uint32_t renderPassTag);
367
368 private: // from PropertyOwner::Observer
369   /**
370    * @copydoc PropertyOwner::Observer::PropertyOwnerConnected( PropertyOwner& owner )
371    */
372   void PropertyOwnerConnected(PropertyOwner& owner) override;
373
374   /**
375    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected( BufferIndex updateBufferIndex, PropertyOwner& owner )
376    */
377   void PropertyOwnerDisconnected(BufferIndex updateBufferIndex, PropertyOwner& owner) override;
378
379   /**
380    * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed( PropertyOwner& owner )
381    */
382   void PropertyOwnerDestroyed(PropertyOwner& owner) override;
383
384 public:
385   /**
386    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::AddInitializeResetter
387    */
388   void AddInitializeResetter(ResetterManager& manager) const override;
389
390 private:
391   void SetActiveStatus();
392
393   /**
394    * Constructor.
395    */
396   RenderTask();
397
398   // Undefined
399   RenderTask(const RenderTask&)            = delete;
400   RenderTask& operator=(const RenderTask&) = delete;
401
402 public:                                          // Animatable Properties
403   AnimatableProperty<Vector2> mViewportPosition; ///< viewportPosition
404   AnimatableProperty<Vector2> mViewportSize;     ///< viewportSize
405   AnimatableProperty<Vector4> mClearColor;       ///< clearColor
406
407 private:
408   ResetterManager*         mResetterManager;
409   RenderMessageDispatcher* mRenderMessageDispatcher;
410   Render::RenderTracker*   mRenderSyncTracker;
411   Node*                    mSourceNode;
412   Node*                    mStopperNode;
413   SceneGraph::Camera*      mCameraNode;
414   Node*                    mViewportGuideNode;
415   Render::FrameBuffer*     mFrameBuffer;
416
417   RenderInstruction mRenderInstruction[2]; ///< Owned double buffered render instruction. (Double buffered because this owns render commands for the currently drawn frame)
418
419   uint32_t mRefreshRate;                   ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
420   uint32_t mFrameCounter;                  ///< counter for rendering every N frames
421   uint32_t mRenderedOnceCounter;           ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
422
423   State mState;                            ///< Render state.
424
425   uint32_t mRenderPassTag{0u};
426
427   bool mRequiresSync : 1;    ///< Whether sync is needed to track the render
428   bool mActive : 1;          ///< True when the task is active, i.e. has valid source and camera
429   bool mWaitingToRender : 1; ///< True when an render once to FBO is waiting
430   bool mNotifyTrigger : 1;   ///< True if a render once render task has finished renderering
431   bool mExclusive : 1;       ///< Whether the render task has exclusive access to the source actor (node in the scene graph).
432   bool mClearEnabled : 1;    ///< Whether previous results are cleared.
433   bool mCullMode : 1;        ///< Whether renderers should be frustum culled
434 };
435
436 } // namespace SceneGraph
437
438 } // namespace Internal
439
440 } // namespace Dali
441
442 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H