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