Simplified the glyph reference counting
[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/public-api/actors/image-actor.h>
22 #include <dali/public-api/common/stage.h>
23 #include <dali/integration-api/debug.h>
24
25 #define MAKE_SHADER(A)#A
26
27 namespace
28 {
29
30 #if defined(DEBUG_ENABLED)
31   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_RENDERING");
32 #endif
33
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;
40
41 void main()
42 {
43   mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
44   position.xyz *= uSize;
45   gl_Position = uMvpMatrix * position;
46   vTexCoord = aTexCoord;
47 }
48 );
49
50 const char* FRAGMENT_SHADER = MAKE_SHADER(
51 uniform         sampler2D sTexture;
52 varying mediump vec2      vTexCoord;
53
54 void main()
55 {
56   gl_FragColor = texture2D( sTexture, vTexCoord );
57 }
58 );
59
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;
65
66 void main()
67 {
68   mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
69   position.xyz *= uSize;
70   gl_Position = position;
71   vTexCoord = aTexCoord;
72 }
73 );
74
75 const char* FRAGMENT_SHADER_SHADOW = MAKE_SHADER(
76 uniform         sampler2D sTexture;
77 uniform lowp    vec4      uColor;
78 varying mediump vec2      vTexCoord;
79
80 void main()
81 {
82   mediump vec4 color = texture2D( sTexture, vTexCoord );
83   gl_FragColor = vec4(uColor.rgb, uColor.a*color.r);
84 }
85 );
86
87 } // unnamed namespace
88
89 namespace Dali
90 {
91
92 namespace Toolkit
93 {
94
95 namespace Internal
96 {
97
98 AtlasGlyphManager::AtlasGlyphManager()
99 {
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 );
103 }
104
105 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
106                              const BufferImage& bitmap,
107                              Dali::Toolkit::AtlasManager::AtlasSlot& slot )
108 {
109   DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index );
110
111   mAtlasManager.Add( bitmap, slot );
112
113   GlyphRecordEntry record;
114   record.mIndex = glyph.index;
115   record.mImageId = slot.mImageId;
116   record.mCount = 1;
117
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 )
122   {
123     if ( fontGlyphRecordIt->mFontId == glyph.fontId )
124     {
125       fontGlyphRecordIt->mGlyphRecords.PushBack( record );
126       foundGlyph = true;
127       break;
128     }
129   }
130
131   if ( !foundGlyph )
132   {
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 );
138   }
139 }
140
141 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
142                                           const Vector2& position,
143                                           Toolkit::AtlasManager::Mesh2D& mesh )
144 {
145   // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
146   mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
147 }
148
149 void AtlasGlyphManager::StitchMesh( Toolkit::AtlasManager::Mesh2D& first,
150                                     const Toolkit::AtlasManager::Mesh2D& second )
151 {
152   mAtlasManager.StitchMesh( first, second );
153 }
154
155 bool AtlasGlyphManager::Cached( Text::FontId fontId,
156                                 Text::GlyphIndex index,
157                                 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
158 {
159   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
160         fontGlyphRecordIt != mFontGlyphRecords.end();
161         ++fontGlyphRecordIt )
162   {
163     if ( fontGlyphRecordIt->mFontId == fontId )
164     {
165       for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
166             glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
167             ++glyphRecordIt )
168       {
169         if ( glyphRecordIt->mIndex == index )
170         {
171           slot.mImageId = glyphRecordIt->mImageId;
172           slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
173           return true;
174         }
175       }
176     }
177   }
178   slot.mImageId = 0;
179   return false;
180 }
181
182 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
183 {
184   Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
185   return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
186 }
187
188 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
189 {
190   Toolkit::AtlasManager::AtlasSize size;
191   size.mWidth = width;
192   size.mHeight = height;
193   size.mBlockWidth = blockWidth;
194   size.mBlockHeight = blockHeight;
195   mAtlasManager.SetNewAtlasSize( size );
196 }
197
198 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
199 {
200   return mAtlasManager.GetPixelFormat( atlasId );
201 }
202
203 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
204 {
205   std::ostringstream verboseMetrics;
206
207   mMetrics.mGlyphCount = 0u;
208   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
209         fontGlyphRecordIt != mFontGlyphRecords.end();
210         ++fontGlyphRecordIt )
211   {
212     mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
213
214     verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
215     for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
216           glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
217           ++glyphRecordEntryIt )
218     {
219       verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
220     }
221     verboseMetrics << "] ";
222   }
223   mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
224
225   mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
226
227   return mMetrics;
228 }
229
230 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta )
231 {
232   if( 0 != delta )
233   {
234     DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
235
236     for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
237           fontGlyphRecordIt != mFontGlyphRecords.end();
238           ++fontGlyphRecordIt )
239     {
240       if ( fontGlyphRecordIt->mFontId == fontId )
241       {
242         for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
243               glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
244               ++glyphRecordIt )
245         {
246           if ( glyphRecordIt->mIndex == index )
247           {
248             glyphRecordIt->mCount += delta;
249             DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
250
251             if ( !glyphRecordIt->mCount )
252             {
253               mAtlasManager.Remove( glyphRecordIt->mImageId );
254               fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
255             }
256             return;
257           }
258         }
259       }
260     }
261
262     // Should not arrive here
263     DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
264   }
265 }
266
267 Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const
268 {
269   return mAtlasManager.GetMaterial( atlasId );
270 }
271
272 Sampler AtlasGlyphManager::GetSampler( uint32_t atlasId ) const
273 {
274   return mAtlasManager.GetSampler( atlasId );
275 }
276
277 AtlasGlyphManager::~AtlasGlyphManager()
278 {
279   // mAtlasManager handle is automatically released here
280 }
281
282 } // namespace Internal
283
284 } // namespace Toolkit
285
286 } // namespace Dali