2 * Copyright (c) 2017 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>
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gPixmapRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_PIXMAP_RENDER_SURFACE_ECORE_X");
51 static const int INITIAL_PRODUCE_BUFFER_INDEX = 0;
52 static const int INITIAL_CONSUME_BUFFER_INDEX = 1;
55 PixmapRenderSurfaceEcoreX::PixmapRenderSurfaceEcoreX(Dali::PositionSize positionSize,
57 const std::string& name,
59 : mPosition( positionSize ),
61 mRenderNotification( NULL ),
62 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
64 mProduceBufferIndex( INITIAL_PRODUCE_BUFFER_INDEX ),
65 mConsumeBufferIndex( INITIAL_CONSUME_BUFFER_INDEX ),
66 mThreadSynchronization(NULL)
68 for( int i = 0; i != BUFFER_COUNT; ++i )
74 Initialize( surface );
77 PixmapRenderSurfaceEcoreX::~PixmapRenderSurfaceEcoreX()
79 // release the surface if we own one
82 for (int i = 0; i < BUFFER_COUNT; ++i)
84 Ecore_X_Pixmap pixmap = mX11Pixmaps[i];
86 // if we did create the pixmap, delete the pixmap
87 DALI_LOG_INFO( gPixmapRenderSurfaceLogFilter, Debug::General, "Own pixmap (%x) freed\n", pixmap );
88 ecore_x_pixmap_free( pixmap );
93 void PixmapRenderSurfaceEcoreX::Initialize( Any surface )
95 // see if there is a surface in Any surface
96 unsigned int surfaceId = GetSurfaceId( surface );
98 // if the surface is empty, create a new one.
101 // we own the surface about to created
107 // XLib should already be initialized so no point in calling XInitThreads
108 UseExistingRenderable( surfaceId );
112 Any PixmapRenderSurfaceEcoreX::GetSurface()
114 Ecore_X_Pixmap pixmap = 0;
116 ConditionalWait::ScopedLock lock( mPixmapCondition );
117 pixmap = mX11Pixmaps[mProduceBufferIndex];
120 return Any( pixmap );
123 void PixmapRenderSurfaceEcoreX::SetRenderNotification(TriggerEventInterface* renderNotification)
125 mRenderNotification = renderNotification;
128 PositionSize PixmapRenderSurfaceEcoreX::GetPositionSize() const
133 void PixmapRenderSurfaceEcoreX::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
138 // 1 inch = 25.4 millimeters
139 xres = ecore_x_dpi_get();
140 yres = ecore_x_dpi_get();
142 dpiHorizontal = int( xres + 0.5f ); // rounding
143 dpiVertical = int( yres + 0.5f );
146 void PixmapRenderSurfaceEcoreX::InitializeEgl( EglInterface& egl )
148 DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
150 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
152 eglImpl.ChooseConfig(false, mColorDepth);
155 void PixmapRenderSurfaceEcoreX::CreateEglSurface( EglInterface& egl )
157 DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
159 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
161 for (int i = 0; i < BUFFER_COUNT; ++i)
163 // create the EGL surface
164 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
165 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
166 mEglSurfaces[i] = eglImpl.CreateSurfacePixmap( EGLNativePixmapType( pixmap ), mColorDepth ); // reinterpret_cast does not compile
170 void PixmapRenderSurfaceEcoreX::DestroyEglSurface( EglInterface& egl )
172 DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
174 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
176 for (int i = 0; i < BUFFER_COUNT; ++i)
178 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
179 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
180 eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[i] );
181 eglImpl.DestroySurface();
185 bool PixmapRenderSurfaceEcoreX::ReplaceEGLSurface( EglInterface& egl )
187 DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
189 bool contextLost = false;
191 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
193 for (int i = 0; i < BUFFER_COUNT; ++i)
195 // a new surface for the new pixmap
196 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
197 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
198 contextLost = eglImpl.ReplaceSurfacePixmap( EGLNativePixmapType( pixmap ), mEglSurfaces[i] ); // reinterpret_cast does not compile
201 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
202 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[mProduceBufferIndex] );
203 eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[mProduceBufferIndex] );
208 void PixmapRenderSurfaceEcoreX::StartRender()
212 bool PixmapRenderSurfaceEcoreX::PreRender( EglInterface& egl, Integration::GlAbstraction&, bool )
214 // Nothing to do for pixmaps
218 void PixmapRenderSurfaceEcoreX::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, Dali::DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
220 // flush gl instruction queue
221 glAbstraction.Flush();
223 if( mThreadSynchronization )
225 mThreadSynchronization->PostRenderStarted();
229 ConditionalWait::ScopedLock lock( mPixmapCondition );
230 mConsumeBufferIndex = __sync_fetch_and_xor( &mProduceBufferIndex, 1 ); // Swap buffer indexes.
232 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
234 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
235 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[mProduceBufferIndex] );
236 eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[mProduceBufferIndex] );
239 // create damage for client applications which wish to know the update timing
240 if( mRenderNotification )
242 // use notification trigger
243 // Tell the event-thread to render the pixmap
244 mRenderNotification->Trigger();
248 // as a fallback, send damage event.
249 Ecore_X_Drawable drawable = Ecore_X_Drawable( mX11Pixmaps[mProduceBufferIndex] );
254 XserverRegion region;
258 rect.width = mPosition.width;
259 rect.height = mPosition.height;
261 XDisplay* display = AnyCast<XDisplay*>(displayConnection->GetDisplay());
263 // make a fixes region as updated area
264 region = XFixesCreateRegion( display, &rect, 1 );
265 // add damage event to updated drawable
266 Drawable xdrawable( drawable ); // ecore type is unsigned int whereas in 64bit linux Drawable is long unsigned int
267 XDamageAdd( display, xdrawable, region );
268 XFixesDestroyRegion( display, region );
274 if( mThreadSynchronization )
276 mThreadSynchronization->PostRenderWaitForCompletion();
280 void PixmapRenderSurfaceEcoreX::StopRender()
285 void PixmapRenderSurfaceEcoreX::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
287 mThreadSynchronization = &threadSynchronization;
290 void PixmapRenderSurfaceEcoreX::ReleaseLock()
292 if( mThreadSynchronization )
294 mThreadSynchronization->PostRenderComplete();
298 RenderSurface::Type PixmapRenderSurfaceEcoreX::GetSurfaceType()
300 return RenderSurface::PIXMAP_RENDER_SURFACE;
303 void PixmapRenderSurfaceEcoreX::CreateRenderable()
305 // check we're creating one with a valid size
306 DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "Pixmap size is invalid" );
308 for (int i = 0; i < BUFFER_COUNT; ++i)
311 mX11Pixmaps[i] = ecore_x_pixmap_new(0, mPosition.width, mPosition.height, mColorDepth);
314 unsigned int foreground;
317 gc = ecore_x_gc_new( mX11Pixmaps[i],
318 ECORE_X_GC_VALUE_MASK_FOREGROUND,
321 DALI_ASSERT_ALWAYS( gc && "CreateRenderable(): failed to get gc" );
323 ecore_x_drawable_rectangle_fill( mX11Pixmaps[i], gc, 0, 0, mPosition.width, mPosition.height );
325 DALI_ASSERT_ALWAYS( mX11Pixmaps[i] && "Failed to create X pixmap" );
327 // we SHOULD guarantee the xpixmap/x11 window was created in x server.
334 void PixmapRenderSurfaceEcoreX::UseExistingRenderable( unsigned int surfaceId )
338 unsigned int PixmapRenderSurfaceEcoreX::GetSurfaceId( Any surface ) const
340 unsigned int surfaceId = 0;
342 if ( surface.Empty() == false )
344 // check we have a valid type
345 DALI_ASSERT_ALWAYS( ( (surface.GetType() == typeid (XWindow) ) ||
346 (surface.GetType() == typeid (Ecore_X_Window) ) )
347 && "Surface type is invalid" );
349 if ( surface.GetType() == typeid (Ecore_X_Window) )
351 surfaceId = AnyCast<Ecore_X_Window>( surface );
355 surfaceId = AnyCast<XWindow>( surface );
361 } // namespace Adaptor
363 } // namespace internal