2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager-impl.h>
21 #include <dali/integration-api/debug.h>
26 #if defined(DEBUG_ENABLED)
27 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_RENDERING");
30 #define MAKE_SHADER(A)#A
32 const char* VERTEX_SHADER = MAKE_SHADER(
33 attribute mediump vec2 aPosition;
34 attribute mediump vec2 aTexCoord;
35 uniform mediump vec2 uOffset;
36 uniform mediump mat4 uMvpMatrix;
37 varying mediump vec2 vTexCoord;
41 mediump vec4 position = vec4( aPosition.xy + uOffset, 0.0, 1.0 );
42 gl_Position = uMvpMatrix * position;
43 vTexCoord = aTexCoord;
47 const char* FRAGMENT_SHADER_L8 = MAKE_SHADER(
48 uniform lowp vec4 uColor;
49 uniform sampler2D sTexture;
50 varying mediump vec2 vTexCoord;
54 mediump vec4 color = texture2D( sTexture, vTexCoord );
55 gl_FragColor = vec4( uColor.rgb, uColor.a * color.r );
59 const char* FRAGMENT_SHADER_RGBA = MAKE_SHADER(
60 uniform sampler2D sTexture;
61 varying mediump vec2 vTexCoord;
65 gl_FragColor = texture2D( sTexture, vTexCoord );
69 } // unnamed namespace
80 AtlasGlyphManager::AtlasGlyphManager()
82 mShaderL8 = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_L8 );
83 mShaderRgba = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_RGBA );
84 mAtlasManager = Dali::Toolkit::AtlasManager::New();
87 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
88 const BufferImage& bitmap,
89 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
91 DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index );
93 if ( mAtlasManager.Add( bitmap, slot ) )
95 // A new atlas was created so set the material details for the atlas
96 Dali::Atlas atlas = mAtlasManager.GetAtlasContainer( slot.mAtlasId );
97 Pixel::Format pixelFormat = mAtlasManager.GetPixelFormat( slot.mAtlasId );
98 Material material = Material::New( pixelFormat == Pixel::L8 ? mShaderL8 : mShaderRgba );
99 material.AddTexture( atlas, "sTexture" );
100 material.SetBlendMode( BlendingMode::ON );
101 mAtlasManager.SetMaterial( slot.mAtlasId, material );
104 GlyphRecordEntry record;
105 record.mIndex = glyph.index;
106 record.mImageId = slot.mImageId;
109 // Have glyph records been created for this fontId ?
110 bool foundGlyph = false;
111 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
112 fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
114 if ( fontGlyphRecordIt->mFontId == glyph.fontId )
116 fontGlyphRecordIt->mGlyphRecords.PushBack( record );
124 // We need to add a new font entry
125 FontGlyphRecord fontGlyphRecord;
126 fontGlyphRecord.mFontId = glyph.fontId;
127 fontGlyphRecord.mGlyphRecords.PushBack( record );
128 mFontGlyphRecords.push_back( fontGlyphRecord );
132 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
133 const Vector2& position,
134 Toolkit::AtlasManager::Mesh2D& mesh )
136 // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
137 mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
140 bool AtlasGlyphManager::IsCached( Text::FontId fontId,
141 Text::GlyphIndex index,
142 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
144 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
145 fontGlyphRecordIt != mFontGlyphRecords.end();
146 ++fontGlyphRecordIt )
148 if ( fontGlyphRecordIt->mFontId == fontId )
150 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
151 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
154 if ( glyphRecordIt->mIndex == index )
156 slot.mImageId = glyphRecordIt->mImageId;
157 slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
167 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
169 Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
170 return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
173 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
175 Toolkit::AtlasManager::AtlasSize size;
177 size.mHeight = height;
178 size.mBlockWidth = blockWidth;
179 size.mBlockHeight = blockHeight;
180 mAtlasManager.SetNewAtlasSize( size );
183 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
185 return mAtlasManager.GetPixelFormat( atlasId );
188 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
190 std::ostringstream verboseMetrics;
192 mMetrics.mGlyphCount = 0u;
193 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
194 fontGlyphRecordIt != mFontGlyphRecords.end();
195 ++fontGlyphRecordIt )
197 mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
199 verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
200 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
201 glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
202 ++glyphRecordEntryIt )
204 verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
206 verboseMetrics << "] ";
208 mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
210 mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
215 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta )
219 DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
221 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
222 fontGlyphRecordIt != mFontGlyphRecords.end();
223 ++fontGlyphRecordIt )
225 if ( fontGlyphRecordIt->mFontId == fontId )
227 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
228 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
231 if ( glyphRecordIt->mIndex == index )
233 glyphRecordIt->mCount += delta;
234 DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
236 if ( !glyphRecordIt->mCount )
238 mAtlasManager.Remove( glyphRecordIt->mImageId );
239 fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
247 // Should not arrive here
248 DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
252 Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const
254 return mAtlasManager.GetMaterial( atlasId );
257 AtlasGlyphManager::~AtlasGlyphManager()
259 // mAtlasManager handle is automatically released here
262 } // namespace Internal
264 } // namespace Toolkit