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;
40 namespace Internal DALI_INTERNAL
46 * @brief RenderTasks describe how the Dali scene should be rendered.
48 * The Stage::GetRenderTaskList() method provides access to an ordered list of render-tasks.
50 * Each RenderTask must specify the source actors to be rendered, and a camera actor from
51 * which the scene is viewed.
53 * RenderTasks may optionally target a frame-buffer, otherwise the default GL surface is used;
54 * typically this is a window provided by the native system.
56 * By default Dali provides a single RenderTask, which renders the entire actor hierachy using
57 * a default camera actor and GL surface. If stereoscopic rendering is enabled, Dali will create
58 * two additional render tasks, on for each eye. Each render task will have its own camera parented
59 * to the default camera actor.
61 * The first RenderTask used for input handling will be the last one rendered, which also has input enabled,
62 * and has a valid source & camera actor; see SetInputEnabled().
64 * If none of the actors are hit in the last RenderTask rendered, then input handling will continue
65 * with the second last RenderTask rendered, and so on.
67 * All RenderTasks which target a frame-buffer (i.e. off screen) will be rendered before all RenderTasks
68 * which target the default GL surface. This allows the user to render intermediate targets which are used
69 * later when targetting the screen.
71 * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the
72 * screen->frame-buffer coordinate conversion is successful; see SetScreenToFrameBufferFunction().
74 * If the refresh rate id REFRESH_ONCE and a "Finish" signal is connected, it will be emitted when the RenderTask is completed.
75 * Note that all connected signals must be disconnected before the object is destroyed. This is typically done in the
76 * object destructor, and requires either the Dali::Connection object or Dali::RenderTask handle to be stored.
79 * | %Signal Name | Method |
80 * |--------------|-----------------------|
81 * | finished | @ref FinishedSignal() |
84 class DALI_IMPORT_API RenderTask : public Handle
89 * @brief An enumeration of properties belonging to the RenderTask class.
95 * @brief An enumeration of properties belonging to the RenderTask class.
101 * @brief name "viewportPosition", type Vector2
104 VIEWPORT_POSITION = DEFAULT_OBJECT_PROPERTY_START_INDEX,
106 * @brief name "viewportSize", type Vector2
111 * @brief name "clearColor", type Vector4
116 * @brief name "requiresSync", type BOOLEAN
117 * @details By default, the sync object is not created.
118 * 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.
119 * Thus the RENDER_ONCE finished signal can be emit at the correct timing.
120 * @note The use of GL sync might cause deadlock with multiple access to the single pixmap happening in the same time.
128 * @brief Typedef for signals sent by this class.
131 typedef Signal< void (RenderTask& source) > RenderTaskSignalType;
134 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
136 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
137 * @return True if the conversion was successful, otherwise coordinates should be unmodified.
139 typedef bool (* ScreenToFrameBufferFunction)( Vector2& coordinates );
142 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
144 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
145 * @return True if the conversion was successful, otherwise coordinates should be unmodified.
147 typedef bool (* const ConstScreenToFrameBufferFunction)( Vector2& coordinates );
150 * @brief The default conversion function returns false for any screen coordinates.
152 * This effectively disables hit-testing for RenderTasks rendering to a frame buffer.
153 * See also FULLSCREEN_FRAMEBUFFER_FUNCTION below.
155 static ConstScreenToFrameBufferFunction DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION;
158 * @brief This conversion function outputs the (unmodified) screen coordinates as frame-buffer coordinates.
160 * Therefore the contents of an off-screen image is expected to be rendered "full screen".
162 static ConstScreenToFrameBufferFunction FULLSCREEN_FRAMEBUFFER_FUNCTION;
165 * @brief The refresh-rate of the RenderTask.
170 REFRESH_ONCE = 0, ///< Process once only e.g. take a snap-shot of the scene. @SINCE_1_0.0
171 REFRESH_ALWAYS = 1 ///< Process every frame. @SINCE_1_0.0
174 static const bool DEFAULT_EXCLUSIVE; ///< false
175 static const bool DEFAULT_INPUT_ENABLED; ///< true
176 static const Vector4 DEFAULT_CLEAR_COLOR; ///< Color::BLACK
177 static const bool DEFAULT_CLEAR_ENABLED; ///< false
178 static const bool DEFAULT_CULL_MODE; ///< true
179 static const unsigned int DEFAULT_REFRESH_RATE; ///< REFRESH_ALWAYS
182 * @brief Create an empty RenderTask handle.
184 * This can be initialised with RenderTaskList::CreateRenderTask().
190 * @brief Downcast a handle to RenderTask handle.
192 * If handle points to a RenderTask the
193 * downcast produces valid handle. If not the returned handle is left uninitialized.
195 * @param[in] handle A handle to an object.
196 * @return A handle to a RenderTask or an uninitialized handle.
198 static RenderTask DownCast( BaseHandle handle );
203 * This is non-virtual since derived Handle types must not contain data or virtual methods.
209 * @brief This copy constructor is required for (smart) pointer semantics.
212 * @param [in] handle A reference to the copied handle
214 RenderTask(const RenderTask& handle);
217 * @brief This assignment operator is required for (smart) pointer semantics.
220 * @param [in] rhs A reference to the copied handle
221 * @return A reference to this
223 RenderTask& operator=(const RenderTask& rhs);
226 * @brief Set the actors to be rendered.
228 * @param[in] actor This actor and its children will be rendered.
229 * If actor is an empty handle, then nothing will be rendered.
231 void SetSourceActor( Actor actor );
234 * @brief Retrieve the actors to be rendered.
236 * @return This actor and its children will be rendered.
238 Actor GetSourceActor() const;
241 * @brief Set whether the RenderTask has exclusive access to the source actors; the default is false.
243 * @param[in] exclusive True if the source actors will only be rendered by this render-task.
245 void SetExclusive( bool exclusive );
248 * @brief Query whether the RenderTask has exclusive access to the source actors.
250 * @return True if the source actors will only be rendered by this render-task.
252 bool IsExclusive() const;
255 * @brief Set whether the render-task should be considered for input handling; the default is true.
257 * The task used for input handling will be last task in the RenderTaskList which has input enabled,
258 * and has a valid source & camera actor.
259 * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the screen->frame-buffer
260 * coordinate conversion is successful; see also SetScreenToFrameBufferFunction().
262 * @param[in] enabled True if the render-task should be considered for input handling.
264 void SetInputEnabled( bool enabled );
267 * @brief Query whether the render-task should be considered for input handling.
269 * @return True if the render-task should be considered for input handling.
271 bool GetInputEnabled() const;
274 * @brief Set the actor from which the scene is viewed.
276 * @param[in] cameraActor The scene is viewed from the perspective of this actor.
278 void SetCameraActor( CameraActor cameraActor );
281 * @brief Retrieve the actor from which the scene is viewed.
283 * @return The scene is viewed from the perspective of this actor.
285 CameraActor GetCameraActor() const;
288 * @brief Set the frame-buffer used as a render target.
290 * @param[in] frameBuffer A valid frame-buffer handle to enable off-screen rendering, or an uninitialized handle to disable.
292 void SetTargetFrameBuffer( FrameBufferImage frameBuffer );
295 * @brief Retrieve the frame-buffer used as a render target.
297 * @return A valid frame-buffer handle, or an uninitialised handle if off-screen rendering is disabled.
299 FrameBufferImage GetTargetFrameBuffer() const;
302 * @brief Set the frame-buffer used as a render target.
304 * @param[in] frameBuffer er A valid FrameBuffer handle to enable off-screen rendering, or an uninitialized handle to disable it.
306 void SetFrameBuffer( FrameBuffer frameBuffer );
309 * @brief Retrieve the frame-buffer used as a render target.
311 * @return The framebuffer
313 FrameBuffer GetFrameBuffer() const;
316 * @brief Set the function used to convert screen coordinates to frame-buffer coordinates.
318 * This is useful for hit-testing actors which are rendered off-screen.
320 * @param[in] conversionFunction The conversion function.
322 void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
325 * @brief Retrieve the function used to convert screen coordinates to frame-buffer coordinates.
327 * @return The conversion function.
329 ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
332 * @brief Set the actor used to convert screen coordinates to frame-buffer coordinates.
334 * The local coordinates of the actor are mapped as frame-buffer coordinates.
335 * This is useful for hit-testing actors which are rendered off-screen.
337 * @param[in] mappingActor The actor used for conversion.
338 * @note The mapping actor needs to be rendered by the default render task to make the mapping work properly.
340 void SetScreenToFrameBufferMappingActor( Actor mappingActor );
343 * @brief Retrieve the actor used to convert screen coordinates to frame-buffer coordinates.
345 * @return The actor used for conversion.
347 Actor GetScreenToFrameBufferMappingActor() const;
350 * @brief Set the GL viewport position used when rendering.
352 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
353 * By default this will match the target window or frame-buffer size.
355 * @param[in] position The viewports position (x,y)
356 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
358 void SetViewportPosition( Vector2 position );
361 * @brief Retrieve the GL viewport position used when rendering.
363 * @return The viewport.
365 Vector2 GetCurrentViewportPosition() const;
368 * @brief Set the GL viewport size used when rendering.
370 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
371 * By default this will match the target window or frame-buffer size.
373 * @param[in] size The viewports size (width,height)
375 void SetViewportSize( Vector2 size );
378 * @brief Retrieve the GL viewport size used when rendering.
380 * @return The viewport.
382 Vector2 GetCurrentViewportSize() const;
385 * @brief Set the GL viewport used when rendering.
387 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
388 * By default this will match the target window or frame-buffer size.
390 * @param[in] viewport The new viewport.
391 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
393 void SetViewport( Viewport viewport );
396 * @brief Retrieve the GL viewport used when rendering.
398 * @return The viewport.
400 Viewport GetViewport() const;
403 * @brief Set the clear color used when SetClearEnabled(true) is used.
405 * @param[in] color The new clear color.
407 void SetClearColor( const Vector4& color );
410 * @brief Retrieve the clear color used when SetClearEnabled(true) is used.
412 * @return The clear color.
413 * @note This property can be animated; the return value may not match the value written with SetClearColor().
415 Vector4 GetClearColor() const;
418 * @brief Set whether the render-task will clear the results of previous render-tasks.
420 * The default is false.
423 * @param[in] enabled True if the render-task should clear.
424 * @note The default GL surface is cleared automatically at the
425 * beginning of each frame; this setting is only useful when 2+
426 * render-tasks are used, and the result of the first task needs to
427 * be (partially) cleared before rendering the second.
430 void SetClearEnabled( bool enabled );
433 * @brief Query whether the render-task will clear the results of previous render-tasks.
435 * @return True if the render-task should clear.
437 bool GetClearEnabled() const;
440 * @brief Set whether the render task will cull the actors to the camera's view frustum.
443 * @param[in] cullMode True if the renderers should be culled.
444 * @note The default mode is to cull actors.
445 * @note If the shader uses @ref Shader::Hint::MODIFIES_GEOMETRY then culling optimizations are disabled.
448 void SetCullMode( bool cullMode );
451 * @brief Get the cull mode.
454 * @return True if the render task should cull the actors to the camera's view frustum
456 bool GetCullMode() const;
459 * @brief Set the refresh-rate of the RenderTask.
461 * The default is REFRESH_ALWAYS (1), meaning that the RenderTask
462 * will be processed every frame if the scene graph is changing. It
463 * may be desirable to process less frequently. For example,
464 * SetRefreshRate(3) will process once every 3 frames if the scene
465 * graph is changing. If the scene graph is not changing, then the
466 * render task will not be rendered, regardless of this value.
468 * The REFRESH_ONCE value means that the RenderTask will be
469 * processed once only, to take a snap-shot of the scene.
470 * Repeatedly calling SetRefreshRate(REFRESH_ONCE) will cause more
471 * snap-shots to be taken.
474 * @param[in] refreshRate The new refresh rate.
476 void SetRefreshRate( unsigned int refreshRate );
479 * @brief Query the refresh-rate of the RenderTask.
481 * @return The refresh-rate.
483 unsigned int GetRefreshRate() const;
486 * @brief Get viewport coordinates for given world position
490 * @param[in] position The world position.
491 * @param[out] viewportX The viewport x position.
492 * @param[out] viewportY The viewport y position.
493 * @return true if the position has a screen coordinate
495 bool WorldToViewport(const Vector3 &position, float& viewportX, float& viewportY) const;
498 * @brief Get actor local coordinates for given viewport coordinates
502 * @param[in] actor The actor describing local coordinate system.
503 * @param[in] viewportX The viewport x position.
504 * @param[in] viewportY The viewport y position.
505 * @param[out] localX The local x position.
506 * @param[out] localY The local y position.
507 * @return true if the screen position has a local coordinate
509 bool ViewportToLocal(Actor actor, float viewportX, float viewportY, float &localX, float &localY) const;
514 * @brief If the refresh rate is REFRESH_ONCE, connect to this signal to be notified when a RenderTask has finished.
516 * @return The signal to connect to
518 RenderTaskSignalType& FinishedSignal();
520 public: // Not intended for application developers
524 * @brief This constructor is used by Dali New() methods.
526 * @param [in] renderTask A pointer to a newly allocated render-task
528 explicit DALI_INTERNAL RenderTask( Internal::RenderTask* renderTask );
536 #endif //__DALI_RENDER_TASK_H__