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-image-impl.h"
23 #include <X11/Xutil.h>
24 #include <dali/integration-api/debug.h>
25 #include <render-surface.h>
28 #include <gl/egl-image-extensions.h>
29 #include <gl/egl-factory.h>
30 #include <adaptor-impl.h>
31 #include <bitmap-saver.h>
41 using Dali::Integration::PixelBuffer;
43 // Pieces needed to save compressed images (temporary location while plumbing):
48 * Free an allocated XImage on destruction.
52 XImageJanitor( XImage* const pXImage ) : mXImage( pXImage )
54 DALI_ASSERT_DEBUG(pXImage != 0 && "Null pointer to XImage.");
61 if( !XDestroyImage(mXImage) )
63 DALI_ASSERT_DEBUG("XImage deallocation failure");
67 XImage* const mXImage;
69 XImageJanitor( const XImageJanitor& rhs );
70 XImageJanitor& operator = ( const XImageJanitor& rhs );
74 PixmapImage* PixmapImage::New(unsigned int width, unsigned int height, Dali::PixmapImage::ColorDepth depth, Dali::Adaptor& adaptor, Any pixmap )
76 PixmapImage* image = new PixmapImage( width, height, depth, adaptor, pixmap );
77 DALI_ASSERT_DEBUG( image && "PixmapImage allocation failed." );
79 // 2nd phase construction
80 if(image) //< Defensive in case we ever compile without exceptions.
88 PixmapImage::PixmapImage(unsigned int width, unsigned int height, Dali::PixmapImage::ColorDepth depth, Dali::Adaptor& adaptor, Any pixmap)
94 mPixelFormat(Pixel::RGB888),
96 mAdaptor(Internal::Adaptor::Adaptor::GetImplementation(adaptor)),
100 mPixmap = GetPixmapFromAny(pixmap);
103 void PixmapImage::Initialize()
105 // Get render-surface being used by Dali
106 Dali::RenderSurface& surface = mAdaptor.GetSurface();
108 // get the X11 display pointer and store it
109 // it is used by eglCreateImageKHR, and XFreePixmap
110 // Any other display (x-connection) will fail in eglCreateImageKHR
111 Any display = surface.GetDisplay();
113 // the dali display pointer is needed
114 mDisplay = AnyCast<Ecore_X_Display*>(display);
116 // if pixmap has been created outside of X11 Image we can return
119 // we don't own the pixmap
122 // find out the pixmap width / height and color depth
127 // get the pixel depth
128 int depth = GetPixelDepth(mColorDepth);
130 // set the pixel format
131 SetPixelFormat(depth);
133 // Get the X-Renderable for which the pixmap is created on
134 Any renderableSurface = surface.GetSurface();
136 // if Dali is using a Pixmap or Window to render to it doesn't matter because they have the same
137 // underlying type of unsigned long
138 Ecore_X_Window daliWindow = AnyCast< Ecore_X_Window >(renderableSurface);
140 mPixmap = ecore_x_pixmap_new(daliWindow, mWidth, mHeight, depth);
144 PixmapImage::~PixmapImage()
146 // Lost the opportunity to call GlExtensionDestroy() if Adaptor is destroyed first
147 if( Adaptor::IsAvailable() )
149 // GlExtensionDestroy() called from GLCleanup on the render thread. Checking this is done here.
150 // (mEglImageKHR is now read/written from different threads although ref counted destruction
151 // should mean this isnt concurrent)
152 DALI_ASSERT_ALWAYS( NULL == mEglImageKHR && "NativeImage GL resources have not been properly cleaned up" );
155 if (mOwnPixmap && mPixmap)
157 ecore_x_pixmap_free(mPixmap);
161 Any PixmapImage::GetPixmap(Dali::PixmapImage::PixmapAPI api) const
163 if (api == Dali::PixmapImage::ECORE_X11)
165 // return ecore x11 type
170 // return x11 type after casting it
171 Pixmap pixmap= static_cast<Pixmap>(mPixmap);
176 Any PixmapImage::GetDisplay() const
178 // return ecore x11 type
179 return Any(mDisplay);
182 bool PixmapImage::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
184 DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
185 bool success = false;
189 XImageJanitor xImageJanitor( XGetImage(static_cast<Display*>(mDisplay),
191 0, 0, // x,y of subregion to extract.
192 width, height, // of suregion to extract.
195 XImage* const pXImage = xImageJanitor.mXImage;
196 DALI_ASSERT_DEBUG(pXImage && "XImage (from pixmap) could not be retrieved from the server");
199 DALI_LOG_ERROR("Could not retrieve Ximage.");
203 switch(pXImage->depth)
205 // Note, depth is a logical value. On target the framebuffer is still 32bpp
206 // (see pXImage->bits_per_pixel) so we go through XGetPixel() and swizzle.
207 // Note, this could be the default, fallback case for all depths if *pXImage
208 // didn't have blank RGB masks (X bug), but we have to hardcode the masks and
212 pixelFormat = Pixel::RGB888;
213 pixbuf.resize(width*height*3);
214 unsigned char* bufPtr = &pixbuf[0];
216 for(unsigned y = height-1; y < height; --y)
218 for(unsigned x = 0; x < width; ++x, bufPtr+=3)
220 const unsigned pixel = XGetPixel(pXImage,x,y);
223 const unsigned blue = pixel & 0xFFU;
224 const unsigned green = (pixel >> 8) & 0xFFU;
225 const unsigned red = (pixel >> 16) & 0xFFU;
239 // Sweep through the image, doing a vertical flip, but handling each scanline as
240 // an inlined intrinsic/builtin memcpy (should be fast):
241 pixbuf.resize(width*height*4);
242 unsigned * bufPtr = reinterpret_cast<unsigned *>(&pixbuf[0]);
243 const unsigned xDataLineSkip = pXImage->bytes_per_line;
244 const size_t copy_count = width * 4;
245 pixelFormat = Pixel::BGRA8888;
247 for(unsigned y = height-1; y < height; --y, bufPtr += width)
249 const char * const in = pXImage->data + xDataLineSkip * y;
251 // Copy a whole scanline at a time:
252 DALI_ASSERT_DEBUG( size_t( bufPtr ) >= size_t( &pixbuf[0] ));
253 DALI_ASSERT_DEBUG( reinterpret_cast<size_t>( bufPtr ) + copy_count <= reinterpret_cast<size_t>( &pixbuf[pixbuf.size()] ) );
254 DALI_ASSERT_DEBUG( in >= pXImage->data );
255 DALI_ASSERT_DEBUG( in + copy_count <= pXImage->data + xDataLineSkip * height );
256 __builtin_memcpy( bufPtr, in, copy_count );
262 DALI_LOG_ERROR("XImage has null data pointer.");
266 // Make a case for 16 bit modes especially to remember that the only reason we don't support them is a bug in X:
269 DALI_ASSERT_DEBUG(pXImage->red_mask && pXImage->green_mask && pXImage->blue_mask && "No image masks mean 16 bit modes are not possible.");
270 ///! If the above assert doesn't fail in a debug build, the X bug may have been fixed, so revisit this function.
271 ///! No break, fall through to the general unsupported format warning below.
275 DALI_LOG_WARNING("Pixmap has unsupported bit-depth for getting pixels: %u", pXImage->depth);
281 DALI_LOG_ERROR("Failed to get pixels from PixmapImage.");
289 bool PixmapImage::EncodeToFile(const std::string& filename) const
291 std::vector< unsigned char > pixbuf;
292 unsigned int width(0), height(0);
293 Pixel::Format pixelFormat;
295 if(GetPixels(pixbuf, width, height, pixelFormat))
297 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
302 bool PixmapImage::GlExtensionCreate()
304 // if the image existed previously delete it.
305 if (mEglImageKHR != NULL)
307 GlExtensionDestroy();
310 EglImageExtensions* eglImageExtensions = GetEglImageExtensions();
312 // casting from an unsigned int to a void *, which should then be cast back
313 // to an unsigned int in the driver.
314 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer > (mPixmap);
316 mEglImageKHR = eglImageExtensions->CreateImageKHR( eglBuffer );
318 return mEglImageKHR != NULL;
321 void PixmapImage::GlExtensionDestroy()
323 EglImageExtensions* eglImageExtensions = GetEglImageExtensions();
325 eglImageExtensions->DestroyImageKHR(mEglImageKHR);
330 unsigned int PixmapImage::TargetTexture()
332 EglImageExtensions* eglImageExtensions = GetEglImageExtensions();
334 eglImageExtensions->TargetTextureKHR(mEglImageKHR);
339 int PixmapImage::GetPixelDepth(Dali::PixmapImage::ColorDepth depth) const
343 case Dali::PixmapImage::COLOR_DEPTH_DEFAULT:
345 // Get the default screen depth
346 return ecore_x_default_depth_get(ecore_x_display_get(), ecore_x_default_screen_get());
348 case Dali::PixmapImage::COLOR_DEPTH_8:
352 case Dali::PixmapImage::COLOR_DEPTH_16:
356 case Dali::PixmapImage::COLOR_DEPTH_24:
360 case Dali::PixmapImage::COLOR_DEPTH_32:
366 DALI_ASSERT_DEBUG(0 && "unknown color enum");
372 void PixmapImage::SetPixelFormat(int depth)
374 // store the pixel format based on the depth
379 mPixelFormat = Pixel::A8;
384 mPixelFormat = Pixel::RGB565;
389 mPixelFormat = Pixel::RGBA8888;
395 mPixelFormat = Pixel::RGB888;
401 Ecore_X_Pixmap PixmapImage::GetPixmapFromAny(Any pixmap) const
408 // see if it is of type x11 pixmap
409 if (pixmap.GetType() == typeid (Pixmap))
411 // get the x pixmap type
412 Pixmap xpixmap = AnyCast<Pixmap>(pixmap);
414 // cast it to a ecore pixmap type
415 return static_cast<Ecore_X_Pixmap>(xpixmap);
419 return AnyCast<Ecore_X_Pixmap>(pixmap);
423 void PixmapImage::GetPixmapDetails()
427 // get the width, height and depth
428 ecore_x_pixmap_geometry_get(mPixmap, &x, &y, (int*)&mWidth, (int*)&mHeight);
430 // set the pixel format
431 SetPixelFormat(ecore_x_pixmap_depth_get(mPixmap));
434 EglImageExtensions* PixmapImage::GetEglImageExtensions() const
436 EglFactory& factory = mAdaptor.GetEGLFactory();
437 EglImageExtensions* egl = factory.GetImageExtensions();
438 DALI_ASSERT_DEBUG( egl && "EGL Image Extensions not initialized" );
442 } // namespace Adaptor
444 } // namespace internal