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