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 mAtlasManager.SetMaterial( slot.mAtlasId, material );
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 bool AtlasGlyphManager::IsCached( Text::FontId fontId,
144 Text::GlyphIndex index,
145 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
147 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
148 fontGlyphRecordIt != mFontGlyphRecords.end();
149 ++fontGlyphRecordIt )
151 if ( fontGlyphRecordIt->mFontId == fontId )
153 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
154 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
157 if ( glyphRecordIt->mIndex == index )
159 slot.mImageId = glyphRecordIt->mImageId;
160 slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
170 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
172 Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
173 return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
176 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
178 Toolkit::AtlasManager::AtlasSize size;
180 size.mHeight = height;
181 size.mBlockWidth = blockWidth;
182 size.mBlockHeight = blockHeight;
183 mAtlasManager.SetNewAtlasSize( size );
186 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
188 return mAtlasManager.GetPixelFormat( atlasId );
191 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
193 std::ostringstream verboseMetrics;
195 mMetrics.mGlyphCount = 0u;
196 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
197 fontGlyphRecordIt != mFontGlyphRecords.end();
198 ++fontGlyphRecordIt )
200 mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
202 verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
203 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
204 glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
205 ++glyphRecordEntryIt )
207 verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
209 verboseMetrics << "] ";
211 mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
213 mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
218 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta )
222 DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
224 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
225 fontGlyphRecordIt != mFontGlyphRecords.end();
226 ++fontGlyphRecordIt )
228 if ( fontGlyphRecordIt->mFontId == fontId )
230 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
231 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
234 if ( glyphRecordIt->mIndex == index )
236 glyphRecordIt->mCount += delta;
237 DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
239 if ( !glyphRecordIt->mCount )
241 mAtlasManager.Remove( glyphRecordIt->mImageId );
242 fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
250 // Should not arrive here
251 DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
255 Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const
257 return mAtlasManager.GetMaterial( atlasId );
260 AtlasGlyphManager::~AtlasGlyphManager()
262 // mAtlasManager handle is automatically released here
265 } // namespace Internal
267 } // namespace Toolkit