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