7c6d147e6887c87344664a2ff0cbb43540c1a65c
[platform/core/uifw/dali-core.git] / dali / public-api / render-tasks / render-task.h
1 #ifndef DALI_RENDER_TASK_H
2 #define DALI_RENDER_TASK_H
3
4 /*
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <cstdint> // uint32_t
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/math/viewport.h>
26 #include <dali/public-api/object/handle.h>
27 #include <dali/public-api/object/property-index-ranges.h>
28 #include <dali/public-api/signals/dali-signal.h>
29
30 namespace Dali
31 {
32 /**
33  * @addtogroup dali_core_rendering_effects
34  * @{
35  */
36
37 class Actor;
38 class CameraActor;
39 class FrameBufferImage;
40 class FrameBuffer;
41 struct Vector4;
42
43 namespace Internal DALI_INTERNAL
44 {
45 class RenderTask;
46 }
47
48 /**
49  * @brief RenderTasks describe how the Dali scene should be rendered.
50  *
51  * The Scene provides access to an ordered list of render-tasks.
52  *
53  * Each RenderTask must specify the source actors to be rendered, and a camera actor from
54  * which the scene is viewed.
55  *
56  * RenderTasks may optionally target a frame-buffer, otherwise the default GL surface is used;
57  * typically this is a window provided by the native system.
58  *
59  * By default Dali provides a single RenderTask, which renders the entire actor hierarchy using
60  * a default camera actor and GL surface. If stereoscopic rendering is enabled, Dali will create
61  * two additional render tasks, on for each eye. Each render task will have its own camera parented
62  * to the default camera actor.
63  *
64  * The first RenderTask used for input handling will be the last one rendered, which also has input enabled,
65  * and has a valid source & camera actor; see SetInputEnabled().
66  *
67  * If none of the actors are hit in the last RenderTask rendered, then input handling will continue
68  * with the second last RenderTask rendered, and so on.
69  *
70  * All RenderTasks which target a frame-buffer (i.e. off screen) will be rendered before all RenderTasks
71  * which target the default GL surface. This allows the user to render intermediate targets which are used
72  * later when targeting the screen.
73  *
74  * A RenderTask targeting a frame-buffer can still be hit-tested, provided that the
75  * screen->frame-buffer coordinate conversion is successful; see SetScreenToFrameBufferFunction().
76  *
77  * If the refresh rate id REFRESH_ONCE and a "Finish" signal is connected, it will be emitted when the RenderTask is completed.
78  * Note that all connected signals must be disconnected before the object is destroyed. This is typically done in the
79  * object destructor, and requires either the Dali::Connection object or Dali::RenderTask handle to be stored.
80  *
81  * Signals
82  * | %Signal Name | Method                |
83  * |--------------|-----------------------|
84  * | finished     | @ref FinishedSignal() |
85  * @SINCE_1_0.0
86  */
87 class DALI_CORE_API RenderTask : public Handle
88 {
89 public:
90
91   /**
92    * @brief Enumeration for instances of properties belonging to the RenderTask class.
93    * @SINCE_1_0.0
94    */
95   struct Property
96   {
97     /**
98      * @brief Enumeration for instances of properties belonging to the RenderTask class.
99      * @SINCE_1_0.0
100      */
101     enum
102     {
103       /**
104        * @brief name "viewportPosition", type Vector2
105        * @SINCE_1_0.0
106        */
107       VIEWPORT_POSITION = DEFAULT_OBJECT_PROPERTY_START_INDEX,
108       /**
109        * @brief name "viewportSize", type Vector2
110        * @SINCE_1_0.0
111        */
112       VIEWPORT_SIZE,
113       /**
114        * @brief name "clearColor", type Vector4
115        * @SINCE_1_0.0
116        */
117       CLEAR_COLOR,
118       /**
119        * @brief name "requiresSync", type BOOLEAN
120        * @details By default, the sync object is not created.
121        *  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.
122        *  Thus the RENDER_ONCE finished signal can be emit at the correct timing.
123        * @SINCE_1_1.29
124        * @note The use of GL sync might cause deadlock with multiple access to the single pixmap happening in the same time.
125        */
126       REQUIRES_SYNC,
127     };
128   };
129
130   /**
131    * @brief Typedef for signals sent by this class.
132    * @SINCE_1_0.0
133    */
134   typedef Signal< void (RenderTask& source) > RenderTaskSignalType;
135
136   /**
137    * @brief A pointer to a function for converting screen to frame-buffer coordinates.
138    * @SINCE_1_0.0
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
141    */
142   typedef bool (* ScreenToFrameBufferFunction)( Vector2& coordinates );
143
144   /**
145    * @brief A pointer to a function for converting screen to frame-buffer coordinates.
146    * @SINCE_1_0.0
147    * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen
148    * @return True if the conversion was successful, otherwise coordinates should be unmodified
149    */
150   typedef bool (* const ConstScreenToFrameBufferFunction)( Vector2& coordinates );
151
152   /**
153    * @brief The default conversion function returns false for any screen coordinates.
154    *
155    * This effectively disables hit-testing for RenderTasks rendering to a frame buffer.
156    * See also FULLSCREEN_FRAMEBUFFER_FUNCTION below.
157    */
158   static ConstScreenToFrameBufferFunction DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION;
159
160   /**
161    * @brief This conversion function outputs the (unmodified) screen coordinates as frame-buffer coordinates.
162    *
163    * Therefore the contents of an off-screen image is expected to be rendered "full screen".
164    */
165   static ConstScreenToFrameBufferFunction FULLSCREEN_FRAMEBUFFER_FUNCTION;
166
167   /**
168    * @brief Enumeration for the refresh-rate of the RenderTask.
169    * @SINCE_1_0.0
170    */
171   enum RefreshRate
172   {
173     REFRESH_ONCE   = 0, ///< Process once only e.g. take a snap-shot of the scene. @SINCE_1_0.0
174     REFRESH_ALWAYS = 1  ///< Process every frame. @SINCE_1_0.0
175   };
176
177   static const bool     DEFAULT_EXCLUSIVE;     ///< false
178   static const bool     DEFAULT_INPUT_ENABLED; ///< true
179   static const Vector4  DEFAULT_CLEAR_COLOR;   ///< Color::BLACK
180   static const bool     DEFAULT_CLEAR_ENABLED; ///< false
181   static const bool     DEFAULT_CULL_MODE;     ///< true
182   static const uint32_t DEFAULT_REFRESH_RATE;  ///< REFRESH_ALWAYS
183
184   /**
185    * @brief Creates an empty RenderTask handle.
186    *
187    * This can be initialised with RenderTaskList::CreateRenderTask().
188    * @SINCE_1_0.0
189    */
190   RenderTask();
191
192   /**
193    * @brief Downcasts a handle to RenderTask handle.
194    *
195    * If handle points to a RenderTask, the
196    * downcast produces valid handle. If not, the returned handle is left uninitialized.
197    * @SINCE_1_0.0
198    * @param[in] handle A handle to an object
199    * @return A handle to a RenderTask or an uninitialized handle
200    */
201   static RenderTask DownCast( BaseHandle handle );
202
203   /**
204    * @brief Destructor.
205    *
206    * This is non-virtual since derived Handle types must not contain data or virtual methods.
207    * @SINCE_1_0.0
208    */
209   ~RenderTask();
210
211   /**
212    * @brief This copy constructor is required for (smart) pointer semantics.
213    *
214    * @SINCE_1_0.0
215    * @param[in] handle A reference to the copied handle
216    */
217   RenderTask(const RenderTask& handle);
218
219   /**
220    * @brief This assignment operator is required for (smart) pointer semantics.
221    *
222    * @SINCE_1_0.0
223    * @param[in] rhs A reference to the copied handle
224    * @return A reference to this
225    */
226   RenderTask& operator=(const RenderTask& rhs);
227
228   /**
229    * @brief Move constructor.
230    *
231    * @SINCE_1_9.22
232    * @param[in] rhs A reference to the moved handle
233    */
234   RenderTask( RenderTask&& rhs );
235
236   /**
237    * @brief Move assignment operator.
238    *
239    * @SINCE_1_9.22
240    * @param[in] rhs A reference to the moved handle
241    * @return A reference to this
242    */
243   RenderTask& operator=( RenderTask&& rhs );
244
245   /**
246    * @brief Sets the actors to be rendered.
247    * @SINCE_1_0.0
248    * @param[in] actor This actor and its children will be rendered.
249    * If actor is an empty handle, then nothing will be rendered
250    */
251   void SetSourceActor( Actor actor );
252
253   /**
254    * @brief Retrieves the actors to be rendered.
255    * @SINCE_1_0.0
256    * @return This actor and its children will be rendered
257    */
258   Actor GetSourceActor() const;
259
260   /**
261    * @brief Sets whether the RenderTask has exclusive access to the source actors; the default is false.
262    * @SINCE_1_0.0
263    * @param[in] exclusive True if the source actors will only be rendered by this render-task
264    */
265   void SetExclusive( bool exclusive );
266
267   /**
268    * @brief Queries whether the RenderTask has exclusive access to the source actors.
269    * @SINCE_1_0.0
270    * @return True if the source actors will only be rendered by this render-task
271    */
272   bool IsExclusive() const;
273
274   /**
275    * @brief Sets whether the render-task should be considered for input handling; the default is true.
276    *
277    * The task used for input handling will be last task in the RenderTaskList which has input enabled,
278    * and has a valid source & camera actor.
279    * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the screen->frame-buffer
280    * coordinate conversion is successful; see also SetScreenToFrameBufferFunction().
281    * @SINCE_1_0.0
282    * @param[in] enabled True if the render-task should be considered for input handling
283    */
284   void SetInputEnabled( bool enabled );
285
286   /**
287    * @brief Queries whether the render-task should be considered for input handling.
288    * @SINCE_1_0.0
289    * @return True if the render-task should be considered for input handling
290    */
291   bool GetInputEnabled() const;
292
293   /**
294    * @brief Sets the actor from which the scene is viewed.
295    * @SINCE_1_0.0
296    * @param[in] cameraActor The scene is viewed from the perspective of this actor
297    */
298   void SetCameraActor( CameraActor cameraActor );
299
300   /**
301    * @brief Retrieves the actor from which the scene is viewed.
302    * @SINCE_1_0.0
303    * @return The scene is viewed from the perspective of this actor
304    */
305   CameraActor GetCameraActor() const;
306
307   /**
308    * @brief Sets the frame-buffer used as a render target.
309    * @SINCE_1_1.38
310    * @param[in] frameBuffer A valid FrameBuffer handle to enable off-screen rendering, or an uninitialized handle to disable it
311    */
312   void SetFrameBuffer( FrameBuffer frameBuffer );
313
314   /**
315    * @brief Retrieves the frame-buffer used as a render target.
316    * @SINCE_1_1.38
317    * @return The framebuffer
318    */
319   FrameBuffer GetFrameBuffer() const;
320
321   /**
322    * @brief Sets the function used to convert screen coordinates to frame-buffer coordinates.
323    *
324    * This is useful for hit-testing actors which are rendered off-screen.
325    * @SINCE_1_0.0
326    * @param[in] conversionFunction The conversion function
327    */
328   void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
329
330   /**
331    * @brief Retrieves the function used to convert screen coordinates to frame-buffer coordinates.
332    * @SINCE_1_0.0
333    * @return The conversion function
334    */
335   ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
336
337   /**
338    * @brief Sets the actor used to convert screen coordinates to frame-buffer coordinates.
339    *
340    * The local coordinates of the actor are mapped as frame-buffer coordinates.
341    * This is useful for hit-testing actors which are rendered off-screen.
342    * @SINCE_1_0.0
343    * @param[in] mappingActor The actor used for conversion
344    * @note The mapping actor needs to be rendered by the default render task to make the mapping work properly.
345    */
346   void SetScreenToFrameBufferMappingActor( Actor mappingActor );
347
348   /**
349    * @brief Retrieves the actor used to convert screen coordinates to frame-buffer coordinates.
350    * @SINCE_1_0.0
351    * @return The actor used for conversion
352    */
353   Actor GetScreenToFrameBufferMappingActor() const;
354
355   /**
356    * @brief Sets the GL viewport position used when rendering.
357    *
358    * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
359    * By default this will match the target window or frame-buffer size.
360    * @SINCE_1_0.0
361    * @param[in] position The viewports position (x,y)
362    * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
363    */
364   void SetViewportPosition( Vector2 position );
365
366   /**
367    * @brief Retrieves the GL viewport position used when rendering.
368    * @SINCE_1_0.0
369    * @return The viewport
370    */
371   Vector2 GetCurrentViewportPosition() const;
372
373   /**
374    * @brief Sets the GL viewport size used when rendering.
375    *
376    * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
377    * By default this will match the target window or frame-buffer size.
378    * @SINCE_1_0.0
379    * @param[in] size The viewports size (width,height)
380    */
381   void SetViewportSize( Vector2 size );
382
383   /**
384    * @brief Retrieves the GL viewport size used when rendering.
385    * @SINCE_1_0.0
386    * @return The viewport
387    */
388   Vector2 GetCurrentViewportSize() const;
389
390   /**
391    * @brief Sets the GL viewport used when rendering.
392    *
393    * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
394    * By default this will match the target window or frame-buffer size.
395    * @SINCE_1_0.0
396    * @param[in] viewport The new viewport
397    * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
398    */
399   void SetViewport( Viewport viewport );
400
401   /**
402    * @brief Retrieves the GL viewport used when rendering.
403    * @SINCE_1_0.0
404    * @return The viewport
405    */
406   Viewport GetViewport() const;
407
408   /**
409    * @brief Sets the clear color used when SetClearEnabled(true) is used.
410    * @SINCE_1_0.0
411    * @param[in] color The new clear color
412    */
413   void SetClearColor( const Vector4& color );
414
415   /**
416    * @brief Retrieves the clear color used when SetClearEnabled(true) is used.
417    * @SINCE_1_0.0
418    * @return The clear color
419    * @note This property can be animated; the return value may not match the value written with SetClearColor().
420    */
421   Vector4 GetClearColor() const;
422
423   /**
424    * @brief Sets whether the render-task will clear the results of previous render-tasks.
425    *
426    * The default is false.
427    *
428    * @SINCE_1_0.0
429    * @param[in] enabled True if the render-task should clear
430    * @note The default GL surface is cleared automatically at the
431    * beginning of each frame; this setting is only useful when 2+
432    * render-tasks are used, and the result of the first task needs to
433    * be (partially) cleared before rendering the second.
434    *
435    */
436   void SetClearEnabled( bool enabled );
437
438   /**
439    * @brief Queries whether the render-task will clear the results of previous render-tasks.
440    * @SINCE_1_0.0
441    * @return True if the render-task should clear
442    */
443   bool GetClearEnabled() const;
444
445   /**
446    * @brief Sets whether the render task will cull the actors to the camera's view frustum.
447    *
448    * @SINCE_1_0.0
449    * @param[in] cullMode True if the renderers should be culled
450    * @note The default mode is to cull actors.
451    * @note If the shader uses @ref Shader::Hint::MODIFIES_GEOMETRY then culling optimizations are disabled.
452    * @see Shader::Hint
453    */
454   void SetCullMode( bool cullMode );
455
456   /**
457    * @brief Gets the cull mode.
458    *
459    * @SINCE_1_0.0
460    * @return True if the render task should cull the actors to the camera's view frustum
461    */
462   bool GetCullMode() const;
463
464   /**
465    * @brief Sets the refresh-rate of the RenderTask.
466    *
467    * The default is REFRESH_ALWAYS (1), meaning that the RenderTask
468    * will be processed every frame if the scene graph is changing.  It
469    * may be desirable to process less frequently. For example,
470    * SetRefreshRate(3) will process once every 3 frames if the scene
471    * graph is changing. If the scene graph is not changing, then the
472    * render task will not be rendered, regardless of this value.
473    *
474    * The REFRESH_ONCE value means that the RenderTask will be
475    * processed once only, to take a snap-shot of the scene.
476    * Repeatedly calling SetRefreshRate(REFRESH_ONCE) will cause more
477    * snap-shots to be taken.
478    *
479    * @SINCE_1_0.0
480    * @param[in] refreshRate The new refresh rate
481    */
482   void SetRefreshRate( uint32_t refreshRate );
483
484   /**
485    * @brief Queries the refresh-rate of the RenderTask.
486    * @SINCE_1_0.0
487    * @return The refresh-rate
488    */
489   uint32_t GetRefreshRate() const;
490
491   /**
492    * @brief Gets viewport coordinates for given world position.
493    *
494    * @SINCE_1_1.13
495    * @param[in] position The world position
496    * @param[out] viewportX The viewport x position
497    * @param[out] viewportY The viewport y position
498    * @return true if the position has a screen coordinate
499    */
500   bool WorldToViewport(const Vector3 &position, float& viewportX, float& viewportY) const;
501
502   /**
503    * @brief Gets actor local coordinates for given viewport coordinates.
504    *
505    * @SINCE_1_1.13
506    * @param[in] actor The actor describing local coordinate system
507    * @param[in] viewportX The viewport x position
508    * @param[in] viewportY The viewport y position
509    * @param[out] localX The local x position
510    * @param[out] localY The local y position
511    * @return true if the screen position has a local coordinate
512    */
513   bool ViewportToLocal(Actor actor, float viewportX, float viewportY, float &localX, float &localY) const;
514
515 public: // Signals
516
517   /**
518    * @brief If the refresh rate is REFRESH_ONCE, connect to this signal to be notified when a RenderTask has finished.
519    * @SINCE_1_0.0
520    * @return The signal to connect to
521    */
522   RenderTaskSignalType& FinishedSignal();
523
524 public: // Not intended for application developers
525
526   /// @cond internal
527   /**
528    * @brief This constructor is used by Dali New() methods.
529    * @SINCE_1_0.0
530    * @param[in] renderTask A pointer to a newly allocated render-task
531    */
532   explicit DALI_INTERNAL RenderTask( Internal::RenderTask* renderTask );
533   /// @endcond
534 };
535
536 /**
537  * @}
538  */
539 } // namespace Dali
540
541 #endif //DALI_RENDER_TASK_H