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 attribute mediump vec4 aColor;
36 uniform mediump vec2 uOffset;
37 uniform mediump mat4 uMvpMatrix;
38 varying mediump vec2 vTexCoord;
39 varying mediump vec4 vColor;
43 mediump vec4 position = vec4( aPosition.xy + uOffset, 0.0, 1.0 );
44 gl_Position = uMvpMatrix * position;
45 vTexCoord = aTexCoord;
50 const char* FRAGMENT_SHADER_L8 = MAKE_SHADER(
51 uniform lowp vec4 uColor;
52 uniform sampler2D sTexture;
53 varying mediump vec2 vTexCoord;
54 varying mediump vec4 vColor;
58 mediump vec4 color = texture2D( sTexture, vTexCoord );
59 gl_FragColor = vec4( vColor.rgb * uColor.rgb, vColor.a * uColor.a * color.r );
63 const char* FRAGMENT_SHADER_RGBA = MAKE_SHADER(
64 uniform sampler2D sTexture;
65 varying mediump vec2 vTexCoord;
69 gl_FragColor = texture2D( sTexture, vTexCoord );
73 } // unnamed namespace
84 AtlasGlyphManager::AtlasGlyphManager()
86 mShaderL8 = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_L8 );
87 mShaderRgba = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_RGBA );
88 mAtlasManager = Dali::Toolkit::AtlasManager::New();
91 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
92 const BufferImage& bitmap,
93 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
95 DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index );
97 if ( mAtlasManager.Add( bitmap, slot ) )
99 // A new atlas was created so set the material details for the atlas
100 Dali::Atlas atlas = mAtlasManager.GetAtlasContainer( slot.mAtlasId );
101 Pixel::Format pixelFormat = mAtlasManager.GetPixelFormat( slot.mAtlasId );
102 Material material = Material::New( pixelFormat == Pixel::L8 ? mShaderL8 : mShaderRgba );
103 material.AddTexture( atlas, "sTexture" );
104 material.SetBlendMode( BlendingMode::ON );
105 mAtlasManager.SetMaterial( slot.mAtlasId, material );
108 GlyphRecordEntry record;
109 record.mIndex = glyph.index;
110 record.mImageId = slot.mImageId;
113 // Have glyph records been created for this fontId ?
114 bool foundGlyph = false;
115 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
116 fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
118 if ( fontGlyphRecordIt->mFontId == glyph.fontId )
120 fontGlyphRecordIt->mGlyphRecords.PushBack( record );
128 // We need to add a new font entry
129 FontGlyphRecord fontGlyphRecord;
130 fontGlyphRecord.mFontId = glyph.fontId;
131 fontGlyphRecord.mGlyphRecords.PushBack( record );
132 mFontGlyphRecords.push_back( fontGlyphRecord );
136 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
137 const Vector2& position,
138 Toolkit::AtlasManager::Mesh2D& mesh )
140 // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
141 mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
144 bool AtlasGlyphManager::IsCached( Text::FontId fontId,
145 Text::GlyphIndex index,
146 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
148 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
149 fontGlyphRecordIt != mFontGlyphRecords.end();
150 ++fontGlyphRecordIt )
152 if ( fontGlyphRecordIt->mFontId == fontId )
154 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
155 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
158 if ( glyphRecordIt->mIndex == index )
160 slot.mImageId = glyphRecordIt->mImageId;
161 slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
171 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
173 Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
174 return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
177 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
179 Toolkit::AtlasManager::AtlasSize size;
181 size.mHeight = height;
182 size.mBlockWidth = blockWidth;
183 size.mBlockHeight = blockHeight;
184 mAtlasManager.SetNewAtlasSize( size );
187 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
189 return mAtlasManager.GetPixelFormat( atlasId );
192 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
194 std::ostringstream verboseMetrics;
196 mMetrics.mGlyphCount = 0u;
197 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
198 fontGlyphRecordIt != mFontGlyphRecords.end();
199 ++fontGlyphRecordIt )
201 mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
203 verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
204 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
205 glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
206 ++glyphRecordEntryIt )
208 verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
210 verboseMetrics << "] ";
212 mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
214 mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
219 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta )
223 DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
225 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
226 fontGlyphRecordIt != mFontGlyphRecords.end();
227 ++fontGlyphRecordIt )
229 if ( fontGlyphRecordIt->mFontId == fontId )
231 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
232 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
235 if ( glyphRecordIt->mIndex == index )
237 glyphRecordIt->mCount += delta;
238 DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
240 if ( !glyphRecordIt->mCount )
242 mAtlasManager.Remove( glyphRecordIt->mImageId );
243 fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
251 // Should not arrive here
252 DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
256 Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const
258 return mAtlasManager.GetMaterial( atlasId );
261 AtlasGlyphManager::~AtlasGlyphManager()
263 // mAtlasManager handle is automatically released here
266 } // namespace Internal
268 } // namespace Toolkit