2 * Copyright (c) 2014 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/render-surface.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 ),
95 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
97 GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
98 mEglGraphics = static_cast<EglGraphics *>(graphics);
100 mTbmSurface = GetSurfaceFromAny( nativeImageSource );
102 if( mTbmSurface != NULL )
104 tbm_surface_internal_ref( mTbmSurface );
105 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
106 mWidth = tbm_surface_get_width( mTbmSurface );
107 mHeight = tbm_surface_get_height( mTbmSurface );
111 void NativeImageSourceTizen::Initialize()
113 if( mTbmSurface != NULL || mWidth == 0 || mHeight == 0 )
118 tbm_format format = TBM_FORMAT_RGB888;
121 switch( mColorDepth )
123 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
125 format = TBM_FORMAT_RGBA8888;
129 case Dali::NativeImageSource::COLOR_DEPTH_8:
131 format = TBM_FORMAT_C8;
135 case Dali::NativeImageSource::COLOR_DEPTH_16:
137 format = TBM_FORMAT_RGB565;
141 case Dali::NativeImageSource::COLOR_DEPTH_24:
143 format = TBM_FORMAT_RGB888;
147 case Dali::NativeImageSource::COLOR_DEPTH_32:
149 format = TBM_FORMAT_RGBA8888;
155 DALI_LOG_WARNING( "Wrong color depth.\n" );
160 // set whether blending is required according to pixel format based on the depth
161 /* default pixel format is RGB888
162 If depth = 8, Pixel::A8;
163 If depth = 16, Pixel::RGB565;
164 If depth = 32, Pixel::RGBA8888 */
165 mBlendingRequired = ( depth == 32 || depth == 8 );
167 mTbmSurface = tbm_surface_create( mWidth, mHeight, format );
168 mOwnTbmSurface = true;
171 tbm_surface_h NativeImageSourceTizen::GetSurfaceFromAny( Any source ) const
178 if( source.GetType() == typeid( tbm_surface_h ) )
180 return AnyCast< tbm_surface_h >( source );
188 NativeImageSourceTizen::~NativeImageSourceTizen()
192 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
194 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
199 if( mTbmSurface != NULL )
201 tbm_surface_internal_unref( mTbmSurface );
206 Any NativeImageSourceTizen::GetNativeImageSource() const
208 return Any( mTbmSurface );
211 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
213 Dali::Mutex::ScopedLock lock( mMutex );
214 if( mTbmSurface != NULL )
216 tbm_surface_info_s surface_info;
218 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
220 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
228 tbm_format format = surface_info.format;
229 uint32_t stride = surface_info.planes[0].stride;
230 unsigned char* ptr = surface_info.planes[0].ptr;
240 case TBM_FORMAT_RGB888:
243 pixelFormat = Pixel::RGB888;
244 pixbuf.resize( lineSize*height );
245 unsigned char* bufptr = &pixbuf[0];
247 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
249 for( unsigned int c = 0; c < width; ++c )
252 offset = cOffset + r*stride;
253 *(bufptr+cOffset) = ptr[offset+2];
254 *(bufptr+cOffset+1) = ptr[offset+1];
255 *(bufptr+cOffset+2) = ptr[offset];
260 case TBM_FORMAT_RGBA8888:
263 pixelFormat = Pixel::RGBA8888;
264 pixbuf.resize( lineSize*height );
265 unsigned char* bufptr = &pixbuf[0];
267 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
269 for( unsigned int c = 0; c < width; ++c )
272 offset = cOffset + r*stride;
273 *(bufptr+cOffset) = ptr[offset+3];
274 *(bufptr+cOffset+1) = ptr[offset+2];
275 *(bufptr+cOffset+2) = ptr[offset+1];
276 *(bufptr+cOffset+3) = ptr[offset];
283 DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
289 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
291 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
297 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
305 bool NativeImageSourceTizen::EncodeToFile(const std::string& filename) const
307 std::vector< unsigned char > pixbuf;
308 unsigned int width(0), height(0);
309 Pixel::Format pixelFormat;
311 if(GetPixels(pixbuf, width, height, pixelFormat))
313 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
318 void NativeImageSourceTizen::SetSource( Any source )
320 Dali::Mutex::ScopedLock lock( mMutex );
323 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
325 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
329 mOwnTbmSurface = false;
333 if( mTbmSurface != NULL )
335 tbm_surface_internal_unref( mTbmSurface );
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_RGBA8888;
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_RGBA8888;
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 } // namespace Adaptor
492 } // namespace internal