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.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>
33 // Allow this to be encoded and saved:
34 #include <platform-abstractions/tizen/resource-loader/resource-loader.h>
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 NativeImageSource* NativeImageSource::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
71 NativeImageSource* image = new NativeImageSource( width, height, depth, nativeImageSource );
72 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
82 NativeImageSource::NativeImageSource( 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() );
95 EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
96 mEglImageExtensions = eglFactory.GetImageExtensions();
97 DALI_ASSERT_DEBUG( mEglImageExtensions );
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 NativeImageSource::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 NativeImageSource::GetSurfaceFromAny( Any source ) const
177 if( source.GetType() == typeid( tbm_surface_h ) )
179 return AnyCast< tbm_surface_h >( source );
187 NativeImageSource::~NativeImageSource()
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 NativeImageSource::GetNativeImageSource() const
207 return Any( mTbmSurface );
210 bool NativeImageSource::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 NativeImageSource::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 NativeImageSource::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 NativeImageSource::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 NativeImageSource::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 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
412 return mEglImageKHR != NULL;
415 void NativeImageSource::GlExtensionDestroy()
419 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
425 unsigned int NativeImageSource::TargetTexture()
427 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
432 void NativeImageSource::PrepareTexture()
436 void* eglImage = mEglImageKHR;
438 if( GlExtensionCreate() )
443 mEglImageExtensions->DestroyImageKHR( eglImage );
449 int NativeImageSource::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
453 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
455 // ToDo: Get the default screen depth
458 case Dali::NativeImageSource::COLOR_DEPTH_8:
462 case Dali::NativeImageSource::COLOR_DEPTH_16:
466 case Dali::NativeImageSource::COLOR_DEPTH_24:
470 case Dali::NativeImageSource::COLOR_DEPTH_32:
476 DALI_ASSERT_DEBUG(0 && "unknown color enum");
482 const char* NativeImageSource::GetCustomFragmentPreFix()
484 return FRAGMENT_PREFIX;
487 const char* NativeImageSource::GetCustomSamplerTypename()
492 int NativeImageSource::GetEglImageTextureTarget()
494 return GL_TEXTURE_EXTERNAL_OES;
497 bool NativeImageSource::CheckBlending( tbm_format format )
499 if( mTbmFormat != format )
501 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
503 if( format == FORMATS_BLENDING_REQUIRED[i] )
505 mBlendingRequired = true;
512 return mBlendingRequired;
515 } // namespace Adaptor
517 } // namespace internal