/*
- * 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.
// CLASS HEADER
#include <dali-toolkit/internal/visuals/npatch-loader.h>
-// EXTERNAL HEADER
+// INTERNAL HEADERS
+#include <dali-toolkit/internal/visuals/rendering-addon.h>
+
+// EXTERNAL HEADERS
#include <dali/devel-api/common/hash.h>
-#include <dali/devel-api/images/texture-set-image.h>
-#include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
+#include <dali/integration-api/debug.h>
namespace Dali
{
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()
{
}
{
}
-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 )
{
// Use cached data
if( mCache[ index ]->border == border )
{
- return index+1u; // valid indices are from 1 onwards
+ if( mCache[ index ]->loadCompleted )
+ {
+ return index + 1u; // valid indices are from 1 onwards
+ }
+ data = mCache[ index ];
+ cachedIndex = index + 1u; // valid indices are from 1 onwards
+ break;
}
else
{
- cachedIndex = index;
+ 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
+ }
}
}
}
}
- if( cachedIndex != -1 )
+ if( cachedIndex == -1 )
{
- // Same url but border is different - use the existing texture
- Data* data = new Data();
+ data = new Data();
+ data->loadCompleted = false;
data->hash = hash;
data->url = url;
- data->croppedWidth = mCache[ cachedIndex ]->croppedWidth;
- data->croppedHeight = mCache[ cachedIndex ]->croppedHeight;
-
- data->textureSet = mCache[ cachedIndex ]->textureSet;
-
- NinePatchImage::StretchRanges stretchRangesX;
- stretchRangesX.PushBack( Uint16Pair( border.left, ( (data->croppedWidth >= static_cast< unsigned int >( border.right )) ? data->croppedWidth - border.right : 0 ) ) );
-
- NinePatchImage::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;
mCache.PushBack( data );
- return mCache.Count(); // valid ids start from 1u
+ cachedIndex = mCache.Count();
}
- // got to the end so no match, decode N patch and append new item to cache
- if( border == Rect< int >( 0, 0, 0, 0 ) )
+ 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 );
+
+ if( pixelBuffer )
{
- 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
- }
- }
+ NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer );
+ preMultiplyOnLoad = ( preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD ) ? true : false;
}
- 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 >= static_cast< unsigned int >( border.right )) ? data->croppedWidth - border.right : 0 ) ) );
- NinePatchImage::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;
+ return cachedIndex;
+}
- mCache.PushBack( data );
+void NPatchLoader::SetNPatchData( std::size_t id, Devel::PixelBuffer& pixelBuffer )
+{
+ Data* data;
+ data = mCache[ id - 1u ];
- return mCache.Count(); // valid ids start from 1u
- }
+ if( !data->loadCompleted )
+ {
+ NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer );
}
-
- return 0u;
}
bool NPatchLoader::GetNPatchData( std::size_t id, const Data*& data )