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/public-api/actors/image-actor.h>
22 #include <dali/public-api/common/stage.h>
23 #include <dali/integration-api/debug.h>
25 #define MAKE_SHADER(A)#A
30 #if defined(DEBUG_ENABLED)
31 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_RENDERING");
34 const char* VERTEX_SHADER = MAKE_SHADER(
35 attribute mediump vec2 aPosition;
36 attribute mediump vec2 aTexCoord;
37 uniform mediump mat4 uMvpMatrix;
38 uniform mediump vec3 uSize;
39 varying mediump vec2 vTexCoord;
43 mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
44 position.xyz *= uSize;
45 gl_Position = uMvpMatrix * position;
46 vTexCoord = aTexCoord;
50 const char* FRAGMENT_SHADER = MAKE_SHADER(
51 uniform sampler2D sTexture;
52 varying mediump vec2 vTexCoord;
56 gl_FragColor = texture2D( sTexture, vTexCoord );
60 const char* VERTEX_SHADER_SHADOW = MAKE_SHADER(
61 attribute mediump vec2 aPosition;
62 attribute mediump vec2 aTexCoord;
63 uniform mediump vec3 uSize;
64 varying mediump vec2 vTexCoord;
68 mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
69 position.xyz *= uSize;
70 gl_Position = position;
71 vTexCoord = aTexCoord;
75 const char* FRAGMENT_SHADER_SHADOW = MAKE_SHADER(
76 uniform sampler2D sTexture;
77 uniform lowp vec4 uColor;
78 varying mediump vec2 vTexCoord;
82 mediump vec4 color = texture2D( sTexture, vTexCoord );
83 gl_FragColor = vec4(uColor.rgb, uColor.a*color.r);
87 } // unnamed namespace
98 AtlasGlyphManager::AtlasGlyphManager()
100 mAtlasManager = Dali::Toolkit::AtlasManager::New();
101 mEffectBufferShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
102 mShadowShader = Shader::New( VERTEX_SHADER_SHADOW, FRAGMENT_SHADER_SHADOW, Dali::Shader::HINT_MODIFIES_GEOMETRY );
105 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
106 const BufferImage& bitmap,
107 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
109 DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index );
111 mAtlasManager.Add( bitmap, slot );
113 GlyphRecordEntry record;
114 record.mIndex = glyph.index;
115 record.mImageId = slot.mImageId;
118 // Have glyph records been created for this fontId ?
119 bool foundGlyph = false;
120 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
121 fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
123 if ( fontGlyphRecordIt->mFontId == glyph.fontId )
125 fontGlyphRecordIt->mGlyphRecords.PushBack( record );
133 // We need to add a new font entry
134 FontGlyphRecord fontGlyphRecord;
135 fontGlyphRecord.mFontId = glyph.fontId;
136 fontGlyphRecord.mGlyphRecords.PushBack( record );
137 mFontGlyphRecords.push_back( fontGlyphRecord );
141 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
142 const Vector2& position,
143 Toolkit::AtlasManager::Mesh2D& mesh )
145 // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
146 mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
149 void AtlasGlyphManager::StitchMesh( Toolkit::AtlasManager::Mesh2D& first,
150 const Toolkit::AtlasManager::Mesh2D& second )
152 mAtlasManager.StitchMesh( first, second );
155 bool AtlasGlyphManager::Cached( Text::FontId fontId,
156 Text::GlyphIndex index,
157 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
159 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
160 fontGlyphRecordIt != mFontGlyphRecords.end();
161 ++fontGlyphRecordIt )
163 if ( fontGlyphRecordIt->mFontId == fontId )
165 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
166 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
169 if ( glyphRecordIt->mIndex == index )
171 slot.mImageId = glyphRecordIt->mImageId;
172 slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
182 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
184 Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
185 return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
188 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
190 Toolkit::AtlasManager::AtlasSize size;
192 size.mHeight = height;
193 size.mBlockWidth = blockWidth;
194 size.mBlockHeight = blockHeight;
195 mAtlasManager.SetNewAtlasSize( size );
198 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
200 return mAtlasManager.GetPixelFormat( atlasId );
203 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
205 std::ostringstream verboseMetrics;
207 mMetrics.mGlyphCount = 0u;
208 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
209 fontGlyphRecordIt != mFontGlyphRecords.end();
210 ++fontGlyphRecordIt )
212 mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
214 verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
215 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
216 glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
217 ++glyphRecordEntryIt )
219 verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
221 verboseMetrics << "] ";
223 mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
225 mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
230 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta )
234 DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
236 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
237 fontGlyphRecordIt != mFontGlyphRecords.end();
238 ++fontGlyphRecordIt )
240 if ( fontGlyphRecordIt->mFontId == fontId )
242 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
243 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
246 if ( glyphRecordIt->mIndex == index )
248 glyphRecordIt->mCount += delta;
249 DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
251 if ( !glyphRecordIt->mCount )
253 mAtlasManager.Remove( glyphRecordIt->mImageId );
254 fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
262 // Should not arrive here
263 DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
267 Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const
269 return mAtlasManager.GetMaterial( atlasId );
272 Sampler AtlasGlyphManager::GetSampler( uint32_t atlasId ) const
274 return mAtlasManager.GetSampler( atlasId );
277 AtlasGlyphManager::~AtlasGlyphManager()
279 // mAtlasManager handle is automatically released here
282 } // namespace Internal
284 } // namespace Toolkit