[4.0] Change NativeImageSourceQueue
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / native-image-source-impl-tizen.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 38000a9..ea65232
  */
 
 // CLASS HEADER
-#include "native-image-source-impl.h"
+#include <native-image-source-impl-tizen.h>
 
 // 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
@@ -65,9 +66,9 @@ const int NUM_FORMATS_BLENDING_REQUIRED = 18;
 
 using Dali::Integration::PixelBuffer;
 
-NativeImageSource* NativeImageSource::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
+NativeImageSourceTizen* NativeImageSourceTizen::New( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
 {
-  NativeImageSource* image = new NativeImageSource( width, height, depth, nativeImageSource );
+  NativeImageSourceTizen* image = new NativeImageSourceTizen( width, height, depth, nativeImageSource );
   DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
 
   if( image )
@@ -78,33 +79,35 @@ NativeImageSource* NativeImageSource::New(unsigned int width, unsigned int heigh
   return image;
 }
 
-NativeImageSource::NativeImageSource( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
+NativeImageSourceTizen::NativeImageSourceTizen( uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
 : mWidth( width ),
   mHeight( height ),
   mOwnTbmSurface( false ),
   mTbmSurface( NULL ),
+  mTbmFormat( 0 ),
   mBlendingRequired( false ),
   mColorDepth( depth ),
   mEglImageKHR( NULL ),
+  mEglFactory( NULL ),
   mEglImageExtensions( NULL ),
-  mSetSource( false )
+  mSetSource( false ),
+  mNotification( NULL )
 {
   DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
-  EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
-  mEglImageExtensions = eglFactory.GetImageExtensions();
-  DALI_ASSERT_DEBUG( mEglImageExtensions );
+  mEglFactory = &( Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory() );
 
   mTbmSurface = GetSurfaceFromAny( nativeImageSource );
 
   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 );
   }
 }
 
-void NativeImageSource::Initialize()
+void NativeImageSourceTizen::Initialize()
 {
   if( mTbmSurface != NULL || mWidth == 0 || mHeight == 0 )
   {
@@ -164,7 +167,7 @@ void NativeImageSource::Initialize()
   mOwnTbmSurface = true;
 }
 
-tbm_surface_h NativeImageSource::GetSurfaceFromAny( Any source ) const
+tbm_surface_h NativeImageSourceTizen::GetSurfaceFromAny( Any source ) const
 {
   if( source.Empty() )
   {
@@ -181,23 +184,41 @@ tbm_surface_h NativeImageSource::GetSurfaceFromAny( Any source ) const
   }
 }
 
-NativeImageSource::~NativeImageSource()
+NativeImageSourceTizen::~NativeImageSourceTizen()
 {
-  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 );
+
+      if (mNotification != NULL) {
+          TriggerEventInterface* triggerEvent = static_cast<TriggerEventInterface* >(mNotification);
+          triggerEvent->Trigger();
+      }
+    }
+  }
 }
 
-Any NativeImageSource::GetNativeImageSource() const
+Any NativeImageSourceTizen::GetNativeImageSource() const
 {
   return Any( mTbmSurface );
 }
 
-bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
+
+void NativeImageSourceTizen::SetDestructorNotification(void* notification)
+{
+  mNotification = notification;
+}
+
+bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
 {
   if( mTbmSurface != NULL )
   {
@@ -238,7 +259,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];
           }
@@ -258,7 +279,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];
@@ -268,11 +289,7 @@ bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned&
       }
       default:
       {
-        DALI_LOG_WARNING( "Tbm surface has unsupported pixel format.\n" );
-
-        pixbuf.resize( 0 );
-        width = 0;
-        height = 0;
+        DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
 
         return false;
       }
@@ -294,7 +311,7 @@ bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned&
   return false;
 }
 
-bool NativeImageSource::EncodeToFile(const std::string& filename) const
+bool NativeImageSourceTizen::EncodeToFile(const std::string& filename) const
 {
   std::vector< unsigned char > pixbuf;
   unsigned int width(0), height(0);
@@ -307,11 +324,11 @@ bool NativeImageSource::EncodeToFile(const std::string& filename) const
   return false;
 }
 
-void NativeImageSource::SetSource( Any source )
+void NativeImageSourceTizen::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,27 +336,28 @@ 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 )
+bool NativeImageSourceTizen::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
 {
   uint32_t* formats;
   uint32_t formatNum;
@@ -390,89 +408,74 @@ bool NativeImageSource::IsColorDepthSupported( Dali::NativeImageSource::ColorDep
   return false;
 }
 
-bool NativeImageSource::GlExtensionCreate()
+bool NativeImageSourceTizen::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;
+  }
+
+  mEglImageExtensions = mEglFactory->GetImageExtensions();
+  DALI_ASSERT_DEBUG( mEglImageExtensions );
 
   mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
 
   return mEglImageKHR != NULL;
 }
 
-void NativeImageSource::GlExtensionDestroy()
+void NativeImageSourceTizen::GlExtensionDestroy()
 {
-  mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
+  if( mEglImageKHR )
+  {
+    mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
 
-  mEglImageKHR = NULL;
+    mEglImageKHR = NULL;
+  }
 }
 
-unsigned int NativeImageSource::TargetTexture()
+uint32_t NativeImageSourceTizen::TargetTexture()
 {
   mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
 
   return 0;
 }
 
-void NativeImageSource::PrepareTexture()
+void NativeImageSourceTizen::PrepareTexture()
 {
-  if( mSetSource && GlExtensionCreate() )
+  if( mSetSource )
   {
-    TargetTexture();
-    mSetSource = false;
-  }
-}
+    void* eglImage = mEglImageKHR;
 
-int NativeImageSource::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
-{
-  switch (depth)
-  {
-    case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
-    {
-      // ToDo: Get the default screen depth
-      return 32;
-    }
-    case Dali::NativeImageSource::COLOR_DEPTH_8:
-    {
-      return 8;
-    }
-    case Dali::NativeImageSource::COLOR_DEPTH_16:
-    {
-      return 16;
-    }
-    case Dali::NativeImageSource::COLOR_DEPTH_24:
-    {
-      return 24;
-    }
-    case Dali::NativeImageSource::COLOR_DEPTH_32:
+    if( GlExtensionCreate() )
     {
-      return 32;
-    }
-    default:
-    {
-      DALI_ASSERT_DEBUG(0 && "unknown color enum");
-      return 0;
+      TargetTexture();
     }
+
+    mEglImageExtensions->DestroyImageKHR( eglImage );
+
+    mSetSource = false;
   }
 }
 
-const char* NativeImageSource::GetCustomFragmentPreFix()
+const char* NativeImageSourceTizen::GetCustomFragmentPreFix()
 {
   return FRAGMENT_PREFIX;
 }
 
-const char* NativeImageSource::GetCustomSamplerTypename()
+const char* NativeImageSourceTizen::GetCustomSamplerTypename()
 {
   return SAMPLER_TYPE;
 }
 
-int NativeImageSource::GetEglImageTextureTarget()
+int NativeImageSourceTizen::GetEglImageTextureTarget()
 {
   return GL_TEXTURE_EXTERNAL_OES;
 }
 
-bool NativeImageSource::CheckBlending( tbm_format format )
+bool NativeImageSourceTizen::CheckBlending( tbm_format format )
 {
   if( mTbmFormat != format )
   {