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.
96 * @brief name "viewportPosition", type Vector2
99 VIEWPORT_POSITION = DEFAULT_OBJECT_PROPERTY_START_INDEX,
101 * @brief name "viewportSize", type Vector2
106 * @brief name "clearColor", type Vector4
111 * @brief name "requiresSync", type BOOLEAN
112 * @details By default, the sync object is not created.
113 * 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.
114 * Thus the RENDER_ONCE finished signal can be emit at the correct timing.
115 * @note The use of GL sync might cause deadlock with multiple access to the single pixmap happening in the same time.
123 * @brief Typedef for signals sent by this class.
126 typedef Signal< void (RenderTask& source) > RenderTaskSignalType;
129 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
131 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
132 * @return True if the conversion was successful, otherwise coordinates should be unmodified.
134 typedef bool (* ScreenToFrameBufferFunction)( Vector2& coordinates );
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 (* const ConstScreenToFrameBufferFunction)( Vector2& coordinates );
145 * @brief The default conversion function returns false for any screen coordinates.
147 * This effectively disables hit-testing for RenderTasks rendering to a frame buffer.
148 * See also FULLSCREEN_FRAMEBUFFER_FUNCTION below.
150 static ConstScreenToFrameBufferFunction DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION;
153 * @brief This conversion function outputs the (unmodified) screen coordinates as frame-buffer coordinates.
155 * Therefore the contents of an off-screen image is expected to be rendered "full screen".
157 static ConstScreenToFrameBufferFunction FULLSCREEN_FRAMEBUFFER_FUNCTION;
160 * @brief The refresh-rate of the RenderTask.
165 REFRESH_ONCE = 0, ///< Process once only e.g. take a snap-shot of the scene. @SINCE_1_0.0
166 REFRESH_ALWAYS = 1 ///< Process every frame. @SINCE_1_0.0
169 static const bool DEFAULT_EXCLUSIVE; ///< false
170 static const bool DEFAULT_INPUT_ENABLED; ///< true
171 static const Vector4 DEFAULT_CLEAR_COLOR; ///< Color::BLACK
172 static const bool DEFAULT_CLEAR_ENABLED; ///< false
173 static const bool DEFAULT_CULL_MODE; ///< true
174 static const unsigned int DEFAULT_REFRESH_RATE; ///< REFRESH_ALWAYS
177 * @brief Create an empty RenderTask handle.
179 * This can be initialised with RenderTaskList::CreateRenderTask().
185 * @brief Downcast a handle to RenderTask handle.
187 * If handle points to a RenderTask the
188 * downcast produces valid handle. If not the returned handle is left uninitialized.
190 * @param[in] handle A handle to an object.
191 * @return A handle to a RenderTask or an uninitialized handle.
193 static RenderTask DownCast( BaseHandle handle );
198 * This is non-virtual since derived Handle types must not contain data or virtual methods.
204 * @brief This copy constructor is required for (smart) pointer semantics.
207 * @param [in] handle A reference to the copied handle
209 RenderTask(const RenderTask& handle);
212 * @brief This assignment operator is required for (smart) pointer semantics.
215 * @param [in] rhs A reference to the copied handle
216 * @return A reference to this
218 RenderTask& operator=(const RenderTask& rhs);
221 * @brief Set the actors to be rendered.
223 * @param[in] actor This actor and its children will be rendered.
224 * If actor is an empty handle, then nothing will be rendered.
226 void SetSourceActor( Actor actor );
229 * @brief Retrieve the actors to be rendered.
231 * @return This actor and its children will be rendered.
233 Actor GetSourceActor() const;
236 * @brief Set whether the RenderTask has exclusive access to the source actors; the default is false.
238 * @param[in] exclusive True if the source actors will only be rendered by this render-task.
240 void SetExclusive( bool exclusive );
243 * @brief Query whether the RenderTask has exclusive access to the source actors.
245 * @return True if the source actors will only be rendered by this render-task.
247 bool IsExclusive() const;
250 * @brief Set whether the render-task should be considered for input handling; the default is true.
252 * The task used for input handling will be last task in the RenderTaskList which has input enabled,
253 * and has a valid source & camera actor.
254 * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the screen->frame-buffer
255 * coordinate conversion is successful; see also SetScreenToFrameBufferFunction().
257 * @param[in] enabled True if the render-task should be considered for input handling.
259 void SetInputEnabled( bool enabled );
262 * @brief Query whether the render-task should be considered for input handling.
264 * @return True if the render-task should be considered for input handling.
266 bool GetInputEnabled() const;
269 * @brief Set the actor from which the scene is viewed.
271 * @param[in] cameraActor The scene is viewed from the perspective of this actor.
273 void SetCameraActor( CameraActor cameraActor );
276 * @brief Retrieve the actor from which the scene is viewed.
278 * @return The scene is viewed from the perspective of this actor.
280 CameraActor GetCameraActor() const;
283 * @brief Set the frame-buffer used as a render target.
285 * @param[in] frameBuffer A valid frame-buffer handle to enable off-screen rendering, or an uninitialized handle to disable.
287 void SetTargetFrameBuffer( FrameBufferImage frameBuffer );
290 * @brief Retrieve the frame-buffer used as a render target.
292 * @return A valid frame-buffer handle, or an uninitialised handle if off-screen rendering is disabled.
294 FrameBufferImage GetTargetFrameBuffer() const;
297 * @brief Set the function used to convert screen coordinates to frame-buffer coordinates.
299 * This is useful for hit-testing actors which are rendered off-screen.
301 * @param[in] conversionFunction The conversion function.
303 void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
306 * @brief Retrieve the function used to convert screen coordinates to frame-buffer coordinates.
308 * @return The conversion function.
310 ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
313 * @brief Set the actor used to convert screen coordinates to frame-buffer coordinates.
315 * The local coordinates of the actor are mapped as frame-buffer coordinates.
316 * This is useful for hit-testing actors which are rendered off-screen.
318 * @param[in] mappingActor The actor used for conversion.
319 * @note The mapping actor needs to be rendered by the default render task to make the mapping work properly.
321 void SetScreenToFrameBufferMappingActor( Actor mappingActor );
324 * @brief Retrieve the actor used to convert screen coordinates to frame-buffer coordinates.
326 * @return The actor used for conversion.
328 Actor GetScreenToFrameBufferMappingActor() const;
331 * @brief Set the GL viewport position used when rendering.
333 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
334 * By default this will match the target window or frame-buffer size.
336 * @param[in] position The viewports position (x,y)
337 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
339 void SetViewportPosition( Vector2 position );
342 * @brief Retrieve the GL viewport position used when rendering.
344 * @return The viewport.
346 Vector2 GetCurrentViewportPosition() const;
349 * @brief Set the GL viewport size used when rendering.
351 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
352 * By default this will match the target window or frame-buffer size.
354 * @param[in] size The viewports size (width,height)
356 void SetViewportSize( Vector2 size );
359 * @brief Retrieve the GL viewport size used when rendering.
361 * @return The viewport.
363 Vector2 GetCurrentViewportSize() const;
366 * @brief Set the GL viewport used when rendering.
368 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
369 * By default this will match the target window or frame-buffer size.
371 * @param[in] viewport The new viewport.
372 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
374 void SetViewport( Viewport viewport );
377 * @brief Retrieve the GL viewport used when rendering.
379 * @return The viewport.
381 Viewport GetViewport() const;
384 * @brief Set the clear color used when SetClearEnabled(true) is used.
386 * @param[in] color The new clear color.
388 void SetClearColor( const Vector4& color );
391 * @brief Retrieve the clear color used when SetClearEnabled(true) is used.
393 * @return The clear color.
394 * @note This property can be animated; the return value may not match the value written with SetClearColor().
396 Vector4 GetClearColor() const;
399 * @brief Set whether the render-task will clear the results of previous render-tasks.
401 * The default is false.
404 * @param[in] enabled True if the render-task should clear.
405 * @note The default GL surface is cleared automatically at the
406 * beginning of each frame; this setting is only useful when 2+
407 * render-tasks are used, and the result of the first task needs to
408 * be (partially) cleared before rendering the second.
411 void SetClearEnabled( bool enabled );
414 * @brief Query whether the render-task will clear the results of previous render-tasks.
416 * @return True if the render-task should clear.
418 bool GetClearEnabled() const;
421 * @brief Set whether the render task will cull the actors to the camera's view frustum.
423 * Note that this will only affect image views that use the default vertex shader.
424 * The default mode is to cull actors.
426 * @param[in] cullMode True if the renderers should be culled.
428 void SetCullMode( bool cullMode );
431 * @brief Get the cull mode.
434 * @return True if the render task should cull the actors to the camera's view frustum
436 bool GetCullMode() const;
439 * @brief Set the refresh-rate of the RenderTask.
441 * The default is REFRESH_ALWAYS (1), meaning that the RenderTask
442 * will be processed every frame if the scene graph is changing. It
443 * may be desirable to process less frequently. For example,
444 * SetRefreshRate(3) will process once every 3 frames if the scene
445 * graph is changing. If the scene graph is not changing, then the
446 * render task will not be rendered, regardless of this value.
448 * The REFRESH_ONCE value means that the RenderTask will be
449 * processed once only, to take a snap-shot of the scene.
450 * Repeatedly calling SetRefreshRate(REFRESH_ONCE) will cause more
451 * snap-shots to be taken.
454 * @param[in] refreshRate The new refresh rate.
456 void SetRefreshRate( unsigned int refreshRate );
459 * @brief Query the refresh-rate of the RenderTask.
461 * @return The refresh-rate.
463 unsigned int GetRefreshRate() const;
466 * @brief Get viewport coordinates for given world position
470 * @param[in] position The world position.
471 * @param[out] viewportX The viewport x position.
472 * @param[out] viewportY The viewport y position.
473 * @return true if the position has a screen coordinate
475 bool WorldToViewport(const Vector3 &position, float& viewportX, float& viewportY) const;
478 * @brief Get actor local coordinates for given viewport coordinates
482 * @param[in] actor The actor describing local coordinate system.
483 * @param[in] viewportX The viewport x position.
484 * @param[in] viewportY The viewport y position.
485 * @param[out] localX The local x position.
486 * @param[out] localY The local y position.
487 * @return true if the screen position has a local coordinate
489 bool ViewportToLocal(Actor actor, float viewportX, float viewportY, float &localX, float &localY) const;
494 * @brief If the refresh rate is REFRESH_ONCE, connect to this signal to be notified when a RenderTask has finished.
497 RenderTaskSignalType& FinishedSignal();
499 public: // Not intended for application developers
502 * @brief This constructor is used by Dali New() methods.
504 * @param [in] renderTask A pointer to a newly allocated render-task
506 explicit DALI_INTERNAL RenderTask( Internal::RenderTask* renderTask );
514 #endif //__DALI_RENDER_TASK_H__