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();
50 for( ; index < count; ++index )
52 if( mCache[ index ]->hash == hash )
54 // hash match, check url as well in case of hash collision
55 if( mCache[ index ]->url == url )
58 if( mCache[ index ]->border == border )
60 return index+1u; // valid indices are from 1 onwards
70 if( cachedIndex != -1 )
72 // Same url but border is different - use the existing texture
73 Data* data = new Data();
76 data->croppedWidth = mCache[ cachedIndex ]->croppedWidth;
77 data->croppedHeight = mCache[ cachedIndex ]->croppedHeight;
79 data->textureSet = mCache[ cachedIndex ]->textureSet;
81 NinePatchImage::StretchRanges stretchRangesX;
82 stretchRangesX.PushBack( Uint16Pair( border.left, ( (data->croppedWidth >= static_cast< unsigned int >( border.right )) ? data->croppedWidth - border.right : 0 ) ) );
84 NinePatchImage::StretchRanges stretchRangesY;
85 stretchRangesY.PushBack( Uint16Pair( border.top, ( (data->croppedHeight >= static_cast< unsigned int >( border.bottom )) ? data->croppedHeight - border.bottom : 0 ) ) );
87 data->stretchPixelsX = stretchRangesX;
88 data->stretchPixelsY = stretchRangesY;
89 data->border = border;
91 mCache.PushBack( data );
93 return mCache.Count(); // valid ids start from 1u
96 // got to the end so no match, decode N patch and append new item to cache
97 if( border == Rect< int >( 0, 0, 0, 0 ) )
99 NinePatchImage ninePatch = NinePatchImage::New( url );
102 BufferImage croppedImage = ninePatch.CreateCroppedBufferImage();
105 Data* data = new Data();
108 data->textureSet = TextureSet::New();
109 TextureSetImage( data->textureSet, 0u, croppedImage );
110 data->croppedWidth = croppedImage.GetWidth();
111 data->croppedHeight = croppedImage.GetHeight();
112 data->stretchPixelsX = ninePatch.GetStretchPixelsX();
113 data->stretchPixelsY = ninePatch.GetStretchPixelsY();
114 data->border = Rect< int >( 0, 0, 0, 0 );
115 mCache.PushBack( data );
117 return mCache.Count(); // valid ids start from 1u
123 // Load image from file
124 PixelData pixels = SyncImageLoader::Load( url );
127 Data* data = new Data();
130 data->croppedWidth = pixels.GetWidth();
131 data->croppedHeight = pixels.GetHeight();
133 Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
134 texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() );
136 data->textureSet = TextureSet::New();
137 data->textureSet.SetTexture( 0u, texture );
139 NinePatchImage::StretchRanges stretchRangesX;
140 stretchRangesX.PushBack( Uint16Pair( border.left, ( (data->croppedWidth >= static_cast< unsigned int >( border.right )) ? data->croppedWidth - border.right : 0 ) ) );
142 NinePatchImage::StretchRanges stretchRangesY;
143 stretchRangesY.PushBack( Uint16Pair( border.top, ( (data->croppedHeight >= static_cast< unsigned int >( border.bottom )) ? data->croppedHeight - border.bottom : 0 ) ) );
145 data->stretchPixelsX = stretchRangesX;
146 data->stretchPixelsY = stretchRangesY;
147 data->border = border;
149 mCache.PushBack( data );
151 return mCache.Count(); // valid ids start from 1u
158 bool NPatchLoader::GetNPatchData( std::size_t id, const Data*& data )
160 if( ( id > UNINITIALIZED_ID )&&( id <= mCache.Count() ) )
162 data = mCache[ id - 1u ]; // id's start from 1u
169 } // namespace Internal
171 } // namespace Toolkit