X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fnpatch-loader.cpp;h=2550001067b70ffeb87d02d2465192ad59182369;hp=0c6409d4bf0b230ee68a84d3e212bbfc5a283983;hb=f27c332dcf251d50ddfe3b2ab15ec2eaff5296b1;hpb=bb0006f9fa400c6e3c3ec8aaf698d8095dbd646d diff --git a/dali-toolkit/internal/visuals/npatch-loader.cpp b/dali-toolkit/internal/visuals/npatch-loader.cpp index 0c6409d..2550001 100644 --- a/dali-toolkit/internal/visuals/npatch-loader.cpp +++ b/dali-toolkit/internal/visuals/npatch-loader.cpp @@ -18,8 +18,10 @@ // CLASS HEADER #include -// EXTERNAL HEADER -#include +// INTERNAL HEADERS +#include + +// EXTERNAL HEADERS #include #include @@ -35,226 +37,11 @@ namespace Internal namespace NPatchBuffer { -void GetRedOffsetAndMask( Dali::Pixel::Format pixelFormat, int& byteOffset, int& bitMask ) -{ - switch( pixelFormat ) - { - case Dali::Pixel::A8: - case Dali::Pixel::L8: - case Dali::Pixel::LA88: - { - byteOffset = 0; - bitMask = 0; - break; - } - case Dali::Pixel::RGB888: - case Dali::Pixel::RGB8888: - case Dali::Pixel::RGBA8888: - { - byteOffset = 0; - bitMask = 0xFF; - break; - } - case Dali::Pixel::BGR8888: - case Dali::Pixel::BGRA8888: - { - byteOffset = 2; - bitMask = 0xff; - break; - } - case Dali::Pixel::RGB565: - { - byteOffset = 0; - bitMask = 0xf8; - break; - } - case Dali::Pixel::BGR565: - { - byteOffset = 1; - bitMask = 0x1f; - break; - } - case Dali::Pixel::RGBA4444: - { - byteOffset = 0; - bitMask = 0xf0; - break; - } - case Dali::Pixel::BGRA4444: - { - byteOffset = 1; - bitMask = 0xf0; - break; - } - case Dali::Pixel::RGBA5551: - { - byteOffset = 0; - bitMask = 0xf8; - break; - } - case Dali::Pixel::BGRA5551: - { - byteOffset = 1; - bitMask = 0x1e; - break; - } - case Dali::Pixel::INVALID: - case Dali::Pixel::COMPRESSED_R11_EAC: - case Dali::Pixel::COMPRESSED_SIGNED_R11_EAC: - case Dali::Pixel::COMPRESSED_RG11_EAC: - case Dali::Pixel::COMPRESSED_SIGNED_RG11_EAC: - case Dali::Pixel::COMPRESSED_RGB8_ETC2: - case Dali::Pixel::COMPRESSED_SRGB8_ETC2: - case Dali::Pixel::COMPRESSED_RGB8_ETC1: - case Dali::Pixel::COMPRESSED_RGB_PVRTC_4BPPV1: - case Dali::Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2: - case Dali::Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2: - case Dali::Pixel::COMPRESSED_RGBA8_ETC2_EAC: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x4_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x5_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x5_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x6_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x5_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x6_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x8_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x5_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x8_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x10_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x10_KHR: - case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x12_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR: - case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR: - { - DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n"); - byteOffset=0; - bitMask=0; - break; - } - case Dali::Pixel::RGB16F: - case Dali::Pixel::RGB32F: - case Dali::Pixel::DEPTH_UNSIGNED_INT: - case Dali::Pixel::DEPTH_FLOAT: - case Dali::Pixel::DEPTH_STENCIL: - { - DALI_LOG_ERROR("Pixel format not compatible.\n"); - byteOffset=0; - bitMask=0; - break; - } - } -} - -Uint16Pair ParseRange( unsigned int& index, unsigned int width, unsigned char*& pixel, unsigned int pixelStride, int testByte, int testBits, int testValue ) -{ - unsigned int start = 0xFFFF; - for( ; index < width; ++index, pixel += pixelStride ) - { - if( ( pixel[ testByte ] & testBits ) == testValue ) - { - start = index; - ++index; - pixel += pixelStride; - break; - } - } - - unsigned int end = width; - for( ; index < width; ++index, pixel += pixelStride ) - { - if( ( pixel[ testByte ] & testBits ) != testValue ) - { - end = index; - ++index; - pixel += pixelStride; - break; - } - } - - return Uint16Pair( start, end ); -} - -void ParseBorders( Devel::PixelBuffer& pixelBuffer, NPatchLoader::Data* data ) -{ - data->stretchPixelsX.Clear(); - data->stretchPixelsY.Clear(); - - Pixel::Format pixelFormat = pixelBuffer.GetPixelFormat(); - - int alphaByte = 0; - int alphaBits = 0; - Pixel::GetAlphaOffsetAndMask( pixelFormat, alphaByte, alphaBits ); - - int testByte = alphaByte; - int testBits = alphaBits; - int testValue = alphaBits; // Opaque == stretch - if( !alphaBits ) - { - GetRedOffsetAndMask( pixelFormat, testByte, testBits ); - testValue = 0; // Black == stretch - } - - unsigned int bytesPerPixel = Pixel::GetBytesPerPixel( pixelFormat ); - unsigned int width = pixelBuffer.GetWidth(); - unsigned int height = pixelBuffer.GetHeight(); - unsigned char* srcPixels = pixelBuffer.GetBuffer(); - unsigned int srcStride = width * bytesPerPixel; - - // TOP - unsigned char* top = srcPixels + bytesPerPixel; - unsigned int index = 0; - - for( ; index < width - 2; ) - { - Uint16Pair range = ParseRange( index, width - 2, top, bytesPerPixel, testByte, testBits, testValue ); - if( range.GetX() != 0xFFFF ) - { - data->stretchPixelsX.PushBack( range ); - } - } - - // LEFT - unsigned char* left = srcPixels + srcStride; - index = 0; - for( ; index < height - 2; ) - { - Uint16Pair range = ParseRange( index, height - 2, left, srcStride, testByte, testBits, testValue ); - if( range.GetX() != 0xFFFF ) - { - data->stretchPixelsY.PushBack( range ); - } - } - - // If there are no stretch pixels then make the entire image stretchable - if( data->stretchPixelsX.Size() == 0 ) - { - data->stretchPixelsX.PushBack( Uint16Pair( 0, width - 2 ) ); - } - if( data->stretchPixelsY.Size() == 0 ) - { - data->stretchPixelsY.PushBack( Uint16Pair( 0, height - 2 ) ); - } -} - void SetLoadedNPatchData( NPatchLoader::Data* data, Devel::PixelBuffer& pixelBuffer ) { if( data->border == Rect< int >( 0, 0, 0, 0 ) ) { - NPatchBuffer::ParseBorders( pixelBuffer, data ); + NPatchUtility::ParseBorders( pixelBuffer, data->stretchPixelsX, data->stretchPixelsY ); // Crop the image pixelBuffer.Crop( 1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2 ); @@ -268,6 +55,9 @@ void SetLoadedNPatchData( NPatchLoader::Data* data, Devel::PixelBuffer& pixelBuf data->croppedWidth = pixelBuffer.GetWidth(); data->croppedHeight = pixelBuffer.GetHeight(); + // Create opacity map + data->renderingMap = RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().BuildNPatch(pixelBuffer, data ) : nullptr; + PixelData pixels = Devel::PixelBuffer::Convert( pixelBuffer ); // takes ownership of buffer Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() ); @@ -281,6 +71,15 @@ void SetLoadedNPatchData( NPatchLoader::Data* data, Devel::PixelBuffer& pixelBuf } // namespace NPatchBuffer +NPatchLoader::Data::~Data() +{ + // If there is an opacity map, it has to be destroyed using addon call + if( renderingMap ) + { + RenderingAddOn::Get().DestroyNPatch( renderingMap ); + } +} + NPatchLoader::NPatchLoader() { } @@ -311,9 +110,9 @@ std::size_t NPatchLoader::Load( TextureManager& textureManager, TextureUploadObs { return index + 1u; // valid indices are from 1 onwards } + mCache[ index ]->observerList.PushBack( textureObserver ); data = mCache[ index ]; - cachedIndex = index + 1u; // valid indices are from 1 onwards - break; + return index + 1u; // valid indices are from 1 onwards } else { @@ -328,10 +127,10 @@ std::size_t NPatchLoader::Load( TextureManager& textureManager, TextureUploadObs data->textureSet = mCache[ index ]->textureSet; - StretchRanges stretchRangesX; + NPatchUtility::StretchRanges stretchRangesX; stretchRangesX.PushBack( Uint16Pair( border.left, ( (data->croppedWidth >= static_cast< unsigned int >( border.right )) ? data->croppedWidth - border.right : 0 ) ) ); - StretchRanges stretchRangesY; + NPatchUtility::StretchRanges stretchRangesY; stretchRangesY.PushBack( Uint16Pair( border.top, ( (data->croppedHeight >= static_cast< unsigned int >( border.bottom )) ? data->croppedHeight - border.bottom : 0 ) ) ); data->stretchPixelsX = stretchRangesX; @@ -377,14 +176,25 @@ std::size_t NPatchLoader::Load( TextureManager& textureManager, TextureUploadObs return cachedIndex; } -void NPatchLoader::SetNPatchData( std::size_t id, Devel::PixelBuffer& pixelBuffer ) +void NPatchLoader::SetNPatchData( bool loadSuccess, std::size_t id, Devel::PixelBuffer& pixelBuffer, const Internal::VisualUrl& url, bool preMultiplied ) { Data* data; data = mCache[ id - 1u ]; - if( !data->loadCompleted ) + // To prevent recursion. + // data->loadCompleted will be set true in the NPatchBuffer::SetLoadedNPatchData when the first observer called this method. + if( data->loadCompleted ) { - NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer ); + return; + } + + NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer ); + + while( data->observerList.Count() ) + { + TextureUploadObserver* observer = data->observerList[0]; + observer->LoadComplete( loadSuccess, Devel::PixelBuffer(), url, preMultiplied ); + data->observerList.Erase( data->observerList.begin() ); } }