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>
34 #include <ecore-x-types.h>
35 #include <trigger-event.h>
36 #include <gl/egl-implementation.h>
37 #include <base/display-connection.h>
42 #if defined(DEBUG_ENABLED)
43 extern Debug::Filter* gRenderSurfaceLogFilter;
49 PixmapRenderSurface::PixmapRenderSurface(Dali::PositionSize positionSize,
51 const std::string& name,
53 : EcoreXRenderSurface( positionSize, surface, name, isTransparent ),
54 mSyncMode(SYNC_MODE_NONE),
60 PixmapRenderSurface::~PixmapRenderSurface()
62 // release the surface if we own one
65 // if we did create the pixmap, delete the pixmap
66 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::General, "Own pixmap (%x) freed\n", mX11Pixmap );
67 ecore_x_pixmap_free( mX11Pixmap );
71 Ecore_X_Drawable PixmapRenderSurface::GetDrawable()
73 return (Ecore_X_Drawable) mX11Pixmap;
76 Any PixmapRenderSurface::GetSurface()
78 return Any( mX11Pixmap );
81 void PixmapRenderSurface::InitializeEgl( EglInterface& egl )
83 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
85 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
87 eglImpl.ChooseConfig(false, mColorDepth);
90 void PixmapRenderSurface::CreateEglSurface( EglInterface& egl )
92 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
94 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
96 // create the EGL surface
97 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
98 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmap );
99 eglImpl.CreateSurfacePixmap( (EGLNativePixmapType)pixmap, mColorDepth ); // reinterpret_cast does not compile
102 void PixmapRenderSurface::DestroyEglSurface( EglInterface& egl )
104 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
106 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
107 eglImpl.DestroySurface();
110 bool PixmapRenderSurface::ReplaceEGLSurface( EglInterface& egl )
112 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
114 // a new surface for the new pixmap
115 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
116 XPixmap pixmap = static_cast<XPixmap>( mX11Pixmap );
117 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
119 return eglImpl.ReplaceSurfacePixmap( (EGLNativePixmapType)pixmap ); // reinterpret_cast does not compile
123 void PixmapRenderSurface::StartRender()
125 mSyncMode = SYNC_MODE_WAIT;
128 bool PixmapRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
130 // nothing to do for pixmaps
134 void PixmapRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, unsigned int deltaTime, bool replacingSurface )
136 // flush gl instruction queue
137 glAbstraction.Flush();
139 // create damage for client applications which wish to know the update timing
140 if( mRenderNotification )
142 // use notification trigger
143 // Tell the event-thread to render the pixmap
144 mRenderNotification->Trigger();
148 // as a fallback, send damage event.
149 Ecore_X_Drawable drawable = GetDrawable();
154 XserverRegion region;
158 rect.width = mPosition.width;
159 rect.height = mPosition.height;
161 XDisplay* display = AnyCast<XDisplay*>(displayConnection->GetDisplay());
163 // make a fixes region as updated area
164 region = XFixesCreateRegion( display, &rect, 1 );
165 // add damage event to updated drawable
166 XDamageAdd( display, (Drawable)drawable, region );
167 XFixesDestroyRegion( display, region );
173 AcquireLock( replacingSurface ? SYNC_MODE_NONE : SYNC_MODE_WAIT );
176 void PixmapRenderSurface::StopRender()
178 SetSyncMode(SYNC_MODE_NONE);
182 void PixmapRenderSurface::CreateXRenderable()
184 // check we're creating one with a valid size
185 DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "Pixmap size is invalid" );
188 mX11Pixmap = ecore_x_pixmap_new(0, mPosition.width, mPosition.height, mColorDepth);
191 unsigned int foreground;
194 gc = ecore_x_gc_new( mX11Pixmap,
195 ECORE_X_GC_VALUE_MASK_FOREGROUND,
197 ecore_x_drawable_rectangle_fill( mX11Pixmap, gc, 0, 0, mPosition.width, mPosition.height );
199 DALI_ASSERT_ALWAYS( mX11Pixmap && "Failed to create X pixmap" );
201 // we SHOULD guarantee the xpixmap/x11 window was created in x server.
207 void PixmapRenderSurface::UseExistingRenderable( unsigned int surfaceId )
209 mX11Pixmap = static_cast< Ecore_X_Pixmap >( surfaceId );
212 void PixmapRenderSurface::SetSyncMode( SyncMode syncMode )
214 mSyncMode = syncMode;
217 void PixmapRenderSurface::AcquireLock( SyncMode syncMode )
219 boost::unique_lock< boost::mutex > lock( mSyncMutex );
222 if( syncMode != SYNC_MODE_NONE &&
223 mSyncMode != SYNC_MODE_NONE &&
226 mSyncNotify.wait( lock );
228 mSyncReceived = false;
231 void PixmapRenderSurface::ReleaseLock()
234 boost::unique_lock< boost::mutex > lock( mSyncMutex );
235 mSyncReceived = true;
238 // wake render thread if it was waiting for the notify
239 mSyncNotify.notify_all();