Add REQUIRES_SYNC property to RenderTask
[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) 2015 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 // INTERNAL INCLUDES
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>
26
27 namespace Dali
28 {
29 /**
30  * @addtogroup dali_core_rendering_effects
31  * @{
32  */
33
34 class Actor;
35 class CameraActor;
36 class FrameBufferImage;
37 struct Vector4;
38
39 namespace Internal DALI_INTERNAL
40 {
41 class RenderTask;
42 }
43
44 /**
45  * @brief RenderTasks describe how the Dali scene should be rendered.
46  *
47  * The Stage::GetRenderTaskList() method provides access to an ordered list of render-tasks.
48  *
49  * Each RenderTask must specify the source actors to be rendered, and a camera actor from
50  * which the scene is viewed.
51  *
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.
54  *
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.
59  *
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().
62  *
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.
65  *
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.
69  *
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().
72  *
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.
76  *
77  * Signals
78  * | %Signal Name | Method                |
79  * |--------------|-----------------------|
80  * | finished     | @ref FinishedSignal() |
81  * @SINCE_1_0.0
82  */
83 class DALI_IMPORT_API RenderTask : public Handle
84 {
85 public:
86
87   /**
88    * @brief An enumeration of properties belonging to the RenderTask class.
89    * @SINCE_1_0.0
90    */
91   struct Property
92   {
93     enum
94     {
95       /**
96        * @brief name "viewportPosition", type Vector2
97        * @SINCE_1_0.0
98        */
99       VIEWPORT_POSITION = DEFAULT_OBJECT_PROPERTY_START_INDEX,
100       /**
101        * @brief name "viewportSize", type Vector2
102        * @SINCE_1_0.0
103        */
104       VIEWPORT_SIZE,
105       /**
106        * @brief name "clearColor", type Vector4
107        * @SINCE_1_0.0
108        */
109       CLEAR_COLOR,
110       /**
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.
116        * @SINCE_1_1.29
117        */
118       REQUIRES_SYNC,
119     };
120   };
121
122   /**
123    * @brief Typedef for signals sent by this class.
124    * @SINCE_1_0.0
125    */
126   typedef Signal< void (RenderTask& source) > RenderTaskSignalType;
127
128   /**
129    * @brief A pointer to a function for converting screen to frame-buffer coordinates.
130    * @SINCE_1_0.0
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.
133    */
134   typedef bool (* ScreenToFrameBufferFunction)( Vector2& coordinates );
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 (* const ConstScreenToFrameBufferFunction)( Vector2& coordinates );
143
144   /**
145    * @brief The default conversion function returns false for any screen coordinates.
146    *
147    * This effectively disables hit-testing for RenderTasks rendering to a frame buffer.
148    * See also FULLSCREEN_FRAMEBUFFER_FUNCTION below.
149    */
150   static ConstScreenToFrameBufferFunction DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION;
151
152   /**
153    * @brief This conversion function outputs the (unmodified) screen coordinates as frame-buffer coordinates.
154    *
155    * Therefore the contents of an off-screen image is expected to be rendered "full screen".
156    */
157   static ConstScreenToFrameBufferFunction FULLSCREEN_FRAMEBUFFER_FUNCTION;
158
159   /**
160    * @brief The refresh-rate of the RenderTask.
161    * @SINCE_1_0.0
162    */
163   enum RefreshRate
164   {
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
167   };
168
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
175
176   /**
177    * @brief Create an empty RenderTask handle.
178    *
179    * This can be initialised with RenderTaskList::CreateRenderTask().
180    * @SINCE_1_0.0
181    */
182   RenderTask();
183
184   /**
185    * @brief Downcast a handle to RenderTask handle.
186    *
187    * If handle points to a RenderTask the
188    * downcast produces valid handle. If not the returned handle is left uninitialized.
189    * @SINCE_1_0.0
190    * @param[in] handle A handle to an object.
191    * @return A handle to a RenderTask or an uninitialized handle.
192    */
193   static RenderTask DownCast( BaseHandle handle );
194
195   /**
196    * @brief Destructor
197    *
198    * This is non-virtual since derived Handle types must not contain data or virtual methods.
199    * @SINCE_1_0.0
200    */
201   ~RenderTask();
202
203   /**
204    * @brief This copy constructor is required for (smart) pointer semantics.
205    *
206    * @SINCE_1_0.0
207    * @param [in] handle A reference to the copied handle
208    */
209   RenderTask(const RenderTask& handle);
210
211   /**
212    * @brief This assignment operator is required for (smart) pointer semantics.
213    *
214    * @SINCE_1_0.0
215    * @param [in] rhs  A reference to the copied handle
216    * @return A reference to this
217    */
218   RenderTask& operator=(const RenderTask& rhs);
219
220   /**
221    * @brief Set the actors to be rendered.
222    * @SINCE_1_0.0
223    * @param[in] actor This actor and its children will be rendered.
224    * If actor is an empty handle, then nothing will be rendered.
225    */
226   void SetSourceActor( Actor actor );
227
228   /**
229    * @brief Retrieve the actors to be rendered.
230    * @SINCE_1_0.0
231    * @return This actor and its children will be rendered.
232    */
233   Actor GetSourceActor() const;
234
235   /**
236    * @brief Set whether the RenderTask has exclusive access to the source actors; the default is false.
237    * @SINCE_1_0.0
238    * @param[in] exclusive True if the source actors will only be rendered by this render-task.
239    */
240   void SetExclusive( bool exclusive );
241
242   /**
243    * @brief Query whether the RenderTask has exclusive access to the source actors.
244    * @SINCE_1_0.0
245    * @return True if the source actors will only be rendered by this render-task.
246    */
247   bool IsExclusive() const;
248
249   /**
250    * @brief Set whether the render-task should be considered for input handling; the default is true.
251    *
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().
256    * @SINCE_1_0.0
257    * @param[in] enabled True if the render-task should be considered for input handling.
258    */
259   void SetInputEnabled( bool enabled );
260
261   /**
262    * @brief Query whether the render-task should be considered for input handling.
263    * @SINCE_1_0.0
264    * @return True if the render-task should be considered for input handling.
265    */
266   bool GetInputEnabled() const;
267
268   /**
269    * @brief Set the actor from which the scene is viewed.
270    * @SINCE_1_0.0
271    * @param[in] cameraActor The scene is viewed from the perspective of this actor.
272    */
273   void SetCameraActor( CameraActor cameraActor );
274
275   /**
276    * @brief Retrieve the actor from which the scene is viewed.
277    * @SINCE_1_0.0
278    * @return The scene is viewed from the perspective of this actor.
279    */
280   CameraActor GetCameraActor() const;
281
282   /**
283    * @brief Set the frame-buffer used as a render target.
284    * @SINCE_1_0.0
285    * @param[in] frameBuffer A valid frame-buffer handle to enable off-screen rendering, or an uninitialized handle to disable.
286    */
287   void SetTargetFrameBuffer( FrameBufferImage frameBuffer );
288
289   /**
290    * @brief Retrieve the frame-buffer used as a render target.
291    * @SINCE_1_0.0
292    * @return A valid frame-buffer handle, or an uninitialised handle if off-screen rendering is disabled.
293    */
294   FrameBufferImage GetTargetFrameBuffer() const;
295
296   /**
297    * @brief Set the function used to convert screen coordinates to frame-buffer coordinates.
298    *
299    * This is useful for hit-testing actors which are rendered off-screen.
300    * @SINCE_1_0.0
301    * @param[in] conversionFunction The conversion function.
302    */
303   void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
304
305   /**
306    * @brief Retrieve the function used to convert screen coordinates to frame-buffer coordinates.
307    * @SINCE_1_0.0
308    * @return The conversion function.
309    */
310   ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
311
312   /**
313    * @brief Set the actor used to convert screen coordinates to frame-buffer coordinates.
314    *
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.
317    * Note: The mapping actor needs to be rendered by the default render task to make the mapping work properly.
318    * @SINCE_1_0.0
319    * @param[in] mappingActor The actor used for conversion.
320    */
321   void SetScreenToFrameBufferMappingActor( Actor mappingActor );
322
323   /**
324    * @brief Retrieve the actor used to convert screen coordinates to frame-buffer coordinates.
325    * @SINCE_1_0.0
326    * @return The actor used for conversion.
327    */
328   Actor GetScreenToFrameBufferMappingActor() const;
329
330   /**
331    * @brief Set the GL viewport position used when rendering.
332    *
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.
335    * @SINCE_1_0.0
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.
338    */
339   void SetViewportPosition( Vector2 position );
340
341   /**
342    * @brief Retrieve the GL viewport position used when rendering.
343    * @SINCE_1_0.0
344    * @return The viewport.
345    */
346   Vector2 GetCurrentViewportPosition() const;
347
348   /**
349    * @brief Set the GL viewport size used when rendering.
350    *
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.
353    * @SINCE_1_0.0
354    * @param[in] size The viewports size (width,height)
355    */
356   void SetViewportSize( Vector2 size );
357
358   /**
359    * @brief Retrieve the GL viewport size used when rendering.
360    * @SINCE_1_0.0
361    * @return The viewport.
362    */
363   Vector2 GetCurrentViewportSize() const;
364
365   /**
366    * @brief Set the GL viewport used when rendering.
367    *
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.
370    * @SINCE_1_0.0
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.
373    */
374   void SetViewport( Viewport viewport );
375
376   /**
377    * @brief Retrieve the GL viewport used when rendering.
378    * @SINCE_1_0.0
379    * @return The viewport.
380    */
381   Viewport GetViewport() const;
382
383   /**
384    * @brief Set the clear color used when SetClearEnabled(true) is used.
385    * @SINCE_1_0.0
386    * @param[in] color The new clear color.
387    */
388   void SetClearColor( const Vector4& color );
389
390   /**
391    * @brief Retrieve the clear color used when SetClearEnabled(true) is used.
392    * @SINCE_1_0.0
393    * @return The clear color.
394    * @note This property can be animated; the return value may not match the value written with SetClearColor().
395    */
396   Vector4 GetClearColor() const;
397
398   /**
399    * @brief Set whether the render-task will clear the results of previous render-tasks.
400    *
401    * The default is false.
402    *
403    * @SINCE_1_0.0
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.
409    *
410    */
411   void SetClearEnabled( bool enabled );
412
413   /**
414    * @brief Query whether the render-task will clear the results of previous render-tasks.
415    * @SINCE_1_0.0
416    * @return True if the render-task should clear.
417    */
418   bool GetClearEnabled() const;
419
420   /**
421    * @brief Set whether the render task will cull the actors to the camera's view frustum.
422    *
423    * Note that this will only affect image actors that use the default vertex shader.
424    * The default mode is to cull actors.
425    * @SINCE_1_0.0
426    * @param[in] cullMode True if the renderers should be culled.
427    */
428   void SetCullMode( bool cullMode );
429
430   /**
431    * @brief Get the cull mode.
432    *
433    * @SINCE_1_0.0
434    * @return True if the render task should cull the actors to the camera's view frustum
435    */
436   bool GetCullMode() const;
437
438   /**
439    * @brief Set the refresh-rate of the RenderTask.
440    *
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.
447    *
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.
452    *
453    * @SINCE_1_0.0
454    * @param[in] refreshRate The new refresh rate.
455    */
456   void SetRefreshRate( unsigned int refreshRate );
457
458   /**
459    * @brief Query the refresh-rate of the RenderTask.
460    * @SINCE_1_0.0
461    * @return The refresh-rate.
462    */
463   unsigned int GetRefreshRate() const;
464
465   /*
466    * @brief Get viewport coordinates for given world position
467    *
468    * @SINCE_1_1.13
469    *
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
474    */
475   bool WorldToViewport(const Vector3 &position, float& viewportX, float& viewportY) const;
476
477   /*
478    * @brief Get actor local coordinates for given viewport coordinates
479    *
480    * @SINCE_1_1.13
481    *
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
488    */
489   bool ViewportToLocal(Actor actor, float viewportX, float viewportY, float &localX, float &localY) const;
490
491 public: // Signals
492
493   /**
494    * @brief If the refresh rate is REFRESH_ONCE, connect to this signal to be notified when a RenderTask has finished.
495    * @SINCE_1_0.0
496    */
497   RenderTaskSignalType& FinishedSignal();
498
499 public: // Not intended for application developers
500
501   /**
502    * @brief This constructor is used by Dali New() methods.
503    * @SINCE_1_0.0
504    * @param [in] renderTask A pointer to a newly allocated render-task
505    */
506   explicit DALI_INTERNAL RenderTask( Internal::RenderTask* renderTask );
507 };
508
509 /**
510  * @}
511  */
512 } // namespace Dali
513
514 #endif //__DALI_RENDER_TASK_H__