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>
27 #include <gl/egl-image-extensions.h>
28 #include <gl/egl-factory.h>
29 #include <adaptor-impl.h>
30 #include <render-surface.h>
32 // Allow this to be encoded and saved:
33 #include <platform-abstractions/tizen/resource-loader/resource-loader.h>
34 #include <bitmap-saver.h>
47 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
48 const char* SAMPLER_TYPE = "samplerExternalOES";
50 tbm_format FORMATS_BLENDING_REQUIRED[] = {
51 TBM_FORMAT_ARGB4444, TBM_FORMAT_ABGR4444,
52 TBM_FORMAT_RGBA4444, TBM_FORMAT_BGRA4444,
53 TBM_FORMAT_RGBX5551, TBM_FORMAT_BGRX5551,
54 TBM_FORMAT_ARGB1555, TBM_FORMAT_ABGR1555,
55 TBM_FORMAT_RGBA5551, TBM_FORMAT_BGRA5551,
56 TBM_FORMAT_ARGB8888, TBM_FORMAT_ABGR8888,
57 TBM_FORMAT_RGBA8888, TBM_FORMAT_BGRA8888,
58 TBM_FORMAT_ARGB2101010, TBM_FORMAT_ABGR2101010,
59 TBM_FORMAT_RGBA1010102, TBM_FORMAT_BGRA1010102
62 const int NUM_FORMATS_BLENDING_REQUIRED = 18;
66 using Dali::Integration::PixelBuffer;
68 NativeImageSource* NativeImageSource::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
70 NativeImageSource* image = new NativeImageSource( width, height, depth, nativeImageSource );
71 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
81 NativeImageSource::NativeImageSource( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
84 mOwnTbmSurface( false ),
87 mBlendingRequired( false ),
90 mEglImageExtensions( NULL ),
93 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
94 EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
95 mEglImageExtensions = eglFactory.GetImageExtensions();
96 DALI_ASSERT_DEBUG( mEglImageExtensions );
98 mTbmSurface = GetSurfaceFromAny( nativeImageSource );
100 if( mTbmSurface != NULL )
102 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
103 mWidth = tbm_surface_get_width( mTbmSurface );
104 mHeight = tbm_surface_get_height( mTbmSurface );
108 void NativeImageSource::Initialize()
110 if( mTbmSurface != NULL || mWidth == 0 || mHeight == 0 )
115 tbm_format format = TBM_FORMAT_RGB888;
118 switch( mColorDepth )
120 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
122 format = TBM_FORMAT_RGBA8888;
126 case Dali::NativeImageSource::COLOR_DEPTH_8:
128 format = TBM_FORMAT_C8;
132 case Dali::NativeImageSource::COLOR_DEPTH_16:
134 format = TBM_FORMAT_RGB565;
138 case Dali::NativeImageSource::COLOR_DEPTH_24:
140 format = TBM_FORMAT_RGB888;
144 case Dali::NativeImageSource::COLOR_DEPTH_32:
146 format = TBM_FORMAT_RGBA8888;
152 DALI_LOG_WARNING( "Wrong color depth.\n" );
157 // set whether blending is required according to pixel format based on the depth
158 /* default pixel format is RGB888
159 If depth = 8, Pixel::A8;
160 If depth = 16, Pixel::RGB565;
161 If depth = 32, Pixel::RGBA8888 */
162 mBlendingRequired = ( depth == 32 || depth == 8 );
164 mTbmSurface = tbm_surface_create( mWidth, mHeight, format );
165 mOwnTbmSurface = true;
168 tbm_surface_h NativeImageSource::GetSurfaceFromAny( Any source ) const
175 if( source.GetType() == typeid( tbm_surface_h ) )
177 return AnyCast< tbm_surface_h >( source );
185 NativeImageSource::~NativeImageSource()
187 if( mOwnTbmSurface && mTbmSurface != NULL )
189 if( tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
191 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
196 Any NativeImageSource::GetNativeImageSource() const
198 return Any( mTbmSurface );
201 bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
203 if( mTbmSurface != NULL )
205 tbm_surface_info_s surface_info;
207 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
209 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
217 tbm_format format = surface_info.format;
218 uint32_t stride = surface_info.planes[0].stride;
219 unsigned char* ptr = surface_info.planes[0].ptr;
229 case TBM_FORMAT_RGB888:
232 pixelFormat = Pixel::RGB888;
233 pixbuf.resize( lineSize*height );
234 unsigned char* bufptr = &pixbuf[0];
236 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
238 for( unsigned int c = 0; c < width; ++c )
241 offset = cOffset + r*stride;
242 *(bufptr) = ptr[offset+2];
243 *(bufptr+cOffset+1) = ptr[offset+1];
244 *(bufptr+cOffset+2) = ptr[offset];
249 case TBM_FORMAT_RGBA8888:
252 pixelFormat = Pixel::RGBA8888;
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) = ptr[offset+3];
263 *(bufptr+cOffset+1) = ptr[offset+2];
264 *(bufptr+cOffset+2) = ptr[offset+1];
265 *(bufptr+cOffset+3) = ptr[offset];
272 DALI_LOG_WARNING( "Tbm surface has unsupported pixel format.\n" );
282 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
284 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
290 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
298 bool NativeImageSource::EncodeToFile(const std::string& filename) const
300 std::vector< unsigned char > pixbuf;
301 unsigned int width(0), height(0);
302 Pixel::Format pixelFormat;
304 if(GetPixels(pixbuf, width, height, pixelFormat))
306 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
311 void NativeImageSource::SetSource( Any source )
313 if( mOwnTbmSurface && mTbmSurface != NULL )
315 if( tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
317 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
321 mOwnTbmSurface = false;
324 mTbmSurface = GetSurfaceFromAny( source );
327 if( mTbmSurface != NULL )
329 mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
330 mWidth = tbm_surface_get_width( mTbmSurface );
331 mHeight = tbm_surface_get_height( mTbmSurface );
335 bool NativeImageSource::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
339 tbm_format format = TBM_FORMAT_RGB888;
343 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
345 format = TBM_FORMAT_RGBA8888;
348 case Dali::NativeImageSource::COLOR_DEPTH_8:
350 format = TBM_FORMAT_C8;
353 case Dali::NativeImageSource::COLOR_DEPTH_16:
355 format = TBM_FORMAT_RGB565;
358 case Dali::NativeImageSource::COLOR_DEPTH_24:
360 format = TBM_FORMAT_RGB888;
363 case Dali::NativeImageSource::COLOR_DEPTH_32:
365 format = TBM_FORMAT_RGBA8888;
370 if( tbm_surface_query_formats( &formats, &formatNum ) )
372 for( unsigned int i = 0; i < formatNum; i++ )
374 if( formats[i] == format )
386 bool NativeImageSource::GlExtensionCreate()
388 // casting from an unsigned int to a void *, which should then be cast back
389 // to an unsigned int in the driver.
390 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
396 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
398 return mEglImageKHR != NULL;
401 void NativeImageSource::GlExtensionDestroy()
405 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
411 unsigned int NativeImageSource::TargetTexture()
413 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
418 void NativeImageSource::PrepareTexture()
422 void* eglImage = mEglImageKHR;
424 if( GlExtensionCreate() )
429 mEglImageExtensions->DestroyImageKHR( eglImage );
435 int NativeImageSource::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
439 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
441 // ToDo: Get the default screen depth
444 case Dali::NativeImageSource::COLOR_DEPTH_8:
448 case Dali::NativeImageSource::COLOR_DEPTH_16:
452 case Dali::NativeImageSource::COLOR_DEPTH_24:
456 case Dali::NativeImageSource::COLOR_DEPTH_32:
462 DALI_ASSERT_DEBUG(0 && "unknown color enum");
468 const char* NativeImageSource::GetCustomFragmentPreFix()
470 return FRAGMENT_PREFIX;
473 const char* NativeImageSource::GetCustomSamplerTypename()
478 int NativeImageSource::GetEglImageTextureTarget()
480 return GL_TEXTURE_EXTERNAL_OES;
483 bool NativeImageSource::CheckBlending( tbm_format format )
485 if( mTbmFormat != format )
487 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
489 if( format == FORMATS_BLENDING_REQUIRED[i] )
491 mBlendingRequired = true;
498 return mBlendingRequired;
501 } // namespace Adaptor
503 } // namespace internal