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 PixelData& bitmap,
48 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
50 DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index );
52 if ( mAtlasManager.Add( bitmap, slot ) )
54 // A new atlas was created so set the texture set details for the atlas
55 Dali::Texture atlas = mAtlasManager.GetAtlasContainer( slot.mAtlasId );
56 TextureSet textureSet = TextureSet::New();
57 textureSet.SetTexture( 0u, atlas );
58 mAtlasManager.SetTextures( slot.mAtlasId, textureSet );
61 GlyphRecordEntry record;
62 record.mIndex = glyph.index;
63 record.mImageId = slot.mImageId;
66 // Have glyph records been created for this fontId ?
67 bool foundGlyph = false;
68 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
69 fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
71 if ( fontGlyphRecordIt->mFontId == glyph.fontId )
73 fontGlyphRecordIt->mGlyphRecords.PushBack( record );
81 // We need to add a new font entry
82 FontGlyphRecord fontGlyphRecord;
83 fontGlyphRecord.mFontId = glyph.fontId;
84 fontGlyphRecord.mGlyphRecords.PushBack( record );
85 mFontGlyphRecords.push_back( fontGlyphRecord );
89 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
90 const Vector2& position,
91 Toolkit::AtlasManager::Mesh2D& mesh )
93 // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
94 mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
97 bool AtlasGlyphManager::IsCached( Text::FontId fontId,
98 Text::GlyphIndex index,
99 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
101 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
102 fontGlyphRecordIt != mFontGlyphRecords.end();
103 ++fontGlyphRecordIt )
105 if ( fontGlyphRecordIt->mFontId == fontId )
107 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
108 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
111 if ( glyphRecordIt->mIndex == index )
113 slot.mImageId = glyphRecordIt->mImageId;
114 slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
124 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
126 Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
127 return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
130 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
132 Toolkit::AtlasManager::AtlasSize size;
134 size.mHeight = height;
135 size.mBlockWidth = blockWidth;
136 size.mBlockHeight = blockHeight;
137 mAtlasManager.SetNewAtlasSize( size );
140 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
142 return mAtlasManager.GetPixelFormat( atlasId );
145 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
147 std::ostringstream verboseMetrics;
149 mMetrics.mGlyphCount = 0u;
150 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
151 fontGlyphRecordIt != mFontGlyphRecords.end();
152 ++fontGlyphRecordIt )
154 mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
156 verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
157 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
158 glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
159 ++glyphRecordEntryIt )
161 verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
163 verboseMetrics << "] ";
165 mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
167 mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
172 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta )
176 DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
178 for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
179 fontGlyphRecordIt != mFontGlyphRecords.end();
180 ++fontGlyphRecordIt )
182 if ( fontGlyphRecordIt->mFontId == fontId )
184 for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
185 glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
188 if ( glyphRecordIt->mIndex == index )
190 glyphRecordIt->mCount += delta;
191 DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
193 if ( !glyphRecordIt->mCount )
195 mAtlasManager.Remove( glyphRecordIt->mImageId );
196 fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
204 // Should not arrive here
205 DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
209 TextureSet AtlasGlyphManager::GetTextures( uint32_t atlasId ) const
211 return mAtlasManager.GetTextures( atlasId );
214 AtlasGlyphManager::~AtlasGlyphManager()
216 // mAtlasManager handle is automatically released here
219 } // namespace Internal
221 } // namespace Toolkit