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