[Tizen] Restore Uploaded signal for BufferImage and ResourceImage
[platform/core/uifw/dali-core.git] / dali / internal / event / images / resource-image-impl.cpp
index b2a937d..ea72dcb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 // CLASS HEADER
 #include <dali/internal/event/images/resource-image-impl.h>
 
+// EXTERNAL INCLUDES
+#include <cstring> // for strcmp
+
 // INTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
 #include <dali/public-api/object/type-registry.h>
 #include <dali/integration-api/debug.h>
+#include <dali/integration-api/platform-abstraction.h>
 #include <dali/internal/event/common/thread-local-storage.h>
-#include <dali/internal/event/images/image-factory.h>
 #include <dali/internal/event/images/nine-patch-image-impl.h>
 #include <dali/internal/event/common/stage-impl.h>
 
@@ -38,22 +41,38 @@ namespace Internal
 namespace
 {
 
+// Signals
+
+const char* const SIGNAL_IMAGE_LOADING_FINISHED = "imageLoadingFinished";
+
+
 BaseHandle CreateImage()
 {
   ImagePtr image = ResourceImage::New();
   return Dali::Image(image.Get());
 }
 
-TypeRegistration mType( typeid(Dali::ResourceImage), typeid(Dali::Image), CreateImage );
+TypeRegistration mType( typeid( Dali::ResourceImage ), typeid( Dali::Image ), CreateImage );
+
+Dali::SignalConnectorType signalConnector1( mType, SIGNAL_IMAGE_LOADING_FINISHED, &ResourceImage::DoConnectSignal );
 
-Dali::SignalConnectorType signalConnector1(mType, Dali::ResourceImage::SIGNAL_IMAGE_LOADING_FINISHED, &ResourceImage::DoConnectSignal);
+}
 
+ResourceImage::ResourceImage()
+: Image(),
+  mLoadingFinished(),
+  mAttributes(),
+  mUrl(),
+  mLoadingState( Dali::ResourceLoading )
+{
 }
 
-ResourceImage::ResourceImage( LoadPolicy loadPol, ReleasePolicy releasePol )
-: Image( releasePol ),
-  mImageFactory( ThreadLocalStorage::Get().GetImageFactory() ),
-  mLoadPolicy(loadPol)
+ResourceImage::ResourceImage( const std::string& url, const ImageAttributes& attributes)
+: Image(),
+  mLoadingFinished(),
+  mAttributes(attributes),
+  mUrl(url),
+  mLoadingState( Dali::ResourceLoading )
 {
 }
 
@@ -64,30 +83,21 @@ ResourceImagePtr ResourceImage::New()
   return image;
 }
 
