1 #ifndef __DALI_RENDER_TASK_H__
2 #define __DALI_RENDER_TASK_H__
5 * Copyright (c) 2015 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/public-api/math/viewport.h>
23 #include <dali/public-api/object/handle.h>
24 #include <dali/public-api/object/property-index-ranges.h>
25 #include <dali/public-api/signals/dali-signal.h>
30 * @addtogroup dali_core_rendering_effects
36 class FrameBufferImage;
39 namespace Internal DALI_INTERNAL
45 * @brief RenderTasks describe how the Dali scene should be rendered.
47 * The Stage::GetRenderTaskList() method provides access to an ordered list of render-tasks.
49 * Each RenderTask must specify the source actors to be rendered, and a camera actor from
50 * which the scene is viewed.
52 * RenderTasks may optionally target a frame-buffer, otherwise the default GL surface is used;
53 * typically this is a window provided by the native system.
55 * By default Dali provides a single RenderTask, which renders the entire actor hierachy using
56 * a default camera actor and GL surface. If stereoscopic rendering is enabled, Dali will create
57 * two additional render tasks, on for each eye. Each render task will have its own camera parented
58 * to the default camera actor.
60 * The first RenderTask used for input handling will be the last one rendered, which also has input enabled,
61 * and has a valid source & camera actor; see SetInputEnabled().
63 * If none of the actors are hit in the last RenderTask rendered, then input handling will continue
64 * with the second last RenderTask rendered, and so on.
66 * All RenderTasks which target a frame-buffer (i.e. off screen) will be rendered before all RenderTasks
67 * which target the default GL surface. This allows the user to render intermediate targets which are used
68 * later when targetting the screen.
70 * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the
71 * screen->frame-buffer coordinate conversion is successful; see SetScreenToFrameBufferFunction().
73 * If the refresh rate id REFRESH_ONCE and a "Finish" signal is connected, it will be emitted when the RenderTask is completed.
74 * Note that all connected signals must be disconnected before the object is destroyed. This is typically done in the
75 * object destructor, and requires either the Dali::Connection object or Dali::RenderTask handle to be stored.
78 * | %Signal Name | Method |
79 * |--------------|-----------------------|
80 * | finished | @ref FinishedSignal() |
83 class DALI_IMPORT_API RenderTask : public Handle
88 * @brief An enumeration of properties belonging to the RenderTask class.
95 VIEWPORT_POSITION = DEFAULT_OBJECT_PROPERTY_START_INDEX, ///< name "viewportPosition", type Vector2 @SINCE_1_0.0
96 VIEWPORT_SIZE, ///< name "viewportSize", type Vector2 @SINCE_1_0.0
97 CLEAR_COLOR, ///< name "clearColor", type Vector4 @SINCE_1_0.0
102 * @brief Typedef for signals sent by this class.
105 typedef Signal< void (RenderTask& source) > RenderTaskSignalType;
108 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
110 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
111 * @return True if the conversion was successful, otherwise coordinates should be unmodified.
113 typedef bool (* ScreenToFrameBufferFunction)( Vector2& coordinates );
116 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
118 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
119 * @return True if the conversion was successful, otherwise coordinates should be unmodified.
121 typedef bool (* const ConstScreenToFrameBufferFunction)( Vector2& coordinates );
124 * @brief The default conversion function returns false for any screen coordinates.
126 * This effectively disables hit-testing for RenderTasks rendering to a frame buffer.
127 * See also FULLSCREEN_FRAMEBUFFER_FUNCTION below.
129 static ConstScreenToFrameBufferFunction DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION;
132 * @brief This conversion function outputs the (unmodified) screen coordinates as frame-buffer coordinates.
134 * Therefore the contents of an off-screen image is expected to be rendered "full screen".
136 static ConstScreenToFrameBufferFunction FULLSCREEN_FRAMEBUFFER_FUNCTION;
139 * @brief The refresh-rate of the RenderTask.
144 REFRESH_ONCE = 0, ///< Process once only e.g. take a snap-shot of the scene. @SINCE_1_0.0
145 REFRESH_ALWAYS = 1 ///< Process every frame. @SINCE_1_0.0
148 static const bool DEFAULT_EXCLUSIVE; ///< false
149 static const bool DEFAULT_INPUT_ENABLED; ///< true
150 static const Vector4 DEFAULT_CLEAR_COLOR; ///< Color::BLACK
151 static const bool DEFAULT_CLEAR_ENABLED; ///< false
152 static const bool DEFAULT_CULL_MODE; ///< true
153 static const unsigned int DEFAULT_REFRESH_RATE; ///< REFRESH_ALWAYS
156 * @brief Create an empty RenderTask handle.
158 * This can be initialised with RenderTaskList::CreateRenderTask().
164 * @brief Downcast a handle to RenderTask handle.
166 * If handle points to a RenderTask the
167 * downcast produces valid handle. If not the returned handle is left uninitialized.
169 * @param[in] handle A handle to an object.
170 * @return A handle to a RenderTask or an uninitialized handle.
172 static RenderTask DownCast( BaseHandle handle );
177 * This is non-virtual since derived Handle types must not contain data or virtual methods.
183 * @brief This copy constructor is required for (smart) pointer semantics.
186 * @param [in] handle A reference to the copied handle
188 RenderTask(const RenderTask& handle);
191 * @brief This assignment operator is required for (smart) pointer semantics.
194 * @param [in] rhs A reference to the copied handle
195 * @return A reference to this
197 RenderTask& operator=(const RenderTask& rhs);
200 * @brief Set the actors to be rendered.
202 * @param[in] actor This actor and its children will be rendered.
203 * If actor is an empty handle, then nothing will be rendered.
205 void SetSourceActor( Actor actor );
208 * @brief Retrieve the actors to be rendered.
210 * @return This actor and its children will be rendered.
212 Actor GetSourceActor() const;
215 * @brief Set whether the RenderTask has exclusive access to the source actors; the default is false.
217 * @param[in] exclusive True if the source actors will only be rendered by this render-task.
219 void SetExclusive( bool exclusive );
222 * @brief Query whether the RenderTask has exclusive access to the source actors.
224 * @return True if the source actors will only be rendered by this render-task.
226 bool IsExclusive() const;
229 * @brief Set whether the render-task should be considered for input handling; the default is true.
231 * The task used for input handling will be last task in the RenderTaskList which has input enabled,
232 * and has a valid source & camera actor.
233 * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the screen->frame-buffer
234 * coordinate conversion is successful; see also SetScreenToFrameBufferFunction().
236 * @param[in] enabled True if the render-task should be considered for input handling.
238 void SetInputEnabled( bool enabled );
241 * @brief Query whether the render-task should be considered for input handling.
243 * @return True if the render-task should be considered for input handling.
245 bool GetInputEnabled() const;
248 * @brief Set the actor from which the scene is viewed.
250 * @param[in] cameraActor The scene is viewed from the perspective of this actor.
252 void SetCameraActor( CameraActor cameraActor );
255 * @brief Retrieve the actor from which the scene is viewed.
257 * @return The scene is viewed from the perspective of this actor.
259 CameraActor GetCameraActor() const;
262 * @brief Set the frame-buffer used as a render target.
264 * @param[in] frameBuffer A valid frame-buffer handle to enable off-screen rendering, or an uninitialized handle to disable.
266 void SetTargetFrameBuffer( FrameBufferImage frameBuffer );
269 * @brief Retrieve the frame-buffer used as a render target.
271 * @return A valid frame-buffer handle, or an uninitialised handle if off-screen rendering is disabled.
273 FrameBufferImage GetTargetFrameBuffer() const;
276 * @brief Set the function used to convert screen coordinates to frame-buffer coordinates.
278 * This is useful for hit-testing actors which are rendered off-screen.
280 * @param[in] conversionFunction The conversion function.
282 void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
285 * @brief Retrieve the function used to convert screen coordinates to frame-buffer coordinates.
287 * @return The conversion function.
289 ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
292 * @brief Set the actor used to convert screen coordinates to frame-buffer coordinates.
294 * The local coordinates of the actor are mapped as frame-buffer coordinates.
295 * This is useful for hit-testing actors which are rendered off-screen.
296 * Note: The mapping actor needs to be rendered by the default render task to make the mapping work properly.
298 * @param[in] mappingActor The actor used for conversion.
300 void SetScreenToFrameBufferMappingActor( Actor mappingActor );
303 * @brief Retrieve the actor used to convert screen coordinates to frame-buffer coordinates.
305 * @return The actor used for conversion.
307 Actor GetScreenToFrameBufferMappingActor() const;
310 * @brief Set the GL viewport position used when rendering.
312 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
313 * By default this will match the target window or frame-buffer size.
315 * @param[in] position The viewports position (x,y)
316 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
318 void SetViewportPosition( Vector2 position );
321 * @brief Retrieve the GL viewport position used when rendering.
323 * @return The viewport.
325 Vector2 GetCurrentViewportPosition() const;
328 * @brief Set the GL viewport size used when rendering.
330 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
331 * By default this will match the target window or frame-buffer size.
333 * @param[in] size The viewports size (width,height)
335 void SetViewportSize( Vector2 size );
338 * @brief Retrieve the GL viewport size used when rendering.
340 * @return The viewport.
342 Vector2 GetCurrentViewportSize() const;
345 * @brief Set the GL viewport used when rendering.
347 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
348 * By default this will match the target window or frame-buffer size.
350 * @param[in] viewport The new viewport.
351 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
353 void SetViewport( Viewport viewport );
356 * @brief Retrieve the GL viewport used when rendering.
358 * @return The viewport.
360 Viewport GetViewport() const;
363 * @brief Set the clear color used when SetClearEnabled(true) is used.
365 * @param[in] color The new clear color.
367 void SetClearColor( const Vector4& color );
370 * @brief Retrieve the clear color used when SetClearEnabled(true) is used.
372 * @return The clear color.
373 * @note This property can be animated; the return value may not match the value written with SetClearColor().
375 Vector4 GetClearColor() const;
378 * @brief Set whether the render-task will clear the results of previous render-tasks.
380 * The default is false.
383 * @param[in] enabled True if the render-task should clear.
384 * @note The default GL surface is cleared automatically at the
385 * beginning of each frame; this setting is only useful when 2+
386 * render-tasks are used, and the result of the first task needs to
387 * be (partially) cleared before rendering the second.
390 void SetClearEnabled( bool enabled );
393 * @brief Query whether the render-task will clear the results of previous render-tasks.
395 * @return True if the render-task should clear.
397 bool GetClearEnabled() const;
400 * @brief Set whether the render task will cull the actors to the camera's view frustum.
402 * Note that this will only affect image actors that use the default vertex shader.
403 * The default mode is to cull actors.
405 * @param[in] cullMode True if the renderers should be culled.
407 void SetCullMode( bool cullMode );
410 * @brief Get the cull mode.
413 * @return True if the render task should cull the actors to the camera's view frustum
415 bool GetCullMode() const;
418 * @brief Set the refresh-rate of the RenderTask.
420 * The default is REFRESH_ALWAYS (1), meaning that the RenderTask
421 * will be processed every frame if the scene graph is changing. It
422 * may be desirable to process less frequently. For example,
423 * SetRefreshRate(3) will process once every 3 frames if the scene
424 * graph is changing. If the scene graph is not changing, then the
425 * render task will not be rendered, regardless of this value.
427 * The REFRESH_ONCE value means that the RenderTask will be
428 * processed once only, to take a snap-shot of the scene.
429 * Repeatedly calling SetRefreshRate(REFRESH_ONCE) will cause more
430 * snap-shots to be taken.
433 * @param[in] refreshRate The new refresh rate.
435 void SetRefreshRate( unsigned int refreshRate );
438 * @brief Query the refresh-rate of the RenderTask.
440 * @return The refresh-rate.
442 unsigned int GetRefreshRate() const;
445 * @brief Get viewport coordinates for given world position
449 * @param[in] position The world position.
450 * @param[out] viewportX The viewport x position.
451 * @param[out] viewportY The viewport y position.
452 * @return true if the position has a screen coordinate
454 bool WorldToViewport(const Vector3 &position, float& viewportX, float& viewportY) const;
457 * @brief Get actor local coordinates for given viewport coordinates
461 * @param[in] actor The actor describing local coordinate system.
462 * @param[in] viewportX The viewport x position.
463 * @param[in] viewportY The viewport y position.
464 * @param[out] localX The local x position.
465 * @param[out] localY The local y position.
466 * @return true if the screen position has a local coordinate
468 bool ViewportToLocal(Actor actor, float viewportX, float viewportY, float &localX, float &localY) const;
473 * @brief If the refresh rate is REFRESH_ONCE, connect to this signal to be notified when a RenderTask has finished.
476 RenderTaskSignalType& FinishedSignal();
478 public: // Not intended for application developers
481 * @brief This constructor is used by Dali New() methods.
483 * @param [in] renderTask A pointer to a newly allocated render-task
485 explicit DALI_INTERNAL RenderTask( Internal::RenderTask* renderTask );
493 #endif //__DALI_RENDER_TASK_H__