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=2550001067b70ffeb87d02d2465192ad59182369;hp=5da7bd1f5b74bdd97da8c36f03ecf1d0d3607d2f;hb=f27c332dcf251d50ddfe3b2ab15ec2eaff5296b1;hpb=cc9366b8f2ebab68ef931414afa0ac8f8ba70199 diff --git a/dali-toolkit/internal/visuals/npatch-loader.cpp b/dali-toolkit/internal/visuals/npatch-loader.cpp index 5da7bd1..2550001 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,6 +34,52 @@ namespace Toolkit namespace Internal { +namespace NPatchBuffer +{ + +void SetLoadedNPatchData( NPatchLoader::Data* data, Devel::PixelBuffer& pixelBuffer ) +{ + if( data->border == Rect< int >( 0, 0, 0, 0 ) ) + { + NPatchUtility::ParseBorders( pixelBuffer, data->stretchPixelsX, data->stretchPixelsY ); + + // Crop the image + pixelBuffer.Crop( 1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2 ); + } + else + { + data->stretchPixelsX.PushBack( Uint16Pair( data->border.left, ( (pixelBuffer.GetWidth() >= static_cast< unsigned int >( data->border.right )) ? pixelBuffer.GetWidth() - data->border.right : 0 ) ) ); + data->stretchPixelsY.PushBack( Uint16Pair( data->border.top, ( (pixelBuffer.GetHeight() >= static_cast< unsigned int >( data->border.bottom )) ? pixelBuffer.GetHeight() - data->border.bottom : 0 ) ) ); + } + + data->croppedWidth = pixelBuffer.GetWidth(); + data->croppedHeight = pixelBuffer.GetHeight(); + + // Create opacity map + data->renderingMap = RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().BuildNPatch(pixelBuffer, data ) : nullptr; + + PixelData pixels = Devel::PixelBuffer::Convert( pixelBuffer ); // takes ownership of buffer + + Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() ); + texture.Upload( pixels ); + + data->textureSet = TextureSet::New(); + data->textureSet.SetTexture( 0u, texture ); + + data->loadCompleted = true; +} + +} // namespace NPatchBuffer + +NPatchLoader::Data::~Data() +{ + // If there is an opacity map, it has to be destroyed using addon call + if( renderingMap ) + { + RenderingAddOn::Get().DestroyNPatch( renderingMap ); + } +} + NPatchLoader::NPatchLoader() { } @@ -40,11 +88,14 @@ NPatchLoader::~NPatchLoader() { } -std::size_t NPatchLoader::Load( const std::string& url, const Rect< int >& border ) +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; + Data* data; + for( ; index < count; ++index ) { if( mCache[ index ]->hash == hash ) @@ -52,68 +103,99 @@ std::size_t NPatchLoader::Load( const std::string& url, const Rect< int >& borde // hash match, check url as well in case of hash collision if( mCache[ index ]->url == url ) { - return index+1u; // valid indices are from 1 onwards + // Use cached data + if( mCache[ index ]->border == border ) + { + if( mCache[ index ]->loadCompleted ) + { + return index + 1u; // valid indices are from 1 onwards + } + mCache[ index ]->observerList.PushBack( textureObserver ); + data = mCache[ index ]; + return index + 1u; // valid indices are from 1 onwards + } + else + { + if( mCache[ index ]->loadCompleted ) + { + // Same url but border is different - use the existing texture + Data* data = new Data(); + data->hash = hash; + data->url = url; + data->croppedWidth = mCache[ index ]->croppedWidth; + data->croppedHeight = mCache[ index ]->croppedHeight; + + data->textureSet = mCache[ index ]->textureSet; + + NPatchUtility::StretchRanges stretchRangesX; + stretchRangesX.PushBack( Uint16Pair( border.left, ( (data->croppedWidth >= static_cast< unsigned int >( border.right )) ? data->croppedWidth - border.right : 0 ) ) ); + + NPatchUtility::StretchRanges stretchRangesY; + stretchRangesY.PushBack( Uint16Pair( border.top, ( (data->croppedHeight >= static_cast< unsigned int >( border.bottom )) ? data->croppedHeight - border.bottom : 0 ) ) ); + + data->stretchPixelsX = stretchRangesX; + data->stretchPixelsY = stretchRangesY; + data->border = border; + + data->loadCompleted = mCache[ index ]->loadCompleted; + + mCache.PushBack( data ); + + return mCache.Count(); // valid ids start from 1u + } + } } } } - // 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(); - mCache.PushBack( data ); - - return mCache.Count(); // valid ids start from 1u - } - } - } - else + + if( cachedIndex == -1 ) { - // 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(); + data = new Data(); + data->loadCompleted = false; + data->hash = hash; + data->url = url; + data->border = border; - Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() ); - texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() ); + mCache.PushBack( data ); - data->textureSet = TextureSet::New(); - data->textureSet.SetTexture( 0u, texture ); + cachedIndex = mCache.Count(); + } - NinePatchImage::StretchRanges stretchRangesX; - stretchRangesX.PushBack( Uint16Pair( border.left, data->croppedWidth - border.right ) ); + 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, + textureObserver, true, preMultiplyOnLoading ); - NinePatchImage::StretchRanges stretchRangesY; - stretchRangesY.PushBack( Uint16Pair( border.top, data->croppedHeight - border.bottom ) ); + if( pixelBuffer ) + { + NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer ); + preMultiplyOnLoad = ( preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD ) ? true : false; + } - data->stretchPixelsX = stretchRangesX; - data->stretchPixelsY = stretchRangesY; + return cachedIndex; +} - mCache.PushBack( data ); +void NPatchLoader::SetNPatchData( bool loadSuccess, std::size_t id, Devel::PixelBuffer& pixelBuffer, const Internal::VisualUrl& url, bool preMultiplied ) +{ + Data* data; + data = mCache[ id - 1u ]; - return mCache.Count(); // valid ids start from 1u - } + // To prevent recursion. + // data->loadCompleted will be set true in the NPatchBuffer::SetLoadedNPatchData when the first observer called this method. + if( data->loadCompleted ) + { + return; } - return 0u; + NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer ); + + while( data->observerList.Count() ) + { + TextureUploadObserver* observer = data->observerList[0]; + observer->LoadComplete( loadSuccess, Devel::PixelBuffer(), url, preMultiplied ); + data->observerList.Erase( data->observerList.begin() ); + } } bool NPatchLoader::GetNPatchData( std::size_t id, const Data*& data )