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