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_render_tasks
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() |
82 class DALI_IMPORT_API RenderTask : public Handle
87 * @brief An enumeration of properties belonging to the RenderTask class.
93 VIEWPORT_POSITION = DEFAULT_OBJECT_PROPERTY_START_INDEX, ///< name "viewportPosition", type Vector2
94 VIEWPORT_SIZE, ///< name "viewportSize", type Vector2
95 CLEAR_COLOR, ///< name "clearColor", type Vector4
100 * @brief Typedef for signals sent by this class.
102 typedef Signal< void (RenderTask& source) > RenderTaskSignalType;
105 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
106 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
107 * @return True if the conversion was successful, otherwise coordinates should be unmodified.
109 typedef bool (* ScreenToFrameBufferFunction)( Vector2& coordinates );
112 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
113 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
114 * @return True if the conversion was successful, otherwise coordinates should be unmodified.
116 typedef bool (* const ConstScreenToFrameBufferFunction)( Vector2& coordinates );
119 * @brief The default conversion function returns false for any screen coordinates.
121 * This effectively disables hit-testing for RenderTasks rendering to a frame buffer.
122 * See also FULLSCREEN_FRAMEBUFFER_FUNCTION below.
124 static ConstScreenToFrameBufferFunction DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION;
127 * @brief This conversion function outputs the (unmodified) screen coordinates as frame-buffer coordinates.
129 * Therefore the contents of an off-screen image is expected to be rendered "full screen".
131 static ConstScreenToFrameBufferFunction FULLSCREEN_FRAMEBUFFER_FUNCTION;
134 * @brief The refresh-rate of the RenderTask.
138 REFRESH_ONCE = 0, ///< Process once only e.g. take a snap-shot of the scene.
139 REFRESH_ALWAYS = 1 ///< Process every frame.
142 static const bool DEFAULT_EXCLUSIVE; ///< false
143 static const bool DEFAULT_INPUT_ENABLED; ///< true
144 static const Vector4 DEFAULT_CLEAR_COLOR; ///< Color::BLACK
145 static const bool DEFAULT_CLEAR_ENABLED; ///< false
146 static const bool DEFAULT_CULL_MODE; ///< true
147 static const unsigned int DEFAULT_REFRESH_RATE; ///< REFRESH_ALWAYS
150 * @brief Create an empty RenderTask handle.
152 * This can be initialised with RenderTaskList::CreateRenderTask().
157 * @brief Downcast a handle to RenderTask handle.
159 * If handle points to a RenderTask the
160 * downcast produces valid handle. If not the returned handle is left uninitialized.
161 * @param[in] handle A handle to an object.
162 * @return A handle to a RenderTask or an uninitialized handle.
164 static RenderTask DownCast( BaseHandle handle );
169 * This is non-virtual since derived Handle types must not contain data or virtual methods.
174 * @brief This copy constructor is required for (smart) pointer semantics.
176 * @param [in] handle A reference to the copied handle
178 RenderTask(const RenderTask& handle);
181 * @brief This assignment operator is required for (smart) pointer semantics.
183 * @param [in] rhs A reference to the copied handle
184 * @return A reference to this
186 RenderTask& operator=(const RenderTask& rhs);
189 * @brief Set the actors to be rendered.
190 * @param[in] actor This actor and its children will be rendered.
191 * If actor is an empty handle, then nothing will be rendered.
193 void SetSourceActor( Actor actor );
196 * @brief Retrieve the actors to be rendered.
197 * @return This actor and its children will be rendered.
199 Actor GetSourceActor() const;
202 * @brief Set whether the RenderTask has exclusive access to the source actors; the default is false.
203 * @param[in] exclusive True if the source actors will only be rendered by this render-task.
205 void SetExclusive( bool exclusive );
208 * @brief Query whether the RenderTask has exclusive access to the source actors.
209 * @return True if the source actors will only be rendered by this render-task.
211 bool IsExclusive() const;
214 * @brief Set whether the render-task should be considered for input handling; the default is true.
216 * The task used for input handling will be last task in the RenderTaskList which has input enabled,
217 * and has a valid source & camera actor.
218 * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the screen->frame-buffer
219 * coordinate conversion is successful; see also SetScreenToFrameBufferFunction().
220 * @param[in] enabled True if the render-task should be considered for input handling.
222 void SetInputEnabled( bool enabled );
225 * @brief Query whether the render-task should be considered for input handling.
226 * @return True if the render-task should be considered for input handling.
228 bool GetInputEnabled() const;
231 * @brief Set the actor from which the scene is viewed.
232 * @param[in] cameraActor The scene is viewed from the perspective of this actor.
234 void SetCameraActor( CameraActor cameraActor );
237 * @brief Retrieve the actor from which the scene is viewed.
238 * @return The scene is viewed from the perspective of this actor.
240 CameraActor GetCameraActor() const;
243 * @brief Set the frame-buffer used as a render target.
244 * @param[in] frameBuffer A valid frame-buffer handle to enable off-screen rendering, or an uninitialized handle to disable.
246 void SetTargetFrameBuffer( FrameBufferImage frameBuffer );
249 * @brief Retrieve the frame-buffer used as a render target.
250 * @return A valid frame-buffer handle, or an uninitialised handle if off-screen rendering is disabled.
252 FrameBufferImage GetTargetFrameBuffer() const;
255 * @brief Set the function used to convert screen coordinates to frame-buffer coordinates.
257 * This is useful for hit-testing actors which are rendered off-screen.
258 * @param[in] conversionFunction The conversion function.
260 void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
263 * @brief Retrieve the function used to convert screen coordinates to frame-buffer coordinates.
264 * @return The conversion function.
266 ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
269 * @brief Set the actor used to convert screen coordinates to frame-buffer coordinates.
271 * The local coordinates of the actor are mapped as frame-buffer coordinates.
272 * This is useful for hit-testing actors which are rendered off-screen.
273 * Note: The mapping actor needs to be rendered by the default render task to make the mapping work properly.
274 * @param[in] mappingActor The actor used for conversion.
276 void SetScreenToFrameBufferMappingActor( Actor mappingActor );
279 * @brief Retrieve the actor used to convert screen coordinates to frame-buffer coordinates.
280 * @return The actor used for conversion.
282 Actor GetScreenToFrameBufferMappingActor() const;
285 * @brief Set the GL viewport position used when rendering.
287 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
288 * By default this will match the target window or frame-buffer size.
289 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
290 * @param[in] position The viewports position (x,y)
292 void SetViewportPosition( Vector2 position );
295 * @brief Retrieve the GL viewport position used when rendering.
296 * @return The viewport.
298 Vector2 GetCurrentViewportPosition() const;
301 * @brief Set the GL viewport size used when rendering.
303 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
304 * By default this will match the target window or frame-buffer size.
305 * @param[in] size The viewports size (width,height)
307 void SetViewportSize( Vector2 size );
310 * @brief Retrieve the GL viewport size used when rendering.
311 * @return The viewport.
313 Vector2 GetCurrentViewportSize() const;
316 * @brief Set the GL viewport used when rendering.
318 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
319 * By default this will match the target window or frame-buffer size.
320 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
321 * @param[in] viewport The new viewport.
323 void SetViewport( Viewport viewport );
326 * @brief Retrieve the GL viewport used when rendering.
327 * @return The viewport.
329 Viewport GetViewport() const;
332 * @brief Set the clear color used when SetClearEnabled(true) is used.
333 * @param[in] color The new clear color.
335 void SetClearColor( const Vector4& color );
338 * @brief Retrieve the clear color used when SetClearEnabled(true) is used.
339 * @note This property can be animated; the return value may not match the value written with SetClearColor().
340 * @return The clear color.
342 Vector4 GetClearColor() const;
345 * @brief Set whether the render-task will clear the results of previous render-tasks.
347 * The default is false.
349 * @note The default GL surface is cleared automatically at the
350 * beginning of each frame; this setting is only useful when 2+
351 * render-tasks are used, and the result of the first task needs to
352 * be (partially) cleared before rendering the second.
354 * @param[in] enabled True if the render-task should clear.
356 void SetClearEnabled( bool enabled );
359 * @brief Query whether the render-task will clear the results of previous render-tasks.
360 * @return True if the render-task should clear.
362 bool GetClearEnabled() const;
365 * @brief Set whether the render task will cull the actors to the camera's view frustum.
367 * Note that this will only affect image actors that use the default vertex shader.
368 * The default mode is to cull actors.
369 * @param[in] cullMode True if the renderers should be culled.
371 void SetCullMode( bool cullMode );
374 * @brief Get the cull mode.
376 * @return True if the render task should cull the actors to the camera's view frustum
378 bool GetCullMode() const;
381 * @brief Set the refresh-rate of the RenderTask.
383 * The default is REFRESH_ALWAYS (1), meaning that the RenderTask
384 * will be processed every frame if the scene graph is changing. It
385 * may be desirable to process less frequently. For example,
386 * SetRefreshRate(3) will process once every 3 frames if the scene
387 * graph is changing. If the scene graph is not changing, then the
388 * render task will not be rendered, regardless of this value.
390 * The REFRESH_ONCE value means that the RenderTask will be
391 * processed once only, to take a snap-shot of the scene.
392 * Repeatedly calling SetRefreshRate(REFRESH_ONCE) will cause more
393 * snap-shots to be taken.
395 * @param[in] refreshRate The new refresh rate.
397 void SetRefreshRate( unsigned int refreshRate );
400 * @brief Query the refresh-rate of the RenderTask.
401 * @return The refresh-rate.
403 unsigned int GetRefreshRate() const;
406 * @brief Get viewport coordinates for given world position
410 * @param[in] position The world position.
411 * @param[out] viewportX The viewport x position.
412 * @param[out] viewportY The viewport y position.
413 * @return true if the position has a screen coordinate
415 bool WorldToViewport(const Vector3 &position, float& viewportX, float& viewportY) const;
418 * @brief Get actor local coordinates for given viewport coordinates
422 * @param[in] actor The actor describing local coordinate system.
423 * @param[in] viewportX The viewport x position.
424 * @param[in] viewportY The viewport y position.
425 * @param[out] localX The local x position.
426 * @param[out] localY The local y position.
427 * @return true if the screen position has a local coordinate
429 bool ViewportToLocal(Actor actor, float viewportX, float viewportY, float &localX, float &localY) const;
434 * @brief If the refresh rate is REFRESH_ONCE, connect to this signal to be notified when a RenderTask has finished.
436 RenderTaskSignalType& FinishedSignal();
438 public: // Not intended for application developers
441 * @brief This constructor is used by Dali New() methods.
442 * @param [in] renderTask A pointer to a newly allocated render-task
444 explicit DALI_INTERNAL RenderTask( Internal::RenderTask* renderTask );
452 #endif //__DALI_RENDER_TASK_H__