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