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>
24 #include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
35 NPatchLoader::NPatchLoader()
39 NPatchLoader::~NPatchLoader()
43 std::size_t NPatchLoader::Load( const std::string& url, const Rect< int >& border )
45 std::size_t hash = CalculateHash( url );
46 OwnerContainer< Data* >::SizeType index = UNINITIALIZED_ID;
47 const OwnerContainer< Data* >::SizeType count = mCache.Count();
48 for( ; index < count; ++index )
50 if( mCache[ index ]->hash == hash )
52 // hash match, check url as well in case of hash collision
53 if( mCache[ index ]->url == url )
55 return index+1u; // valid indices are from 1 onwards
59 // got to the end so no match, decode N patch and append new item to cache
60 if( border == Rect< int >( 0, 0, 0, 0 ) )
62 NinePatchImage ninePatch = NinePatchImage::New( url );
65 BufferImage croppedImage = ninePatch.CreateCroppedBufferImage();
68 Data* data = new Data();
71 data->textureSet = TextureSet::New();
72 TextureSetImage( data->textureSet, 0u, croppedImage );
73 data->croppedWidth = croppedImage.GetWidth();
74 data->croppedHeight = croppedImage.GetHeight();
75 data->stretchPixelsX = ninePatch.GetStretchPixelsX();
76 data->stretchPixelsY = ninePatch.GetStretchPixelsY();
77 mCache.PushBack( data );
79 return mCache.Count(); // valid ids start from 1u
85 // Load image from file
86 PixelData pixels = SyncImageLoader::Load( url );
89 Data* data = new Data();
92 data->croppedWidth = pixels.GetWidth();
93 data->croppedHeight = pixels.GetHeight();
95 Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
96 texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() );
98 data->textureSet = TextureSet::New();
99 data->textureSet.SetTexture( 0u, texture );
101 NinePatchImage::StretchRanges stretchRangesX;
102 stretchRangesX.PushBack( Uint16Pair( border.left, data->croppedWidth - border.right ) );
104 NinePatchImage::StretchRanges stretchRangesY;
105 stretchRangesY.PushBack( Uint16Pair( border.top, data->croppedHeight - border.bottom ) );
107 data->stretchPixelsX = stretchRangesX;
108 data->stretchPixelsY = stretchRangesY;
110 mCache.PushBack( data );
112 return mCache.Count(); // valid ids start from 1u
119 bool NPatchLoader::GetNPatchData( std::size_t id, const Data*& data )
121 if( ( id > UNINITIALIZED_ID )&&( id <= mCache.Count() ) )
123 data = mCache[ id - 1u ]; // id's start from 1u
130 } // namespace Internal
132 } // namespace Toolkit