[Tizen] Restore Uploaded signal for BufferImage and ResourceImage
[platform/core/uifw/dali-core.git] / dali / internal / event / images / resource-image-impl.cpp
index de87657..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.
@@ -25,8 +25,8 @@
 #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>
 
@@ -43,7 +43,8 @@ namespace
 
 // Signals
 
-const char* const SIGNAL_IMAGE_LOADING_FINISHED = "image-loading-finished";
+const char* const SIGNAL_IMAGE_LOADING_FINISHED = "imageLoadingFinished";
+
 
 BaseHandle CreateImage()
 {
@@ -57,10 +58,21 @@ Dali::SignalConnectorType signalConnector1( mType, SIGNAL_IMAGE_LOADING_FINISHED
 
 }
 
-ResourceImage::ResourceImage( LoadPolicy loadPol, ReleasePolicy releasePol )
-: Image( releasePol ),
-  mImageFactory( ThreadLocalStorage::Get().GetImageFactory() ),
-  mLoadPolicy(loadPol)
+ResourceImage::ResourceImage()
+: Image(),
+  mLoadingFinished(),
+  mAttributes(),
+  mUrl(),
+  mLoadingState( Dali::ResourceLoading )
+{
+}
+
+ResourceImage::ResourceImage( const std::string& url, const ImageAttributes& attributes)
+: Image(),
+  mLoadingFinished(),
+  mAttributes(attributes),
+  mUrl(url),
+  mLoadingState( Dali::ResourceLoading )
 {
 }
 
@@ -71,30 +83,21 @@ ResourceImagePtr ResourceImage::New()
   return image;
 }
 
-ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol )
+ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttributes& attributes )
 {
   ResourceImagePtr image;
+
   if( NinePatchImage::IsNinePatchUrl( url ) )
   {
-    image = NinePatchImage::New( url, 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;
@@ -102,21 +105,12 @@ ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttribut
 
 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( 0 == strcmp( signalName.c_str(), SIGNAL_IMAGE_LOADING_FINISHED ) )
@@ -132,140 +126,85 @@ bool ResourceImage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterf
   return connected;
 }
 
+
 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;
-    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;
-    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)
+unsigned int ResourceImage::GetWidth() const
 {
-  mLoadingFinished.Emit( Dali::ResourceImage( this ) );
+  return mWidth;
 }
 
-void ResourceImage::Connect()
+unsigned int ResourceImage::GetHeight() 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 mHeight;
 }
 
-void ResourceImage::Disconnect()
+Vector2 ResourceImage::GetNaturalSize() 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 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