2 * Copyright (c) 2019 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 <dali/internal/window-system/ubuntu-x11/pixmap-render-surface-ecore-x.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>
31 #include <dali/devel-api/threading/mutex.h>
34 #include <dali/integration-api/thread-synchronization-interface.h>
35 #include <dali/internal/system/common/trigger-event.h>
36 #include <dali/internal/window-system/common/display-connection.h>
37 #include <dali/internal/graphics/gles/egl-graphics.h>
38 #include <dali/internal/adaptor/common/adaptor-impl.h>
39 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter* gPixmapRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_PIXMAP_RENDER_SURFACE_ECORE_X");
55 static const int INITIAL_PRODUCE_BUFFER_INDEX = 0;
56 static const int INITIAL_CONSUME_BUFFER_INDEX = 1;
59 PixmapRenderSurfaceEcoreX::PixmapRenderSurfaceEcoreX( Dali::PositionSize positionSize, Any surface, bool isTransparent )
60 : mGraphics( nullptr ),
61 mDisplayConnection( nullptr ),
62 mPosition( positionSize ),
63 mRenderNotification( NULL ),
64 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
66 mProduceBufferIndex( INITIAL_PRODUCE_BUFFER_INDEX ),
67 mConsumeBufferIndex( INITIAL_CONSUME_BUFFER_INDEX ),
70 mThreadSynchronization( nullptr ),
73 for( int i = 0; i != BUFFER_COUNT; ++i )
79 Initialize( surface );
82 PixmapRenderSurfaceEcoreX::~PixmapRenderSurfaceEcoreX()
86 // release the surface if we own one
89 for (int i = 0; i < BUFFER_COUNT; ++i)
91 Ecore_X_Pixmap pixmap = mX11Pixmaps[i];
93 // if we did create the pixmap, delete the pixmap
94 DALI_LOG_INFO( gPixmapRenderSurfaceLogFilter, Debug::General, "Own pixmap (%x) freed\n", pixmap );
95 ecore_x_pixmap_free( pixmap );
100 void PixmapRenderSurfaceEcoreX::Initialize( Any surface )
102 // see if there is a surface in Any surface
103 unsigned int surfaceId = GetSurfaceId( surface );
105 // if the surface is empty, create a new one.
106 if ( surfaceId == 0 )
108 // we own the surface about to created
114 // XLib should already be initialized so no point in calling XInitThreads
115 UseExistingRenderable( surfaceId );
119 Any PixmapRenderSurfaceEcoreX::GetSurface()
121 Ecore_X_Pixmap pixmap = 0;
123 ConditionalWait::ScopedLock lock( mPixmapCondition );
124 pixmap = mX11Pixmaps[mProduceBufferIndex];
127 return Any( pixmap );
130 void PixmapRenderSurfaceEcoreX::SetRenderNotification(TriggerEventInterface* renderNotification)
132 mRenderNotification = renderNotification;
135 PositionSize PixmapRenderSurfaceEcoreX::GetPositionSize() const
140 void PixmapRenderSurfaceEcoreX::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
145 // 1 inch = 25.4 millimeters
146 xres = ecore_x_dpi_get();
147 yres = ecore_x_dpi_get();
149 dpiHorizontal = int( xres + 0.5f ); // rounding
150 dpiVertical = int( yres + 0.5f );
153 int PixmapRenderSurfaceEcoreX::GetOrientation() const
158 void PixmapRenderSurfaceEcoreX::InitializeGraphics()
160 mGraphics = &mAdaptor->GetGraphicsInterface();
161 mDisplayConnection = &mAdaptor->GetDisplayConnectionInterface();
164 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
165 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
166 eglImpl.ChooseConfig(false, mColorDepth);
169 void PixmapRenderSurfaceEcoreX::CreateSurface()
171 DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
173 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
174 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
176 for (int i = 0; i < BUFFER_COUNT; ++i)
178 // create the EGL surface
179 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
180 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
181 mEglSurfaces[i] = eglImpl.CreateSurfacePixmap( EGLNativePixmapType( pixmap ), mColorDepth ); // reinterpret_cast does not compile
185 void PixmapRenderSurfaceEcoreX::DestroySurface()
187 DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
189 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
191 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
193 for (int i = 0; i < BUFFER_COUNT; ++i)
195 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
196 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
197 eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[i] );
198 eglImpl.DestroySurface( mEglSurfaces[i] );
202 bool PixmapRenderSurfaceEcoreX::ReplaceGraphicsSurface()
204 DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
206 bool contextLost = false;
208 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
210 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
212 for (int i = 0; i < BUFFER_COUNT; ++i)
214 // a new surface for the new pixmap
215 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
216 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
217 contextLost = eglImpl.ReplaceSurfacePixmap( EGLNativePixmapType( pixmap ), mEglSurfaces[i] ); // reinterpret_cast does not compile
220 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
221 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[mProduceBufferIndex] );
222 eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[mProduceBufferIndex] );
227 void PixmapRenderSurfaceEcoreX::StartRender()
231 bool PixmapRenderSurfaceEcoreX::PreRender( bool )
233 // Nothing to do for pixmaps
237 Rect<int32_t> PixmapRenderSurfaceEcoreX::SetDamagedRect( Rect<int32_t> damagedRect )
242 int PixmapRenderSurfaceEcoreX::GetBufferAge()
247 void PixmapRenderSurfaceEcoreX::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
249 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
251 // flush gl instruction queue
252 Integration::GlAbstraction& glAbstraction = eglGraphics->GetGlAbstraction();
253 glAbstraction.Flush();
255 if( mThreadSynchronization )
257 mThreadSynchronization->PostRenderStarted();
261 ConditionalWait::ScopedLock lock( mPixmapCondition );
262 mConsumeBufferIndex = __sync_fetch_and_xor( &mProduceBufferIndex, 1 ); // Swap buffer indexes.
264 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
266 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
267 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[mProduceBufferIndex] );
268 eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[mProduceBufferIndex] );
271 // create damage for client applications which wish to know the update timing
272 if( mRenderNotification )
274 // use notification trigger
275 // Tell the event-thread to render the pixmap
276 mRenderNotification->Trigger();
280 // as a fallback, send damage event.
281 Ecore_X_Drawable drawable = Ecore_X_Drawable( mX11Pixmaps[mProduceBufferIndex] );
286 XserverRegion region;
290 rect.width = mPosition.width;
291 rect.height = mPosition.height;
293 XDisplay* display = AnyCast<XDisplay*>(mDisplayConnection->GetDisplay());
295 // make a fixes region as updated area
296 region = XFixesCreateRegion( display, &rect, 1 );
297 // add damage event to updated drawable
298 Drawable xdrawable( drawable ); // ecore type is unsigned int whereas in 64bit linux Drawable is long unsigned int
299 XDamageAdd( display, xdrawable, region );
300 XFixesDestroyRegion( display, region );
306 if( mThreadSynchronization )
308 mThreadSynchronization->PostRenderWaitForCompletion();
312 void PixmapRenderSurfaceEcoreX::StopRender()
317 void PixmapRenderSurfaceEcoreX::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
319 mThreadSynchronization = &threadSynchronization;
322 void PixmapRenderSurfaceEcoreX::ReleaseLock()
324 if( mThreadSynchronization )
326 mThreadSynchronization->PostRenderComplete();
330 Integration::RenderSurface::Type PixmapRenderSurfaceEcoreX::GetSurfaceType()
332 return Integration::RenderSurface::PIXMAP_RENDER_SURFACE;
335 void PixmapRenderSurfaceEcoreX::MakeContextCurrent()
339 void PixmapRenderSurfaceEcoreX::CreateRenderable()
341 // check we're creating one with a valid size
342 DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "Pixmap size is invalid" );
344 for (int i = 0; i < BUFFER_COUNT; ++i)
347 mX11Pixmaps[i] = ecore_x_pixmap_new(0, mPosition.width, mPosition.height, mColorDepth);
350 unsigned int foreground;
353 gc = ecore_x_gc_new( mX11Pixmaps[i],
354 ECORE_X_GC_VALUE_MASK_FOREGROUND,
357 DALI_ASSERT_ALWAYS( gc && "CreateRenderable(): failed to get gc" );
359 ecore_x_drawable_rectangle_fill( mX11Pixmaps[i], gc, 0, 0, mPosition.width, mPosition.height );
361 DALI_ASSERT_ALWAYS( mX11Pixmaps[i] && "Failed to create X pixmap" );
363 // we SHOULD guarantee the xpixmap/x11 window was created in x server.
370 void PixmapRenderSurfaceEcoreX::UseExistingRenderable( unsigned int surfaceId )
374 unsigned int PixmapRenderSurfaceEcoreX::GetSurfaceId( Any surface ) const
376 unsigned int surfaceId = 0;
378 if ( surface.Empty() == false )
380 // check we have a valid type
381 DALI_ASSERT_ALWAYS( ( (surface.GetType() == typeid (XWindow) ) ||
382 (surface.GetType() == typeid (Ecore_X_Window) ) )
383 && "Surface type is invalid" );
385 if ( surface.GetType() == typeid (Ecore_X_Window) )
387 surfaceId = AnyCast<Ecore_X_Window>( surface );
391 surfaceId = AnyCast<XWindow>( surface );
397 } // namespace Adaptor
399 } // namespace internal