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 mRenderNotification( NULL ),
77 // see if there is a display in Any display
78 SetDisplay( display );
81 void RenderSurface::Init( Any surface )
83 // see if there is a surface in Any surface
84 unsigned int surfaceId = GetSurfaceId( surface );
86 // if the surface is empty, create a new one.
89 // make sure XInitThreads is called
90 if ( !gXInitThreadsCalled )
93 gXInitThreadsCalled = true;
96 // we own the surface about to created
102 // XLib should already be initialized so no point in calling XInitThreads
103 UseExistingRenderable( surfaceId );
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",
117 RenderSurface::~RenderSurface()
119 // release the display connection if we use our own
124 // NOTE, on 64bit desktop with some NVidia driver versions this crashes
126 XCloseDisplay( mMainDisplay );
132 Ecore_X_Window RenderSurface::GetXWindow()
137 XDisplay* RenderSurface::GetMainDisplay()
142 void RenderSurface::SetRenderNotification( TriggerEvent* renderNotification )
144 mRenderNotification = renderNotification;
147 Ecore_X_Drawable RenderSurface::GetDrawable()
152 Any RenderSurface::GetDisplay()
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() );
158 PositionSize RenderSurface::GetPositionSize() const
163 void RenderSurface::MoveResize( Dali::PositionSize positionSize )
165 // nothing to do in base class
168 void RenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical ) const
173 // 1 inch = 25.4 millimeters
174 xres = ecore_x_dpi_get();
175 yres = ecore_x_dpi_get();
177 dpiHorizontal = int(xres + 0.5f); // rounding
178 dpiVertical = int(yres + 0.5f);
181 void RenderSurface::Map()
185 void RenderSurface::TransferDisplayOwner( Internal::Adaptor::RenderSurface& newSurface )
187 // if we don't own the display return
188 if( mOwnDisplay == false )
193 RenderSurface* other = dynamic_cast< RenderSurface* >( &newSurface );
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 )
201 other->mOwnDisplay = true;
206 void RenderSurface::ConsumeEvents()
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
212 // looping if events remain
216 // Check if there are any events in the queue
217 events = XEventsQueued( mMainDisplay, QueuedAfterFlush );
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
224 XNextEvent( mMainDisplay, &ev );
231 void RenderSurface::SetViewMode( ViewMode )
235 void RenderSurface::SetDisplay( Any display )
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
240 if ( display.Empty() == false )
242 // check we have a valid type
243 DALI_ASSERT_ALWAYS( ( ( display.GetType() == typeid (Ecore_X_Display *)) ||
244 ( display.GetType() == typeid (XDisplay *) ) )
246 "Display type is invalid" );
250 // display may point to EcoreXDisplay so may need to cast
251 if( display.GetType() == typeid (Ecore_X_Display*) )
253 mMainDisplay = static_cast< XDisplay* >( AnyCast< Ecore_X_Display* >( display ) );
257 mMainDisplay = AnyCast< XDisplay* >( display );
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);
269 unsigned int RenderSurface::GetSurfaceId( Any surface ) const
271 unsigned int surfaceId = 0;
273 if ( surface.Empty() == false )
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" );
280 if ( surface.GetType() == typeid (Ecore_X_Window) )
282 surfaceId = AnyCast<Ecore_X_Window>( surface );
286 surfaceId = AnyCast<XWindow>( surface );
294 } // namespace Adaptor
296 } // namespace Internal