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