f2c328e51c8576bd5e3c4333ee67e17ee3f1b2fb
[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 #define MAKE_SHADER(A)#A
31
32 const char* VERTEX_SHADER = MAKE_SHADER(
33 attribute mediump vec2    aPosition;
34 attribute mediump vec2    aTexCoord;
35 uniform   mediump vec2    uOffset;
36 uniform   mediump mat4    uMvpMatrix;
37 varying   mediump vec2    vTexCoord;
38
39 void main()
40 {
41   mediump vec4 position = vec4( aPosition.xy + uOffset, 0.0, 1.0 );
42   gl_Position = uMvpMatrix * position;
43   vTexCoord = aTexCoord;
44 }
45 );
46
47 const char* FRAGMENT_SHADER_L8 = MAKE_SHADER(
48 uniform lowp    vec4      uColor;
49 uniform         sampler2D sTexture;
50 varying mediump vec2      vTexCoord;
51
52 void main()
53 {
54   mediump vec4 color = texture2D( sTexture, vTexCoord );
55   gl_FragColor = vec4( uColor.rgb, uColor.a * color.r );
56 }
57 );
58
59 const char* FRAGMENT_SHADER_RGBA = MAKE_SHADER(
60 uniform         sampler2D sTexture;
61 varying mediump vec2      vTexCoord;
62
63 void main()
64 {
65   gl_FragColor = texture2D( sTexture, vTexCoord );
66 }
67 );
68
69 } // unnamed namespace
70
71 namespace Dali
72 {
73
74 namespace Toolkit
75 {
76
77 namespace Internal
78 {
79
80 AtlasGlyphManager::AtlasGlyphManager()
81 {
82   mShaderL8 = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_L8 );
83   mShaderRgba = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_RGBA );
84   mAtlasManager = Dali::Toolkit::AtlasManager::New();
85 }
86
87 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
88                              const BufferImage& bitmap,
89                              Dali::Toolkit::AtlasManager::AtlasSlot& slot )
90 {
91   DALI_LOG_INFO( gLogFilter, Debug::General, "Added glyph, font: %d index: %d\n", glyph.fontId, glyph.index );
92
93   if ( mAtlasManager.Add( bitmap, slot ) )
94   {
95     // A new atlas was created so set the material details for the atlas
96     Dali::Atlas atlas = mAtlasManager.GetAtlasContainer( slot.mAtlasId );
97     Pixel::Format pixelFormat = mAtlasManager.GetPixelFormat( slot.mAtlasId );
98     Material material = Material::New( pixelFormat == Pixel::L8 ? mShaderL8 : mShaderRgba );
99     material.AddTexture( atlas, "sTexture" );
100     material.SetBlendMode( BlendingMode::ON );
101     mAtlasManager.SetMaterial( slot.mAtlasId, material );
102   }
103
104   GlyphRecordEntry record;
105   record.mIndex = glyph.index;
106   record.mImageId = slot.mImageId;
107   record.mCount = 1;
108
109   // Have glyph records been created for this fontId ?
110   bool foundGlyph = false;
111   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
112         fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
113   {
114     if ( fontGlyphRecordIt->mFontId == glyph.fontId )
115     {
116       fontGlyphRecordIt->mGlyphRecords.PushBack( record );
117       foundGlyph = true;
118       break;
119     }
120   }
121
122   if ( !foundGlyph )
123   {
124     // We need to add a new font entry
125     FontGlyphRecord fontGlyphRecord;
126     fontGlyphRecord.mFontId = glyph.fontId;
127     fontGlyphRecord.mGlyphRecords.PushBack( record );
128     mFontGlyphRecords.push_back( fontGlyphRecord );
129   }
130 }
131
132 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
133                                           const Vector2& position,
134                                           Toolkit::AtlasManager::Mesh2D& mesh )
135 {
136   // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
137   mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
138 }
139
140 bool AtlasGlyphManager::IsCached( Text::FontId fontId,
141                                 Text::GlyphIndex index,
142                                 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
143 {
144   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
145         fontGlyphRecordIt != mFontGlyphRecords.end();
146         ++fontGlyphRecordIt )
147   {
148     if ( fontGlyphRecordIt->mFontId == fontId )
149     {
150       for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
151             glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
152             ++glyphRecordIt )
153       {
154         if ( glyphRecordIt->mIndex == index )
155         {
156           slot.mImageId = glyphRecordIt->mImageId;
157           slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
158           return true;
159         }
160       }
161     }
162   }
163   slot.mImageId = 0;
164   return false;
165 }
166
167 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
168 {
169   Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
170   return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
171 }
172
173 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
174 {
175   Toolkit::AtlasManager::AtlasSize size;
176   size.mWidth = width;
177   size.mHeight = height;
178   size.mBlockWidth = blockWidth;
179   size.mBlockHeight = blockHeight;
180   mAtlasManager.SetNewAtlasSize( size );
181 }
182
183 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
184 {
185   return mAtlasManager.GetPixelFormat( atlasId );
186 }
187
188 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
189 {
190   std::ostringstream verboseMetrics;
191
192   mMetrics.mGlyphCount = 0u;
193   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
194         fontGlyphRecordIt != mFontGlyphRecords.end();
195         ++fontGlyphRecordIt )
196   {
197     mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
198
199     verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
200     for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
201           glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
202           ++glyphRecordEntryIt )
203     {
204       verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
205     }
206     verboseMetrics << "] ";
207   }
208   mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
209
210   mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
211
212   return mMetrics;
213 }
214
215 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta )
216 {
217   if( 0 != delta )
218   {
219     DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
220
221     for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
222           fontGlyphRecordIt != mFontGlyphRecords.end();
223           ++fontGlyphRecordIt )
224     {
225       if ( fontGlyphRecordIt->mFontId == fontId )
226       {
227         for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
228               glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
229               ++glyphRecordIt )
230         {
231           if ( glyphRecordIt->mIndex == index )
232           {
233             glyphRecordIt->mCount += delta;
234             DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
235
236             if ( !glyphRecordIt->mCount )
237             {
238               mAtlasManager.Remove( glyphRecordIt->mImageId );
239               fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
240             }
241             return;
242           }
243         }
244       }
245     }
246
247     // Should not arrive here
248     DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
249   }
250 }
251
252 Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const
253 {
254   return mAtlasManager.GetMaterial( atlasId );
255 }
256
257 AtlasGlyphManager::~AtlasGlyphManager()
258 {
259   // mAtlasManager handle is automatically released here
260 }
261
262 } // namespace Internal
263
264 } // namespace Toolkit
265
266 } // namespace Dali