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(unsigned int width, unsigned int 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( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
84 mOwnTbmSurface( false ),
87 mBlendingRequired( false ),
91 mEglImageExtensions( NULL ),
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_RGBA8888;
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_RGBA8888;
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 NativeImageSourceTizen::~NativeImageSourceTizen()
191 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
193 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
198 if( mTbmSurface != NULL )
200 tbm_surface_internal_unref( mTbmSurface );
205 Any NativeImageSourceTizen::GetNativeImageSource() const
207 return Any( mTbmSurface );
210 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
212 if( mTbmSurface != NULL )
214 tbm_surface_info_s surface_info;
216 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
218 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
226 tbm_format format = surface_info.format;
227 uint32_t stride = surface_info.planes[0].stride;
228 unsigned char* ptr = surface_info.planes[0].ptr;
238 case TBM_FORMAT_RGB888:
241 pixelFormat = Pixel::RGB888;
242 pixbuf.resize( lineSize*height );
243 unsigned char* bufptr = &pixbuf[0];
245 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
247 for( unsigned int c = 0; c < width; ++c )
250 offset = cOffset + r*stride;
251 *(bufptr+cOffset) = ptr[offset+2];
252 *(bufptr+cOffset+1) = ptr[offset+1];
253 *(bufptr+cOffset+2) = ptr[offset];
258 case TBM_FORMAT_RGBA8888:
261 pixelFormat = Pixel::RGBA8888;
262 pixbuf.resize( lineSize*height );
263 unsigned char* bufptr = &pixbuf[0];
265 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
267 for( unsigned int c = 0; c < width; ++c )
270 offset = cOffset + r*stride;
271 *(bufptr+cOffset) = ptr[offset+3];
272 *(bufptr+cOffset+1) = ptr[offset+2];
273 *(bufptr+cOffset+2) = ptr[offset+1];
274 *(bufptr+cOffset+3) = ptr[offset];
281 DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
287 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
289 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
295 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
303 bool NativeImageSourceTizen::EncodeToFile(const std::string& filename) const
305 std::vector< unsigned char > pixbuf;
306 unsigned int width(0), height(0);
307 Pixel::Format pixelFormat;
309 if(GetPixels(pixbuf, width, height, pixelFormat))
311 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
316 void NativeImageSourceTizen::SetSource( Any source )
320 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
322 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
326 mOwnTbmSurface = false;
330 if( mTbmSurface != NULL )
332 tbm_surface_internal_unref( mTbmSurface );
337 mTbmSurface = GetSurfaceFromAny( source );
339 if( mTbmSurface != NULL )
342 tbm_surface_internal_ref( mTbmSurface );
343 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
344 mWidth = tbm_surface_get_width( mTbmSurface );
345 mHeight = tbm_surface_get_height( mTbmSurface );
349 bool NativeImageSourceTizen::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
353 tbm_format format = TBM_FORMAT_RGB888;
357 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
359 format = TBM_FORMAT_RGBA8888;
362 case Dali::NativeImageSource::COLOR_DEPTH_8:
364 format = TBM_FORMAT_C8;
367 case Dali::NativeImageSource::COLOR_DEPTH_16:
369 format = TBM_FORMAT_RGB565;
372 case Dali::NativeImageSource::COLOR_DEPTH_24:
374 format = TBM_FORMAT_RGB888;
377 case Dali::NativeImageSource::COLOR_DEPTH_32:
379 format = TBM_FORMAT_RGBA8888;
384 if( tbm_surface_query_formats( &formats, &formatNum ) )
386 for( unsigned int i = 0; i < formatNum; i++ )
388 if( formats[i] == format )
400 bool NativeImageSourceTizen::GlExtensionCreate()
402 // casting from an unsigned int to a void *, which should then be cast back
403 // to an unsigned int in the driver.
404 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
410 mEglImageExtensions = mEglGraphics->GetImageExtensions();
411 DALI_ASSERT_DEBUG( mEglImageExtensions );
413 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
415 return mEglImageKHR != NULL;
418 void NativeImageSourceTizen::GlExtensionDestroy()
422 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
428 unsigned int NativeImageSourceTizen::TargetTexture()
430 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
435 void NativeImageSourceTizen::PrepareTexture()
439 void* eglImage = mEglImageKHR;
441 if( GlExtensionCreate() )
446 mEglImageExtensions->DestroyImageKHR( eglImage );
452 const char* NativeImageSourceTizen::GetCustomFragmentPreFix()
454 return FRAGMENT_PREFIX;
457 const char* NativeImageSourceTizen::GetCustomSamplerTypename()
462 int NativeImageSourceTizen::GetEglImageTextureTarget()
464 return GL_TEXTURE_EXTERNAL_OES;
467 bool NativeImageSourceTizen::CheckBlending( tbm_format format )
469 if( mTbmFormat != format )
471 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
473 if( format == FORMATS_BLENDING_REQUIRED[i] )
475 mBlendingRequired = true;
482 return mBlendingRequired;
485 } // namespace Adaptor
487 } // namespace internal