X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fnpatch-loader.cpp;h=f0b154a3b82600b5e8c474465fb742b06d5cfb38;hp=f23f1cabc3236305d9c9086f87afdd91fe8b0cb7;hb=fb8417de823d79277c4c8ce4eca1b62c419cbb88;hpb=aa906fd49430568986b95c51bd29f3a58c33272f diff --git a/dali-toolkit/internal/visuals/npatch-loader.cpp b/dali-toolkit/internal/visuals/npatch-loader.cpp index f23f1ca..f0b154a 100644 --- a/dali-toolkit/internal/visuals/npatch-loader.cpp +++ b/dali-toolkit/internal/visuals/npatch-loader.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 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,10 +18,12 @@ // CLASS HEADER #include -// EXTERNAL HEADER +// INTERNAL HEADERS +#include + +// EXTERNAL HEADERS #include -#include -#include +#include namespace Dali { @@ -32,7 +34,16 @@ namespace Toolkit namespace Internal { +namespace +{ + +constexpr auto INVALID_CACHE_INDEX = int32_t{-1}; ///< Invalid Cache index +constexpr auto UNINITIALIZED_ID = int32_t{0}; ///< uninitialised id, use to initialize ids + +} // Anonymous namespace + NPatchLoader::NPatchLoader() +: mCurrentNPatchDataId(0) { } @@ -40,132 +51,144 @@ NPatchLoader::~NPatchLoader() { } -std::size_t NPatchLoader::Load( const std::string& url, const Rect< int >& border ) +NPatchData::NPatchDataId NPatchLoader::GenerateUniqueNPatchDataId() +{ + return mCurrentNPatchDataId++; +} + +std::size_t NPatchLoader::Load( TextureManager& textureManager, TextureUploadObserver* textureObserver, const std::string& url, const Rect< int >& border, bool& preMultiplyOnLoad, bool synchronousLoading ) { std::size_t hash = CalculateHash( url ); - OwnerContainer< Data* >::SizeType index = UNINITIALIZED_ID; - const OwnerContainer< Data* >::SizeType count = mCache.Count(); - int cachedIndex = -1; + OwnerContainer< NPatchData* >::SizeType index = UNINITIALIZED_ID; + const OwnerContainer< NPatchData* >::SizeType count = mCache.Count(); for( ; index < count; ++index ) { - if( mCache[ index ]->hash == hash ) + if( mCache[ index ]->GetHash() == hash ) { // hash match, check url as well in case of hash collision - if( mCache[ index ]->url == url ) + if(mCache[ index ]->GetUrl() == url) { // Use cached data - if( mCache[ index ]->border == border ) + if( mCache[ index ]->GetBorder() == border ) { - return index+1u; // valid indices are from 1 onwards + if( mCache[ index ]->GetLoadingState() == NPatchData::LoadingState::LOADING ) + { + mCache[ index ]->AddObserver( textureObserver ); + } + return mCache[ index ]->GetId(); // valid indices are from 1 onwards } else { - cachedIndex = index; - } - } - } - } + if( mCache[ index ]->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE ) + { + // Same url but border is different - use the existing texture + NPatchData* newData = new NPatchData(); + newData->SetId(GenerateUniqueNPatchDataId()); + newData->SetHash(hash); + newData->SetUrl(url); + newData->SetCroppedWidth(mCache[ index ]->GetCroppedWidth()); + newData->SetCroppedHeight(mCache[ index ]->GetCroppedHeight()); - if( cachedIndex != -1 ) - { - // Same url but border is different - use the existing texture - Data* data = new Data(); - data->hash = hash; - data->url = url; - data->croppedWidth = mCache[ cachedIndex ]->croppedWidth; - data->croppedHeight = mCache[ cachedIndex ]->croppedHeight; + newData->SetTextures(mCache[ index ]->GetTextures()); - data->textureSet = mCache[ cachedIndex ]->textureSet; + NPatchUtility::StretchRanges stretchRangesX; + stretchRangesX.PushBack( Uint16Pair( border.left, ( (newData->GetCroppedWidth() >= static_cast< unsigned int >( border.right )) ? newData->GetCroppedHeight() - border.right : 0 ) ) ); - NinePatchImage::StretchRanges stretchRangesX; - stretchRangesX.PushBack( Uint16Pair( border.left, data->croppedWidth - border.right ) ); + NPatchUtility::StretchRanges stretchRangesY; + stretchRangesY.PushBack( Uint16Pair( border.top, ( (newData->GetCroppedWidth() >= static_cast< unsigned int >( border.bottom )) ? newData->GetCroppedHeight() - border.bottom : 0 ) ) ); - NinePatchImage::StretchRanges stretchRangesY; - stretchRangesY.PushBack( Uint16Pair( border.top, data->croppedHeight - border.bottom ) ); + newData->SetStretchPixelsX(stretchRangesX); + newData->SetStretchPixelsY(stretchRangesY); + newData->SetBorder(border); - data->stretchPixelsX = stretchRangesX; - data->stretchPixelsY = stretchRangesY; - data->border = border; + newData->SetPreMultiplyOnLoad(mCache[ index ]->IsPreMultiplied()); - mCache.PushBack( data ); + newData->SetLoadingState(NPatchData::LoadingState::LOAD_COMPLETE); + newData->AddObserver( textureObserver ); - return mCache.Count(); // valid ids start from 1u - } + mCache.PushBack( newData ); - // got to the end so no match, decode N patch and append new item to cache - if( border == Rect< int >( 0, 0, 0, 0 ) ) - { - NinePatchImage ninePatch = NinePatchImage::New( url ); - if( ninePatch ) - { - BufferImage croppedImage = ninePatch.CreateCroppedBufferImage(); - if( croppedImage ) - { - Data* data = new Data(); - data->hash = hash; - data->url = url; - data->textureSet = TextureSet::New(); - TextureSetImage( data->textureSet, 0u, croppedImage ); - data->croppedWidth = croppedImage.GetWidth(); - data->croppedHeight = croppedImage.GetHeight(); - data->stretchPixelsX = ninePatch.GetStretchPixelsX(); - data->stretchPixelsY = ninePatch.GetStretchPixelsY(); - data->border = Rect< int >( 0, 0, 0, 0 ); - mCache.PushBack( data ); - - return mCache.Count(); // valid ids start from 1u + return newData->GetId(); // valid ids start from 1u + } + } } } } - else - { - // Load image from file - PixelData pixels = SyncImageLoader::Load( url ); - if( pixels ) - { - Data* data = new Data(); - data->hash = hash; - data->url = url; - data->croppedWidth = pixels.GetWidth(); - data->croppedHeight = pixels.GetHeight(); - Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() ); - texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() ); - - data->textureSet = TextureSet::New(); - data->textureSet.SetTexture( 0u, texture ); - - NinePatchImage::StretchRanges stretchRangesX; - stretchRangesX.PushBack( Uint16Pair( border.left, data->croppedWidth - border.right ) ); - - NinePatchImage::StretchRanges stretchRangesY; - stretchRangesY.PushBack( Uint16Pair( border.top, data->croppedHeight - border.bottom ) ); + // If this is new image loading, make new cache data + NPatchData* data; + data = new NPatchData(); + data->SetId(GenerateUniqueNPatchDataId()); + data->SetHash(hash); + data->SetUrl(url); + data->SetBorder(border); + data->SetPreMultiplyOnLoad(preMultiplyOnLoad); + data->AddObserver(textureObserver); + mCache.PushBack(data); + + auto preMultiplyOnLoading = preMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD + : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY; + + Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer( url, Dali::ImageDimensions(), FittingMode::DEFAULT, + SamplingMode::BOX_THEN_LINEAR, synchronousLoading, + data, true, preMultiplyOnLoading ); + + if( pixelBuffer ) + { + preMultiplyOnLoad = ( preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD ) ? true : false; + data->SetLoadedNPatchData( pixelBuffer, preMultiplyOnLoad ); + } - data->stretchPixelsX = stretchRangesX; - data->stretchPixelsY = stretchRangesY; - data->border = border; + return data->GetId(); +} - mCache.PushBack( data ); +int32_t NPatchLoader::GetCacheIndexFromId( const NPatchData::NPatchDataId id ) +{ + const unsigned int size = mCache.Count(); - return mCache.Count(); // valid ids start from 1u + for( unsigned int i = 0; i < size; ++i ) + { + if( mCache[i]->GetId() == id ) + { + return i; } } - return 0u; + return INVALID_CACHE_INDEX; } -bool NPatchLoader::GetNPatchData( std::size_t id, const Data*& data ) +bool NPatchLoader::GetNPatchData( const NPatchData::NPatchDataId id, const NPatchData*& data ) { - if( ( id > UNINITIALIZED_ID )&&( id <= mCache.Count() ) ) + int32_t cacheIndex = GetCacheIndexFromId(id); + if( cacheIndex != INVALID_CACHE_INDEX ) { - data = mCache[ id - 1u ]; // id's start from 1u + data = mCache[cacheIndex]; return true; } - data = NULL; + data = nullptr; return false; } +void NPatchLoader::Remove( std::size_t id, TextureUploadObserver* textureObserver ) +{ + int32_t cacheIndex = GetCacheIndexFromId(id); + if( cacheIndex == INVALID_CACHE_INDEX ) + { + return; + } + + NPatchData* data; + data = mCache[cacheIndex]; + + data->RemoveObserver(textureObserver); + + if(data->GetObserverCount() == 0) + { + mCache.Erase( mCache.Begin() + cacheIndex ); + } +} + } // namespace Internal } // namespace Toolkit