2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
18 #include "ecore-x-render-surface.h"
21 #include <X11/Xatom.h>
23 #include <X11/Xutil.h>
25 #include <X11/extensions/Xfixes.h> // for damage notify
26 #include <X11/extensions/Xdamage.h> // for damage notify
28 #include <dali/integration-api/gl-abstraction.h>
29 #include <dali/integration-api/debug.h>
32 #include <internal/common/ecore-x/ecore-x-types.h>
33 #include <internal/common/trigger-event.h>
44 #if defined(DEBUG_ENABLED)
45 Debug::Filter* gRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_ECORE_X_RENDER_SURFACE");
54 const float MINIMUM_DIMENSION_CHANGE = 1.0f; ///< Minimum change for window to be considered to have moved
55 static bool gXInitThreadsCalled = false; ///< global to say whether XInitThreads has been called in this process
56 const unsigned int MICROSECONDS_PER_SECOND = 1000000;
57 const unsigned int MILLISECONDS_PER_SECOND = 1000;
59 } // unnamed namespace
61 RenderSurface::RenderSurface( SurfaceType type,
62 Dali::PositionSize positionSize,
65 const std::string& name,
69 mPosition(positionSize),
71 mColorDepth(isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24),
72 mRenderMode((type == PIXMAP) ? RENDER_SYNC : RENDER_DEFAULT),
73 mRenderNotification( NULL ),
74 mSyncReceived( false ),
79 // see if there is a display in Any display
80 SetDisplay( display );
83 void RenderSurface::Init( Any surface )
85 // see if there is a surface in Any surface
86 unsigned int surfaceId = GetSurfaceId( surface );
88 // if the surface is empty, create a new one.
91 // make sure XInitThreads is called
92 if ( !gXInitThreadsCalled )
95 gXInitThreadsCalled = true;
98 // we own the surface about to created
104 // XLib should already be initialized so no point in calling XInitThreads
105 UseExistingRenderable( surfaceId );
109 // prints out 'INFO: DALI: new RenderSurface, created display xx, used existing surface xx
110 // we can not use LOG_INFO because the surface can be created before Dali Core is created.
111 printf( "INFO: DALI: new RenderSurface, %s display %p, %s %s surface %X \n",
112 mOwnDisplay?"created":"used existing",mMainDisplay,
113 mOwnSurface?"created":"used existing",
114 Dali::RenderSurface::PIXMAP==mType?" pixmap" :"window",
119 RenderSurface::~RenderSurface()
121 // release the display connection if we use our own
126 // NOTE, on 64bit desktop with some NVidia driver versions this crashes
128 XCloseDisplay( mMainDisplay );
134 Ecore_X_Window RenderSurface::GetXWindow()
139 XDisplay* RenderSurface::GetMainDisplay()
144 void RenderSurface::SetRenderNotification( TriggerEvent* renderNotification )
146 mRenderNotification = renderNotification;
149 Ecore_X_Drawable RenderSurface::GetDrawable()
154 Any RenderSurface::GetDisplay()
156 // this getter is used by main thread so we need to return the main thread version of the display
157 return Any( ecore_x_display_get() );
160 PositionSize RenderSurface::GetPositionSize() const
165 void RenderSurface::SetRenderMode(RenderMode mode)
170 Dali::RenderSurface::RenderMode RenderSurface::GetRenderMode() const
175 void RenderSurface::MoveResize( Dali::PositionSize positionSize )
177 // nothing to do in base class
180 void RenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical ) const
185 // 1 inch = 25.4 millimeters
186 xres = ecore_x_dpi_get();
187 yres = ecore_x_dpi_get();
189 dpiHorizontal = int(xres + 0.5f); // rounding
190 dpiVertical = int(yres + 0.5f);
193 void RenderSurface::Map()
197 void RenderSurface::TransferDisplayOwner( Internal::Adaptor::RenderSurface& newSurface )
199 // if we don't own the display return
200 if( mOwnDisplay == false )
205 RenderSurface* other = dynamic_cast< RenderSurface* >( &newSurface );
208 // if both surfaces share the same display, and this surface owns it,
209 // then transfer the ownership to the new surface
210 if( other->mMainDisplay == mMainDisplay )
213 other->mOwnDisplay = true;
218 void RenderSurface::ConsumeEvents()
220 // if the render surface has own display, check events so that we can flush the queue and avoid
221 // any potential memory leaks in X
224 // looping if events remain
228 // Check if there are any events in the queue
229 events = XEventsQueued( mMainDisplay, QueuedAfterFlush );
233 // Just flush event to prevent memory leak from event queue as the events get built up in
234 // memory but are only deleted when we retrieve them
236 XNextEvent( mMainDisplay, &ev );
243 void RenderSurface::StopRender()
245 // Stop blocking waiting for sync
246 SetSyncMode( RenderSurface::SYNC_MODE_NONE );
247 // Simulate a RenderSync in case render-thread is currently blocked
253 void RenderSurface::SetDisplay( Any display )
255 // the render surface can be passed either EFL e-core types, or x11 types
256 // we use boost any to determine at run time which type
258 if ( display.Empty() == false )
260 // check we have a valid type
261 DALI_ASSERT_ALWAYS( ( ( display.GetType() == typeid (Ecore_X_Display *)) ||
262 ( display.GetType() == typeid (XDisplay *) ) )
264 "Display type is invalid" );
268 // display may point to EcoreXDisplay so may need to cast
269 if( display.GetType() == typeid (Ecore_X_Display*) )
271 mMainDisplay = static_cast< XDisplay* >( AnyCast< Ecore_X_Display* >( display ) );
275 mMainDisplay = AnyCast< XDisplay* >( display );
281 // mMainDisplay = (XDisplay*)ecore_x_display_get();
282 // Because of DDK issue, we need to use separated x display instead of ecore default display
283 mMainDisplay = XOpenDisplay(0);
287 unsigned int RenderSurface::GetSurfaceId( Any surface ) const
289 unsigned int surfaceId = 0;
291 if ( surface.Empty() == false )
293 // check we have a valid type
294 DALI_ASSERT_ALWAYS( ( (surface.GetType() == typeid (XWindow) ) ||
295 (surface.GetType() == typeid (Ecore_X_Window) ) )
296 && "Surface type is invalid" );
298 if ( surface.GetType() == typeid (Ecore_X_Window) )
300 surfaceId = AnyCast<Ecore_X_Window>( surface );
304 surfaceId = AnyCast<XWindow>( surface );
310 void RenderSurface::RenderSync()
315 void RenderSurface::DoRenderSync( unsigned int timeDelta, SyncMode syncMode )
317 // Should block waiting for RenderSync?
318 if( mRenderMode == Dali::RenderSurface::RENDER_SYNC )
320 boost::unique_lock< boost::mutex > lock( mSyncMutex );
323 if( syncMode != SYNC_MODE_NONE &&
324 mSyncMode != SYNC_MODE_NONE &&
327 mSyncNotify.wait( lock );
329 mSyncReceived = false;
331 // Software sync based on a timed delay?
332 else if( mRenderMode > Dali::RenderSurface::RENDER_SYNC )
334 unsigned int syncPeriod( MICROSECONDS_PER_SECOND / static_cast< unsigned int >( mRenderMode ) - MILLISECONDS_PER_SECOND );
335 if( timeDelta < syncPeriod )
337 usleep( syncPeriod - timeDelta );
342 } // namespace ECoreX
344 } // namespace Adaptor
346 } // namespace Internal