2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/visuals/npatch-loader.h>
22 #include <dali-toolkit/internal/visuals/rendering-addon.h>
25 #include <dali/devel-api/common/hash.h>
26 #include <dali/integration-api/debug.h>
37 namespace NPatchBuffer
40 void SetLoadedNPatchData( NPatchLoader::Data* data, Devel::PixelBuffer& pixelBuffer )
42 if( data->border == Rect< int >( 0, 0, 0, 0 ) )
44 NPatchUtility::ParseBorders( pixelBuffer, data->stretchPixelsX, data->stretchPixelsY );
47 pixelBuffer.Crop( 1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2 );
51 data->stretchPixelsX.PushBack( Uint16Pair( data->border.left, ( (pixelBuffer.GetWidth() >= static_cast< unsigned int >( data->border.right )) ? pixelBuffer.GetWidth() - data->border.right : 0 ) ) );
52 data->stretchPixelsY.PushBack( Uint16Pair( data->border.top, ( (pixelBuffer.GetHeight() >= static_cast< unsigned int >( data->border.bottom )) ? pixelBuffer.GetHeight() - data->border.bottom : 0 ) ) );
55 data->croppedWidth = pixelBuffer.GetWidth();
56 data->croppedHeight = pixelBuffer.GetHeight();
59 data->renderingMap = RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().BuildNPatch(pixelBuffer, data ) : nullptr;
61 PixelData pixels = Devel::PixelBuffer::Convert( pixelBuffer ); // takes ownership of buffer
63 Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
64 texture.Upload( pixels );
66 data->textureSet = TextureSet::New();
67 data->textureSet.SetTexture( 0u, texture );
69 data->loadCompleted = true;
72 } // namespace NPatchBuffer
74 NPatchLoader::Data::~Data()
76 // If there is an opacity map, it has to be destroyed using addon call
79 RenderingAddOn::Get().DestroyNPatch( renderingMap );
83 NPatchLoader::NPatchLoader()
87 NPatchLoader::~NPatchLoader()
91 std::size_t NPatchLoader::Load( TextureManager& textureManager, TextureUploadObserver* textureObserver, const std::string& url, const Rect< int >& border, bool& preMultiplyOnLoad, bool synchronousLoading )
93 std::size_t hash = CalculateHash( url );
94 OwnerContainer< Data* >::SizeType index = UNINITIALIZED_ID;
95 const OwnerContainer< Data* >::SizeType count = mCache.Count();
99 for( ; index < count; ++index )
101 if( mCache[ index ]->hash == hash )
103 // hash match, check url as well in case of hash collision
104 if( mCache[ index ]->url == url )
107 if( mCache[ index ]->border == border )
109 if( mCache[ index ]->loadCompleted )
111 return index + 1u; // valid indices are from 1 onwards
113 data = mCache[ index ];
114 cachedIndex = index + 1u; // valid indices are from 1 onwards
119 if( mCache[ index ]->loadCompleted )
121 // Same url but border is different - use the existing texture
122 Data* data = new Data();
125 data->croppedWidth = mCache[ index ]->croppedWidth;
126 data->croppedHeight = mCache[ index ]->croppedHeight;
128 data->textureSet = mCache[ index ]->textureSet;
130 NPatchUtility::StretchRanges stretchRangesX;
131 stretchRangesX.PushBack( Uint16Pair( border.left, ( (data->croppedWidth >= static_cast< unsigned int >( border.right )) ? data->croppedWidth - border.right : 0 ) ) );
133 NPatchUtility::StretchRanges stretchRangesY;
134 stretchRangesY.PushBack( Uint16Pair( border.top, ( (data->croppedHeight >= static_cast< unsigned int >( border.bottom )) ? data->croppedHeight - border.bottom : 0 ) ) );
136 data->stretchPixelsX = stretchRangesX;
137 data->stretchPixelsY = stretchRangesY;
138 data->border = border;
140 data->loadCompleted = mCache[ index ]->loadCompleted;
142 mCache.PushBack( data );
144 return mCache.Count(); // valid ids start from 1u
151 if( cachedIndex == -1 )
154 data->loadCompleted = false;
157 data->border = border;
159 mCache.PushBack( data );
161 cachedIndex = mCache.Count();
164 auto preMultiplyOnLoading = preMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
165 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
166 Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer( url, Dali::ImageDimensions(), FittingMode::DEFAULT,
167 SamplingMode::BOX_THEN_LINEAR, synchronousLoading,
168 textureObserver, true, preMultiplyOnLoading );
172 NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer );
173 preMultiplyOnLoad = ( preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD ) ? true : false;
179 void NPatchLoader::SetNPatchData( std::size_t id, Devel::PixelBuffer& pixelBuffer )
182 data = mCache[ id - 1u ];
184 if( !data->loadCompleted )
186 NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer );
190 bool NPatchLoader::GetNPatchData( std::size_t id, const Data*& data )
192 if( ( id > UNINITIALIZED_ID )&&( id <= mCache.Count() ) )
194 data = mCache[ id - 1u ]; // id's start from 1u
201 } // namespace Internal
203 } // namespace Toolkit