[3.0] Added ref/unref tbm_surface
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / native-image-source-impl-tizen.cpp
index 38000a9..e83af6c 100755 (executable)
@@ -22,6 +22,7 @@
 #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>
@@ -83,6 +84,7 @@ NativeImageSource::NativeImageSource( unsigned int width, unsigned int height, D
   mHeight( height ),
   mOwnTbmSurface( false ),
   mTbmSurface( NULL ),
+  mTbmFormat( 0 ),
   mBlendingRequired( false ),
   mColorDepth( depth ),
   mEglImageKHR( NULL ),
@@ -98,6 +100,7 @@ NativeImageSource::NativeImageSource( unsigned int width, unsigned int height, D
 
   if( mTbmSurface != NULL )
   {
+    tbm_surface_internal_ref( mTbmSurface );
     mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
     mWidth = tbm_surface_get_width( mTbmSurface );
     mHeight = tbm_surface_get_height( mTbmSurface );
@@ -183,13 +186,20 @@ tbm_surface_h NativeImageSource::GetSurfaceFromAny( Any source ) const
 
 NativeImageSource::~NativeImageSource()
 {
-  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\n" );
     }
   }
+  else
+  {
+    if( mTbmSurface != NULL )
+    {
+      tbm_surface_internal_unref( mTbmSurface );
+    }
+  }
 }
 
 Any NativeImageSource::GetNativeImageSource() const
@@ -309,9 +319,9 @@ 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\n" );
     }
@@ -319,24 +329,25 @@ void NativeImageSource::SetSource( Any source )
     mTbmSurface = NULL;
     mOwnTbmSurface = false;
   }
+  else
+  {
+    if( mTbmSurface != NULL )
+    {
+      tbm_surface_internal_unref( mTbmSurface );
+      mTbmSurface = NULL;
+    }
+  }
 
   mTbmSurface = GetSurfaceFromAny( source );
-  mSetSource = true;
 
   if( mTbmSurface != NULL )
   {
+    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 );
   }
-
-  if( mEglImageKHRContainer.Size() > 2 )
-  {
-    mEglImageExtensions->DestroyImageKHR(mEglImageKHRContainer[0]);
-    mEglImageKHRContainer.Erase( mEglImageKHRContainer.Begin() );
-  }
-
-  mEglImageKHRContainer.PushBack( mEglImageKHR );
 }
 
 bool NativeImageSource::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
@@ -394,7 +405,11 @@ 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 );
 
@@ -403,9 +418,12 @@ bool NativeImageSource::GlExtensionCreate()
 
 void NativeImageSource::GlExtensionDestroy()
 {
-  mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
+  if( mEglImageKHR )
+  {
+    mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
 
-  mEglImageKHR = NULL;
+    mEglImageKHR = NULL;
+  }
 }
 
 unsigned int NativeImageSource::TargetTexture()
@@ -417,9 +435,17 @@ unsigned int NativeImageSource::TargetTexture()
 
 void NativeImageSource::PrepareTexture()
 {
-  if( mSetSource && GlExtensionCreate() )
+  if( mSetSource )
   {
-    TargetTexture();
+    void* eglImage = mEglImageKHR;
+
+    if( GlExtensionCreate() )
+    {
+      TargetTexture();
+    }
+
+    mEglImageExtensions->DestroyImageKHR( eglImage );
+
     mSetSource = false;
   }
 }