Add handling for context loss and regain behaviour
[platform/core/uifw/dali-adaptor.git] / adaptors / common / adaptor-impl.h
1 #ifndef __DALI_INTERNAL_ADAPTOR_IMPL_H__
2 #define __DALI_INTERNAL_ADAPTOR_IMPL_H__
3
4 /*
5  * Copyright (c) 2014 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 <boost/bind.hpp>
23 #include <boost/function.hpp>
24 #include <boost/thread.hpp>
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/common/vector-wrapper.h>
28 #include <dali/public-api/common/view-mode.h>
29 #include <dali/public-api/math/rect.h>
30 #include <dali/integration-api/render-controller.h>
31
32 #include <adaptor.h>
33 #include <render-surface.h>
34 #include <tts-player.h>
35 #include <imf-manager.h>
36 #include <device-layout.h>
37 #include <clipboard.h>
38
39 #include <slp-platform-abstraction.h>
40 #include <base/interfaces/adaptor-internal-services.h>
41 #include <base/environment-options.h>
42 #include <base/core-event-interface.h>
43 #include <drag-and-drop-detector-impl.h>
44 #include <damage-observer.h>
45 #include <window-visibility-observer.h>
46 #include <kernel-trace.h>
47 #include <trigger-event-factory.h>
48
49 namespace Dali
50 {
51
52 namespace Integration
53 {
54 class Core;
55 class GlAbstraction;
56 }
57
58 namespace Internal
59 {
60
61 namespace Adaptor
62 {
63 class EventHandler;
64 class EglFactory;
65 class GestureManager;
66 class GlImplementation;
67 class GlSyncImplementation;
68 class RenderSurface;
69 class UpdateRenderController;
70 class TriggerEvent;
71 class CallbackManager;
72 class FeedbackPluginProxy;
73 class FeedbackController;
74 class RotationObserver;
75 class VSyncMonitor;
76 class PerformanceInterface;
77 class LifeCycleObserver;
78 class ObjectProfiler;
79
80 /**
81  * Implementation of the Adaptor class.
82  */
83 class Adaptor : public Integration::RenderController,
84                 public AdaptorInternalServices,
85                 public CoreEventInterface,
86                 public DamageObserver,
87                 public WindowVisibilityObserver
88 {
89 public:
90
91   typedef Dali::Adaptor::AdaptorSignalV2 AdaptorSignalV2;
92
93   /**
94    * Creates a New Adaptor
95    * @param[in]  surface     A render surface can be one of the following
96    *                         - Pixmap, adaptor will use existing Pixmap to draw on to
97    *                         - Window, adaptor will use existing Window to draw on to
98    * @param[in]  baseLayout  The base layout that the application has been written for
99    * @param[in]  configuration The context loss configuration ( to choose resource discard policy )
100    */
101   static Dali::Adaptor* New( RenderSurface* surface,
102                              const DeviceLayout& baseLayout,
103                              Dali::Configuration::ContextLoss configuration );
104
105   /**
106    * 2-step initialisation, this should be called after creating an adaptor instance.
107    */
108   void Initialize(Dali::Configuration::ContextLoss configuration);
109
110   /**
111    * Virtual destructor.
112    */
113   virtual ~Adaptor();
114
115   /**
116    * @copydoc Dali::Adaptor::Get()
117    */
118   static Dali::Adaptor& Get();
119
120   /**
121    * @copydoc Dali::Adaptor::IsAvailable()
122    */
123   static bool IsAvailable();
124
125 public: // AdaptorInternalServices implementation
126   /**
127    * @copydoc Dali::Adaptor::Start()
128    */
129   virtual void Start();
130
131   /**
132    * @copydoc Dali::Adaptor::Pause()
133    */
134   virtual void Pause();
135
136   /**
137    * @copydoc Dali::Adaptor::Resume()
138    */
139   virtual void Resume();
140
141   /**
142    * @copydoc Dali::Adaptor::Stop()
143    */
144   virtual void Stop();
145
146   /**
147    * @copydoc Dali::EventFeeder::FeedTouchPoint()
148    */
149   virtual void FeedTouchPoint( TouchPoint& point, int timeStamp );
150
151   /**
152    * @copydoc Dali::EventFeeder::FeedWheelEvent()
153    */
154   virtual void FeedWheelEvent( MouseWheelEvent& wheelEvent );
155
156   /**
157    * @copydoc Dali::EventFeeder::FeedKeyEvent()
158    */
159   virtual void FeedKeyEvent( KeyEvent& keyEvent );
160
161   /**
162    * @copydoc AdaptorInterface::MoveResize()
163    */
164   virtual bool MoveResize( const PositionSize& positionSize );
165
166   /**
167    * @copydoc AdaptorInterface::SurfaceResized()
168    */
169   virtual void SurfaceResized( const PositionSize& positionSize );
170
171   /**
172    * @copydoc AdaptorInterface::ReplaceSurface()
173    */
174   virtual void ReplaceSurface( Dali::RenderSurface& surface );
175
176   /**
177    * @copydoc Dali::Adaptor::GetSurface()
178    */
179   virtual Dali::RenderSurface& GetSurface() const;
180
181   /**
182    * @copydoc Dali::Adaptor::ReleaseSurfaceLock()
183    */
184   virtual void ReleaseSurfaceLock();
185
186   /**
187    * Retrieve the TtsPlayer.
188    * @param[in] mode The mode of TtsPlayer
189    * @return A handle to the TtsPlayer.
190    */
191   virtual Dali::TtsPlayer GetTtsPlayer(Dali::TtsPlayer::Mode mode);
192
193   /**
194    * @copydoc Dali::Adaptor::AddIdle()
195    */
196   virtual bool AddIdle( boost::function<void(void)> callBack );
197
198   /**
199    * @copydoc Internal::Framework::CallFromMainLoop()
200    */
201   virtual bool CallFromMainLoop(boost::function<void(void)> callBack);
202
203 public:
204
205   /**
206    * @return the Core instance
207    */
208   virtual Dali::Integration::Core& GetCore();
209
210   /**
211    * @copydoc Dali::Adaptor::SetRenderRefreshRate()
212    */
213   void SetRenderRefreshRate( unsigned int numberOfVSyncsPerRender );
214
215   /**
216    * @copydoc Dali::Adaptor::SetUseHardwareVSync()
217    */
218   void SetUseHardwareVSync(bool useHardware);
219
220   /**
221    * Overrides DPI.
222    * Primarily for host/simulation testing
223    * @param[in] hDpi The Horizontal DPI
224    * @param[in] vDpi The Vertical DPI
225    */
226   void SetDpi(size_t hDpi, size_t vDpi);
227
228   /**
229    * @return reference to EglFactory class
230    */
231   EglFactory& GetEGLFactory() const;
232
233   /**
234    * Return GlAbstraction.
235    * @return the GlAbstraction.
236    */
237   Integration::GlAbstraction& GetGlAbstraction() const;
238
239   /**
240    * Return the PlatformAbstraction.
241    * @return The PlatformAbstraction.
242    */
243   Integration::PlatformAbstraction& GetPlatformAbstraction() const;
244
245   /**
246    * Sets the Drag & Drop Listener.
247    * @param[in] detector The detector to send Drag & Drop events to.
248    */
249   void SetDragAndDropDetector( DragAndDropDetectorPtr detector );
250
251   /**
252    * Sets a rotation observer, or set to NULL to remove.
253    * @pre Adaptor::Start() has been called ( to create EventHandler )
254    * @param[in] observer The observer to listen for window rotation events
255    */
256   void SetRotationObserver( RotationObserver* observer );
257
258   /**
259    * Destroy the TtsPlayer of sepcific mode.
260    * @param[in] mode The mode of TtsPlayer to destroy
261    */
262   void DestroyTtsPlayer(Dali::TtsPlayer::Mode mode);
263
264   /**
265    * @brief Sets minimum distance in pixels that the fingers must move towards/away from each other in order to
266    * trigger a pinch gesture
267    *
268    * @param[in] distance The minimum pinch distance in pixels
269    */
270   void SetMinimumPinchDistance(float distance);
271
272 public:
273
274   /**
275    * Adds an adaptor observer so that we can observe the adaptor's lifetime events.
276    * @param[in]  observer  The observer.
277    * @note Observers should remove themselves when they are destroyed.
278    */
279   void AddObserver( LifeCycleObserver& observer );
280
281   /**
282    * Removes the observer from the adaptor.
283    * @param[in]  observer  The observer to remove.
284    * @note Observers should remove themselves when they are destroyed.
285    */
286   void RemoveObserver( LifeCycleObserver& observer );
287
288   /**
289    * Emits the Notification event to the Dali core.
290    */
291   void SendNotificationEvent();
292
293   /**
294    * Request adaptor to update once
295    */
296   void RequestUpdateOnce();
297
298   /**
299    * @copydoc Dali::Adaptor::NotifyLanguageChanged()
300    */
301   void NotifyLanguageChanged();
302
303 public:  //AdaptorInternalServices
304
305   /**
306    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetPlatformAbstractionInterface()
307    */
308   virtual Dali::Integration::PlatformAbstraction& GetPlatformAbstractionInterface();
309
310   /**
311    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetGlesInterface()
312    */
313   virtual Dali::Integration::GlAbstraction& GetGlesInterface();
314
315   /**
316   * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetEGLFactoryInterface()
317   */
318   virtual EglFactoryInterface& GetEGLFactoryInterface() const;
319
320   /**
321    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetTriggerEventInterface()
322    */
323   virtual TriggerEventInterface& GetTriggerEventInterface();
324
325   /**
326    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetTriggerEventFactoryInterface()
327    */
328   virtual TriggerEventFactoryInterface& GetTriggerEventFactoryInterface();
329
330   /**
331    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetRenderSurfaceInterface()
332    */
333   virtual RenderSurface* GetRenderSurfaceInterface();
334
335   /**
336    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetVSyncMonitorInterface()
337    */
338   virtual VSyncMonitorInterface* GetVSyncMonitorInterface();
339
340   /**
341    * @copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetPerformanceInterface()
342    */
343   virtual PerformanceInterface* GetPerformanceInterface();
344
345   /**
346    * copydoc Dali::Internal::Adaptor::AdaptorInternalServices::GetKernelTraceInterface()
347    */
348   virtual KernelTraceInterface& GetKernelTraceInterface();
349
350 public: // Stereoscopy
351
352   /**
353    * @copydoc Dali::Integration::Core::SetViewMode()
354    */
355   void SetViewMode( ViewMode viewMode );
356
357   /**
358    * @copydoc Dali::Integration::Core::GetViewMode()
359    */
360   ViewMode GetViewMode() const;
361
362   /**
363    * @copydoc Dali::Integration::Core::SetStereoBase()
364    */
365   void SetStereoBase( float stereoBase );
366
367   /**
368    * @copydoc Dali::Integration::Core::GetStereoBase()
369    */
370   float GetStereoBase() const;
371
372 public: // Signals
373
374   /**
375    * @copydoc Dali::Adaptor::SignalResized
376    */
377   AdaptorSignalV2& ResizedSignal()
378   {
379     return mResizedSignalV2;
380   }
381
382   /**
383    * @copydoc Dali::Adaptor::LanguageChangedSignal
384    */
385   AdaptorSignalV2& LanguageChangedSignal()
386   {
387     return mLanguageChangedSignalV2;
388   }
389
390 private: // From Dali::Internal::Adaptor::CoreEventInterface
391
392   /**
393    * @copydoc Dali::Internal::Adaptor::CoreEventInterface::QueueCoreEvent()
394    */
395   virtual void QueueCoreEvent(const Dali::Integration::Event& event);
396
397   /**
398    * @copydoc Dali::Internal::Adaptor:CoreEventInterface:::ProcessCoreEvents()
399    */
400   virtual void ProcessCoreEvents();
401
402 private: // From Dali::Integration::RenderController
403
404   /**
405    * Called by the Dali core when it requires another update
406    */
407   virtual void RequestUpdate();
408
409   /**
410    * Called by Dali core when it requires an notification event being sent on idle.
411    * Multi-threading note: this method must be called from the main thread only.
412    */
413   virtual void RequestProcessEventsOnIdle();
414
415 private: // From Dali::Internal::Adaptor::WindowVisibilityObserver
416
417   /**
418    * Called when the window becomes fully or partially visible.
419    */
420   virtual void OnWindowShown();
421
422   /**
423    * Called when the window is fully hidden.
424    */
425   virtual void OnWindowHidden();
426
427 private: // From Dali::Internal::Adaptor::DamageObserver
428
429   /**
430    * @copydoc Dali::Internal::Adaptor::DamageObserver::OnDamaged()
431    */
432   void OnDamaged( const DamageArea& area );
433
434 private:
435
436   // Undefined
437   Adaptor(const Adaptor&);
438   Adaptor& operator=(Adaptor&);
439
440 private:
441
442   /**
443    * Helper to parse log options
444    */
445   void ParseEnvironmentOptions();
446
447   /**
448    * Informs core the surface size has changed
449    */
450   void SurfaceSizeChanged(const PositionSize& positionSize);
451
452   /**
453    * Assigns the render surface to the adaptor
454    *
455    */
456   void SetSurface(Dali::RenderSurface *surface);
457
458   /**
459    * Sends an notification message from main loop idle handler
460    */
461   void ProcessCoreEventsFromIdle();
462
463 private:
464
465   /**
466    * Constructor
467    * @param[in]  adaptor     The public adaptor
468    * @param[in]  surface     A render surface can be one of the following
469    *                         - Pixmap, adaptor will use existing Pixmap to draw on to
470    *                         - Window, adaptor will use existing Window to draw on to
471    * @param[in]  baseLayout  The base layout that the application has been written for
472    */
473   Adaptor( Dali::Adaptor& adaptor, RenderSurface* surface, const DeviceLayout& baseLayout );
474
475 private: // Types
476
477   enum State
478   {
479     READY,               ///< Initial state before Adaptor::Start is called.
480     RUNNING,             ///< Adaptor is running.
481     PAUSED,              ///< Adaptor has been paused.
482     PAUSED_WHILE_HIDDEN, ///< Adaptor is paused while window is hidden (& cannot be resumed until window is shown).
483     STOPPED,             ///< Adaptor has been stopped.
484   };
485
486   typedef std::vector<LifeCycleObserver*>  ObserverContainer;
487
488 private: // Data
489
490   AdaptorSignalV2                       mResizedSignalV2;             ///< Resized signal.
491   AdaptorSignalV2                       mLanguageChangedSignalV2;     ///< Language changed signal.
492
493   Dali::Adaptor&                        mAdaptor;                     ///< Reference to public adaptor instance.
494   State                                 mState;                       ///< Current state of the adaptor
495   Dali::Integration::Core*              mCore;                        ///< Dali Core
496   UpdateRenderController*               mUpdateRenderController;      ///< Controls update/render threads
497   VSyncMonitor*                         mVSyncMonitor;                ///< Monitors VSync events
498   GlImplementation*                     mGLES;                        ///< GL implementation
499   GlSyncImplementation*                 mGlSync;                      ///< GL Sync implementation
500   EglFactory*                           mEglFactory;                  ///< EGL Factory
501
502   RenderSurface*                        mSurface;                     ///< Current surface
503   SlpPlatform::SlpPlatformAbstraction*  mPlatformAbstraction;         ///< Platform abstraction
504
505   EventHandler*                         mEventHandler;                ///< event handler
506   CallbackManager*                      mCallbackManager;             ///< Used to install callbacks
507   bool                                  mNotificationOnIdleInstalled; ///< whether the idle handler is installed to send an notification event
508   TriggerEvent*                         mNotificationTrigger;         ///< Notification event trigger
509   GestureManager*                       mGestureManager;              ///< Gesture manager
510   size_t                                mHDpi;                        ///< Override horizontal DPI
511   size_t                                mVDpi;                        ///< Override vertical DPI
512   FeedbackPluginProxy*                  mDaliFeedbackPlugin;          ///< Used to access feedback support
513   FeedbackController*                   mFeedbackController;          ///< Plays feedback effects for Dali-Toolkit UI Controls.
514   Dali::TtsPlayer                       mTtsPlayers[Dali::TtsPlayer::MODE_NUM];                   ///< Provides TTS support
515   ObserverContainer                     mObservers;                   ///< A list of adaptor observer pointers
516   DragAndDropDetectorPtr                mDragAndDropDetector;         ///< The Drag & Drop detector
517   RotationObserver*                     mDeferredRotationObserver;    ///< deferred Rotation observer needs event handler
518   DeviceLayout                          mBaseLayout;                  ///< The base layout of the application
519   EnvironmentOptions                    mEnvironmentOptions;          ///< environment options
520   PerformanceInterface*                 mPerformanceInterface;        ///< Performance interface
521   KernelTrace                           mKernelTracer;                ///< Kernel tracer
522   TriggerEventFactory                   mTriggerEventFactory;         ///< Trigger event factory
523   ObjectProfiler*                       mObjectProfiler;              ///< Tracks object lifetime for profiling
524 public:
525   inline static Adaptor& GetImplementation(Dali::Adaptor& adaptor) {return *adaptor.mImpl;}
526 };
527
528 } // namespace Internal
529
530 } // namespace Adaptor
531
532 } // namespace Dali
533
534 #endif // __DALI_INTERNAL_ADAPTOR_IMPL_H__