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 ),
92 mResourceDestructionCallback()
94 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
96 GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
97 mEglGraphics = static_cast<EglGraphics *>(graphics);
99 mTbmSurface = GetSurfaceFromAny( nativeImageSource );
101 if( mTbmSurface != NULL )
103 tbm_surface_internal_ref( mTbmSurface );
104 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
105 mWidth = tbm_surface_get_width( mTbmSurface );
106 mHeight = tbm_surface_get_height( mTbmSurface );
110 void NativeImageSourceTizen::Initialize()
112 if( mTbmSurface != NULL || mWidth == 0 || mHeight == 0 )
117 tbm_format format = TBM_FORMAT_RGB888;
120 switch( mColorDepth )
122 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
124 format = TBM_FORMAT_ARGB8888;
128 case Dali::NativeImageSource::COLOR_DEPTH_8:
130 format = TBM_FORMAT_C8;
134 case Dali::NativeImageSource::COLOR_DEPTH_16:
136 format = TBM_FORMAT_RGB565;
140 case Dali::NativeImageSource::COLOR_DEPTH_24:
142 format = TBM_FORMAT_RGB888;
146 case Dali::NativeImageSource::COLOR_DEPTH_32:
148 format = TBM_FORMAT_ARGB8888;
154 DALI_LOG_WARNING( "Wrong color depth.\n" );
159 // set whether blending is required according to pixel format based on the depth
160 /* default pixel format is RGB888
161 If depth = 8, Pixel::A8;
162 If depth = 16, Pixel::RGB565;
163 If depth = 32, Pixel::RGBA8888 */
164 mBlendingRequired = ( depth == 32 || depth == 8 );
166 mTbmSurface = tbm_surface_create( mWidth, mHeight, format );
167 mOwnTbmSurface = true;
170 tbm_surface_h NativeImageSourceTizen::GetSurfaceFromAny( Any source ) const
177 if( source.GetType() == typeid( tbm_surface_h ) )
179 return AnyCast< tbm_surface_h >( source );
187 void NativeImageSourceTizen::DestroySurface()
191 if( mIsBufferAcquired )
197 if( tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
199 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
204 tbm_surface_internal_unref( mTbmSurface );
209 NativeImageSourceTizen::~NativeImageSourceTizen()
214 Any NativeImageSourceTizen::GetNativeImageSource() const
216 return Any( mTbmSurface );
219 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
221 Dali::Mutex::ScopedLock lock( mMutex );
222 if( mTbmSurface != NULL )
224 tbm_surface_info_s surface_info;
226 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
228 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
236 tbm_format format = surface_info.format;
237 uint32_t stride = surface_info.planes[0].stride;
238 unsigned char* ptr = surface_info.planes[0].ptr;
248 case TBM_FORMAT_RGB888:
251 pixelFormat = Pixel::RGB888;
252 pixbuf.resize( lineSize*height );
253 unsigned char* bufptr = &pixbuf[0];
255 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
257 for( unsigned int c = 0; c < width; ++c )
260 offset = cOffset + r*stride;
261 *(bufptr+cOffset) = ptr[offset+2];
262 *(bufptr+cOffset+1) = ptr[offset+1];
263 *(bufptr+cOffset+2) = ptr[offset];
268 case TBM_FORMAT_RGBA8888:
271 pixelFormat = Pixel::RGBA8888;
272 pixbuf.resize( lineSize*height );
273 unsigned char* bufptr = &pixbuf[0];
275 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
277 for( unsigned int c = 0; c < width; ++c )
280 offset = cOffset + r*stride;
281 *(bufptr+cOffset) = ptr[offset+3];
282 *(bufptr+cOffset+1) = ptr[offset+2];
283 *(bufptr+cOffset+2) = ptr[offset+1];
284 *(bufptr+cOffset+3) = ptr[offset];
289 case TBM_FORMAT_ARGB8888:
292 pixelFormat = Pixel::RGBA8888;
293 pixbuf.resize( lineSize*height );
294 unsigned char* bufptr = &pixbuf[0];
296 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
298 for( unsigned int c = 0; c < width; ++c )
301 offset = cOffset + r*stride;
302 *(bufptr+cOffset) = ptr[offset+2];
303 *(bufptr+cOffset+1) = ptr[offset+1];
304 *(bufptr+cOffset+2) = ptr[offset];
305 *(bufptr+cOffset+3) = ptr[offset+3];
312 DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
318 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
320 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
326 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
334 void NativeImageSourceTizen::SetSource( Any source )
336 Dali::Mutex::ScopedLock lock( mMutex );
340 mOwnTbmSurface = false;
341 mTbmSurface = GetSurfaceFromAny( source );
343 if( mTbmSurface != NULL )
346 tbm_surface_internal_ref( mTbmSurface );
347 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
348 mWidth = tbm_surface_get_width( mTbmSurface );
349 mHeight = tbm_surface_get_height( mTbmSurface );
353 bool NativeImageSourceTizen::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
357 tbm_format format = TBM_FORMAT_RGB888;
361 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
363 format = TBM_FORMAT_ARGB8888;
366 case Dali::NativeImageSource::COLOR_DEPTH_8:
368 format = TBM_FORMAT_C8;
371 case Dali::NativeImageSource::COLOR_DEPTH_16:
373 format = TBM_FORMAT_RGB565;
376 case Dali::NativeImageSource::COLOR_DEPTH_24:
378 format = TBM_FORMAT_RGB888;
381 case Dali::NativeImageSource::COLOR_DEPTH_32:
383 format = TBM_FORMAT_ARGB8888;
388 if( tbm_surface_query_formats( &formats, &formatNum ) )
390 for( unsigned int i = 0; i < formatNum; i++ )
392 if( formats[i] == format )
404 bool NativeImageSourceTizen::CreateResource()
406 // casting from an unsigned int to a void *, which should then be cast back
407 // to an unsigned int in the driver.
408 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
409 if( !eglBuffer || !tbm_surface_internal_is_valid( mTbmSurface ) )
414 mEglImageExtensions = mEglGraphics->GetImageExtensions();
415 DALI_ASSERT_DEBUG( mEglImageExtensions );
417 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
419 return mEglImageKHR != NULL;
422 void NativeImageSourceTizen::DestroyResource()
424 Dali::Mutex::ScopedLock lock( mMutex );
427 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
432 if(mResourceDestructionCallback)
434 mResourceDestructionCallback->Trigger();
438 uint32_t NativeImageSourceTizen::TargetTexture()
440 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
445 void NativeImageSourceTizen::PrepareTexture()
447 Dali::Mutex::ScopedLock lock( mMutex );
450 void* eglImage = mEglImageKHR;
452 if( CreateResource() )
457 mEglImageExtensions->DestroyImageKHR( eglImage );
463 const char* NativeImageSourceTizen::GetCustomFragmentPrefix() const
465 return FRAGMENT_PREFIX;
468 const char* NativeImageSourceTizen::GetCustomSamplerTypename() const
473 int NativeImageSourceTizen::GetTextureTarget() const
475 return GL_TEXTURE_EXTERNAL_OES;
478 Any NativeImageSourceTizen::GetNativeImageHandle() const
480 return GetNativeImageSource();
483 bool NativeImageSourceTizen::SourceChanged() const
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;
552 void NativeImageSourceTizen::SetResourceDestructionCallback(EventThreadCallback* callback)
554 mResourceDestructionCallback = std::unique_ptr<EventThreadCallback>(callback);
557 } // namespace Adaptor
559 } // namespace internal