Add codes to prevent underflow
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / atlas / atlas-glyph-manager-impl.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // CLASS HEADER
18 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager-impl.h>
19
20 // EXTERNAL INCLUDES
21 #include <dali/integration-api/debug.h>
22
23 namespace
24 {
25
26 #if defined(DEBUG_ENABLED)
27   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_RENDERING");
28 #endif
29
30 } // unnamed namespace
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 AtlasGlyphManager::AtlasGlyphManager()
42 {
43   mAtlasManager = Dali::Toolkit::AtlasManager::New();
44 }
45
46 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
47                              const PixelData& bitmap,
48                              Dali::Toolkit::AtlasManager::AtlasSlot& slot )
49 {
50   DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index );
51
52   // If glyph added to an existing or new atlas then a new glyph record is required.
53   // Check if an existing atlas will fit the image, create a new one if required.
54   if ( mAtlasManager.Add( bitmap, slot ) )
55   {
56     // A new atlas was created so set the texture set details for the atlas
57     Dali::Texture atlas = mAtlasManager.GetAtlasContainer( slot.mAtlasId );
58     TextureSet textureSet = TextureSet::New();
59     textureSet.SetTexture( 0u, atlas );
60     mAtlasManager.SetTextures( slot.mAtlasId, textureSet );
61   }
62
63   GlyphRecordEntry record;
64   record.mIndex = glyph.index;
65   record.mImageId = slot.mImageId;
66   record.mCount = 1;
67
68   // Have glyph records been created for this fontId ?
69   bool foundGlyph = false;
70   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
71         fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
72   {
73     if ( fontGlyphRecordIt->mFontId == glyph.fontId )
74     {
75       fontGlyphRecordIt->mGlyphRecords.PushBack( record );
76       foundGlyph = true;
77       break;
78     }
79   }
80
81   if ( !foundGlyph )
82   {
83     // We need to add a new font entry
84     FontGlyphRecord fontGlyphRecord;
85     fontGlyphRecord.mFontId = glyph.fontId;
86     fontGlyphRecord.mGlyphRecords.PushBack( record );
87     mFontGlyphRecords.push_back( fontGlyphRecord );
88   }
89 }
90
91 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
92                                           const Vector2& position,
93                                           Toolkit::AtlasManager::Mesh2D& mesh )
94 {
95   // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
96   mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
97 }
98
99 bool AtlasGlyphManager::IsCached( Text::FontId fontId,
100                                 Text::GlyphIndex index,
101                                 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
102 {
103   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
104         fontGlyphRecordIt != mFontGlyphRecords.end();
105         ++fontGlyphRecordIt )
106   {
107     if ( fontGlyphRecordIt->mFontId == fontId )
108     {
109       for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
110             glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
111             ++glyphRecordIt )
112       {
113         if ( glyphRecordIt->mIndex == index )
114         {
115           slot.mImageId = glyphRecordIt->mImageId;
116           slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
117           return true;
118         }
119       }
120     }
121   }
122   slot.mImageId = 0;
123   return false;
124 }
125
126 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
127 {
128   Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
129   return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
130 }
131
132 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
133 {
134   Toolkit::AtlasManager::AtlasSize size;
135   size.mWidth = width;
136   size.mHeight = height;
137   size.mBlockWidth = blockWidth;
138   size.mBlockHeight = blockHeight;
139   mAtlasManager.SetNewAtlasSize( size );
140 }
141
142 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
143 {
144   return mAtlasManager.GetPixelFormat( atlasId );
145 }
146
147 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
148 {
149   std::ostringstream verboseMetrics;
150
151   mMetrics.mGlyphCount = 0u;
152   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
153         fontGlyphRecordIt != mFontGlyphRecords.end();
154         ++fontGlyphRecordIt )
155   {
156     mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
157
158     verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
159     for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
160           glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
161           ++glyphRecordEntryIt )
162     {
163       verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
164     }
165     verboseMetrics << "] ";
166   }
167   mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
168
169   mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
170
171   return mMetrics;
172 }
173
174 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta )
175 {
176   if( 0 != delta )
177   {
178     DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
179
180     for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
181           fontGlyphRecordIt != mFontGlyphRecords.end();
182           ++fontGlyphRecordIt )
183     {
184       if ( fontGlyphRecordIt->mFontId == fontId )
185       {
186         for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
187               glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
188               ++glyphRecordIt )
189         {
190           if ( glyphRecordIt->mIndex == index )
191           {
192             glyphRecordIt->mCount += delta;
193             DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
194
195             if ( !glyphRecordIt->mCount )
196             {
197               mAtlasManager.Remove( glyphRecordIt->mImageId );
198               fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
199             }
200             return;
201           }
202         }
203       }
204     }
205
206     // Should not arrive here
207     DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
208   }
209 }
210
211 TextureSet AtlasGlyphManager::GetTextures( uint32_t atlasId ) const
212 {
213   return mAtlasManager.GetTextures( atlasId );
214 }
215
216 AtlasGlyphManager::~AtlasGlyphManager()
217 {
218   // mAtlasManager handle is automatically released here
219 }
220
221 } // namespace Internal
222
223 } // namespace Toolkit
224
225 } // namespace Dali