/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
#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>
namespace Internal
{
-namespace
-{
-
-// Signals
-
-const char* const SIGNAL_IMAGE_LOADING_FINISHED = "imageLoadingFinished";
-
-BaseHandle CreateImage()
+ResourceImage::ResourceImage()
+: Image(),
+ mLoadingFinished(),
+ mAttributes(),
+ mUrl(),
+ mLoadingState( Dali::ResourceLoading )
{
- ImagePtr image = ResourceImage::New();
- return Dali::Image(image.Get());
}
-TypeRegistration mType( typeid( Dali::ResourceImage ), typeid( Dali::Image ), CreateImage );
-
-Dali::SignalConnectorType signalConnector1( mType, SIGNAL_IMAGE_LOADING_FINISHED, &ResourceImage::DoConnectSignal );
-
-}
-
-ResourceImage::ResourceImage()
+ResourceImage::ResourceImage( const std::string& url, const ImageAttributes& attributes)
: Image(),
- mImageFactory( ThreadLocalStorage::Get().GetImageFactory() )
+ mLoadingFinished(),
+ mAttributes(attributes),
+ mUrl(url),
+ mLoadingState( Dali::ResourceLoading )
{
}
ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttributes& attributes )
{
ResourceImagePtr image;
+
if( NinePatchImage::IsNinePatchUrl( url ) )
{
image = NinePatchImage::New( url );
}
else
{
- image = new ResourceImage();
+ 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 );
- image->mTicket = image->mImageFactory.Load( *image->mRequest.Get() );
- image->mTicket->AddObserver( *image );
+ image->Reload();
}
+
DALI_LOG_SET_OBJECT_STRING( image, url );
return image;
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" );
- ResourceImage* image = static_cast<ResourceImage*>(object);
-
- if( 0 == strcmp( signalName.c_str(), SIGNAL_IMAGE_LOADING_FINISHED ) )
- {
- image->LoadingFinishedSignal().Connect( tracker, functor );
- }
- else
- {
- // signalName does not match any signal
- connected = false;
- }
-
- 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
+ uint32_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 )
- {
- // release image memory when it's not visible anymore (decrease ref. count of texture)
- SetTicket( NULL );
- }
+ return Vector2( static_cast<float>( mWidth ), static_cast<float>( 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