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>
26 #include <gl/egl-image-extensions.h>
27 #include <gl/egl-factory.h>
28 #include <adaptor-impl.h>
29 #include <render-surface.h>
31 // Allow this to be encoded and saved:
32 #include <platform-abstractions/tizen/resource-loader/resource-loader.h>
33 #include <bitmap-saver.h>
46 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
47 const char* SAMPLER_TYPE = "samplerExternalOES";
50 using Dali::Integration::PixelBuffer;
52 NativeImageSource* NativeImageSource::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
54 NativeImageSource* image = new NativeImageSource( width, height, depth, nativeImageSource );
55 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
65 NativeImageSource::NativeImageSource( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
68 mOwnTbmsurface( false ),
70 mBlendingRequired( false ),
73 mEglImageExtensions( NULL ),
76 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
77 EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
78 mEglImageExtensions = eglFactory.GetImageExtensions();
79 DALI_ASSERT_DEBUG( mEglImageExtensions );
81 mTbmsurface = GetSurfaceFromAny( nativeImageSource );
83 if( mTbmsurface != NULL )
85 mWidth = tbm_surface_get_width( mTbmsurface );
86 mHeight = tbm_surface_get_height( mTbmsurface );
90 void NativeImageSource::Initialize()
92 if( mTbmsurface != NULL || mWidth == 0 || mHeight == 0 )
97 tbm_format format = TBM_FORMAT_RGB888;
100 switch( mColorDepth )
102 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
104 format = TBM_FORMAT_RGBA8888;
108 case Dali::NativeImageSource::COLOR_DEPTH_8:
110 format = TBM_FORMAT_C8;
114 case Dali::NativeImageSource::COLOR_DEPTH_16:
116 format = TBM_FORMAT_RGB565;
120 case Dali::NativeImageSource::COLOR_DEPTH_24:
122 format = TBM_FORMAT_RGB888;
126 case Dali::NativeImageSource::COLOR_DEPTH_32:
128 format = TBM_FORMAT_RGBA8888;
134 DALI_LOG_WARNING( "Wrong color depth." );
139 // set whether blending is required according to pixel format based on the depth
140 /* default pixel format is RGB888
141 If depth = 8, Pixel::A8;
142 If depth = 16, Pixel::RGB565;
143 If depth = 32, Pixel::RGBA8888 */
144 mBlendingRequired = ( depth == 32 || depth == 8 );
146 mTbmsurface = tbm_surface_create( mWidth, mHeight, format );
147 mOwnTbmsurface = true;
150 tbm_surface_h NativeImageSource::GetSurfaceFromAny( Any source ) const
157 if( source.GetType() == typeid( tbm_surface_h ) )
159 return AnyCast< tbm_surface_h >( source );
167 NativeImageSource::~NativeImageSource()
169 if( mOwnTbmsurface && mTbmsurface != NULL )
171 if( tbm_surface_destroy( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
173 DALI_LOG_ERROR( "Failed to destroy tbm_surface" );
178 Any NativeImageSource::GetNativeImageSource() const
180 return Any( mTbmsurface );
183 bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
185 if( mTbmsurface != NULL )
187 tbm_surface_info_s surface_info;
189 if( tbm_surface_map( mTbmsurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
191 DALI_LOG_ERROR( "Fail to map tbm_surface" );
199 tbm_format format = surface_info.format;
200 uint32_t stride = surface_info.planes[0].stride;
201 unsigned char* ptr = surface_info.planes[0].ptr;
211 case TBM_FORMAT_RGB888:
214 pixelFormat = Pixel::RGB888;
215 pixbuf.resize( lineSize*height );
216 unsigned char* bufptr = &pixbuf[0];
218 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
220 for( unsigned int c = 0; c < width; ++c )
223 offset = cOffset + r*stride;
224 *(bufptr) = ptr[offset+2];
225 *(bufptr+cOffset+1) = ptr[offset+1];
226 *(bufptr+cOffset+2) = ptr[offset];
231 case TBM_FORMAT_RGBA8888:
234 pixelFormat = Pixel::RGBA8888;
235 pixbuf.resize( lineSize*height );
236 unsigned char* bufptr = &pixbuf[0];
238 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
240 for( unsigned int c = 0; c < width; ++c )
243 offset = cOffset + r*stride;
244 *(bufptr) = ptr[offset+3];
245 *(bufptr+cOffset+1) = ptr[offset+2];
246 *(bufptr+cOffset+2) = ptr[offset+1];
247 *(bufptr+cOffset+3) = ptr[offset];
254 DALI_LOG_WARNING( "Tbm surface has unsupported pixel format." );
264 if( tbm_surface_unmap( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
266 DALI_LOG_ERROR( "Fail to unmap tbm_surface" );
272 DALI_LOG_WARNING( "TBM surface does not exist." );
280 bool NativeImageSource::EncodeToFile(const std::string& filename) const
282 std::vector< unsigned char > pixbuf;
283 unsigned int width(0), height(0);
284 Pixel::Format pixelFormat;
286 if(GetPixels(pixbuf, width, height, pixelFormat))
288 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
293 void NativeImageSource::SetSource( Any source )
295 if( mOwnTbmsurface && mTbmsurface != NULL )
297 if( tbm_surface_destroy( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
299 DALI_LOG_ERROR( "Failed to destroy tbm_surface" );
303 mOwnTbmsurface = false;
306 mTbmsurface = GetSurfaceFromAny( source );
309 if( mTbmsurface != NULL )
311 mWidth = tbm_surface_get_width( mTbmsurface );
312 mHeight = tbm_surface_get_height( mTbmsurface );
315 if( mEglImageKHRContainer.Size() > 2 )
317 mEglImageExtensions->DestroyImageKHR(mEglImageKHRContainer[0]);
318 mEglImageKHRContainer.Erase( mEglImageKHRContainer.Begin() );
321 mEglImageKHRContainer.PushBack( mEglImageKHR );
324 bool NativeImageSource::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
328 tbm_format format = TBM_FORMAT_RGB888;
332 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
334 format = TBM_FORMAT_RGBA8888;
337 case Dali::NativeImageSource::COLOR_DEPTH_8:
339 format = TBM_FORMAT_C8;
342 case Dali::NativeImageSource::COLOR_DEPTH_16:
344 format = TBM_FORMAT_RGB565;
347 case Dali::NativeImageSource::COLOR_DEPTH_24:
349 format = TBM_FORMAT_RGB888;
352 case Dali::NativeImageSource::COLOR_DEPTH_32:
354 format = TBM_FORMAT_RGBA8888;
359 if( tbm_surface_query_formats( &formats, &formatNum ) )
361 for( unsigned int i = 0; i < formatNum; i++ )
363 if( formats[i] == format )
375 bool NativeImageSource::GlExtensionCreate()
377 // casting from an unsigned int to a void *, which should then be cast back
378 // to an unsigned int in the driver.
379 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer > (mTbmsurface);
381 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
383 return mEglImageKHR != NULL;
386 void NativeImageSource::GlExtensionDestroy()
388 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
393 unsigned int NativeImageSource::TargetTexture()
395 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
400 void NativeImageSource::PrepareTexture()
402 if( mSetSource && GlExtensionCreate() )
409 int NativeImageSource::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
413 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
415 // ToDo: Get the default screen depth
418 case Dali::NativeImageSource::COLOR_DEPTH_8:
422 case Dali::NativeImageSource::COLOR_DEPTH_16:
426 case Dali::NativeImageSource::COLOR_DEPTH_24:
430 case Dali::NativeImageSource::COLOR_DEPTH_32:
436 DALI_ASSERT_DEBUG(0 && "unknown color enum");
442 const char* NativeImageSource::GetCustomFragmentPreFix()
444 return FRAGMENT_PREFIX;
447 const char* NativeImageSource::GetCustomSamplerTypename()
452 } // namespace Adaptor
454 } // namespace internal