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