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