DALi Version 2.2.21
[platform/core/uifw/dali-core.git] / dali / integration-api / bitmap.cpp
1 /*
2  * Copyright (c) 2022 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/integration-api/bitmap.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-abstraction.h>
24 #include <dali/integration-api/gl-defines.h>
25 #include <dali/integration-api/platform-abstraction.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/event/images/bitmap-compressed.h>
28 #include <dali/internal/event/images/bitmap-packed-pixel.h>
29
30 namespace Dali
31 {
32 namespace Integration
33 {
34 using namespace Dali::Pixel;
35
36 void ConvertToGlFormat(Format pixelformat, unsigned& pixelDataType, unsigned& internalFormat)
37 {
38   // Compressed textures have no pixelDataType, so init to an invalid value:
39   pixelDataType = GL_INVALID_VALUE;
40
41   switch(pixelformat)
42   {
43     case A8:
44     {
45       pixelDataType  = GL_UNSIGNED_BYTE;
46       internalFormat = GL_ALPHA;
47       break;
48     }
49
50     case L8:
51     {
52       pixelDataType  = GL_UNSIGNED_BYTE;
53       internalFormat = GL_LUMINANCE;
54       break;
55     }
56
57     case LA88:
58     {
59       pixelDataType  = GL_UNSIGNED_BYTE;
60       internalFormat = GL_LUMINANCE_ALPHA;
61       break;
62     }
63
64     case RGB565:
65     {
66       pixelDataType  = GL_UNSIGNED_SHORT_5_6_5;
67       internalFormat = GL_RGB;
68       break;
69     }
70
71     case BGR565:
72     {
73       DALI_LOG_ERROR("Pixel format BGR565 is not supported by GLES.\n");
74       pixelDataType = GL_UNSIGNED_SHORT_5_6_5;
75 #ifdef _ARCH_ARM_
76       internalFormat = GL_BGRA_EXT; // alpha is reserved but not used
77 #else
78       internalFormat = GL_RGBA; // alpha is reserved but not used
79 #endif
80       break;
81     }
82
83     case RGBA4444:
84     {
85       pixelDataType  = GL_UNSIGNED_SHORT_4_4_4_4;
86       internalFormat = GL_RGBA;
87       break;
88     }
89
90     case BGRA4444:
91     {
92       DALI_LOG_ERROR("Pixel format BGRA4444 is not supported by GLES.\n");
93       pixelDataType = GL_UNSIGNED_SHORT_4_4_4_4;
94 #ifdef _ARCH_ARM_
95       internalFormat = GL_BGRA_EXT; // alpha is reserved but not used
96 #else
97       internalFormat = GL_RGBA; // alpha is reserved but not used
98 #endif
99       break;
100     }
101
102     case RGBA5551:
103     {
104       pixelDataType  = GL_UNSIGNED_SHORT_5_5_5_1;
105       internalFormat = GL_RGBA;
106       break;
107     }
108
109     case BGRA5551:
110     {
111       DALI_LOG_ERROR("Pixel format BGRA5551 is not supported by GLES.\n");
112       pixelDataType = GL_UNSIGNED_SHORT_5_5_5_1;
113 #ifdef _ARCH_ARM_
114       internalFormat = GL_BGRA_EXT; // alpha is reserved but not used
115 #else
116       internalFormat = GL_RGBA; // alpha is reserved but not used
117 #endif
118       break;
119     }
120
121     case RGB888:
122     {
123       pixelDataType  = GL_UNSIGNED_BYTE;
124       internalFormat = GL_RGB;
125       break;
126     }
127
128     case RGB8888:
129     {
130       pixelDataType  = GL_UNSIGNED_BYTE;
131       internalFormat = GL_RGBA; // alpha is reserved but not used
132       break;
133     }
134
135     case BGR8888:
136     {
137       pixelDataType = GL_UNSIGNED_BYTE;
138 #ifdef GL_BGRA_EXT
139       internalFormat = GL_BGRA_EXT; // alpha is reserved but not used
140 #else
141       internalFormat = GL_RGBA; // alpha is reserved but not used
142 #endif
143       break;
144     }
145
146     case RGBA8888:
147     {
148       pixelDataType  = GL_UNSIGNED_BYTE;
149       internalFormat = GL_RGBA;
150       break;
151     }
152
153     case BGRA8888:
154     {
155       pixelDataType = GL_UNSIGNED_BYTE;
156 #ifdef GL_BGRA_EXT
157       internalFormat = GL_BGRA_EXT; // alpha is reserved but not used
158 #else
159       internalFormat = GL_RGBA; // alpha is reserved but not used
160 #endif
161       break;
162     }
163
164     // GLES 2 extension compressed formats:
165     case COMPRESSED_RGB8_ETC1:
166     {
167       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using non-standard GLES 2.0 extension compressed pixel format COMPRESSED_RGB8_ETC1.\n");
168       internalFormat = 0x8D64; ///! < Hardcoded so we can test before we move to GLES 3.0 or greater.
169       break;
170     }
171     case COMPRESSED_RGB_PVRTC_4BPPV1:
172     {
173       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using non-standard GLES 2.0 extension compressed pixel format COMPRESSED_RGB_PVRTC_4BPPV1.\n");
174       internalFormat = 0x8C00; ///! < Hardcoded so we can test before we move to GLES 3.0 or greater.
175       break;
176     }
177
178     // GLES 3.0 standard compressed formats:
179     case COMPRESSED_R11_EAC:
180     {
181       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_R11_EAC.\n");
182       internalFormat = GL_COMPRESSED_R11_EAC;
183       break;
184     }
185     case COMPRESSED_SIGNED_R11_EAC:
186     {
187       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SIGNED_R11_EAC.\n");
188       internalFormat = GL_COMPRESSED_SIGNED_R11_EAC;
189       break;
190     }
191     case COMPRESSED_RG11_EAC:
192     {
193       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RG11_EAC.\n");
194       internalFormat = GL_COMPRESSED_RG11_EAC;
195       break;
196     }
197     case COMPRESSED_SIGNED_RG11_EAC:
198     {
199       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SIGNED_RG11_EAC.\n");
200       internalFormat = GL_COMPRESSED_SIGNED_RG11_EAC;
201       break;
202     }
203     case COMPRESSED_RGB8_ETC2:
204     {
205       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RGB8_ETC2.\n");
206       internalFormat = GL_COMPRESSED_RGB8_ETC2;
207       break;
208     }
209     case COMPRESSED_SRGB8_ETC2:
210     {
211       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SRGB8_ETC2.\n");
212       internalFormat = GL_COMPRESSED_SRGB8_ETC2;
213       break;
214     }
215     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
216     {
217       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2.\n");
218       internalFormat = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
219       break;
220     }
221     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
222     {
223       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2.\n");
224       internalFormat = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
225       break;
226     }
227     case COMPRESSED_RGBA8_ETC2_EAC:
228     {
229       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RGBA8_ETC2_EAC.\n");
230       internalFormat = GL_COMPRESSED_RGBA8_ETC2_EAC;
231       break;
232     }
233     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
234     {
235       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ETC2_EAC.\n");
236       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
237       break;
238     }
239
240     // GLES 3.1 extension compressed formats:
241     case COMPRESSED_RGBA_ASTC_4x4_KHR:
242     {
243       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_4x4_KHR.\n");
244       internalFormat = GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
245       break;
246     }
247     case COMPRESSED_RGBA_ASTC_5x4_KHR:
248     {
249       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_5x4_KHR.\n");
250       internalFormat = GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
251       break;
252     }
253     case COMPRESSED_RGBA_ASTC_5x5_KHR:
254     {
255       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_5x5_KHR.\n");
256       internalFormat = GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
257       break;
258     }
259     case COMPRESSED_RGBA_ASTC_6x5_KHR:
260     {
261       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_6x5_KHR.\n");
262       internalFormat = GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
263       break;
264     }
265     case COMPRESSED_RGBA_ASTC_6x6_KHR:
266     {
267       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_6x6_KHR.\n");
268       internalFormat = GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
269       break;
270     }
271     case COMPRESSED_RGBA_ASTC_8x5_KHR:
272     {
273       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_8x5_KHR.\n");
274       internalFormat = GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
275       break;
276     }
277     case COMPRESSED_RGBA_ASTC_8x6_KHR:
278     {
279       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_8x6_KHR.\n");
280       internalFormat = GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
281       break;
282     }
283     case COMPRESSED_RGBA_ASTC_8x8_KHR:
284     {
285       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_8x8_KHR.\n");
286       internalFormat = GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
287       break;
288     }
289     case COMPRESSED_RGBA_ASTC_10x5_KHR:
290     {
291       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_10x5_KHR.\n");
292       internalFormat = GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
293       break;
294     }
295     case COMPRESSED_RGBA_ASTC_10x6_KHR:
296     {
297       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_10x6_KHR.\n");
298       internalFormat = GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
299       break;
300     }
301     case COMPRESSED_RGBA_ASTC_10x8_KHR:
302     {
303       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_10x8_KHR.\n");
304       internalFormat = GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
305       break;
306     }
307     case COMPRESSED_RGBA_ASTC_10x10_KHR:
308     {
309       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_10x10_KHR.\n");
310       internalFormat = GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
311       break;
312     }
313     case COMPRESSED_RGBA_ASTC_12x10_KHR:
314     {
315       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_12x10_KHR.\n");
316       internalFormat = GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
317       break;
318     }
319     case COMPRESSED_RGBA_ASTC_12x12_KHR:
320     {
321       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_12x12_KHR.\n");
322       internalFormat = GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
323       break;
324     }
325     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
326     {
327       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR.\n");
328       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
329       break;
330     }
331     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
332     {
333       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR.\n");
334       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
335       break;
336     }
337     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
338     {
339       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR.\n");
340       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
341       break;
342     }
343     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
344     {
345       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR.\n");
346       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
347       break;
348     }
349     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
350     {
351       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR.\n");
352       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
353       break;
354     }
355     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
356     {
357       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR.\n");
358       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
359       break;
360     }
361     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
362     {
363       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR.\n");
364       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
365       break;
366     }
367     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
368     {
369       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR.\n");
370       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
371       break;
372     }
373     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
374     {
375       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR.\n");
376       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
377       break;
378     }
379     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
380     {
381       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR.\n");
382       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
383       break;
384     }
385     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
386     {
387       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR.\n");
388       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
389       break;
390     }
391     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
392     {
393       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR.\n");
394       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
395       break;
396     }
397     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
398     {
399       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR.\n");
400       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
401       break;
402     }
403     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
404     {
405       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR.\n");
406       internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
407       break;
408     }
409
410     // GLES 3.0 floating point formats.
411     case RGB16F:
412     {
413       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 GLES 3.0 floating point format.\n");
414       pixelDataType  = GL_HALF_FLOAT;
415       internalFormat = GL_RGB;
416       break;
417     }
418     case RGB32F:
419     {
420       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 GLES 3.0 floating point format.\n");
421       pixelDataType  = GL_FLOAT;
422       internalFormat = GL_RGB;
423       break;
424     }
425     case R11G11B10F:
426     {
427       DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 GLES 3.0 floating point format.\n");
428       pixelDataType  = GL_FLOAT;
429       internalFormat = GL_R11F_G11F_B10F;
430       break;
431     }
432
433     // GLES 3.0 depth and stencil formats
434     case Pixel::DEPTH_UNSIGNED_INT:
435     {
436       pixelDataType  = GL_UNSIGNED_INT;
437       internalFormat = GL_DEPTH_COMPONENT;
438       break;
439     }
440     case Pixel::DEPTH_FLOAT:
441     {
442       pixelDataType  = GL_FLOAT;
443       internalFormat = GL_DEPTH_COMPONENT;
444       break;
445     }
446     case Pixel::DEPTH_STENCIL:
447     {
448       pixelDataType  = GL_UNSIGNED_INT_24_8;
449       internalFormat = GL_DEPTH_STENCIL;
450       break;
451     }
452
453     // Chrominance formats
454     case Pixel::CHROMINANCE_U:
455     {
456       pixelDataType  = GL_UNSIGNED_BYTE;
457       internalFormat = GL_LUMINANCE; // GL doesn't support chrominance format. We should convert it in the shader.
458       break;
459     }
460     case Pixel::CHROMINANCE_V:
461     {
462       pixelDataType  = GL_UNSIGNED_BYTE;
463       internalFormat = GL_LUMINANCE; // GL doesn't support chrominance format. We should convert it in the shader.
464       break;
465     }
466
467     case INVALID:
468     {
469       DALI_LOG_ERROR("Invalid pixel format for bitmap\n");
470       internalFormat = 0;
471       break;
472     }
473   }
474 }
475
476 Bitmap* Bitmap::New(const Profile               profile     = BITMAP_2D_PACKED_PIXELS,
477                     ResourcePolicy::Discardable discardable = ResourcePolicy::OWNED_DISCARD)
478 {
479   DALI_ASSERT_DEBUG(profile == BITMAP_2D_PACKED_PIXELS || profile == BITMAP_COMPRESSED);
480
481   switch(profile)
482   {
483     /** A 2D array of pixels where each pixel is a whole number of bytes
484      * and each scanline of the backing memory buffer may have additional
485      * bytes off the right edge if requested, and there may be additional
486      * scanlines past the bottom of the image in the buffer if requested.*/
487     case BITMAP_2D_PACKED_PIXELS:
488     {
489       Bitmap* const bitmap = new Dali::Internal::BitmapPackedPixel(discardable);
490       return bitmap;
491     }
492
493     /** The data for the bitmap is buffered in an opaque form.*/
494     case BITMAP_COMPRESSED:
495     {
496       return new Dali::Internal::BitmapCompressed(discardable);
497     }
498   }
499   return nullptr;
500 }
501
502 Bitmap::Bitmap(ResourcePolicy::Discardable discardable, Dali::Integration::PixelBuffer* pixBuf)
503 : mImageWidth(0),
504   mImageHeight(0),
505   mPixelFormat(Pixel::RGBA8888),
506   mHasAlphaChannel(true),
507   mAlphaChannelUsed(true),
508   mData(pixBuf),
509   mDiscardable(discardable)
510 {
511 }
512
513 PixelBuffer* Bitmap::GetBufferOwnership()
514 {
515   PixelBuffer* buffer = mData;
516   mData               = nullptr;
517   return buffer;
518 }
519
520 void Bitmap::DiscardBuffer()
521 {
522   if(mDiscardable == ResourcePolicy::OWNED_DISCARD)
523   {
524     DeletePixelBuffer();
525   }
526 }
527
528 Bitmap::~Bitmap()
529 {
530   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
531   DeletePixelBuffer();
532 }
533
534 /**
535  * */
536 void Bitmap::DeletePixelBuffer()
537 {
538   if(!mData)
539   {
540     return;
541   }
542   free(mData);
543   mData = nullptr;
544 }
545
546 void Bitmap::Initialize(Pixel::Format pixelFormat,
547                         unsigned int  width,
548                         unsigned int  height)
549 {
550   DALI_ASSERT_DEBUG(width * height < (32 * 1024) * (32 * 1024) && "The total area of the bitmap is too great.\n");
551   mImageWidth  = width;
552   mImageHeight = height;
553   mPixelFormat = pixelFormat;
554
555   mHasAlphaChannel = Pixel::HasAlpha(pixelFormat);
556 }
557
558 } //namespace Integration
559
560 } //namespace Dali