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