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>
33 #include <ecore-x-types.h>
34 #include <trigger-event.h>
45 #if defined(DEBUG_ENABLED)
46 extern Debug::Filter* gRenderSurfaceLogFilter;
52 PixmapRenderSurface::PixmapRenderSurface( Dali::PositionSize positionSize,
55 const std::string& name,
57 : RenderSurface( Dali::RenderSurface::PIXMAP, positionSize, surface, display, name, isTransparent ),
58 mSyncMode(SYNC_MODE_NONE),
64 PixmapRenderSurface::~PixmapRenderSurface()
66 // release the surface if we own one
69 // if we did create the pixmap, delete the pixmap
70 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::General, "Own pixmap (%x) freed\n", mX11Pixmap );
71 ecore_x_pixmap_free( mX11Pixmap );
75 Ecore_X_Drawable PixmapRenderSurface::GetDrawable()
77 return (Ecore_X_Drawable)mX11Pixmap;
80 Dali::RenderSurface::SurfaceType PixmapRenderSurface::GetType()
82 return Dali::RenderSurface::PIXMAP;
85 Any PixmapRenderSurface::GetSurface()
87 return Any( mX11Pixmap );
90 void PixmapRenderSurface::InitializeEgl( EglInterface& eglIf )
92 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
94 EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
95 eglImpl.InitializeGles( reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ) );
97 eglImpl.ChooseConfig(false, mColorDepth);
100 void PixmapRenderSurface::CreateEglSurface( EglInterface& eglIf )
102 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
104 EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
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>( mX11Pixmap );
109 eglImpl.CreateSurfacePixmap( (EGLNativePixmapType)pixmap, mColorDepth ); // reinterpret_cast does not compile
112 void PixmapRenderSurface::DestroyEglSurface( EglInterface& eglIf )
114 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
116 EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
117 eglImpl.DestroySurface();
120 bool PixmapRenderSurface::ReplaceEGLSurface( EglInterface& eglIf )
122 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
124 EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
125 eglImpl.InitializeGles( reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ) );
127 // a new surface for the new pixmap
128 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
129 XPixmap pixmap = static_cast< XPixmap>( mX11Pixmap );
130 return eglImpl.ReplaceSurfacePixmap( (EGLNativePixmapType)pixmap, // reinterpret_cast does not compile
131 reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ) );
134 void PixmapRenderSurface::StartRender()
136 mSyncMode = SYNC_MODE_WAIT;
139 bool PixmapRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
141 // nothing to do for pixmaps
145 void PixmapRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, unsigned int timeDelta, bool replacingSurface )
147 // flush gl instruction queue
148 glAbstraction.Flush();
150 // create damage for client applications which wish to know the update timing
151 if( mRenderNotification )
153 // use notification trigger
154 // Tell the event-thread to render the pixmap
155 mRenderNotification->Trigger();
159 // as a fallback, send damage event. This is needed until livebox is fixed to
160 // stop using damage events for render
161 Ecore_X_Drawable drawable = GetDrawable();
166 XserverRegion region;
170 rect.width = mPosition.width;
171 rect.height = mPosition.height;
173 // make a fixes region as updated area
174 region = XFixesCreateRegion( mMainDisplay, &rect, 1 );
175 // add damage event to updated drawable
176 XDamageAdd( mMainDisplay, (Drawable)drawable, region );
177 XFixesDestroyRegion( mMainDisplay, region );
179 XFlush( mMainDisplay );
183 AcquireLock( replacingSurface ? SYNC_MODE_NONE : SYNC_MODE_WAIT );
186 void PixmapRenderSurface::StopRender()
188 SetSyncMode(SYNC_MODE_NONE);
192 void PixmapRenderSurface::CreateXRenderable()
194 // check we're creating one with a valid size
195 DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "Pixmap size is invalid" );
198 mX11Pixmap = ecore_x_pixmap_new(0, mPosition.width, mPosition.height, mColorDepth);
201 unsigned int foreground;
204 gc = ecore_x_gc_new( mX11Pixmap,
205 ECORE_X_GC_VALUE_MASK_FOREGROUND,
207 ecore_x_drawable_rectangle_fill( mX11Pixmap, gc, 0, 0, mPosition.width, mPosition.height );
209 DALI_ASSERT_ALWAYS( mX11Pixmap && "Failed to create X pixmap" );
211 // we SHOULD guarantee the xpixmap/x11 window was created in x server.
217 void PixmapRenderSurface::UseExistingRenderable( unsigned int surfaceId )
219 mX11Pixmap = static_cast< Ecore_X_Pixmap >( surfaceId );
222 void PixmapRenderSurface::SetSyncMode( SyncMode syncMode )
224 mSyncMode = syncMode;
227 void PixmapRenderSurface::AcquireLock( SyncMode syncMode )
229 boost::unique_lock< boost::mutex > lock( mSyncMutex );
232 if( syncMode != SYNC_MODE_NONE &&
233 mSyncMode != SYNC_MODE_NONE &&
236 mSyncNotify.wait( lock );
238 mSyncReceived = false;
241 void PixmapRenderSurface::ReleaseLock()
244 boost::unique_lock< boost::mutex > lock( mSyncMutex );
245 mSyncReceived = true;
248 // wake render thread if it was waiting for the notify
249 mSyncNotify.notify_all();
255 } // namespace Adaptor
257 } // namespace Internal