1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H
5 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
43 class RenderInstruction;
44 class RenderMessageDispatcher;
45 class ResetterManager;
48 * RenderTasks describe how the Dali scene should be rendered.
50 class RenderTask : public PropertyOwner, public PropertyOwner::Observer, public NotifierInterface
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
62 * Create a new RenderTask
64 static RenderTask* New();
69 ~RenderTask() override;
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
76 void Initialize(ResetterManager& resetterManager, RenderMessageDispatcher& renderMessageDispatcher);
79 * Set the nodes to be rendered.
80 * @param[in] node This node and its children will be rendered.
82 void SetSourceNode(Node* node);
85 * Retrieve the source node.
86 * @return This node and its children will be rendered.
88 Node* GetSourceNode() const;
90 void SetStopperNode(Node* node);
91 Node* GetStopperNode() const;
94 * Set the ViewportGuideNode.
95 * @param[in] node This node is used to compute viewport of the render task.
97 void SetViewportGuideNode(Node* node);
100 * Retrieve the ViewportGuideNode.
101 * @return This node is used to compute viewport of the render task.
103 Node* GetViewportGuideNode() const;
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.
109 void SetExclusive(bool exclusive);
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.
115 bool IsExclusive() const;
118 * Set the camera from which the scene is viewed.
119 * @param[in] cameraNode that camera is connected with
121 void SetCamera(Camera* cameraNode);
124 * Set the frame-buffer used as a render target.
125 * @param[in] frameBuffer The framebuffer
127 void SetFrameBuffer(Render::FrameBuffer* frameBuffer);
130 * Retrieve the resource ID of the frame-buffer.
131 * @return The resource ID, or zero if not rendering off-screen.
133 Render::FrameBuffer* GetFrameBuffer();
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.
141 void UpdateViewport(BufferIndex updateBufferIndex, Vector2 sceneSize, Vector3 cameraPosition);
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
149 void SetViewportPosition(BufferIndex updateBufferIndex, const Vector2& value);
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.
157 const Vector2& GetViewportPosition(BufferIndex bufferIndex) const;
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.
165 void BakeViewportPosition(BufferIndex updateBufferIndex, const Vector2& value);
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
173 void SetViewportSize(BufferIndex updateBufferIndex, const Vector2& value);
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.
181 const Vector2& GetViewportSize(BufferIndex bufferIndex) const;
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.
189 void BakeViewportSize(BufferIndex updateBufferIndex, const Vector2& value);
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.
197 bool GetViewportEnabled(BufferIndex bufferIndex) const;
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
205 bool QueryViewport(BufferIndex bufferIndex, Viewport& viewport) const;
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
213 void SetClearColor(BufferIndex updateBufferIndex, const Vector4& value);
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.
221 const Vector4& GetClearColor(BufferIndex bufferIndex) const;
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.
229 void BakeClearColor(BufferIndex updateBufferIndex, const Vector4& value);
232 * @copydoc Dali::RenderTask::SetClearEnabled()
234 void SetClearEnabled(bool enabled);
237 * @copydoc Dali::RenderTask::GetClearEnabled()
239 bool GetClearEnabled() const;
242 * @copydoc Dali::RenderTask::SetCullMode()
244 void SetCullMode(bool mode);
247 * @copydoc Dali::RenderTask::GetCullMode()
249 bool GetCullMode() const;
252 * Set the refresh-rate of the RenderTask.
253 * @param[in] refreshRate The new refresh rate.
255 void SetRefreshRate(uint32_t refreshRate);
258 * Retrieve the refresh-rate of the RenderTask.
259 * @return The refresh rate.
261 uint32_t GetRefreshRate() const;
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.
268 bool ReadyToRender(BufferIndex updateBufferIndex);
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
275 bool IsRenderRequired();
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).
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
288 bool IsWaitingToRender();
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.
298 * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
300 uint32_t GetRenderedOnceCounter() const;
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.
308 const Matrix& GetViewMatrix(BufferIndex bufferIndex) const;
311 * @brief Retrieve the camera.
312 * @pre GetCameraNode() returns a node with valid Camera.
314 * @return The camera.
316 const SceneGraph::Camera& GetCamera() const;
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.
324 const Matrix& GetProjectionMatrix(BufferIndex bufferIndex) const;
327 * Prepares the render-instruction buffer to be populated with instructions.
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.
333 * @param[in] updateBufferIndex The current update buffer index.
334 * @return instruction to prepare
336 RenderInstruction& PrepareRenderInstruction(BufferIndex updateBufferIndex);
339 * @return true if the view matrix has been updated during this or last frame
341 bool ViewMatrixUpdated();
344 * Indicate whether GL sync is required for native render target.
345 * @param[in] requiresSync whether GL sync is required for native render target
347 void SetSyncRequired(bool requiresSync);
350 * Retrieve the render instruction.
351 * @param[in] updateBufferIndex The current update buffer index.
352 * @return The render instruction
354 RenderInstruction& GetRenderInstruction(BufferIndex updateBufferIndex)
356 return mRenderInstruction[updateBufferIndex];
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.
366 void SetRenderPassTag(uint32_t renderPassTag);
368 private: // from PropertyOwner::Observer
370 * @copydoc PropertyOwner::Observer::PropertyOwnerConnected( PropertyOwner& owner )
372 void PropertyOwnerConnected(PropertyOwner& owner) override;
375 * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected( BufferIndex updateBufferIndex, PropertyOwner& owner )
377 PropertyOwner::Observer::NotifyReturnType PropertyOwnerDisconnected(BufferIndex updateBufferIndex, PropertyOwner& owner) override;
380 * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed( PropertyOwner& owner )
382 void PropertyOwnerDestroyed(PropertyOwner& owner) override;
386 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::AddInitializeResetter
388 void AddInitializeResetter(ResetterManager& manager) const override;
391 void SetActiveStatus();
399 RenderTask(const RenderTask&) = delete;
400 RenderTask& operator=(const RenderTask&) = delete;
402 public: // Animatable Properties
403 AnimatableProperty<Vector2> mViewportPosition; ///< viewportPosition
404 AnimatableProperty<Vector2> mViewportSize; ///< viewportSize
405 AnimatableProperty<Vector4> mClearColor; ///< clearColor
408 ResetterManager* mResetterManager;
409 RenderMessageDispatcher* mRenderMessageDispatcher;
410 Render::RenderTracker* mRenderSyncTracker;
413 SceneGraph::Camera* mCameraNode;
414 Node* mViewportGuideNode;
415 Render::FrameBuffer* mFrameBuffer;
417 RenderInstruction mRenderInstruction[2]; ///< Owned double buffered render instruction. (Double buffered because this owns render commands for the currently drawn frame)
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
423 State mState; ///< Render state.
425 uint32_t mRenderPassTag{0u};
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
436 } // namespace SceneGraph
438 } // namespace Internal
442 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H