X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Ftext%2Frendering%2Fatlas%2Fatlas-glyph-manager-impl.cpp;h=12d39a51b30532f3c176abd91db7e2a70442c9f0;hp=d7fde4edd01f90252f8416d292761cc40cbf0a23;hb=3b5d5f82057a49f259e62aa8349e37ea6a9c221f;hpb=64cacc4d250a25eb19c27ed55741f3ae61266518 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 index d7fde4e..12d39a5 100644 --- 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,4 +1,4 @@ - /* +/* * Copyright (c) 2015 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,54 +18,91 @@ #include // EXTERNAL INCLUDES -#include -#include +#include -namespace Dali +namespace { -namespace Toolkit -{ +#if defined(DEBUG_ENABLED) + Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_RENDERING"); +#endif -namespace Internal +#define MAKE_SHADER(A)#A + +const char* VERTEX_SHADER = MAKE_SHADER( +attribute mediump vec2 aPosition; +attribute mediump vec2 aTexCoord; +attribute mediump vec4 aColor; +uniform mediump vec2 uOffset; +uniform mediump mat4 uMvpMatrix; +varying mediump vec2 vTexCoord; +varying mediump vec4 vColor; + +void main() { + mediump vec4 position = vec4( aPosition.xy + uOffset, 0.0, 1.0 ); + gl_Position = uMvpMatrix * position; + vTexCoord = aTexCoord; + vColor = aColor; +} +); -//#define DISPLAY_ATLAS +const char* FRAGMENT_SHADER_L8 = MAKE_SHADER( +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec2 vTexCoord; +varying mediump vec4 vColor; -AtlasGlyphManager::AtlasGlyphManager() -: mCount( 0 ) +void main() { - mAtlasManager = Dali::Toolkit::AtlasManager::New(); + mediump vec4 color = texture2D( sTexture, vTexCoord ); + gl_FragColor = vec4( vColor.rgb * uColor.rgb, vColor.a * uColor.a * color.r ); } +); -AtlasGlyphManager::~AtlasGlyphManager() +const char* FRAGMENT_SHADER_RGBA = MAKE_SHADER( +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec2 vTexCoord; + +void main() { - // Clear up any remaining references - for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); - fontGlyphRecordIt != mFontGlyphRecords.end(); - ++fontGlyphRecordIt ) - { - for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin(); - glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End(); - ++glyphRecordEntryIt ) - { - mAtlasManager.Remove( glyphRecordEntryIt->mImageId ); - } - } + gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor; } +); + +} // unnamed namespace + +namespace Dali +{ -AtlasGlyphManagerPtr AtlasGlyphManager::New() +namespace Toolkit { - AtlasGlyphManagerPtr internal = new AtlasGlyphManager(); - return internal; + +namespace Internal +{ + +AtlasGlyphManager::AtlasGlyphManager() +{ + mShaderL8 = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_L8 ); + mShaderRgba = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_RGBA ); + mAtlasManager = Dali::Toolkit::AtlasManager::New(); } -void AtlasGlyphManager::Add( Text::FontId fontId, - const Text::GlyphInfo& glyph, - const BufferImage& bitmap, +void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph, + const PixelData& bitmap, Dali::Toolkit::AtlasManager::AtlasSlot& slot ) { - mAtlasManager.Add( bitmap, slot ); + DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index ); + + 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 ); + mAtlasManager.SetTextures( slot.mAtlasId, textureSet ); + } GlyphRecordEntry record; record.mIndex = glyph.index; @@ -77,7 +114,7 @@ void AtlasGlyphManager::Add( Text::FontId fontId, for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt ) { - if ( fontGlyphRecordIt->mFontId == fontId ) + if ( fontGlyphRecordIt->mFontId == glyph.fontId ) { fontGlyphRecordIt->mGlyphRecords.PushBack( record ); foundGlyph = true; @@ -89,46 +126,22 @@ void AtlasGlyphManager::Add( Text::FontId fontId, { // We need to add a new font entry FontGlyphRecord fontGlyphRecord; - fontGlyphRecord.mFontId = fontId; + fontGlyphRecord.mFontId = glyph.fontId; fontGlyphRecord.mGlyphRecords.PushBack( record ); mFontGlyphRecords.push_back( fontGlyphRecord ); } - -#ifdef DISPLAY_ATLAS - { - uint32_t atlasCount = mAtlasManager.GetAtlasCount(); - if ( atlasCount > mCount ) - { - for ( uint32_t i = 0; i < atlasCount; ++i ) - { - 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 ); - } - } - mCount = atlasCount; - } -#endif } void AtlasGlyphManager::GenerateMeshData( uint32_t imageId, const Vector2& position, - MeshData& meshData ) + Toolkit::AtlasManager::Mesh2D& mesh ) { // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it ) - mAtlasManager.GenerateMeshData( imageId, position, meshData, false ); + mAtlasManager.GenerateMeshData( imageId, position, mesh, false ); } -void AtlasGlyphManager::StitchMesh( MeshData& first, - const MeshData& second ) -{ - mAtlasManager.StitchMesh( first, second ); -} - -bool AtlasGlyphManager::Cached( Text::FontId fontId, - uint32_t index, +bool AtlasGlyphManager::IsCached( Text::FontId fontId, + Text::GlyphIndex index, Dali::Toolkit::AtlasManager::AtlasSlot& slot ) { for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); @@ -177,38 +190,84 @@ Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId ) const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics() { - mMetrics.mGlyphCount = mFontGlyphRecords.size(); + std::ostringstream verboseMetrics; + + mMetrics.mGlyphCount = 0u; + for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); + fontGlyphRecordIt != mFontGlyphRecords.end(); + ++fontGlyphRecordIt ) + { + mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size(); + + verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph "; + for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin(); + glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End(); + ++glyphRecordEntryIt ) + { + verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") "; + } + verboseMetrics << "] "; + } + mMetrics.mVerboseGlyphCounts = verboseMetrics.str(); + mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics ); + return mMetrics; } -void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, uint32_t imageId, int32_t delta ) +void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta ) { - for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); - fontGlyphRecordIt != mFontGlyphRecords.end(); - ++fontGlyphRecordIt ) + if( 0 != delta ) { - if ( fontGlyphRecordIt->mFontId == fontId ) + 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 ) { - for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin(); - glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End(); - ++glyphRecordIt ) + if ( fontGlyphRecordIt->mFontId == fontId ) { - if ( glyphRecordIt->mImageId == imageId ) + for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin(); + glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End(); + ++glyphRecordIt ) { - glyphRecordIt->mCount += delta; - if ( !glyphRecordIt->mCount ) + if ( glyphRecordIt->mIndex == index ) { - mAtlasManager.Remove( glyphRecordIt->mImageId ); - fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt ); + 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; } - return; } } } + + // Should not arrive here + DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" ); } } +TextureSet AtlasGlyphManager::GetTextures( uint32_t atlasId ) const +{ + return mAtlasManager.GetTextures( atlasId ); +} + +Shader AtlasGlyphManager::GetShader( uint32_t atlasId ) const +{ + Pixel::Format pixelFormat = mAtlasManager.GetPixelFormat( atlasId ); + return pixelFormat == Pixel::L8 ? mShaderL8 : mShaderRgba; +} + +AtlasGlyphManager::~AtlasGlyphManager() +{ + // mAtlasManager handle is automatically released here +} + } // namespace Internal } // namespace Toolkit