X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;ds=sidebyside;f=dali-toolkit%2Finternal%2Ftext%2Frendering%2Fatlas%2Fatlas-glyph-manager-impl.cpp;h=a9728109d0833030103b30f003cb7c7410368f07;hb=6610599b02f7ff98c58a778f106be41f264226d4;hp=03091e5539997551afd3113ea5fe8d5c4b33fddb;hpb=0f548619463110abcd5c2b8d6787491110188da8;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager-impl.cpp b/dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager-impl.cpp old mode 100644 new mode 100755 index 03091e5..a972810 --- a/dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager-impl.cpp +++ b/dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager-impl.cpp @@ -1,5 +1,5 @@ - /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. +/* + * Copyright (c) 2019 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. @@ -18,8 +18,16 @@ #include // EXTERNAL INCLUDES -#include -#include +#include + +namespace +{ + +#if defined(DEBUG_ENABLED) + Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_RENDERING"); +#endif + +} // unnamed namespace namespace Dali { @@ -30,115 +38,198 @@ namespace Toolkit namespace Internal { -//#define DISPLAY_ATLAS - AtlasGlyphManager::AtlasGlyphManager() -: mCount( 0 ) { mAtlasManager = Dali::Toolkit::AtlasManager::New(); + mSampler = Sampler::New(); + mSampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST ); } -AtlasGlyphManager::~AtlasGlyphManager() +void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph, + const Toolkit::AtlasGlyphManager::GlyphStyle& style, + const PixelData& bitmap, + Dali::Toolkit::AtlasManager::AtlasSlot& slot ) { + DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index ); + + // If glyph added to an existing or new atlas then a new glyph record is required. + // Check if an existing atlas will fit the image, create a new one if required. + if ( mAtlasManager.Add( bitmap, slot ) ) + { + // A new atlas was created so set the texture set details for the atlas + Dali::Texture atlas = mAtlasManager.GetAtlasContainer( slot.mAtlasId ); + TextureSet textureSet = TextureSet::New(); + textureSet.SetTexture( 0u, atlas ); + textureSet.SetSampler( 0u, mSampler); + mAtlasManager.SetTextures( slot.mAtlasId, textureSet ); + } + + GlyphRecordEntry record; + record.mIndex = glyph.index; + record.mImageId = slot.mImageId; + record.mCount = 1; + record.mOutlineWidth = style.outline; + record.isItalic = style.isItalic; + record.isBold = style.isBold; + + // Have glyph records been created for this fontId ? + bool foundGlyph = false; + for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); + fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt ) + { + if ( fontGlyphRecordIt->mFontId == glyph.fontId ) + { + fontGlyphRecordIt->mGlyphRecords.PushBack( record ); + foundGlyph = true; + break; + } + } + + if ( !foundGlyph ) + { + // We need to add a new font entry + FontGlyphRecord fontGlyphRecord; + fontGlyphRecord.mFontId = glyph.fontId; + fontGlyphRecord.mGlyphRecords.PushBack( record ); + mFontGlyphRecords.push_back( fontGlyphRecord ); + } } -AtlasGlyphManagerPtr AtlasGlyphManager::New() +void AtlasGlyphManager::GenerateMeshData( uint32_t imageId, + const Vector2& position, + Toolkit::AtlasManager::Mesh2D& mesh ) { - AtlasGlyphManagerPtr internal = new AtlasGlyphManager(); - return internal; + // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it ) + mAtlasManager.GenerateMeshData( imageId, position, mesh, false ); } -void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph, - const BufferImage& bitmap, - Dali::Toolkit::AtlasManager::AtlasSlot& slot ) +bool AtlasGlyphManager::IsCached( Text::FontId fontId, + Text::GlyphIndex index, + const Toolkit::AtlasGlyphManager::GlyphStyle& style, + Dali::Toolkit::AtlasManager::AtlasSlot& slot ) { - GlyphRecord record; - record.mFontId = glyph.fontId; - record.mIndex = glyph.index; - - mAtlasManager.Add( bitmap, slot ); - record.mImageId = slot.mImageId; - mGlyphRecords.PushBack( record ); - -#ifdef DISPLAY_ATLAS + for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); + fontGlyphRecordIt != mFontGlyphRecords.end(); + ++fontGlyphRecordIt ) { - uint32_t atlasCount = mAtlasManager.GetAtlasCount(); - if ( atlasCount > mCount ) + if ( fontGlyphRecordIt->mFontId == fontId ) { - for ( uint32_t i = 0; i < atlasCount; ++i ) + for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin(); + glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End(); + ++glyphRecordIt ) { - ImageActor actor = ImageActor::New( mAtlasManager.GetAtlasContainer( i + 1u ) ); - actor.SetParentOrigin( Vector3( 0.5f, 0.25f + ( static_cast< float >( i ) * 0.25f ), 0.5f ) ); - actor.SetAnchorPoint( AnchorPoint::CENTER ); - actor.SetSize( 256.0f, 256.0f ); - Stage::GetCurrent().Add( actor ); + if ( ( glyphRecordIt->mIndex == index ) && + ( glyphRecordIt->mOutlineWidth == style.outline ) && + ( glyphRecordIt->isItalic == style.isItalic ) && + ( glyphRecordIt->isBold == style.isBold ) ) + { + slot.mImageId = glyphRecordIt->mImageId; + slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId ); + return true; + } } } - mCount = atlasCount; } -#endif + slot.mImageId = 0; + return false; } -void AtlasGlyphManager::GenerateMeshData( uint32_t imageId, - const Vector2& position, - MeshData& meshData ) +Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId ) +{ + Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId ); + return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) ); +} + +void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight ) { - mAtlasManager.GenerateMeshData( imageId, position, meshData ); + Toolkit::AtlasManager::AtlasSize size; + size.mWidth = width; + size.mHeight = height; + size.mBlockWidth = blockWidth; + size.mBlockHeight = blockHeight; + mAtlasManager.SetNewAtlasSize( size ); } -void AtlasGlyphManager::StitchMesh( MeshData& first, - const MeshData& second ) +Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId ) { - mAtlasManager.StitchMesh( first, second ); + return mAtlasManager.GetPixelFormat( atlasId ); } -void AtlasGlyphManager::Cached( Text::FontId fontId, - uint32_t index, - Dali::Toolkit::AtlasManager::AtlasSlot& slot ) +const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics() { - for ( uint32_t i = 0; i < mGlyphRecords.Size(); ++i ) + std::ostringstream verboseMetrics; + + mMetrics.mGlyphCount = 0u; + for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); + fontGlyphRecordIt != mFontGlyphRecords.end(); + ++fontGlyphRecordIt ) { - if ( fontId == mGlyphRecords[ i ].mFontId && index == mGlyphRecords[ i ].mIndex ) + mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size(); + + verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph "; + for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin(); + glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End(); + ++glyphRecordEntryIt ) { - slot.mImageId = mGlyphRecords[ i ].mImageId; - slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId ); - return; + verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") "; } + verboseMetrics << "] "; } - slot.mImageId = 0; -} + mMetrics.mVerboseGlyphCounts = verboseMetrics.str(); -void AtlasGlyphManager::SetNewAtlasSize( const Vector2& size, - const Vector2& blockSize ) -{ - mAtlasManager.SetNewAtlasSize( size, blockSize ); + mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics ); + + return mMetrics; } -void AtlasGlyphManager::Remove( uint32_t imageId ) +void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, const Toolkit::AtlasGlyphManager::GlyphStyle& style, int32_t delta ) { - if ( mAtlasManager.Remove( imageId ) ) + if( 0 != delta ) { - for ( uint32_t i = 0; i < mGlyphRecords.Size(); ++i ) + DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index ); + + for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); + fontGlyphRecordIt != mFontGlyphRecords.end(); + ++fontGlyphRecordIt ) { - if ( mGlyphRecords[ i ].mImageId == imageId ) + if ( fontGlyphRecordIt->mFontId == fontId ) { - mGlyphRecords.Remove( mGlyphRecords.Begin() + i ); - return; + for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin(); + glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End(); + ++glyphRecordIt ) + { + if ( ( glyphRecordIt->mIndex == index ) && + ( glyphRecordIt->mOutlineWidth == style.outline ) && + ( glyphRecordIt->isItalic == style.isItalic ) && + ( glyphRecordIt->isBold == style.isBold ) ) + { + glyphRecordIt->mCount += delta; + DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" ); + + if ( !glyphRecordIt->mCount ) + { + mAtlasManager.Remove( glyphRecordIt->mImageId ); + fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt ); + } + return; + } + } } } + + // Should not arrive here + DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" ); } } -Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId ) +TextureSet AtlasGlyphManager::GetTextures( uint32_t atlasId ) const { - return mAtlasManager.GetPixelFormat( atlasId ); + return mAtlasManager.GetTextures( atlasId ); } -const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics() +AtlasGlyphManager::~AtlasGlyphManager() { - mMetrics.mGlyphCount = mGlyphRecords.Size(); - mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics ); - return mMetrics; + // mAtlasManager handle is automatically released here } } // namespace Internal