8532b93ef1b42ef77bb498f3cc372d33f52e3ed9
[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 PixelData& 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 texture set details for the atlas
100     Dali::Texture atlas = mAtlasManager.GetAtlasContainer( slot.mAtlasId );
101     TextureSet textureSet = TextureSet::New();
102     textureSet.SetTexture( 0u, atlas );
103     mAtlasManager.SetTextures( slot.mAtlasId, textureSet );
104   }
105
106   GlyphRecordEntry record;
107   record.mIndex = glyph.index;
108   record.mImageId = slot.mImageId;
109   record.mCount = 1;
110
111   // Have glyph records been created for this fontId ?
112   bool foundGlyph = false;
113   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
114         fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
115   {
116     if ( fontGlyphRecordIt->mFontId == glyph.fontId )
117     {
118       fontGlyphRecordIt->mGlyphRecords.PushBack( record );
119       foundGlyph = true;
120       break;
121     }
122   }
123
124   if ( !foundGlyph )
125   {
126     // We need to add a new font entry
127     FontGlyphRecord fontGlyphRecord;
128     fontGlyphRecord.mFontId = glyph.fontId;
129     fontGlyphRecord.mGlyphRecords.PushBack( record );
130     mFontGlyphRecords.push_back( fontGlyphRecord );
131   }
132 }
133
134 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
135                                           const Vector2& position,
136                                           Toolkit::AtlasManager::Mesh2D& mesh )
137 {
138   // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
139   mAtlasManager.GenerateMeshData( imageId, position, mesh, false );
140 }
141
142 bool AtlasGlyphManager::IsCached( Text::FontId fontId,
143                                 Text::GlyphIndex index,
144                                 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
145 {
146   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
147         fontGlyphRecordIt != mFontGlyphRecords.end();
148         ++fontGlyphRecordIt )
149   {
150     if ( fontGlyphRecordIt->mFontId == fontId )
151     {
152       for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
153             glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
154             ++glyphRecordIt )
155       {
156         if ( glyphRecordIt->mIndex == index )
157         {
158           slot.mImageId = glyphRecordIt->mImageId;
159           slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
160           return true;
161         }
162       }
163     }
164   }
165   slot.mImageId = 0;
166   return false;
167 }
168
169 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
170 {
171   Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
172   return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
173 }
174
175 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
176 {
177   Toolkit::AtlasManager::AtlasSize size;
178   size.mWidth = width;
179   size.mHeight = height;
180   size.mBlockWidth = blockWidth;
181   size.mBlockHeight = blockHeight;
182   mAtlasManager.SetNewAtlasSize( size );
183 }
184
185 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
186 {
187   return mAtlasManager.GetPixelFormat( atlasId );
188 }
189
190 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
191 {
192   std::ostringstream verboseMetrics;
193
194   mMetrics.mGlyphCount = 0u;
195   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
196         fontGlyphRecordIt != mFontGlyphRecords.end();
197         ++fontGlyphRecordIt )
198   {
199     mMetrics.mGlyphCount += fontGlyphRecordIt->mGlyphRecords.Size();
200
201     verboseMetrics << "[FontId " << fontGlyphRecordIt->mFontId << " Glyph ";
202     for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
203           glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
204           ++glyphRecordEntryIt )
205     {
206       verboseMetrics << glyphRecordEntryIt->mIndex << "(" << glyphRecordEntryIt->mCount << ") ";
207     }
208     verboseMetrics << "] ";
209   }
210   mMetrics.mVerboseGlyphCounts = verboseMetrics.str();
211
212   mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
213
214   return mMetrics;
215 }
216
217 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, Text::GlyphIndex index, int32_t delta )
218 {
219   if( 0 != delta )
220   {
221     DALI_LOG_INFO( gLogFilter, Debug::General, "AdjustReferenceCount %d, font: %d index: %d\n", delta, fontId, index );
222
223     for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
224           fontGlyphRecordIt != mFontGlyphRecords.end();
225           ++fontGlyphRecordIt )
226     {
227       if ( fontGlyphRecordIt->mFontId == fontId )
228       {
229         for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
230               glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
231               ++glyphRecordIt )
232         {
233           if ( glyphRecordIt->mIndex == index )
234           {
235             glyphRecordIt->mCount += delta;
236             DALI_ASSERT_DEBUG( glyphRecordIt->mCount >= 0 && "Glyph ref-count should not be negative" );
237
238             if ( !glyphRecordIt->mCount )
239             {
240               mAtlasManager.Remove( glyphRecordIt->mImageId );
241               fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
242             }
243             return;
244           }
245         }
246       }
247     }
248
249     // Should not arrive here
250     DALI_ASSERT_DEBUG( false && "Failed to adjust ref-count" );
251   }
252 }
253
254 TextureSet AtlasGlyphManager::GetTextures( uint32_t atlasId ) const
255 {
256   return mAtlasManager.GetTextures( atlasId );
257 }
258
259 Shader AtlasGlyphManager::GetShader( uint32_t atlasId ) const
260 {
261   Pixel::Format pixelFormat = mAtlasManager.GetPixelFormat( atlasId );
262   return pixelFormat == Pixel::L8 ? mShaderL8 : mShaderRgba;
263 }
264
265 AtlasGlyphManager::~AtlasGlyphManager()
266 {
267   // mAtlasManager handle is automatically released here
268 }
269
270 } // namespace Internal
271
272 } // namespace Toolkit
273
274 } // namespace Dali