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>
40 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
41 const char* SAMPLER_TYPE = "samplerExternalOES";
53 using Dali::Integration::PixelBuffer;
55 NativeImageSourceAndroid* NativeImageSourceAndroid::New( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
57 NativeImageSourceAndroid* image = new NativeImageSourceAndroid( width, height, depth, nativeImageSource );
58 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
60 // 2nd phase construction
61 if( image ) //< Defensive in case we ever compile without exceptions.
69 NativeImageSourceAndroid::NativeImageSourceAndroid( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
74 mBlendingRequired( false ),
77 mEglImageExtensions( NULL )
79 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
81 GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
82 auto eglGraphics = static_cast<EglGraphics*>( graphics );
84 mEglImageExtensions = eglGraphics->GetImageExtensions();
86 DALI_ASSERT_DEBUG( mEglImageExtensions );
89 mPixmap = static_cast<AHardwareBuffer*>( GetPixmapFromAny( nativeImageSource ) );
92 AHardwareBuffer_Desc bufferDescription;
93 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
94 bufferDescription.width = width;
95 bufferDescription.height = height;
96 bufferDescription.layers = 1;
97 bufferDescription.usage = AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
100 case Dali::NativeImageSource::COLOR_DEPTH_32:
101 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
102 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
104 case Dali::NativeImageSource::COLOR_DEPTH_24:
105 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM;
107 case Dali::NativeImageSource::COLOR_DEPTH_16:
108 bufferDescription.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
110 case Dali::NativeImageSource::COLOR_DEPTH_8:
111 bufferDescription.format = AHARDWAREBUFFER_FORMAT_BLOB;
115 int ret = AHardwareBuffer_allocate( &bufferDescription, &mPixmap );
118 DALI_LOG_ERROR("Failed to allocate AHardwareBuffer %d", ret);
125 void NativeImageSourceAndroid::Initialize()
127 if( mPixmap && !mOwnPixmap )
129 AHardwareBuffer_acquire( mPixmap ) ;
131 // find out the pixmap width / height and color depth
136 NativeImageSourceAndroid::~NativeImageSourceAndroid()
138 AHardwareBuffer_release( mPixmap );
142 Any NativeImageSourceAndroid::GetNativeImageSource() const
144 return Any( mPixmap );
147 bool NativeImageSourceAndroid::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
149 DALI_ASSERT_DEBUG( sizeof(unsigned) == 4 );
150 bool success = false;
155 AHardwareBuffer_Desc bufferDescription;
156 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
157 AHardwareBuffer_describe( mPixmap, &bufferDescription );
158 switch( bufferDescription.format )
160 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
161 pixelFormat = Pixel::Format::RGBA8888;
163 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
164 pixelFormat = Pixel::Format::RGB8888;
166 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
167 pixelFormat = Pixel::Format::RGB888;
169 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
170 pixelFormat = Pixel::Format::RGB565;
172 case AHARDWAREBUFFER_FORMAT_BLOB:
174 pixelFormat = Pixel::Format::A8;
179 int ret = AHardwareBuffer_lock( mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer );
182 DALI_LOG_ERROR("Failed to AHardwareBuffer_lock %d", ret);
186 uint32_t size = bufferDescription.stride * bufferDescription.height;
187 pixbuf.resize( size );
188 memcpy( pixbuf.data(), buffer, size );
190 ret = AHardwareBuffer_unlock( mPixmap, NULL );
193 DALI_LOG_ERROR("failed to AHardwareBuffer_unlock %d", ret);
201 void NativeImageSourceAndroid::SetSource( Any source )
207 AHardwareBuffer_release( mPixmap );
211 mPixmap = static_cast<AHardwareBuffer*>( GetPixmapFromAny( source ) );
215 // we don't own the pixmap
218 // find out the pixmap width / height and color depth
223 bool NativeImageSourceAndroid::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
228 bool NativeImageSourceAndroid::CreateResource()
230 // if the image existed previously delete it.
231 if( mEglImageKHR != NULL )
236 DALI_ASSERT_ALWAYS( mPixmap );
237 EGLClientBuffer eglBuffer = eglGetNativeClientBufferANDROID( mPixmap );
238 switch( eglGetError() )
244 case EGL_BAD_PARAMETER:
246 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: bad pixmap parameter\n" );
251 DALI_LOG_ERROR( "EGL_BAD_ACCESS: bad access to pixmap\n" );
256 DALI_LOG_ERROR( "EGL_BAD_ALLOC: Insufficient memory is available\n" );
261 DALI_LOG_ERROR( "eglGetNativeClientBufferANDROID error\n" );
266 DALI_ASSERT_ALWAYS( eglBuffer );
267 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
269 return mEglImageKHR != NULL;
272 void NativeImageSourceAndroid::DestroyResource()
274 mEglImageExtensions->DestroyImageKHR( mEglImageKHR );
279 uint32_t NativeImageSourceAndroid::TargetTexture()
281 mEglImageExtensions->TargetTextureKHR( mEglImageKHR );
286 void NativeImageSourceAndroid::PrepareTexture()
290 int NativeImageSourceAndroid::GetTextureTarget() const
292 return GL_TEXTURE_2D;
295 const char* NativeImageSourceAndroid::GetCustomFragmentPrefix() const
300 const char* NativeImageSourceAndroid::GetCustomSamplerTypename() const
306 void* NativeImageSourceAndroid::GetPixmapFromAny(Any pixmap) const
313 return AnyCast<void*>( pixmap );
316 void NativeImageSourceAndroid::GetPixmapDetails()
318 // get the width, height and depth
319 mBlendingRequired = false;
321 AHardwareBuffer_Desc bufferDescription;
322 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
323 AHardwareBuffer_describe( mPixmap, &bufferDescription );
325 mWidth = bufferDescription.width;
326 mHeight = bufferDescription.height;
327 switch (bufferDescription.format)
329 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
330 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_32;
332 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
333 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_24;
335 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
336 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_16;
338 case AHARDWAREBUFFER_FORMAT_BLOB:
340 mColorDepth = Dali::NativeImageSource::COLOR_DEPTH_8;
344 uint8_t* NativeImageSourceAndroid::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
348 AHardwareBuffer_Desc bufferDescription;
349 memset( &bufferDescription, 0, sizeof( AHardwareBuffer_Desc ) );
350 AHardwareBuffer_describe( mPixmap, &bufferDescription );
353 if( AHardwareBuffer_lock( mPixmap, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &buffer ) != 0 )
355 DALI_LOG_ERROR( "Failed to AHardwareBuffer_lock\n" );
359 stride = bufferDescription.stride;
360 width = bufferDescription.width;
361 height = bufferDescription.height;
363 return static_cast< uint8_t* >( buffer );
370 bool NativeImageSourceAndroid::ReleaseBuffer()
374 if( AHardwareBuffer_unlock( mPixmap, NULL ) != 0 )
376 DALI_LOG_ERROR( "failed to AHardwareBuffer_unlock\n" );
384 } // namespace Adaptor
386 } // namespace internal