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