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.
19 #include <dali/internal/imaging/tizen/native-image-source-impl-tizen.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-defines.h>
25 #include <tbm_surface_internal.h>
28 #include <dali/internal/graphics/common/egl-image-extensions.h>
29 #include <dali/internal/graphics/gles/egl-graphics.h>
30 #include <dali/internal/adaptor/common/adaptor-impl.h>
31 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
33 // Allow this to be encoded and saved:
34 #include <dali/devel-api/adaptor-framework/bitmap-saver.h>
47 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
48 const char* SAMPLER_TYPE = "samplerExternalOES";
50 tbm_format FORMATS_BLENDING_REQUIRED[] = {
51 TBM_FORMAT_ARGB4444, TBM_FORMAT_ABGR4444,
52 TBM_FORMAT_RGBA4444, TBM_FORMAT_BGRA4444,
53 TBM_FORMAT_RGBX5551, TBM_FORMAT_BGRX5551,
54 TBM_FORMAT_ARGB1555, TBM_FORMAT_ABGR1555,
55 TBM_FORMAT_RGBA5551, TBM_FORMAT_BGRA5551,
56 TBM_FORMAT_ARGB8888, TBM_FORMAT_ABGR8888,
57 TBM_FORMAT_RGBA8888, TBM_FORMAT_BGRA8888,
58 TBM_FORMAT_ARGB2101010, TBM_FORMAT_ABGR2101010,
59 TBM_FORMAT_RGBA1010102, TBM_FORMAT_BGRA1010102
62 const int NUM_FORMATS_BLENDING_REQUIRED = 18;
66 using Dali::Integration::PixelBuffer;
68 NativeImageSourceTizen* NativeImageSourceTizen::New( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
70 NativeImageSourceTizen* image = new NativeImageSourceTizen( width, height, depth, nativeImageSource );
71 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
81 NativeImageSourceTizen::NativeImageSourceTizen( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
84 mOwnTbmSurface( false ),
87 mBlendingRequired( false ),
91 mEglImageExtensions( NULL ),
94 mIsBufferAcquired( false )
96 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
98 GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
99 mEglGraphics = static_cast<EglGraphics *>(graphics);
101 mTbmSurface = GetSurfaceFromAny( nativeImageSource );
103 if( mTbmSurface != NULL )
105 tbm_surface_internal_ref( mTbmSurface );
106 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
107 mWidth = tbm_surface_get_width( mTbmSurface );
108 mHeight = tbm_surface_get_height( mTbmSurface );
112 void NativeImageSourceTizen::Initialize()
114 if( mTbmSurface != NULL || mWidth == 0 || mHeight == 0 )
119 tbm_format format = TBM_FORMAT_RGB888;
122 switch( mColorDepth )
124 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
126 format = TBM_FORMAT_ARGB8888;
130 case Dali::NativeImageSource::COLOR_DEPTH_8:
132 format = TBM_FORMAT_C8;
136 case Dali::NativeImageSource::COLOR_DEPTH_16:
138 format = TBM_FORMAT_RGB565;
142 case Dali::NativeImageSource::COLOR_DEPTH_24:
144 format = TBM_FORMAT_RGB888;
148 case Dali::NativeImageSource::COLOR_DEPTH_32:
150 format = TBM_FORMAT_ARGB8888;
156 DALI_LOG_WARNING( "Wrong color depth.\n" );
161 // set whether blending is required according to pixel format based on the depth
162 /* default pixel format is RGB888
163 If depth = 8, Pixel::A8;
164 If depth = 16, Pixel::RGB565;
165 If depth = 32, Pixel::RGBA8888 */
166 mBlendingRequired = ( depth == 32 || depth == 8 );
168 mTbmSurface = tbm_surface_create( mWidth, mHeight, format );
169 mOwnTbmSurface = true;
172 tbm_surface_h NativeImageSourceTizen::GetSurfaceFromAny( Any source ) const
179 if( source.GetType() == typeid( tbm_surface_h ) )
181 return AnyCast< tbm_surface_h >( source );
189 void NativeImageSourceTizen::DestroySurface()
193 if( mIsBufferAcquired )
199 if( tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
201 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
206 tbm_surface_internal_unref( mTbmSurface );
211 NativeImageSourceTizen::~NativeImageSourceTizen()
216 Any NativeImageSourceTizen::GetNativeImageSource() const
218 return Any( mTbmSurface );
221 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
223 Dali::Mutex::ScopedLock lock( mMutex );
224 if( mTbmSurface != NULL )
226 tbm_surface_info_s surface_info;
228 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
230 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
238 tbm_format format = surface_info.format;
239 uint32_t stride = surface_info.planes[0].stride;
240 unsigned char* ptr = surface_info.planes[0].ptr;
250 case TBM_FORMAT_RGB888:
253 pixelFormat = Pixel::RGB888;
254 pixbuf.resize( lineSize*height );
255 unsigned char* bufptr = &pixbuf[0];
257 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
259 for( unsigned int c = 0; c < width; ++c )
262 offset = cOffset + r*stride;
263 *(bufptr+cOffset) = ptr[offset+2];
264 *(bufptr+cOffset+1) = ptr[offset+1];
265 *(bufptr+cOffset+2) = ptr[offset];
270 case TBM_FORMAT_RGBA8888:
273 pixelFormat = Pixel::RGBA8888;
274 pixbuf.resize( lineSize*height );
275 unsigned char* bufptr = &pixbuf[0];
277 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
279 for( unsigned int c = 0; c < width; ++c )
282 offset = cOffset + r*stride;
283 *(bufptr+cOffset) = ptr[offset+3];
284 *(bufptr+cOffset+1) = ptr[offset+2];
285 *(bufptr+cOffset+2) = ptr[offset+1];
286 *(bufptr+cOffset+3) = ptr[offset];
291 case TBM_FORMAT_ARGB8888:
294 pixelFormat = Pixel::RGBA8888;
295 pixbuf.resize( lineSize*height );
296 unsigned char* bufptr = &pixbuf[0];
298 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
300 for( unsigned int c = 0; c < width; ++c )
303 offset = cOffset + r*stride;
304 *(bufptr+cOffset) = ptr[offset];
305 *(bufptr+cOffset+1) = ptr[offset+3];
306 *(bufptr+cOffset+2) = ptr[offset+2];
307 *(bufptr+cOffset+3) = ptr[offset+1];
314 DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
320 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
322 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
328 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
336 bool NativeImageSourceTizen::EncodeToFile(const std::string& filename) const
338 std::vector< unsigned char > pixbuf;
339 unsigned int width(0), height(0);
340 Pixel::Format pixelFormat;
342 if(GetPixels(pixbuf, width, height, pixelFormat))
344 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
349 void NativeImageSourceTizen::SetSource( Any source )
351 Dali::Mutex::ScopedLock lock( mMutex );
355 mOwnTbmSurface = false;
356 mTbmSurface = GetSurfaceFromAny( source );
358 if( mTbmSurface != NULL )
361 tbm_surface_internal_ref( mTbmSurface );
362 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
363 mWidth = tbm_surface_get_width( mTbmSurface );
364 mHeight = tbm_surface_get_height( mTbmSurface );
368 bool NativeImageSourceTizen::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
372 tbm_format format = TBM_FORMAT_RGB888;
376 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
378 format = TBM_FORMAT_ARGB8888;
381 case Dali::NativeImageSource::COLOR_DEPTH_8:
383 format = TBM_FORMAT_C8;
386 case Dali::NativeImageSource::COLOR_DEPTH_16:
388 format = TBM_FORMAT_RGB565;
391 case Dali::NativeImageSource::COLOR_DEPTH_24:
393 format = TBM_FORMAT_RGB888;
396 case Dali::NativeImageSource::COLOR_DEPTH_32:
398 format = TBM_FORMAT_ARGB8888;
403 if( tbm_surface_query_formats( &formats, &formatNum ) )
405 for( unsigned int i = 0; i < formatNum; i++ )
407 if( formats[i] == format )
419 bool NativeImageSourceTizen::GlExtensionCreate()
421 // casting from an unsigned int to a void *, which should then be cast back
422 // to an unsigned int in the driver.
423 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
424 if( !eglBuffer || !tbm_surface_internal_is_valid( mTbmSurface ) )
429 mEglImageExtensions = mEglGraphics->GetImageExtensions();
430 DALI_ASSERT_DEBUG( mEglImageExtensions );
432 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
434 return mEglImageKHR != NULL;
437 void NativeImageSourceTizen::GlExtensionDestroy()
439 Dali::Mutex::ScopedLock lock( mMutex );
442 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
448 uint32_t NativeImageSourceTizen::TargetTexture()
450 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
455 void NativeImageSourceTizen::PrepareTexture()
457 Dali::Mutex::ScopedLock lock( mMutex );
460 void* eglImage = mEglImageKHR;
462 if( GlExtensionCreate() )
467 mEglImageExtensions->DestroyImageKHR( eglImage );
473 const char* NativeImageSourceTizen::GetCustomFragmentPreFix()
475 return FRAGMENT_PREFIX;
478 const char* NativeImageSourceTizen::GetCustomSamplerTypename()
483 int NativeImageSourceTizen::GetEglImageTextureTarget()
485 return GL_TEXTURE_EXTERNAL_OES;
488 bool NativeImageSourceTizen::CheckBlending( tbm_format format )
490 if( mTbmFormat != format )
492 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
494 if( format == FORMATS_BLENDING_REQUIRED[i] )
496 mBlendingRequired = true;
503 return mBlendingRequired;
506 uint8_t* NativeImageSourceTizen::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
508 Dali::Mutex::ScopedLock lock( mMutex );
509 if( mTbmSurface != NULL )
511 tbm_surface_info_s info;
513 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &info) != TBM_SURFACE_ERROR_NONE )
515 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
522 tbm_surface_internal_ref( mTbmSurface );
523 mIsBufferAcquired = true;
525 stride = info.planes[0].stride;
529 return info.planes[0].ptr;
535 bool NativeImageSourceTizen::ReleaseBuffer()
537 Dali::Mutex::ScopedLock lock( mMutex );
539 if( mTbmSurface != NULL )
541 ret = ( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE );
544 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
546 tbm_surface_internal_unref( mTbmSurface );
547 mIsBufferAcquired = false;
553 } // namespace Adaptor
555 } // namespace internal