Minor code cleans for render task
[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    */
117   void SetCamera(Camera* cameraNode);
118
119   /**
120    * Set the frame-buffer used as a render target.
121    * @param[in] frameBuffer The framebuffer
122    */
123   void SetFrameBuffer(Render::FrameBuffer* frameBuffer);
124
125   /**
126    * Retrieve the resource ID of the frame-buffer.
127    * @return The resource ID, or zero if not rendering off-screen.
128    */
129   Render::FrameBuffer* GetFrameBuffer();
130
131   /**
132    * Update viewport by using viewport guide node
133    * @param[in] updateBufferIndex The current update buffer index.
134    * @param[in] sceneSize The size of scene.
135    * @param[in] cameraPosition The position of default camera of the scene.
136    */
137   void UpdateViewport(BufferIndex updateBufferIndex, Vector2 sceneSize, Vector3 cameraPosition);
138
139   /**
140    * Set the value of property viewportPosition
141    * This value will persist only for the current frame.
142    * @param[in] updateBufferIndex The current update buffer index.
143    * @param[in] value The value of the property
144    */
145   void SetViewportPosition(BufferIndex updateBufferIndex, const Vector2& value);
146
147   /**
148    * Get the value of property viewportPosition
149    * @warning Should only be called from the Update thread
150    * @param[in] bufferIndex The buffer to read from.
151    * @return the value of the property.
152    */
153   const Vector2& GetViewportPosition(BufferIndex bufferIndex) const;
154
155   /**
156    * Bake the value of the property viewportPosition
157    * This will also set the base value
158    * @param[in] updateBufferIndex The current update buffer index.
159    * @param[in] value The new value for property.
160    */
161   void BakeViewportPosition(BufferIndex updateBufferIndex, const Vector2& value);
162
163   /**
164    * Set the value of property viewportSize
165    * This value will persist only for the current frame.
166    * @param[in] updateBufferIndex The current update buffer index.
167    * @param[in] value The value of the property
168    */
169   void SetViewportSize(BufferIndex updateBufferIndex, const Vector2& value);
170
171   /**
172    * Get the value of property viewportSize
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   const Vector2& GetViewportSize(BufferIndex bufferIndex) const;
178
179   /**
180    * Bake the value of the property viewportSize
181    * This will also set the base value
182    * @param[in] updateBufferIndex The current update buffer index.
183    * @param[in] value The new value for property.
184    */
185   void BakeViewportSize(BufferIndex updateBufferIndex, const Vector2& value);
186
187   /**
188    * Get the value of property viewportEnabled
189    * @warning Should only be called from the Update thread
190    * @param[in] bufferIndex The buffer to read from.
191    * @return the value of the property.
192    */
193   bool GetViewportEnabled(BufferIndex bufferIndex) const;
194
195   /**
196    * Query whether the optional viewport is set.
197    * @param[in] bufferIndex The buffer to read from.
198    * @param[out] viewport The viewport position and size is populated.
199    * @return true if the viewport has been set
200    */
201   bool QueryViewport(BufferIndex bufferIndex, Viewport& viewport) const;
202
203   /**
204    * Set the value of property clearColor
205    * This value will persist only for the current frame.
206    * @param[in] updateBufferIndex The current update buffer index.
207    * @param[in] value The value of the property
208    */
209   void SetClearColor(BufferIndex updateBufferIndex, const Vector4& value);
210
211   /**
212    * Get the value of property clearColor
213    * @warning Should only be called from the Update thread
214    * @param[in] bufferIndex The buffer to read from.
215    * @return the value of the property.
216    */
217   const Vector4& GetClearColor(BufferIndex bufferIndex) const;
218
219   /**
220    * Bake the value of the property clearColor
221    * This will also set the base value
222    * @param[in] updateBufferIndex The current update buffer index.
223    * @param[in] value The new value for property.
224    */
225   void BakeClearColor(BufferIndex updateBufferIndex, const Vector4& value);
226
227   /**
228    * @copydoc Dali::RenderTask::SetClearEnabled()
229    */
230   void SetClearEnabled(bool enabled);
231
232   /**
233    * @copydoc Dali::RenderTask::GetClearEnabled()
234    */
235   bool GetClearEnabled() const;
236
237   /**
238    * @copydoc Dali::RenderTask::SetCullMode()
239    */
240   void SetCullMode(bool mode);
241
242   /**
243    * @copydoc Dali::RenderTask::GetCullMode()
244    */
245   bool GetCullMode() const;
246
247   /**
248    * Set the refresh-rate of the RenderTask.
249    * @param[in] refreshRate The new refresh rate.
250    */
251   void SetRefreshRate(uint32_t refreshRate);
252
253   /**
254    * Retrieve the refresh-rate of the RenderTask.
255    * @return The refresh rate.
256    */
257   uint32_t GetRefreshRate() const;
258
259   /**
260    * Check if the render task is ready for rendering.
261    * @param[in] updateBufferIndex The current update buffer index.
262    * @return True if the render-task is ready for rendering.
263    */
264   bool ReadyToRender(BufferIndex updateBufferIndex);
265
266   /**
267    * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
268    * 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.
269    * @return true if a render is required
270    */
271   bool IsRenderRequired();
272
273   /**
274    * Process a frame. This method is called each frame for every ready render task, regardless
275    * of whether it needs to render (so that the frame counter can be updated).
276    */
277   void UpdateState();
278
279   /**
280    * Return true only if currently waiting for the render task to
281    * finish rendering and the update thread should be kept alive.
282    * @return true if waiting to be rendered
283    */
284   bool IsWaitingToRender();
285
286   /**
287    * Return true when the render task has finished rendering and a notification
288    * needs sending. (Only one notification is sent per render once request)
289    * @return true if notification is required.
290    */
291   bool HasRendered();
292
293   /**
294    * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
295    */
296   uint32_t GetRenderedOnceCounter() const;
297
298   /**
299    * Retrieve the view-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 view-matrix.
303    */
304   const Matrix& GetViewMatrix(BufferIndex bufferIndex) const;
305
306   /**
307    * @brief Retrieve the camera.
308    * @pre GetCameraNode() returns a node with valid Camera.
309    *
310    * @return The camera.
311    */
312   const SceneGraph::Camera& GetCamera() const;
313
314   /**
315    * Retrieve the projection-matrix; this is double buffered for input handling.
316    * @pre GetCameraNode() returns a node with valid Camera.
317    * @param[in] bufferIndex The buffer to read from.
318    * @return The projection-matrix.
319    */
320   const Matrix& GetProjectionMatrix(BufferIndex bufferIndex) const;
321
322   /**
323    * Prepares the render-instruction buffer to be populated with instructions.
324    *
325    * If the render task is a render-once framebuffer backed by a native image,
326    * then this method will ensure that a GL sync object is created to track
327    * when the rendering has finished.
328    *
329    * @param[in] updateBufferIndex The current update buffer index.
330    * @return instruction to prepare
331    */
332   RenderInstruction& PrepareRenderInstruction(BufferIndex updateBufferIndex);
333
334   /**
335    * @return true if the view matrix has been updated during this or last frame
336    */
337   bool ViewMatrixUpdated();
338
339   /**
340    * Indicate whether GL sync is required for native render target.
341    * @param[in] requiresSync whether GL sync is required for native render target
342    */
343   void SetSyncRequired(bool requiresSync);
344
345   /**
346    * Retrieve the render instruction.
347    * @param[in] updateBufferIndex The current update buffer index.
348    * @return The render instruction
349    */
350   RenderInstruction& GetRenderInstruction(BufferIndex updateBufferIndex)
351   {
352     return mRenderInstruction[updateBufferIndex];
353   }
354
355 private: // from PropertyOwner::Observer
356   /**
357    * @copydoc PropertyOwner::Observer::PropertyOwnerConnected( PropertyOwner& owner )
358    */
359   void PropertyOwnerConnected(PropertyOwner& owner) override;
360
361   /**
362    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected( BufferIndex updateBufferIndex, PropertyOwner& owner )
363    */
364   void PropertyOwnerDisconnected(BufferIndex updateBufferIndex, PropertyOwner& owner) override;
365
366   /**
367    * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed( PropertyOwner& owner )
368    */
369   void PropertyOwnerDestroyed(PropertyOwner& owner) override;
370
371 public:
372   /**
373    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::AddInitializeResetter
374    */
375   void AddInitializeResetter(ResetterManager& manager) const override;
376
377 private:
378   void SetActiveStatus();
379
380   /**
381    * Constructor.
382    */
383   RenderTask();
384
385   // Undefined
386   RenderTask(const RenderTask&) = delete;
387   RenderTask& operator=(const RenderTask&) = delete;
388
389 public:                                          // Animatable Properties
390   AnimatableProperty<Vector2> mViewportPosition; ///< viewportPosition
391   AnimatableProperty<Vector2> mViewportSize;     ///< viewportSize
392   AnimatableProperty<Vector4> mClearColor;       ///< clearColor
393
394 private:
395   ResetterManager*         mResetterManager;
396   RenderMessageDispatcher* mRenderMessageDispatcher;
397   Render::RenderTracker*   mRenderSyncTracker;
398   Node*                    mSourceNode;
399   SceneGraph::Camera*      mCameraNode;
400   Node*                    mViewportGuideNode;
401   Render::FrameBuffer*     mFrameBuffer;
402
403   RenderInstruction mRenderInstruction[2]; ///< Owned double buffered render instruction. (Double buffered because this owns render commands for the currently drawn frame)
404
405   uint32_t mRefreshRate;         ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
406   uint32_t mFrameCounter;        ///< counter for rendering every N frames
407   uint32_t mRenderedOnceCounter; ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
408
409   State mState; ///< Render state.
410
411   bool mRequiresSync : 1;    ///< Whether sync is needed to track the render
412   bool mActive : 1;          ///< True when the task is active, i.e. has valid source and camera
413   bool mWaitingToRender : 1; ///< True when an render once to FBO is waiting
414   bool mNotifyTrigger : 1;   ///< True if a render once render task has finished renderering
415   bool mExclusive : 1;       ///< Whether the render task has exclusive access to the source actor (node in the scene graph).
416   bool mClearEnabled : 1;    ///< Whether previous results are cleared.
417   bool mCullMode : 1;        ///< Whether renderers should be frustum culled
418 };
419
420 } // namespace SceneGraph
421
422 } // namespace Internal
423
424 } // namespace Dali
425
426 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H