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/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/common/hash.h>
24 #include <dali/integration-api/debug.h>
35 namespace NPatchBuffer
38 void GetRedOffsetAndMask( Dali::Pixel::Format pixelFormat, int& byteOffset, int& bitMask )
44 case Dali::Pixel::LA88:
50 case Dali::Pixel::RGB888:
51 case Dali::Pixel::RGB8888:
52 case Dali::Pixel::RGBA8888:
58 case Dali::Pixel::BGR8888:
59 case Dali::Pixel::BGRA8888:
65 case Dali::Pixel::RGB565:
71 case Dali::Pixel::BGR565:
77 case Dali::Pixel::RGBA4444:
83 case Dali::Pixel::BGRA4444:
89 case Dali::Pixel::RGBA5551:
95 case Dali::Pixel::BGRA5551:
101 case Dali::Pixel::INVALID:
102 case Dali::Pixel::COMPRESSED_R11_EAC:
103 case Dali::Pixel::COMPRESSED_SIGNED_R11_EAC:
104 case Dali::Pixel::COMPRESSED_RG11_EAC:
105 case Dali::Pixel::COMPRESSED_SIGNED_RG11_EAC:
106 case Dali::Pixel::COMPRESSED_RGB8_ETC2:
107 case Dali::Pixel::COMPRESSED_SRGB8_ETC2:
108 case Dali::Pixel::COMPRESSED_RGB8_ETC1:
109 case Dali::Pixel::COMPRESSED_RGB_PVRTC_4BPPV1:
110 case Dali::Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
111 case Dali::Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
112 case Dali::Pixel::COMPRESSED_RGBA8_ETC2_EAC:
113 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
114 case Dali::Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR:
115 case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x4_KHR:
116 case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x5_KHR:
117 case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x5_KHR:
118 case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x6_KHR:
119 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x5_KHR:
120 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x6_KHR:
121 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x8_KHR:
122 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x5_KHR:
123 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR:
124 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x8_KHR:
125 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x10_KHR:
126 case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x10_KHR:
127 case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x12_KHR:
128 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
129 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
130 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
131 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
132 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
133 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
134 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
135 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
136 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
137 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
138 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
139 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
140 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
141 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
143 DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
148 case Dali::Pixel::RGB16F:
149 case Dali::Pixel::RGB32F:
151 DALI_LOG_ERROR("Pixel format not compatible.\n");
159 Uint16Pair ParseRange( unsigned int& index, unsigned int width, unsigned char*& pixel, unsigned int pixelStride, int testByte, int testBits, int testValue )
161 unsigned int start = 0xFFFF;
162 for( ; index < width; ++index, pixel += pixelStride )
164 if( ( pixel[ testByte ] & testBits ) == testValue )
168 pixel += pixelStride;
173 unsigned int end = width;
174 for( ; index < width; ++index, pixel += pixelStride )
176 if( ( pixel[ testByte ] & testBits ) != testValue )
180 pixel += pixelStride;
185 return Uint16Pair( start, end );
188 void ParseBorders( Devel::PixelBuffer& pixelBuffer, NPatchLoader::Data* data )
190 data->stretchPixelsX.Clear();
191 data->stretchPixelsY.Clear();
193 Pixel::Format pixelFormat = pixelBuffer.GetPixelFormat();
197 Pixel::GetAlphaOffsetAndMask( pixelFormat, alphaByte, alphaBits );
199 int testByte = alphaByte;
200 int testBits = alphaBits;
201 int testValue = alphaBits; // Opaque == stretch
204 GetRedOffsetAndMask( pixelFormat, testByte, testBits );
205 testValue = 0; // Black == stretch
208 unsigned int bytesPerPixel = Pixel::GetBytesPerPixel( pixelFormat );
209 unsigned int width = pixelBuffer.GetWidth();
210 unsigned int height = pixelBuffer.GetHeight();
211 unsigned char* srcPixels = pixelBuffer.GetBuffer();
212 unsigned int srcStride = width * bytesPerPixel;
215 unsigned char* top = srcPixels + bytesPerPixel;
216 unsigned int index = 0;
218 for( ; index < width - 2; )
220 Uint16Pair range = ParseRange( index, width - 2, top, bytesPerPixel, testByte, testBits, testValue );
221 if( range.GetX() != 0xFFFF )
223 data->stretchPixelsX.PushBack( range );
228 unsigned char* left = srcPixels + srcStride;
230 for( ; index < height - 2; )
232 Uint16Pair range = ParseRange( index, height - 2, left, srcStride, testByte, testBits, testValue );
233 if( range.GetX() != 0xFFFF )
235 data->stretchPixelsY.PushBack( range );
239 // If there are no stretch pixels then make the entire image stretchable
240 if( data->stretchPixelsX.Size() == 0 )
242 data->stretchPixelsX.PushBack( Uint16Pair( 0, width - 2 ) );
244 if( data->stretchPixelsY.Size() == 0 )
246 data->stretchPixelsY.PushBack( Uint16Pair( 0, height - 2 ) );
250 void SetLoadedNPatchData( NPatchLoader::Data* data, Devel::PixelBuffer& pixelBuffer )
252 if( data->border == Rect< int >( 0, 0, 0, 0 ) )
254 NPatchBuffer::ParseBorders( pixelBuffer, data );
257 pixelBuffer.Crop( 1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2 );
261 data->stretchPixelsX.PushBack( Uint16Pair( data->border.left, ( (pixelBuffer.GetWidth() >= static_cast< unsigned int >( data->border.right )) ? pixelBuffer.GetWidth() - data->border.right : 0 ) ) );
262 data->stretchPixelsY.PushBack( Uint16Pair( data->border.top, ( (pixelBuffer.GetHeight() >= static_cast< unsigned int >( data->border.bottom )) ? pixelBuffer.GetHeight() - data->border.bottom : 0 ) ) );
265 data->croppedWidth = pixelBuffer.GetWidth();
266 data->croppedHeight = pixelBuffer.GetHeight();
268 PixelData pixels = Devel::PixelBuffer::Convert( pixelBuffer ); // takes ownership of buffer
270 Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
271 texture.Upload( pixels );
273 data->textureSet = TextureSet::New();
274 data->textureSet.SetTexture( 0u, texture );
276 data->loadCompleted = true;
279 } // namespace NPatchBuffer
281 NPatchLoader::NPatchLoader()
285 NPatchLoader::~NPatchLoader()
289 std::size_t NPatchLoader::Load( TextureManager& textureManager, TextureUploadObserver* textureObserver, const std::string& url, const Rect< int >& border, bool& preMultiplyOnLoad, bool synchronousLoading )
291 std::size_t hash = CalculateHash( url );
292 OwnerContainer< Data* >::SizeType index = UNINITIALIZED_ID;
293 const OwnerContainer< Data* >::SizeType count = mCache.Count();
294 int cachedIndex = -1;
297 for( ; index < count; ++index )
299 if( mCache[ index ]->hash == hash )
301 // hash match, check url as well in case of hash collision
302 if( mCache[ index ]->url == url )
305 if( mCache[ index ]->border == border )
307 if( mCache[ index ]->loadCompleted )
309 return index + 1u; // valid indices are from 1 onwards
311 data = mCache[ index ];
312 cachedIndex = index + 1u; // valid indices are from 1 onwards
317 if( mCache[ index ]->loadCompleted )
319 // Same url but border is different - use the existing texture
320 Data* data = new Data();
323 data->croppedWidth = mCache[ index ]->croppedWidth;
324 data->croppedHeight = mCache[ index ]->croppedHeight;
326 data->textureSet = mCache[ index ]->textureSet;
328 StretchRanges stretchRangesX;
329 stretchRangesX.PushBack( Uint16Pair( border.left, ( (data->croppedWidth >= static_cast< unsigned int >( border.right )) ? data->croppedWidth - border.right : 0 ) ) );
331 StretchRanges stretchRangesY;
332 stretchRangesY.PushBack( Uint16Pair( border.top, ( (data->croppedHeight >= static_cast< unsigned int >( border.bottom )) ? data->croppedHeight - border.bottom : 0 ) ) );
334 data->stretchPixelsX = stretchRangesX;
335 data->stretchPixelsY = stretchRangesY;
336 data->border = border;
338 data->loadCompleted = mCache[ index ]->loadCompleted;
340 mCache.PushBack( data );
342 return mCache.Count(); // valid ids start from 1u
349 if( cachedIndex == -1 )
352 data->loadCompleted = false;
355 data->border = border;
357 mCache.PushBack( data );
359 cachedIndex = mCache.Count();
362 auto preMultiplyOnLoading = preMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
363 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
364 Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer( url, Dali::ImageDimensions(), FittingMode::DEFAULT,
365 SamplingMode::BOX_THEN_LINEAR, synchronousLoading,
366 textureObserver, true, preMultiplyOnLoading );
370 NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer );
371 preMultiplyOnLoad = ( preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD ) ? true : false;
377 void NPatchLoader::SetNPatchData( std::size_t id, Devel::PixelBuffer& pixelBuffer )
380 data = mCache[ id - 1u ];
382 if( !data->loadCompleted )
384 NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer );
388 bool NPatchLoader::GetNPatchData( std::size_t id, const Data*& data )
390 if( ( id > UNINITIALIZED_ID )&&( id <= mCache.Count() ) )
392 data = mCache[ id - 1u ]; // id's start from 1u
399 } // namespace Internal
401 } // namespace Toolkit