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/threading/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>
44 #if defined(DEBUG_ENABLED)
45 extern Debug::Filter* gRenderSurfaceLogFilter;
51 struct PixmapRenderSurface::Impl
53 Ecore_X_Pixmap mX11Pixmap; ///< X-Pixmap
54 ThreadSynchronizationInterface* mThreadSynchronization; ///< A pointer to the thread-synchronization
57 PixmapRenderSurface::PixmapRenderSurface(Dali::PositionSize positionSize,
59 const std::string& name,
61 : EcoreXRenderSurface( positionSize, surface, name, isTransparent ),
64 mImpl->mThreadSynchronization = NULL;
68 PixmapRenderSurface::~PixmapRenderSurface()
70 // release the surface if we own one
73 // if we did create the pixmap, delete the pixmap
74 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::General, "Own pixmap (%x) freed\n", mImpl->mX11Pixmap );
75 ecore_x_pixmap_free( mImpl->mX11Pixmap );
81 Ecore_X_Drawable PixmapRenderSurface::GetDrawable()
83 return (Ecore_X_Drawable) mImpl->mX11Pixmap;
86 Any PixmapRenderSurface::GetSurface()
88 return Any( mImpl->mX11Pixmap );
91 void PixmapRenderSurface::InitializeEgl( EglInterface& egl )
93 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
95 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
97 eglImpl.ChooseConfig(false, mColorDepth);
100 void PixmapRenderSurface::CreateEglSurface( EglInterface& egl )
102 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
104 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
106 // create the EGL surface
107 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
108 XPixmap pixmap = static_cast<XPixmap>( mImpl->mX11Pixmap );
109 eglImpl.CreateSurfacePixmap( (EGLNativePixmapType)pixmap, mColorDepth ); // reinterpret_cast does not compile
112 void PixmapRenderSurface::DestroyEglSurface( EglInterface& egl )
114 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
116 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
117 eglImpl.DestroySurface();
120 bool PixmapRenderSurface::ReplaceEGLSurface( EglInterface& egl )
122 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
124 // a new surface for the new pixmap
125 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
126 XPixmap pixmap = static_cast<XPixmap>( mImpl->mX11Pixmap );
127 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
129 return eglImpl.ReplaceSurfacePixmap( (EGLNativePixmapType)pixmap ); // reinterpret_cast does not compile
133 void PixmapRenderSurface::StartRender()
137 bool PixmapRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
139 // nothing to do for pixmaps
143 void PixmapRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
145 // flush gl instruction queue
146 glAbstraction.Flush();
148 if( mImpl->mThreadSynchronization )
150 mImpl->mThreadSynchronization->PostRenderStarted();
153 // create damage for client applications which wish to know the update timing
154 if( mRenderNotification )
156 // use notification trigger
157 // Tell the event-thread to render the pixmap
158 mRenderNotification->Trigger();
162 // as a fallback, send damage event.
163 Ecore_X_Drawable drawable = GetDrawable();
168 XserverRegion region;
172 rect.width = mPosition.width;
173 rect.height = mPosition.height;
175 XDisplay* display = AnyCast<XDisplay*>(displayConnection->GetDisplay());
177 // make a fixes region as updated area
178 region = XFixesCreateRegion( display, &rect, 1 );
179 // add damage event to updated drawable
180 XDamageAdd( display, (Drawable)drawable, region );
181 XFixesDestroyRegion( display, region );
187 if( mImpl->mThreadSynchronization )
189 mImpl->mThreadSynchronization->PostRenderWaitForCompletion();
193 void PixmapRenderSurface::StopRender()
198 void PixmapRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
200 mImpl->mThreadSynchronization = &threadSynchronization;
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::ReleaseLock()
238 if( mImpl->mThreadSynchronization )
240 mImpl->mThreadSynchronization->PostRenderComplete();