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 <integration-api/thread-synchronization-interface.h>
36 #include <ecore-x-types.h>
37 #include <trigger-event.h>
38 #include <gl/egl-implementation.h>
39 #include <base/display-connection.h>
40 #include <base/conditional-wait.h>
45 #if defined(DEBUG_ENABLED)
46 extern Debug::Filter* gRenderSurfaceLogFilter;
52 struct PixmapRenderSurface::Impl
54 Ecore_X_Pixmap mX11Pixmap; ///< X-Pixmap
55 ThreadSynchronizationInterface* mThreadSynchronization; ///< A pointer to the thread-synchronization
58 PixmapRenderSurface::PixmapRenderSurface(Dali::PositionSize positionSize,
60 const std::string& name,
62 : EcoreXRenderSurface( positionSize, surface, name, isTransparent ),
65 mImpl->mThreadSynchronization = NULL;
69 PixmapRenderSurface::~PixmapRenderSurface()
71 // release the surface if we own one
74 // if we did create the pixmap, delete the pixmap
75 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::General, "Own pixmap (%x) freed\n", mImpl->mX11Pixmap );
76 ecore_x_pixmap_free( mImpl->mX11Pixmap );
82 Ecore_X_Drawable PixmapRenderSurface::GetDrawable()
84 return (Ecore_X_Drawable) mImpl->mX11Pixmap;
87 Any PixmapRenderSurface::GetSurface()
89 return Any( mImpl->mX11Pixmap );
92 void PixmapRenderSurface::InitializeEgl( EglInterface& egl )
94 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
96 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
98 eglImpl.ChooseConfig(false, mColorDepth);
101 void PixmapRenderSurface::CreateEglSurface( EglInterface& egl )
103 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
105 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
107 // create the EGL surface
108 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
109 XPixmap pixmap = static_cast<XPixmap>( mImpl->mX11Pixmap );
110 eglImpl.CreateSurfacePixmap( (EGLNativePixmapType)pixmap, mColorDepth ); // reinterpret_cast does not compile
113 void PixmapRenderSurface::DestroyEglSurface( EglInterface& egl )
115 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
117 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
118 eglImpl.DestroySurface();
121 bool PixmapRenderSurface::ReplaceEGLSurface( EglInterface& egl )
123 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
125 // a new surface for the new pixmap
126 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
127 XPixmap pixmap = static_cast<XPixmap>( mImpl->mX11Pixmap );
128 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
130 return eglImpl.ReplaceSurfacePixmap( (EGLNativePixmapType)pixmap ); // reinterpret_cast does not compile
134 void PixmapRenderSurface::StartRender()
138 bool PixmapRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
140 // nothing to do for pixmaps
144 void PixmapRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
146 // flush gl instruction queue
147 glAbstraction.Flush();
149 if( mImpl->mThreadSynchronization )
151 mImpl->mThreadSynchronization->PostRenderStarted();
154 // create damage for client applications which wish to know the update timing
155 if( mRenderNotification )
157 // use notification trigger
158 // Tell the event-thread to render the pixmap
159 mRenderNotification->Trigger();
163 // as a fallback, send damage event.
164 Ecore_X_Drawable drawable = GetDrawable();
169 XserverRegion region;
173 rect.width = mPosition.width;
174 rect.height = mPosition.height;
176 XDisplay* display = AnyCast<XDisplay*>(displayConnection->GetDisplay());
178 // make a fixes region as updated area
179 region = XFixesCreateRegion( display, &rect, 1 );
180 // add damage event to updated drawable
181 XDamageAdd( display, (Drawable)drawable, region );
182 XFixesDestroyRegion( display, region );
188 if( mImpl->mThreadSynchronization )
190 mImpl->mThreadSynchronization->PostRenderWaitForCompletion();
194 void PixmapRenderSurface::StopRender()
199 void PixmapRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
201 mImpl->mThreadSynchronization = &threadSynchronization;
204 void PixmapRenderSurface::CreateXRenderable()
206 // check we're creating one with a valid size
207 DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "Pixmap size is invalid" );
210 mImpl->mX11Pixmap = ecore_x_pixmap_new(0, mPosition.width, mPosition.height, mColorDepth);
213 unsigned int foreground;
216 gc = ecore_x_gc_new( mImpl->mX11Pixmap,
217 ECORE_X_GC_VALUE_MASK_FOREGROUND,
220 DALI_ASSERT_ALWAYS( gc && "CreateXRenderable(): failed to get gc" );
222 ecore_x_drawable_rectangle_fill( mImpl->mX11Pixmap, gc, 0, 0, mPosition.width, mPosition.height );
224 DALI_ASSERT_ALWAYS( mImpl->mX11Pixmap && "Failed to create X pixmap" );
226 // we SHOULD guarantee the xpixmap/x11 window was created in x server.
232 void PixmapRenderSurface::UseExistingRenderable( unsigned int surfaceId )
234 mImpl->mX11Pixmap = static_cast< Ecore_X_Pixmap >( surfaceId );
237 void PixmapRenderSurface::ReleaseLock()
239 if( mImpl->mThreadSynchronization )
241 mImpl->mThreadSynchronization->PostRenderComplete();