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.
97 * @brief name "viewportPosition", type Vector2
100 VIEWPORT_POSITION = DEFAULT_OBJECT_PROPERTY_START_INDEX,
102 * @brief name "viewportSize", type Vector2
107 * @brief name "clearColor", type Vector4
112 * @brief name "requiresSync", type BOOLEAN
113 * @details By default, the sync object is not created.
114 * 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.
115 * Thus the RENDER_ONCE finished signal can be emit at the correct timing.
116 * @note The use of GL sync might cause deadlock with multiple access to the single pixmap happening in the same time.
124 * @brief Typedef for signals sent by this class.
127 typedef Signal< void (RenderTask& source) > RenderTaskSignalType;
130 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
132 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
133 * @return True if the conversion was successful, otherwise coordinates should be unmodified.
135 typedef bool (* ScreenToFrameBufferFunction)( Vector2& coordinates );
138 * @brief A pointer to a function for converting screen to frame-buffer coordinates.
140 * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
141 * @return True if the conversion was successful, otherwise coordinates should be unmodified.
143 typedef bool (* const ConstScreenToFrameBufferFunction)( Vector2& coordinates );
146 * @brief The default conversion function returns false for any screen coordinates.
148 * This effectively disables hit-testing for RenderTasks rendering to a frame buffer.
149 * See also FULLSCREEN_FRAMEBUFFER_FUNCTION below.
151 static ConstScreenToFrameBufferFunction DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION;
154 * @brief This conversion function outputs the (unmodified) screen coordinates as frame-buffer coordinates.
156 * Therefore the contents of an off-screen image is expected to be rendered "full screen".
158 static ConstScreenToFrameBufferFunction FULLSCREEN_FRAMEBUFFER_FUNCTION;
161 * @brief The refresh-rate of the RenderTask.
166 REFRESH_ONCE = 0, ///< Process once only e.g. take a snap-shot of the scene. @SINCE_1_0.0
167 REFRESH_ALWAYS = 1 ///< Process every frame. @SINCE_1_0.0
170 static const bool DEFAULT_EXCLUSIVE; ///< false
171 static const bool DEFAULT_INPUT_ENABLED; ///< true
172 static const Vector4 DEFAULT_CLEAR_COLOR; ///< Color::BLACK
173 static const bool DEFAULT_CLEAR_ENABLED; ///< false
174 static const bool DEFAULT_CULL_MODE; ///< true
175 static const unsigned int DEFAULT_REFRESH_RATE; ///< REFRESH_ALWAYS
178 * @brief Create an empty RenderTask handle.
180 * This can be initialised with RenderTaskList::CreateRenderTask().
186 * @brief Downcast a handle to RenderTask handle.
188 * If handle points to a RenderTask the
189 * downcast produces valid handle. If not the returned handle is left uninitialized.
191 * @param[in] handle A handle to an object.
192 * @return A handle to a RenderTask or an uninitialized handle.
194 static RenderTask DownCast( BaseHandle handle );
199 * This is non-virtual since derived Handle types must not contain data or virtual methods.
205 * @brief This copy constructor is required for (smart) pointer semantics.
208 * @param [in] handle A reference to the copied handle
210 RenderTask(const RenderTask& handle);
213 * @brief This assignment operator is required for (smart) pointer semantics.
216 * @param [in] rhs A reference to the copied handle
217 * @return A reference to this
219 RenderTask& operator=(const RenderTask& rhs);
222 * @brief Set the actors to be rendered.
224 * @param[in] actor This actor and its children will be rendered.
225 * If actor is an empty handle, then nothing will be rendered.
227 void SetSourceActor( Actor actor );
230 * @brief Retrieve the actors to be rendered.
232 * @return This actor and its children will be rendered.
234 Actor GetSourceActor() const;
237 * @brief Set whether the RenderTask has exclusive access to the source actors; the default is false.
239 * @param[in] exclusive True if the source actors will only be rendered by this render-task.
241 void SetExclusive( bool exclusive );
244 * @brief Query whether the RenderTask has exclusive access to the source actors.
246 * @return True if the source actors will only be rendered by this render-task.
248 bool IsExclusive() const;
251 * @brief Set whether the render-task should be considered for input handling; the default is true.
253 * The task used for input handling will be last task in the RenderTaskList which has input enabled,
254 * and has a valid source & camera actor.
255 * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the screen->frame-buffer
256 * coordinate conversion is successful; see also SetScreenToFrameBufferFunction().
258 * @param[in] enabled True if the render-task should be considered for input handling.
260 void SetInputEnabled( bool enabled );
263 * @brief Query whether the render-task should be considered for input handling.
265 * @return True if the render-task should be considered for input handling.
267 bool GetInputEnabled() const;
270 * @brief Set the actor from which the scene is viewed.
272 * @param[in] cameraActor The scene is viewed from the perspective of this actor.
274 void SetCameraActor( CameraActor cameraActor );
277 * @brief Retrieve the actor from which the scene is viewed.
279 * @return The scene is viewed from the perspective of this actor.
281 CameraActor GetCameraActor() const;
284 * @brief Set the frame-buffer used as a render target.
286 * @param[in] frameBuffer A valid frame-buffer handle to enable off-screen rendering, or an uninitialized handle to disable.
288 void SetTargetFrameBuffer( FrameBufferImage frameBuffer );
291 * @brief Retrieve the frame-buffer used as a render target.
293 * @return A valid frame-buffer handle, or an uninitialised handle if off-screen rendering is disabled.
295 FrameBufferImage GetTargetFrameBuffer() const;
298 * @brief Set the frame-buffer used as a render target.
300 * @param[in] frameBuffer er A valid FrameBuffer handle to enable off-screen rendering, or an uninitialized handle to disable it.
302 void SetFrameBuffer( FrameBuffer frameBuffer );
305 * @brief Retrieve the frame-buffer used as a render target.
307 * @return The framebuffer
309 FrameBuffer GetFrameBuffer() const;
312 * @brief Set the function used to convert screen coordinates to frame-buffer coordinates.
314 * This is useful for hit-testing actors which are rendered off-screen.
316 * @param[in] conversionFunction The conversion function.
318 void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
321 * @brief Retrieve the function used to convert screen coordinates to frame-buffer coordinates.
323 * @return The conversion function.
325 ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
328 * @brief Set the actor used to convert screen coordinates to frame-buffer coordinates.
330 * The local coordinates of the actor are mapped as frame-buffer coordinates.
331 * This is useful for hit-testing actors which are rendered off-screen.
333 * @param[in] mappingActor The actor used for conversion.
334 * @note The mapping actor needs to be rendered by the default render task to make the mapping work properly.
336 void SetScreenToFrameBufferMappingActor( Actor mappingActor );
339 * @brief Retrieve the actor used to convert screen coordinates to frame-buffer coordinates.
341 * @return The actor used for conversion.
343 Actor GetScreenToFrameBufferMappingActor() const;
346 * @brief Set the GL viewport position used when rendering.
348 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
349 * By default this will match the target window or frame-buffer size.
351 * @param[in] position The viewports position (x,y)
352 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
354 void SetViewportPosition( Vector2 position );
357 * @brief Retrieve the GL viewport position used when rendering.
359 * @return The viewport.
361 Vector2 GetCurrentViewportPosition() const;
364 * @brief Set the GL viewport size used when rendering.
366 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
367 * By default this will match the target window or frame-buffer size.
369 * @param[in] size The viewports size (width,height)
371 void SetViewportSize( Vector2 size );
374 * @brief Retrieve the GL viewport size used when rendering.
376 * @return The viewport.
378 Vector2 GetCurrentViewportSize() const;
381 * @brief Set the GL viewport used when rendering.
383 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
384 * By default this will match the target window or frame-buffer size.
386 * @param[in] viewport The new viewport.
387 * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
389 void SetViewport( Viewport viewport );
392 * @brief Retrieve the GL viewport used when rendering.
394 * @return The viewport.
396 Viewport GetViewport() const;
399 * @brief Set the clear color used when SetClearEnabled(true) is used.
401 * @param[in] color The new clear color.
403 void SetClearColor( const Vector4& color );
406 * @brief Retrieve the clear color used when SetClearEnabled(true) is used.
408 * @return The clear color.
409 * @note This property can be animated; the return value may not match the value written with SetClearColor().
411 Vector4 GetClearColor() const;
414 * @brief Set whether the render-task will clear the results of previous render-tasks.
416 * The default is false.
419 * @param[in] enabled True if the render-task should clear.
420 * @note The default GL surface is cleared automatically at the
421 * beginning of each frame; this setting is only useful when 2+
422 * render-tasks are used, and the result of the first task needs to
423 * be (partially) cleared before rendering the second.
426 void SetClearEnabled( bool enabled );
429 * @brief Query whether the render-task will clear the results of previous render-tasks.
431 * @return True if the render-task should clear.
433 bool GetClearEnabled() const;
436 * @brief Set whether the render task will cull the actors to the camera's view frustum.
439 * @param[in] cullMode True if the renderers should be culled.
440 * @note The default mode is to cull actors.
441 * @note If the shader uses @ref Shader::Hint::MODIFIES_GEOMETRY then culling optimizations are disabled.
444 void SetCullMode( bool cullMode );
447 * @brief Get the cull mode.
450 * @return True if the render task should cull the actors to the camera's view frustum
452 bool GetCullMode() const;
455 * @brief Set the refresh-rate of the RenderTask.
457 * The default is REFRESH_ALWAYS (1), meaning that the RenderTask
458 * will be processed every frame if the scene graph is changing. It
459 * may be desirable to process less frequently. For example,
460 * SetRefreshRate(3) will process once every 3 frames if the scene
461 * graph is changing. If the scene graph is not changing, then the
462 * render task will not be rendered, regardless of this value.
464 * The REFRESH_ONCE value means that the RenderTask will be
465 * processed once only, to take a snap-shot of the scene.
466 * Repeatedly calling SetRefreshRate(REFRESH_ONCE) will cause more
467 * snap-shots to be taken.
470 * @param[in] refreshRate The new refresh rate.
472 void SetRefreshRate( unsigned int refreshRate );
475 * @brief Query the refresh-rate of the RenderTask.
477 * @return The refresh-rate.
479 unsigned int GetRefreshRate() const;
482 * @brief Get viewport coordinates for given world position
486 * @param[in] position The world position.
487 * @param[out] viewportX The viewport x position.
488 * @param[out] viewportY The viewport y position.
489 * @return true if the position has a screen coordinate
491 bool WorldToViewport(const Vector3 &position, float& viewportX, float& viewportY) const;
494 * @brief Get actor local coordinates for given viewport coordinates
498 * @param[in] actor The actor describing local coordinate system.
499 * @param[in] viewportX The viewport x position.
500 * @param[in] viewportY The viewport y position.
501 * @param[out] localX The local x position.
502 * @param[out] localY The local y position.
503 * @return true if the screen position has a local coordinate
505 bool ViewportToLocal(Actor actor, float viewportX, float viewportY, float &localX, float &localY) const;
510 * @brief If the refresh rate is REFRESH_ONCE, connect to this signal to be notified when a RenderTask has finished.
513 RenderTaskSignalType& FinishedSignal();
515 public: // Not intended for application developers
518 * @brief This constructor is used by Dali New() methods.
520 * @param [in] renderTask A pointer to a newly allocated render-task
522 explicit DALI_INTERNAL RenderTask( Internal::RenderTask* renderTask );
530 #endif //__DALI_RENDER_TASK_H__