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(unsigned int width, unsigned int 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( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
85 mOwnTbmSurface( false ),
88 mBlendingRequired( false ),
91 mEglImageExtensions( NULL ),
95 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
96 EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
97 mEglImageExtensions = eglFactory.GetImageExtensions();
98 DALI_ASSERT_DEBUG( mEglImageExtensions );
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 );
203 if (mNotification != NULL) {
204 TriggerEventInterface* triggerEvent = static_cast<TriggerEventInterface* >(mNotification);
205 triggerEvent->Trigger();
211 Any NativeImageSourceTizen::GetNativeImageSource() const
213 return Any( mTbmSurface );
217 void NativeImageSourceTizen::SetDestructorNotification(void* notification)
219 mNotification = notification;
222 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
224 if( mTbmSurface != NULL )
226 tbm_surface_info_s surface_info;
228 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
230 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
238 tbm_format format = surface_info.format;
239 uint32_t stride = surface_info.planes[0].stride;
240 unsigned char* ptr = surface_info.planes[0].ptr;
250 case TBM_FORMAT_RGB888:
253 pixelFormat = Pixel::RGB888;
254 pixbuf.resize( lineSize*height );
255 unsigned char* bufptr = &pixbuf[0];
257 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
259 for( unsigned int c = 0; c < width; ++c )
262 offset = cOffset + r*stride;
263 *(bufptr+cOffset) = ptr[offset+2];
264 *(bufptr+cOffset+1) = ptr[offset+1];
265 *(bufptr+cOffset+2) = ptr[offset];
270 case TBM_FORMAT_RGBA8888:
273 pixelFormat = Pixel::RGBA8888;
274 pixbuf.resize( lineSize*height );
275 unsigned char* bufptr = &pixbuf[0];
277 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
279 for( unsigned int c = 0; c < width; ++c )
282 offset = cOffset + r*stride;
283 *(bufptr+cOffset) = ptr[offset+3];
284 *(bufptr+cOffset+1) = ptr[offset+2];
285 *(bufptr+cOffset+2) = ptr[offset+1];
286 *(bufptr+cOffset+3) = ptr[offset];
293 DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
299 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
301 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
307 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
315 bool NativeImageSourceTizen::EncodeToFile(const std::string& filename) const
317 std::vector< unsigned char > pixbuf;
318 unsigned int width(0), height(0);
319 Pixel::Format pixelFormat;
321 if(GetPixels(pixbuf, width, height, pixelFormat))
323 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
328 void NativeImageSourceTizen::SetSource( Any source )
332 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
334 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
338 mOwnTbmSurface = false;
342 if( mTbmSurface != NULL )
344 tbm_surface_internal_unref( mTbmSurface );
349 mTbmSurface = GetSurfaceFromAny( source );
351 if( mTbmSurface != NULL )
354 tbm_surface_internal_ref( mTbmSurface );
355 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
356 mWidth = tbm_surface_get_width( mTbmSurface );
357 mHeight = tbm_surface_get_height( mTbmSurface );
361 bool NativeImageSourceTizen::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
365 tbm_format format = TBM_FORMAT_RGB888;
369 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
371 format = TBM_FORMAT_RGBA8888;
374 case Dali::NativeImageSource::COLOR_DEPTH_8:
376 format = TBM_FORMAT_C8;
379 case Dali::NativeImageSource::COLOR_DEPTH_16:
381 format = TBM_FORMAT_RGB565;
384 case Dali::NativeImageSource::COLOR_DEPTH_24:
386 format = TBM_FORMAT_RGB888;
389 case Dali::NativeImageSource::COLOR_DEPTH_32:
391 format = TBM_FORMAT_RGBA8888;
396 if( tbm_surface_query_formats( &formats, &formatNum ) )
398 for( unsigned int i = 0; i < formatNum; i++ )
400 if( formats[i] == format )
412 bool NativeImageSourceTizen::GlExtensionCreate()
414 // casting from an unsigned int to a void *, which should then be cast back
415 // to an unsigned int in the driver.
416 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
422 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
424 return mEglImageKHR != NULL;
427 void NativeImageSourceTizen::GlExtensionDestroy()
431 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
437 unsigned int NativeImageSourceTizen::TargetTexture()
439 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
444 void NativeImageSourceTizen::PrepareTexture()
448 void* eglImage = mEglImageKHR;
450 if( GlExtensionCreate() )
455 mEglImageExtensions->DestroyImageKHR( eglImage );
461 const char* NativeImageSourceTizen::GetCustomFragmentPreFix()
463 return FRAGMENT_PREFIX;
466 const char* NativeImageSourceTizen::GetCustomSamplerTypename()
471 int NativeImageSourceTizen::GetEglImageTextureTarget()
473 return GL_TEXTURE_EXTERNAL_OES;
476 bool NativeImageSourceTizen::CheckBlending( tbm_format format )
478 if( mTbmFormat != format )
480 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
482 if( format == FORMATS_BLENDING_REQUIRED[i] )
484 mBlendingRequired = true;
491 return mBlendingRequired;
494 } // namespace Adaptor
496 } // namespace internal