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