2 * Copyright (c) 2016 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/devel-api/common/hash.h>
23 #include <dali/devel-api/images/texture-set-image.h>
34 NPatchLoader::NPatchLoader()
38 NPatchLoader::~NPatchLoader()
42 std::size_t NPatchLoader::Load( const std::string& url )
44 std::size_t hash = CalculateHash( url );
45 OwnerContainer< Data* >::SizeType index = UNINITIALIZED_ID;
46 const OwnerContainer< Data* >::SizeType count = mCache.Count();
47 for( ; index < count; ++index )
49 if( mCache[ index ]->hash == hash )
51 // hash match, check url as well in case of hash collision
52 if( mCache[ index ]->url == url )
54 return index+1u; // valid indices are from 1 onwards
58 // got to the end so no match, decode N patch and append new item to cache
59 NinePatchImage ninePatch = NinePatchImage::New( url );
62 BufferImage croppedImage = ninePatch.CreateCroppedBufferImage();
65 Data* data = new Data();
68 data->textureSet = TextureSet::New();
69 TextureSetImage( data->textureSet, 0u, croppedImage );
70 data->croppedWidth = croppedImage.GetWidth();
71 data->croppedHeight = croppedImage.GetHeight();
72 data->stretchPixelsX = ninePatch.GetStretchPixelsX();
73 data->stretchPixelsY = ninePatch.GetStretchPixelsY();
74 mCache.PushBack( data );
76 return mCache.Count(); // valid ids start from 1u
82 bool NPatchLoader::GetNPatchData( std::size_t id, const Data*& data )
84 if( ( id > UNINITIALIZED_ID )&&( id <= mCache.Count() ) )
86 data = mCache[ id - 1u ]; // id's start from 1u
93 } // namespace Internal
95 } // namespace Toolkit