X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-toolkit%2Finternal%2Ftext%2Frendering%2Fatlas%2Fatlas-glyph-manager-impl.cpp;h=1053ea9c0731499ef59b91f31fd7fe94538765b7;hb=refs%2Fchanges%2F87%2F42887%2F6;hp=3c21923b7804a4904e763f45cc39d4e15030eb64;hpb=2071317d301e9d5af326a3a2efcd2996263fbead;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 index 3c21923..1053ea9 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 @@ -21,6 +21,64 @@ #include #include +#define MAKE_SHADER(A)#A + +namespace +{ +const char* VERTEX_SHADER = MAKE_SHADER( +attribute mediump vec2 aPosition; +attribute mediump vec2 aTexCoord; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec4 position = vec4( aPosition, 0.0, 1.0 ); + position.xyz *= uSize; + gl_Position = uMvpMatrix * position; + vTexCoord = aTexCoord; +} +); + +const char* FRAGMENT_SHADER = MAKE_SHADER( +uniform sampler2D sTexture; +varying mediump vec2 vTexCoord; + +void main() +{ + gl_FragColor = texture2D( sTexture, vTexCoord ); +} +); + +const char* VERTEX_SHADER_SHADOW = MAKE_SHADER( +attribute mediump vec2 aPosition; +attribute mediump vec2 aTexCoord; +uniform mediump vec3 uSize; +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec4 position = vec4( aPosition, 0.0, 1.0 ); + position.xyz *= uSize; + gl_Position = position; + vTexCoord = aTexCoord; +} +); + +const char* FRAGMENT_SHADER_SHADOW = MAKE_SHADER( +uniform sampler2D sTexture; +uniform lowp vec4 uColor; +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec4 color = texture2D( sTexture, vTexCoord ); + gl_FragColor = vec4(uColor.rgb, uColor.a*color.r); +} +); +} + namespace Dali { @@ -30,16 +88,27 @@ namespace Toolkit namespace Internal { -//#define DISPLAY_ATLAS - AtlasGlyphManager::AtlasGlyphManager() -: mCount( 0 ) { mAtlasManager = Dali::Toolkit::AtlasManager::New(); + mEffectBufferShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER ); + mShadowShader = Shader::New( VERTEX_SHADER_SHADOW, FRAGMENT_SHADER_SHADOW, Dali::Shader::HINT_MODIFIES_GEOMETRY ); } AtlasGlyphManager::~AtlasGlyphManager() { + // 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 ); + } + } } AtlasGlyphManagerPtr AtlasGlyphManager::New() @@ -52,38 +121,42 @@ void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph, const BufferImage& bitmap, Dali::Toolkit::AtlasManager::AtlasSlot& slot ) { - GlyphRecord record; - record.mFontId = glyph.fontId; - record.mIndex = glyph.index; - mAtlasManager.Add( bitmap, slot ); + + GlyphRecordEntry record; + record.mIndex = glyph.index; record.mImageId = slot.mImageId; - mGlyphRecords.PushBack( record ); + record.mCount = 1; -#ifdef DISPLAY_ATLAS + // Have glyph records been created for this fontId ? + bool foundGlyph = false; + for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); + fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt ) { - uint32_t atlasCount = mAtlasManager.GetAtlasCount(); - if ( atlasCount > mCount ) + if ( fontGlyphRecordIt->mFontId == glyph.fontId ) { - 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 ); - } + fontGlyphRecordIt->mGlyphRecords.PushBack( record ); + foundGlyph = true; + break; } - mCount = atlasCount; } -#endif + + if ( !foundGlyph ) + { + // We need to add a new font entry + FontGlyphRecord fontGlyphRecord; + fontGlyphRecord.mFontId = glyph.fontId; + fontGlyphRecord.mGlyphRecords.PushBack( record ); + mFontGlyphRecords.push_back( fontGlyphRecord ); + } } void AtlasGlyphManager::GenerateMeshData( uint32_t imageId, const Vector2& position, Toolkit::AtlasManager::Mesh2D& mesh ) { - mAtlasManager.GenerateMeshData( imageId, position, mesh ); + // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it ) + mAtlasManager.GenerateMeshData( imageId, position, mesh, false ); } void AtlasGlyphManager::StitchMesh( Toolkit::AtlasManager::Mesh2D& first, @@ -92,20 +165,31 @@ void AtlasGlyphManager::StitchMesh( Toolkit::AtlasManager::Mesh2D& first, mAtlasManager.StitchMesh( first, second ); } -void AtlasGlyphManager::Cached( Text::FontId fontId, +bool AtlasGlyphManager::Cached( Text::FontId fontId, uint32_t index, Dali::Toolkit::AtlasManager::AtlasSlot& slot ) { - for ( uint32_t i = 0; i < mGlyphRecords.Size(); ++i ) + for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); + fontGlyphRecordIt != mFontGlyphRecords.end(); + ++fontGlyphRecordIt ) { - if ( fontId == mGlyphRecords[ i ].mFontId && index == mGlyphRecords[ i ].mIndex ) + if ( fontGlyphRecordIt->mFontId == fontId ) { - slot.mImageId = mGlyphRecords[ i ].mImageId; - slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId ); - return; + for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin(); + glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End(); + ++glyphRecordIt ) + { + if ( glyphRecordIt->mIndex == index ) + { + slot.mImageId = glyphRecordIt->mImageId; + slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId ); + return true; + } + } } } slot.mImageId = 0; + return false; } Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId ) @@ -124,26 +208,51 @@ void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32 mAtlasManager.SetNewAtlasSize( size ); } -void AtlasGlyphManager::Remove( uint32_t imageId ) +Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId ) { - if ( mAtlasManager.Remove( imageId ) ) + return mAtlasManager.GetPixelFormat( atlasId ); +} + +const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics() +{ + mMetrics.mGlyphCount = 0u; + for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); + fontGlyphRecordIt != mFontGlyphRecords.end(); + ++fontGlyphRecordIt ) { - for ( uint32_t i = 0; i < mGlyphRecords.Size(); ++i ) + mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size(); + } + mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics ); + return mMetrics; +} + +void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, uint32_t imageId, int32_t delta ) +{ + for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin(); + fontGlyphRecordIt != mFontGlyphRecords.end(); + ++fontGlyphRecordIt ) + { + if ( fontGlyphRecordIt->mFontId == fontId ) { - if ( mGlyphRecords[ i ].mImageId == imageId ) + for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin(); + glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End(); + ++glyphRecordIt ) { - mGlyphRecords.Remove( mGlyphRecords.Begin() + i ); - return; + if ( glyphRecordIt->mImageId == imageId ) + { + glyphRecordIt->mCount += delta; + if ( !glyphRecordIt->mCount ) + { + mAtlasManager.Remove( glyphRecordIt->mImageId ); + fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt ); + } + return; + } } } } } -Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId ) -{ - return mAtlasManager.GetPixelFormat( atlasId ); -} - Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const { return mAtlasManager.GetMaterial( atlasId ); @@ -154,13 +263,6 @@ Sampler AtlasGlyphManager::GetSampler( uint32_t atlasId ) const return mAtlasManager.GetSampler( atlasId ); } -const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics() -{ - mMetrics.mGlyphCount = mGlyphRecords.Size(); - mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics ); - return mMetrics; -} - } // namespace Internal } // namespace Toolkit