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