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( true ),
70 mBlendingRequired( false ),
73 mEglImageExtensions( NULL )
75 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
76 EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
77 mEglImageExtensions = eglFactory.GetImageExtensions();
78 DALI_ASSERT_DEBUG( mEglImageExtensions );
80 mTbmsurface = GetSurfaceFromAny( nativeImageSource );
82 if( mTbmsurface != NULL )
84 mWidth = tbm_surface_get_width( mTbmsurface );
85 mHeight = tbm_surface_get_height( mTbmsurface );
89 void NativeImageSource::Initialize()
91 if( mTbmsurface != NULL )
93 mOwnTbmsurface = false;
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 );
149 tbm_surface_h NativeImageSource::GetSurfaceFromAny( Any source ) const
156 if( source.GetType() == typeid( tbm_surface_h ) )
158 return AnyCast< tbm_surface_h >( source );
166 NativeImageSource::~NativeImageSource()
168 if( mOwnTbmsurface && mTbmsurface != NULL )
170 if( tbm_surface_destroy( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
172 DALI_LOG_ERROR( "Failed to destroy tbm_surface" );
177 Any NativeImageSource::GetNativeImageSource() const
179 return Any( mTbmsurface );
182 bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
184 if( mTbmsurface != NULL )
186 tbm_surface_info_s surface_info;
188 if( tbm_surface_map( mTbmsurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
190 DALI_LOG_ERROR( "Fail to map tbm_surface" );
198 tbm_format format = surface_info.format;
199 uint32_t stride = surface_info.planes[0].stride;
200 unsigned char* ptr = surface_info.planes[0].ptr;
210 case TBM_FORMAT_RGB888:
213 pixelFormat = Pixel::RGB888;
214 pixbuf.resize( lineSize*height );
215 unsigned char* bufptr = &pixbuf[0];
217 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
219 for( unsigned int c = 0; c < width; ++c )
222 offset = cOffset + r*stride;
223 *(bufptr) = ptr[offset+2];
224 *(bufptr+cOffset+1) = ptr[offset+1];
225 *(bufptr+cOffset+2) = ptr[offset];
230 case TBM_FORMAT_RGBA8888:
233 pixelFormat = Pixel::RGBA8888;
234 pixbuf.resize( lineSize*height );
235 unsigned char* bufptr = &pixbuf[0];
237 for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
239 for( unsigned int c = 0; c < width; ++c )
242 offset = cOffset + r*stride;
243 *(bufptr) = ptr[offset+3];
244 *(bufptr+cOffset+1) = ptr[offset+2];
245 *(bufptr+cOffset+2) = ptr[offset+1];
246 *(bufptr+cOffset+3) = ptr[offset];
253 DALI_LOG_WARNING( "Tbm surface has unsupported pixel format." );
263 if( tbm_surface_unmap( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
265 DALI_LOG_ERROR( "Fail to unmap tbm_surface" );
271 DALI_LOG_WARNING( "TBM surface does not exist." );
279 bool NativeImageSource::EncodeToFile(const std::string& filename) const
281 std::vector< unsigned char > pixbuf;
282 unsigned int width(0), height(0);
283 Pixel::Format pixelFormat;
285 if(GetPixels(pixbuf, width, height, pixelFormat))
287 return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
292 void NativeImageSource::SetSource( Any source )
294 if( mOwnTbmsurface && mTbmsurface != NULL )
296 if( tbm_surface_destroy( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
298 DALI_LOG_ERROR( "Failed to destroy tbm_surface" );
304 mTbmsurface = GetSurfaceFromAny( source );
305 mOwnTbmsurface = false;
307 if( mTbmsurface != NULL )
309 mWidth = tbm_surface_get_width( mTbmsurface );
310 mHeight = tbm_surface_get_height( mTbmsurface );
314 bool NativeImageSource::GlExtensionCreate()
316 if( mEglImageKHR != NULL )
318 GlExtensionDestroy();
321 // casting from an unsigned int to a void *, which should then be cast back
322 // to an unsigned int in the driver.
323 EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer > (mTbmsurface);
325 mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
327 return mEglImageKHR != NULL;
330 void NativeImageSource::GlExtensionDestroy()
332 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
337 unsigned int NativeImageSource::TargetTexture()
339 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
344 int NativeImageSource::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
348 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
350 // ToDo: Get the default screen depth
353 case Dali::NativeImageSource::COLOR_DEPTH_8:
357 case Dali::NativeImageSource::COLOR_DEPTH_16:
361 case Dali::NativeImageSource::COLOR_DEPTH_24:
365 case Dali::NativeImageSource::COLOR_DEPTH_32:
371 DALI_ASSERT_DEBUG(0 && "unknown color enum");
377 const char* NativeImageSource::GetCustomFragmentPreFix()
379 return FRAGMENT_PREFIX;
382 const char* NativeImageSource::GetCustomSamplerTypename()
387 } // namespace Adaptor
389 } // namespace internal