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