2 * Copyright (c) 2022 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 <dali/internal/imaging/ubuntu-x11/native-image-source-impl-x.h>
23 #include <X11/Xutil.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/internal/system/linux/dali-ecore-x.h>
28 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
29 #include <dali/internal/adaptor/common/adaptor-impl.h>
30 #include <dali/internal/graphics/common/egl-image-extensions.h>
31 #include <dali/internal/graphics/gles/egl-graphics.h>
39 using Dali::Integration::PixelBuffer;
41 // Pieces needed to save compressed images (temporary location while plumbing):
45 * Free an allocated XImage on destruction.
49 XImageJanitor(XImage* const pXImage)
52 DALI_ASSERT_DEBUG(pXImage != 0 && "Null pointer to XImage.");
59 if(!XDestroyImage(mXImage))
61 DALI_LOG_ERROR("XImage deallocation failure");
65 XImage* const mXImage;
68 XImageJanitor(const XImageJanitor& rhs);
69 XImageJanitor& operator=(const XImageJanitor& rhs);
73 NativeImageSourceX* NativeImageSourceX::New(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
75 NativeImageSourceX* image = new NativeImageSourceX(width, height, depth, nativeImageSource);
76 DALI_ASSERT_DEBUG(image && "NativeImageSource allocation failed.");
78 // 2nd phase construction
79 if(image) //< Defensive in case we ever compile without exceptions.
87 NativeImageSourceX::NativeImageSourceX(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
92 mBlendingRequired(false),
96 mEglImageExtensions(NULL),
97 mResourceDestructionCallback()
99 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
101 GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
102 mEglGraphics = static_cast<EglGraphics*>(graphics);
105 mPixmap = GetPixmapFromAny(nativeImageSource);
108 void NativeImageSourceX::Initialize()
110 // if pixmap has been created outside of X11 Image we can return
113 // we don't own the pixmap
116 // find out the pixmap width / height and color depth
121 // get the pixel depth
122 int depth = GetPixelDepth(mColorDepth);
124 // set whether blending is required according to pixel format based on the depth
125 /* default pixel format is RGB888
126 If depth = 8, Pixel::A8;
127 If depth = 16, Pixel::RGB565;
128 If depth = 32, Pixel::RGBA8888 */
129 mBlendingRequired = (depth == 32 || depth == 8);
131 mPixmap = ecore_x_pixmap_new(0, mWidth, mHeight, depth);
135 NativeImageSourceX::~NativeImageSourceX()
137 if(mOwnPixmap && mPixmap)
139 // Temporarily disable this as this causes a crash with EFL Version 1.24.0
140 //ecore_x_pixmap_free(mPixmap);
144 Any NativeImageSourceX::GetNativeImageSource() const
146 // return ecore x11 type
150 bool NativeImageSourceX::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
152 DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
153 bool success = false;
157 // Open a display connection
158 Display* displayConnection = XOpenDisplay(0);
160 XImageJanitor xImageJanitor(XGetImage(displayConnection,
163 0, // x,y of subregion to extract.
165 height, // of subregion to extract.
168 XImage* const pXImage = xImageJanitor.mXImage;
169 DALI_ASSERT_DEBUG(pXImage && "XImage (from pixmap) could not be retrieved from the server");
172 DALI_LOG_ERROR("Could not retrieve Ximage.\n");
176 switch(pXImage->depth)
178 // Note, depth is a logical value. On target the framebuffer is still 32bpp
179 // (see pXImage->bits_per_pixel) so we go through XGetPixel() and swizzle.
180 // Note, this could be the default, fallback case for all depths if *pXImage
181 // didn't have blank RGB masks (X bug), but we have to hardcode the masks and
185 pixelFormat = Pixel::RGB888;
186 pixbuf.resize(width * height * 3);
187 unsigned char* bufPtr = &pixbuf[0];
189 for(unsigned y = 0; y < height; ++y)
191 for(unsigned x = 0; x < width; ++x, bufPtr += 3)
193 const unsigned pixel = XGetPixel(pXImage, x, y);
196 const unsigned blue = pixel & 0xFFU;
197 const unsigned green = (pixel >> 8) & 0xFFU;
198 const unsigned red = (pixel >> 16) & 0xFFU;
201 *(bufPtr + 1) = green;
202 *(bufPtr + 2) = blue;
212 // Sweep through the image, doing a vertical flip, but handling each scanline as
213 // an inlined intrinsic/builtin memcpy (should be fast):
214 pixbuf.resize(width * height * 4);
215 unsigned* bufPtr = reinterpret_cast<unsigned*>(&pixbuf[0]);
216 const unsigned xDataLineSkip = pXImage->bytes_per_line;
217 const size_t copy_count = static_cast<size_t>(width) * 4;
218 pixelFormat = Pixel::BGRA8888;
220 for(unsigned y = 0; y < height; ++y, bufPtr += width)
222 const char* const in = pXImage->data + xDataLineSkip * y;
224 // Copy a whole scanline at a time:
225 DALI_ASSERT_DEBUG(size_t(bufPtr) >= size_t(&pixbuf[0]));
226 DALI_ASSERT_DEBUG(reinterpret_cast<size_t>(bufPtr) + copy_count <= reinterpret_cast<size_t>(&pixbuf[pixbuf.size()]));
227 DALI_ASSERT_DEBUG(in >= pXImage->data);
228 DALI_ASSERT_DEBUG(in + copy_count <= pXImage->data + xDataLineSkip * height);
229 __builtin_memcpy(bufPtr, in, copy_count);
235 DALI_LOG_ERROR("XImage has null data pointer.\n");
239 // Make a case for 16 bit modes especially to remember that the only reason we don't support them is a bug in X:
242 DALI_ASSERT_DEBUG(pXImage->red_mask && pXImage->green_mask && pXImage->blue_mask && "No image masks mean 16 bit modes are not possible.");
243 ///! If the above assert doesn't fail in a debug build, the X bug may have been fixed, so revisit this function.
244 ///! No break, fall through to the general unsupported format warning below.
248 DALI_LOG_WARNING("Pixmap has unsupported bit-depth for getting pixels: %u\n", pXImage->depth);
254 DALI_LOG_ERROR("Failed to get pixels from NativeImageSource.\n");
260 // Close the display connection
261 XCloseDisplay(displayConnection);
266 void NativeImageSourceX::SetSource(Any source)
268 mPixmap = GetPixmapFromAny(source);
272 // we don't own the pixmap
275 // find out the pixmap width / height and color depth
280 bool NativeImageSourceX::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
285 bool NativeImageSourceX::CreateResource()
287 mEglImageExtensions = mEglGraphics->GetImageExtensions();
288 DALI_ASSERT_DEBUG(mEglImageExtensions);
290 // if the image existed previously delete it.
291 if(mEglImageKHR != NULL)
296 // casting from an unsigned int to a void *, which should then be cast back
297 // to an unsigned int in the driver.
298 EGLClientBuffer eglBuffer = reinterpret_cast<EGLClientBuffer>(mPixmap);
300 mEglImageKHR = mEglImageExtensions->CreateImageKHR(eglBuffer);
302 return mEglImageKHR != NULL;
305 void NativeImageSourceX::DestroyResource()
307 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
311 if(mResourceDestructionCallback)
313 mResourceDestructionCallback->Trigger();
317 uint32_t NativeImageSourceX::TargetTexture()
319 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
324 void NativeImageSourceX::PrepareTexture()
328 int NativeImageSourceX::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
332 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
334 // Get the default screen depth
335 return ecore_x_default_depth_get(ecore_x_display_get(), ecore_x_default_screen_get());
337 case Dali::NativeImageSource::COLOR_DEPTH_8:
341 case Dali::NativeImageSource::COLOR_DEPTH_16:
345 case Dali::NativeImageSource::COLOR_DEPTH_24:
349 case Dali::NativeImageSource::COLOR_DEPTH_32:
355 DALI_ASSERT_DEBUG(0 && "unknown color enum");
361 int NativeImageSourceX::GetTextureTarget() const
363 return GL_TEXTURE_2D;
366 bool NativeImageSourceX::ApplyNativeFragmentShader(std::string& shader)
371 const char* NativeImageSourceX::GetCustomSamplerTypename() const
376 Any NativeImageSourceX::GetNativeImageHandle() const
381 bool NativeImageSourceX::SourceChanged() const
386 Ecore_X_Pixmap NativeImageSourceX::GetPixmapFromAny(Any pixmap) const
393 // see if it is of type x11 pixmap
394 if(pixmap.GetType() == typeid(Pixmap))
396 // get the x pixmap type
397 Pixmap xpixmap = AnyCast<Pixmap>(pixmap);
399 // cast it to a ecore pixmap type
400 return static_cast<Ecore_X_Pixmap>(xpixmap);
404 return AnyCast<Ecore_X_Pixmap>(pixmap);
408 void NativeImageSourceX::GetPixmapDetails()
412 // get the width, height and depth
413 ecore_x_pixmap_geometry_get(mPixmap, &x, &y, reinterpret_cast<int*>(&mWidth), reinterpret_cast<int*>(&mHeight));
415 // set whether blending is required according to pixel format based on the depth
416 /* default pixel format is RGB888
417 If depth = 8, Pixel::A8;
418 If depth = 16, Pixel::RGB565;
419 If depth = 32, Pixel::RGBA8888 */
420 int depth = ecore_x_pixmap_depth_get(mPixmap);
421 mBlendingRequired = (depth == 32 || depth == 8);
424 uint8_t* NativeImageSourceX::AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride)
429 bool NativeImageSourceX::ReleaseBuffer()
434 void NativeImageSourceX::SetResourceDestructionCallback(EventThreadCallback* callback)
436 mResourceDestructionCallback = std::unique_ptr<EventThreadCallback>(callback);
439 } // namespace Adaptor
441 } // namespace Internal