2 * Copyright (c) 2020 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 <include/EGL/eglext.h>
29 #include <dali/integration-api/debug.h>
32 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
33 #include <dali/internal/graphics/common/egl-image-extensions.h>
34 #include <dali/internal/graphics/gles/egl-graphics.h>
35 #include <dali/internal/adaptor/common/adaptor-impl.h>
45 using Dali::Integration::PixelBuffer;
47 NativeImageSourceAndroid* NativeImageSourceAndroid::New( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
49 NativeImageSourceAndroid* image = new NativeImageSourceAndroid( width, height, depth, nativeImageSource );
50 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
52 // 2nd phase construction
53 if( image ) //< Defensive in case we ever compile without exceptions.
61 NativeImageSourceAndroid::NativeImageSourceAndroid( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
66 mBlendingRequired( false ),
69 mEglImageExtensions( NULL )
71 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
73 GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
74 auto eglGraphics = static_cast<EglGraphics*>( graphics );
76 mEglImageExtensions = eglGraphics->GetImageExtensions();
78 DALI_ASSERT_DEBUG( mEglImageExtensions );
81 mPixmap = static_cast<AHardwareBuffer*>( GetPixmapFromAny( nativeImageSource ) );
84 AHardwareBuffer_Desc bufferDescription;
85 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
86 bufferDescription.width = width;
87 bufferDescription.height = height;
88 bufferDescription.layers = 1;
89 bufferDescription.usage = AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
92 case Dali::NativeImageSource::COLOR_DEPTH_32:
93 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
94 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
96 case Dali::NativeImageSource::COLOR_DEPTH_24:
97 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM;
99 case Dali::NativeImageSource::COLOR_DEPTH_16:
100 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
102 case Dali::NativeImageSource::COLOR_DEPTH_8:
103 bufferDescription.format = AHARDWAREBUFFER_FORMAT_BLOB;
107 int ret = AHardwareBuffer_allocate( &bufferDescription, &mPixmap );
110 DALI_LOG_ERROR("Failed to allocate AHardwareBuffer %d", ret);
117 void NativeImageSourceAndroid::Initialize()
119 if( mPixmap && !mOwnPixmap )
121 AHardwareBuffer_acquire( mPixmap ) ;
123 // find out the pixmap width / height and color depth
128 NativeImageSourceAndroid::~NativeImageSourceAndroid()
130 AHardwareBuffer_release( mPixmap );
134 Any NativeImageSourceAndroid::GetNativeImageSource() const
136 return Any( mPixmap );
139 bool NativeImageSourceAndroid::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
141 DALI_ASSERT_DEBUG( sizeof(unsigned) == 4 );
142 bool success = false;
147 AHardwareBuffer_Desc bufferDescription;
148 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
149 AHardwareBuffer_describe( mPixmap, &bufferDescription );
150 switch( bufferDescription.format )
152 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
153 pixelFormat = Pixel::Format::RGBA8888;
155 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
156 pixelFormat = Pixel::Format::RGB8888;
158 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
159 pixelFormat = Pixel::Format::RGB888;
161 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
162 pixelFormat = Pixel::Format::RGB565;
164 case AHARDWAREBUFFER_FORMAT_BLOB:
166 pixelFormat = Pixel::Format::A8;
171 int ret = AHardwareBuffer_lock( mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer );
174 DALI_LOG_ERROR("Failed to AHardwareBuffer_lock %d", ret);
178 uint32_t size = bufferDescription.stride * bufferDescription.height;
179 pixbuf.resize( size );
180 memcpy( pixbuf.data(), buffer, size );
182 ret = AHardwareBuffer_unlock( mPixmap, NULL );
185 DALI_LOG_ERROR("failed to AHardwareBuffer_unlock %d", ret);
193 void NativeImageSourceAndroid::SetSource( Any source )
199 AHardwareBuffer_release( mPixmap );
203 mPixmap = static_cast<AHardwareBuffer*>( GetPixmapFromAny( source ) );
207 // we don't own the pixmap
210 // find out the pixmap width / height and color depth
215 bool NativeImageSourceAndroid::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
220 bool NativeImageSourceAndroid::GlExtensionCreate()
222 // if the image existed previously delete it.
223 if( mEglImageKHR != NULL )
225 GlExtensionDestroy();
228 DALI_ASSERT_ALWAYS( mPixmap );
229 EGLClientBuffer eglBuffer = eglGetNativeClientBufferANDROID( mPixmap );
230 switch( eglGetError() )
236 case EGL_BAD_PARAMETER:
238 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: bad pixmap parameter\n" );
243 DALI_LOG_ERROR( "EGL_BAD_ACCESS: bad access to pixmap\n" );
248 DALI_LOG_ERROR( "EGL_BAD_ALLOC: Insufficient memory is available\n" );
253 DALI_LOG_ERROR( "eglGetNativeClientBufferANDROID error\n" );
258 DALI_ASSERT_ALWAYS( eglBuffer );
259 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
261 return mEglImageKHR != NULL;
264 void NativeImageSourceAndroid::GlExtensionDestroy()
266 mEglImageExtensions->DestroyImageKHR( mEglImageKHR );
271 uint32_t NativeImageSourceAndroid::TargetTexture()
273 mEglImageExtensions->TargetTextureKHR( mEglImageKHR );
278 void NativeImageSourceAndroid::PrepareTexture()
282 void* NativeImageSourceAndroid::GetPixmapFromAny(Any pixmap) const
289 return AnyCast<void*>( pixmap );
292 void NativeImageSourceAndroid::GetPixmapDetails()
294 // get the width, height and depth
295 mBlendingRequired = false;
297 AHardwareBuffer_Desc bufferDescription;
298 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
299 AHardwareBuffer_describe( mPixmap, &bufferDescription );
301 mWidth = bufferDescription.width;
302 mHeight = bufferDescription.height;
303 switch (bufferDescription.format)
305 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
306 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_32;
308 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
309 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_24;
311 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
312 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_16;
314 case AHARDWAREBUFFER_FORMAT_BLOB:
316 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_8;
320 uint8_t* NativeImageSourceAndroid::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
324 AHardwareBuffer_Desc bufferDescription;
325 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
326 AHardwareBuffer_describe( mPixmap, &bufferDescription );
329 if( AHardwareBuffer_lock( mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer ) != 0 )
331 DALI_LOG_ERROR( "Failed to AHardwareBuffer_lock\n" );
335 stride = bufferDescription.stride;
336 width = bufferDescription.width;
337 height = bufferDescription.height;
339 return static_cast< uint8_t* >( buffer );
346 bool NativeImageSourceAndroid::ReleaseBuffer()
350 if( AHardwareBuffer_unlock( mPixmap, NULL ) != 0 )
352 DALI_LOG_ERROR( "failed to AHardwareBuffer_unlock\n" );
360 } // namespace Adaptor
362 } // namespace internal