2 * Copyright (c) 2021 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.
18 #define EGL_EGLEXT_PROTOTYPES
19 #if __ANDROID_API__ < 26
20 #error "Unsupported Android API version, must be >= 26"
24 #include <dali/internal/imaging/android/native-image-source-impl-android.h>
28 #include <dali/integration-api/debug.h>
29 #include <include/EGL/eglext.h>
32 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
33 #include <dali/internal/adaptor/common/adaptor-impl.h>
34 #include <dali/internal/graphics/common/egl-image-extensions.h>
35 #include <dali/internal/graphics/gles/egl-graphics.h>
39 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
40 const char* SAMPLER_TYPE = "samplerExternalOES";
49 using Dali::Integration::PixelBuffer;
51 NativeImageSourceAndroid* NativeImageSourceAndroid::New(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
53 NativeImageSourceAndroid* image = new NativeImageSourceAndroid(width, height, depth, nativeImageSource);
54 DALI_ASSERT_DEBUG(image && "NativeImageSource allocation failed.");
56 // 2nd phase construction
57 if(image) //< Defensive in case we ever compile without exceptions.
65 NativeImageSourceAndroid::NativeImageSourceAndroid(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
70 mBlendingRequired(false),
73 mEglImageExtensions(NULL)
75 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
77 GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
78 auto eglGraphics = static_cast<EglGraphics*>(graphics);
80 mEglImageExtensions = eglGraphics->GetImageExtensions();
82 DALI_ASSERT_DEBUG(mEglImageExtensions);
85 mPixmap = static_cast<AHardwareBuffer*>(GetPixmapFromAny(nativeImageSource));
88 AHardwareBuffer_Desc bufferDescription;
89 memset(&bufferDescription, 0, sizeof(AHardwareBuffer_Desc));
90 bufferDescription.width = width;
91 bufferDescription.height = height;
92 bufferDescription.layers = 1;
93 bufferDescription.usage = AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
96 case Dali::NativeImageSource::COLOR_DEPTH_32:
97 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
98 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
100 case Dali::NativeImageSource::COLOR_DEPTH_24:
101 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM;
103 case Dali::NativeImageSource::COLOR_DEPTH_16:
104 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
106 case Dali::NativeImageSource::COLOR_DEPTH_8:
107 bufferDescription.format = AHARDWAREBUFFER_FORMAT_BLOB;
111 int ret = AHardwareBuffer_allocate(&bufferDescription, &mPixmap);
114 DALI_LOG_ERROR("Failed to allocate AHardwareBuffer %d", ret);
121 void NativeImageSourceAndroid::Initialize()
123 if(mPixmap && !mOwnPixmap)
125 AHardwareBuffer_acquire(mPixmap);
127 // find out the pixmap width / height and color depth
132 NativeImageSourceAndroid::~NativeImageSourceAndroid()
134 AHardwareBuffer_release(mPixmap);
138 Any NativeImageSourceAndroid::GetNativeImageSource() const
143 bool NativeImageSourceAndroid::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
145 DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
146 bool success = false;
151 AHardwareBuffer_Desc bufferDescription;
152 memset(&bufferDescription, 0, sizeof(AHardwareBuffer_Desc));
153 AHardwareBuffer_describe(mPixmap, &bufferDescription);
154 switch(bufferDescription.format)
156 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
157 pixelFormat = Pixel::Format::RGBA8888;
159 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
160 pixelFormat = Pixel::Format::RGB8888;
162 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
163 pixelFormat = Pixel::Format::RGB888;
165 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
166 pixelFormat = Pixel::Format::RGB565;
168 case AHARDWAREBUFFER_FORMAT_BLOB:
170 pixelFormat = Pixel::Format::A8;
175 int ret = AHardwareBuffer_lock(mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer);
178 DALI_LOG_ERROR("Failed to AHardwareBuffer_lock %d", ret);
182 uint32_t pixelBytes = GetBytesPerPixel(pixelFormat);
183 if(bufferDescription.stride < (pixelBytes * bufferDescription.width))
185 //On Android device, bufferDescription.stride doesn't seem to mean (width * pixelbytes)
186 //in an actual case, (AHardwareBuffer_Desc) bufferDescription = (width = 1080, height = 1060, layers = 1, format = 1, usage = 306, stride = 1088, rfu0 = 0, rfu1 = 0)
187 //deal with situation
188 uint32_t dstStride = pixelBytes * bufferDescription.width;
189 uint32_t srcStride = pixelBytes * bufferDescription.stride;
190 uint32_t size = dstStride * bufferDescription.height;
193 const unsigned char* ptrSrc = reinterpret_cast<const unsigned char*>(buffer);
194 unsigned char* ptrDst = pixbuf.data();
195 for(int y = 0; y < bufferDescription.height; y++, ptrSrc += srcStride, ptrDst += dstStride)
197 memcpy(ptrDst, ptrSrc, dstStride);
202 uint32_t size = bufferDescription.stride * bufferDescription.height;
204 memcpy(pixbuf.data(), buffer, size);
207 ret = AHardwareBuffer_unlock(mPixmap, NULL);
210 DALI_LOG_ERROR("failed to AHardwareBuffer_unlock %d", ret);
218 void NativeImageSourceAndroid::SetSource(Any source)
224 AHardwareBuffer_release(mPixmap);
228 mPixmap = static_cast<AHardwareBuffer*>(GetPixmapFromAny(source));
232 // we don't own the pixmap
235 // find out the pixmap width / height and color depth
240 bool NativeImageSourceAndroid::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
245 bool NativeImageSourceAndroid::CreateResource()
247 // if the image existed previously delete it.
248 if(mEglImageKHR != NULL)
253 DALI_ASSERT_ALWAYS(mPixmap);
254 EGLClientBuffer eglBuffer = eglGetNativeClientBufferANDROID(mPixmap);
255 switch(eglGetError())
261 case EGL_BAD_PARAMETER:
263 DALI_LOG_ERROR("EGL_BAD_PARAMETER: bad pixmap parameter\n");
268 DALI_LOG_ERROR("EGL_BAD_ACCESS: bad access to pixmap\n");
273 DALI_LOG_ERROR("EGL_BAD_ALLOC: Insufficient memory is available\n");
278 DALI_LOG_ERROR("eglGetNativeClientBufferANDROID error\n");
283 DALI_ASSERT_ALWAYS(eglBuffer);
284 mEglImageKHR = mEglImageExtensions->CreateImageKHR(eglBuffer);
286 return mEglImageKHR != NULL;
289 void NativeImageSourceAndroid::DestroyResource()
291 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
296 uint32_t NativeImageSourceAndroid::TargetTexture()
298 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
303 void NativeImageSourceAndroid::PrepareTexture()
307 int NativeImageSourceAndroid::GetTextureTarget() const
309 return GL_TEXTURE_2D;
312 const char* NativeImageSourceAndroid::GetCustomFragmentPrefix() const
317 const char* NativeImageSourceAndroid::GetCustomSamplerTypename() const
322 void* NativeImageSourceAndroid::GetPixmapFromAny(Any pixmap) const
329 return AnyCast<void*>(pixmap);
332 void NativeImageSourceAndroid::GetPixmapDetails()
334 // get the width, height and depth
335 mBlendingRequired = false;
337 AHardwareBuffer_Desc bufferDescription;
338 memset(&bufferDescription, 0, sizeof(AHardwareBuffer_Desc));
339 AHardwareBuffer_describe(mPixmap, &bufferDescription);
341 mWidth = bufferDescription.width;
342 mHeight = bufferDescription.height;
343 switch(bufferDescription.format)
345 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
346 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_32;
348 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
349 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_24;
351 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
352 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_16;
354 case AHARDWAREBUFFER_FORMAT_BLOB:
356 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_8;
360 uint8_t* NativeImageSourceAndroid::AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride)
364 AHardwareBuffer_Desc bufferDescription;
365 memset(&bufferDescription, 0, sizeof(AHardwareBuffer_Desc));
366 AHardwareBuffer_describe(mPixmap, &bufferDescription);
369 if(AHardwareBuffer_lock(mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer) != 0)
371 DALI_LOG_ERROR("Failed to AHardwareBuffer_lock\n");
375 stride = bufferDescription.stride;
376 width = bufferDescription.width;
377 height = bufferDescription.height;
379 return static_cast<uint8_t*>(buffer);
385 bool NativeImageSourceAndroid::ReleaseBuffer()
389 if(AHardwareBuffer_unlock(mPixmap, NULL) != 0)
391 DALI_LOG_ERROR("failed to AHardwareBuffer_unlock\n");
399 } // namespace Adaptor
401 } // namespace Internal