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