Moved FrameTime from Core to Adaptor
[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   mRenderMode((type == PIXMAP) ? RENDER_SYNC : RENDER_DEFAULT),
74   mRenderNotification( NULL ),
75   mSyncMode(SYNC_MODE_NONE),
76   mSyncReceived( false ),
77   mOwnSurface(false),
78   mOwnDisplay(false),
79   mIsStopped( false )
80 {
81   // see if there is a display in Any display
82   SetDisplay( display );
83 }
84
85 void RenderSurface::Init( Any surface )
86 {
87   // see if there is a surface in Any surface
88   unsigned int surfaceId  = GetSurfaceId( surface );
89
90   // if the surface is empty, create a new one.
91   if ( surfaceId == 0 )
92   {
93     // make sure XInitThreads is called
94     if ( !gXInitThreadsCalled )
95     {
96       XInitThreads();
97       gXInitThreadsCalled = true;
98     }
99
100     // we own the surface about to created
101     mOwnSurface = true;
102     CreateXRenderable();
103   }
104   else
105   {
106     // XLib should already be initialized so no point in calling XInitThreads
107     UseExistingRenderable( surfaceId );
108   }
109
110 #ifdef DEBUG_ENABLED
111   // prints out 'INFO: DALI: new RenderSurface, created display xx, used existing surface xx
112   // we can not use LOG_INFO because the surface can be created before Dali Core is created.
113   printf(  "INFO: DALI: new RenderSurface, %s display %p, %s %s surface %X \n",
114              mOwnDisplay?"created":"used existing",mMainDisplay,
115              mOwnSurface?"created":"used existing",
116              Dali::RenderSurface::PIXMAP==mType?" pixmap" :"window",
117              GetDrawable() );
118 #endif
119 }
120
121 RenderSurface::~RenderSurface()
122 {
123   // release the display connection if we use our own
124   if( mOwnDisplay )
125   {
126     if( mMainDisplay )
127     {
128       // NOTE, on 64bit desktop with some NVidia driver versions this crashes
129 #ifdef _ARCH_ARM_
130       XCloseDisplay( mMainDisplay );
131 #endif
132     }
133   }
134 }
135
136 Ecore_X_Window RenderSurface::GetXWindow()
137 {
138   return 0;
139 }
140
141 XDisplay* RenderSurface::GetMainDisplay()
142 {
143   return mMainDisplay;
144 }
145
146 void RenderSurface::SetRenderNotification( TriggerEvent* renderNotification )
147 {
148   mRenderNotification = renderNotification;
149 }
150
151 Ecore_X_Drawable RenderSurface::GetDrawable()
152 {
153   return 0;
154 }
155
156 Any RenderSurface::GetDisplay()
157 {
158   // this getter is used by main thread so we need to return the main thread version of the display
159   return Any( ecore_x_display_get() );
160 }
161
162 PositionSize RenderSurface::GetPositionSize() const
163 {
164   return mPosition;
165 }
166
167 void RenderSurface::SetRenderMode(RenderMode mode)
168 {
169   mRenderMode = mode;
170 }
171
172 Dali::RenderSurface::RenderMode RenderSurface::GetRenderMode() const
173 {
174   return mRenderMode;
175 }
176
177 void RenderSurface::MoveResize( Dali::PositionSize positionSize )
178 {
179   // nothing to do in base class
180 }
181
182 void RenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical ) const
183 {
184   // calculate DPI
185   float xres, yres;
186
187   // 1 inch = 25.4 millimeters
188   xres = ecore_x_dpi_get();
189   yres = ecore_x_dpi_get();
190
191   dpiHorizontal = int(xres + 0.5f);  // rounding
192   dpiVertical   = int(yres + 0.5f);
193 }
194
195 void RenderSurface::Map()
196 {
197 }
198
199 void RenderSurface::TransferDisplayOwner( Internal::Adaptor::RenderSurface& newSurface )
200 {
201   // if we don't own the display return
202   if( mOwnDisplay == false )
203   {
204     return;
205   }
206
207   RenderSurface* other = dynamic_cast< RenderSurface* >( &newSurface );
208   if( other )
209   {
210     // if both surfaces share the same display, and this surface owns it,
211     // then transfer the ownership to the new surface
212     if( other->mMainDisplay == mMainDisplay )
213     {
214       mOwnDisplay = false;
215       other->mOwnDisplay = true;
216     }
217   }
218 }
219
220 void RenderSurface::ConsumeEvents()
221 {
222   // if the render surface has own display, check events so that we can flush the queue and avoid
223   // any potential memory leaks in X
224   if( mOwnDisplay )
225   {
226     // looping if events remain
227     int events( 0 );
228     do
229     {
230       // Check if there are any events in the queue
231       events = XEventsQueued( mMainDisplay, QueuedAfterFlush );
232
233       if ( events > 0 )
234       {
235         // Just flush event to prevent memory leak from event queue as the events get built up in
236         // memory but are only deleted when we retrieve them
237         XEvent ev;
238         XNextEvent( mMainDisplay, &ev );
239       }
240     }
241     while( events > 0 );
242   }
243 }
244
245 void RenderSurface::StopRender()
246 {
247   // Stop blocking waiting for sync
248   SetSyncMode( RenderSurface::SYNC_MODE_NONE );
249   // Simulate a RenderSync in case render-thread is currently blocked
250   RenderSync();
251
252   mIsStopped = true;
253 }
254
255 void RenderSurface::SetViewMode( ViewMode )
256 {
257 }
258
259 void RenderSurface::SetDisplay( Any display )
260 {
261   // the render surface can be passed either EFL e-core types, or x11 types
262   // we use boost any to determine at run time which type
263
264   if ( display.Empty() == false )
265   {
266     // check we have a valid type
267     DALI_ASSERT_ALWAYS( ( ( display.GetType() == typeid (Ecore_X_Display *)) ||
268                           ( display.GetType() == typeid (XDisplay *) ) )
269                         &&
270                         "Display type is invalid" );
271
272     mOwnDisplay = false;
273
274     // display may point to EcoreXDisplay so may need to cast
275     if( display.GetType() == typeid (Ecore_X_Display*) )
276     {
277       mMainDisplay = static_cast< XDisplay* >( AnyCast< Ecore_X_Display* >( display ) );
278     }
279     else
280     {
281       mMainDisplay = AnyCast< XDisplay* >( display );
282     }
283   }
284   else
285   {
286     mOwnDisplay = true;
287     // mMainDisplay = (XDisplay*)ecore_x_display_get();
288     // Because of DDK issue, we need to use separated x display instead of ecore default display
289     mMainDisplay = XOpenDisplay(0);
290   }
291 }
292
293 unsigned int RenderSurface::GetSurfaceId( Any surface ) const
294 {
295   unsigned int surfaceId = 0;
296
297   if ( surface.Empty() == false )
298   {
299     // check we have a valid type
300     DALI_ASSERT_ALWAYS( ( (surface.GetType() == typeid (XWindow) ) ||
301                           (surface.GetType() == typeid (Ecore_X_Window) ) )
302                         && "Surface type is invalid" );
303
304     if ( surface.GetType() == typeid (Ecore_X_Window) )
305     {
306       surfaceId = AnyCast<Ecore_X_Window>( surface );
307     }
308     else
309     {
310       surfaceId = AnyCast<XWindow>( surface );
311     }
312   }
313   return surfaceId;
314 }
315
316 void RenderSurface::RenderSync()
317 {
318   // nothing to do
319 }
320
321 void RenderSurface::DoRenderSync( unsigned int timeDelta, SyncMode syncMode )
322 {
323   // Should block waiting for RenderSync?
324   if( mRenderMode == Dali::RenderSurface::RENDER_SYNC )
325   {
326     boost::unique_lock< boost::mutex > lock( mSyncMutex );
327
328     // wait for sync
329     if( syncMode != SYNC_MODE_NONE &&
330         mSyncMode != SYNC_MODE_NONE &&
331         !mSyncReceived )
332     {
333       mSyncNotify.wait( lock );
334     }
335     mSyncReceived = false;
336   }
337   // Software sync based on a timed delay?
338   else if( mRenderMode > Dali::RenderSurface::RENDER_SYNC )
339   {
340     unsigned int syncPeriod( MICROSECONDS_PER_SECOND / static_cast< unsigned int >( mRenderMode ) - MILLISECONDS_PER_SECOND );
341
342     if( timeDelta < syncPeriod )
343     {
344       usleep( syncPeriod - timeDelta );
345     }
346   }
347 }
348
349 } // namespace ECore
350
351 } // namespace Adaptor
352
353 } // namespace Internal
354
355 } // namespace Dali