2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/integration-api/bitmap.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/image-data.h>
26 #include <dali/internal/event/images/bitmap-packed-pixel.h>
27 #include <dali/internal/event/images/bitmap-compressed.h>
28 #include <dali/internal/event/images/bitmap-external.h>
29 #include <dali/integration-api/gl-abstraction.h>
30 #include <dali/public-api/images/pixel-extras.h>
37 using namespace Dali::Pixel;
39 void ConvertToGlFormat( Format pixelformat, unsigned& pixelDataType, unsigned& internalFormat )
41 // Compressed textures have no pixelDataType, so init to an invalid value:
48 pixelDataType = GL_UNSIGNED_BYTE;
49 internalFormat= GL_ALPHA;
55 pixelDataType = GL_UNSIGNED_BYTE;
56 internalFormat= GL_LUMINANCE;
62 pixelDataType = GL_UNSIGNED_BYTE;
63 internalFormat= GL_LUMINANCE_ALPHA;
69 pixelDataType = GL_UNSIGNED_SHORT_5_6_5;
70 internalFormat= GL_RGB;
76 DALI_LOG_ERROR("Pixel format BGR565 is not supported by GLES.\n");
77 pixelDataType = GL_UNSIGNED_SHORT_5_6_5;
79 internalFormat= GL_BGRA_EXT; // alpha is reserved but not used
81 internalFormat= GL_RGBA; // alpha is reserved but not used
88 pixelDataType = GL_UNSIGNED_SHORT_4_4_4_4;
89 internalFormat= GL_RGBA;
95 DALI_LOG_ERROR("Pixel format BGRA4444 is not supported by GLES.\n");
96 pixelDataType = GL_UNSIGNED_SHORT_4_4_4_4;
98 internalFormat= GL_BGRA_EXT; // alpha is reserved but not used
100 internalFormat= GL_RGBA; // alpha is reserved but not used
107 pixelDataType = GL_UNSIGNED_SHORT_5_5_5_1;
108 internalFormat= GL_RGBA;
114 DALI_LOG_ERROR("Pixel format BGRA5551 is not supported by GLES.\n");
115 pixelDataType = GL_UNSIGNED_SHORT_5_5_5_1;
117 internalFormat= GL_BGRA_EXT; // alpha is reserved but not used
119 internalFormat= GL_RGBA; // alpha is reserved but not used
126 pixelDataType = GL_UNSIGNED_BYTE;
127 internalFormat= GL_RGB;
133 pixelDataType = GL_UNSIGNED_BYTE;
134 internalFormat= GL_RGBA; // alpha is reserved but not used
140 pixelDataType = GL_UNSIGNED_BYTE;
142 internalFormat= GL_BGRA_EXT; // alpha is reserved but not used
144 internalFormat= GL_RGBA; // alpha is reserved but not used
151 pixelDataType = GL_UNSIGNED_BYTE;
152 internalFormat= GL_RGBA;
158 pixelDataType = GL_UNSIGNED_BYTE;
160 internalFormat= GL_BGRA_EXT; // alpha is reserved but not used
162 internalFormat= GL_RGBA; // alpha is reserved but not used
167 // GLES 3.0 standard compressed formats:
168 case COMPRESSED_R11_EAC:
170 DALI_LOG_INFO(Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_R11_EAC.\n");
171 internalFormat = 0x9270; ///! < Hardcoded until we move to GLES 3.0 or greater.
174 case COMPRESSED_SIGNED_R11_EAC:
176 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SIGNED_R11_EAC.\n" );
177 internalFormat = 0x9271; ///! < Hardcoded until we move to GLES 3.0 or greater.
181 case COMPRESSED_RG11_EAC:
183 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RG11_EAC.\n" );
184 internalFormat = 0x9272; ///! < Hardcoded until we move to GLES 3.0 or greater.
187 case COMPRESSED_SIGNED_RG11_EAC:
189 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SIGNED_RG11_EAC.\n" );
190 internalFormat = 0x9273; ///! < Hardcoded until we move to GLES 3.0 or greater.
193 case COMPRESSED_RGB8_ETC2:
195 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RGB8_ETC2.\n" );
196 internalFormat = 0x9274; ///! < Hardcoded until we move to GLES 3.0 or greater.
199 case COMPRESSED_SRGB8_ETC2:
201 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SRGB8_ETC2.\n" );
202 internalFormat = 0x9275; ///! < Hardcoded until we move to GLES 3.0 or greater.
205 case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
207 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2.\n" );
208 internalFormat = 0x9276; ///! < Hardcoded until we move to GLES 3.0 or greater.
211 case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
213 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2.\n" );
214 internalFormat = 0x9277; ///! < Hardcoded until we move to GLES 3.0 or greater.
217 case COMPRESSED_RGBA8_ETC2_EAC:
219 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_RGBA8_ETC2_EAC.\n" );
220 internalFormat = 0x9278; ///! < Hardcoded until we move to GLES 3.0 or greater.
223 case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
225 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using GLES 3.0 standard compressed pixel format COMPRESSED_SRGB8_ALPHA8_ETC2_EAC.\n" );
226 internalFormat = 0x9279; ///! < Hardcoded until we move to GLES 3.0 or greater.
230 // GLES 2 extension compressed formats:
231 case COMPRESSED_RGB8_ETC1:
233 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using non-standard GLES 2.0 extension compressed pixel format COMPRESSED_RGB8_ETC1.\n" );
234 internalFormat = 0x8D64; ///! < Hardcoded so we can test before we move to GLES 3.0 or greater.
237 case COMPRESSED_RGB_PVRTC_4BPPV1:
239 DALI_LOG_INFO( Debug::Filter::gImage, Debug::Verbose, "Using non-standard GLES 2.0 extension compressed pixel format COMPRESSED_RGB_PVRTC_4BPPV1.\n" );
240 internalFormat = 0x8C00; ///! < Hardcoded so we can test before we move to GLES 3.0 or greater.
246 Bitmap* Bitmap::New(const Profile profile = BITMAP_2D_PACKED_PIXELS, const bool managePixelBuffer = true)
248 DALI_ASSERT_DEBUG(profile == BITMAP_2D_PACKED_PIXELS || profile == BITMAP_COMPRESSED);
252 /** A 2D array of pixels where each pixel is a whole number of bytes
253 * and each scanline of the backing memory buffer may have additional
254 * bytes off the right edge if requested, and there may be additional
255 * scanlines past the bottom of the image in the buffer if requested.*/
256 case BITMAP_2D_PACKED_PIXELS:
258 return new Dali::Internal::BitmapPackedPixel(managePixelBuffer);
261 /** The data for the bitmap is buffered in an opaque form.*/
262 case BITMAP_COMPRESSED:
264 return new Dali::Internal::BitmapCompressed(managePixelBuffer);
270 Bitmap* Bitmap::New(const Pixel::Format format)
272 if( Pixel::IsEncoded( format ) )
274 return new Dali::Internal::BitmapCompressed(true);
278 return new Dali::Internal::BitmapPackedPixel(true);
282 Bitmap::Bitmap( bool discardable, Dali::Integration::PixelBuffer* pixBuf)
285 mPixelFormat(Pixel::RGBA8888),
286 mHasAlphaChannel(true),
287 mAlphaChannelUsed(true),
289 mDiscardable(discardable)
293 void Bitmap::DiscardBuffer()
301 PixelBuffer* Bitmap::ReleaseBuffer()
303 PixelBuffer* const data = mData;
305 // Ownership of mData has been transferred, so indicate that mData pointer is no longer valid:
313 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
319 void Bitmap::DeletePixelBuffer()
330 void Bitmap::Initialize( Pixel::Format pixelFormat,
332 unsigned int height )
334 DALI_ASSERT_DEBUG(width * height < (32 * 1024) * (32 * 1024) && "The total area of the bitmap is too great.\n");
336 mImageHeight = height;
337 mPixelFormat = pixelFormat;
339 mHasAlphaChannel = Pixel::HasAlpha(pixelFormat);
342 BitmapPtr ConvertToBitmap( ImageData& imageData )
344 BitmapPtr bitmap = Bitmap::New( imageData.pixelFormat );
345 Bitmap::PackedPixelsProfile* packedBitmap = bitmap->GetPackedPixelsProfile();
348 // Pass the buffer of pixels into the new Bitmap:
349 packedBitmap->AssignBuffer( imageData.pixelFormat, imageData.ReleaseImageBuffer(), imageData.dataSize, imageData.imageWidth, imageData.imageHeight, imageData.imageWidth, imageData.imageHeight );
351 // Convert the info about the alpha channel:
352 bitmap->SetAlphaChannelUsed( imageData.GetAlphaUsage() == ImageData::ALPHA_USAGE_SOME_TRANSLUCENT );
356 ///@Todo: In one of the follow-on patches, the copy here will be replaced with a buffer ownership assignment, after the Bitmap classes are refactored, rather than enhancing BitmapCompressed in this one.
357 Bitmap::CompressedProfile * compressedBitmap = bitmap->GetCompressedProfile();
358 if( compressedBitmap )
360 compressedBitmap->ReserveBufferOfSize( imageData.pixelFormat, imageData.imageWidth, imageData.imageHeight, imageData.dataSize );
361 memcpy( bitmap->GetBuffer(), imageData.GetBuffer(), imageData.dataSize );
365 DALI_ASSERT_DEBUG( 0 == "Unknown bitmap profile." );
371 } //namespace Integration