From: adam.b Date: Fri, 5 Jan 2018 11:43:21 +0000 (+0000) Subject: Fixed loading of compressed texture formats X-Git-Tag: dali_1.3.7~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=56487a2c05fd81e2a630bd3c71418e74bf357e33;p=platform%2Fcore%2Fuifw%2Fdali-adaptor.git Fixed loading of compressed texture formats - ASTC loader fixed - KTX loader fixed - PixelBuffer implementation allows to allocate fixed size memory buffer independent on the texture format Change-Id: I97dbd4e4b9910832a86c1b0b6229808e5ae4a64f --- diff --git a/adaptors/common/pixel-buffer-impl.cpp b/adaptors/common/pixel-buffer-impl.cpp index 23af6bc..b5b5ca4 100644 --- a/adaptors/common/pixel-buffer-impl.cpp +++ b/adaptors/common/pixel-buffer-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * Copyright (c) 2018 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -204,6 +204,13 @@ void PixelBuffer::ReleaseBuffer() } } +void PixelBuffer::AllocateFixedSize( uint32_t size ) +{ + ReleaseBuffer(); + mBuffer = reinterpret_cast(malloc( size )); + mBufferSize = size; +} + void PixelBuffer::ScaleAndCrop( float scaleFactor, ImageDimensions cropDimensions ) { ImageDimensions outDimensions( float(mWidth) * scaleFactor, @@ -352,7 +359,9 @@ void PixelBuffer::MultiplyColorByAlpha() { auto bytesPerPixel = Pixel::GetBytesPerPixel( mPixelFormat ); - if( Pixel::HasAlpha(mPixelFormat) ) + // Compressed textures have unknown size of the pixel. Alpha premultiplication + // must be skipped in such case + if( Pixel::GetBytesPerPixel(mPixelFormat) && Pixel::HasAlpha(mPixelFormat) ) { unsigned char* pixel = mBuffer; const unsigned int bufferSize = mWidth * mHeight; diff --git a/adaptors/common/pixel-buffer-impl.h b/adaptors/common/pixel-buffer-impl.h index ae899be..35cb5a4 100644 --- a/adaptors/common/pixel-buffer-impl.h +++ b/adaptors/common/pixel-buffer-impl.h @@ -2,7 +2,7 @@ #define DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H /* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * Copyright (c) 2018 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -206,6 +206,12 @@ public: */ void SetMetadata(std::unique_ptr metadata); + /** + * Allocates fixed amount of memory for the pixel data. Used by compressed formats. + * @param[in] size Size of memory to be allocated + */ + void AllocateFixedSize( uint32_t size ); + private: /* * Undefined copy constructor. diff --git a/platform-abstractions/tizen/image-loaders/loader-astc.cpp b/platform-abstractions/tizen/image-loaders/loader-astc.cpp index a0362aa..10460e4 100755 --- a/platform-abstractions/tizen/image-loaders/loader-astc.cpp +++ b/platform-abstractions/tizen/image-loaders/loader-astc.cpp @@ -24,6 +24,7 @@ #include #include #include +#include namespace Dali { @@ -226,7 +227,16 @@ bool LoadBitmapFromAstc( const ImageLoader::Input& input, Dali::Devel::PixelBuff // allocate pixel data bitmap = Dali::Devel::PixelBuffer::New(width, height, pixelFormat); + + // Compressed format won't allocate the buffer auto pixels = bitmap.GetBuffer(); + if( !pixels ) + { + // allocate buffer manually + auto& impl = GetImplementation( bitmap ); + impl.AllocateFixedSize( imageByteCount ); + pixels = bitmap.GetBuffer(); + } // Load the image data. const size_t bytesRead = fread( pixels, 1, imageByteCount, filePointer ); diff --git a/platform-abstractions/tizen/image-loaders/loader-ktx.cpp b/platform-abstractions/tizen/image-loaders/loader-ktx.cpp index fc67104..e8e9e13 100755 --- a/platform-abstractions/tizen/image-loaders/loader-ktx.cpp +++ b/platform-abstractions/tizen/image-loaders/loader-ktx.cpp @@ -23,6 +23,7 @@ #include #include #include +#include namespace Dali { @@ -587,13 +588,23 @@ bool LoadBitmapFromKtx( const ImageLoader::Input& input, Dali::Devel::PixelBuffe // Load up the image bytes: bitmap = Dali::Devel::PixelBuffer::New(width, height, pixelFormat); + + // Compressed format won't allocate the buffer auto pixels = bitmap.GetBuffer(); + if( !pixels ) + { + // allocate buffer manually + auto &impl = GetImplementation(bitmap); + impl.AllocateFixedSize(imageByteCount); + pixels = bitmap.GetBuffer(); + } if(!pixels) { DALI_LOG_ERROR( "Unable to reserve a pixel buffer to load the requested bitmap into.\n" ); return false; } + const size_t bytesRead = fread(pixels, 1, imageByteCount, fp); if(bytesRead != imageByteCount) {