add IndicatorVisibilityChangedSignal
[platform/core/uifw/dali-adaptor.git] / adaptors / x11 / ecore-x-render-surface.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "ecore-x-render-surface.h"
20
21 // EXTERNAL INCLUDES
22 #include <X11/Xatom.h>
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25
26 #include <X11/extensions/Xfixes.h> // for damage notify
27 #include <X11/extensions/Xdamage.h> // for damage notify
28
29 #include <dali/integration-api/gl-abstraction.h>
30 #include <dali/integration-api/debug.h>
31
32 // INTERNAL INCLUDES
33 #include <ecore-x-types.h>
34 #include <trigger-event.h>
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41
42 namespace Adaptor
43 {
44
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_ECORE_X_RENDER_SURFACE");
47 #endif
48
49 namespace ECore
50 {
51
52 namespace
53 {
54
55 const float MINIMUM_DIMENSION_CHANGE = 1.0f; ///< Minimum change for window to be considered to have moved
56 static bool gXInitThreadsCalled = false;     ///< global to say whether XInitThreads has been called in this process
57 const unsigned int MICROSECONDS_PER_SECOND = 1000000;
58 const unsigned int MILLISECONDS_PER_SECOND = 1000;
59
60 } // unnamed namespace
61
62 RenderSurface::RenderSurface( SurfaceType type,
63                               Dali::PositionSize positionSize,
64                               Any surface,
65                               Any display,
66                               const std::string& name,
67                               bool isTransparent)
68 : mMainDisplay(NULL),
69   mType(type),
70   mPosition(positionSize),
71   mTitle(name),
72   mColorDepth(isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24),
73   mRenderNotification( NULL ),
74   mOwnSurface(false),
75   mOwnDisplay(false)
76 {
77   // see if there is a display in Any display
78   SetDisplay( display );
79 }
80
81 void RenderSurface::Init( Any surface )
82 {
83   // see if there is a surface in Any surface
84   unsigned int surfaceId  = GetSurfaceId( surface );
85
86   // if the surface is empty, create a new one.
87   if ( surfaceId == 0 )
88   {
89     // make sure XInitThreads is called
90     if ( !gXInitThreadsCalled )
91     {
92       XInitThreads();
93       gXInitThreadsCalled = true;
94     }
95
96     // we own the surface about to created
97     mOwnSurface = true;
98     CreateXRenderable();
99   }
100   else
101   {
102     // XLib should already be initialized so no point in calling XInitThreads
103     UseExistingRenderable( surfaceId );
104   }
105
106 #ifdef DEBUG_ENABLED
107   // prints out 'INFO: DALI: new RenderSurface, created display xx, used existing surface xx
108   // we can not use LOG_INFO because the surface can be created before Dali Core is created.
109   printf(  "INFO: DALI: new RenderSurface, %s display %p, %s %s surface %X \n",
110              mOwnDisplay?"created":"used existing",mMainDisplay,
111              mOwnSurface?"created":"used existing",
112              Dali::RenderSurface::PIXMAP==mType?" pixmap" :"window",
113              GetDrawable() );
114 #endif
115 }
116
117 RenderSurface::~RenderSurface()
118 {
119   // release the display connection if we use our own
120   if( mOwnDisplay )
121   {
122     if( mMainDisplay )
123     {
124       // NOTE, on 64bit desktop with some NVidia driver versions this crashes
125 #ifdef _ARCH_ARM_
126       XCloseDisplay( mMainDisplay );
127 #endif
128     }
129   }
130 }
131
132 Ecore_X_Window RenderSurface::GetXWindow()
133 {
134   return 0;
135 }
136
137 XDisplay* RenderSurface::GetMainDisplay()
138 {
139   return mMainDisplay;
140 }
141
142 void RenderSurface::SetRenderNotification( TriggerEvent* renderNotification )
143 {
144   mRenderNotification = renderNotification;
145 }
146
147 Ecore_X_Drawable RenderSurface::GetDrawable()
148 {
149   return 0;
150 }
151
152 Any RenderSurface::GetDisplay()
153 {
154   // this getter is used by main thread so we need to return the main thread version of the display
155   return Any( ecore_x_display_get() );
156 }
157
158 PositionSize RenderSurface::GetPositionSize() const
159 {
160   return mPosition;
161 }
162
163 void RenderSurface::MoveResize( Dali::PositionSize positionSize )
164 {
165   // nothing to do in base class
166 }
167
168 void RenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical ) const
169 {
170   // calculate DPI
171   float xres, yres;
172
173   // 1 inch = 25.4 millimeters
174   xres = ecore_x_dpi_get();
175   yres = ecore_x_dpi_get();
176
177   dpiHorizontal = int(xres + 0.5f);  // rounding
178   dpiVertical   = int(yres + 0.5f);
179 }
180
181 void RenderSurface::Map()
182 {
183 }
184
185 void RenderSurface::TransferDisplayOwner( Internal::Adaptor::RenderSurface& newSurface )
186 {
187   // if we don't own the display return
188   if( mOwnDisplay == false )
189   {
190     return;
191   }
192
193   RenderSurface* other = dynamic_cast< RenderSurface* >( &newSurface );
194   if( other )
195   {
196     // if both surfaces share the same display, and this surface owns it,
197     // then transfer the ownership to the new surface
198     if( other->mMainDisplay == mMainDisplay )
199     {
200       mOwnDisplay = false;
201       other->mOwnDisplay = true;
202     }
203   }
204 }
205
206 void RenderSurface::ConsumeEvents()
207 {
208   // if the render surface has own display, check events so that we can flush the queue and avoid
209   // any potential memory leaks in X
210   if( mOwnDisplay )
211   {
212     // looping if events remain
213     int events( 0 );
214     do
215     {
216       // Check if there are any events in the queue
217       events = XEventsQueued( mMainDisplay, QueuedAfterFlush );
218
219       if ( events > 0 )
220       {
221         // Just flush event to prevent memory leak from event queue as the events get built up in
222         // memory but are only deleted when we retrieve them
223         XEvent ev;
224         XNextEvent( mMainDisplay, &ev );
225       }
226     }
227     while( events > 0 );
228   }
229 }
230
231 void RenderSurface::SetViewMode( ViewMode )
232 {
233 }
234
235 void RenderSurface::SetDisplay( Any display )
236 {
237   // the render surface can be passed either EFL e-core types, or x11 types
238   // we use boost any to determine at run time which type
239
240   if ( display.Empty() == false )
241   {
242     // check we have a valid type
243     DALI_ASSERT_ALWAYS( ( ( display.GetType() == typeid (Ecore_X_Display *)) ||
244                           ( display.GetType() == typeid (XDisplay *) ) )
245                         &&
246                         "Display type is invalid" );
247
248     mOwnDisplay = false;
249
250     // display may point to EcoreXDisplay so may need to cast
251     if( display.GetType() == typeid (Ecore_X_Display*) )
252     {
253       mMainDisplay = static_cast< XDisplay* >( AnyCast< Ecore_X_Display* >( display ) );
254     }
255     else
256     {
257       mMainDisplay = AnyCast< XDisplay* >( display );
258     }
259   }
260   else
261   {
262     mOwnDisplay = true;
263     // mMainDisplay = (XDisplay*)ecore_x_display_get();
264     // Because of DDK issue, we need to use separated x display instead of ecore default display
265     mMainDisplay = XOpenDisplay(0);
266   }
267 }
268
269 unsigned int RenderSurface::GetSurfaceId( Any surface ) const
270 {
271   unsigned int surfaceId = 0;
272
273   if ( surface.Empty() == false )
274   {
275     // check we have a valid type
276     DALI_ASSERT_ALWAYS( ( (surface.GetType() == typeid (XWindow) ) ||
277                           (surface.GetType() == typeid (Ecore_X_Window) ) )
278                         && "Surface type is invalid" );
279
280     if ( surface.GetType() == typeid (Ecore_X_Window) )
281     {
282       surfaceId = AnyCast<Ecore_X_Window>( surface );
283     }
284     else
285     {
286       surfaceId = AnyCast<XWindow>( surface );
287     }
288   }
289   return surfaceId;
290 }
291
292 } // namespace ECore
293
294 } // namespace Adaptor
295
296 } // namespace Internal
297
298 } // namespace Dali