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>
32 #include <trigger-event-factory.h>
35 // Allow this to be encoded and saved:
36 #include <bitmap-saver.h>
49 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
50 const char* SAMPLER_TYPE = "samplerExternalOES";
52 tbm_format FORMATS_BLENDING_REQUIRED[] = {
53 TBM_FORMAT_ARGB4444, TBM_FORMAT_ABGR4444,
54 TBM_FORMAT_RGBA4444, TBM_FORMAT_BGRA4444,
55 TBM_FORMAT_RGBX5551, TBM_FORMAT_BGRX5551,
56 TBM_FORMAT_ARGB1555, TBM_FORMAT_ABGR1555,
57 TBM_FORMAT_RGBA5551, TBM_FORMAT_BGRA5551,
58 TBM_FORMAT_ARGB8888, TBM_FORMAT_ABGR8888,
59 TBM_FORMAT_RGBA8888, TBM_FORMAT_BGRA8888,
60 TBM_FORMAT_ARGB2101010, TBM_FORMAT_ABGR2101010,
61 TBM_FORMAT_RGBA1010102, TBM_FORMAT_BGRA1010102
64 const int NUM_FORMATS_BLENDING_REQUIRED = 18;
68 using Dali::Integration::PixelBuffer;
70 NativeImageSource* NativeImageSource::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
72 NativeImageSource* image = new NativeImageSource( width, height, depth, nativeImageSource );
73 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
83 NativeImageSource::NativeImageSource( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
86 mOwnTbmSurface( false ),
89 mBlendingRequired( false ),
92 mEglImageExtensions( NULL ),
96 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
97 EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
98 mEglImageExtensions = eglFactory.GetImageExtensions();
99 DALI_ASSERT_DEBUG( mEglImageExtensions );
101 mTbmSurface = GetSurfaceFromAny( nativeImageSource );
103 if( mTbmSurface != NULL )
105 tbm_surface_internal_ref( mTbmSurface );
106 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
107 mWidth = tbm_surface_get_width( mTbmSurface );
108 mHeight = tbm_surface_get_height( mTbmSurface );
112 void NativeImageSource::Initialize()
114 if( mTbmSurface != NULL || mWidth == 0 || mHeight == 0 )
119 tbm_format format = TBM_FORMAT_RGB888;
122 switch( mColorDepth )
124 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
126 format = TBM_FORMAT_RGBA8888;
130 case Dali::NativeImageSource::COLOR_DEPTH_8:
132 format = TBM_FORMAT_C8;
136 case Dali::NativeImageSource::COLOR_DEPTH_16:
138 format = TBM_FORMAT_RGB565;
142 case Dali::NativeImageSource::COLOR_DEPTH_24:
144 format = TBM_FORMAT_RGB888;
148 case Dali::NativeImageSource::COLOR_DEPTH_32:
150 format = TBM_FORMAT_RGBA8888;
156 DALI_LOG_WARNING( "Wrong color depth.\n" );
161 // set whether blending is required according to pixel format based on the depth
162 /* default pixel format is RGB888
163 If depth = 8, Pixel::A8;
164 If depth = 16, Pixel::RGB565;
165 If depth = 32, Pixel::RGBA8888 */
166 mBlendingRequired = ( depth == 32 || depth == 8 );
168 mTbmSurface = tbm_surface_create( mWidth, mHeight, format );
169 mOwnTbmSurface = true;
172 tbm_surface_h NativeImageSource::GetSurfaceFromAny( Any source ) const
179 if( source.GetType() == typeid( tbm_surface_h ) )
181 return AnyCast< tbm_surface_h >( source );
189 NativeImageSource::~NativeImageSource()
193 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
195 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
200 if( mTbmSurface != NULL )
202 tbm_surface_internal_unref( mTbmSurface );
204 if (mNotification != NULL) {
205 TriggerEventInterface* triggerEvent = static_cast<TriggerEventInterface* >(mNotification);
206 triggerEvent->Trigger();
212 Any NativeImageSource::GetNativeImageSource() const
214 return Any( mTbmSurface );
218 void NativeImageSource::SetDestructorNotification(void* notification)
220 mNotification = notification;
223 bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
225 if( mTbmSurface != NULL )
227 tbm_surface_info_s surface_info;
229 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
231 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
239 tbm_format format = surface_info.format;
240 uint32_t stride = surface_info.planes[0].stride;
241 unsigned char* ptr = surface_info.planes[0].ptr;
251 case TBM_FORMAT_RGB888:
254 pixelFormat = Pixel::RGB888;
255 pixbuf.resize( lineSize*height );
256 unsigned char* bufptr = &pixbuf[0];
258 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
260 for( unsigned int c = 0; c < width; ++c )
263 offset = cOffset + r*stride;
264 *(bufptr+cOffset) = ptr[offset+2];
265 *(bufptr+cOffset+1) = ptr[offset+1];
266 *(bufptr+cOffset+2) = ptr[offset];
271 case TBM_FORMAT_RGBA8888:
274 pixelFormat = Pixel::RGBA8888;
275 pixbuf.resize( lineSize*height );
276 unsigned char* bufptr = &pixbuf[0];
278 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
280 for( unsigned int c = 0; c < width; ++c )
283 offset = cOffset + r*stride;
284 *(bufptr+cOffset) = ptr[offset+3];
285 *(bufptr+cOffset+1) = ptr[offset+2];
286 *(bufptr+cOffset+2) = ptr[offset+1];
287 *(bufptr+cOffset+3) = ptr[offset];
294 DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
300 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
302 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
308 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
316 bool NativeImageSource::EncodeToFile(const std::string& filename) const
318 std::vector< unsigned char > pixbuf;
319 unsigned int width(0), height(0);
320 Pixel::Format pixelFormat;
322 if(GetPixels(pixbuf, width, height, pixelFormat))
324 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
329 void NativeImageSource::SetSource( Any source )
333 if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
335 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
339 mOwnTbmSurface = false;
343 if( mTbmSurface != NULL )
345 tbm_surface_internal_unref( mTbmSurface );
350 mTbmSurface = GetSurfaceFromAny( source );
352 if( mTbmSurface != NULL )
355 tbm_surface_internal_ref( mTbmSurface );
356 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
357 mWidth = tbm_surface_get_width( mTbmSurface );
358 mHeight = tbm_surface_get_height( mTbmSurface );
362 bool NativeImageSource::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
366 tbm_format format = TBM_FORMAT_RGB888;
370 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
372 format = TBM_FORMAT_RGBA8888;
375 case Dali::NativeImageSource::COLOR_DEPTH_8:
377 format = TBM_FORMAT_C8;
380 case Dali::NativeImageSource::COLOR_DEPTH_16:
382 format = TBM_FORMAT_RGB565;
385 case Dali::NativeImageSource::COLOR_DEPTH_24:
387 format = TBM_FORMAT_RGB888;
390 case Dali::NativeImageSource::COLOR_DEPTH_32:
392 format = TBM_FORMAT_RGBA8888;
397 if( tbm_surface_query_formats( &formats, &formatNum ) )
399 for( unsigned int i = 0; i < formatNum; i++ )
401 if( formats[i] == format )
413 bool NativeImageSource::GlExtensionCreate()
415 // casting from an unsigned int to a void *, which should then be cast back
416 // to an unsigned int in the driver.
417 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
423 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
425 return mEglImageKHR != NULL;
428 void NativeImageSource::GlExtensionDestroy()
432 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
438 unsigned int NativeImageSource::TargetTexture()
440 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
445 void NativeImageSource::PrepareTexture()
449 void* eglImage = mEglImageKHR;
451 if( GlExtensionCreate() )
456 mEglImageExtensions->DestroyImageKHR( eglImage );
462 int NativeImageSource::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
466 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
468 // ToDo: Get the default screen depth
471 case Dali::NativeImageSource::COLOR_DEPTH_8:
475 case Dali::NativeImageSource::COLOR_DEPTH_16:
479 case Dali::NativeImageSource::COLOR_DEPTH_24:
483 case Dali::NativeImageSource::COLOR_DEPTH_32:
489 DALI_ASSERT_DEBUG(0 && "unknown color enum");
495 const char* NativeImageSource::GetCustomFragmentPreFix()
497 return FRAGMENT_PREFIX;
500 const char* NativeImageSource::GetCustomSamplerTypename()
505 int NativeImageSource::GetEglImageTextureTarget()
507 return GL_TEXTURE_EXTERNAL_OES;
510 bool NativeImageSource::CheckBlending( tbm_format format )
512 if( mTbmFormat != format )
514 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
516 if( format == FORMATS_BLENDING_REQUIRED[i] )
518 mBlendingRequired = true;
525 return mBlendingRequired;
528 } // namespace Adaptor
530 } // namespace internal