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>
44 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
45 const char* SAMPLER_TYPE = "samplerExternalOES";
47 tbm_format FORMATS_BLENDING_REQUIRED[] = {
48 TBM_FORMAT_ARGB4444, TBM_FORMAT_ABGR4444,
49 TBM_FORMAT_RGBA4444, TBM_FORMAT_BGRA4444,
50 TBM_FORMAT_RGBX5551, TBM_FORMAT_BGRX5551,
51 TBM_FORMAT_ARGB1555, TBM_FORMAT_ABGR1555,
52 TBM_FORMAT_RGBA5551, TBM_FORMAT_BGRA5551,
53 TBM_FORMAT_ARGB8888, TBM_FORMAT_ABGR8888,
54 TBM_FORMAT_RGBA8888, TBM_FORMAT_BGRA8888,
55 TBM_FORMAT_ARGB2101010, TBM_FORMAT_ABGR2101010,
56 TBM_FORMAT_RGBA1010102, TBM_FORMAT_BGRA1010102
59 const int NUM_FORMATS_BLENDING_REQUIRED = 18;
63 using Dali::Integration::PixelBuffer;
65 NativeImageSourceTizen* NativeImageSourceTizen::New( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
67 NativeImageSourceTizen* image = new NativeImageSourceTizen( width, height, depth, nativeImageSource );
68 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
78 NativeImageSourceTizen::NativeImageSourceTizen( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
81 mOwnTbmSurface( false ),
84 mBlendingRequired( false ),
88 mEglImageExtensions( NULL ),
91 mIsBufferAcquired( false )
93 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
95 GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
96 mEglGraphics = static_cast<EglGraphics *>(graphics);
98 mTbmSurface = GetSurfaceFromAny( nativeImageSource );
100 if( mTbmSurface != NULL )
102 tbm_surface_internal_ref( mTbmSurface );
103 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
104 mWidth = tbm_surface_get_width( mTbmSurface );
105 mHeight = tbm_surface_get_height( mTbmSurface );
109 void NativeImageSourceTizen::Initialize()
111 if( mTbmSurface != NULL || mWidth == 0 || mHeight == 0 )
116 tbm_format format = TBM_FORMAT_RGB888;
119 switch( mColorDepth )
121 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
123 format = TBM_FORMAT_ARGB8888;
127 case Dali::NativeImageSource::COLOR_DEPTH_8:
129 format = TBM_FORMAT_C8;
133 case Dali::NativeImageSource::COLOR_DEPTH_16:
135 format = TBM_FORMAT_RGB565;
139 case Dali::NativeImageSource::COLOR_DEPTH_24:
141 format = TBM_FORMAT_RGB888;
145 case Dali::NativeImageSource::COLOR_DEPTH_32:
147 format = TBM_FORMAT_ARGB8888;
153 DALI_LOG_WARNING( "Wrong color depth.\n" );
158 // set whether blending is required according to pixel format based on the depth
159 /* default pixel format is RGB888
160 If depth = 8, Pixel::A8;
161 If depth = 16, Pixel::RGB565;
162 If depth = 32, Pixel::RGBA8888 */
163 mBlendingRequired = ( depth == 32 || depth == 8 );
165 mTbmSurface = tbm_surface_create( mWidth, mHeight, format );
166 mOwnTbmSurface = true;
169 tbm_surface_h NativeImageSourceTizen::GetSurfaceFromAny( Any source ) const
176 if( source.GetType() == typeid( tbm_surface_h ) )
178 return AnyCast< tbm_surface_h >( source );
186 void NativeImageSourceTizen::DestroySurface()
190 if( mIsBufferAcquired )
196 if( tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
198 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
203 tbm_surface_internal_unref( mTbmSurface );
208 NativeImageSourceTizen::~NativeImageSourceTizen()
213 Any NativeImageSourceTizen::GetNativeImageSource() const
215 return Any( mTbmSurface );
218 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
220 Dali::Mutex::ScopedLock lock( mMutex );
221 if( mTbmSurface != NULL )
223 tbm_surface_info_s surface_info;
225 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
227 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
235 tbm_format format = surface_info.format;
236 uint32_t stride = surface_info.planes[0].stride;
237 unsigned char* ptr = surface_info.planes[0].ptr;
247 case TBM_FORMAT_RGB888:
250 pixelFormat = Pixel::RGB888;
251 pixbuf.resize( lineSize*height );
252 unsigned char* bufptr = &pixbuf[0];
254 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
256 for( unsigned int c = 0; c < width; ++c )
259 offset = cOffset + r*stride;
260 *(bufptr+cOffset) = ptr[offset+2];
261 *(bufptr+cOffset+1) = ptr[offset+1];
262 *(bufptr+cOffset+2) = ptr[offset];
267 case TBM_FORMAT_RGBA8888:
270 pixelFormat = Pixel::RGBA8888;
271 pixbuf.resize( lineSize*height );
272 unsigned char* bufptr = &pixbuf[0];
274 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
276 for( unsigned int c = 0; c < width; ++c )
279 offset = cOffset + r*stride;
280 *(bufptr+cOffset) = ptr[offset+3];
281 *(bufptr+cOffset+1) = ptr[offset+2];
282 *(bufptr+cOffset+2) = ptr[offset+1];
283 *(bufptr+cOffset+3) = ptr[offset];
288 case TBM_FORMAT_ARGB8888:
291 pixelFormat = Pixel::RGBA8888;
292 pixbuf.resize( lineSize*height );
293 unsigned char* bufptr = &pixbuf[0];
295 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
297 for( unsigned int c = 0; c < width; ++c )
300 offset = cOffset + r*stride;
301 *(bufptr+cOffset) = ptr[offset+2];
302 *(bufptr+cOffset+1) = ptr[offset+1];
303 *(bufptr+cOffset+2) = ptr[offset];
304 *(bufptr+cOffset+3) = ptr[offset+3];
311 DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
317 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
319 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
325 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
333 void NativeImageSourceTizen::SetSource( Any source )
335 Dali::Mutex::ScopedLock lock( mMutex );
339 mOwnTbmSurface = false;
340 mTbmSurface = GetSurfaceFromAny( source );
342 if( mTbmSurface != NULL )
345 tbm_surface_internal_ref( mTbmSurface );
346 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
347 mWidth = tbm_surface_get_width( mTbmSurface );
348 mHeight = tbm_surface_get_height( mTbmSurface );
352 bool NativeImageSourceTizen::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
356 tbm_format format = TBM_FORMAT_RGB888;
360 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
362 format = TBM_FORMAT_ARGB8888;
365 case Dali::NativeImageSource::COLOR_DEPTH_8:
367 format = TBM_FORMAT_C8;
370 case Dali::NativeImageSource::COLOR_DEPTH_16:
372 format = TBM_FORMAT_RGB565;
375 case Dali::NativeImageSource::COLOR_DEPTH_24:
377 format = TBM_FORMAT_RGB888;
380 case Dali::NativeImageSource::COLOR_DEPTH_32:
382 format = TBM_FORMAT_ARGB8888;
387 if( tbm_surface_query_formats( &formats, &formatNum ) )
389 for( unsigned int i = 0; i < formatNum; i++ )
391 if( formats[i] == format )
403 bool NativeImageSourceTizen::GlExtensionCreate()
405 // casting from an unsigned int to a void *, which should then be cast back
406 // to an unsigned int in the driver.
407 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
408 if( !eglBuffer || !tbm_surface_internal_is_valid( mTbmSurface ) )
413 mEglImageExtensions = mEglGraphics->GetImageExtensions();
414 DALI_ASSERT_DEBUG( mEglImageExtensions );
416 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
418 return mEglImageKHR != NULL;
421 void NativeImageSourceTizen::GlExtensionDestroy()
423 Dali::Mutex::ScopedLock lock( mMutex );
426 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
432 uint32_t NativeImageSourceTizen::TargetTexture()
434 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
439 void NativeImageSourceTizen::PrepareTexture()
441 Dali::Mutex::ScopedLock lock( mMutex );
444 void* eglImage = mEglImageKHR;
446 if( GlExtensionCreate() )
451 mEglImageExtensions->DestroyImageKHR( eglImage );
457 const char* NativeImageSourceTizen::GetCustomFragmentPreFix()
459 return FRAGMENT_PREFIX;
462 const char* NativeImageSourceTizen::GetCustomSamplerTypename()
467 int NativeImageSourceTizen::GetEglImageTextureTarget()
469 return GL_TEXTURE_EXTERNAL_OES;
472 bool NativeImageSourceTizen::CheckBlending( tbm_format format )
474 if( mTbmFormat != format )
476 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
478 if( format == FORMATS_BLENDING_REQUIRED[i] )
480 mBlendingRequired = true;
487 return mBlendingRequired;
490 uint8_t* NativeImageSourceTizen::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
492 Dali::Mutex::ScopedLock lock( mMutex );
493 if( mTbmSurface != NULL )
495 tbm_surface_info_s info;
497 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &info) != TBM_SURFACE_ERROR_NONE )
499 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
506 tbm_surface_internal_ref( mTbmSurface );
507 mIsBufferAcquired = true;
509 stride = info.planes[0].stride;
513 return info.planes[0].ptr;
519 bool NativeImageSourceTizen::ReleaseBuffer()
521 Dali::Mutex::ScopedLock lock( mMutex );
523 if( mTbmSurface != NULL )
525 ret = ( tbm_surface_unmap( mTbmSurface ) == TBM_SURFACE_ERROR_NONE );
528 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
530 tbm_surface_internal_unref( mTbmSurface );
531 mIsBufferAcquired = false;
537 } // namespace Adaptor
539 } // namespace internal