-ResourceImagePtr ResourceImage::New( const std::string& url, const Dali::ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol )
+ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttributes& attributes )
 {
   ResourceImagePtr image;
-  if( IsNinePatch( url ) )
+
+  if( NinePatchImage::IsNinePatchUrl( url ) )
   {
-    image = NinePatchImage::New( url, attributes, releasePol );
+    image = NinePatchImage::New( url );
   }
   else
   {
-    image = new ResourceImage( loadPol, releasePol );
+    image = new ResourceImage(url, attributes);
     image->Initialize();
-
-    // consider the requested size as natural size, 0 means we don't (yet) know it
-    image->mWidth = attributes.GetWidth();
-    image->mHeight = attributes.GetHeight();
-    image->mRequest = image->mImageFactory.RegisterRequest( url, &attributes );
-
-    if( Dali::ResourceImage::IMMEDIATE == loadPol )
-    {
-      // Trigger loading of the image on a as soon as it can be done
-      image->mTicket = image->mImageFactory.Load( *image->mRequest.Get() );
-      image->mTicket->AddObserver( *image );
-    }
+    image->Reload();
   }
+
   DALI_LOG_SET_OBJECT_STRING( image, url );
 
   return image;
@@ -95,31 +105,18 @@ ResourceImagePtr ResourceImage::New( const std::string& url, const Dali::ImageAt
 
 ResourceImage::~ResourceImage()
 {
-  if( mTicket )
-  {
-    mTicket->RemoveObserver( *this );
-    if( Stage::IsInstalled() )
-    {
-      mImageFactory.ReleaseTicket( mTicket.Get() );
-    }
-    mTicket.Reset();
-  }
 }
 
 bool ResourceImage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
 {
   bool connected( true );
-  DALI_ASSERT_DEBUG( dynamic_cast<ResourceImage*>( object ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
+  DALI_ASSERT_DEBUG( dynamic_cast<ResourceImage*>( object ) && "Failed to downcast from BaseObject to ResourceImage.\n" );
   ResourceImage* image = static_cast<ResourceImage*>(object);
 
-  if( Dali::ResourceImage::SIGNAL_IMAGE_LOADING_FINISHED == signalName )
+  if( 0 == strcmp( signalName.c_str(), SIGNAL_IMAGE_LOADING_FINISHED ) )
   {
     image->LoadingFinishedSignal().Connect( tracker, functor );
   }
-  else if(Dali::ResourceImage::SIGNAL_IMAGE_UPLOADED == signalName)
-  {
-    image->UploadedSignal().Connect( tracker, functor );
-  }
   else
   {
     // signalName does not match any signal
@@ -129,203 +126,85 @@ bool ResourceImage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterf
   return connected;
 }
 
-const Dali::ImageAttributes& ResourceImage::GetAttributes() const
+
+const ImageAttributes& ResourceImage::GetAttributes() const
 {
-  if( mTicket )
-  {
-    return mImageFactory.GetActualAttributes( mTicket );
-  }
-  else
-  {
-    return mImageFactory.GetRequestAttributes( mRequest );
-  }
+  return mAttributes;
 }
 
 const std::string& ResourceImage::GetUrl() const
 {
-  return mImageFactory.GetRequestPath( mRequest );
+  return mUrl;
 }
 
 void ResourceImage::Reload()
 {
-  if ( mRequest )
-  {
-    ResourceTicketPtr ticket = mImageFactory.Reload( *mRequest.Get() );
-    SetTicket( ticket.Get() );
-  }
-}
-
-unsigned int ResourceImage::GetWidth() const
-{
-  // if width is 0, it means we've not yet loaded the image
-  if( 0u == mWidth )
-  {
-    Size size;
-    mImageFactory.GetImageSize( mRequest, mTicket, size );
-    mWidth = size.width;
-    // The app will probably ask for the height immediately, so don't waste the synchronous file IO that ImageFactory may have just done:
-    DALI_ASSERT_DEBUG( 0 == mHeight || unsigned(size.height) == mHeight );
-    if( 0 == mHeight )
+  ThreadLocalStorage& tls = ThreadLocalStorage::Get();
+  Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
+  Integration::BitmapResourceType resourceType( ImageDimensions(mAttributes.GetWidth(), mAttributes.GetHeight()),
+                                                mAttributes.GetScalingMode(),
+                                                mAttributes.GetFilterMode(),
+                                                mAttributes.GetOrientationCorrection() );
+
+  // Note, bitmap is only destroyed when the image is destroyed.
+  Integration::ResourcePointer resource = platformAbstraction.LoadImageSynchronously( resourceType, mUrl );
+  if( resource )
+  {
+    Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
+    unsigned width  = bitmap->GetImageWidth();
+    unsigned height = bitmap->GetImageHeight();
+
+    //Create texture
+    Pixel::Format format = bitmap->GetPixelFormat();
+    mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, format, width, height );
+
+    //Upload data to the texture
+    size_t bufferSize = bitmap->GetBufferSize();
+    PixelDataPtr pixelData = PixelData::New( bitmap->GetBufferOwnership(), bufferSize, width, height, format,
+                                             static_cast< Dali::PixelData::ReleaseFunction >( bitmap->GetReleaseFunction() ) );
+    mTexture->Upload( pixelData );
+
+    mWidth = mAttributes.GetWidth();
+    if( mWidth == 0 )
     {
-      mHeight = size.height;
+      mWidth = width;
     }
-  }
-  return mWidth;
-}
 
-unsigned int ResourceImage::GetHeight() const
-{
-  if( 0u == mHeight )
-  {
-    Size size;
-    mImageFactory.GetImageSize( mRequest, mTicket, size );
-    mHeight = size.height;
-    DALI_ASSERT_DEBUG( 0 == mWidth || unsigned(size.width) == mWidth );
-    if( 0 == mWidth )
+    mHeight = mAttributes.GetHeight();
+    if( mHeight == 0 )
     {
-      mWidth = size.width;
+      mHeight = height;
     }
-  }
-  return mHeight;
-}
 
-Vector2 ResourceImage::GetNaturalSize() const
-{
-  Vector2 naturalSize(mWidth, mHeight);
-  if( 0u == mWidth || 0u == mHeight )
+    mLoadingState = Dali::ResourceLoadingSucceeded;
+
+  }
+  else
   {
-    mImageFactory.GetImageSize( mRequest, mTicket, naturalSize );
-    mWidth = naturalSize.width;
-    mHeight = naturalSize.height;
+    mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, 0u, 0u );
+    mWidth = mHeight = 0u;
+    mLoadingState = Dali::ResourceLoadingFailed;
   }
-  return naturalSize;
-}
 
-void ResourceImage::ResourceLoadingFailed(const ResourceTicket& ticket)
-{
+  UploadedSignal().Emit( Dali::Image( this ) );
   mLoadingFinished.Emit( Dali::ResourceImage( this ) );
 }
 
-void ResourceImage::ResourceLoadingSucceeded(const ResourceTicket& ticket)
-{
-  mLoadingFinished.Emit( Dali::ResourceImage( this ) );
-}
-
-void ResourceImage::Connect()
+unsigned int ResourceImage::GetWidth() const
 {
-  ++mConnectionCount;
-
-  if( mConnectionCount == 1 )
-  {
-    // ticket was thrown away when related actors went offstage or image loading on demand
-    if( !mTicket )
-    {
-      DALI_ASSERT_DEBUG( mRequest.Get() );
-      ResourceTicketPtr newTicket = mImageFactory.Load( *mRequest.Get() );
-      SetTicket( newTicket.Get() );
-    }
-  }
+  return mWidth;
 }
 
-void ResourceImage::Disconnect()
+unsigned int ResourceImage::GetHeight() const
 {
-  if( !mTicket )
-  {
-    return;
-  }
-
-  DALI_ASSERT_DEBUG( mConnectionCount > 0 );
-  --mConnectionCount;
-  if( mConnectionCount == 0 && mReleasePolicy == Dali::ResourceImage::UNUSED )
-  {
-    // release image memory when it's not visible anymore (decrease ref. count of texture)
-    SetTicket( NULL );
-  }
+  return mHeight;
 }
 
-bool ResourceImage::IsNinePatch( const std::string& url )
+Vector2 ResourceImage::GetNaturalSize() const
 {
-  bool match = false;
-
-  std::string::const_reverse_iterator iter = url.rbegin();
-  enum { SUFFIX, HASH, HASH_DOT, DONE } state = SUFFIX;
-  while(iter < url.rend())
-  {
-    switch(state)
-    {
-      case SUFFIX:
-      {
-        if(*iter == '.')
-        {
-          state = HASH;
-        }
-        else if(!isalnum(*iter))
-        {
-          state = DONE;
-        }
-      }
-      break;
-      case HASH:
-      {
-        if( *iter == '#' || *iter == '9' )
-        {
-          state = HASH_DOT;
-        }
-        else
-        {
-          state = DONE;
-        }
-      }
-      break;
-      case HASH_DOT:
-      {
-        if(*iter == '.')
-        {
-          match = true;
-        }
-        state = DONE; // Stop testing characters
-      }
-      break;
-      case DONE:
-      {
-      }
-      break;
-    }
-
-    // Satisfy prevent
-    if( state == DONE )
-    {
-      break;
-    }
-
-    ++iter;
-  }
-  return match;
+  return Vector2(mWidth, mHeight);
 }
 
-void ResourceImage::SetTicket( ResourceTicket* ticket )
-{
-  if( ticket == mTicket.Get() )
-  {
-    return;
-  }
-
-  if( mTicket )
-  {
-    mTicket->RemoveObserver( *this );
-    mImageFactory.ReleaseTicket( mTicket.Get() );
-  }
-
-  if( ticket )
-  {
-    mTicket.Reset( ticket );
-    mTicket->AddObserver( *this );
-  }
-  else
-  {
-    mTicket.Reset();
-  }
-}
 
 } // namespace Internal