2 * Copyright (c) 2017 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/integration-api/bitmap.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/platform-abstraction.h>
24 #include <dali/internal/event/common/thread-local-storage.h>
25 #include <dali/internal/event/images/bitmap-packed-pixel.h>
26 #include <dali/internal/event/images/bitmap-compressed.h>
27 #include <dali/integration-api/gl-abstraction.h>
28 #include <dali/integration-api/gl-defines.h>
35 using namespace Dali::Pixel;
37 void ConvertToGlFormat( Format pixelformat, unsigned& pixelDataType, unsigned& internalFormat )
39 // Compressed textures have no pixelDataType, so init to an invalid value:
46 pixelDataType = GL_UNSIGNED_BYTE;
47 internalFormat= GL_ALPHA;
53 pixelDataType = GL_UNSIGNED_BYTE;
54 internalFormat= GL_LUMINANCE;
60 pixelDataType = GL_UNSIGNED_BYTE;
61 internalFormat= GL_LUMINANCE_ALPHA;
67 pixelDataType = GL_UNSIGNED_SHORT_5_6_5;
68 internalFormat= GL_RGB;
74 DALI_LOG_ERROR("Pixel format BGR565 is not supported by GLES.\n");
75 pixelDataType = GL_UNSIGNED_SHORT_5_6_5;
77 internalFormat= GL_BGRA_EXT; // alpha is reserved but not used
79 internalFormat= GL_RGBA; // alpha is reserved but not used
86 pixelDataType = GL_UNSIGNED_SHORT_4_4_4_4;
87 internalFormat= GL_RGBA;
93 DALI_LOG_ERROR("Pixel format BGRA4444 is not supported by GLES.\n");
94 pixelDataType = GL_UNSIGNED_SHORT_4_4_4_4;
96 internalFormat= GL_BGRA_EXT; // alpha is reserved but not used
98 internalFormat= GL_RGBA; // alpha is reserved but not used
105 pixelDataType = GL_UNSIGNED_SHORT_5_5_5_1;
106 internalFormat= GL_RGBA;
112 DALI_LOG_ERROR("Pixel format BGRA5551 is not supported by GLES.\n");
113 pixelDataType = GL_UNSIGNED_SHORT_5_5_5_1;
115 internalFormat= GL_BGRA_EXT; // alpha is reserved but not used
117 internalFormat= GL_RGBA; // alpha is reserved but not used
124 pixelDataType = GL_UNSIGNED_BYTE;
125 internalFormat= GL_RGB;
131 pixelDataType = GL_UNSIGNED_BYTE;
132 internalFormat= GL_RGBA; // alpha is reserved but not used
138 pixelDataType = GL_UNSIGNED_BYTE;
140 internalFormat= GL_BGRA_EXT; // alpha is reserved but not used
142 internalFormat= GL_RGBA; // alpha is reserved but not used
149 pixelDataType = GL_UNSIGNED_BYTE;
150 internalFormat= GL_RGBA;
156 pixelDataType = GL_UNSIGNED_BYTE;
158 internalFormat= GL_BGRA_EXT; // alpha is reserved but not used
160 internalFormat= GL_RGBA; // alpha is reserved but not used
165 // GLES 2 extension compressed formats:
166 case COMPRESSED_RGB8_ETC1:
168 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using non-standard GLES 2.0 extension compressed pixel format COMPRESSED_RGB8_ETC1.\n" );
169 internalFormat = 0x8D64; ///! < Hardcoded so we can test before we move to GLES 3.0 or greater.
172 case COMPRESSED_RGB_PVRTC_4BPPV1:
174 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using non-standard GLES 2.0 extension compressed pixel format COMPRESSED_RGB_PVRTC_4BPPV1.\n" );
175 internalFormat = 0x8C00; ///! < Hardcoded so we can test before we move to GLES 3.0 or greater.
179 // GLES 3.0 standard compressed formats:
180 case COMPRESSED_R11_EAC:
182 DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_R11_EAC.\n");
183 internalFormat = GL_COMPRESSED_R11_EAC;
186 case COMPRESSED_SIGNED_R11_EAC:
188 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SIGNED_R11_EAC.\n" );
189 internalFormat = GL_COMPRESSED_SIGNED_R11_EAC;
192 case COMPRESSED_RG11_EAC:
194 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RG11_EAC.\n" );
195 internalFormat = GL_COMPRESSED_RG11_EAC;
198 case COMPRESSED_SIGNED_RG11_EAC:
200 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SIGNED_RG11_EAC.\n" );
201 internalFormat = GL_COMPRESSED_SIGNED_RG11_EAC;
204 case COMPRESSED_RGB8_ETC2:
206 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RGB8_ETC2.\n" );
207 internalFormat = GL_COMPRESSED_RGB8_ETC2;
210 case COMPRESSED_SRGB8_ETC2:
212 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SRGB8_ETC2.\n" );
213 internalFormat = GL_COMPRESSED_SRGB8_ETC2;
216 case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
218 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2.\n" );
219 internalFormat = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
222 case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
224 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2.\n" );
225 internalFormat = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
228 case COMPRESSED_RGBA8_ETC2_EAC:
230 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RGBA8_ETC2_EAC.\n" );
231 internalFormat = GL_COMPRESSED_RGBA8_ETC2_EAC;
234 case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
236 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ETC2_EAC.\n" );
237 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
241 // GLES 3.1 extension compressed formats:
242 case COMPRESSED_RGBA_ASTC_4x4_KHR:
244 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_4x4_KHR.\n" );
245 internalFormat = GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
248 case COMPRESSED_RGBA_ASTC_5x4_KHR:
250 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_5x4_KHR.\n" );
251 internalFormat = GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
254 case COMPRESSED_RGBA_ASTC_5x5_KHR:
256 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_5x5_KHR.\n" );
257 internalFormat = GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
260 case COMPRESSED_RGBA_ASTC_6x5_KHR:
262 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_6x5_KHR.\n" );
263 internalFormat = GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
266 case COMPRESSED_RGBA_ASTC_6x6_KHR:
268 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_6x6_KHR.\n" );
269 internalFormat = GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
272 case COMPRESSED_RGBA_ASTC_8x5_KHR:
274 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_8x5_KHR.\n" );
275 internalFormat = GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
278 case COMPRESSED_RGBA_ASTC_8x6_KHR:
280 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_8x6_KHR.\n" );
281 internalFormat = GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
284 case COMPRESSED_RGBA_ASTC_8x8_KHR:
286 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_8x8_KHR.\n" );
287 internalFormat = GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
290 case COMPRESSED_RGBA_ASTC_10x5_KHR:
292 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_10x5_KHR.\n" );
293 internalFormat = GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
296 case COMPRESSED_RGBA_ASTC_10x6_KHR:
298 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_10x6_KHR.\n" );
299 internalFormat = GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
302 case COMPRESSED_RGBA_ASTC_10x8_KHR:
304 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_10x8_KHR.\n" );
305 internalFormat = GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
308 case COMPRESSED_RGBA_ASTC_10x10_KHR:
310 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_10x10_KHR.\n" );
311 internalFormat = GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
314 case COMPRESSED_RGBA_ASTC_12x10_KHR:
316 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_12x10_KHR.\n" );
317 internalFormat = GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
320 case COMPRESSED_RGBA_ASTC_12x12_KHR:
322 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_RGBA_ASTC_12x12_KHR.\n" );
323 internalFormat = GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
326 case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
328 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR.\n" );
329 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
332 case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
334 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR.\n" );
335 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
338 case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
340 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR.\n" );
341 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
344 case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
346 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR.\n" );
347 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
350 case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
352 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR.\n" );
353 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
356 case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
358 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR.\n" );
359 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
362 case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
364 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR.\n" );
365 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
368 case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
370 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR.\n" );
371 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
374 case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
376 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR.\n" );
377 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
380 case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
382 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR.\n" );
383 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
386 case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
388 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR.\n" );
389 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
392 case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
394 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR.\n" );
395 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
398 case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
400 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR.\n" );
401 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
404 case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
406 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.1 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR.\n" );
407 internalFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
411 // GLES 3.0 floating point formats.
414 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 GLES 3.0 floating point format.\n" );
415 pixelDataType = GL_HALF_FLOAT;
416 internalFormat= GL_RGB;
421 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 GLES 3.0 floating point format.\n" );
422 pixelDataType = GL_FLOAT;
423 internalFormat= GL_RGB;
429 DALI_LOG_ERROR( "Invalid pixel format for bitmap\n" );
436 Bitmap* Bitmap::New( const Profile profile = BITMAP_2D_PACKED_PIXELS,
437 ResourcePolicy::Discardable discardable = ResourcePolicy::OWNED_DISCARD )
439 DALI_ASSERT_DEBUG(profile == BITMAP_2D_PACKED_PIXELS || profile == BITMAP_COMPRESSED);
443 /** A 2D array of pixels where each pixel is a whole number of bytes
444 * and each scanline of the backing memory buffer may have additional
445 * bytes off the right edge if requested, and there may be additional
446 * scanlines past the bottom of the image in the buffer if requested.*/
447 case BITMAP_2D_PACKED_PIXELS:
449 Bitmap * const bitmap = new Dali::Internal::BitmapPackedPixel( discardable );
453 /** The data for the bitmap is buffered in an opaque form.*/
454 case BITMAP_COMPRESSED:
456 return new Dali::Internal::BitmapCompressed( discardable );
463 Bitmap::Bitmap( ResourcePolicy::Discardable discardable, Dali::Integration::PixelBuffer* pixBuf)
466 mPixelFormat(Pixel::RGBA8888),
467 mHasAlphaChannel(true),
468 mAlphaChannelUsed(true),
470 mDiscardable(discardable)
474 PixelBuffer* Bitmap::GetBufferOwnership()
476 PixelBuffer* buffer = mData;
481 void Bitmap::DiscardBuffer()
483 if( mDiscardable == ResourcePolicy::OWNED_DISCARD )
491 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
497 void Bitmap::DeletePixelBuffer()
508 void Bitmap::Initialize( Pixel::Format pixelFormat,
512 DALI_ASSERT_DEBUG(width * height < (32 * 1024) * (32 * 1024) && "The total area of the bitmap is too great.\n");
514 mImageHeight = height;
515 mPixelFormat = pixelFormat;
517 mHasAlphaChannel = Pixel::HasAlpha(pixelFormat);
520 } //namespace Integration