2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "ecore-x-render-surface.h"
22 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
26 #include <X11/extensions/Xfixes.h> // for damage notify
27 #include <X11/extensions/Xdamage.h> // for damage notify
29 #include <dali/integration-api/gl-abstraction.h>
30 #include <dali/integration-api/debug.h>
33 #include <ecore-x-types.h>
34 #include <trigger-event.h>
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_ECORE_X_RENDER_SURFACE");
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;
60 } // unnamed namespace
62 RenderSurface::RenderSurface( SurfaceType type,
63 Dali::PositionSize positionSize,
66 const std::string& name,
70 mPosition(positionSize),
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 ),
81 // see if there is a display in Any display
82 SetDisplay( display );
85 void RenderSurface::Init( Any surface )
87 // see if there is a surface in Any surface
88 unsigned int surfaceId = GetSurfaceId( surface );
90 // if the surface is empty, create a new one.
93 // make sure XInitThreads is called
94 if ( !gXInitThreadsCalled )
97 gXInitThreadsCalled = true;
100 // we own the surface about to created
106 // XLib should already be initialized so no point in calling XInitThreads
107 UseExistingRenderable( surfaceId );
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",
121 RenderSurface::~RenderSurface()
123 // release the display connection if we use our own
128 // NOTE, on 64bit desktop with some NVidia driver versions this crashes
130 XCloseDisplay( mMainDisplay );
136 Ecore_X_Window RenderSurface::GetXWindow()
141 XDisplay* RenderSurface::GetMainDisplay()
146 void RenderSurface::SetRenderNotification( TriggerEvent* renderNotification )
148 mRenderNotification = renderNotification;
151 Ecore_X_Drawable RenderSurface::GetDrawable()
156 Any RenderSurface::GetDisplay()
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() );
162 PositionSize RenderSurface::GetPositionSize() const
167 void RenderSurface::SetRenderMode(RenderMode mode)
172 Dali::RenderSurface::RenderMode RenderSurface::GetRenderMode() const
177 void RenderSurface::MoveResize( Dali::PositionSize positionSize )
179 // nothing to do in base class
182 void RenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical ) const
187 // 1 inch = 25.4 millimeters
188 xres = ecore_x_dpi_get();
189 yres = ecore_x_dpi_get();
191 dpiHorizontal = int(xres + 0.5f); // rounding
192 dpiVertical = int(yres + 0.5f);
195 void RenderSurface::Map()
199 void RenderSurface::TransferDisplayOwner( Internal::Adaptor::RenderSurface& newSurface )
201 // if we don't own the display return
202 if( mOwnDisplay == false )
207 RenderSurface* other = dynamic_cast< RenderSurface* >( &newSurface );
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 )
215 other->mOwnDisplay = true;
220 void RenderSurface::ConsumeEvents()
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
226 // looping if events remain
230 // Check if there are any events in the queue
231 events = XEventsQueued( mMainDisplay, QueuedAfterFlush );
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
238 XNextEvent( mMainDisplay, &ev );
245 void RenderSurface::StopRender()
247 // Stop blocking waiting for sync
248 SetSyncMode( RenderSurface::SYNC_MODE_NONE );
249 // Simulate a RenderSync in case render-thread is currently blocked
255 void RenderSurface::SetViewMode( ViewMode )
259 void RenderSurface::SetDisplay( Any display )
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
264 if ( display.Empty() == false )
266 // check we have a valid type
267 DALI_ASSERT_ALWAYS( ( ( display.GetType() == typeid (Ecore_X_Display *)) ||
268 ( display.GetType() == typeid (XDisplay *) ) )
270 "Display type is invalid" );
274 // display may point to EcoreXDisplay so may need to cast
275 if( display.GetType() == typeid (Ecore_X_Display*) )
277 mMainDisplay = static_cast< XDisplay* >( AnyCast< Ecore_X_Display* >( display ) );
281 mMainDisplay = AnyCast< XDisplay* >( display );
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);
293 unsigned int RenderSurface::GetSurfaceId( Any surface ) const
295 unsigned int surfaceId = 0;
297 if ( surface.Empty() == false )
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" );
304 if ( surface.GetType() == typeid (Ecore_X_Window) )
306 surfaceId = AnyCast<Ecore_X_Window>( surface );
310 surfaceId = AnyCast<XWindow>( surface );
316 void RenderSurface::RenderSync()
321 void RenderSurface::DoRenderSync( unsigned int timeDelta, SyncMode syncMode )
323 // Should block waiting for RenderSync?
324 if( mRenderMode == Dali::RenderSurface::RENDER_SYNC )
326 boost::unique_lock< boost::mutex > lock( mSyncMutex );
329 if( syncMode != SYNC_MODE_NONE &&
330 mSyncMode != SYNC_MODE_NONE &&
333 mSyncNotify.wait( lock );
335 mSyncReceived = false;
337 // Software sync based on a timed delay?
338 else if( mRenderMode > Dali::RenderSurface::RENDER_SYNC )
340 unsigned int syncPeriod( MICROSECONDS_PER_SECOND / static_cast< unsigned int >( mRenderMode ) - MILLISECONDS_PER_SECOND );
342 if( timeDelta < syncPeriod )
344 usleep( syncPeriod - timeDelta );
351 } // namespace Adaptor
353 } // namespace Internal