0d80b351df010600738355b3f9c61dc0abd5d63f
[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) 2023 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/devel-api/events/touch-point.h>
23 #include <dali/integration-api/processor-interface.h>
24 #include <dali/public-api/math/rect.h>
25 #include <dali/public-api/math/uint-16-pair.h>
26 #include <dali/public-api/object/any.h>
27 #include <dali/public-api/signals/callback.h>
28 #include <dali/public-api/signals/dali-signal.h>
29
30 // INTERNAL INCLUDES
31 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
32 #include <dali/integration-api/adaptor-framework/trace-factory-interface.h>
33 #include <dali/public-api/adaptor-framework/window.h>
34 #include <dali/public-api/dali-adaptor-common.h>
35
36 namespace Dali
37 {
38 class ObjectRegistry;
39 class RenderSurfaceInterface;
40
41 using WindowContainer = std::vector<Window>;
42
43 namespace Integration
44 {
45 class SceneHolder;
46 }
47
48 using SceneHolderList = std::vector<Dali::Integration::SceneHolder>;
49
50 namespace Internal
51 {
52 namespace Adaptor
53 {
54 class GraphicsFactory;
55 class Adaptor;
56 } // namespace Adaptor
57 } // namespace Internal
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_ADAPTOR_API Adaptor
118 {
119 public:
120   typedef Signal<void(Adaptor&)>                        AdaptorSignalType;       ///< Generic Type for adaptor signals
121   typedef Signal<void(Dali::Integration::SceneHolder&)> WindowCreatedSignalType; ///< SceneHolder created signal type
122
123   using SurfaceSize = Uint16Pair; ///< Surface size type
124
125 public:
126   /**
127    * @brief Create a new adaptor using the window.
128    *
129    * @param[in] window The window to draw onto
130    * @return a reference to the adaptor handle
131    */
132   static Adaptor& New(Window window);
133
134   /**
135    * @brief Create a new adaptor using render surface.
136    *
137    * @param[in] window The window to draw onto
138    * @param[in] surface The surface to draw onto
139    * @return a reference to the adaptor handle
140    */
141   static Adaptor& New(Window window, const Dali::RenderSurfaceInterface& surface);
142
143   /**
144    * @brief Create a new adaptor using the SceneHolder.
145    *
146    * @param[in] sceneHolder The SceneHolder to draw onto
147    * @return a reference to the adaptor handle
148    */
149   static Adaptor& New(Dali::Integration::SceneHolder sceneHolder);
150
151   /**
152    * @brief Create a new adaptor using render surface.
153    *
154    * @param[in] sceneHolder The SceneHolder to draw onto
155    * @param[in] surface The surface to draw onto
156    * @return a reference to the adaptor handle
157    */
158   static Adaptor& New(Dali::Integration::SceneHolder sceneHolder, const Dali::RenderSurfaceInterface& surface);
159
160   /**
161    * @brief Virtual Destructor.
162    */
163   virtual ~Adaptor();
164
165 public:
166   /**
167    * @brief Starts the Adaptor.
168    */
169   void Start();
170
171   /**
172    * @brief Pauses the Adaptor.
173    */
174   void Pause();
175
176   /**
177    * @brief Resumes the Adaptor, if previously paused.
178    *
179    * @note If the adaptor is not paused, this does not do anything.
180    */
181   void Resume();
182
183   /**
184    * @brief Stops the Adaptor.
185    */
186   void Stop();
187
188   /**
189    * @brief Ensures that the function passed in is called from the main loop when it is idle.
190    * @note Function must be called from the main event thread only.
191    *
192    * Callbacks of the following types may be used:
193    * @code
194    *   void MyFunction();
195    * @endcode
196    * This callback will be deleted once it is called.
197    *
198    * @code
199    *   bool MyFunction();
200    * @endcode
201    * This callback will be called repeatedly as long as it returns true. A return of 0 deletes this callback.
202    *
203    * @param[in] callback The function to call.
204    * @param[in] hasReturnValue Sould be set to true if the callback function has a return value.
205    * @return true if added successfully, false otherwise
206    *
207    * @note Ownership of the callback is passed onto this class.
208    */
209   bool AddIdle(CallbackBase* callback, bool hasReturnValue);
210
211   /**
212    * @brief Adds a new Window instance to the Adaptor
213    *
214    * @param[in]  childWindow The child window instance
215    */
216   bool AddWindow(Dali::Integration::SceneHolder childWindow);
217
218   /**
219    * @brief Removes a previously added @p callback.
220    * @note Function must be called from the main event thread only.
221    *
222    * Does nothing if the @p callback doesn't exist.
223    *
224    * @param[in] callback The callback to be removed.
225    */
226   void RemoveIdle(CallbackBase* callback);
227
228   /**
229    * @brief Processes the idle callbacks.
230    *
231    * @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.
232    */
233   void ProcessIdle();
234
235   /**
236    * @brief Replaces the rendering surface
237    *
238    * @param[in] window The window to replace the surface for
239    * @param[in] surface to use
240    */
241   void ReplaceSurface(Window window, Dali::RenderSurfaceInterface& surface);
242
243   /**
244    * @brief Replaces the rendering surface
245    *
246    * @param[in] sceneHolder The SceneHolder to replace the surface for
247    * @param[in] surface to use
248    */
249   void ReplaceSurface(Dali::Integration::SceneHolder sceneHolder, Dali::RenderSurfaceInterface& surface);
250
251   /**
252    * @brief Get the render surface the adaptor is using to render to.
253    *
254    * @return reference to current render surface
255    */
256   Dali::RenderSurfaceInterface& GetSurface();
257
258   /**
259    * @brief Gets native window handle
260    *
261    * @return Native window handle
262    */
263   Any GetNativeWindowHandle();
264
265   /**
266    * @brief Retrieve native window handle that the given actor is added to.
267    *
268    * @param[in] actor The actor
269    * @return native window handle
270    */
271   Any GetNativeWindowHandle(Actor actor);
272
273   /**
274    * @brief Get the native display associated with the graphics backend
275    *
276    * @return A handle to the native display
277    */
278   Any GetGraphicsDisplay();
279
280   /**
281    * @brief Release any locks the surface may hold.
282    *
283    * For example, after compositing an offscreen surface, use this method to allow
284    * rendering to continue.
285    */
286   void ReleaseSurfaceLock();
287
288   /**
289    * @brief Set the number of frames per render.
290    *
291    * This enables an application to deliberately render with a reduced FPS.
292    * @param[in] numberOfVSyncsPerRender The number of vsyncs between successive renders.
293    * Suggest this is a power of two:
294    * 1 - render each vsync frame
295    * 2 - render every other vsync frame
296    * 4 - render every fourth vsync frame
297    * 8 - render every eighth vsync frame
298    */
299   void SetRenderRefreshRate(unsigned int numberOfVSyncsPerRender);
300
301   /**
302    * @brief The callback is called from the Update/Render thread prior to rendering.
303    *
304    * @param[in] callback The function to call
305    *
306    * @note The function is called from the Update thread, so should do as little processing as possible.
307    * It is not possible to call any DALi event side APIs from within the callback; doing so will cause
308    * instability. Only 1 callback is supported. Setting the callback to NULL will remove the current callback.
309    *
310    * A callback of the following type should be used:
311    * @code
312    *   bool MyFunction();
313    * @endcode
314    * This callback will be called repeatedly as long as it returns true. A return of 0 deletes this callback.
315    */
316   void SetPreRenderCallback(CallbackBase* callback);
317
318   /**
319    * @brief Returns a reference to the instance of the adaptor used by the current thread.
320    *
321    * @return A reference to the adaptor.
322    * @pre The adaptor has been initialised.
323    * @note This is only valid in the main thread.
324    */
325   static Adaptor& Get();
326
327   /**
328    * @brief Checks whether the adaptor is available.
329    *
330    * @return true, if it is available, false otherwise.
331    */
332   static bool IsAvailable();
333
334   /**
335    * @brief Call this method to notify Dali when scene is created and initialized.
336    *
337    * Notify Adaptor that the scene has been created.
338    */
339   void NotifySceneCreated();
340
341   /**
342    * @brief Call this method to notify Dali when the system language changes.
343    *
344    * Use this only when NOT using Dali::Application, As Application created using
345    * Dali::Application will automatically receive notification of language change.
346    * When Dali::Application is not used, the application developer should
347    * use app-core to receive language change notifications and should update Dali
348    * by calling this method.
349    */
350   void NotifyLanguageChanged();
351
352   /**
353    * @brief Feed a touch point to the adaptor.
354    *
355    * @param[in] point touch point
356    * @param[in] timeStamp time value of event
357    */
358   void FeedTouchPoint(TouchPoint& point, int timeStamp);
359
360   /**
361    * @brief Feed a wheel event to the adaptor.
362    *
363    * @param[in]  wheelEvent wheel event
364    */
365   void FeedWheelEvent(WheelEvent& wheelEvent);
366
367   /**
368    * @brief Feed a key event to the adaptor.
369    *
370    * @param[in] keyEvent The key event holding the key information.
371    */
372   void FeedKeyEvent(KeyEvent& keyEvent);
373
374   /**
375    * @copydoc Dali::Core::SceneCreated();
376    */
377   void SceneCreated();
378
379   /**
380    * @brief Informs core the surface size has changed.
381    *
382    * @param[in] surface The current render surface
383    * @param[in] surfaceSize The new surface size
384    */
385   void SurfaceResizePrepare(Dali::RenderSurfaceInterface* surface, SurfaceSize surfaceSize);
386
387   /**
388    * @brief Informs ThreadController the surface size has changed.
389    *
390    * @param[in] surface The current render surface
391    * @param[in] surfaceSize The new surface size
392    */
393   void SurfaceResizeComplete(Dali::RenderSurfaceInterface* surface, SurfaceSize surfaceSize);
394
395   /**
396    * @brief Renders once more even if we're paused
397    * @note Will not work if the window is hidden.
398    */
399   void RenderOnce();
400
401   /**
402    * @brief Relayout and ensure all pending operations are flushed to the update thread.
403    */
404   void FlushUpdateMessages();
405
406   /**
407    * @brief Requests a future call to Dali::Integration::Core::ProcessEvents(), when the application is idle.
408    */
409   void RequestProcessEventsOnIdle();
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 The trace factory allows installation of a trace function in worker threads.
419    * @return An interface to a tracing factory
420    */
421   const TraceFactoryInterface& GetTraceFactory();
422
423   /**
424    * @brief Register a processor implementing the Integration::Processor interface with dali-core.
425    * @param[in] processor the Processor to register
426    * @param[in] postProcessor set this processor required to be called after size negotiation. Default is false.
427    * @note using this api does not maintain the processor's lifecycle, must be done elsewhere.
428    */
429   void RegisterProcessor(Integration::Processor& processor, bool postProcessor = false);
430
431   /**
432    * @brief Unregister a previously registered processor from dali-core.
433    * @param[in] processor the Processor to unregister
434    * @param[in] postProcessor True if the processor to be unregister is for post processor.
435    */
436   void UnregisterProcessor(Integration::Processor& processor, bool postProcessor = false);
437
438   /**
439    * @brief Get the list of windows created.
440    * @return The list of windows
441    */
442   Dali::WindowContainer GetWindows() const;
443
444   /**
445    * @brief Get the list of scene holders.
446    * @return The list of scene holers
447    */
448   SceneHolderList GetSceneHolders() const;
449
450   /**
451    * @brief Gets the Object registry.
452    * @return The object registry
453    */
454   Dali::ObjectRegistry GetObjectRegistry() const;
455
456   /**
457    * @brief Called when the window becomes fully or partially visible.
458    */
459   void OnWindowShown();
460
461   /**
462    * @brief Called when the window is fully hidden.
463    */
464   void OnWindowHidden();
465
466 public: // Signals
467   /**
468    * @brief The user should connect to this signal if they need to perform any
469    * special activities when the surface Dali is being rendered on is resized.
470    *
471    * @return The signal to connect to
472    */
473   AdaptorSignalType& ResizedSignal();
474
475   /**
476    * @brief This signal is emitted when the language is changed on the device.
477    *
478    * @return The signal to connect to
479    */
480   AdaptorSignalType& LanguageChangedSignal();
481
482   /**
483    * @brief This signal is emitted when a new window (scene holder) is created
484    *
485    * @return The signal to connect to
486    */
487   WindowCreatedSignalType& WindowCreatedSignal();
488
489 private:
490   // Undefined
491   Adaptor(const Adaptor&);
492   Adaptor& operator=(Adaptor&);
493
494 private:
495   /**
496    * @brief Create an uninitialized Adaptor.
497    */
498   Adaptor();
499
500   Internal::Adaptor::Adaptor* mImpl; ///< Implementation object
501   friend class Internal::Adaptor::Adaptor;
502 };
503
504 } // namespace Dali
505
506 #endif // DALI_INTEGRATION_ADAPTOR_H