Merge "Fix a bug where NativeImage::GetUrl() was always returning empty string" into...
[platform/core/uifw/dali-core.git] / dali / internal / event / images / nine-patch-image-impl.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/images/nine-patch-image-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for memcmp
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/integration-api/bitmap.h>
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/event/resources/resource-client.h>
29 #include <dali/internal/update/manager/update-manager.h>
30 #include <dali/internal/event/images/image-factory.h>
31 #include <dali/integration-api/platform-abstraction.h>
32 #include <dali/integration-api/resource-types.h>
33 #include <dali/integration-api/resource-cache.h>
34
35
36 namespace
37 {
38 void GetRedOffsetAndMask(Dali::Pixel::Format pixelFormat, int& byteOffset, int& bitMask)
39 {
40   switch (pixelFormat)
41   {
42     case Dali::Pixel::A8:
43     case Dali::Pixel::L8:
44     case Dali::Pixel::LA88:
45     {
46       byteOffset=0;
47       bitMask=0;
48       break;
49     }
50
51     case Dali::Pixel::RGB888:
52     case Dali::Pixel::RGB8888:
53     case Dali::Pixel::RGBA8888:
54     {
55       byteOffset=0;
56       bitMask=0xFF;
57       break;
58     }
59     case Dali::Pixel::BGR8888:
60     case Dali::Pixel::BGRA8888:
61     {
62       byteOffset=2;
63       bitMask=0xff;
64       break;
65     }
66     case Dali::Pixel::RGB565:
67     {
68       byteOffset=0;
69       bitMask=0xf8;
70       break;
71     }
72     case Dali::Pixel::BGR565:
73     {
74       byteOffset=1;
75       bitMask=0x1f;
76       break;
77     }
78
79     case Dali::Pixel::RGBA4444:
80     {
81       byteOffset=0;
82       bitMask=0xf0;
83       break;
84     }
85     case Dali::Pixel::BGRA4444:
86     {
87       byteOffset=1;
88       bitMask=0xf0;
89       break;
90     }
91
92     case Dali::Pixel::RGBA5551:
93     {
94       byteOffset=0;
95       bitMask=0xf8;
96       break;
97     }
98
99     case Dali::Pixel::BGRA5551:
100     {
101       byteOffset=1;
102       bitMask=0x1e;
103       break;
104     }
105
106     case Dali::Pixel::INVALID:
107     case Dali::Pixel::COMPRESSED_R11_EAC:
108     case Dali::Pixel::COMPRESSED_SIGNED_R11_EAC:
109     case Dali::Pixel::COMPRESSED_RG11_EAC:
110     case Dali::Pixel::COMPRESSED_SIGNED_RG11_EAC:
111     case Dali::Pixel::COMPRESSED_RGB8_ETC2:
112     case Dali::Pixel::COMPRESSED_SRGB8_ETC2:
113     case Dali::Pixel::COMPRESSED_RGB8_ETC1:
114     case Dali::Pixel::COMPRESSED_RGB_PVRTC_4BPPV1:
115     case Dali::Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
116     case Dali::Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
117     case Dali::Pixel::COMPRESSED_RGBA8_ETC2_EAC:
118     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
119     case Dali::Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR:
120     case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x4_KHR:
121     case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x5_KHR:
122     case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x5_KHR:
123     case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x6_KHR:
124     case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x5_KHR:
125     case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x6_KHR:
126     case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x8_KHR:
127     case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x5_KHR:
128     case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR:
129     case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x8_KHR:
130     case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x10_KHR:
131     case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x10_KHR:
132     case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x12_KHR:
133     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
134     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
135     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
136     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
137     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
138     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
139     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
140     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
141     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
142     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
143     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
144     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
145     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
146     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
147     {
148       DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
149       byteOffset=0;
150       bitMask=0;
151       break;
152     }
153   }
154 }
155
156 } // anonymous namespace
157
158
159 namespace Dali
160 {
161 namespace Internal
162 {
163
164 namespace
165 {
166 TypeRegistration mType( typeid( Dali::NinePatchImage ), typeid( Dali::Image ), NULL );
167 } // unnamed namespace
168
169 NinePatchImagePtr NinePatchImage::New( const std::string& filename )
170 {
171   Internal::NinePatchImagePtr internal( new NinePatchImage( filename ) );
172   internal->Initialize();
173   return internal;
174 }
175
176 NinePatchImage::NinePatchImage( const std::string& filename )
177 : ResourceImage(),
178   mParsedBorder(false)
179 {
180   mUrl = filename;
181   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
182   mResourceClient = &tls.GetResourceClient();
183
184   Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
185   Integration::BitmapResourceType resourceType;
186
187   // Note, bitmap is only destroyed when the image is destroyed.
188   Integration::ResourcePointer resource = platformAbstraction.LoadResourceSynchronously( resourceType, filename );
189   if( resource )
190   {
191     mBitmap = static_cast<Integration::Bitmap*>( resource.Get());
192     mWidth = mBitmap->GetImageWidth();
193     mHeight = mBitmap->GetImageHeight();
194   }
195   else
196   {
197     mBitmap.Reset();
198     mWidth = 0;
199     mHeight = 0;
200   }
201 }
202
203 NinePatchImage* NinePatchImage::DownCast( Image* image)
204 {
205   return dynamic_cast<NinePatchImage*>(image);
206 }
207
208 NinePatchImage::~NinePatchImage()
209 {
210 }
211
212 const NinePatchImage::StretchRanges& NinePatchImage::GetStretchPixelsX()
213 {
214   if( ! mParsedBorder )
215   {
216     ParseBorders();
217   }
218   return mStretchPixelsX;
219 }
220
221 const NinePatchImage::StretchRanges& NinePatchImage::GetStretchPixelsY()
222 {
223   if( ! mParsedBorder )
224   {
225     ParseBorders();
226   }
227   return mStretchPixelsY;
228 }
229
230 Rect<int> NinePatchImage::GetChildRectangle()
231 {
232   if( ! mParsedBorder )
233   {
234     ParseBorders();
235   }
236   return mChildRectangle;
237 }
238
239 Internal::BufferImagePtr NinePatchImage::CreateCroppedBufferImage()
240 {
241   BufferImagePtr cropped;
242
243   if( ! mBitmap )
244   {
245     DALI_LOG_ERROR( "NinePatchImage: Bitmap not loaded, cannot perform operation\n");
246   }
247   else
248   {
249     Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
250
251     cropped = BufferImage::New( mWidth-2, mHeight-2, pixelFormat );
252
253     Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
254     DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap");
255
256     if( srcProfile )
257     {
258       PixelBuffer* destPixels = cropped->GetBuffer();
259       unsigned int destStride = cropped->GetBufferStride();
260       unsigned int pixelWidth = GetBytesPerPixel(pixelFormat);
261
262       PixelBuffer* srcPixels = mBitmap->GetBuffer();
263       unsigned int srcStride = srcProfile->GetBufferStride();
264
265       for( unsigned int row=1; row < mHeight-1; ++row )
266       {
267         PixelBuffer* src  = srcPixels + row*srcStride + pixelWidth;
268         PixelBuffer* dest = destPixels + (row-1)*destStride;
269         memcpy(dest, src, destStride );
270       }
271     }
272
273     RectArea area;
274     cropped->Update(area); // default area has no width or height
275   }
276   return cropped;
277 }
278
279 const std::string& NinePatchImage::GetUrl() const
280 {
281   return mUrl;
282 }
283
284 void NinePatchImage::Connect()
285 {
286   if( !mTicket )
287   {
288     if( mBitmap )
289     {
290       const ImageTicketPtr& t = mResourceClient->AddBitmapImage(mBitmap.Get());
291       mTicket = t.Get();
292       mTicket->AddObserver(*this);
293     }
294   }
295
296   ++mConnectionCount;
297 }
298
299 void NinePatchImage::Disconnect()
300 {
301   if( mConnectionCount > 0 )
302   {
303     --mConnectionCount;
304   }
305 }
306
307
308 void NinePatchImage::ParseBorders()
309 {
310   if( !mBitmap )
311   {
312     DALI_LOG_ERROR( "NinePatchImage: Bitmap not loaded, cannot perform operation\n");
313     return;
314   }
315
316   mStretchPixelsX.Clear();
317   mStretchPixelsY.Clear();
318
319   Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
320
321   const Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
322   DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap" );
323
324   if( srcProfile )
325   {
326     int alphaByte = 0;
327     int alphaBits = 0;
328     Pixel::GetAlphaOffsetAndMask( pixelFormat, alphaByte, alphaBits );
329
330     int testByte = alphaByte;
331     int testBits = alphaBits;
332     int testValue = alphaBits; // Opaque == stretch
333     if( ! alphaBits )
334     {
335       GetRedOffsetAndMask( pixelFormat, testByte, testBits );
336       testValue = 0;           // Black == stretch
337     }
338
339     unsigned int pixelWidth = GetBytesPerPixel( pixelFormat );
340     const PixelBuffer* srcPixels = mBitmap->GetBuffer();
341     unsigned int srcStride = srcProfile->GetBufferStride();
342
343     //TOP
344     const PixelBuffer* top = srcPixels + pixelWidth;
345     unsigned int index = 0;
346     unsigned int width = mBitmap->GetImageWidth();
347     unsigned int height = mBitmap->GetImageHeight();
348
349     for(; index < width - 2; )
350     {
351       Uint16Pair range = ParseRange( index, width - 2, top, pixelWidth, testByte, testBits, testValue );
352       if( range.GetX() != 0xFFFF )
353       {
354         mStretchPixelsX.PushBack( range );
355       }
356     }
357
358     //LEFT
359     const PixelBuffer* left  = srcPixels + srcStride;
360     index = 0;
361     for(; index < height - 2; )
362     {
363       Uint16Pair range = ParseRange( index, height - 2, left, srcStride, testByte, testBits, testValue );
364       if( range.GetX() != 0xFFFF )
365       {
366         mStretchPixelsY.PushBack( range );
367       }
368     }
369
370     //If there are no stretch pixels then make the entire image stretchable
371     if( mStretchPixelsX.Size() == 0 )
372     {
373       mStretchPixelsX.PushBack( Uint16Pair( 0, width - 2 ) );
374     }
375     if( mStretchPixelsY.Size() == 0 )
376     {
377       mStretchPixelsY.PushBack( Uint16Pair( 0, height - 2 ) );
378     }
379
380     //Child Rectangle
381     //BOTTOM
382     const PixelBuffer* bottom = srcPixels + ( height - 1 ) * srcStride + pixelWidth;
383     index = 0;
384     Uint16Pair contentRangeX = ParseRange( index, width - 2, bottom, pixelWidth, testByte, testBits, testValue );
385     if( contentRangeX.GetX() == 0xFFFF )
386     {
387       contentRangeX = Uint16Pair();
388     }
389
390     //RIGHT
391     const PixelBuffer* right = srcPixels + srcStride + ( width - 1 ) * pixelWidth;
392     index = 0;
393     Uint16Pair contentRangeY = ParseRange( index, height - 2, right, srcStride, testByte, testBits, testValue );
394     if( contentRangeY.GetX() == 0xFFFF )
395     {
396       contentRangeY = Uint16Pair();
397     }
398
399     mChildRectangle.x = contentRangeX.GetX() + 1;
400     mChildRectangle.y = contentRangeY.GetX() + 1;
401     mChildRectangle.width = contentRangeX.GetY() - contentRangeX.GetX();
402     mChildRectangle.height = contentRangeY.GetY() - contentRangeY.GetX();
403
404     mParsedBorder = true;
405   }
406 }
407
408 Uint16Pair NinePatchImage::ParseRange( unsigned int& index, unsigned int width, const PixelBuffer* & pixel, unsigned int pixelStride, int testByte, int testBits, int testValue )
409 {
410   unsigned int start = 0xFFFF;
411   for( ; index < width; ++index, pixel += pixelStride )
412   {
413     if( ( pixel[ testByte ] & testBits ) == testValue )
414     {
415         start = index;
416         ++index;
417         pixel += pixelStride;
418         break;
419     }
420   }
421
422   unsigned int end = width;
423   for( ; index < width; ++index, pixel += pixelStride )
424   {
425     if( ( pixel[ testByte ] & testBits ) != testValue )
426     {
427         end = index;
428         ++index;
429         pixel += pixelStride;
430         break;
431     }
432   }
433
434   return Uint16Pair( start, end );
435 }
436
437 bool NinePatchImage::IsNinePatchUrl( const std::string& url )
438 {
439   bool match = false;
440
441   std::string::const_reverse_iterator iter = url.rbegin();
442   enum { SUFFIX, HASH, HASH_DOT, DONE } state = SUFFIX;
443   while(iter < url.rend())
444   {
445     switch(state)
446     {
447       case SUFFIX:
448       {
449         if(*iter == '.')
450         {
451           state = HASH;
452         }
453         else if(!isalnum(*iter))
454         {
455           state = DONE;
456         }
457       }
458       break;
459       case HASH:
460       {
461         if( *iter == '#' || *iter == '9' )
462         {
463           state = HASH_DOT;
464         }
465         else
466         {
467           state = DONE;
468         }
469       }
470       break;
471       case HASH_DOT:
472       {
473         if(*iter == '.')
474         {
475           match = true;
476         }
477         state = DONE; // Stop testing characters
478       }
479       break;
480       case DONE:
481       {
482       }
483       break;
484     }
485
486     // Satisfy prevent
487     if( state == DONE )
488     {
489       break;
490     }
491
492     ++iter;
493   }
494   return match;
495 }
496
497 } // namespace Internal
498
499 } // namespace Dali