Refactoring window system
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / ubuntu-x11 / pixmap-render-surface-ecore-x.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/window-system/ubuntu-x11/pixmap-render-surface-ecore-x.h>
20
21 // EXTERNAL INCLUDES
22 #include <X11/Xatom.h>
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25
26 #include <X11/extensions/Xfixes.h> // for damage notify
27 #include <X11/extensions/Xdamage.h> // for damage notify
28
29 #include <dali/integration-api/gl-abstraction.h>
30 #include <dali/integration-api/debug.h>
31 #include <dali/devel-api/threading/mutex.h>
32
33 // INTERNAL INCLUDES
34 #include <dali/integration-api/thread-synchronization-interface.h>
35 #include <dali/internal/system/common/trigger-event.h>
36 #include <dali/internal/window-system/common/display-connection.h>
37
38 namespace Dali
39 {
40 namespace Internal
41 {
42 namespace Adaptor
43 {
44
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gPixmapRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_PIXMAP_RENDER_SURFACE_ECORE_X");
47 #endif
48
49 namespace
50 {
51 static const int INITIAL_PRODUCE_BUFFER_INDEX = 0;
52 static const int INITIAL_CONSUME_BUFFER_INDEX = 1;
53 }
54
55 PixmapRenderSurfaceEcoreX::PixmapRenderSurfaceEcoreX(Dali::PositionSize positionSize,
56                                          Any surface,
57                                          const std::string& name,
58                                          bool isTransparent)
59 : mPosition( positionSize ),
60   mTitle( name ),
61   mRenderNotification( NULL ),
62   mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
63   mOwnSurface( false ),
64   mProduceBufferIndex( INITIAL_PRODUCE_BUFFER_INDEX ),
65   mConsumeBufferIndex( INITIAL_CONSUME_BUFFER_INDEX ),
66   mThreadSynchronization(NULL)
67 {
68   for( int i = 0; i != BUFFER_COUNT; ++i )
69   {
70     mX11Pixmaps[i] = 0;
71     mEglSurfaces[i] = 0;
72   }
73
74   Initialize( surface );
75 }
76
77 PixmapRenderSurfaceEcoreX::~PixmapRenderSurfaceEcoreX()
78 {
79   // release the surface if we own one
80   if( mOwnSurface )
81   {
82     for (int i = 0; i < BUFFER_COUNT; ++i)
83     {
84       Ecore_X_Pixmap pixmap = mX11Pixmaps[i];
85
86       // if we did create the pixmap, delete the pixmap
87       DALI_LOG_INFO( gPixmapRenderSurfaceLogFilter, Debug::General, "Own pixmap (%x) freed\n", pixmap );
88       ecore_x_pixmap_free( pixmap );
89     }
90   }
91 }
92
93 void PixmapRenderSurfaceEcoreX::Initialize( Any surface )
94 {
95   // see if there is a surface in Any surface
96   unsigned int surfaceId  = GetSurfaceId( surface );
97
98   // if the surface is empty, create a new one.
99   if ( surfaceId == 0 )
100   {
101     // we own the surface about to created
102     mOwnSurface = true;
103     CreateRenderable();
104   }
105   else
106   {
107     // XLib should already be initialized so no point in calling XInitThreads
108     UseExistingRenderable( surfaceId );
109   }
110 }
111
112 Any PixmapRenderSurfaceEcoreX::GetSurface()
113 {
114   Ecore_X_Pixmap pixmap = 0;
115   {
116     ConditionalWait::ScopedLock lock( mPixmapCondition );
117     pixmap = mX11Pixmaps[mProduceBufferIndex];
118   }
119
120   return Any( pixmap );
121 }
122
123 void PixmapRenderSurfaceEcoreX::SetRenderNotification(TriggerEventInterface* renderNotification)
124 {
125   mRenderNotification = renderNotification;
126 }
127
128 PositionSize PixmapRenderSurfaceEcoreX::GetPositionSize() const
129 {
130   return mPosition;
131 }
132
133 void PixmapRenderSurfaceEcoreX::InitializeEgl( EglInterface& egl )
134 {
135   DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
136
137   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
138
139   eglImpl.ChooseConfig(false, mColorDepth);
140 }
141
142 void PixmapRenderSurfaceEcoreX::CreateEglSurface( EglInterface& egl )
143 {
144   DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
145
146   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
147
148   for (int i = 0; i < BUFFER_COUNT; ++i)
149   {
150     // create the EGL surface
151     // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
152     XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
153     mEglSurfaces[i] = eglImpl.CreateSurfacePixmap( EGLNativePixmapType( pixmap ), mColorDepth ); // reinterpret_cast does not compile
154   }
155 }
156
157 void PixmapRenderSurfaceEcoreX::DestroyEglSurface( EglInterface& egl )
158 {
159   DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
160
161   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
162
163   for (int i = 0; i < BUFFER_COUNT; ++i)
164   {
165     // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
166     XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
167     eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[i] );
168     eglImpl.DestroySurface();
169   }
170 }
171
172 bool PixmapRenderSurfaceEcoreX::ReplaceEGLSurface( EglInterface& egl )
173 {
174   DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
175
176   bool contextLost = false;
177
178   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
179
180   for (int i = 0; i < BUFFER_COUNT; ++i)
181   {
182     // a new surface for the new pixmap
183     // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
184     XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
185     contextLost = eglImpl.ReplaceSurfacePixmap( EGLNativePixmapType( pixmap ), mEglSurfaces[i] ); // reinterpret_cast does not compile
186   }
187
188   // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
189   XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[mProduceBufferIndex] );
190   eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[mProduceBufferIndex] );
191
192   return contextLost;
193 }
194
195 void PixmapRenderSurfaceEcoreX::StartRender()
196 {
197 }
198
199 bool PixmapRenderSurfaceEcoreX::PreRender( EglInterface& egl, Integration::GlAbstraction&, bool )
200 {
201   // Nothing to do for pixmaps
202   return true;
203 }
204
205 void PixmapRenderSurfaceEcoreX::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, Dali::DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
206 {
207   // flush gl instruction queue
208   glAbstraction.Flush();
209
210   if( mThreadSynchronization )
211   {
212     mThreadSynchronization->PostRenderStarted();
213   }
214
215   {
216     ConditionalWait::ScopedLock lock( mPixmapCondition );
217     mConsumeBufferIndex = __sync_fetch_and_xor( &mProduceBufferIndex, 1 ); // Swap buffer indexes.
218
219     Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
220
221     // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
222     XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[mProduceBufferIndex] );
223     eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[mProduceBufferIndex] );
224   }
225
226   // create damage for client applications which wish to know the update timing
227   if( mRenderNotification )
228   {
229     // use notification trigger
230     // Tell the event-thread to render the pixmap
231     mRenderNotification->Trigger();
232   }
233   else
234   {
235     // as a fallback, send damage event.
236     Ecore_X_Drawable drawable = Ecore_X_Drawable( mX11Pixmaps[mProduceBufferIndex] );
237
238     if( drawable )
239     {
240       XRectangle rect;
241       XserverRegion region;
242
243       rect.x = 0;
244       rect.y = 0;
245       rect.width = mPosition.width;
246       rect.height = mPosition.height;
247
248       XDisplay* display = AnyCast<XDisplay*>(displayConnection->GetDisplay());
249
250       // make a fixes region as updated area
251       region = XFixesCreateRegion( display, &rect, 1 );
252       // add damage event to updated drawable
253       Drawable xdrawable( drawable ); // ecore type is unsigned int whereas in 64bit linux Drawable is long unsigned int
254       XDamageAdd( display, xdrawable, region );
255       XFixesDestroyRegion( display, region );
256
257       XFlush( display );
258     }
259   }
260
261   if( mThreadSynchronization )
262   {
263     mThreadSynchronization->PostRenderWaitForCompletion();
264   }
265 }
266
267 void PixmapRenderSurfaceEcoreX::StopRender()
268 {
269   ReleaseLock();
270 }
271
272 void PixmapRenderSurfaceEcoreX::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
273 {
274   mThreadSynchronization = &threadSynchronization;
275 }
276
277 void PixmapRenderSurfaceEcoreX::ReleaseLock()
278 {
279   if( mThreadSynchronization )
280   {
281     mThreadSynchronization->PostRenderComplete();
282   }
283 }
284
285 RenderSurface::Type PixmapRenderSurfaceEcoreX::GetSurfaceType()
286 {
287   return RenderSurface::PIXMAP_RENDER_SURFACE;
288 }
289
290 void PixmapRenderSurfaceEcoreX::CreateRenderable()
291 {
292   // check we're creating one with a valid size
293   DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "Pixmap size is invalid" );
294
295   for (int i = 0; i < BUFFER_COUNT; ++i)
296   {
297     // create the pixmap
298     mX11Pixmaps[i] = ecore_x_pixmap_new(0, mPosition.width, mPosition.height, mColorDepth);
299
300     // clear the pixmap
301     unsigned int foreground;
302     Ecore_X_GC gc;
303     foreground = 0;
304     gc = ecore_x_gc_new( mX11Pixmaps[i],
305                          ECORE_X_GC_VALUE_MASK_FOREGROUND,
306                          &foreground );
307
308     DALI_ASSERT_ALWAYS( gc && "CreateRenderable(): failed to get gc" );
309
310     ecore_x_drawable_rectangle_fill( mX11Pixmaps[i], gc, 0, 0, mPosition.width, mPosition.height );
311
312     DALI_ASSERT_ALWAYS( mX11Pixmaps[i] && "Failed to create X pixmap" );
313
314     // we SHOULD guarantee the xpixmap/x11 window was created in x server.
315     ecore_x_sync();
316
317     ecore_x_gc_free(gc);
318   }
319 }
320
321 void PixmapRenderSurfaceEcoreX::UseExistingRenderable( unsigned int surfaceId )
322 {
323 }
324
325 unsigned int PixmapRenderSurfaceEcoreX::GetSurfaceId( Any surface ) const
326 {
327   unsigned int surfaceId = 0;
328
329   if ( surface.Empty() == false )
330   {
331     // check we have a valid type
332     DALI_ASSERT_ALWAYS( ( (surface.GetType() == typeid (XWindow) ) ||
333                           (surface.GetType() == typeid (Ecore_X_Window) ) )
334                         && "Surface type is invalid" );
335
336     if ( surface.GetType() == typeid (Ecore_X_Window) )
337     {
338       surfaceId = AnyCast<Ecore_X_Window>( surface );
339     }
340     else
341     {
342       surfaceId = AnyCast<XWindow>( surface );
343     }
344   }
345   return surfaceId;
346 }
347
348 } // namespace Adaptor
349
350 } // namespace internal
351
352 } // namespace Dali