Merge "Modern compilers consider auto_ptr deprecated, use unique_ptr instead." into...
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / native-image-source-impl-tizen.cpp
old mode 100644 (file)
new mode 100755 (executable)
index f9d1923..e998ed4
@@ -20,6 +20,7 @@
 
 // EXTERNAL INCLUDES
 #include <dali/integration-api/debug.h>
+#include <dali/integration-api/gl-defines.h>
 #include <cstring>
 
 // INTERNAL INCLUDES
@@ -65,12 +66,13 @@ NativeImageSource* NativeImageSource::New(unsigned int width, unsigned int heigh
 NativeImageSource::NativeImageSource( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
 : mWidth( width ),
   mHeight( height ),
-  mOwnTbmsurface( true ),
+  mOwnTbmsurface( false ),
   mTbmsurface( NULL ),
   mBlendingRequired( false ),
   mColorDepth( depth ),
   mEglImageKHR( NULL ),
-  mEglImageExtensions( NULL )
+  mEglImageExtensions( NULL ),
+  mSetSource( false )
 {
   DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
   EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
@@ -88,9 +90,8 @@ NativeImageSource::NativeImageSource( unsigned int width, unsigned int height, D
 
 void NativeImageSource::Initialize()
 {
-  if( mTbmsurface != NULL )
+  if( mTbmsurface != NULL || mWidth == 0 || mHeight == 0 )
   {
-    mOwnTbmsurface = false;
     return;
   }
 
@@ -131,7 +132,7 @@ void NativeImageSource::Initialize()
     }
     default:
     {
-      DALI_LOG_WARNING( "Wrong color depth." );
+      DALI_LOG_WARNING( "Wrong color depth.\n" );
       return;
     }
   }
@@ -144,6 +145,7 @@ void NativeImageSource::Initialize()
   mBlendingRequired = ( depth == 32 || depth == 8 );
 
   mTbmsurface = tbm_surface_create( mWidth, mHeight, format );
+  mOwnTbmsurface = true;
 }
 
 tbm_surface_h NativeImageSource::GetSurfaceFromAny( Any source ) const
@@ -169,7 +171,7 @@ NativeImageSource::~NativeImageSource()
   {
     if( tbm_surface_destroy( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
     {
-      DALI_LOG_ERROR( "Failed to destroy tbm_surface" );
+      DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
     }
   }
 }
@@ -187,7 +189,7 @@ bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned&
 
     if( tbm_surface_map( mTbmsurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
     {
-      DALI_LOG_ERROR( "Fail to map tbm_surface" );
+      DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
 
       width = 0;
       height = 0;
@@ -250,7 +252,7 @@ bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned&
       }
       default:
       {
-        DALI_LOG_WARNING( "Tbm surface has unsupported pixel format." );
+        DALI_LOG_WARNING( "Tbm surface has unsupported pixel format.\n" );
 
         pixbuf.resize( 0 );
         width = 0;
@@ -262,13 +264,13 @@ bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned&
 
     if( tbm_surface_unmap( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
     {
-      DALI_LOG_ERROR( "Fail to unmap tbm_surface" );
+      DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
     }
 
     return true;
   }
 
-  DALI_LOG_WARNING( "TBM surface does not exist." );
+  DALI_LOG_WARNING( "TBM surface does not exist.\n" );
 
   width = 0;
   height = 0;
@@ -295,29 +297,84 @@ void NativeImageSource::SetSource( Any source )
   {
     if( tbm_surface_destroy( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
     {
-      DALI_LOG_ERROR( "Failed to destroy tbm_surface" );
+      DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
     }
 
     mTbmsurface = NULL;
+    mOwnTbmsurface = false;
   }
 
   mTbmsurface = GetSurfaceFromAny( source );
-  mOwnTbmsurface = false;
+  mSetSource = true;
 
   if( mTbmsurface != NULL )
   {
     mWidth = tbm_surface_get_width( mTbmsurface );
     mHeight = tbm_surface_get_height( mTbmsurface );
   }
+
+  if( mEglImageKHRContainer.Size() > 2 )
+  {
+    mEglImageExtensions->DestroyImageKHR(mEglImageKHRContainer[0]);
+    mEglImageKHRContainer.Erase( mEglImageKHRContainer.Begin() );
+  }
+
+  mEglImageKHRContainer.PushBack( mEglImageKHR );
 }
 
-bool NativeImageSource::GlExtensionCreate()
+bool NativeImageSource::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
 {
-  if( mEglImageKHR != NULL )
+  uint32_t* formats;
+  uint32_t formatNum;
+  tbm_format format = TBM_FORMAT_RGB888;
+
+  switch( colorDepth )
   {
-    GlExtensionDestroy();
+    case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
+    {
+      format = TBM_FORMAT_RGBA8888;
+      break;
+    }
+    case Dali::NativeImageSource::COLOR_DEPTH_8:
+    {
+      format = TBM_FORMAT_C8;
+      break;
+    }
+    case Dali::NativeImageSource::COLOR_DEPTH_16:
+    {
+      format = TBM_FORMAT_RGB565;
+      break;
+    }
+    case Dali::NativeImageSource::COLOR_DEPTH_24:
+    {
+      format = TBM_FORMAT_RGB888;
+      break;
+    }
+    case Dali::NativeImageSource::COLOR_DEPTH_32:
+    {
+      format = TBM_FORMAT_RGBA8888;
+      break;
+    }
+  }
+
+  if( tbm_surface_query_formats( &formats, &formatNum ) )
+  {
+    for( unsigned int i = 0; i < formatNum; i++ )
+    {
+      if( formats[i] == format )
+      {
+        free( formats );
+        return true;
+      }
+    }
   }
 
+  free( formats );
+  return false;
+}
+
+bool NativeImageSource::GlExtensionCreate()
+{
   // casting from an unsigned int to a void *, which should then be cast back
   // to an unsigned int in the driver.
   EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer > (mTbmsurface);
@@ -341,6 +398,15 @@ unsigned int NativeImageSource::TargetTexture()
   return 0;
 }
 
+void NativeImageSource::PrepareTexture()
+{
+  if( mSetSource && GlExtensionCreate() )
+  {
+    TargetTexture();
+    mSetSource = false;
+  }
+}
+
 int NativeImageSource::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
 {
   switch (depth)
@@ -384,6 +450,11 @@ const char* NativeImageSource::GetCustomSamplerTypename()
   return SAMPLER_TYPE;
 }
 
+int NativeImageSource::GetEglImageTextureTarget()
+{
+  return GL_TEXTURE_EXTERNAL_OES;
+}
+
 } // namespace Adaptor
 
 } // namespace internal