(Ubuntu) Temporarily disable freeing of pixmap as it crashes on EFL 1.24.0
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / ubuntu-x11 / native-image-source-impl-x.cpp
1 /*
2  * Copyright (c) 2020 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/imaging/ubuntu-x11/native-image-source-impl-x.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/internal/system/linux/dali-ecore-x.h>
23 #include <X11/Xutil.h>
24 #include <X11/Xlib.h>
25 #include <dali/integration-api/debug.h>
26
27 // INTERNAL INCLUDES
28 #include <dali/internal/graphics/common/egl-image-extensions.h>
29 #include <dali/internal/graphics/gles/egl-graphics.h>
30 #include <dali/internal/adaptor/common/adaptor-impl.h>
31 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
32
33 namespace Dali
34 {
35
36 namespace Internal
37 {
38
39 namespace Adaptor
40 {
41 using Dali::Integration::PixelBuffer;
42
43 // Pieces needed to save compressed images (temporary location while plumbing):
44 namespace
45 {
46
47   /**
48    * Free an allocated XImage on destruction.
49    */
50   struct XImageJanitor
51   {
52     XImageJanitor( XImage* const  pXImage ) : mXImage( pXImage )
53     {
54       DALI_ASSERT_DEBUG(pXImage != 0 && "Null pointer to XImage.");
55     }
56
57     ~XImageJanitor()
58     {
59       if( mXImage )
60       {
61         if( !XDestroyImage(mXImage) )
62         {
63           DALI_LOG_ERROR("XImage deallocation failure");
64         }
65       }
66     }
67     XImage* const  mXImage;
68   private:
69     XImageJanitor( const XImageJanitor& rhs );
70     XImageJanitor& operator = ( const XImageJanitor& rhs );
71   };
72 }
73
74 NativeImageSourceX* NativeImageSourceX::New( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
75 {
76   NativeImageSourceX* image = new NativeImageSourceX( width, height, depth, nativeImageSource );
77   DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
78
79   // 2nd phase construction
80   if(image) //< Defensive in case we ever compile without exceptions.
81   {
82     image->Initialize();
83   }
84
85   return image;
86 }
87
88 NativeImageSourceX::NativeImageSourceX( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
89 : mWidth( width ),
90   mHeight( height ),
91   mOwnPixmap( true ),
92   mPixmap( 0 ),
93   mBlendingRequired( false ),
94   mColorDepth( depth ),
95   mEglImageKHR( NULL ),
96   mEglImageExtensions( NULL )
97 {
98   DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
99
100   GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
101   auto eglGraphics = static_cast<EglGraphics *>(graphics);
102
103   mEglImageExtensions = eglGraphics->GetImageExtensions();
104
105   DALI_ASSERT_DEBUG( mEglImageExtensions );
106
107   // assign the pixmap
108   mPixmap = GetPixmapFromAny(nativeImageSource);
109 }
110
111 void NativeImageSourceX::Initialize()
112 {
113   // if pixmap has been created outside of X11 Image we can return
114   if (mPixmap)
115   {
116     // we don't own the pixmap
117     mOwnPixmap = false;
118
119     // find out the pixmap width / height and color depth
120     GetPixmapDetails();
121     return;
122   }
123
124   // get the pixel depth
125   int depth = GetPixelDepth(mColorDepth);
126
127   // set whether blending is required according to pixel format based on the depth
128   /* default pixel format is RGB888
129      If depth = 8, Pixel::A8;
130      If depth = 16, Pixel::RGB565;
131      If depth = 32, Pixel::RGBA8888 */
132   mBlendingRequired = ( depth == 32 || depth == 8 );
133
134   mPixmap = ecore_x_pixmap_new( 0, mWidth, mHeight, depth );
135   ecore_x_sync();
136 }
137
138 NativeImageSourceX::~NativeImageSourceX()
139 {
140   if (mOwnPixmap && mPixmap)
141   {
142     // Temporarily disable this as this causes a crash with EFL Version 1.24.0
143     //ecore_x_pixmap_free(mPixmap);
144   }
145 }
146
147 Any NativeImageSourceX::GetNativeImageSource() const
148 {
149   // return ecore x11 type
150   return Any(mPixmap);
151 }
152
153 bool NativeImageSourceX::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
154 {
155   DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
156   bool success = false;
157   width  = mWidth;
158   height = mHeight;
159
160   // Open a display connection
161   Display* displayConnection = XOpenDisplay( 0 );
162
163   XImageJanitor xImageJanitor( XGetImage( displayConnection,
164                                           mPixmap,
165                                           0, 0, // x,y of subregion to extract.
166                                           width, height, // of subregion to extract.
167                                           0xFFFFFFFF,
168                                           ZPixmap ) );
169   XImage* const  pXImage = xImageJanitor.mXImage;
170   DALI_ASSERT_DEBUG(pXImage && "XImage (from pixmap) could not be retrieved from the server");
171   if(!pXImage)
172   {
173     DALI_LOG_ERROR("Could not retrieve Ximage.\n");
174   }
175   else
176   {
177     switch(pXImage->depth)
178     {
179       // Note, depth is a logical value. On target the framebuffer is still 32bpp
180       // (see pXImage->bits_per_pixel) so we go through XGetPixel() and swizzle.
181       // Note, this could be the default, fallback case for all depths if *pXImage
182       // didn't have blank RGB masks (X bug), but we have to hardcode the masks and
183       // shifts instead.
184       case 24:
185       {
186         pixelFormat = Pixel::RGB888;
187         pixbuf.resize(width*height*3);
188         unsigned char* bufPtr = &pixbuf[0];
189
190         for(unsigned y = height-1; y < height; --y)
191         {
192           for(unsigned x = 0; x < width; ++x, bufPtr+=3)
193           {
194             const unsigned pixel = XGetPixel(pXImage,x,y);
195
196             // store as RGB
197             const unsigned blue  =  pixel & 0xFFU;
198             const unsigned green = (pixel >> 8)  & 0xFFU;
199             const unsigned red   = (pixel >> 16) & 0xFFU;
200
201             *bufPtr = red;
202             *(bufPtr+1) = green;
203             *(bufPtr+2) = blue;
204           }
205         }
206         success = true;
207         break;
208       }
209       case 32:
210       {
211         if(pXImage->data)
212         {
213           // Sweep through the image, doing a vertical flip, but handling each scanline as
214           // an inlined intrinsic/builtin memcpy (should be fast):
215           pixbuf.resize(width*height*4);
216           unsigned * bufPtr = reinterpret_cast<unsigned *>(&pixbuf[0]);
217           const unsigned xDataLineSkip = pXImage->bytes_per_line;
218           const size_t copy_count = static_cast< size_t >( width ) * 4;
219           pixelFormat = Pixel::BGRA8888;
220
221           for(unsigned y = height-1; y < height; --y, bufPtr += width)
222           {
223             const char * const in = pXImage->data + xDataLineSkip * y;
224
225             // Copy a whole scanline at a time:
226             DALI_ASSERT_DEBUG( size_t( bufPtr ) >= size_t( &pixbuf[0] ));
227             DALI_ASSERT_DEBUG( reinterpret_cast<size_t>( bufPtr ) + copy_count <= reinterpret_cast<size_t>( &pixbuf[pixbuf.size()] ) );
228             DALI_ASSERT_DEBUG( in >= pXImage->data );
229             DALI_ASSERT_DEBUG( in + copy_count <= pXImage->data + xDataLineSkip * height );
230             __builtin_memcpy( bufPtr, in, copy_count );
231           }
232           success = true;
233         }
234         else
235         {
236           DALI_LOG_ERROR("XImage has null data pointer.\n");
237         }
238         break;
239       }
240       // Make a case for 16 bit modes especially to remember that the only reason we don't support them is a bug in X:
241       case 16:
242       {
243         DALI_ASSERT_DEBUG(pXImage->red_mask && pXImage->green_mask && pXImage->blue_mask && "No image masks mean 16 bit modes are not possible.");
244         ///! If the above assert doesn't fail in a debug build, the X bug may have been fixed, so revisit this function.
245         ///! No break, fall through to the general unsupported format warning below.
246       }
247       default:
248       {
249         DALI_LOG_WARNING("Pixmap has unsupported bit-depth for getting pixels: %u\n", pXImage->depth);
250       }
251     }
252   }
253   if(!success)
254   {
255     DALI_LOG_ERROR("Failed to get pixels from NativeImageSource.\n");
256     pixbuf.resize(0);
257     width = 0;
258     height = 0;
259   }
260
261   // Close the display connection
262   XCloseDisplay( displayConnection );
263
264   return success;
265 }
266
267 void NativeImageSourceX::SetSource( Any source )
268 {
269   mPixmap = GetPixmapFromAny( source );
270
271   if (mPixmap)
272   {
273     // we don't own the pixmap
274     mOwnPixmap = false;
275
276     // find out the pixmap width / height and color depth
277     GetPixmapDetails();
278   }
279 }
280
281 bool NativeImageSourceX::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
282 {
283   return true;
284 }
285
286 bool NativeImageSourceX::CreateResource()
287 {
288   // if the image existed previously delete it.
289   if (mEglImageKHR != NULL)
290   {
291     DestroyResource();
292   }
293
294   // casting from an unsigned int to a void *, which should then be cast back
295   // to an unsigned int in the driver.
296   EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer > (mPixmap);
297
298   mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
299
300   return mEglImageKHR != NULL;
301 }
302
303 void NativeImageSourceX::DestroyResource()
304 {
305   mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
306
307   mEglImageKHR = NULL;
308 }
309
310 uint32_t NativeImageSourceX::TargetTexture()
311 {
312   mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
313
314   return 0;
315 }
316
317 void NativeImageSourceX::PrepareTexture()
318 {
319 }
320
321 int NativeImageSourceX::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
322 {
323   switch (depth)
324   {
325     case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
326     {
327       // Get the default screen depth
328       return ecore_x_default_depth_get(ecore_x_display_get(), ecore_x_default_screen_get());
329     }
330     case Dali::NativeImageSource::COLOR_DEPTH_8:
331     {
332       return 8;
333     }
334     case Dali::NativeImageSource::COLOR_DEPTH_16:
335     {
336       return 16;
337     }
338     case Dali::NativeImageSource::COLOR_DEPTH_24:
339     {
340       return 24;
341     }
342     case Dali::NativeImageSource::COLOR_DEPTH_32:
343     {
344       return 32;
345     }
346     default:
347     {
348       DALI_ASSERT_DEBUG(0 && "unknown color enum");
349       return 0;
350     }
351   }
352 }
353
354 int NativeImageSourceX::GetTextureTarget() const
355 {
356   return GL_TEXTURE_2D;
357 }
358
359 const char* NativeImageSourceX::GetCustomFragmentPrefix() const
360 {
361   return nullptr;
362 }
363
364 const char* NativeImageSourceX::GetCustomSamplerTypename() const
365 {
366   return nullptr;
367 }
368
369 Any NativeImageSourceX::GetNativeImageHandle() const
370 {
371   return Any(mPixmap);
372 }
373
374 bool NativeImageSourceX::SourceChanged() const
375 {
376   return false;
377 }
378
379 Ecore_X_Pixmap NativeImageSourceX::GetPixmapFromAny(Any pixmap) const
380 {
381   if (pixmap.Empty())
382   {
383     return 0;
384   }
385
386   // see if it is of type x11 pixmap
387   if (pixmap.GetType() == typeid (Pixmap))
388   {
389     // get the x pixmap type
390     Pixmap xpixmap = AnyCast<Pixmap>(pixmap);
391
392     // cast it to a ecore pixmap type
393     return static_cast<Ecore_X_Pixmap>(xpixmap);
394   }
395   else
396   {
397     return AnyCast<Ecore_X_Pixmap>(pixmap);
398   }
399 }
400
401 void NativeImageSourceX::GetPixmapDetails()
402 {
403   int x, y;
404
405   // get the width, height and depth
406   ecore_x_pixmap_geometry_get( mPixmap, &x, &y, reinterpret_cast< int* >( &mWidth ), reinterpret_cast< int* >( &mHeight ) );
407
408   // set whether blending is required according to pixel format based on the depth
409   /* default pixel format is RGB888
410      If depth = 8, Pixel::A8;
411      If depth = 16, Pixel::RGB565;
412      If depth = 32, Pixel::RGBA8888 */
413   int depth = ecore_x_pixmap_depth_get(mPixmap);
414   mBlendingRequired = ( depth == 32 || depth == 8 );
415 }
416
417 uint8_t* NativeImageSourceX::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
418 {
419   return NULL;
420 }
421
422
423 bool NativeImageSourceX::ReleaseBuffer()
424 {
425   return false;
426 }
427
428 } // namespace Adaptor
429
430 } // namespace internal
431
432 } // namespace Dali