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 static bool gXInitThreadsCalled = false; ///< global to say whether XInitThreads has been called in this process
57 } // unnamed namespace
59 RenderSurface::RenderSurface( SurfaceType type,
60 Dali::PositionSize positionSize,
63 const std::string& name,
67 mPosition(positionSize),
69 mColorDepth(isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24),
70 mRenderNotification( NULL ),
74 // see if there is a display in Any display
75 SetDisplay( display );
78 void RenderSurface::Init( Any surface )
80 // see if there is a surface in Any surface
81 unsigned int surfaceId = GetSurfaceId( surface );
83 // if the surface is empty, create a new one.
86 // make sure XInitThreads is called
87 if ( !gXInitThreadsCalled )
90 gXInitThreadsCalled = true;
93 // we own the surface about to created
99 // XLib should already be initialized so no point in calling XInitThreads
100 UseExistingRenderable( surfaceId );
104 // prints out 'INFO: DALI: new RenderSurface, created display xx, used existing surface xx
105 // we can not use LOG_INFO because the surface can be created before Dali Core is created.
106 printf( "INFO: DALI: new RenderSurface, %s display %p, %s %s surface %X \n",
107 mOwnDisplay?"created":"used existing",mMainDisplay,
108 mOwnSurface?"created":"used existing",
109 Dali::RenderSurface::PIXMAP==mType?" pixmap" :"window",
114 RenderSurface::~RenderSurface()
116 // release the display connection if we use our own
121 // NOTE, on 64bit desktop with some NVidia driver versions this crashes
123 XCloseDisplay( mMainDisplay );
129 Ecore_X_Window RenderSurface::GetXWindow()
134 XDisplay* RenderSurface::GetMainDisplay()
139 void RenderSurface::SetRenderNotification( TriggerEvent* renderNotification )
141 mRenderNotification = renderNotification;
144 Ecore_X_Drawable RenderSurface::GetDrawable()
149 Any RenderSurface::GetDisplay()
151 // this getter is used by main thread so we need to return the main thread version of the display
152 return Any( ecore_x_display_get() );
155 PositionSize RenderSurface::GetPositionSize() const
160 void RenderSurface::MoveResize( Dali::PositionSize positionSize )
162 // nothing to do in base class
165 void RenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical ) const
170 // 1 inch = 25.4 millimeters
171 xres = ecore_x_dpi_get();
172 yres = ecore_x_dpi_get();
174 dpiHorizontal = int(xres + 0.5f); // rounding
175 dpiVertical = int(yres + 0.5f);
178 void RenderSurface::Map()
182 void RenderSurface::TransferDisplayOwner( Internal::Adaptor::RenderSurface& newSurface )
184 // if we don't own the display return
185 if( mOwnDisplay == false )
190 RenderSurface* other = dynamic_cast< RenderSurface* >( &newSurface );
193 // if both surfaces share the same display, and this surface owns it,
194 // then transfer the ownership to the new surface
195 if( other->mMainDisplay == mMainDisplay )
198 other->mOwnDisplay = true;
203 void RenderSurface::ConsumeEvents()
205 // if the render surface has own display, check events so that we can flush the queue and avoid
206 // any potential memory leaks in X
209 // looping if events remain
213 // Check if there are any events in the queue
214 events = XEventsQueued( mMainDisplay, QueuedAfterFlush );
218 // Just flush event to prevent memory leak from event queue as the events get built up in
219 // memory but are only deleted when we retrieve them
221 XNextEvent( mMainDisplay, &ev );
228 void RenderSurface::SetViewMode( ViewMode )
232 void RenderSurface::SetDisplay( Any display )
234 // the render surface can be passed either EFL e-core types, or x11 types
235 // we use boost any to determine at run time which type
237 if ( display.Empty() == false )
239 // check we have a valid type
240 DALI_ASSERT_ALWAYS( ( ( display.GetType() == typeid (Ecore_X_Display *)) ||
241 ( display.GetType() == typeid (XDisplay *) ) )
243 "Display type is invalid" );
247 // display may point to EcoreXDisplay so may need to cast
248 if( display.GetType() == typeid (Ecore_X_Display*) )
250 mMainDisplay = static_cast< XDisplay* >( AnyCast< Ecore_X_Display* >( display ) );
254 mMainDisplay = AnyCast< XDisplay* >( display );
260 // mMainDisplay = (XDisplay*)ecore_x_display_get();
261 // Because of DDK issue, we need to use separated x display instead of ecore default display
262 mMainDisplay = XOpenDisplay(0);
266 unsigned int RenderSurface::GetSurfaceId( Any surface ) const
268 unsigned int surfaceId = 0;
270 if ( surface.Empty() == false )
272 // check we have a valid type
273 DALI_ASSERT_ALWAYS( ( (surface.GetType() == typeid (XWindow) ) ||
274 (surface.GetType() == typeid (Ecore_X_Window) ) )
275 && "Surface type is invalid" );
277 if ( surface.GetType() == typeid (Ecore_X_Window) )
279 surfaceId = AnyCast<Ecore_X_Window>( surface );
283 surfaceId = AnyCast<XWindow>( surface );
291 } // namespace Adaptor
293 } // namespace Internal