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)
69 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
71 GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
72 auto eglGraphics = static_cast<EglGraphics*>(graphics);
74 mEglImageExtensions = eglGraphics->GetImageExtensions();
76 DALI_ASSERT_DEBUG(mEglImageExtensions);
79 mPixmap = static_cast<AHardwareBuffer*>(GetPixmapFromAny(nativeImageSource));
82 AHardwareBuffer_Desc bufferDescription;
83 memset(&bufferDescription, 0, sizeof(AHardwareBuffer_Desc));
84 bufferDescription.width = width;
85 bufferDescription.height = height;
86 bufferDescription.layers = 1;
87 bufferDescription.usage = AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
90 case Dali::NativeImageSource::COLOR_DEPTH_32:
91 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
92 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
94 case Dali::NativeImageSource::COLOR_DEPTH_24:
95 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM;
97 case Dali::NativeImageSource::COLOR_DEPTH_16:
98 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
100 case Dali::NativeImageSource::COLOR_DEPTH_8:
101 bufferDescription.format = AHARDWAREBUFFER_FORMAT_BLOB;
105 int ret = AHardwareBuffer_allocate(&bufferDescription, &mPixmap);
108 DALI_LOG_ERROR("Failed to allocate AHardwareBuffer %d", ret);
115 void NativeImageSourceAndroid::Initialize()
117 if(mPixmap && !mOwnPixmap)
119 AHardwareBuffer_acquire(mPixmap);
121 // find out the pixmap width / height and color depth
126 NativeImageSourceAndroid::~NativeImageSourceAndroid()
128 AHardwareBuffer_release(mPixmap);
132 Any NativeImageSourceAndroid::GetNativeImageSource() const
137 bool NativeImageSourceAndroid::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
139 DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
140 bool success = false;
145 AHardwareBuffer_Desc bufferDescription;
146 memset(&bufferDescription, 0, sizeof(AHardwareBuffer_Desc));
147 AHardwareBuffer_describe(mPixmap, &bufferDescription);
148 switch(bufferDescription.format)
150 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
151 pixelFormat = Pixel::Format::RGBA8888;
153 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
154 pixelFormat = Pixel::Format::RGB8888;
156 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
157 pixelFormat = Pixel::Format::RGB888;
159 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
160 pixelFormat = Pixel::Format::RGB565;
162 case AHARDWAREBUFFER_FORMAT_BLOB:
164 pixelFormat = Pixel::Format::A8;
169 int ret = AHardwareBuffer_lock(mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer);
172 DALI_LOG_ERROR("Failed to AHardwareBuffer_lock %d", ret);
176 uint32_t pixelBytes = GetBytesPerPixel(pixelFormat);
177 if(bufferDescription.stride < (pixelBytes * bufferDescription.width))
179 //On Android device, bufferDescription.stride doesn't seem to mean (width * pixelbytes)
180 //in an actual case, (AHardwareBuffer_Desc) bufferDescription = (width = 1080, height = 1060, layers = 1, format = 1, usage = 306, stride = 1088, rfu0 = 0, rfu1 = 0)
181 //deal with situation
182 uint32_t dstStride = pixelBytes * bufferDescription.width;
183 uint32_t srcStride = pixelBytes * bufferDescription.stride;
184 uint32_t size = dstStride * bufferDescription.height;
187 const unsigned char* ptrSrc = reinterpret_cast<const unsigned char*>(buffer);
188 unsigned char* ptrDst = pixbuf.data();
189 for(int y = 0; y < bufferDescription.height; y++, ptrSrc += srcStride, ptrDst += dstStride)
191 memcpy(ptrDst, ptrSrc, dstStride);
196 uint32_t size = bufferDescription.stride * bufferDescription.height;
198 memcpy(pixbuf.data(), buffer, size);
201 ret = AHardwareBuffer_unlock(mPixmap, NULL);
204 DALI_LOG_ERROR("failed to AHardwareBuffer_unlock %d", ret);
212 void NativeImageSourceAndroid::SetSource(Any source)
218 AHardwareBuffer_release(mPixmap);
222 mPixmap = static_cast<AHardwareBuffer*>(GetPixmapFromAny(source));
226 // we don't own the pixmap
229 // find out the pixmap width / height and color depth
234 bool NativeImageSourceAndroid::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
239 bool NativeImageSourceAndroid::CreateResource()
241 // if the image existed previously delete it.
242 if(mEglImageKHR != NULL)
247 DALI_ASSERT_ALWAYS(mPixmap);
248 EGLClientBuffer eglBuffer = eglGetNativeClientBufferANDROID(mPixmap);
249 switch(eglGetError())
255 case EGL_BAD_PARAMETER:
257 DALI_LOG_ERROR("EGL_BAD_PARAMETER: bad pixmap parameter\n");
262 DALI_LOG_ERROR("EGL_BAD_ACCESS: bad access to pixmap\n");
267 DALI_LOG_ERROR("EGL_BAD_ALLOC: Insufficient memory is available\n");
272 DALI_LOG_ERROR("eglGetNativeClientBufferANDROID error\n");
277 DALI_ASSERT_ALWAYS(eglBuffer);
278 mEglImageKHR = mEglImageExtensions->CreateImageKHR(eglBuffer);
280 return mEglImageKHR != NULL;
283 void NativeImageSourceAndroid::DestroyResource()
285 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
290 uint32_t NativeImageSourceAndroid::TargetTexture()
292 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
297 void NativeImageSourceAndroid::PrepareTexture()
301 int NativeImageSourceAndroid::GetTextureTarget() const
303 return GL_TEXTURE_2D;
306 const char* NativeImageSourceAndroid::GetCustomFragmentPrefix() const
311 const char* NativeImageSourceAndroid::GetCustomSamplerTypename() const
316 void* NativeImageSourceAndroid::GetPixmapFromAny(Any pixmap) const
323 return AnyCast<void*>(pixmap);
326 void NativeImageSourceAndroid::GetPixmapDetails()
328 // get the width, height and depth
329 mBlendingRequired = false;
331 AHardwareBuffer_Desc bufferDescription;
332 memset(&bufferDescription, 0, sizeof(AHardwareBuffer_Desc));
333 AHardwareBuffer_describe(mPixmap, &bufferDescription);
335 mWidth = bufferDescription.width;
336 mHeight = bufferDescription.height;
337 switch(bufferDescription.format)
339 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
340 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_32;
342 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
343 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_24;
345 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
346 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_16;
348 case AHARDWAREBUFFER_FORMAT_BLOB:
350 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_8;
354 uint8_t* NativeImageSourceAndroid::AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride)
358 AHardwareBuffer_Desc bufferDescription;
359 memset(&bufferDescription, 0, sizeof(AHardwareBuffer_Desc));
360 AHardwareBuffer_describe(mPixmap, &bufferDescription);
363 if(AHardwareBuffer_lock(mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer) != 0)
365 DALI_LOG_ERROR("Failed to AHardwareBuffer_lock\n");
369 stride = bufferDescription.stride;
370 width = bufferDescription.width;
371 height = bufferDescription.height;
373 return static_cast<uint8_t*>(buffer);
379 bool NativeImageSourceAndroid::ReleaseBuffer()
383 if(AHardwareBuffer_unlock(mPixmap, NULL) != 0)
385 DALI_LOG_ERROR("failed to AHardwareBuffer_unlock\n");
393 } // namespace Adaptor
395 } // namespace Internal