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>
23 #define MAKE_SHADER(A)#A
28 #if defined(DEBUG_ENABLED)
29 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_RENDERING");
32 const char* VERTEX_SHADER = MAKE_SHADER(
33 attribute mediump vec2 aPosition;
34 attribute mediump vec2 aTexCoord;
35 uniform mediump mat4 uMvpMatrix;
36 varying mediump vec2 vTexCoord;
40 mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
41 gl_Position = uMvpMatrix * position;
42 vTexCoord = aTexCoord;
46 const char* FRAGMENT_SHADER = MAKE_SHADER(
47 uniform sampler2D sTexture;
48 varying mediump vec2 vTexCoord;
52 gl_FragColor = texture2D( sTexture, vTexCoord );
56 const char* VERTEX_SHADER_SHADOW = MAKE_SHADER(
57 attribute mediump vec2 aPosition;
58 attribute mediump vec2 aTexCoord;
59 varying mediump vec2 vTexCoord;
63 mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
64 gl_Position = position;
65 vTexCoord = aTexCoord;
69 const char* FRAGMENT_SHADER_SHADOW = MAKE_SHADER(
70 uniform sampler2D sTexture;
71 uniform lowp vec4 uColor;
72 varying mediump vec2 vTexCoord;
76 mediump vec4 color = texture2D( sTexture, vTexCoord );
77 gl_FragColor = vec4(uColor.rgb, uColor.a*color.r);
81 } // unnamed namespace
92 AtlasGlyphManager::AtlasGlyphManager()
94 mAtlasManager = Dali::Toolkit::AtlasManager::New();
95 mEffectBufferShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
96 mShadowShader = Shader::New( VERTEX_SHADER_SHADOW, FRAGMENT_SHADER_SHADOW, Dali::Shader::HINT_MODIFIES_GEOMETRY );
99 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
100 const BufferImage& bitmap,
101 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
103 DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index );
105 mAtlasManager.Add( bitmap, slot );
107 GlyphRecordEntry record;
108 record.mIndex = glyph.index;
109 record.mImageId = slot.mImageId;
112 // Have glyph records been created for this fontId ?
113 bool foundGlyph = false;
114 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
115 fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
117 if ( fontGlyphRecordIt->mFontId == glyph.fontId )
119 fontGlyphRecordIt->mGlyphRecords.PushBack( record );
127 // We need to add a new font entry
128 FontGlyphRecord fontGlyphRecord;
129 fontGlyphRecord.mFontId = glyph.fontId;
130 fontGlyphRecord.mGlyphRecords.PushBack( record );
131 mFontGlyphRecords.push_back( fontGlyphRecord );
135 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
136 const Vector2& position,
137 Toolkit::AtlasManager::Mesh2D& mesh )
139 // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
140 mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
143 void AtlasGlyphManager::StitchMesh( Toolkit::AtlasManager::Mesh2D& first,
144 const Toolkit::AtlasManager::Mesh2D& second )
146 mAtlasManager.StitchMesh( first, second );
149 bool AtlasGlyphManager::Cached( Text::FontId fontId,
150 Text::GlyphIndex index,
151 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
153 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
154 fontGlyphRecordIt != mFontGlyphRecords.end();
155 ++fontGlyphRecordIt )
157 if ( fontGlyphRecordIt->mFontId == fontId )
159 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
160 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
163 if ( glyphRecordIt->mIndex == index )
165 slot.mImageId = glyphRecordIt->mImageId;
166 slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
176 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
178 Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
179 return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
182 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
184 Toolkit::AtlasManager::AtlasSize size;
186 size.mHeight = height;
187 size.mBlockWidth = blockWidth;
188 size.mBlockHeight = blockHeight;
189 mAtlasManager.SetNewAtlasSize( size );
192 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
194 return mAtlasManager.GetPixelFormat( atlasId );
197 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
199 std::ostringstream verboseMetrics;
201 mMetrics.mGlyphCount = 0u;
202 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
203 fontGlyphRecordIt != mFontGlyphRecords.end();
204 ++fontGlyphRecordIt )
206 mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
208 verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
209 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
210 glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
211 ++glyphRecordEntryIt )
213 verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
215 verboseMetrics << "] ";
217 mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
219 mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
224 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta )
228 DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
230 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
231 fontGlyphRecordIt != mFontGlyphRecords.end();
232 ++fontGlyphRecordIt )
234 if ( fontGlyphRecordIt->mFontId == fontId )
236 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
237 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
240 if ( glyphRecordIt->mIndex == index )
242 glyphRecordIt->mCount += delta;
243 DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
245 if ( !glyphRecordIt->mCount )
247 mAtlasManager.Remove( glyphRecordIt->mImageId );
248 fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
256 // Should not arrive here
257 DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
261 Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const
263 return mAtlasManager.GetMaterial( atlasId );
266 Image AtlasGlyphManager::GetImage( uint32_t atlasId ) const
268 return mAtlasManager.GetImage( atlasId );
271 AtlasGlyphManager::~AtlasGlyphManager()
273 // mAtlasManager handle is automatically released here
276 } // namespace Internal
278 } // namespace Toolkit