1 #ifndef DALI_RENDER_TASK_H
2 #define DALI_RENDER_TASK_H
5 * Copyright (c) 2020 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 <cstdint> // uint32_t
25 #include <dali/public-api/math/viewport.h>
26 #include <dali/public-api/object/handle.h>
27 #include <dali/public-api/object/property-index-ranges.h>
28 #include <dali/public-api/signals/dali-signal.h>
33 * @addtogroup dali_core_rendering_effects
39 class FrameBufferImage;
43 namespace Internal DALI_INTERNAL
49 * @brief RenderTasks describe how the Dali scene should be rendered.
51 * The Scene provides access to an ordered list of render-tasks.
53 * Each RenderTask must specify the source actors to be rendered, and a camera actor from
54 * which the scene is viewed.
56 * RenderTasks may optionally target a frame-buffer, otherwise the default GL surface is used;
57 * typically this is a window provided by the native system.
59 * By default Dali provides a single RenderTask, which renders the entire actor hierarchy using
60 * a default camera actor and GL surface. If stereoscopic rendering is enabled, Dali will create
61 * two additional render tasks, on for each eye. Each render task will have its own camera parented
62 * to the default camera actor.
64 * The first RenderTask used for input handling will be the last one rendered, which also has input enabled,
65 * and has a valid source & camera actor; see SetInputEnabled().
67 * If none of the actors are hit in the last RenderTask rendered, then input handling will continue
68 * with the second last RenderTask rendered, and so on.
70 * All RenderTasks which target a frame-buffer (i.e. off screen) will be rendered before all RenderTasks
71 * which target the default GL surface. This allows the user to render intermediate targets which are used
72 * later when targeting the screen.
74 * A RenderTask targeting a frame-buffer can still be hit-tested, provided that the
75 * screen->frame-buffer coordinate conversion is successful; see SetScreenToFrameBufferFunction().
77 * If the refresh rate id REFRESH_ONCE and a "Finish" signal is connected, it will be emitted when the RenderTask is completed.
78 * Note that all connected signals must be disconnected before the object is destroyed. This is typically done in the
79 * object destructor, and requires either the Dali::Connection object or Dali::RenderTask handle to be stored.
82 * | %Signal Name | Method |
83 * |--------------|-----------------------|
84 * | finished | @ref FinishedSignal() |
87 class DALI_CORE_API RenderTask : public Handle
91 * @brief Enumeration for instances of properties belonging to the RenderTask class.
97 * @brief Enumeration for instances of properties belonging to the RenderTask class.
103 * @brief name "viewportPosition", type Vector2
106 VIEWPORT_POSITION = DEFAULT_OBJECT_PROPERTY_START_INDEX,
108 * @brief name "viewportSize", type Vector2
113 * @brief name "clearColor", type Vector4
118 * @brief name "requiresSync", type BOOLEAN
119 * @details By default, the sync object is not created.
120 * When native image source is used as render target, in order to track when the render to pixmap is completed, the GL sync should be enabled.
121 * Thus the RENDER_ONCE finished signal can be emit at the correct timing.
123 * @note The use of GL sync might cause deadlock with multiple access to the single pixmap happening in the same time.
130 * @brief Typedef for signals sent by this class.
133 using RenderTaskSignalType = Signal<void(RenderTask&)>;
136 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
138 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen
139 * @return True if the conversion was successful, otherwise coordinates should be unmodified
141 using ScreenToFrameBufferFunction = bool (*)(Vector2&);
144 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
146 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen
147 * @return True if the conversion was successful, otherwise coordinates should be unmodified
149 using ConstScreenToFrameBufferFunction = bool (*const)(Vector2&);
152 * @brief The default conversion function returns false for any screen coordinates.
154 * This effectively disables hit-testing for RenderTasks rendering to a frame buffer.
155 * See also FULLSCREEN_FRAMEBUFFER_FUNCTION below.
157 static ConstScreenToFrameBufferFunction DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION;
160 * @brief This conversion function outputs the (unmodified) screen coordinates as frame-buffer coordinates.
162 * Therefore the contents of an off-screen image is expected to be rendered "full screen".
164 static ConstScreenToFrameBufferFunction FULLSCREEN_FRAMEBUFFER_FUNCTION;
167 * @brief Enumeration for the refresh-rate of the RenderTask.
172 REFRESH_ONCE = 0, ///< Process once only e.g. take a snap-shot of the scene. @SINCE_1_0.0
173 REFRESH_ALWAYS = 1 ///< Process every frame. @SINCE_1_0.0
176 static const bool DEFAULT_EXCLUSIVE; ///< false
177 static const bool DEFAULT_INPUT_ENABLED; ///< true
178 static const Vector4 DEFAULT_CLEAR_COLOR; ///< Color::BLACK
179 static const bool DEFAULT_CLEAR_ENABLED; ///< false
180 static const bool DEFAULT_CULL_MODE; ///< true
181 static const uint32_t DEFAULT_REFRESH_RATE; ///< REFRESH_ALWAYS
184 * @brief Creates an empty RenderTask handle.
186 * This can be initialised with RenderTaskList::CreateRenderTask().
192 * @brief Downcasts a handle to RenderTask handle.
194 * If handle points to a RenderTask, the
195 * downcast produces valid handle. If not, the returned handle is left uninitialized.
197 * @param[in] handle A handle to an object
198 * @return A handle to a RenderTask or an uninitialized handle
200 static RenderTask DownCast(BaseHandle handle);
205 * This is non-virtual since derived Handle types must not contain data or virtual methods.
211 * @brief This copy constructor is required for (smart) pointer semantics.
214 * @param[in] handle A reference to the copied handle
216 RenderTask(const RenderTask& handle);
219 * @brief This assignment operator is required for (smart) pointer semantics.
222 * @param[in] rhs A reference to the copied handle
223 * @return A reference to this
225 RenderTask& operator=(const RenderTask& rhs);
228 * @brief Move constructor.
231 * @param[in] rhs A reference to the moved handle
233 RenderTask(RenderTask&& rhs) noexcept;
236 * @brief Move assignment operator.
239 * @param[in] rhs A reference to the moved handle
240 * @return A reference to this
242 RenderTask& operator=(RenderTask&& rhs) noexcept;
245 * @brief Sets the actors to be rendered.
247 * @param[in] actor This actor and its children will be rendered.
248 * If actor is an empty handle, then nothing will be rendered
250 void SetSourceActor(Actor actor);
253 * @brief Retrieves the actors to be rendered.
255 * @return This actor and its children will be rendered
257 Actor GetSourceActor() const;
259 Actor GetStopperActor() const;
262 * @brief Sets whether the RenderTask has exclusive access to the source actors; the default is false.
264 * @param[in] exclusive True if the source actors will only be rendered by this render-task
266 void SetExclusive(bool exclusive);
269 * @brief Queries whether the RenderTask has exclusive access to the source actors.
271 * @return True if the source actors will only be rendered by this render-task
273 bool IsExclusive() const;
276 * @brief Sets whether the render-task should be considered for input handling; the default is true.
278 * The task used for input handling will be last task in the RenderTaskList which has input enabled,
279 * and has a valid source & camera actor.
280 * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the screen->frame-buffer
281 * coordinate conversion is successful; see also SetScreenToFrameBufferFunction().
283 * @param[in] enabled True if the render-task should be considered for input handling
285 void SetInputEnabled(bool enabled);
288 * @brief Queries whether the render-task should be considered for input handling.
290 * @return True if the render-task should be considered for input handling
292 bool GetInputEnabled() const;
295 * @brief Sets the actor from which the scene is viewed.
297 * @param[in] cameraActor The scene is viewed from the perspective of this actor
299 void SetCameraActor(CameraActor cameraActor);
302 * @brief Retrieves the actor from which the scene is viewed.
304 * @return The scene is viewed from the perspective of this actor
306 CameraActor GetCameraActor() const;
309 * @brief Sets the frame-buffer used as a render target.
311 * @param[in] frameBuffer A valid FrameBuffer handle to enable off-screen rendering, or an uninitialized handle to disable it
313 void SetFrameBuffer(FrameBuffer frameBuffer);
316 * @brief Retrieves the frame-buffer used as a render target.
318 * @return The framebuffer
320 FrameBuffer GetFrameBuffer() const;
323 * @brief Sets the function used to convert screen coordinates to frame-buffer coordinates.
325 * This is useful for hit-testing actors which are rendered off-screen.
327 * @param[in] conversionFunction The conversion function
329 void SetScreenToFrameBufferFunction(ScreenToFrameBufferFunction conversionFunction);
332 * @brief Retrieves the function used to convert screen coordinates to frame-buffer coordinates.
334 * @return The conversion function
336 ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
339 * @brief Sets the actor used to convert screen coordinates to frame-buffer coordinates.
341 * The local coordinates of the actor are mapped as frame-buffer coordinates.
342 * This is useful for hit-testing actors which are rendered off-screen.
344 * @param[in] mappingActor The actor used for conversion
345 * @note The mapping actor needs to be rendered by the default render task to make the mapping work properly.
347 void SetScreenToFrameBufferMappingActor(Actor mappingActor);
350 * @brief Retrieves the actor used to convert screen coordinates to frame-buffer coordinates.
352 * @return The actor used for conversion
354 Actor GetScreenToFrameBufferMappingActor() const;
357 * @brief Sets the actor to compute viewport of this render task.
358 * Actor should be added on Scene.
360 * @param[in] actor This actor is used to compute viewport of the render task.
361 * @note If window default camera is rotated and the actor is no longer a rectangle on the screen, Viewport may be computed incorrectly.
362 * The Viewport properties VIEWPORT_POSITION and VIEWPORT_SIZE is kept during using ViewportGuideActor, but only current value is changed.
364 void SetViewportGuideActor(Actor actor);
367 * @brief Retrieves the actor to compute viewport of this render task.
369 * @return This actor is used to compute viewport of the render task.
371 Actor GetViewportGuideActor() const;
374 * @brief Resets the actor to compute viewport of this render task.
376 * @note The Viewport properties VIEWPORT_POSITION and VIEWPORT_SIZE is still kept.
378 void ResetViewportGuideActor();
381 * @brief Sets the GL viewport position used when rendering.
383 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
384 * By default this will match the target window or frame-buffer size.
386 * @param[in] position The viewports position (x,y)
387 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
389 void SetViewportPosition(Vector2 position);
392 * @brief Retrieves the GL viewport position used when rendering.
394 * @return The viewport
396 Vector2 GetCurrentViewportPosition() const;
399 * @brief Sets the GL viewport size used when rendering.
401 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
402 * By default this will match the target window or frame-buffer size.
404 * @param[in] size The viewports size (width,height)
406 void SetViewportSize(Vector2 size);
409 * @brief Retrieves the GL viewport size used when rendering.
411 * @return The viewport
413 Vector2 GetCurrentViewportSize() const;
416 * @brief Sets the GL viewport used when rendering.
418 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
419 * By default this will match the target window or frame-buffer size.
421 * @param[in] viewport The new viewport
422 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
424 void SetViewport(Viewport viewport);
427 * @brief Retrieves the GL viewport used when rendering.
429 * @return The viewport
431 Viewport GetViewport() const;
434 * @brief Sets the clear color used when SetClearEnabled(true) is used.
436 * @param[in] color The new clear color
438 void SetClearColor(const Vector4& color);
441 * @brief Retrieves the clear color used when SetClearEnabled(true) is used.
443 * @return The clear color
444 * @note This property can be animated; the return value may not match the value written with SetClearColor().
446 Vector4 GetClearColor() const;
449 * @brief Sets whether the render-task will clear the results of previous render-tasks.
451 * The default is false.
454 * @param[in] enabled True if the render-task should clear
455 * @note The default GL surface is cleared automatically at the
456 * beginning of each frame; this setting is only useful when 2+
457 * render-tasks are used, and the result of the first task needs to
458 * be (partially) cleared before rendering the second.
461 void SetClearEnabled(bool enabled);
464 * @brief Queries whether the render-task will clear the results of previous render-tasks.
466 * @return True if the render-task should clear
468 bool GetClearEnabled() const;
471 * @brief Sets whether the render task will cull the actors to the camera's view frustum.
474 * @param[in] cullMode True if the renderers should be culled
475 * @note The default mode is to cull actors.
476 * @note If the shader uses @ref Shader::Hint::MODIFIES_GEOMETRY then culling optimizations are disabled.
479 void SetCullMode(bool cullMode);
482 * @brief Gets the cull mode.
485 * @return True if the render task should cull the actors to the camera's view frustum
487 bool GetCullMode() const;
490 * @brief Sets the refresh-rate of the RenderTask.
492 * The default is REFRESH_ALWAYS (1), meaning that the RenderTask
493 * will be processed every frame if the scene graph is changing. It
494 * may be desirable to process less frequently. For example,
495 * SetRefreshRate(3) will process once every 3 frames if the scene
496 * graph is changing. If the scene graph is not changing, then the
497 * render task will not be rendered, regardless of this value.
499 * The REFRESH_ONCE value means that the RenderTask will be
500 * processed once only, to take a snap-shot of the scene.
501 * Repeatedly calling SetRefreshRate(REFRESH_ONCE) will cause more
502 * snap-shots to be taken.
505 * @param[in] refreshRate The new refresh rate
507 void SetRefreshRate(uint32_t refreshRate);
510 * @brief Queries the refresh-rate of the RenderTask.
512 * @return The refresh-rate
514 uint32_t GetRefreshRate() const;
517 * @brief Gets viewport coordinates for given world position.
520 * @param[in] position The world position
521 * @param[out] viewportX The viewport x position
522 * @param[out] viewportY The viewport y position
523 * @return true if the position has a screen coordinate
525 bool WorldToViewport(const Vector3& position, float& viewportX, float& viewportY) const;
528 * @brief Gets actor local coordinates for given viewport coordinates.
531 * @param[in] actor The actor describing local coordinate system
532 * @param[in] viewportX The viewport x position
533 * @param[in] viewportY The viewport y position
534 * @param[out] localX The local x position
535 * @param[out] localY The local y position
536 * @return true if the screen position has a local coordinate
538 bool ViewportToLocal(Actor actor, float viewportX, float viewportY, float& localX, float& localY) const;
541 * Sets Render Pass key for this RenderTask.
542 * Shader code that matches this render pass is used for rendering.
543 * If no matching shader is found, the code with a render pass of 0 is used.
544 * In other cases, operation is not guaranteed.
545 * @param[in] renderPassTag RenderPassTag value for this render task.
546 * @note RenderPassTag of default RenderTask is 0u.
548 void SetRenderPassTag(uint32_t renderPassTag);
551 * Gets Render Pass key for this RenderTask.
552 * @return RenderPassTag value for this render task.
554 uint32_t GetRenderPassTag() const;
557 * Sets Order Index to define rendering order for this RenderTask.
558 * In the DALi, offscreen renderTasks are rendered earlier than onscreen renderTask.
559 * In each category of OffScreen RenderTask and OnScreen RenderTask,
560 * a RenderTask with a smaller orderIndex is rendered first.
561 * The RenderTasks in RenderTaskList is always sorted as acending order of the OrderIndex.
562 * The OrderIndex value is needed to be set between [-1000, 1000].
563 * Default orderIndex is 0.
564 * @param[in] orderIndex the order index for this render task.
565 * @note The order among RenderTasks whose OrderIndex has not changed follows the order in which they were created.
566 * @note Rendering order among RenderTasks those have same OrderIndex cannot be guaranteed after the OrderIndex is changed
568 void SetOrderIndex(int32_t orderIndex);
571 * Gets Order Index for this RenderTask.
572 * @return OrderIndex value for this render task.
574 int32_t GetOrderIndex() const;
577 * @brief Get the unique id of RenderTask. It could be 0 given render task is invalid.
580 * @return The unique id of RenderTask, or 0 if invalid.
582 uint32_t GetRenderTaskId() const;
584 void RenderUntil(Actor actor);
588 * @brief If the refresh rate is REFRESH_ONCE, connect to this signal to be notified when a RenderTask has finished.
590 * @return The signal to connect to
592 RenderTaskSignalType& FinishedSignal();
594 public: // Not intended for application developers
597 * @brief This constructor is used by Dali New() methods.
599 * @param[in] renderTask A pointer to a newly allocated render-task
601 explicit DALI_INTERNAL RenderTask(Internal::RenderTask* renderTask);
610 #endif //DALI_RENDER_TASK_H