X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali%2Finternal%2Fevent%2Fimages%2Fresource-image-impl.cpp;h=dc116cc9e9c1f1d8bc647e6fbfac9cc2dfe06c32;hb=0b46c533530b62d898765b28eada591f2ce347d5;hp=b2a937dc6652851acbdd7029e9a7a6f586208f9d;hpb=f47a49768010de60f06c22065ad3f293f019046a;p=platform%2Fcore%2Fuifw%2Fdali-core.git diff --git a/dali/internal/event/images/resource-image-impl.cpp b/dali/internal/event/images/resource-image-impl.cpp index b2a937d..dc116cc 100644 --- a/dali/internal/event/images/resource-image-impl.cpp +++ b/dali/internal/event/images/resource-image-impl.cpp @@ -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. @@ -18,12 +18,15 @@ // CLASS HEADER #include +// EXTERNAL INCLUDES +#include // for strcmp + // INTERNAL INCLUDES #include #include #include +#include #include -#include #include #include @@ -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( object ) && "Resource ticket not ImageTicket subclass for image resource.\n" ); + DALI_ASSERT_DEBUG( dynamic_cast( object ) && "Failed to downcast from BaseObject to ResourceImage.\n" ); ResourceImage* image = static_cast(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,84 @@ 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( 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) -{ 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