Adaptor refactor
[platform/core/uifw/dali-adaptor.git] / dali / integration-api / adaptor.h
1 #ifndef __DALI_INTEGRATION_ADAPTOR_H__
2 #define __DALI_INTEGRATION_ADAPTOR_H__
3
4 /*
5  * Copyright (c) 2017 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 // EXTERNAL INCLUDES
22 #include <dali/public-api/signals/callback.h>
23 #include <dali/public-api/signals/dali-signal.h>
24 #include <dali/public-api/math/rect.h>
25 #include <dali/public-api/events/touch-event.h>
26 #include <dali/public-api/common/view-mode.h>
27
28 // INTERNAL INCLUDES
29 #include <dali/public-api/adaptor-framework/window.h>
30 #include <dali/public-api/adaptor-framework/application-configuration.h>
31
32
33 namespace Dali
34 {
35
36 class RenderSurface;
37
38 namespace Internal
39 {
40 namespace Adaptor
41 {
42 class Adaptor;
43 }
44 }
45
46 /**
47  * @brief An Adaptor object is used to initialize and control how Dali runs.
48  *
49  * It provides a lifecycle interface that allows the application
50  * writer to provide their own main loop and other platform related
51  * features.
52  *
53  * The Adaptor class provides a means for initialising the resources required by the Dali::Core.
54  *
55  * When dealing with platform events, the application writer MUST ensure that Dali is called in a
56  * thread-safe manner.
57  *
58  * As soon as the Adaptor class is created and started, the application writer can initialise their
59  * Dali::Actor objects straight away or as required by the main loop they intend to use (there is no
60  * need to wait for an initialise signal as per the Dali::Application class).
61  *
62  * The Adaptor does emit a Resize signal which informs the user when the surface is resized.
63  * Tizen and Linux Adaptors should follow the example below:
64  *
65  * @code
66  * void CreateProgram(DaliAdaptor& adaptor)
67  * {
68  *   // Create Dali components...
69  *   // Can instantiate adaptor here instead, if required
70  * }
71  *
72  * int main ()
73  * {
74  *   // Initialise platform
75  *   MyPlatform.Init();
76  *
77  *   // Create an 800 by 1280 window positioned at (0,0).
78  *   Dali::PositionSize positionSize(0, 0, 800, 1280);
79  *   Dali::Window window = Dali::Window::New( positionSize, "My Application" );
80  *
81  *   // Create an adaptor which uses that window for rendering
82  *   Dali::Adaptor adaptor = Dali::Adaptor::New( window );
83  *   adaptor.Start();
84  *
85  *   CreateProgram(adaptor);
86  *   // Or use this as a callback function depending on the platform initialisation sequence.
87  *
88  *   // Start Main Loop of your platform
89  *   MyPlatform.StartMainLoop();
90  *
91  *   return 0;
92  * }
93  * @endcode
94  *
95  * If required, you can also connect class member functions to a signal:
96  *
97  * @code
98  * MyApplication application;
99  * adaptor.ResizedSignal().Connect(&application, &MyApplication::Resize);
100  * @endcode
101  *
102  * @see RenderSurface
103  */
104 class DALI_IMPORT_API Adaptor
105 {
106 public:
107
108   typedef Signal< void (Adaptor&) > AdaptorSignalType; ///< Generic Type for adaptor signals
109
110 public:
111   /**
112    * @brief Create a new adaptor using the window.
113    *
114    * @param[in] window The window to draw onto
115    * @return a reference to the adaptor handle
116    */
117   static Adaptor& New( Window window );
118
119   /**
120    * @brief Create a new adaptor using the window.
121    *
122    * @param[in] window The window to draw onto
123    * @param[in] configuration The context loss configuration.
124    * @return a reference to the adaptor handle
125    */
126   static Adaptor& New( Window window, Configuration::ContextLoss configuration );
127
128   /**
129    * @brief Create a new adaptor using render surface.
130    *
131    * @param[in] nativeWindow native window handle
132    * @param[in] surface The surface to draw onto
133    * @return a reference to the adaptor handle
134    */
135   static Adaptor& New( Any nativeWindow, const Dali::RenderSurface& surface );
136
137   /**
138    * @brief Create a new adaptor using render surface.
139    *
140    * @param[in] nativeWindow native window handle
141    * @param[in] surface The surface to draw onto
142    * @param[in] configuration The context loss configuration.
143    * @return a reference to the adaptor handle
144    */
145   static Adaptor& New( Any nativeWindow, const Dali::RenderSurface& surface, Configuration::ContextLoss configuration = Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS);
146
147   /**
148    * @brief Virtual Destructor.
149    */
150   virtual ~Adaptor();
151
152 public:
153
154   /**
155    * @brief Starts the Adaptor.
156    */
157   void Start();
158
159   /**
160    * @brief Pauses the Adaptor.
161    */
162   void Pause();
163
164   /**
165    * @brief Resumes the Adaptor, if previously paused.
166    *
167    * @note If the adaptor is not paused, this does not do anything.
168    */
169   void Resume();
170
171   /**
172    * @brief Stops the Adaptor.
173    */
174   void Stop();
175
176   /**
177    * @brief Ensures that the function passed in is called from the main loop when it is idle.
178    * @note Function must be called from the main event thread only.
179    *
180    * A callback of the following type may be used:
181    * @code
182    *   void MyFunction();
183    * @endcode
184    *
185    * @param[in] callback The function to call.
186    * @return true if added successfully, false otherwise
187    *
188    * @note Ownership of the callback is passed onto this class.
189    */
190   bool AddIdle( CallbackBase* callback );
191
192   /**
193    * @brief Removes a previously added @p callback.
194    * @note Function must be called from the main event thread only.
195    *
196    * Does nothing if the @p callback doesn't exist.
197    *
198    * @param[in] callback The callback to be removed.
199    */
200   void RemoveIdle( CallbackBase* callback );
201
202   /**
203    * @brief Replaces the rendering surface
204    *
205    * @param[in] nativeWindow native window handle
206    * @param[in] surface to use
207    */
208   void ReplaceSurface( Any nativeWindow, Dali::RenderSurface& surface );
209
210   /**
211    * @brief Get the render surface the adaptor is using to render to.
212    *
213    * @return reference to current render surface
214    */
215   RenderSurface& GetSurface();
216
217   /**
218    * @brief Gets native window handle
219    *
220    * @return Native window handle
221    */
222   Any GetNativeWindowHandle();
223
224   /**
225    * @brief Release any locks the surface may hold.
226    *
227    * For example, after compositing an offscreen surface, use this method to allow
228    * rendering to continue.
229    */
230   void ReleaseSurfaceLock();
231
232   /**
233    * @brief Set the number of frames per render.
234    *
235    * This enables an application to deliberately render with a reduced FPS.
236    * @param[in] numberOfVSyncsPerRender The number of vsyncs between successive renders.
237    * Suggest this is a power of two:
238    * 1 - render each vsync frame
239    * 2 - render every other vsync frame
240    * 4 - render every fourth vsync frame
241    * 8 - render every eighth vsync frame
242    */
243   void SetRenderRefreshRate( unsigned int numberOfVSyncsPerRender );
244
245   /**
246    * @brief Set whether the frame count per render is managed using the hardware VSync or
247    * manually timed.
248    *
249    * @param[in] useHardware True if the hardware VSync should be used
250    */
251   void SetUseHardwareVSync(bool useHardware);
252
253   /**
254    * @brief Returns a reference to the instance of the adaptor used by the current thread.
255    *
256    * @return A reference to the adaptor.
257    * @pre The adaptor has been initialised.
258    * @note This is only valid in the main thread.
259    */
260   static Adaptor& Get();
261
262   /**
263    * @brief Checks whether the adaptor is available.
264    *
265    * @return true, if it is available, false otherwise.
266    */
267   static bool IsAvailable();
268
269   /**
270    * @brief Call this method to notify Dali when scene is created and initialized.
271    *
272    * Notify Adaptor that the scene has been created.
273    */
274   void NotifySceneCreated();
275
276   /**
277    * @brief Call this method to notify Dali when the system language changes.
278    *
279    * Use this only when NOT using Dali::Application, As Application created using
280    * Dali::Application will automatically receive notification of language change.
281    * When Dali::Application is not used, the application developer should
282    * use app-core to receive language change notifications and should update Dali
283    * by calling this method.
284    */
285   void NotifyLanguageChanged();
286
287   /**
288    * @brief Sets minimum distance in pixels that the fingers must move towards/away from each other in order to
289    * trigger a pinch gesture
290    *
291    * @param[in] distance The minimum pinch distance in pixels
292    */
293   void SetMinimumPinchDistance(float distance);
294
295   /**
296    * @brief Feed a touch point to the adaptor.
297    *
298    * @param[in] point touch point
299    * @param[in] timeStamp time value of event
300    */
301   void FeedTouchPoint( TouchPoint& point, int timeStamp );
302
303   /**
304    * @brief Feed a wheel event to the adaptor.
305    *
306    * @param[in]  wheelEvent wheel event
307    */
308   void FeedWheelEvent( WheelEvent& wheelEvent );
309
310   /**
311    * @brief Feed a key event to the adaptor.
312    *
313    * @param[in] keyEvent The key event holding the key information.
314    */
315   void FeedKeyEvent( KeyEvent& keyEvent );
316
317   /**
318    * @copydoc Dali::Core::SceneCreated();
319    */
320   void SceneCreated();
321
322   /**
323    * @copydoc Dali::Application::SetViewMode();
324    */
325   void SetViewMode( ViewMode viewMode );
326
327   /**
328    * @copydoc Dali::Application::SetStereoBase();
329    */
330   void SetStereoBase( float stereoBase );
331
332   /**
333    * @brief Renders once more even if we're paused
334    * @note Will not work if the window is hidden.
335    */
336   void RenderOnce();
337
338 public:  // Signals
339
340   /**
341    * @brief The user should connect to this signal if they need to perform any
342    * special activities when the surface Dali is being rendered on is resized.
343    *
344    * @return The signal to connect to
345    */
346   AdaptorSignalType& ResizedSignal();
347
348   /**
349    * @brief This signal is emitted when the language is changed on the device.
350    *
351    * @return The signal to connect to
352    */
353   AdaptorSignalType& LanguageChangedSignal();
354
355 private:
356
357   // Undefined
358   Adaptor(const Adaptor&);
359   Adaptor& operator=(Adaptor&);
360
361 private:
362
363   /**
364    * @brief Create an uninitialized Adaptor.
365    */
366   Adaptor();
367
368   Internal::Adaptor::Adaptor* mImpl; ///< Implementation object
369   friend class Internal::Adaptor::Adaptor;
370 };
371
372 } // namespace Dali
373
374 #endif // __DALI_INTEGRATION_ADAPTOR_H__