Merge "Moves shader creation from Text Atlas Renderer to GlyphManager" into devel...
[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
24 #define MAKE_SHADER(A)#A
25
26 namespace
27 {
28 const char* VERTEX_SHADER = MAKE_SHADER(
29 attribute mediump vec2    aPosition;
30 attribute mediump vec2    aTexCoord;
31 uniform   mediump mat4    uMvpMatrix;
32 uniform   mediump vec3    uSize;
33 varying   mediump vec2    vTexCoord;
34
35 void main()
36 {
37   mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
38   position.xyz *= uSize;
39   gl_Position = uMvpMatrix * position;
40   vTexCoord = aTexCoord;
41 }
42 );
43
44 const char* FRAGMENT_SHADER = MAKE_SHADER(
45 uniform         sampler2D sTexture;
46 varying mediump vec2      vTexCoord;
47
48 void main()
49 {
50   gl_FragColor = texture2D( sTexture, vTexCoord );
51 }
52 );
53
54 const char* VERTEX_SHADER_SHADOW = MAKE_SHADER(
55 attribute mediump vec2    aPosition;
56 attribute mediump vec2    aTexCoord;
57 uniform   mediump vec3    uSize;
58 varying   mediump vec2    vTexCoord;
59
60 void main()
61 {
62   mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
63   position.xyz *= uSize;
64   gl_Position = position;
65   vTexCoord = aTexCoord;
66 }
67 );
68
69 const char* FRAGMENT_SHADER_SHADOW = MAKE_SHADER(
70 uniform         sampler2D sTexture;
71 uniform lowp    vec4      uColor;
72 varying mediump vec2      vTexCoord;
73
74 void main()
75 {
76   mediump vec4 color = texture2D( sTexture, vTexCoord );
77   gl_FragColor = vec4(uColor.rgb, uColor.a*color.r);
78 }
79 );
80 }
81
82 namespace Dali
83 {
84
85 namespace Toolkit
86 {
87
88 namespace Internal
89 {
90
91 AtlasGlyphManager::AtlasGlyphManager()
92 {
93   mAtlasManager = Dali::Toolkit::AtlasManager::New();
94   mEffectBufferShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
95   mShadowShader = Shader::New( VERTEX_SHADER_SHADOW, FRAGMENT_SHADER_SHADOW, Dali::Shader::HINT_MODIFIES_GEOMETRY );
96 }
97
98 AtlasGlyphManager::~AtlasGlyphManager()
99 {
100 }
101
102 AtlasGlyphManagerPtr AtlasGlyphManager::New()
103 {
104   AtlasGlyphManagerPtr internal = new AtlasGlyphManager();
105   return internal;
106 }
107
108 void AtlasGlyphManager::Add( const Text::GlyphInfo& glyph,
109                              const BufferImage& bitmap,
110                              Dali::Toolkit::AtlasManager::AtlasSlot& slot )
111 {
112   GlyphRecord record;
113   record.mFontId = glyph.fontId;
114   record.mIndex = glyph.index;
115
116   mAtlasManager.Add( bitmap, slot );
117   record.mImageId = slot.mImageId;
118   mGlyphRecords.PushBack( record );
119 }
120
121 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
122                                           const Vector2& position,
123                                           Toolkit::AtlasManager::Mesh2D& mesh )
124 {
125   mAtlasManager.GenerateMeshData( imageId, position, mesh );
126 }
127
128 void AtlasGlyphManager::StitchMesh( Toolkit::AtlasManager::Mesh2D& first,
129                                     const Toolkit::AtlasManager::Mesh2D& second )
130 {
131   mAtlasManager.StitchMesh( first, second );
132 }
133
134 void AtlasGlyphManager::Cached( Text::FontId fontId,
135                                 uint32_t index,
136                                 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
137 {
138   for ( uint32_t i = 0; i < mGlyphRecords.Size(); ++i )
139   {
140     if ( fontId == mGlyphRecords[ i ].mFontId && index == mGlyphRecords[ i ].mIndex )
141     {
142       slot.mImageId = mGlyphRecords[ i ].mImageId;
143       slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
144       return;
145     }
146   }
147   slot.mImageId = 0;
148 }
149
150 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
151 {
152   Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
153   return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
154 }
155
156 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
157 {
158   Toolkit::AtlasManager::AtlasSize size;
159   size.mWidth = width;
160   size.mHeight = height;
161   size.mBlockWidth = blockWidth;
162   size.mBlockHeight = blockHeight;
163   mAtlasManager.SetNewAtlasSize( size );
164 }
165
166 void AtlasGlyphManager::Remove( uint32_t imageId )
167 {
168   if ( mAtlasManager.Remove( imageId ) )
169   {
170     for ( uint32_t i = 0; i < mGlyphRecords.Size(); ++i )
171     {
172       if ( mGlyphRecords[ i ].mImageId == imageId )
173       {
174         mGlyphRecords.Remove( mGlyphRecords.Begin() + i );
175         return;
176       }
177     }
178   }
179 }
180
181 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
182 {
183   return mAtlasManager.GetPixelFormat( atlasId );
184 }
185
186 Material AtlasGlyphManager::GetMaterial( uint32_t atlasId ) const
187 {
188   return mAtlasManager.GetMaterial( atlasId );
189 }
190
191 Sampler AtlasGlyphManager::GetSampler( uint32_t atlasId ) const
192 {
193   return mAtlasManager.GetSampler( atlasId );
194 }
195
196 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
197 {
198   mMetrics.mGlyphCount = mGlyphRecords.Size();
199   mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
200   return mMetrics;
201 }
202
203 } // namespace Internal
204
205 } // namespace Toolkit
206
207 } // namespace Dali