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/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
24 #include <dali/devel-api/common/hash.h>
25 #include <dali/integration-api/debug.h>
36 namespace NPatchBuffer
39 void GetRedOffsetAndMask( Dali::Pixel::Format pixelFormat, int& byteOffset, int& bitMask )
45 case Dali::Pixel::LA88:
51 case Dali::Pixel::RGB888:
52 case Dali::Pixel::RGB8888:
53 case Dali::Pixel::RGBA8888:
59 case Dali::Pixel::BGR8888:
60 case Dali::Pixel::BGRA8888:
66 case Dali::Pixel::RGB565:
72 case Dali::Pixel::BGR565:
78 case Dali::Pixel::RGBA4444:
84 case Dali::Pixel::BGRA4444:
90 case Dali::Pixel::RGBA5551:
96 case Dali::Pixel::BGRA5551:
102 case Dali::Pixel::INVALID:
103 case Dali::Pixel::COMPRESSED_R11_EAC:
104 case Dali::Pixel::COMPRESSED_SIGNED_R11_EAC:
105 case Dali::Pixel::COMPRESSED_RG11_EAC:
106 case Dali::Pixel::COMPRESSED_SIGNED_RG11_EAC:
107 case Dali::Pixel::COMPRESSED_RGB8_ETC2:
108 case Dali::Pixel::COMPRESSED_SRGB8_ETC2:
109 case Dali::Pixel::COMPRESSED_RGB8_ETC1:
110 case Dali::Pixel::COMPRESSED_RGB_PVRTC_4BPPV1:
111 case Dali::Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
112 case Dali::Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
113 case Dali::Pixel::COMPRESSED_RGBA8_ETC2_EAC:
114 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
115 case Dali::Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR:
116 case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x4_KHR:
117 case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x5_KHR:
118 case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x5_KHR:
119 case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x6_KHR:
120 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x5_KHR:
121 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x6_KHR:
122 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x8_KHR:
123 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x5_KHR:
124 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR:
125 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x8_KHR:
126 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x10_KHR:
127 case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x10_KHR:
128 case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x12_KHR:
129 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
130 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
131 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
132 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
133 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
134 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
135 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
136 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
137 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
138 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
139 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
140 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
141 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
142 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
144 DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
149 case Dali::Pixel::RGB16F:
150 case Dali::Pixel::RGB32F:
152 DALI_LOG_ERROR("Pixel format not compatible.\n");
160 Uint16Pair ParseRange( unsigned int& index, unsigned int width, unsigned char* pixel, unsigned int pixelStride, int testByte, int testBits, int testValue )
162 unsigned int start = 0xFFFF;
163 for( ; index < width; ++index, pixel += pixelStride )
165 if( ( pixel[ testByte ] & testBits ) == testValue )
169 pixel += pixelStride;
174 unsigned int end = width;
175 for( ; index < width; ++index, pixel += pixelStride )
177 if( ( pixel[ testByte ] & testBits ) != testValue )
181 pixel += pixelStride;
186 return Uint16Pair( start, end );
189 void ParseBorders( Devel::PixelBuffer& pixelBuffer, NPatchLoader::Data* data )
191 data->stretchPixelsX.Clear();
192 data->stretchPixelsY.Clear();
194 Pixel::Format pixelFormat = pixelBuffer.GetPixelFormat();
198 Pixel::GetAlphaOffsetAndMask( pixelFormat, alphaByte, alphaBits );
200 int testByte = alphaByte;
201 int testBits = alphaBits;
202 int testValue = alphaBits; // Opaque == stretch
205 GetRedOffsetAndMask( pixelFormat, testByte, testBits );
206 testValue = 0; // Black == stretch
209 unsigned int bytesPerPixel = Pixel::GetBytesPerPixel( pixelFormat );
210 unsigned int width = pixelBuffer.GetWidth();
211 unsigned int height = pixelBuffer.GetHeight();
212 unsigned char* srcPixels = pixelBuffer.GetBuffer();
213 unsigned int srcStride = width * bytesPerPixel;
216 unsigned char* top = srcPixels + bytesPerPixel;
217 unsigned int index = 0;
219 for( ; index < width - 2; )
221 Uint16Pair range = ParseRange( index, width - 2, top, bytesPerPixel, testByte, testBits, testValue );
222 if( range.GetX() != 0xFFFF )
224 data->stretchPixelsX.PushBack( range );
229 unsigned char* left = srcPixels + srcStride;
231 for( ; index < height - 2; )
233 Uint16Pair range = ParseRange( index, height - 2, left, srcStride, testByte, testBits, testValue );
234 if( range.GetX() != 0xFFFF )
236 data->stretchPixelsY.PushBack( range );
240 // If there are no stretch pixels then make the entire image stretchable
241 if( data->stretchPixelsX.Size() == 0 )
243 data->stretchPixelsX.PushBack( Uint16Pair( 0, width - 2 ) );
245 if( data->stretchPixelsY.Size() == 0 )
247 data->stretchPixelsY.PushBack( Uint16Pair( 0, height - 2 ) );
251 } // namespace NPatchBuffer
253 NPatchLoader::NPatchLoader()
257 NPatchLoader::~NPatchLoader()
261 std::size_t NPatchLoader::Load( const std::string& url, const Rect< int >& border, bool& preMultiplyOnLoad )
263 std::size_t hash = CalculateHash( url );
264 OwnerContainer< Data* >::SizeType index = UNINITIALIZED_ID;
265 const OwnerContainer< Data* >::SizeType count = mCache.Count();
266 int cachedIndex = -1;
268 for( ; index < count; ++index )
270 if( mCache[ index ]->hash == hash )
272 // hash match, check url as well in case of hash collision
273 if( mCache[ index ]->url == url )
276 if( mCache[ index ]->border == border )
278 return index+1u; // valid indices are from 1 onwards
288 if( cachedIndex != -1 )
290 // Same url but border is different - use the existing texture
291 Data* data = new Data();
294 data->croppedWidth = mCache[ cachedIndex ]->croppedWidth;
295 data->croppedHeight = mCache[ cachedIndex ]->croppedHeight;
297 data->textureSet = mCache[ cachedIndex ]->textureSet;
299 StretchRanges stretchRangesX;
300 stretchRangesX.PushBack( Uint16Pair( border.left, ( (data->croppedWidth >= static_cast< unsigned int >( border.right )) ? data->croppedWidth - border.right : 0 ) ) );
302 StretchRanges stretchRangesY;
303 stretchRangesY.PushBack( Uint16Pair( border.top, ( (data->croppedHeight >= static_cast< unsigned int >( border.bottom )) ? data->croppedHeight - border.bottom : 0 ) ) );
305 data->stretchPixelsX = stretchRangesX;
306 data->stretchPixelsY = stretchRangesY;
307 data->border = border;
309 mCache.PushBack( data );
311 return mCache.Count(); // valid ids start from 1u
314 // got to the end so no match, decode N patch and append new item to cache
315 Devel::PixelBuffer pixelBuffer = Dali::LoadImageFromFile( url, ImageDimensions(), FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, true );
318 Data* data = new Data();
322 if( border == Rect< int >( 0, 0, 0, 0 ) )
324 NPatchBuffer::ParseBorders( pixelBuffer, data );
326 data->border = Rect< int >( 0, 0, 0, 0 );
329 pixelBuffer.Crop( 1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2 );
333 data->stretchPixelsX.PushBack( Uint16Pair( border.left, ( (pixelBuffer.GetWidth() >= static_cast< unsigned int >( border.right )) ? pixelBuffer.GetWidth() - border.right : 0 ) ) );
334 data->stretchPixelsY.PushBack( Uint16Pair( border.top, ( (pixelBuffer.GetHeight() >= static_cast< unsigned int >( border.bottom )) ? pixelBuffer.GetHeight() - border.bottom : 0 ) ) );
335 data->border = border;
338 data->croppedWidth = pixelBuffer.GetWidth();
339 data->croppedHeight = pixelBuffer.GetHeight();
341 if( preMultiplyOnLoad && Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
343 pixelBuffer.MultiplyColorByAlpha();
347 preMultiplyOnLoad = false;
350 PixelData pixels = Devel::PixelBuffer::Convert( pixelBuffer ); // takes ownership of buffer
352 Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
353 texture.Upload( pixels );
355 data->textureSet = TextureSet::New();
356 data->textureSet.SetTexture( 0u, texture );
358 mCache.PushBack( data );
360 return mCache.Count(); // valid ids start from 1u
366 bool NPatchLoader::GetNPatchData( std::size_t id, const Data*& data )
368 if( ( id > UNINITIALIZED_ID )&&( id <= mCache.Count() ) )
370 data = mCache[ id - 1u ]; // id's start from 1u
377 } // namespace Internal
379 } // namespace Toolkit