[3.0] Add code to handle native indicator image
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / common / ecore-indicator-impl.h
1 #ifndef __DALI_INTERNAL_ECORE_INDICATOR_H__
2 #define __DALI_INTERNAL_ECORE_INDICATOR_H__
3
4 /*
5  * Copyright (c) 2016 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/animation/animation.h>
23 #include <dali/public-api/events/pan-gesture.h>
24 #include <dali/public-api/events/pan-gesture-detector.h>
25 #include <dali/public-api/rendering/renderer.h>
26 #include <dali/public-api/images/image.h>
27 #include <dali/public-api/object/any.h>
28
29 // INTERNAL INCLUDES
30 #include <base/interfaces/indicator-interface.h>
31 #include <native-image-source.h>
32 #include <indicator-buffer.h>
33 #include <ecore-server-connection.h>
34 #include <shared-file.h>
35 #include <timer.h>
36 #include <window.h>
37
38 namespace Dali
39 {
40 namespace Integration
41 {
42 class Core;
43 }
44
45 namespace Internal
46 {
47 namespace Adaptor
48 {
49 class Adaptor;
50
51 typedef unsigned int PixmapId;
52
53 /**
54  * The Indicator class connects to the indicator server, and gets and draws the indicator
55  * for the given orientation.
56  */
57 class Indicator : public ConnectionTracker, public ServerConnection::Observer, public IndicatorInterface
58 {
59 public:
60
61   enum State
62   {
63     DISCONNECTED,
64     CONNECTED
65   };
66
67   /**
68    * copied from ecore_evas_extn_engine.h
69    */
70   enum BufferType
71   {
72     BUFFER_TYPE_SHM = 0,        ///< shared memory-based buffer backend
73     BUFFER_TYPE_DRI2_PIXMAP,    ///< dri2 pixmap-based buffer backend
74     BUFFER_TYPE_EVASGL_PIXMAP,  ///< pixmap backend for Evas GL only (DEPRECATED)
75     BUFFER_TYPE_GL_PIXMAP,      ///< double buffered GL pixmap backend
76   };
77
78 protected:
79   /**
80    * Class to encapsulate lock file
81    */
82   class LockFile
83   {
84   public:
85     /**
86      * Constructor. open lock file
87      */
88     LockFile(const std::string filename);
89
90     /**
91      * Close lock file
92      */
93     ~LockFile();
94
95     /**
96      * Grab an exclusive lock on this file
97      * @return true if the lock succeeded, false if it failed
98      */
99     bool Lock();
100
101     /**
102      * Remove the lock
103      */
104     void Unlock();
105
106     /**
107      * Test if there is an error with the lock file, and clears
108      * the error flag;
109      * @return true if an error was thrown
110      */
111     bool RetrieveAndClearErrorStatus();
112
113   private:
114     std::string mFilename;
115     int         mFileDescriptor;
116     bool        mErrorThrown;
117   };
118
119   /**
120    * Class to ensure lock/unlock through object destruction
121    */
122   class ScopedLock
123   {
124   public:
125     /**
126      * Constructor - creates a lock on the lockfile
127      * @param[in] lockFile The lockfile to use
128      */
129     ScopedLock( LockFile* lockFile );
130
131     /**
132      * Destructor - removes the lock (if any) on the lockfile
133      */
134     ~ScopedLock();
135
136     /**
137      * Method to test if the locking succeeded
138      * @return TRUE if locked
139      */
140     bool IsLocked();
141
142   private:
143     LockFile* mLockFile; ///< The lock file to use
144     bool      mLocked;   ///< Whether the lock succeeded
145   };
146
147
148 public:  // Dali::Internal::Adaptor::IndicicatorInterface
149   /**
150    * @copydoc Dali::Internal::IndicatorInterface::IndicatorInterface
151    */
152   Indicator( Adaptor* adaptor,
153              Dali::Window::WindowOrientation orientation,
154              IndicatorInterface::Observer* observer );
155
156   /**
157    * @copydoc Dali::Internal::IndicatorInterface::~IndicatorInterface
158    */
159   virtual ~Indicator();
160
161
162   virtual void SetAdaptor(Adaptor* adaptor);
163
164   /**
165    * @copydoc Dali::Internal::IndicatorInterface::GetActor
166    */
167   virtual Dali::Actor GetActor();
168
169   /**
170    * @copydoc Dali::Internal::IndicatorInterface::Open
171    */
172   virtual void Open( Dali::Window::WindowOrientation orientation );
173
174   /**
175    * @copydoc Dali::Internal::IndicatorInterface::Close
176    */
177   virtual void Close();
178
179   /**
180    * @copydoc Dali::Internal::IndicatorInterface::SetOpacityMode
181    */
182   virtual void SetOpacityMode( Dali::Window::IndicatorBgOpacity mode );
183
184   /**
185    * @copydoc Dali::Internal::IndicatorInterface::SetVisible
186    */
187   virtual void SetVisible( Dali::Window::IndicatorVisibleMode visibleMode, bool forceUpdate = false );
188
189   /**
190    * @copydoc Dali::Internal::IndicatorInterface::IsConnected
191    */
192   virtual bool IsConnected();
193
194   /**
195    * @copydoc Dali::Internal::IndicatorInterface::SendMessage
196    */
197   virtual bool SendMessage( int messageDomain, int messageId, const void *data, int size );
198
199   /**
200    * Update native indicator image
201    * @param[in] source Native indicator image source
202    */
203   void UpdateIndicatorImage( Dali::Any source );
204
205 private:
206   /**
207    * Initialize the indicator actors
208    */
209   void Initialize();
210
211   /**
212    * Constructs the renderers used for the background
213    */
214   Dali::Geometry CreateBackgroundGeometry();
215
216   /**
217    * Set the texture to be rendered as indicator foreground
218    * @param[in] texture The foreground texture.
219    */
220   void SetForegroundImage( Dali::Texture texture );
221
222   /**
223    * Set the texture to be rendered as indicator foreground
224    * @param[in] image The foreground image.
225    */
226   void SetForegroundNativeImage( Dali::Image image );
227
228   /**
229    * Touch event callback.
230    * It should pass the valid touch event to indicator server
231    *
232    * @param[in] indicator  The indicator actor that was touched
233    * @param[in] touchEvent The touch event
234    */
235   bool OnTouched(Dali::Actor indicator, const TouchEvent& touchEvent);
236
237   /**
238    * Pan gesture callback.
239    * It finds flick down gesture to show hidden indicator image
240    *
241    * @param[in] actor  The actor for gesture
242    * @param[in] gesture The gesture event
243    */
244   void OnPan( Dali::Actor actor, const Dali::PanGesture& gesture );
245
246   /**
247    * Touch event callback on stage.
248    * If stage is touched, hide showing indicator image
249    *
250    * @param[in] touchEvent The touch event
251    */
252   void OnStageTouched(const Dali::TouchEvent& touchEvent);
253
254   /**
255    * Connect to the indicator service
256    */
257   bool Connect();
258
259   /**
260    * Start the reconnection timer. This will run every second until we reconnect to
261    * the indicator service.
262    */
263   void StartReconnectionTimer();
264
265   /**
266    * If connection failed, attempt to re-connect every second
267    */
268   bool OnReconnectTimer();
269
270   /**
271    * Disconnect from the indicator service
272    */
273   void Disconnect();
274
275   /**
276    * Handle Resize event
277    * @param[in] width The new width
278    * @param[in] height The new height
279    */
280   void Resize( int width, int height );
281
282   /**
283    * Set the lock file info.
284    * @param[in] epcEvent Current ecore event.
285    */
286   void SetLockFileInfo( Ecore_Ipc_Event_Server_Data *epcEvent );
287
288   /**
289    * Set the shared indicator image info
290    * @param[in] epcEvent The event containing the image data
291    */
292   void SetSharedImageInfo( Ecore_Ipc_Event_Server_Data *epcEvent );
293
294   /**
295    * Load the shared indicator image
296    * @param[in] epcEvent The event containing the image data
297    */
298   void LoadSharedImage( Ecore_Ipc_Event_Server_Data *epcEvent );
299
300   /**
301    * Load the pixmap indicator image
302    * @param[in] epcEvent The event containing the image data
303    */
304   void LoadPixmapImage( Ecore_Ipc_Event_Server_Data *epcEvent );
305
306   /**
307    * Update top margin of the stage as much as indicator height
308    */
309   void UpdateTopMargin();
310
311   /**
312    * Setup native indicator image
313    * @param[in] epcEvent The event containing the image data information
314    */
315   void SetupNativeIndicatorImage( Ecore_Ipc_Event_Server_Data *epcEvent );
316
317   /**
318    * Update the visibility and position of the actors
319    */
320   void UpdateVisibility();
321
322   /**
323    * Inform dali that the indicator data has been updated.
324    * @param[in] bufferNumber The shared file number
325    */
326   void UpdateImageData( int bufferNumber );
327
328   /**
329    * Lock the temporary file, Copy the shared image into IndicatorBuffer
330    * and then unlock the temporary file.
331    * Caller should ensure we are not writing image to gl texture.
332    * @param[in] bufferNumber The shared file number
333    */
334   bool CopyToBuffer( int bufferNumber );
335
336   /**
337    * Create a new image for the indicator, and set up signal handling for it.
338    * @param[in] bufferNumber The shared file number
339    */
340   void CreateNewImage( int bufferNumber );
341
342   /**
343    * Create a new pixmap image for the indicator, and set up signal handling for it.
344    */
345   void CreateNewPixmapImage();
346
347   /**
348    * Indicator type has changed.
349    * Inform observer
350    * @param[in] type The new indicator type
351    */
352   void OnIndicatorTypeChanged( Type type );
353
354   /**
355    * Check whether the indicator could be visible or invisible
356    * @return true if indicator should be shown
357    */
358   bool CheckVisibleState();
359
360   /**
361    * Show/Hide indicator actor with effect
362    * @param[in] duration how long need to show the indicator,
363    *                     if it equal to 0, hide the indicator
364    *                     if it less than 0, show always
365    */
366   void ShowIndicator( float duration );
367
368   /**
369    * Showing timer callback
370    */
371   bool OnShowTimer();
372
373   /**
374    * Showing animation finished callback
375    * @param[in] animation
376    */
377   void OnAnimationFinished( Dali::Animation& animation );
378
379   /**
380    * Set up native indicator image
381    */
382   void SetupNativeIndicatorImage();
383
384 private: // Implementation of ServerConnection::Observer
385   /**
386    * @copydoc Dali::Internal::Adaptor::ServerConnection::Observer::DataReceived()
387    */
388   virtual void DataReceived( void* event );
389
390   /**
391    * @copydoc Dali::Internal::Adaptor::ServerConnection::Observer::DataReceived()
392    */
393   virtual void ConnectionClosed();
394
395 private:
396
397   /**
398    * Clear shared file info
399    */
400   void ClearSharedFileInfo();
401
402 private:
403
404   struct SharedFileInfo
405   {
406     SharedFileInfo()
407       : mLock( NULL ),
408         mSharedFile( NULL ),
409         mImageWidth( 0 ),
410         mImageHeight( 0 ),
411         mLockFileName(),
412         mSharedFileName(),
413         mSharedFileID( 0 ),
414         mSharedFileNumber( 0 )
415     {
416     }
417
418     LockFile*                        mLock;              ///< File lock for the shared file
419     SharedFile*                      mSharedFile;        ///< Shared file
420
421     int                              mImageWidth;        ///< Shared image width
422     int                              mImageHeight;       ///< Shared image height
423
424     std::string                      mLockFileName;      ///< Lock file name
425     std::string                      mSharedFileName;    ///< Shared file name
426     int                              mSharedFileID;      ///< Shared file ID
427     int                              mSharedFileNumber;  ///< Shared file number
428   };
429
430   static const int SHARED_FILE_NUMBER = 2;               ///< Shared file number
431
432   Dali::Geometry                   mTranslucentGeometry; ///< Geometry used for rendering the translucent background
433   Dali::Geometry                   mSolidGeometry;       ///< Geometry used for rendering the opaque background
434   Dali::Shader                     mBackgroundShader;    ///< Shader used for rendering the background
435
436   IndicatorBufferPtr               mIndicatorBuffer;     ///< class which handles indicator rendering
437   PixmapId                         mPixmap;              ///< Pixmap including indicator content
438   Dali::NativeImageSourcePtr       mNativeImageSource;
439   Dali::Renderer                   mForegroundRenderer;  ///< Renderer renders the indicator foreground
440   Dali::Renderer                   mBackgroundRenderer;  ///< Renderer renders the indicator background
441
442   Dali::Actor                      mIndicatorContentActor; ///< Actor container for image and background
443   Dali::Actor                      mIndicatorActor;      ///< Handle to topmost indicator actor
444   Dali::Actor                      mEventActor;          ///< Handle to event
445   Dali::PanGestureDetector         mPanDetector;         ///< Pan detector to find flick gesture for hidden indicator
446   float                            mGestureDeltaY;       ///< Checking how much panning moved
447   bool                             mGestureDetected;     ///< Whether find the flick gesture
448
449   Dali::Timer                      mReconnectTimer;      ///< Reconnection timer
450   SlotDelegate< Indicator >        mConnection;
451
452   Dali::Window::IndicatorBgOpacity mOpacityMode;         ///< Opacity enum for background
453   Indicator::State                 mState;               ///< The connection state
454
455   Adaptor*                         mAdaptor;
456   ServerConnection*                mServerConnection;
457   IndicatorInterface::Observer*    mObserver;            ///< Upload observer
458
459   Dali::Window::WindowOrientation  mOrientation;
460   int                              mImageWidth;
461   int                              mImageHeight;
462   Dali::Window::IndicatorVisibleMode mVisible;           ///< Whether the indicator is visible
463
464   Dali::Timer                      mShowTimer;           ///< Timer to show indicator
465   bool                             mIsShowing;           ///< Whether the indicator is showing on the screen
466   Dali::Animation                  mIndicatorAnimation;  ///< Animation to show/hide indicator image
467
468   bool                             mIsAnimationPlaying;  ///< Whether the animation is playing
469
470   int                              mCurrentSharedFile;   ///< Current shared file number
471   SharedFileInfo                   mSharedFileInfo[SHARED_FILE_NUMBER];    ///< Table to store shared file info
472
473   BufferType                       mSharedBufferType;    ///< Shared buffer type which is used to render indicator
474
475   struct Impl; ///< Contains Ecore specific information
476   Impl* mImpl; ///< Created on construction and destroyed on destruction.
477
478   bool                             mBackgroundVisible;   ///< Indicate whether background is visible
479   int                              mTopMargin;   ///< Top margin of the stage for indicator
480 };
481
482 } // Adaptor
483 } // Internal
484 } // Dali
485
486 #endif