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/gles20/egl-factory.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 ),
90 mEglImageExtensions( NULL ),
93 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
94 EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
95 mEglImageExtensions = eglFactory.GetImageExtensions();
96 DALI_ASSERT_DEBUG( mEglImageExtensions );
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_RGBA8888;
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_RGBA8888;
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 NativeImageSourceTizen::~NativeImageSourceTizen()
190 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
192 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
197 if( mTbmSurface != NULL )
199 tbm_surface_internal_unref( mTbmSurface );
204 Any NativeImageSourceTizen::GetNativeImageSource() const
206 return Any( mTbmSurface );
209 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
211 if( mTbmSurface != NULL )
213 tbm_surface_info_s surface_info;
215 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
217 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
225 tbm_format format = surface_info.format;
226 uint32_t stride = surface_info.planes[0].stride;
227 unsigned char* ptr = surface_info.planes[0].ptr;
237 case TBM_FORMAT_RGB888:
240 pixelFormat = Pixel::RGB888;
241 pixbuf.resize( lineSize*height );
242 unsigned char* bufptr = &pixbuf[0];
244 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
246 for( unsigned int c = 0; c < width; ++c )
249 offset = cOffset + r*stride;
250 *(bufptr+cOffset) = ptr[offset+2];
251 *(bufptr+cOffset+1) = ptr[offset+1];
252 *(bufptr+cOffset+2) = ptr[offset];
257 case TBM_FORMAT_RGBA8888:
260 pixelFormat = Pixel::RGBA8888;
261 pixbuf.resize( lineSize*height );
262 unsigned char* bufptr = &pixbuf[0];
264 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
266 for( unsigned int c = 0; c < width; ++c )
269 offset = cOffset + r*stride;
270 *(bufptr+cOffset) = ptr[offset+3];
271 *(bufptr+cOffset+1) = ptr[offset+2];
272 *(bufptr+cOffset+2) = ptr[offset+1];
273 *(bufptr+cOffset+3) = ptr[offset];
280 DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
286 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
288 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
294 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
302 bool NativeImageSourceTizen::EncodeToFile(const std::string& filename) const
304 std::vector< unsigned char > pixbuf;
305 unsigned int width(0), height(0);
306 Pixel::Format pixelFormat;
308 if(GetPixels(pixbuf, width, height, pixelFormat))
310 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
315 void NativeImageSourceTizen::SetSource( Any source )
319 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
321 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
325 mOwnTbmSurface = false;
329 if( mTbmSurface != NULL )
331 tbm_surface_internal_unref( mTbmSurface );
336 mTbmSurface = GetSurfaceFromAny( source );
338 if( mTbmSurface != NULL )
341 tbm_surface_internal_ref( mTbmSurface );
342 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
343 mWidth = tbm_surface_get_width( mTbmSurface );
344 mHeight = tbm_surface_get_height( mTbmSurface );
348 bool NativeImageSourceTizen::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
352 tbm_format format = TBM_FORMAT_RGB888;
356 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
358 format = TBM_FORMAT_RGBA8888;
361 case Dali::NativeImageSource::COLOR_DEPTH_8:
363 format = TBM_FORMAT_C8;
366 case Dali::NativeImageSource::COLOR_DEPTH_16:
368 format = TBM_FORMAT_RGB565;
371 case Dali::NativeImageSource::COLOR_DEPTH_24:
373 format = TBM_FORMAT_RGB888;
376 case Dali::NativeImageSource::COLOR_DEPTH_32:
378 format = TBM_FORMAT_RGBA8888;
383 if( tbm_surface_query_formats( &formats, &formatNum ) )
385 for( unsigned int i = 0; i < formatNum; i++ )
387 if( formats[i] == format )
399 bool NativeImageSourceTizen::GlExtensionCreate()
401 // casting from an unsigned int to a void *, which should then be cast back
402 // to an unsigned int in the driver.
403 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
409 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
411 return mEglImageKHR != NULL;
414 void NativeImageSourceTizen::GlExtensionDestroy()
418 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
424 unsigned int NativeImageSourceTizen::TargetTexture()
426 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
431 void NativeImageSourceTizen::PrepareTexture()
435 void* eglImage = mEglImageKHR;
437 if( GlExtensionCreate() )
442 mEglImageExtensions->DestroyImageKHR( eglImage );
448 const char* NativeImageSourceTizen::GetCustomFragmentPreFix()
450 return FRAGMENT_PREFIX;
453 const char* NativeImageSourceTizen::GetCustomSamplerTypename()
458 int NativeImageSourceTizen::GetEglImageTextureTarget()
460 return GL_TEXTURE_EXTERNAL_OES;
463 bool NativeImageSourceTizen::CheckBlending( tbm_format format )
465 if( mTbmFormat != format )
467 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
469 if( format == FORMATS_BLENDING_REQUIRED[i] )
471 mBlendingRequired = true;
478 return mBlendingRequired;
481 } // namespace Adaptor
483 } // namespace internal