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-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>
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 ),
94 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
96 GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
97 auto eglGraphics = static_cast<EglGraphics *>(graphics);
99 mEglImageExtensions = eglGraphics->GetImageExtensions();
101 DALI_ASSERT_DEBUG( mEglImageExtensions );
103 mTbmSurface = GetSurfaceFromAny( nativeImageSource );
105 if( mTbmSurface != NULL )
107 tbm_surface_internal_ref( mTbmSurface );
108 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
109 mWidth = tbm_surface_get_width( mTbmSurface );
110 mHeight = tbm_surface_get_height( mTbmSurface );
114 void NativeImageSourceTizen::Initialize()
116 if( mTbmSurface != NULL || mWidth == 0 || mHeight == 0 )
121 tbm_format format = TBM_FORMAT_RGB888;
124 switch( mColorDepth )
126 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
128 format = TBM_FORMAT_RGBA8888;
132 case Dali::NativeImageSource::COLOR_DEPTH_8:
134 format = TBM_FORMAT_C8;
138 case Dali::NativeImageSource::COLOR_DEPTH_16:
140 format = TBM_FORMAT_RGB565;
144 case Dali::NativeImageSource::COLOR_DEPTH_24:
146 format = TBM_FORMAT_RGB888;
150 case Dali::NativeImageSource::COLOR_DEPTH_32:
152 format = TBM_FORMAT_RGBA8888;
158 DALI_LOG_WARNING( "Wrong color depth.\n" );
163 // set whether blending is required according to pixel format based on the depth
164 /* default pixel format is RGB888
165 If depth = 8, Pixel::A8;
166 If depth = 16, Pixel::RGB565;
167 If depth = 32, Pixel::RGBA8888 */
168 mBlendingRequired = ( depth == 32 || depth == 8 );
170 mTbmSurface = tbm_surface_create( mWidth, mHeight, format );
171 mOwnTbmSurface = true;
174 tbm_surface_h NativeImageSourceTizen::GetSurfaceFromAny( Any source ) const
181 if( source.GetType() == typeid( tbm_surface_h ) )
183 return AnyCast< tbm_surface_h >( source );
191 NativeImageSourceTizen::~NativeImageSourceTizen()
195 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
197 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
202 if( mTbmSurface != NULL )
204 tbm_surface_internal_unref( mTbmSurface );
209 Any NativeImageSourceTizen::GetNativeImageSource() const
211 return Any( mTbmSurface );
214 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
216 if( mTbmSurface != NULL )
218 tbm_surface_info_s surface_info;
220 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
222 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
230 tbm_format format = surface_info.format;
231 uint32_t stride = surface_info.planes[0].stride;
232 unsigned char* ptr = surface_info.planes[0].ptr;
242 case TBM_FORMAT_RGB888:
245 pixelFormat = Pixel::RGB888;
246 pixbuf.resize( lineSize*height );
247 unsigned char* bufptr = &pixbuf[0];
249 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
251 for( unsigned int c = 0; c < width; ++c )
254 offset = cOffset + r*stride;
255 *(bufptr+cOffset) = ptr[offset+2];
256 *(bufptr+cOffset+1) = ptr[offset+1];
257 *(bufptr+cOffset+2) = ptr[offset];
262 case TBM_FORMAT_RGBA8888:
265 pixelFormat = Pixel::RGBA8888;
266 pixbuf.resize( lineSize*height );
267 unsigned char* bufptr = &pixbuf[0];
269 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
271 for( unsigned int c = 0; c < width; ++c )
274 offset = cOffset + r*stride;
275 *(bufptr+cOffset) = ptr[offset+3];
276 *(bufptr+cOffset+1) = ptr[offset+2];
277 *(bufptr+cOffset+2) = ptr[offset+1];
278 *(bufptr+cOffset+3) = ptr[offset];
285 DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
291 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
293 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
299 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
307 bool NativeImageSourceTizen::EncodeToFile(const std::string& filename) const
309 std::vector< unsigned char > pixbuf;
310 unsigned int width(0), height(0);
311 Pixel::Format pixelFormat;
313 if(GetPixels(pixbuf, width, height, pixelFormat))
315 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
320 void NativeImageSourceTizen::SetSource( Any source )
324 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
326 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
330 mOwnTbmSurface = false;
334 if( mTbmSurface != NULL )
336 tbm_surface_internal_unref( mTbmSurface );
341 mTbmSurface = GetSurfaceFromAny( source );
343 if( mTbmSurface != NULL )
346 tbm_surface_internal_ref( mTbmSurface );
347 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
348 mWidth = tbm_surface_get_width( mTbmSurface );
349 mHeight = tbm_surface_get_height( mTbmSurface );
353 bool NativeImageSourceTizen::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
357 tbm_format format = TBM_FORMAT_RGB888;
361 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
363 format = TBM_FORMAT_RGBA8888;
366 case Dali::NativeImageSource::COLOR_DEPTH_8:
368 format = TBM_FORMAT_C8;
371 case Dali::NativeImageSource::COLOR_DEPTH_16:
373 format = TBM_FORMAT_RGB565;
376 case Dali::NativeImageSource::COLOR_DEPTH_24:
378 format = TBM_FORMAT_RGB888;
381 case Dali::NativeImageSource::COLOR_DEPTH_32:
383 format = TBM_FORMAT_RGBA8888;
388 if( tbm_surface_query_formats( &formats, &formatNum ) )
390 for( unsigned int i = 0; i < formatNum; i++ )
392 if( formats[i] == format )
404 bool NativeImageSourceTizen::GlExtensionCreate()
406 // casting from an unsigned int to a void *, which should then be cast back
407 // to an unsigned int in the driver.
408 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
414 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
416 return mEglImageKHR != NULL;
419 void NativeImageSourceTizen::GlExtensionDestroy()
423 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
429 unsigned int NativeImageSourceTizen::TargetTexture()
431 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
436 void NativeImageSourceTizen::PrepareTexture()
440 void* eglImage = mEglImageKHR;
442 if( GlExtensionCreate() )
447 mEglImageExtensions->DestroyImageKHR( eglImage );
453 const char* NativeImageSourceTizen::GetCustomFragmentPreFix()
455 return FRAGMENT_PREFIX;
458 const char* NativeImageSourceTizen::GetCustomSamplerTypename()
463 int NativeImageSourceTizen::GetEglImageTextureTarget()
465 return GL_TEXTURE_EXTERNAL_OES;
468 bool NativeImageSourceTizen::CheckBlending( tbm_format format )
470 if( mTbmFormat != format )
472 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
474 if( format == FORMATS_BLENDING_REQUIRED[i] )
476 mBlendingRequired = true;
483 return mBlendingRequired;
486 } // namespace Adaptor
488 } // namespace internal