License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / capi / 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 /**
22  * @addtogroup CAPI_DALI_RENDER_TASKS_MODULE
23  * @{
24  */
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/math/viewport.h>
28 #include <dali/public-api/object/constrainable.h>
29 #include <dali/public-api/signals/dali-signal-v2.h>
30
31 namespace Dali DALI_IMPORT_API
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.
57  *
58  * The first RenderTask used for input handling will be the last one rendered, which also has input enabled,
59  * and has a valid source & camera actor; see SetInputEnabled().
60  *
61  * If none of the actors are hit in the last RenderTask rendered, then input handling will continue
62  * with the second last RenderTask rendered, and so on.
63  *
64  * All RenderTasks which target a frame-buffer (i.e. off screen) will be rendered before all RenderTasks
65  * which target the default GL surface. This allows the user to render intermediate targets which are used
66  * later when targetting the screen.
67  *
68  * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the
69  * screen->frame-buffer coordinate conversion is successful; see SetScreenToFrameBufferFunction().
70  *
71  * If the refresh rate id REFRESH_ONCE and a "Finish" signal is connected, it will be emitted when the RenderTask is completed.
72  * Note that all connected signals must be disconnected before the object is destroyed. This is typically done in the
73  * object destructor, and requires either the Dali::Connection object or Dali::RenderTask handle to be stored.
74  */
75 class RenderTask : public Constrainable
76 {
77 public:
78   /**
79    * @brief Typedef for signals sent by this class.
80    */
81   typedef SignalV2< void (RenderTask& source) > RenderTaskSignalV2;
82
83   // Default Properties
84   static const Property::Index VIEWPORT_POSITION;    ///< Property  0, name "viewport-position",   type VECTOR2
85   static const Property::Index VIEWPORT_SIZE;        ///< Property  1, name "viewport-size",       type VECTOR2
86   static const Property::Index CLEAR_COLOR;          ///< Property  2, name "clear-color",         type VECTOR4
87
88   //Signal Names
89   static const char* const SIGNAL_FINISHED; ///< Name for Finished signal
90
91   /**
92    * @brief A pointer to a function for converting screen to frame-buffer coordinates.
93    * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
94    * @return True if the conversion was successful, otherwise coordinates should be unmodified.
95    */
96   typedef bool (* ScreenToFrameBufferFunction)( Vector2& coordinates );
97
98   /**
99    * @brief A pointer to a function for converting screen to frame-buffer coordinates.
100    * @param[in,out] coordinates The screen coordinates to convert where (0,0) is the top-left of the screen.
101    * @return True if the conversion was successful, otherwise coordinates should be unmodified.
102    */
103   typedef bool (* const ConstScreenToFrameBufferFunction)( Vector2& coordinates );
104
105   /**
106    * @brief The default conversion function returns false for any screen coordinates.
107    *
108    * This effectively disables hit-testing for RenderTasks rendering to a frame buffer.
109    * See also FULLSCREEN_FRAMEBUFFER_FUNCTION below.
110    */
111   static ConstScreenToFrameBufferFunction DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION;
112
113   /**
114    * @brief This conversion function outputs the (unmodified) screen coordinates as frame-buffer coordinates.
115    *
116    * Therefore the contents of an off-screen image is expected to be rendered "full screen".
117    */
118   static ConstScreenToFrameBufferFunction FULLSCREEN_FRAMEBUFFER_FUNCTION;
119
120   /**
121    * @brief The refresh-rate of the RenderTask.
122    */
123   enum RefreshRate
124   {
125     REFRESH_ONCE   = 0, ///< Process once only e.g. take a snap-shot of the scene.
126     REFRESH_ALWAYS = 1  ///< Process every frame.
127   };
128
129   static const bool         DEFAULT_EXCLUSIVE;     ///< false
130   static const bool         DEFAULT_INPUT_ENABLED; ///< true
131   static const Vector4      DEFAULT_CLEAR_COLOR;   ///< Color::BLACK
132   static const bool         DEFAULT_CLEAR_ENABLED; ///< false
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 Virtual destructor.
154    */
155   virtual ~RenderTask();
156
157   /**
158    * @copydoc Dali::BaseHandle::operator=
159    */
160   using BaseHandle::operator=;
161
162   /**
163    * @brief Set the actors to be rendered.
164    * @param[in] actor This actor and its children will be rendered.
165    * If actor is an empty handle, then nothing will be rendered.
166    */
167   void SetSourceActor( Actor actor );
168
169   /**
170    * @brief Retrieve the actors to be rendered.
171    * @return This actor and its children will be rendered.
172    */
173   Actor GetSourceActor() const;
174
175   /**
176    * @brief Set whether the RenderTask has exclusive access to the source actors; the default is false.
177    * @param[in] exclusive True if the source actors will only be rendered by this render-task.
178    */
179   void SetExclusive( bool exclusive );
180
181   /**
182    * @brief Query whether the RenderTask has exclusive access to the source actors.
183    * @return True if the source actors will only be rendered by this render-task.
184    */
185   bool IsExclusive() const;
186
187   /**
188    * @brief Set whether the render-task should be considered for input handling; the default is true.
189    *
190    * The task used for input handling will be last task in the RenderTaskList which has input enabled,
191    * and has a valid source & camera actor.
192    * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the screen->frame-buffer
193    * coordinate conversion is successful; see also SetScreenToFrameBufferFunction().
194    * @param[in] enabled True if the render-task should be considered for input handling.
195    */
196   void SetInputEnabled( bool enabled );
197
198   /**
199    * @brief Query whether the render-task should be considered for input handling.
200    * @return True if the render-task should be considered for input handling.
201    */
202   bool GetInputEnabled() const;
203
204   /**
205    * @brief Set the actor from which the scene is viewed.
206    * @param[in] cameraActor The scene is viewed from the perspective of this actor.
207    */
208   void SetCameraActor( CameraActor cameraActor );
209
210   /**
211    * @brief Retrieve the actor from which the scene is viewed.
212    * @return The scene is viewed from the perspective of this actor.
213    */
214   CameraActor GetCameraActor() const;
215
216   /**
217    * @brief Set the frame-buffer used as a render target.
218    * @param[in] frameBuffer A valid frame-buffer handle to enable off-screen rendering, or an uninitialized handle to disable.
219    */
220   void SetTargetFrameBuffer( FrameBufferImage frameBuffer );
221
222   /**
223    * @brief Retrieve the frame-buffer used as a render target.
224    * @return A valid frame-buffer handle, or an uninitialised handle if off-screen rendering is disabled.
225    */
226   FrameBufferImage GetTargetFrameBuffer() const;
227
228   /**
229    * @brief Set the function used to convert screen coordinates to frame-buffer coordinates.
230    *
231    * This is useful for hit-testing actors which are rendered off-screen.
232    * @param[in] conversionFunction The conversion function.
233    */
234   void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
235
236   /**
237    * @brief Retrieve the function used to convert screen coordinates to frame-buffer coordinates.
238    * @return The conversion function.
239    */
240   ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
241
242   /**
243    * @brief Set the actor used to convert screen coordinates to frame-buffer coordinates.
244    *
245    * The local coordinates of the actor are mapped as frame-buffer coordinates.
246    * This is useful for hit-testing actors which are rendered off-screen.
247    * Note: The mapping actor needs to be rendered by the default render task to make the mapping work properly.
248    * @param[in] mappingActor The actor used for conversion.
249    */
250   void SetScreenToFrameBufferMappingActor( Actor mappingActor );
251
252   /**
253    * @brief Retrieve the actor used to convert screen coordinates to frame-buffer coordinates.
254    * @return The actor used for conversion.
255    */
256   Actor GetScreenToFrameBufferMappingActor() const;
257
258   /**
259    * @brief Set the GL viewport position used when rendering.
260    *
261    * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
262    * By default this will match the target window or frame-buffer size.
263    * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
264    * @param[in] position The viewports position (x,y)
265    */
266   void SetViewportPosition( Vector2 position );
267
268   /**
269    * @brief Retrieve the GL viewport position used when rendering.
270    * @return The viewport.
271    */
272   Vector2 GetCurrentViewportPosition() const;
273
274   /**
275    * @brief Set the GL viewport size used when rendering.
276    *
277    * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
278    * By default this will match the target window or frame-buffer size.
279    * @param[in] size The viewports size (width,height)
280    */
281   void SetViewportSize( Vector2 size );
282
283   /**
284    * @brief Retrieve the GL viewport size used when rendering.
285    * @return The viewport.
286    */
287   Vector2 GetCurrentViewportSize() const;
288
289   /**
290    * @brief Set the GL viewport used when rendering.
291    *
292    * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
293    * By default this will match the target window or frame-buffer size.
294    * @note Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
295    * @param[in] viewport The new viewport.
296    */
297   void SetViewport( Viewport viewport );
298
299   /**
300    * @brief Retrieve the GL viewport used when rendering.
301    * @return The viewport.
302    */
303   Viewport GetViewport() const;
304
305   /**
306    * @brief Set the clear color used when SetClearEnabled(true) is used.
307    * @param[in] color The new clear color.
308    */
309   void SetClearColor( const Vector4& color );
310
311   /**
312    * @brief Retrieve the clear color used when SetClearEnabled(true) is used.
313    * @note This property can be animated; the return value may not match the value written with SetClearColor().
314    * @return The clear color.
315    */
316   Vector4 GetClearColor() const;
317
318   /**
319    * @brief Set whether the render-task will clear the results of previous render-tasks.
320    *
321    * The default is false.
322    *
323    * @note The default GL surface is cleared automatically at the
324    * beginning of each frame; this setting is only useful when 2+
325    * render-tasks are used, and the result of the first task needs to
326    * be (partially) cleared before rendering the second.
327    *
328    * @param[in] enabled True if the render-task should clear.
329    */
330   void SetClearEnabled( bool enabled );
331
332   /**
333    * @brief Query whether the render-task will clear the results of previous render-tasks.
334    * @return True if the render-task should clear.
335    */
336   bool GetClearEnabled() const;
337
338   /**
339    * @brief Set the refresh-rate of the RenderTask.
340    *
341    * The default is REFRESH_ALWAYS (1), meaning that the RenderTask will be processed every frame.
342    * It may be desirable to process less frequently e.g. SetRefreshRate(3) will process once every 3 frames.
343    * The REFRESH_ONCE value means that the RenderTask will be processed once only, to take a snap-shot of the scene.
344    * Repeatedly calling SetRefreshRate(REFRESH_ONCE) will cause more snap-shots to be taken.
345    * @param[in] refreshRate The new refresh rate.
346    */
347   void SetRefreshRate( unsigned int refreshRate );
348
349   /**
350    * @brief Query the refresh-rate of the RenderTask.
351    * @return The refresh-rate.
352    */
353   unsigned int GetRefreshRate() const;
354
355 public: // Signals
356
357   /**
358    * @brief If the refresh rate is REFRESH_ONCE, connect to this signal to be notified when a RenderTask has finished.
359    */
360   RenderTaskSignalV2& FinishedSignal();
361
362 public: // Not intended for application developers
363
364   /**
365    * @brief This constructor is used by Dali New() methods.
366    * @param [in] renderTask A pointer to a newly allocated render-task
367    */
368   explicit DALI_INTERNAL RenderTask( Internal::RenderTask* renderTask );
369 };
370
371 } // namespace Dali
372
373 /**
374  * @}
375  */
376 #endif //__DALI_RENDER_TASK_H__