Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / adaptor-impl.h
1 #ifndef DALI_INTERNAL_ADAPTOR_IMPL_H
2 #define DALI_INTERNAL_ADAPTOR_IMPL_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/common/vector-wrapper.h>
23 #include <dali/public-api/common/view-mode.h>
24 #include <dali/public-api/math/rect.h>
25 #include <dali/public-api/signals/callback.h>
26 #include <dali/public-api/math/uint-16-pair.h>
27 #include <dali/integration-api/render-controller.h>
28
29 // INTERNAL INCLUDES
30 #include <dali/integration-api/adaptor.h>
31 #include <dali/integration-api/scene.h>
32 #include <dali/public-api/adaptor-framework/tts-player.h>
33 #include <dali/devel-api/adaptor-framework/clipboard.h>
34
35 #include <dali/internal/graphics/common/graphics-interface.h>
36
37 #include <dali/internal/legacy/common/tizen-platform-abstraction.h>
38 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
39 #include <dali/internal/system/common/environment-options.h>
40 #include <dali/internal/system/common/core-event-interface.h>
41 #include <dali/internal/input/common/drag-and-drop-detector-impl.h>
42 #include <dali/internal/window-system/common/damage-observer.h>
43 #include <dali/internal/window-system/common/window-impl.h>
44 #include <dali/internal/window-system/common/window-visibility-observer.h>
45 #include <dali/internal/system/common/kernel-trace.h>
46 #include <dali/internal/system/common/system-trace.h>
47 #include <dali/integration-api/trigger-event-factory.h>
48 #include <dali/internal/network/common/socket-factory.h>
49
50
51 namespace Dali
52 {
53
54 class RenderSurfaceInterface;
55 class Window;
56
57 namespace Integration
58 {
59 class Core;
60 class GlAbstraction;
61 class Processor;
62 }
63
64 namespace Internal
65 {
66
67 namespace Adaptor
68 {
69 class DisplayConnection;
70 class GraphicsFactory;
71 class GestureManager;
72 class GlImplementation;
73 class GlSyncImplementation;
74 class ThreadController;
75 class TriggerEvent;
76 class CallbackManager;
77 class FeedbackPluginProxy;
78 class FeedbackController;
79 class RotationObserver;
80 class VSyncMonitor;
81 class PerformanceInterface;
82 class LifeCycleObserver;
83 class ObjectProfiler;
84
85 /**
86  * Implementation of the Adaptor class.
87  */
88 class Adaptor : public Integration::RenderController,
89                 public AdaptorInternalServices,
90                 public CoreEventInterface,
91                 public DamageObserver,
92                 public WindowVisibilityObserver
93 {
94 public:
95
96   typedef Dali::Adaptor::AdaptorSignalType AdaptorSignalType;
97
98   typedef Uint16Pair SurfaceSize;          ///< Surface size type
99
100   /**
101    * Creates a New Adaptor
102    * @param[in]  window              The window handle
103    * @param[in]  surface             A render surface can be one of the following
104    *                                  - Pixmap, adaptor will use existing Pixmap to draw on to
105    *                                  - Window, adaptor will use existing Window to draw on to
106    * @param[in]  configuration       The context loss configuration ( to choose resource discard policy )
107    * @param[in]  environmentOptions  A pointer to the environment options. If NULL then one is created.
108    */
109   static Dali::Adaptor* New( Dali::Window window,
110                              Dali::RenderSurfaceInterface* surface,
111                              Dali::Configuration::ContextLoss configuration,
112                              EnvironmentOptions* environmentOptions );
113
114   /**
115    * Creates a New Adaptor
116    * @param[in]  window              The window handle
117    * @param[in]  configuration       The context loss configuration ( to choose resource discard policy )
118    * @param[in]  environmentOptions  A pointer to the environment options. If NULL then one is created.
119    */
120   static Dali::Adaptor* New( Dali::Window window,
121                              Dali::Configuration::ContextLoss configuration,
122                              EnvironmentOptions* environmentOptions );
123
124   /**
125    * Creates a New Adaptor
126    * @param[in]  graphicsFactory     A factory that creates the graphics interface
127    * @param[in]  window              The window handle
128    * @param[in]  surface             A render surface can be one of the following
129    *                                  - Pixmap, adaptor will use existing Pixmap to draw on to
130    *                                  - Window, adaptor will use existing Window to draw on to
131    * @param[in]  configuration       The context loss configuration ( to choose resource discard policy )
132    * @param[in]  environmentOptions  A pointer to the environment options. If NULL then one is created.
133    */
134   static Dali::Adaptor* New( GraphicsFactory& graphicsFactory,
135                              Dali::Window window,
136                              Dali::RenderSurfaceInterface* surface,
137                              Dali::Configuration::ContextLoss configuration,
138                              EnvironmentOptions* environmentOptions );
139
140   /**
141    * Creates a New Adaptor
142    * @param[in]  graphicsFactory     A factory that creates the graphics interface
143    * @param[in]  window              The window handle
144    * @param[in]  configuration       The context loss configuration ( to choose resource discard policy )
145    * @param[in]  environmentOptions  A pointer to the environment options. If NULL then one is created.
146    */
147   static Dali::Adaptor* New( GraphicsFactory& graphicsFactory,
148                              Dali::Window window,
149                              Dali::Configuration::ContextLoss configuration,
150                              EnvironmentOptions* environmentOptions );
151
152   /**
153    * 2-step initialisation, this should be called after creating an adaptor instance.
154    * @param[in]  graphicsFactory     A factory that creates the graphics interface
155    * @param[in]  configuration       The context loss configuration ( to choose resource discard policy )
156    */
157   void Initialize( GraphicsFactory& graphicsFactory, Dali::Configuration::ContextLoss configuration );
158
159   /**
160    * Virtual destructor.
161    */
162   virtual ~Adaptor();
163
164   /**
165    * @copydoc Dali::Adaptor::Get()
166    */
167   static Dali::Adaptor& Get();
168
169   /**
170    * @copydoc Dali::Adaptor::IsAvailable()
171    */
172   static bool IsAvailable();
173
174   /**
175    * @copydoc Dali::Core::SceneCreated();
176    */
177   void SceneCreated();
178
179 public: // AdaptorInternalServices implementation
180   /**
181    * @copydoc Dali::Adaptor::Start()
182    */
183   virtual void Start();
184
185   /**
186    * @copydoc Dali::Adaptor::Pause()
187    */
188   virtual void Pause();
189
190   /**
191    * @copydoc Dali::Adaptor::Resume()
192    */
193   virtual void Resume();
194
195   /**
196    * @copydoc Dali::Adaptor::Stop()
197    */
198   virtual void Stop();
199
200   /**
201    * @copydoc Dali::Adaptor::ContextLost()
202    */
203   virtual void ContextLost();
204
205   /**
206    * @copydoc Dali::Adaptor::ContextRegained()
207    */
208   virtual void ContextRegained();
209
210   /**
211    * @copydoc Dali::EventFeeder::FeedTouchPoint()
212    */
213   virtual void FeedTouchPoint( TouchPoint& point, int timeStamp );
214
215   /**
216    * @copydoc Dali::EventFeeder::FeedWheelEvent()
217    */
218   virtual void FeedWheelEvent( WheelEvent& wheelEvent );
219
220   /**
221    * @copydoc Dali::EventFeeder::FeedKeyEvent()
222    */
223   virtual void FeedKeyEvent( KeyEvent& keyEvent );
224
225   /**
226    * @copydoc AdaptorInterface::ReplaceSurface()
227    */
228   virtual void ReplaceSurface( Dali::Window window, Dali::RenderSurfaceInterface& surface );
229
230   /**
231    * @copydoc Dali::Adaptor::GetSurface()
232    */
233   virtual Dali::RenderSurfaceInterface& GetSurface() const;
234
235   /**
236    * @copydoc Dali::Adaptor::ReleaseSurfaceLock()
237    */
238   virtual void ReleaseSurfaceLock();
239
240   /**
241    * Retrieve the TtsPlayer.
242    * @param[in] mode The mode of TtsPlayer
243    * @return A handle to the TtsPlayer.
244    */
245   virtual Dali::TtsPlayer GetTtsPlayer(Dali::TtsPlayer::Mode mode);
246
247   /**
248    * @copydoc Dali::Adaptor::AddIdle()
249    */
250   virtual bool AddIdle( CallbackBase* callback, bool hasReturnValue, bool forceAdd );
251
252   /**
253    * Adds a new Window instance to the Adaptor
254    * @param[in]  childWindow The child window instance
255    * @param[in]  childWindowName The child window title/name
256    * @param[in]  childWindowClassName The class name that the child window belongs to
257    * @param[in]  childWindowMode The mode of the child window
258    */
259   virtual bool AddWindow( Dali::Window* childWindow,
260                           const std::string& childWindowName,
261                           const std::string& childWindowClassName,
262                           const bool& childWindowMode );
263
264   /**
265    * Removes an existing Window instance from the Adaptor
266    * @param[in]  window The Window instance
267    */
268   virtual bool RemoveWindow( Dali::Window* childWindow );
269
270   /**
271    * Removes an existing Window instance from the Adaptor
272    * @param[in]  windowName The Window name
273    * @note If two Windows have the same name, the first one that matches will be removed
274    */
275   virtual bool RemoveWindow( std::string childWindowName );
276
277   /**
278    * @copydoc Dali::Adaptor::RemoveIdle()
279    */
280   virtual void RemoveIdle( CallbackBase* callback );
281
282   /**
283    * Sets a pre-render callback.
284    */
285   void SetPreRenderCallback( CallbackBase* callback );
286
287   /**
288    * Removes an existing Window instance from the Adaptor
289    * @param[in]  childWindow The Window instance
290    */
291   bool RemoveWindow( Dali::Internal::Adaptor::Window* childWindow );
292
293 public:
294
295   /**
296    * @return the Core instance
297    */
298   virtual Dali::Integration::Core& GetCore();
299
300   /**
301    * @copydoc Dali::Adaptor::SetRenderRefreshRate()
302    */
303   void SetRenderRefreshRate( unsigned int numberOfVSyncsPerRender );
304
305   /**
306    * @copydoc Dali::Adaptor::SetUseHardwareVSync()
307    */
308   void SetUseHardwareVSync(bool useHardware);
309
310   /**
311    * Return the PlatformAbstraction.
312    * @return The PlatformAbstraction.
313    */
314   Integration::PlatformAbstraction& GetPlatformAbstraction() const;
315
316   /**
317    * Sets the Drag & Drop Listener.
318    * @param[in] detector The detector to send Drag & Drop events to.
319    */
320   void SetDragAndDropDetector( DragAndDropDetectorPtr detector );
321
322   /**
323    * Sets a rotation observer, or set to NULL to remove.
324    * @pre Adaptor::Start() has been called ( to create EventHandler )
325    * @param[in] observer The observer to listen for window rotation events
326    */
327   void SetRotationObserver( RotationObserver* observer );
328
329   /**
330    * Destroy the TtsPlayer of specific mode.
331    * @param[in] mode The mode of TtsPlayer to destroy
332    */
333   void DestroyTtsPlayer(Dali::TtsPlayer::Mode mode);
334
335   /**
336    * @brief Sets minimum distance in pixels that the fingers must move towards/away from each other in order to
337    * trigger a pinch gesture
338    *
339    * @param[in] distance The minimum pinch distance in pixels
340    */
341   void SetMinimumPinchDistance(float distance);
342
343   /**
344    * Gets native window handle
345    *
346    * @return native window handle
347    */
348   Any GetNativeWindowHandle();
349
350   /**
351    * Get the native display associated with the graphics backend
352    *
353    * @return A handle to the native display
354    */
355   Any GetGraphicsDisplay();
356
357   /**
358    * Sets use remote surface for Surface output
359    * @param[in] useRemoteSurface True if the remote surface is used
360    */
361   void SetUseRemoteSurface(bool useRemoteSurface);
362
363 public:
364
365   /**
366    * Adds an adaptor observer so that we can observe the adaptor's lifetime events.
367    * @param[in]  observer  The observer.
368    * @note Observers should remove themselves when they are destroyed.
369    */
370   void AddObserver( LifeCycleObserver& observer );
371
372   /**
373    * Removes the observer from the adaptor.
374    * @param[in]  observer  The observer to remove.
375    * @note Observers should remove themselves when they are destroyed.
376    */
377   void RemoveObserver( LifeCycleObserver& observer );
378
379   /**
380    * Emits the Notification event to the Dali core.
381    */
382   void SendNotificationEvent();
383
384   /**
385    * Request adaptor to update once
386    */
387   void RequestUpdateOnce();
388
389   /**
390    * @copydoc Dali::Adaptor::NotifySceneCreated()
391    */
392   void NotifySceneCreated();
393
394   /**
395    * @copydoc Dali::Adaptor::NotifyLanguageChanged()
396    */
397   void NotifyLanguageChanged();
398
399   /**
400    * Gets AppId of current application
401    */
402   void GetAppId( std::string& appId );
403
404   /**
405    * Informs core the surface size has changed
406    */
407   void SurfaceResizePrepare( Dali::RenderSurfaceInterface* surface, SurfaceSize surfaceSize );
408
409   /**
410    * Informs ThreadController the surface size has changed
411    */
412   void SurfaceResizeComplete( Dali::RenderSurfaceInterface* surface, SurfaceSize surfaceSize );
413
414   /**
415    * Sets layout direction of root by system language
416    * @param[in] locale System locale
417    */
418   void SetRootLayoutDirection( std::string locale );
419
420   /**
421    * @copydoc Dali::Adaptor::RenderOnce
422    */
423   void RenderOnce();
424
425   /**
426    * @copydoc Dali::Adaptor::GetLogFactory
427    */
428   const LogFactoryInterface& GetLogFactory();
429
430   /**
431    * @copydoc Dali::Adaptor::RegisterProcessor
432    */
433   void RegisterProcessor( Integration::Processor& processor );
434
435   /**
436    * @coydoc Dali::Adaptor::UnregisterProcessor
437    */
438   void UnregisterProcessor( Integration::Processor& processor );
439
440 public:  //AdaptorInternalServices
441
442   /**
443    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetPlatformAbstractionInterface()
444    */
445   virtual Dali::Integration::PlatformAbstraction& GetPlatformAbstractionInterface();
446
447   /**
448    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetDisplayConnectionInterface()
449    */
450   virtual Dali::DisplayConnection& GetDisplayConnectionInterface();
451
452   /**
453    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetGraphicsInterface()
454    */
455   virtual GraphicsInterface& GetGraphicsInterface();
456
457   /**
458    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetTriggerEventInterface()
459    */
460   virtual TriggerEventInterface& GetProcessCoreEventsTrigger();
461
462   /**
463    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetTriggerEventFactoryInterface()
464    */
465   virtual TriggerEventFactoryInterface& GetTriggerEventFactoryInterface();
466
467   /**
468    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetSocketFactoryInterface()
469    */
470   virtual SocketFactoryInterface& GetSocketFactoryInterface();
471
472   /**
473    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetRenderSurfaceInterface()
474    */
475   virtual Dali::RenderSurfaceInterface* GetRenderSurfaceInterface();
476
477   /**
478    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetVSyncMonitorInterface()
479    */
480   virtual VSyncMonitorInterface* GetVSyncMonitorInterface();
481
482   /**
483    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetPerformanceInterface()
484    */
485   virtual PerformanceInterface* GetPerformanceInterface();
486
487   /**
488    * copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetKernelTraceInterface()
489    */
490   virtual TraceInterface& GetKernelTraceInterface();
491
492   /**
493    * copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetSystemTraceInterface()
494    */
495   virtual TraceInterface& GetSystemTraceInterface();
496
497 public: // Signals
498
499   /**
500    * @copydoc Dali::Adaptor::SignalResized
501    */
502   AdaptorSignalType& ResizedSignal()
503   {
504     return mResizedSignal;
505   }
506
507   /**
508    * @copydoc Dali::Adaptor::LanguageChangedSignal
509    */
510   AdaptorSignalType& LanguageChangedSignal()
511   {
512     return mLanguageChangedSignal;
513   }
514
515   /**
516    * Gets the gesture manager.
517    * @return The GestureManager
518    */
519   GestureManager* GetGestureManager() const
520   {
521     return mGestureManager;
522   }
523
524 private: // From Dali::Internal::Adaptor::CoreEventInterface
525
526   /**
527    * @copydoc Dali::Internal::Adaptor::CoreEventInterface::QueueCoreEvent()
528    */
529   virtual void QueueCoreEvent(const Dali::Integration::Event& event);
530
531   /**
532    * @copydoc Dali::Internal::Adaptor:CoreEventInterface:::ProcessCoreEvents()
533    */
534   virtual void ProcessCoreEvents();
535
536 private: // From Dali::Integration::RenderController
537
538   /**
539    * @copydoc Dali::Integration::RenderController::RequestUpdate()
540    */
541   virtual void RequestUpdate( bool forceUpdate );
542
543   /**
544    * @copydoc Dali::Integration::RenderController::RequestProcessEventsOnIdle()
545    */
546   virtual void RequestProcessEventsOnIdle( bool forceProcess );
547
548 private: // From Dali::Internal::Adaptor::WindowVisibilityObserver
549
550   /**
551    * Called when the window becomes fully or partially visible.
552    */
553   virtual void OnWindowShown();
554
555   /**
556    * Called when the window is fully hidden.
557    */
558   virtual void OnWindowHidden();
559
560 private: // From Dali::Internal::Adaptor::DamageObserver
561
562   /**
563    * @copydoc Dali::Internal::Adaptor::DamageObserver::OnDamaged()
564    */
565   void OnDamaged( const DamageArea& area );
566
567 private:
568
569   // Undefined
570   Adaptor(const Adaptor&) = delete;
571   Adaptor& operator=(Adaptor&) = delete;
572
573 private:
574
575   /**
576    * Assigns the render surface to the adaptor
577    *
578    */
579   void SetSurface(Dali::RenderSurfaceInterface *surface);
580
581   /**
582    * called after surface is created
583    */
584   void SurfaceInitialized();
585
586   /**
587    * Sends an notification message from main loop idle handler
588    */
589   bool ProcessCoreEventsFromIdle();
590
591   /**
592    * Gets path for data/resource storage.
593    * @param[out] path Path for data/resource storage
594    */
595   void GetDataStoragePath(std::string& path);
596
597   /**
598    * Sets up system information if needs
599    */
600   void SetupSystemInformation();
601
602   /**
603    * Adds a callback to be run when entering an idle state.
604    *
605    * A callback of the following type should be used:
606    * @code
607    *   bool MyFunction();
608    * @endcode
609    * This callback will be called repeatedly as long as it returns true. A return of 0 deletes this callback.
610    */
611   bool AddIdleEnterer( CallbackBase* callback, bool forceAdd );
612
613   /**
614    * Removes a previously added the idle enterer callback.
615    */
616   void RemoveIdleEnterer( CallbackBase* callback );
617
618 private:
619
620   /**
621    * Constructor
622    * @param[in]  window       window handle
623    * @param[in]  adaptor      The public adaptor
624    * @param[in]  surface      A render surface can be one of the following
625    *                          - Pixmap, adaptor will use existing Pixmap to draw on to
626    *                          - Window, adaptor will use existing Window to draw on to
627    * @param[in]  environmentOptions  A pointer to the environment options. If NULL then one is created.
628    */
629   Adaptor( Dali::Window window, Dali::Adaptor& adaptor, Dali::RenderSurfaceInterface* surface, EnvironmentOptions* environmentOptions );
630
631 private: // Types
632
633   enum State
634   {
635     READY,               ///< Initial state before Adaptor::Start is called.
636     RUNNING,             ///< Adaptor is running.
637     PAUSED,              ///< Adaptor has been paused.
638     PAUSED_WHILE_HIDDEN, ///< Adaptor is paused while window is hidden (& cannot be resumed until window is shown).
639     STOPPED,             ///< Adaptor has been stopped.
640   };
641
642   using WindowPtr = IntrusivePtr< Window >;
643   using WindowContainer = std::vector<WindowPtr>;
644   using ObserverContainer = std::vector<LifeCycleObserver*>;
645
646 private: // Data
647
648   AdaptorSignalType                     mResizedSignal;               ///< Resized signal.
649   AdaptorSignalType                     mLanguageChangedSignal;       ///< Language changed signal.
650
651   Dali::Adaptor&                        mAdaptor;                     ///< Reference to public adaptor instance.
652   State                                 mState;                       ///< Current state of the adaptor
653   Dali::Integration::Core*              mCore;                        ///< Dali Core
654   ThreadController*                     mThreadController;            ///< Controls the threads
655   VSyncMonitor*                         mVSyncMonitor;                ///< Monitors VSync events
656
657   GraphicsInterface*                    mGraphics;                    ///< Graphics interface
658   Dali::DisplayConnection*              mDisplayConnection;           ///< Display connection
659   WindowContainer                       mWindows;                     ///< A container of all the Windows that are currently created
660
661   TizenPlatform::TizenPlatformAbstraction* mPlatformAbstraction;      ///< Platform abstraction
662
663   CallbackManager*                      mCallbackManager;             ///< Used to install callbacks
664   bool                                  mNotificationOnIdleInstalled; ///< whether the idle handler is installed to send an notification event
665   TriggerEventInterface*                mNotificationTrigger;         ///< Notification event trigger
666   GestureManager*                       mGestureManager;              ///< Gesture manager
667   FeedbackPluginProxy*                  mDaliFeedbackPlugin;          ///< Used to access feedback support
668   FeedbackController*                   mFeedbackController;          ///< Plays feedback effects for Dali-Toolkit UI Controls.
669   Dali::TtsPlayer                       mTtsPlayers[Dali::TtsPlayer::MODE_NUM];                   ///< Provides TTS support
670   ObserverContainer                     mObservers;                   ///< A list of adaptor observer pointers
671   DragAndDropDetectorPtr                mDragAndDropDetector;         ///< The Drag & Drop detector
672   RotationObserver*                     mDeferredRotationObserver;    ///< deferred Rotation observer needs event handler
673   EnvironmentOptions*                   mEnvironmentOptions;          ///< environment options
674   PerformanceInterface*                 mPerformanceInterface;        ///< Performance interface
675   KernelTrace                           mKernelTracer;                ///< Kernel tracer
676   SystemTrace                           mSystemTracer;                ///< System tracer
677   TriggerEventFactory                   mTriggerEventFactory;         ///< Trigger event factory
678   ObjectProfiler*                       mObjectProfiler;              ///< Tracks object lifetime for profiling
679   SocketFactory                         mSocketFactory;               ///< Socket factory
680   const bool                            mEnvironmentOptionsOwned:1;   ///< Whether we own the EnvironmentOptions (and thus, need to delete it)
681   bool                                  mUseRemoteSurface;            ///< whether the remoteSurface is used or not
682
683 public:
684   inline static Adaptor& GetImplementation(Dali::Adaptor& adaptor) { return *adaptor.mImpl; }
685 };
686
687 } // namespace Internal
688
689 } // namespace Adaptor
690
691 } // namespace Dali
692
693 #endif // DALI_INTERNAL_ADAPTOR_IMPL_H