1 #ifndef DALI_RENDER_TASK_H
2 #define DALI_RENDER_TASK_H
5 * Copyright (c) 2019 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 Stage::GetRenderTaskList() method 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
92 * @brief Enumeration for instances of properties belonging to the RenderTask class.
98 * @brief Enumeration for instances of properties belonging to the RenderTask class.
104 * @brief name "viewportPosition", type Vector2
107 VIEWPORT_POSITION = DEFAULT_OBJECT_PROPERTY_START_INDEX,
109 * @brief name "viewportSize", type Vector2
114 * @brief name "clearColor", type Vector4
119 * @brief name "requiresSync", type BOOLEAN
120 * @details By default, the sync object is not created.
121 * 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.
122 * Thus the RENDER_ONCE finished signal can be emit at the correct timing.
124 * @note The use of GL sync might cause deadlock with multiple access to the single pixmap happening in the same time.
131 * @brief Typedef for signals sent by this class.
134 typedef Signal< void (RenderTask& source) > RenderTaskSignalType;
137 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
139 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen
140 * @return True if the conversion was successful, otherwise coordinates should be unmodified
142 typedef bool (* ScreenToFrameBufferFunction)( Vector2& coordinates );
145 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
147 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen
148 * @return True if the conversion was successful, otherwise coordinates should be unmodified
150 typedef bool (* const ConstScreenToFrameBufferFunction)( Vector2& coordinates );
153 * @brief The default conversion function returns false for any screen coordinates.
155 * This effectively disables hit-testing for RenderTasks rendering to a frame buffer.
156 * See also FULLSCREEN_FRAMEBUFFER_FUNCTION below.
158 static ConstScreenToFrameBufferFunction DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION;
161 * @brief This conversion function outputs the (unmodified) screen coordinates as frame-buffer coordinates.
163 * Therefore the contents of an off-screen image is expected to be rendered "full screen".
165 static ConstScreenToFrameBufferFunction FULLSCREEN_FRAMEBUFFER_FUNCTION;
168 * @brief Enumeration for the refresh-rate of the RenderTask.
173 REFRESH_ONCE = 0, ///< Process once only e.g. take a snap-shot of the scene. @SINCE_1_0.0
174 REFRESH_ALWAYS = 1 ///< Process every frame. @SINCE_1_0.0
177 static const bool DEFAULT_EXCLUSIVE; ///< false
178 static const bool DEFAULT_INPUT_ENABLED; ///< true
179 static const Vector4 DEFAULT_CLEAR_COLOR; ///< Color::BLACK
180 static const bool DEFAULT_CLEAR_ENABLED; ///< false
181 static const bool DEFAULT_CULL_MODE; ///< true
182 static const uint32_t DEFAULT_REFRESH_RATE; ///< REFRESH_ALWAYS
185 * @brief Creates an empty RenderTask handle.
187 * This can be initialised with RenderTaskList::CreateRenderTask().
193 * @brief Downcasts a handle to RenderTask handle.
195 * If handle points to a RenderTask, the
196 * downcast produces valid handle. If not, the returned handle is left uninitialized.
198 * @param[in] handle A handle to an object
199 * @return A handle to a RenderTask or an uninitialized handle
201 static RenderTask DownCast( BaseHandle handle );
206 * This is non-virtual since derived Handle types must not contain data or virtual methods.
212 * @brief This copy constructor is required for (smart) pointer semantics.
215 * @param[in] handle A reference to the copied handle
217 RenderTask(const RenderTask& handle);
220 * @brief This assignment operator is required for (smart) pointer semantics.
223 * @param[in] rhs A reference to the copied handle
224 * @return A reference to this
226 RenderTask& operator=(const RenderTask& rhs);
229 * @brief Sets the actors to be rendered.
231 * @param[in] actor This actor and its children will be rendered.
232 * If actor is an empty handle, then nothing will be rendered
234 void SetSourceActor( Actor actor );
237 * @brief Retrieves the actors to be rendered.
239 * @return This actor and its children will be rendered
241 Actor GetSourceActor() const;
244 * @brief Sets whether the RenderTask has exclusive access to the source actors; the default is false.
246 * @param[in] exclusive True if the source actors will only be rendered by this render-task
248 void SetExclusive( bool exclusive );
251 * @brief Queries whether the RenderTask has exclusive access to the source actors.
253 * @return True if the source actors will only be rendered by this render-task
255 bool IsExclusive() const;
258 * @brief Sets whether the render-task should be considered for input handling; the default is true.
260 * The task used for input handling will be last task in the RenderTaskList which has input enabled,
261 * and has a valid source & camera actor.
262 * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the screen->frame-buffer
263 * coordinate conversion is successful; see also SetScreenToFrameBufferFunction().
265 * @param[in] enabled True if the render-task should be considered for input handling
267 void SetInputEnabled( bool enabled );
270 * @brief Queries whether the render-task should be considered for input handling.
272 * @return True if the render-task should be considered for input handling
274 bool GetInputEnabled() const;
277 * @brief Sets the actor from which the scene is viewed.
279 * @param[in] cameraActor The scene is viewed from the perspective of this actor
281 void SetCameraActor( CameraActor cameraActor );
284 * @brief Retrieves the actor from which the scene is viewed.
286 * @return The scene is viewed from the perspective of this actor
288 CameraActor GetCameraActor() const;
291 * @brief Sets the frame-buffer used as a render target.
293 * @param[in] frameBuffer A valid frame-buffer handle to enable off-screen rendering, or an uninitialized handle to disable
295 void SetTargetFrameBuffer( FrameBufferImage frameBuffer );
298 * @brief Retrieves the frame-buffer used as a render target.
300 * @return A valid frame-buffer handle, or an uninitialised handle if off-screen rendering is disabled
302 FrameBufferImage GetTargetFrameBuffer() const;
305 * @brief Sets the frame-buffer used as a render target.
307 * @param[in] frameBuffer A valid FrameBuffer handle to enable off-screen rendering, or an uninitialized handle to disable it
309 void SetFrameBuffer( FrameBuffer frameBuffer );
312 * @brief Retrieves the frame-buffer used as a render target.
314 * @return The framebuffer
316 FrameBuffer GetFrameBuffer() const;
319 * @brief Sets the function used to convert screen coordinates to frame-buffer coordinates.
321 * This is useful for hit-testing actors which are rendered off-screen.
323 * @param[in] conversionFunction The conversion function
325 void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
328 * @brief Retrieves the function used to convert screen coordinates to frame-buffer coordinates.
330 * @return The conversion function
332 ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
335 * @brief Sets the actor used to convert screen coordinates to frame-buffer coordinates.
337 * The local coordinates of the actor are mapped as frame-buffer coordinates.
338 * This is useful for hit-testing actors which are rendered off-screen.
340 * @param[in] mappingActor The actor used for conversion
341 * @note The mapping actor needs to be rendered by the default render task to make the mapping work properly.
343 void SetScreenToFrameBufferMappingActor( Actor mappingActor );
346 * @brief Retrieves the actor used to convert screen coordinates to frame-buffer coordinates.
348 * @return The actor used for conversion
350 Actor GetScreenToFrameBufferMappingActor() const;
353 * @brief Sets the GL viewport position used when rendering.
355 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
356 * By default this will match the target window or frame-buffer size.
358 * @param[in] position The viewports position (x,y)
359 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
361 void SetViewportPosition( Vector2 position );
364 * @brief Retrieves the GL viewport position used when rendering.
366 * @return The viewport
368 Vector2 GetCurrentViewportPosition() const;
371 * @brief Sets the GL viewport size used when rendering.
373 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
374 * By default this will match the target window or frame-buffer size.
376 * @param[in] size The viewports size (width,height)
378 void SetViewportSize( Vector2 size );
381 * @brief Retrieves the GL viewport size used when rendering.
383 * @return The viewport
385 Vector2 GetCurrentViewportSize() const;
388 * @brief Sets the GL viewport used when rendering.
390 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
391 * By default this will match the target window or frame-buffer size.
393 * @param[in] viewport The new viewport
394 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
396 void SetViewport( Viewport viewport );
399 * @brief Retrieves the GL viewport used when rendering.
401 * @return The viewport
403 Viewport GetViewport() const;
406 * @brief Sets the clear color used when SetClearEnabled(true) is used.
408 * @param[in] color The new clear color
410 void SetClearColor( const Vector4& color );
413 * @brief Retrieves the clear color used when SetClearEnabled(true) is used.
415 * @return The clear color
416 * @note This property can be animated; the return value may not match the value written with SetClearColor().
418 Vector4 GetClearColor() const;
421 * @brief Sets whether the render-task will clear the results of previous render-tasks.
423 * The default is false.
426 * @param[in] enabled True if the render-task should clear
427 * @note The default GL surface is cleared automatically at the
428 * beginning of each frame; this setting is only useful when 2+
429 * render-tasks are used, and the result of the first task needs to
430 * be (partially) cleared before rendering the second.
433 void SetClearEnabled( bool enabled );
436 * @brief Queries whether the render-task will clear the results of previous render-tasks.
438 * @return True if the render-task should clear
440 bool GetClearEnabled() const;
443 * @brief Sets whether the render task will cull the actors to the camera's view frustum.
446 * @param[in] cullMode True if the renderers should be culled
447 * @note The default mode is to cull actors.
448 * @note If the shader uses @ref Shader::Hint::MODIFIES_GEOMETRY then culling optimizations are disabled.
451 void SetCullMode( bool cullMode );
454 * @brief Gets the cull mode.
457 * @return True if the render task should cull the actors to the camera's view frustum
459 bool GetCullMode() const;
462 * @brief Sets the refresh-rate of the RenderTask.
464 * The default is REFRESH_ALWAYS (1), meaning that the RenderTask
465 * will be processed every frame if the scene graph is changing. It
466 * may be desirable to process less frequently. For example,
467 * SetRefreshRate(3) will process once every 3 frames if the scene
468 * graph is changing. If the scene graph is not changing, then the
469 * render task will not be rendered, regardless of this value.
471 * The REFRESH_ONCE value means that the RenderTask will be
472 * processed once only, to take a snap-shot of the scene.
473 * Repeatedly calling SetRefreshRate(REFRESH_ONCE) will cause more
474 * snap-shots to be taken.
477 * @param[in] refreshRate The new refresh rate
479 void SetRefreshRate( uint32_t refreshRate );
482 * @brief Queries the refresh-rate of the RenderTask.
484 * @return The refresh-rate
486 uint32_t GetRefreshRate() const;
489 * @brief Gets viewport coordinates for given world position.
492 * @param[in] position The world position
493 * @param[out] viewportX The viewport x position
494 * @param[out] viewportY The viewport y position
495 * @return true if the position has a screen coordinate
497 bool WorldToViewport(const Vector3 &position, float& viewportX, float& viewportY) const;
500 * @brief Gets actor local coordinates for given viewport coordinates.
503 * @param[in] actor The actor describing local coordinate system
504 * @param[in] viewportX The viewport x position
505 * @param[in] viewportY The viewport y position
506 * @param[out] localX The local x position
507 * @param[out] localY The local y position
508 * @return true if the screen position has a local coordinate
510 bool ViewportToLocal(Actor actor, float viewportX, float viewportY, float &localX, float &localY) const;
515 * @brief If the refresh rate is REFRESH_ONCE, connect to this signal to be notified when a RenderTask has finished.
517 * @return The signal to connect to
519 RenderTaskSignalType& FinishedSignal();
521 public: // Not intended for application developers
525 * @brief This constructor is used by Dali New() methods.
527 * @param[in] renderTask A pointer to a newly allocated render-task
529 explicit DALI_INTERNAL RenderTask( Internal::RenderTask* renderTask );
538 #endif //DALI_RENDER_TASK_H