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";
51 using Dali::Integration::PixelBuffer;
53 NativeImageSource* NativeImageSource::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
55 NativeImageSource* image = new NativeImageSource( width, height, depth, nativeImageSource );
56 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
66 NativeImageSource::NativeImageSource( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
69 mOwnTbmsurface( false ),
71 mBlendingRequired( false ),
74 mEglImageExtensions( NULL ),
77 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
78 EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
79 mEglImageExtensions = eglFactory.GetImageExtensions();
80 DALI_ASSERT_DEBUG( mEglImageExtensions );
82 mTbmsurface = GetSurfaceFromAny( nativeImageSource );
84 if( mTbmsurface != NULL )
86 mWidth = tbm_surface_get_width( mTbmsurface );
87 mHeight = tbm_surface_get_height( mTbmsurface );
91 void NativeImageSource::Initialize()
93 if( mTbmsurface != NULL || mWidth == 0 || mHeight == 0 )
98 tbm_format format = TBM_FORMAT_RGB888;
101 switch( mColorDepth )
103 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
105 format = TBM_FORMAT_RGBA8888;
109 case Dali::NativeImageSource::COLOR_DEPTH_8:
111 format = TBM_FORMAT_C8;
115 case Dali::NativeImageSource::COLOR_DEPTH_16:
117 format = TBM_FORMAT_RGB565;
121 case Dali::NativeImageSource::COLOR_DEPTH_24:
123 format = TBM_FORMAT_RGB888;
127 case Dali::NativeImageSource::COLOR_DEPTH_32:
129 format = TBM_FORMAT_RGBA8888;
135 DALI_LOG_WARNING( "Wrong color depth.\n" );
140 // set whether blending is required according to pixel format based on the depth
141 /* default pixel format is RGB888
142 If depth = 8, Pixel::A8;
143 If depth = 16, Pixel::RGB565;
144 If depth = 32, Pixel::RGBA8888 */
145 mBlendingRequired = ( depth == 32 || depth == 8 );
147 mTbmsurface = tbm_surface_create( mWidth, mHeight, format );
148 mOwnTbmsurface = true;
151 tbm_surface_h NativeImageSource::GetSurfaceFromAny( Any source ) const
158 if( source.GetType() == typeid( tbm_surface_h ) )
160 return AnyCast< tbm_surface_h >( source );
168 NativeImageSource::~NativeImageSource()
170 if( mOwnTbmsurface && mTbmsurface != NULL )
172 if( tbm_surface_destroy( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
174 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
179 Any NativeImageSource::GetNativeImageSource() const
181 return Any( mTbmsurface );
184 bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
186 if( mTbmsurface != NULL )
188 tbm_surface_info_s surface_info;
190 if( tbm_surface_map( mTbmsurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
192 DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
200 tbm_format format = surface_info.format;
201 uint32_t stride = surface_info.planes[0].stride;
202 unsigned char* ptr = surface_info.planes[0].ptr;
212 case TBM_FORMAT_RGB888:
215 pixelFormat = Pixel::RGB888;
216 pixbuf.resize( lineSize*height );
217 unsigned char* bufptr = &pixbuf[0];
219 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
221 for( unsigned int c = 0; c < width; ++c )
224 offset = cOffset + r*stride;
225 *(bufptr) = ptr[offset+2];
226 *(bufptr+cOffset+1) = ptr[offset+1];
227 *(bufptr+cOffset+2) = ptr[offset];
232 case TBM_FORMAT_RGBA8888:
235 pixelFormat = Pixel::RGBA8888;
236 pixbuf.resize( lineSize*height );
237 unsigned char* bufptr = &pixbuf[0];
239 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
241 for( unsigned int c = 0; c < width; ++c )
244 offset = cOffset + r*stride;
245 *(bufptr) = ptr[offset+3];
246 *(bufptr+cOffset+1) = ptr[offset+2];
247 *(bufptr+cOffset+2) = ptr[offset+1];
248 *(bufptr+cOffset+3) = ptr[offset];
255 DALI_LOG_WARNING( "Tbm surface has unsupported pixel format.\n" );
265 if( tbm_surface_unmap( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
267 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
273 DALI_LOG_WARNING( "TBM surface does not exist.\n" );
281 bool NativeImageSource::EncodeToFile(const std::string& filename) const
283 std::vector< unsigned char > pixbuf;
284 unsigned int width(0), height(0);
285 Pixel::Format pixelFormat;
287 if(GetPixels(pixbuf, width, height, pixelFormat))
289 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
294 void NativeImageSource::SetSource( Any source )
296 if( mOwnTbmsurface && mTbmsurface != NULL )
298 if( tbm_surface_destroy( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
300 DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
304 mOwnTbmsurface = false;
307 mTbmsurface = GetSurfaceFromAny( source );
310 if( mTbmsurface != NULL )
312 mWidth = tbm_surface_get_width( mTbmsurface );
313 mHeight = tbm_surface_get_height( mTbmsurface );
316 if( mEglImageKHRContainer.Size() > 2 )
318 mEglImageExtensions->DestroyImageKHR(mEglImageKHRContainer[0]);
319 mEglImageKHRContainer.Erase( mEglImageKHRContainer.Begin() );
322 mEglImageKHRContainer.PushBack( mEglImageKHR );
325 bool NativeImageSource::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
329 tbm_format format = TBM_FORMAT_RGB888;
333 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
335 format = TBM_FORMAT_RGBA8888;
338 case Dali::NativeImageSource::COLOR_DEPTH_8:
340 format = TBM_FORMAT_C8;
343 case Dali::NativeImageSource::COLOR_DEPTH_16:
345 format = TBM_FORMAT_RGB565;
348 case Dali::NativeImageSource::COLOR_DEPTH_24:
350 format = TBM_FORMAT_RGB888;
353 case Dali::NativeImageSource::COLOR_DEPTH_32:
355 format = TBM_FORMAT_RGBA8888;
360 if( tbm_surface_query_formats( &formats, &formatNum ) )
362 for( unsigned int i = 0; i < formatNum; i++ )
364 if( formats[i] == format )
376 bool NativeImageSource::GlExtensionCreate()
378 // casting from an unsigned int to a void *, which should then be cast back
379 // to an unsigned int in the driver.
380 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer > (mTbmsurface);
382 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
384 return mEglImageKHR != NULL;
387 void NativeImageSource::GlExtensionDestroy()
389 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
394 unsigned int NativeImageSource::TargetTexture()
396 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
401 void NativeImageSource::PrepareTexture()
403 if( mSetSource && GlExtensionCreate() )
410 int NativeImageSource::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
414 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
416 // ToDo: Get the default screen depth
419 case Dali::NativeImageSource::COLOR_DEPTH_8:
423 case Dali::NativeImageSource::COLOR_DEPTH_16:
427 case Dali::NativeImageSource::COLOR_DEPTH_24:
431 case Dali::NativeImageSource::COLOR_DEPTH_32:
437 DALI_ASSERT_DEBUG(0 && "unknown color enum");
443 const char* NativeImageSource::GetCustomFragmentPreFix()
445 return FRAGMENT_PREFIX;
448 const char* NativeImageSource::GetCustomSamplerTypename()
453 int NativeImageSource::GetEglImageTextureTarget()
455 return GL_TEXTURE_EXTERNAL_OES;
458 } // namespace Adaptor
460 } // namespace internal