[4.0] When native image is destroyed, It call TriggerEventInterface callback.
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / native-image-source-impl-tizen.cpp
index f9d1923..fecf43b 100644 (file)
 
 // EXTERNAL INCLUDES
 #include <dali/integration-api/debug.h>
+#include <dali/integration-api/gl-defines.h>
 #include <cstring>
+#include <tbm_surface_internal.h>
 
 // INTERNAL INCLUDES
 #include <gl/egl-image-extensions.h>
 #include <gl/egl-factory.h>
 #include <adaptor-impl.h>
 #include <render-surface.h>
+#include <trigger-event-factory.h>
+
 
 // Allow this to be encoded and saved:
-#include <platform-abstractions/tizen/resource-loader/resource-loader.h>
 #include <bitmap-saver.h>
 
 namespace Dali
@@ -45,6 +48,21 @@ namespace
 {
 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
 const char* SAMPLER_TYPE = "samplerExternalOES";
+
+tbm_format FORMATS_BLENDING_REQUIRED[] = {
+  TBM_FORMAT_ARGB4444, TBM_FORMAT_ABGR4444,
+  TBM_FORMAT_RGBA4444, TBM_FORMAT_BGRA4444,
+  TBM_FORMAT_RGBX5551, TBM_FORMAT_BGRX5551,
+  TBM_FORMAT_ARGB1555, TBM_FORMAT_ABGR1555,
+  TBM_FORMAT_RGBA5551, TBM_FORMAT_BGRA5551,
+  TBM_FORMAT_ARGB8888, TBM_FORMAT_ABGR8888,
+  TBM_FORMAT_RGBA8888, TBM_FORMAT_BGRA8888,
+  TBM_FORMAT_ARGB2101010, TBM_FORMAT_ABGR2101010,
+  TBM_FORMAT_RGBA1010102, TBM_FORMAT_BGRA1010102
+};
+
+const int NUM_FORMATS_BLENDING_REQUIRED = 18;
+
 }
 
 using Dali::Integration::PixelBuffer;
@@ -65,32 +83,36 @@ 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 ),
-  mTbmsurface( NULL ),
+  mOwnTbmSurface( false ),
+  mTbmSurface( NULL ),
+  mTbmFormat( 0 ),
   mBlendingRequired( false ),
   mColorDepth( depth ),
   mEglImageKHR( NULL ),
-  mEglImageExtensions( NULL )
+  mEglImageExtensions( NULL ),
+  mSetSource( false ),
+  mNotification( NULL )
 {
   DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
   EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
   mEglImageExtensions = eglFactory.GetImageExtensions();
   DALI_ASSERT_DEBUG( mEglImageExtensions );
 
-  mTbmsurface = GetSurfaceFromAny( nativeImageSource );
+  mTbmSurface = GetSurfaceFromAny( nativeImageSource );
 
-  if( mTbmsurface != NULL )
+  if( mTbmSurface != NULL )
   {
-    mWidth = tbm_surface_get_width( mTbmsurface );
-    mHeight = tbm_surface_get_height( mTbmsurface );
+    tbm_surface_internal_ref( mTbmSurface );
+    mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
+    mWidth = tbm_surface_get_width( mTbmSurface );
+    mHeight = tbm_surface_get_height( mTbmSurface );
   }
 }
 
 void NativeImageSource::Initialize()
 {
-  if( mTbmsurface != NULL )
+  if( mTbmSurface != NULL || mWidth == 0 || mHeight == 0 )
   {
-    mOwnTbmsurface = false;
     return;
   }
 
@@ -131,7 +153,7 @@ void NativeImageSource::Initialize()
     }
     default:
     {
-      DALI_LOG_WARNING( "Wrong color depth." );
+      DALI_LOG_WARNING( "Wrong color depth.\n" );
       return;
     }
   }
@@ -143,7 +165,8 @@ void NativeImageSource::Initialize()
      If depth = 32, Pixel::RGBA8888 */
   mBlendingRequired = ( depth == 32 || depth == 8 );
 
-  mTbmsurface = tbm_surface_create( mWidth, mHeight, format );
+  mTbmSurface = tbm_surface_create( mWidth, mHeight, format );
+  mOwnTbmSurface = true;
 }
 
 tbm_surface_h NativeImageSource::GetSurfaceFromAny( Any source ) const
