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/devel-api/adaptor-framework/bitmap-saver.h>
33 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
34 #include <dali/internal/graphics/common/egl-image-extensions.h>
35 #include <dali/internal/graphics/gles/egl-graphics.h>
36 #include <dali/internal/adaptor/common/adaptor-impl.h>
46 using Dali::Integration::PixelBuffer;
48 NativeImageSourceAndroid* NativeImageSourceAndroid::New( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
50 NativeImageSourceAndroid* image = new NativeImageSourceAndroid( width, height, depth, nativeImageSource );
51 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
53 // 2nd phase construction
54 if( image ) //< Defensive in case we ever compile without exceptions.
62 NativeImageSourceAndroid::NativeImageSourceAndroid( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
67 mBlendingRequired( false ),
70 mEglImageExtensions( NULL )
72 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
74 GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
75 auto eglGraphics = static_cast<EglGraphics*>( graphics );
77 mEglImageExtensions = eglGraphics->GetImageExtensions();
79 DALI_ASSERT_DEBUG( mEglImageExtensions );
82 mPixmap = static_cast<AHardwareBuffer*>( GetPixmapFromAny( nativeImageSource ) );
85 AHardwareBuffer_Desc bufferDescription;
86 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
87 bufferDescription.width = width;
88 bufferDescription.height = height;
89 bufferDescription.layers = 1;
90 bufferDescription.usage = AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
93 case Dali::NativeImageSource::COLOR_DEPTH_32:
94 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
95 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
97 case Dali::NativeImageSource::COLOR_DEPTH_24:
98 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM;
100 case Dali::NativeImageSource::COLOR_DEPTH_16:
101 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
103 case Dali::NativeImageSource::COLOR_DEPTH_8:
104 bufferDescription.format = AHARDWAREBUFFER_FORMAT_BLOB;
108 int ret = AHardwareBuffer_allocate( &bufferDescription, &mPixmap );
111 DALI_LOG_ERROR("Failed to allocate AHardwareBuffer %d", ret);
118 void NativeImageSourceAndroid::Initialize()
120 if( mPixmap && !mOwnPixmap )
122 AHardwareBuffer_acquire( mPixmap ) ;
124 // find out the pixmap width / height and color depth
129 NativeImageSourceAndroid::~NativeImageSourceAndroid()
131 AHardwareBuffer_release( mPixmap );
135 Any NativeImageSourceAndroid::GetNativeImageSource() const
137 return Any( mPixmap );
140 bool NativeImageSourceAndroid::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
142 DALI_ASSERT_DEBUG( sizeof(unsigned) == 4 );
143 bool success = false;
148 AHardwareBuffer_Desc bufferDescription;
149 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
150 AHardwareBuffer_describe( mPixmap, &bufferDescription );
151 switch( bufferDescription.format )
153 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
154 pixelFormat = Pixel::Format::RGBA8888;
156 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
157 pixelFormat = Pixel::Format::RGB8888;
159 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
160 pixelFormat = Pixel::Format::RGB888;
162 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
163 pixelFormat = Pixel::Format::RGB565;
165 case AHARDWAREBUFFER_FORMAT_BLOB:
167 pixelFormat = Pixel::Format::A8;
172 int ret = AHardwareBuffer_lock( mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer );
175 DALI_LOG_ERROR("Failed to AHardwareBuffer_lock %d", ret);
179 uint32_t size = bufferDescription.stride * bufferDescription.height;
180 pixbuf.resize( size );
181 memcpy( pixbuf.data(), buffer, size );
183 ret = AHardwareBuffer_unlock( mPixmap, NULL );
186 DALI_LOG_ERROR("failed to AHardwareBuffer_unlock %d", ret);
194 bool NativeImageSourceAndroid::EncodeToFile(const std::string& filename) const
196 std::vector< unsigned char > pixbuf;
197 unsigned int width(0), height(0);
198 Pixel::Format pixelFormat;
200 if( GetPixels( pixbuf, width, height, pixelFormat ) )
202 return Dali::EncodeToFile( &pixbuf[0], filename, pixelFormat, width, height );
207 void NativeImageSourceAndroid::SetSource( Any source )
213 AHardwareBuffer_release( mPixmap );
217 mPixmap = static_cast<AHardwareBuffer*>( GetPixmapFromAny( source ) );
221 // we don't own the pixmap
224 // find out the pixmap width / height and color depth
229 bool NativeImageSourceAndroid::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
234 bool NativeImageSourceAndroid::GlExtensionCreate()
236 // if the image existed previously delete it.
237 if( mEglImageKHR != NULL )
239 GlExtensionDestroy();
242 DALI_ASSERT_ALWAYS( mPixmap );
243 EGLClientBuffer eglBuffer = eglGetNativeClientBufferANDROID( mPixmap );
244 switch( eglGetError() )
250 case EGL_BAD_PARAMETER:
252 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: bad pixmap parameter\n" );
257 DALI_LOG_ERROR( "EGL_BAD_ACCESS: bad access to pixmap\n" );
262 DALI_LOG_ERROR( "EGL_BAD_ALLOC: Insufficient memory is available\n" );
267 DALI_LOG_ERROR( "eglGetNativeClientBufferANDROID error\n" );
272 DALI_ASSERT_ALWAYS( eglBuffer );
273 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
275 return mEglImageKHR != NULL;
278 void NativeImageSourceAndroid::GlExtensionDestroy()
280 mEglImageExtensions->DestroyImageKHR( mEglImageKHR );
285 uint32_t NativeImageSourceAndroid::TargetTexture()
287 mEglImageExtensions->TargetTextureKHR( mEglImageKHR );
292 void NativeImageSourceAndroid::PrepareTexture()
296 void* NativeImageSourceAndroid::GetPixmapFromAny(Any pixmap) const
303 return AnyCast<void*>( pixmap );
306 void NativeImageSourceAndroid::GetPixmapDetails()
308 // get the width, height and depth
309 mBlendingRequired = false;
311 AHardwareBuffer_Desc bufferDescription;
312 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
313 AHardwareBuffer_describe( mPixmap, &bufferDescription );
315 mWidth = bufferDescription.width;
316 mHeight = bufferDescription.height;
317 switch (bufferDescription.format)
319 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
320 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_32;
322 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
323 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_24;
325 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
326 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_16;
328 case AHARDWAREBUFFER_FORMAT_BLOB:
330 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_8;
334 uint8_t* NativeImageSourceAndroid::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
338 AHardwareBuffer_Desc bufferDescription;
339 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
340 AHardwareBuffer_describe( mPixmap, &bufferDescription );
343 if( AHardwareBuffer_lock( mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer ) != 0 )
345 DALI_LOG_ERROR( "Failed to AHardwareBuffer_lock\n" );
349 stride = bufferDescription.stride;
350 width = bufferDescription.width;
351 height = bufferDescription.height;
353 return static_cast< uint8_t* >( buffer );
360 bool NativeImageSourceAndroid::ReleaseBuffer()
364 if( AHardwareBuffer_unlock( mPixmap, NULL ) != 0 )
366 DALI_LOG_ERROR( "failed to AHardwareBuffer_unlock\n" );
374 } // namespace Adaptor
376 } // namespace internal