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 <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 <gl/egl-image-extensions.h>
29 #include <gl/egl-factory.h>
30 #include <adaptor-impl.h>
31 #include <render-surface.h>
32 #include <trigger-event-factory.h>
34 // Allow this to be encoded and saved:
35 #include <bitmap-saver.h>
48 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
49 const char* SAMPLER_TYPE = "samplerExternalOES";
51 tbm_format FORMATS_BLENDING_REQUIRED[] = {
52 TBM_FORMAT_ARGB4444, TBM_FORMAT_ABGR4444,
53 TBM_FORMAT_RGBA4444, TBM_FORMAT_BGRA4444,
54 TBM_FORMAT_RGBX5551, TBM_FORMAT_BGRX5551,
55 TBM_FORMAT_ARGB1555, TBM_FORMAT_ABGR1555,
56 TBM_FORMAT_RGBA5551, TBM_FORMAT_BGRA5551,
57 TBM_FORMAT_ARGB8888, TBM_FORMAT_ABGR8888,
58 TBM_FORMAT_RGBA8888, TBM_FORMAT_BGRA8888,
59 TBM_FORMAT_ARGB2101010, TBM_FORMAT_ABGR2101010,
60 TBM_FORMAT_RGBA1010102, TBM_FORMAT_BGRA1010102
63 const int NUM_FORMATS_BLENDING_REQUIRED = 18;
67 using Dali::Integration::PixelBuffer;
69 NativeImageSourceTizen* NativeImageSourceTizen::New( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
71 NativeImageSourceTizen* image = new NativeImageSourceTizen( width, height, depth, nativeImageSource );
72 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
82 NativeImageSourceTizen::NativeImageSourceTizen( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
85 mOwnTbmSurface( false ),
88 mBlendingRequired( false ),
92 mEglImageExtensions( NULL ),
96 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
97 mEglFactory = &( Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory() );
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 );
202 if (mNotification != NULL) {
203 TriggerEventInterface* triggerEvent = static_cast<TriggerEventInterface* >(mNotification);
204 triggerEvent->Trigger();
210 Any NativeImageSourceTizen::GetNativeImageSource() const
212 return Any( mTbmSurface );
216 void NativeImageSourceTizen::SetDestructorNotification(void* notification)
218 mNotification = notification;
221 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
223 if( mTbmSurface != NULL )
225 tbm_surface_info_s surface_info;
227 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
229 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
237 tbm_format format = surface_info.format;
238 uint32_t stride = surface_info.planes[0].stride;
239 unsigned char* ptr = surface_info.planes[0].ptr;
249 case TBM_FORMAT_RGB888:
252 pixelFormat = Pixel::RGB888;
253 pixbuf.resize( lineSize*height );
254 unsigned char* bufptr = &pixbuf[0];
256 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
258 for( unsigned int c = 0; c < width; ++c )
261 offset = cOffset + r*stride;
262 *(bufptr+cOffset) = ptr[offset+2];
263 *(bufptr+cOffset+1) = ptr[offset+1];
264 *(bufptr+cOffset+2) = ptr[offset];
269 case TBM_FORMAT_RGBA8888:
272 pixelFormat = Pixel::RGBA8888;
273 pixbuf.resize( lineSize*height );
274 unsigned char* bufptr = &pixbuf[0];
276 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
278 for( unsigned int c = 0; c < width; ++c )
281 offset = cOffset + r*stride;
282 *(bufptr+cOffset) = ptr[offset+3];
283 *(bufptr+cOffset+1) = ptr[offset+2];
284 *(bufptr+cOffset+2) = ptr[offset+1];
285 *(bufptr+cOffset+3) = ptr[offset];
292 DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
298 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
300 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
306 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
314 bool NativeImageSourceTizen::EncodeToFile(const std::string& filename) const
316 std::vector< unsigned char > pixbuf;
317 unsigned int width(0), height(0);
318 Pixel::Format pixelFormat;
320 if(GetPixels(pixbuf, width, height, pixelFormat))
322 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
327 void NativeImageSourceTizen::SetSource( Any source )
331 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
333 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
337 mOwnTbmSurface = false;
341 if( mTbmSurface != NULL )
343 tbm_surface_internal_unref( mTbmSurface );
348 mTbmSurface = GetSurfaceFromAny( source );
350 if( mTbmSurface != NULL )
353 tbm_surface_internal_ref( mTbmSurface );
354 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
355 mWidth = tbm_surface_get_width( mTbmSurface );
356 mHeight = tbm_surface_get_height( mTbmSurface );
360 bool NativeImageSourceTizen::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
364 tbm_format format = TBM_FORMAT_RGB888;
368 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
370 format = TBM_FORMAT_RGBA8888;
373 case Dali::NativeImageSource::COLOR_DEPTH_8:
375 format = TBM_FORMAT_C8;
378 case Dali::NativeImageSource::COLOR_DEPTH_16:
380 format = TBM_FORMAT_RGB565;
383 case Dali::NativeImageSource::COLOR_DEPTH_24:
385 format = TBM_FORMAT_RGB888;
388 case Dali::NativeImageSource::COLOR_DEPTH_32:
390 format = TBM_FORMAT_RGBA8888;
395 if( tbm_surface_query_formats( &formats, &formatNum ) )
397 for( unsigned int i = 0; i < formatNum; i++ )
399 if( formats[i] == format )
411 bool NativeImageSourceTizen::GlExtensionCreate()
413 // casting from an unsigned int to a void *, which should then be cast back
414 // to an unsigned int in the driver.
415 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
421 mEglImageExtensions = mEglFactory->GetImageExtensions();
422 DALI_ASSERT_DEBUG( mEglImageExtensions );
424 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
426 return mEglImageKHR != NULL;
429 void NativeImageSourceTizen::GlExtensionDestroy()
433 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
439 uint32_t NativeImageSourceTizen::TargetTexture()
441 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
446 void NativeImageSourceTizen::PrepareTexture()
450 void* eglImage = mEglImageKHR;
452 if( GlExtensionCreate() )
457 mEglImageExtensions->DestroyImageKHR( eglImage );
463 const char* NativeImageSourceTizen::GetCustomFragmentPreFix()
465 return FRAGMENT_PREFIX;
468 const char* NativeImageSourceTizen::GetCustomSamplerTypename()
473 int NativeImageSourceTizen::GetEglImageTextureTarget()
475 return GL_TEXTURE_EXTERNAL_OES;
478 bool NativeImageSourceTizen::CheckBlending( tbm_format format )
480 if( mTbmFormat != format )
482 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
484 if( format == FORMATS_BLENDING_REQUIRED[i] )
486 mBlendingRequired = true;
493 return mBlendingRequired;
496 } // namespace Adaptor
498 } // namespace internal