Merge branch 'tizen' of platform/core/uifw/dali-core into devel/new_mesh
[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) 2014 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 class Actor;
31 class CameraActor;
32 class FrameBufferImage;
33 struct Vector4;
34
35 namespace Internal DALI_INTERNAL
36 {
37 class RenderTask;
38 }
39
40 /**
41  * @brief RenderTasks describe how the Dali scene should be rendered.
42  *
43  * The Stage::GetRenderTaskList() method provides access to an ordered list of render-tasks.
44  *
45  * Each RenderTask must specify the source actors to be rendered, and a camera actor from
46  * which the scene is viewed.
47  *
48  * RenderTasks may optionally target a frame-buffer, otherwise the default GL surface is used;
49  * typically this is a window provided by the native system.
50  *
51  * By default Dali provides a single RenderTask, which renders the entire actor hierachy using
52  * a default camera actor and GL surface. If stereoscopic rendering is enabled, Dali will create
53  * two additional render tasks, on for each eye. Each render task will have its own camera parented
54  * to the default camera actor.
55  *
56  * The first RenderTask used for input handling will be the last one rendered, which also has input enabled,
57  * and has a valid source & camera actor; see SetInputEnabled().
58  *
59  * If none of the actors are hit in the last RenderTask rendered, then input handling will continue
60  * with the second last RenderTask rendered, and so on.
61  *
62  * All RenderTasks which target a frame-buffer (i.e. off screen) will be rendered before all RenderTasks
63  * which target the default GL surface. This allows the user to render intermediate targets which are used
64  * later when targetting the screen.
65  *
66  * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the
67  * screen->frame-buffer coordinate conversion is successful; see SetScreenToFrameBufferFunction().
68  *
69  * If the refresh rate id REFRESH_ONCE and a "Finish" signal is connected, it will be emitted when the RenderTask is completed.
70  * Note that all connected signals must be disconnected before the object is destroyed. This is typically done in the
71  * object destructor, and requires either the Dali::Connection object or Dali::RenderTask handle to be stored.
72  *
73  * Signals
74  * | %Signal Name | Method                |
75  * |--------------|-----------------------|
76  * | finished     | @ref FinishedSignal() |
77  */
78 class DALI_IMPORT_API RenderTask : public Handle
79 {
80 public:
81
82   /**
83    * @brief An enumeration of properties belonging to the RenderTask class.
84    */
85   struct Property
86   {
87     enum
88     {
89       VIEWPORT_POSITION = DEFAULT_OBJECT_PROPERTY_START_INDEX, ///< name "viewport-position", type Vector2
90       VIEWPORT_SIZE,                                           ///< name "viewport-size",     type Vector2
91       CLEAR_COLOR,                                             ///< name "clear-color",       type Vector4
92     };
93   };
94
95   /**
96    * @brief Typedef for signals sent by this class.
97    */
98   typedef Signal< void (RenderTask& source) > RenderTaskSignalType;
99
100   /**
101    * @brief A pointer to a function for converting screen to frame-buffer coordinates.
102    * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
103    * @return True if the conversion was successful, otherwise coordinates should be unmodified.
104    */
105   typedef bool (* ScreenToFrameBufferFunction)( Vector2& coordinates );
106
107   /**
108    * @brief A pointer to a function for converting screen to frame-buffer coordinates.
109    * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
110    * @return True if the conversion was successful, otherwise coordinates should be unmodified.
111    */
112   typedef bool (* const ConstScreenToFrameBufferFunction)( Vector2& coordinates );
113
114   /**
115    * @brief The default conversion function returns false for any screen coordinates.
116    *
117    * This effectively disables hit-testing for RenderTasks rendering to a frame buffer.
118    * See also FULLSCREEN_FRAMEBUFFER_FUNCTION below.
119    */
120   static ConstScreenToFrameBufferFunction DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION;
121
122   /**
123    * @brief This conversion function outputs the (unmodified) screen coordinates as frame-buffer coordinates.
124    *
125    * Therefore the contents of an off-screen image is expected to be rendered "full screen".
126    */
127   static ConstScreenToFrameBufferFunction FULLSCREEN_FRAMEBUFFER_FUNCTION;
128
129   /**
130    * @brief The refresh-rate of the RenderTask.
131    */
132   enum RefreshRate
133   {
134     REFRESH_ONCE   = 0, ///< Process once only e.g. take a snap-shot of the scene.
135     REFRESH_ALWAYS = 1  ///< Process every frame.
136   };
137
138   static const bool         DEFAULT_EXCLUSIVE;     ///< false
139   static const bool         DEFAULT_INPUT_ENABLED; ///< true
140   static const Vector4      DEFAULT_CLEAR_COLOR;   ///< Color::BLACK
141   static const bool         DEFAULT_CLEAR_ENABLED; ///< false
142   static const bool         DEFAULT_CULL_MODE;     ///< true
143   static const unsigned int DEFAULT_REFRESH_RATE;  ///< REFRESH_ALWAYS
144
145   /**
146    * @brief Create an empty RenderTask handle.
147    *
148    * This can be initialised with RenderTaskList::CreateRenderTask().
149    */
150   RenderTask();
151
152   /**
153    * @brief Downcast a handle to RenderTask handle.
154    *
155    * If handle points to a RenderTask the
156    * downcast produces valid handle. If not the returned handle is left uninitialized.
157    * @param[in] handle A handle to an object.
158    * @return A handle to a RenderTask or an uninitialized handle.
159    */
160   static RenderTask DownCast( BaseHandle handle );
161
162   /**
163    * @brief Destructor
164    *
165    * This is non-virtual since derived Handle types must not contain data or virtual methods.
166    */
167   ~RenderTask();
168
169   /**
170    * @brief This copy constructor is required for (smart) pointer semantics.
171    *
172    * @param [in] handle A reference to the copied handle
173    */
174   RenderTask(const RenderTask& handle);
175
176   /**
177    * @brief This assignment operator is required for (smart) pointer semantics.
178    *
179    * @param [in] rhs  A reference to the copied handle
180    * @return A reference to this
181    */
182   RenderTask& operator=(const RenderTask& rhs);
183
184   /**
185    * @brief Set the actors to be rendered.
186    * @param[in] actor This actor and its children will be rendered.
187    * If actor is an empty handle, then nothing will be rendered.
188    */
189   void SetSourceActor( Actor actor );
190
191   /**
192    * @brief Retrieve the actors to be rendered.
193    * @return This actor and its children will be rendered.
194    */
195   Actor GetSourceActor() const;
196
197   /**
198    * @brief Set whether the RenderTask has exclusive access to the source actors; the default is false.
199    * @param[in] exclusive True if the source actors will only be rendered by this render-task.
200    */
201   void SetExclusive( bool exclusive );
202
203   /**
204    * @brief Query whether the RenderTask has exclusive access to the source actors.
205    * @return True if the source actors will only be rendered by this render-task.
206    */
207   bool IsExclusive() const;
208
209   /**
210    * @brief Set whether the render-task should be considered for input handling; the default is true.
211    *
212    * The task used for input handling will be last task in the RenderTaskList which has input enabled,
213    * and has a valid source & camera actor.
214    * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the screen->frame-buffer
215    * coordinate conversion is successful; see also SetScreenToFrameBufferFunction().
216    * @param[in] enabled True if the render-task should be considered for input handling.
217    */
218   void SetInputEnabled( bool enabled );
219
220   /**
221    * @brief Query whether the render-task should be considered for input handling.
222    * @return True if the render-task should be considered for input handling.
223    */
224   bool GetInputEnabled() const;
225
226   /**
227    * @brief Set the actor from which the scene is viewed.
228    * @param[in] cameraActor The scene is viewed from the perspective of this actor.
229    */
230   void SetCameraActor( CameraActor cameraActor );
231
232   /**
233    * @brief Retrieve the actor from which the scene is viewed.
234    * @return The scene is viewed from the perspective of this actor.
235    */
236   CameraActor GetCameraActor() const;
237
238   /**
239    * @brief Set the frame-buffer used as a render target.
240    * @param[in] frameBuffer A valid frame-buffer handle to enable off-screen rendering, or an uninitialized handle to disable.
241    */
242   void SetTargetFrameBuffer( FrameBufferImage frameBuffer );
243
244   /**
245    * @brief Retrieve the frame-buffer used as a render target.
246    * @return A valid frame-buffer handle, or an uninitialised handle if off-screen rendering is disabled.
247    */
248   FrameBufferImage GetTargetFrameBuffer() const;
249
250   /**
251    * @brief Set the function used to convert screen coordinates to frame-buffer coordinates.
252    *
253    * This is useful for hit-testing actors which are rendered off-screen.
254    * @param[in] conversionFunction The conversion function.
255    */
256   void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
257
258   /**
259    * @brief Retrieve the function used to convert screen coordinates to frame-buffer coordinates.
260    * @return The conversion function.
261    */
262   ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
263
264   /**
265    * @brief Set the actor used to convert screen coordinates to frame-buffer coordinates.
266    *
267    * The local coordinates of the actor are mapped as frame-buffer coordinates.
268    * This is useful for hit-testing actors which are rendered off-screen.
269    * Note: The mapping actor needs to be rendered by the default render task to make the mapping work properly.
270    * @param[in] mappingActor The actor used for conversion.
271    */
272   void SetScreenToFrameBufferMappingActor( Actor mappingActor );
273
274   /**
275    * @brief Retrieve the actor used to convert screen coordinates to frame-buffer coordinates.
276    * @return The actor used for conversion.
277    */
278   Actor GetScreenToFrameBufferMappingActor() const;
279
280   /**
281    * @brief Set the GL viewport position used when rendering.
282    *
283    * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
284    * By default this will match the target window or frame-buffer size.
285    * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
286    * @param[in] position The viewports position (x,y)
287    */
288   void SetViewportPosition( Vector2 position );
289
290   /**
291    * @brief Retrieve the GL viewport position used when rendering.
292    * @return The viewport.
293    */
294   Vector2 GetCurrentViewportPosition() const;
295
296   /**
297    * @brief Set the GL viewport size used when rendering.
298    *
299    * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
300    * By default this will match the target window or frame-buffer size.
301    * @param[in] size The viewports size (width,height)
302    */
303   void SetViewportSize( Vector2 size );
304
305   /**
306    * @brief Retrieve the GL viewport size used when rendering.
307    * @return The viewport.
308    */
309   Vector2 GetCurrentViewportSize() const;
310
311   /**
312    * @brief Set the GL viewport used when rendering.
313    *
314    * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
315    * By default this will match the target window or frame-buffer size.
316    * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
317    * @param[in] viewport The new viewport.
318    */
319   void SetViewport( Viewport viewport );
320
321   /**
322    * @brief Retrieve the GL viewport used when rendering.
323    * @return The viewport.
324    */
325   Viewport GetViewport() const;
326
327   /**
328    * @brief Set the clear color used when SetClearEnabled(true) is used.
329    * @param[in] color The new clear color.
330    */
331   void SetClearColor( const Vector4& color );
332
333   /**
334    * @brief Retrieve the clear color used when SetClearEnabled(true) is used.
335    * @note This property can be animated; the return value may not match the value written with SetClearColor().
336    * @return The clear color.
337    */
338   Vector4 GetClearColor() const;
339
340   /**
341    * @brief Set whether the render-task will clear the results of previous render-tasks.
342    *
343    * The default is false.
344    *
345    * @note The default GL surface is cleared automatically at the
346    * beginning of each frame; this setting is only useful when 2+
347    * render-tasks are used, and the result of the first task needs to
348    * be (partially) cleared before rendering the second.
349    *
350    * @param[in] enabled True if the render-task should clear.
351    */
352   void SetClearEnabled( bool enabled );
353
354   /**
355    * @brief Query whether the render-task will clear the results of previous render-tasks.
356    * @return True if the render-task should clear.
357    */
358   bool GetClearEnabled() const;
359
360   /**
361    * @brief Set whether the render task will cull the actors to the camera's view frustum.
362    *
363    * Note that this will only affect image actors that use the default vertex shader.
364    * The default mode is to cull actors.
365    * @param[in] cullMode True if the renderers should be culled.
366    */
367   void SetCullMode( bool cullMode );
368
369   /**
370    * @brief Get the cull mode.
371    *
372    * @return True if the render task should cull the actors to the camera's view frustum
373    */
374   bool GetCullMode() const;
375
376   /**
377    * @brief Set the refresh-rate of the RenderTask.
378    *
379    * The default is REFRESH_ALWAYS (1), meaning that the RenderTask
380    * will be processed every frame if the scene graph is changing.  It
381    * may be desirable to process less frequently. For example,
382    * SetRefreshRate(3) will process once every 3 frames if the scene
383    * graph is changing.  If the scene graph is not changing, then the
384    * render task will not be rendered, regardless of this value.
385    *
386    * The REFRESH_ONCE value means that the RenderTask will be
387    * processed once only, to take a snap-shot of the scene.
388    * Repeatedly calling SetRefreshRate(REFRESH_ONCE) will cause more
389    * snap-shots to be taken.
390    *
391    * @param[in] refreshRate The new refresh rate.
392    */
393   void SetRefreshRate( unsigned int refreshRate );
394
395   /**
396    * @brief Query the refresh-rate of the RenderTask.
397    * @return The refresh-rate.
398    */
399   unsigned int GetRefreshRate() const;
400
401 public: // Signals
402
403   /**
404    * @brief If the refresh rate is REFRESH_ONCE, connect to this signal to be notified when a RenderTask has finished.
405    */
406   RenderTaskSignalType& FinishedSignal();
407
408 public: // Not intended for application developers
409
410   /**
411    * @brief This constructor is used by Dali New() methods.
412    * @param [in] renderTask A pointer to a newly allocated render-task
413    */
414   explicit DALI_INTERNAL RenderTask( Internal::RenderTask* renderTask );
415 };
416
417 } // namespace Dali
418
419 #endif //__DALI_RENDER_TASK_H__