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