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 "pixmap-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>
31 #include <dali/devel-api/common/mutex.h>
35 #include <ecore-x-types.h>
36 #include <trigger-event.h>
37 #include <gl/egl-implementation.h>
38 #include <base/display-connection.h>
39 #include <base/conditional-wait.h>
45 #if defined(DEBUG_ENABLED)
46 extern Debug::Filter* gRenderSurfaceLogFilter;
53 struct PixmapRenderSurface::Impl
55 Internal::Adaptor::ConditionalWait mSyncNotify; ///< condition to notify main thread that pixmap was flushed to onscreen
56 Dali::Mutex mSyncMutex; ///< mutex to lock during waiting sync
57 Ecore_X_Pixmap mX11Pixmap; ///< X-Pixmap
58 SyncMode mSyncMode; ///< Stores whether the post render should block waiting for compositor
59 bool mSyncReceived; ///< true, when a pixmap sync has occurred, (cleared after reading)
62 PixmapRenderSurface::PixmapRenderSurface(Dali::PositionSize positionSize,
64 const std::string& name,
66 : EcoreXRenderSurface( positionSize, surface, name, isTransparent ),
69 mImpl->mSyncMode = SYNC_MODE_NONE;
70 mImpl->mSyncReceived = false;
74 PixmapRenderSurface::~PixmapRenderSurface()
76 // release the surface if we own one
79 // if we did create the pixmap, delete the pixmap
80 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::General, "Own pixmap (%x) freed\n", mImpl->mX11Pixmap );
81 ecore_x_pixmap_free( mImpl->mX11Pixmap );
87 Ecore_X_Drawable PixmapRenderSurface::GetDrawable()
89 return (Ecore_X_Drawable) mImpl->mX11Pixmap;
92 Any PixmapRenderSurface::GetSurface()
94 return Any( mImpl->mX11Pixmap );
97 void PixmapRenderSurface::InitializeEgl( EglInterface& egl )
99 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
101 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
103 eglImpl.ChooseConfig(false, mColorDepth);
106 void PixmapRenderSurface::CreateEglSurface( EglInterface& egl )
108 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
110 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
112 // create the EGL surface
113 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
114 XPixmap pixmap = static_cast<XPixmap>( mImpl->mX11Pixmap );
115 eglImpl.CreateSurfacePixmap( (EGLNativePixmapType)pixmap, mColorDepth ); // reinterpret_cast does not compile
118 void PixmapRenderSurface::DestroyEglSurface( EglInterface& egl )
120 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
122 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
123 eglImpl.DestroySurface();
126 bool PixmapRenderSurface::ReplaceEGLSurface( EglInterface& egl )
128 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
130 // a new surface for the new pixmap
131 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
132 XPixmap pixmap = static_cast<XPixmap>( mImpl->mX11Pixmap );
133 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
135 return eglImpl.ReplaceSurfacePixmap( (EGLNativePixmapType)pixmap ); // reinterpret_cast does not compile
139 void PixmapRenderSurface::StartRender()
144 bool PixmapRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
146 // nothing to do for pixmaps
150 void PixmapRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
152 // flush gl instruction queue
153 glAbstraction.Flush();
155 mImpl->mSyncReceived = false;
157 // create damage for client applications which wish to know the update timing
158 if( mRenderNotification )
160 // use notification trigger
161 // Tell the event-thread to render the pixmap
162 mRenderNotification->Trigger();
166 // as a fallback, send damage event.
167 Ecore_X_Drawable drawable = GetDrawable();
172 XserverRegion region;
176 rect.width = mPosition.width;
177 rect.height = mPosition.height;
179 XDisplay* display = AnyCast<XDisplay*>(displayConnection->GetDisplay());
181 // make a fixes region as updated area
182 region = XFixesCreateRegion( display, &rect, 1 );
183 // add damage event to updated drawable
184 XDamageAdd( display, (Drawable)drawable, region );
185 XFixesDestroyRegion( display, region );
191 if( !replacingSurface && mImpl->mSyncMode != SYNC_MODE_NONE )
197 void PixmapRenderSurface::StopRender()
199 SetSyncMode(SYNC_MODE_NONE);
203 void PixmapRenderSurface::CreateXRenderable()
205 // check we're creating one with a valid size
206 DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "Pixmap size is invalid" );
209 mImpl->mX11Pixmap = ecore_x_pixmap_new(0, mPosition.width, mPosition.height, mColorDepth);
212 unsigned int foreground;
215 gc = ecore_x_gc_new( mImpl->mX11Pixmap,
216 ECORE_X_GC_VALUE_MASK_FOREGROUND,
219 DALI_ASSERT_ALWAYS( gc && "CreateXRenderable(): failed to get gc" );
221 ecore_x_drawable_rectangle_fill( mImpl->mX11Pixmap, gc, 0, 0, mPosition.width, mPosition.height );
223 DALI_ASSERT_ALWAYS( mImpl->mX11Pixmap && "Failed to create X pixmap" );
225 // we SHOULD guarantee the xpixmap/x11 window was created in x server.
231 void PixmapRenderSurface::UseExistingRenderable( unsigned int surfaceId )
233 mImpl->mX11Pixmap = static_cast< Ecore_X_Pixmap >( surfaceId );
236 void PixmapRenderSurface::SetSyncMode( SyncMode syncMode )
238 mImpl->mSyncMode = syncMode;
241 void PixmapRenderSurface::AcquireLock()
243 if( !mImpl->mSyncReceived )
245 mImpl->mSyncNotify.Wait();
247 mImpl->mSyncReceived = false;
250 void PixmapRenderSurface::ReleaseLock()
252 mImpl->mSyncReceived = true;
254 // wake render thread if it was waiting for the notify
255 mImpl->mSyncNotify.Notify();