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