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, Any surface, bool isTransparent )
56 : mPosition( positionSize ),
57 mRenderNotification( NULL ),
58 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
60 mProduceBufferIndex( INITIAL_PRODUCE_BUFFER_INDEX ),
61 mConsumeBufferIndex( INITIAL_CONSUME_BUFFER_INDEX ),
62 mThreadSynchronization(NULL)
64 for( int i = 0; i != BUFFER_COUNT; ++i )
70 Initialize( surface );
73 PixmapRenderSurfaceEcoreX::~PixmapRenderSurfaceEcoreX()
75 // release the surface if we own one
78 for (int i = 0; i < BUFFER_COUNT; ++i)
80 Ecore_X_Pixmap pixmap = mX11Pixmaps[i];
82 // if we did create the pixmap, delete the pixmap
83 DALI_LOG_INFO( gPixmapRenderSurfaceLogFilter, Debug::General, "Own pixmap (%x) freed\n", pixmap );
84 ecore_x_pixmap_free( pixmap );
89 void PixmapRenderSurfaceEcoreX::Initialize( Any surface )
91 // see if there is a surface in Any surface
92 unsigned int surfaceId = GetSurfaceId( surface );
94 // if the surface is empty, create a new one.
97 // we own the surface about to created
103 // XLib should already be initialized so no point in calling XInitThreads
104 UseExistingRenderable( surfaceId );
108 Any PixmapRenderSurfaceEcoreX::GetSurface()
110 Ecore_X_Pixmap pixmap = 0;
112 ConditionalWait::ScopedLock lock( mPixmapCondition );
113 pixmap = mX11Pixmaps[mProduceBufferIndex];
116 return Any( pixmap );
119 void PixmapRenderSurfaceEcoreX::SetRenderNotification(TriggerEventInterface* renderNotification)
121 mRenderNotification = renderNotification;
124 PositionSize PixmapRenderSurfaceEcoreX::GetPositionSize() const
129 void PixmapRenderSurfaceEcoreX::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
134 // 1 inch = 25.4 millimeters
135 xres = ecore_x_dpi_get();
136 yres = ecore_x_dpi_get();
138 dpiHorizontal = int( xres + 0.5f ); // rounding
139 dpiVertical = int( yres + 0.5f );
142 void PixmapRenderSurfaceEcoreX::InitializeEgl( EglInterface& egl )
144 DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
146 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
148 eglImpl.ChooseConfig(false, mColorDepth);
151 void PixmapRenderSurfaceEcoreX::CreateEglSurface( EglInterface& egl )
153 DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
155 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
157 for (int i = 0; i < BUFFER_COUNT; ++i)
159 // create the EGL surface
160 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
161 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
162 mEglSurfaces[i] = eglImpl.CreateSurfacePixmap( EGLNativePixmapType( pixmap ), mColorDepth ); // reinterpret_cast does not compile
166 void PixmapRenderSurfaceEcoreX::DestroyEglSurface( EglInterface& egl )
168 DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
170 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
172 for (int i = 0; i < BUFFER_COUNT; ++i)
174 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
175 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
176 eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[i] );
177 eglImpl.DestroySurface();
181 bool PixmapRenderSurfaceEcoreX::ReplaceEGLSurface( EglInterface& egl )
183 DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
185 bool contextLost = false;
187 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
189 for (int i = 0; i < BUFFER_COUNT; ++i)
191 // a new surface for the new pixmap
192 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
193 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
194 contextLost = eglImpl.ReplaceSurfacePixmap( EGLNativePixmapType( pixmap ), mEglSurfaces[i] ); // reinterpret_cast does not compile
197 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
198 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[mProduceBufferIndex] );
199 eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[mProduceBufferIndex] );
204 void PixmapRenderSurfaceEcoreX::StartRender()
208 bool PixmapRenderSurfaceEcoreX::PreRender( EglInterface& egl, Integration::GlAbstraction&, bool )
210 // Nothing to do for pixmaps
214 void PixmapRenderSurfaceEcoreX::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, Dali::DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
216 // flush gl instruction queue
217 glAbstraction.Flush();
219 if( mThreadSynchronization )
221 mThreadSynchronization->PostRenderStarted();
225 ConditionalWait::ScopedLock lock( mPixmapCondition );
226 mConsumeBufferIndex = __sync_fetch_and_xor( &mProduceBufferIndex, 1 ); // Swap buffer indexes.
228 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
230 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
231 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[mProduceBufferIndex] );
232 eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[mProduceBufferIndex] );
235 // create damage for client applications which wish to know the update timing
236 if( mRenderNotification )
238 // use notification trigger
239 // Tell the event-thread to render the pixmap
240 mRenderNotification->Trigger();
244 // as a fallback, send damage event.
245 Ecore_X_Drawable drawable = Ecore_X_Drawable( mX11Pixmaps[mProduceBufferIndex] );
250 XserverRegion region;
254 rect.width = mPosition.width;
255 rect.height = mPosition.height;
257 XDisplay* display = AnyCast<XDisplay*>(displayConnection->GetDisplay());
259 // make a fixes region as updated area
260 region = XFixesCreateRegion( display, &rect, 1 );
261 // add damage event to updated drawable
262 Drawable xdrawable( drawable ); // ecore type is unsigned int whereas in 64bit linux Drawable is long unsigned int
263 XDamageAdd( display, xdrawable, region );
264 XFixesDestroyRegion( display, region );
270 if( mThreadSynchronization )
272 mThreadSynchronization->PostRenderWaitForCompletion();
276 void PixmapRenderSurfaceEcoreX::StopRender()
281 void PixmapRenderSurfaceEcoreX::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
283 mThreadSynchronization = &threadSynchronization;
286 void PixmapRenderSurfaceEcoreX::ReleaseLock()
288 if( mThreadSynchronization )
290 mThreadSynchronization->PostRenderComplete();
294 RenderSurface::Type PixmapRenderSurfaceEcoreX::GetSurfaceType()
296 return RenderSurface::PIXMAP_RENDER_SURFACE;
299 void PixmapRenderSurfaceEcoreX::CreateRenderable()
301 // check we're creating one with a valid size
302 DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "Pixmap size is invalid" );
304 for (int i = 0; i < BUFFER_COUNT; ++i)
307 mX11Pixmaps[i] = ecore_x_pixmap_new(0, mPosition.width, mPosition.height, mColorDepth);
310 unsigned int foreground;
313 gc = ecore_x_gc_new( mX11Pixmaps[i],
314 ECORE_X_GC_VALUE_MASK_FOREGROUND,
317 DALI_ASSERT_ALWAYS( gc && "CreateRenderable(): failed to get gc" );
319 ecore_x_drawable_rectangle_fill( mX11Pixmaps[i], gc, 0, 0, mPosition.width, mPosition.height );
321 DALI_ASSERT_ALWAYS( mX11Pixmaps[i] && "Failed to create X pixmap" );
323 // we SHOULD guarantee the xpixmap/x11 window was created in x server.
330 void PixmapRenderSurfaceEcoreX::UseExistingRenderable( unsigned int surfaceId )
334 unsigned int PixmapRenderSurfaceEcoreX::GetSurfaceId( Any surface ) const
336 unsigned int surfaceId = 0;
338 if ( surface.Empty() == false )
340 // check we have a valid type
341 DALI_ASSERT_ALWAYS( ( (surface.GetType() == typeid (XWindow) ) ||
342 (surface.GetType() == typeid (Ecore_X_Window) ) )
343 && "Surface type is invalid" );
345 if ( surface.GetType() == typeid (Ecore_X_Window) )
347 surfaceId = AnyCast<Ecore_X_Window>( surface );
351 surfaceId = AnyCast<XWindow>( surface );
357 } // namespace Adaptor
359 } // namespace internal