@@ -165,29 +188,47 @@ tbm_surface_h NativeImageSource::GetSurfaceFromAny( Any source ) const
 
 NativeImageSource::~NativeImageSource()
 {
-  if( mOwnTbmsurface && mTbmsurface != NULL )
+  if( mOwnTbmSurface )
+  {
+    if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
+    {
+      DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
+    }
+  }
+  else
   {
-    if( tbm_surface_destroy( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
+    if( mTbmSurface != NULL )
     {
-      DALI_LOG_ERROR( "Failed to destroy tbm_surface" );
+      tbm_surface_internal_unref( mTbmSurface );
+
+      if (mNotification != NULL) {
+          TriggerEventInterface* triggerEvent = static_cast<TriggerEventInterface* >(mNotification);
+          triggerEvent->Trigger();
+      }
     }
   }
 }
 
 Any NativeImageSource::GetNativeImageSource() const
 {
-  return Any( mTbmsurface );
+  return Any( mTbmSurface );
+}
+
+
+void NativeImageSource::SetDestructorNotification(void* notification)
+{
+  mNotification = notification;
 }
 
 bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
 {
-  if( mTbmsurface != NULL )
+  if( mTbmSurface != NULL )
   {
     tbm_surface_info_s surface_info;
 
-    if( tbm_surface_map( mTbmsurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
+    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;
@@ -220,7 +261,7 @@ bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned&
           {
             cOffset = c*3;
             offset = cOffset + r*stride;
-            *(bufptr) = ptr[offset+2];
+            *(bufptr+cOffset) = ptr[offset+2];
             *(bufptr+cOffset+1) = ptr[offset+1];
             *(bufptr+cOffset+2) = ptr[offset];
           }
@@ -240,7 +281,7 @@ bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned&
           {
             cOffset = c*4;
             offset = cOffset + r*stride;
-            *(bufptr) = ptr[offset+3];
+            *(bufptr+cOffset) = ptr[offset+3];
             *(bufptr+cOffset+1) = ptr[offset+2];
             *(bufptr+cOffset+2) = ptr[offset+1];
             *(bufptr+cOffset+3) = ptr[offset];
@@ -250,25 +291,21 @@ bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned&
       }
       default:
       {
-        DALI_LOG_WARNING( "Tbm surface has unsupported pixel format." );
-
-        pixbuf.resize( 0 );
-        width = 0;
-        height = 0;
+        DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
 
         return false;
       }
     }
 
-    if( tbm_surface_unmap( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
+    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;
@@ -291,36 +328,97 @@ bool NativeImageSource::EncodeToFile(const std::string& filename) const
 
 void NativeImageSource::SetSource( Any source )
 {
-  if( mOwnTbmsurface && mTbmsurface != NULL )
+  if( mOwnTbmSurface )
   {
-    if( tbm_surface_destroy( mTbmsurface ) != TBM_SURFACE_ERROR_NONE )
+    if( mTbmSurface != NULL && 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;
+    mTbmSurface = NULL;
+    mOwnTbmSurface = false;
+  }
+  else
+  {
+    if( mTbmSurface != NULL )
+    {
+      tbm_surface_internal_unref( mTbmSurface );
+      mTbmSurface = NULL;
+    }
   }
 
-  mTbmsurface = GetSurfaceFromAny( source );
-  mOwnTbmsurface = false;
+  mTbmSurface = GetSurfaceFromAny( source );
 
-  if( mTbmsurface != NULL )
+  if( mTbmSurface != NULL )
   {
-    mWidth = tbm_surface_get_width( mTbmsurface );
-    mHeight = tbm_surface_get_height( mTbmsurface );
+    mSetSource = true;
+    tbm_surface_internal_ref( mTbmSurface );
+    mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
+    mWidth = tbm_surface_get_width( mTbmSurface );
+    mHeight = tbm_surface_get_height( mTbmSurface );
   }
 }
 
-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);
+  EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
+  if( !eglBuffer )
+  {
+    return false;
+  }
 
   mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
 
@@ -329,9 +427,12 @@ bool NativeImageSource::GlExtensionCreate()
 
 void NativeImageSource::GlExtensionDestroy()
 {
-  mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
+  if( mEglImageKHR )
+  {
+    mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
 
-  mEglImageKHR = NULL;
+    mEglImageKHR = NULL;
+  }
 }
 
 unsigned int NativeImageSource::TargetTexture()
@@ -341,6 +442,23 @@ unsigned int NativeImageSource::TargetTexture()
   return 0;
 }
 
+void NativeImageSource::PrepareTexture()
+{
+  if( mSetSource )
+  {
+    void* eglImage = mEglImageKHR;
+
+    if( GlExtensionCreate() )
+    {
+      TargetTexture();
+    }
+
+    mEglImageExtensions->DestroyImageKHR( eglImage );
+
+    mSetSource = false;
+  }
+}
+
 int NativeImageSource::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
 {
   switch (depth)
@@ -384,6 +502,29 @@ const char* NativeImageSource::GetCustomSamplerTypename()
   return SAMPLER_TYPE;
 }
 
+int NativeImageSource::GetEglImageTextureTarget()
+{
+  return GL_TEXTURE_EXTERNAL_OES;
+}
+
+bool NativeImageSource::CheckBlending( tbm_format format )
+{
+  if( mTbmFormat != format )
+  {
+    for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
+    {
+      if( format == FORMATS_BLENDING_REQUIRED[i] )
+      {
+        mBlendingRequired = true;
+        break;
+      }
+    }
+    mTbmFormat = format;
+  }
+
+  return mBlendingRequired;
+}
+
 } // namespace Adaptor
 
 } // namespace internal