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>
43 using Dali::Integration::PixelBuffer;
45 NativeImageSourceAndroid* NativeImageSourceAndroid::New(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
47 NativeImageSourceAndroid* image = new NativeImageSourceAndroid(width, height, depth, nativeImageSource);
48 DALI_ASSERT_DEBUG(image && "NativeImageSource allocation failed.");
50 // 2nd phase construction
51 if(image) //< Defensive in case we ever compile without exceptions.
59 NativeImageSourceAndroid::NativeImageSourceAndroid( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
64 mBlendingRequired(false),
67 mEglImageExtensions(NULL),
68 mResourceDestructionCallback()
70 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
72 GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
73 auto eglGraphics = static_cast<EglGraphics*>(graphics);
75 mEglImageExtensions = eglGraphics->GetImageExtensions();
77 DALI_ASSERT_DEBUG(mEglImageExtensions);
80 mPixmap = static_cast<AHardwareBuffer*>(GetPixmapFromAny(nativeImageSource));
83 AHardwareBuffer_Desc bufferDescription;
84 memset(&bufferDescription, 0, sizeof(AHardwareBuffer_Desc));
85 bufferDescription.width = width;
86 bufferDescription.height = height;
87 bufferDescription.layers = 1;
88 bufferDescription.usage = AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
91 case Dali::NativeImageSource::COLOR_DEPTH_32:
92 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
93 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
95 case Dali::NativeImageSource::COLOR_DEPTH_24:
96 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM;
98 case Dali::NativeImageSource::COLOR_DEPTH_16:
99 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
101 case Dali::NativeImageSource::COLOR_DEPTH_8:
102 bufferDescription.format = AHARDWAREBUFFER_FORMAT_BLOB;
106 int ret = AHardwareBuffer_allocate(&bufferDescription, &mPixmap);
109 DALI_LOG_ERROR("Failed to allocate AHardwareBuffer %d", ret);
116 void NativeImageSourceAndroid::Initialize()
118 if(mPixmap && !mOwnPixmap)
120 AHardwareBuffer_acquire(mPixmap);
122 // find out the pixmap width / height and color depth
127 NativeImageSourceAndroid::~NativeImageSourceAndroid()
129 AHardwareBuffer_release(mPixmap);
133 Any NativeImageSourceAndroid::GetNativeImageSource() const
138 bool NativeImageSourceAndroid::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
140 DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
141 bool success = false;
146 AHardwareBuffer_Desc bufferDescription;
147 memset(&bufferDescription, 0, sizeof(AHardwareBuffer_Desc));
148 AHardwareBuffer_describe(mPixmap, &bufferDescription);
149 switch(bufferDescription.format)
151 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
152 pixelFormat = Pixel::Format::RGBA8888;
154 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
155 pixelFormat = Pixel::Format::RGB8888;
157 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
158 pixelFormat = Pixel::Format::RGB888;
160 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
161 pixelFormat = Pixel::Format::RGB565;
163 case AHARDWAREBUFFER_FORMAT_BLOB:
165 pixelFormat = Pixel::Format::A8;
170 int ret = AHardwareBuffer_lock(mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer);
173 DALI_LOG_ERROR("Failed to AHardwareBuffer_lock %d", ret);
177 uint32_t pixelBytes = GetBytesPerPixel(pixelFormat);
178 if(bufferDescription.stride < (pixelBytes * bufferDescription.width))
180 //On Android device, bufferDescription.stride doesn't seem to mean (width * pixelbytes)
181 //in an actual case, (AHardwareBuffer_Desc) bufferDescription = (width = 1080, height = 1060, layers = 1, format = 1, usage = 306, stride = 1088, rfu0 = 0, rfu1 = 0)
182 //deal with situation
183 uint32_t dstStride = pixelBytes * bufferDescription.width;
184 uint32_t srcStride = pixelBytes * bufferDescription.stride;
185 uint32_t size = dstStride * bufferDescription.height;
188 const unsigned char* ptrSrc = reinterpret_cast<const unsigned char*>(buffer);
189 unsigned char* ptrDst = pixbuf.data();
190 for(int y = 0; y < bufferDescription.height; y++, ptrSrc += srcStride, ptrDst += dstStride)
192 memcpy(ptrDst, ptrSrc, dstStride);
197 uint32_t size = bufferDescription.stride * bufferDescription.height;
199 memcpy(pixbuf.data(), buffer, size);
202 ret = AHardwareBuffer_unlock(mPixmap, NULL);
205 DALI_LOG_ERROR("failed to AHardwareBuffer_unlock %d", ret);
213 void NativeImageSourceAndroid::SetSource(Any source)
219 AHardwareBuffer_release(mPixmap);
223 mPixmap = static_cast<AHardwareBuffer*>(GetPixmapFromAny(source));
227 // we don't own the pixmap
230 // find out the pixmap width / height and color depth
235 bool NativeImageSourceAndroid::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
240 bool NativeImageSourceAndroid::CreateResource()
242 // if the image existed previously delete it.
243 if(mEglImageKHR != NULL)
248 DALI_ASSERT_ALWAYS(mPixmap);
249 EGLClientBuffer eglBuffer = eglGetNativeClientBufferANDROID(mPixmap);
250 switch(eglGetError())
256 case EGL_BAD_PARAMETER:
258 DALI_LOG_ERROR("EGL_BAD_PARAMETER: bad pixmap parameter\n");
263 DALI_LOG_ERROR("EGL_BAD_ACCESS: bad access to pixmap\n");
268 DALI_LOG_ERROR("EGL_BAD_ALLOC: Insufficient memory is available\n");
273 DALI_LOG_ERROR("eglGetNativeClientBufferANDROID error\n");
278 DALI_ASSERT_ALWAYS(eglBuffer);
279 mEglImageKHR = mEglImageExtensions->CreateImageKHR(eglBuffer);
281 return mEglImageKHR != NULL;
284 void NativeImageSourceAndroid::DestroyResource()
286 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
290 if(mResourceDestructionCallback)
292 mResourceDestructionCallback->Trigger();
296 uint32_t NativeImageSourceAndroid::TargetTexture()
298 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
303 void NativeImageSourceAndroid::PrepareTexture()
307 int NativeImageSourceAndroid::GetTextureTarget() const
309 return GL_TEXTURE_2D;
312 bool NativeImageSourceAndroid::ApplyNativeFragmentShader(std::string& shader)
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 void NativeImageSourceAndroid::SetResourceDestructionCallback(EventThreadCallback* callback)
401 mResourceDestructionCallback = std::unique_ptr<EventThreadCallback>(callback);
404 } // namespace Adaptor
406 } // namespace Internal