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