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 } // unnamed namespace
41 AtlasGlyphManager::AtlasGlyphManager()
43 mAtlasManager = Dali::Toolkit::AtlasManager::New();
46 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
47 const uint32_t outlineWidth,
48 const PixelData& bitmap,
49 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
51 DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index );
53 // If glyph added to an existing or new atlas then a new glyph record is required.
54 // Check if an existing atlas will fit the image, create a new one if required.
55 if ( mAtlasManager.Add( bitmap, slot ) )
57 // A new atlas was created so set the texture set details for the atlas
58 Dali::Texture atlas = mAtlasManager.GetAtlasContainer( slot.mAtlasId );
59 TextureSet textureSet = TextureSet::New();
60 textureSet.SetTexture( 0u, atlas );
61 mAtlasManager.SetTextures( slot.mAtlasId, textureSet );
64 GlyphRecordEntry record;
65 record.mIndex = glyph.index;
66 record.mOutlineWidth = outlineWidth;
67 record.mImageId = slot.mImageId;
70 // Have glyph records been created for this fontId ?
71 bool foundGlyph = false;
72 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
73 fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
75 if ( fontGlyphRecordIt->mFontId == glyph.fontId )
77 fontGlyphRecordIt->mGlyphRecords.PushBack( record );
85 // We need to add a new font entry
86 FontGlyphRecord fontGlyphRecord;
87 fontGlyphRecord.mFontId = glyph.fontId;
88 fontGlyphRecord.mGlyphRecords.PushBack( record );
89 mFontGlyphRecords.push_back( fontGlyphRecord );
93 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
94 const Vector2& position,
95 Toolkit::AtlasManager::Mesh2D& mesh )
97 // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
98 mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
101 bool AtlasGlyphManager::IsCached( Text::FontId fontId,
102 Text::GlyphIndex index,
103 uint32_t outlineWidth,
104 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
106 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
107 fontGlyphRecordIt != mFontGlyphRecords.end();
108 ++fontGlyphRecordIt )
110 if ( fontGlyphRecordIt->mFontId == fontId )
112 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
113 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
116 if ( glyphRecordIt->mIndex == index && glyphRecordIt->mOutlineWidth == outlineWidth )
118 slot.mImageId = glyphRecordIt->mImageId;
119 slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
129 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
131 Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
132 return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
135 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
137 Toolkit::AtlasManager::AtlasSize size;
139 size.mHeight = height;
140 size.mBlockWidth = blockWidth;
141 size.mBlockHeight = blockHeight;
142 mAtlasManager.SetNewAtlasSize( size );
145 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
147 return mAtlasManager.GetPixelFormat( atlasId );
150 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
152 std::ostringstream verboseMetrics;
154 mMetrics.mGlyphCount = 0u;
155 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
156 fontGlyphRecordIt != mFontGlyphRecords.end();
157 ++fontGlyphRecordIt )
159 mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
161 verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
162 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
163 glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
164 ++glyphRecordEntryIt )
166 verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
168 verboseMetrics << "] ";
170 mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
172 mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
177 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, uint32_t outlineWidth, int32_t delta )
181 DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
183 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
184 fontGlyphRecordIt != mFontGlyphRecords.end();
185 ++fontGlyphRecordIt )
187 if ( fontGlyphRecordIt->mFontId == fontId )
189 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
190 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
193 if ( glyphRecordIt->mIndex == index && glyphRecordIt->mOutlineWidth == outlineWidth )
195 glyphRecordIt->mCount += delta;
196 DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
198 if ( !glyphRecordIt->mCount )
200 mAtlasManager.Remove( glyphRecordIt->mImageId );
201 fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
209 // Should not arrive here
210 DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
214 TextureSet AtlasGlyphManager::GetTextures( uint32_t atlasId ) const
216 return mAtlasManager.GetTextures( atlasId );
219 AtlasGlyphManager::~AtlasGlyphManager()
221 // mAtlasManager handle is automatically released here
224 } // namespace Internal
226 } // namespace Toolkit