TextModel - Update the color runs.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / vector-based / glyphy-shader / glyphy-shader.cpp
1 /*
2  * Copyright (c) 2016 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
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/rendering/vector-based/glyphy-shader/glyphy-shader.h>
20
21 // EXTERNAL HEADERS
22 #include <sstream>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/text/rendering/vector-based/glyphy-shader/glyphy-common-glsl.h>
26 #include <dali-toolkit/internal/text/rendering/vector-based/glyphy-shader/glyphy-sdf-glsl.h>
27
28 using namespace Dali;
29
30 namespace
31 {
32
33 const char* const ENABLE_EXTENSION_PREFIX =
34 "#extension GL_OES_standard_derivatives : enable\n"
35 "precision highp float;\n"
36 "precision highp int;\n";
37
38 const char* const VERTEX_SHADER_MAIN =
39 "uniform   mediump mat4    uProjection;\n"
40 "uniform   mediump mat4    uModelView;\n"
41 "uniform   mediump mat4    uMvpMatrix;\n"
42 "uniform           bool    uTextureMapped;\n"
43 "uniform   mediump vec4    uCustomTextureCoords;\n"
44 "attribute highp   vec2    aTexCoord;\n"
45 "varying   mediump vec2    vTexCoord;\n"
46 "uniform   mat3            uModelViewIT;\n"
47 "attribute mediump vec3    aNormal;\n"
48 "varying   mediump vec3    vNormal;\n"
49 "attribute mediump vec2    aPosition;\n"
50 "varying   mediump vec4    vVertex;\n"
51 "attribute mediump vec4    aColor;\n"
52 "varying   mediump vec4    vColor;\n"
53 "varying vec4 v_glyph;\n"
54 "\n"
55 "vec4\n"
56 "glyph_vertex_transcode (vec2 v)\n"
57 "{\n"
58 "  ivec2 g = ivec2 (v);\n"
59 "  ivec2 corner = ivec2 (mod (v, 2.));\n"
60 "  g /= 2;\n"
61 "  ivec2 nominal_size = ivec2 (mod (vec2(g), 64.));\n"
62 "  return vec4 (corner * nominal_size, g * 4);\n"
63 "}\n"
64 "\n"
65 "void\n"
66 "main()\n"
67 "{\n"
68 "  gl_Position = uMvpMatrix * vec4 (aPosition, 0.0, 1.0);\n"
69 "  v_glyph = glyph_vertex_transcode (aTexCoord);\n"
70 "  vColor = aColor;\n"
71 "}\n"
72 ;
73
74 const char* const FRAGMENT_SHADER_PREFIX =
75 "struct Material\n"
76 "{\n"
77 "  mediump float mOpacity;\n"
78 "  mediump float mShininess;\n"
79 "  lowp    vec4  mAmbient;\n"
80 "  lowp    vec4  mDiffuse;\n"
81 "  lowp    vec4  mSpecular;\n"
82 "  lowp    vec4  mEmissive;\n"
83 "};\n"
84 "uniform sampler2D     sTexture;\n"
85 "uniform sampler2D     sOpacityTexture;\n"
86 "uniform sampler2D     sNormalMapTexture;\n"
87 "uniform sampler2D     sEffect;\n"
88 "varying mediump vec2 vTexCoord;\n"
89 "uniform Material      uMaterial;\n"
90 "uniform lowp  vec4    uColor;\n"
91 "varying highp vec4    vVertex;\n"
92 "varying highp vec3    vNormal;\n"
93 "varying mediump vec4  vColor;\n"
94 "uniform vec4 u_atlas_info;\n"
95 "\n"
96 "#define GLYPHY_TEXTURE1D_EXTRA_DECLS , sampler2D _tex, ivec4 _atlas_info, ivec2 _atlas_pos\n"
97 "#define GLYPHY_TEXTURE1D_EXTRA_ARGS , _tex, _atlas_info, _atlas_pos\n"
98 "#define GLYPHY_DEMO_EXTRA_ARGS , sTexture, uu_atlas_info, gi.atlas_pos\n"
99 "\n"
100 "vec4\n"
101 "glyphy_texture1D_func (int offset GLYPHY_TEXTURE1D_EXTRA_DECLS)\n"
102 "{\n"
103 "  ivec2 item_geom = _atlas_info.zw;\n"
104 "  vec2 pos = (vec2 (_atlas_pos.xy * item_geom +\n"
105 "                    ivec2 (mod (float (offset), float (item_geom.x)), offset / item_geom.x)) +\n"
106 "             + vec2 (.5, .5)) / vec2(_atlas_info.xy);\n"
107 "  return texture2D (_tex, pos);\n"
108 "}\n"
109 ;
110
111 static const char* FRAGMENT_SHADER_MAIN =
112 "uniform float u_contrast;\n"
113 "uniform float u_gamma_adjust;\n"
114 "uniform float u_outline_thickness;\n"
115 "uniform float u_outline;\n"
116 "uniform float u_boldness;\n"
117 "\n"
118 "varying vec4 v_glyph;\n"
119 "\n"
120 "\n"
121 "#define SQRT2_2 0.70711 /* 1 / sqrt(2.) */\n"
122 "#define SQRT2   1.41421\n"
123 "\n"
124 "struct glyph_info_t {\n"
125 "  ivec2 nominal_size;\n"
126 "  ivec2 atlas_pos;\n"
127 "};\n"
128 "\n"
129 "glyph_info_t\n"
130 "glyph_info_decode (vec4 v)\n"
131 "{\n"
132 "  glyph_info_t gi;\n"
133 "  gi.nominal_size = (ivec2 (mod (v.zw, 256.)) + 2) / 4;\n"
134 "  gi.atlas_pos = ivec2 (v_glyph.zw) / 256;\n"
135 "  return gi;\n"
136 "}\n"
137 "\n"
138 "\n"
139 "float\n"
140 "antialias (float d)\n"
141 "{\n"
142 "  return smoothstep (-.75, +.75, d);\n"
143 "}\n"
144 "\n"
145 "vec4\n"
146 "source_over (const vec4 src, const vec4 dst)\n"
147 "{\n"
148 "  // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators_srcover\n"
149 "  float alpha = src.a + (dst.a * (1. - src.a));\n"
150 "  return vec4 (((src.rgb * src.a) + (dst.rgb * dst.a * (1. - src.a))) / alpha, alpha);\n"
151 "}\n"
152 "\n"
153 "void\n"
154 "main()\n"
155 "{\n"
156 "  vec2 p = v_glyph.xy;\n"
157 "  glyph_info_t gi = glyph_info_decode (v_glyph);\n"
158 "\n"
159 "  /* isotropic antialiasing */\n"
160 "  vec2 dpdx = dFdx (p);\n"
161 "  vec2 dpdy = dFdy (p);\n"
162 "  float m = length (vec2 (length (dpdx), length (dpdy))) * SQRT2_2;\n"
163 "\n"
164 "  vec4 color = vec4( vColor.rgb * uColor.rgb, vColor.a * uColor.a );\n"
165 "\n"
166 "  ivec4 uu_atlas_info = ivec4( u_atlas_info );"
167 "  float gsdist = glyphy_sdf (p, gi.nominal_size GLYPHY_DEMO_EXTRA_ARGS);\n"
168 "  float sdist = gsdist / m * u_contrast;\n"
169 "\n"
170 "    sdist -= u_boldness * 10.;\n"
171 "    if ( glyphy_iszero( u_outline ) )\n"
172 "      sdist = abs (sdist) - u_outline_thickness * .5;\n"
173 "    if (sdist > 1.)\n"
174 "      discard;\n"
175 "    float alpha = antialias (-sdist);\n"
176 "    if (u_gamma_adjust != 1.)\n"
177 "      alpha = pow (alpha, 1./u_gamma_adjust);\n"
178 "    color = vec4 (color.rgb,color.a * alpha);\n"
179 "\n"
180 "  gl_FragColor = color;\n"
181 "}\n"
182 ;
183
184 } // namespace
185
186 namespace Dali
187 {
188
189 namespace Toolkit
190 {
191
192 namespace Text
193 {
194
195 GlyphyShader::GlyphyShader()
196 {
197 }
198
199 GlyphyShader::GlyphyShader( Shader handle )
200 : Shader( handle )
201 {
202 }
203
204 GlyphyShader::~GlyphyShader()
205 {
206 }
207
208 GlyphyShader GlyphyShader::New( const Dali::Vector4& atlasInfo )
209 {
210   std::ostringstream vertexShaderStringStream;
211   std::ostringstream fragmentShaderStringStream;
212
213   vertexShaderStringStream << ENABLE_EXTENSION_PREFIX << VERTEX_SHADER_MAIN;
214
215   fragmentShaderStringStream << ENABLE_EXTENSION_PREFIX
216                              << FRAGMENT_SHADER_PREFIX
217                              << glyphy_common_glsl
218                              << "#define GLYPHY_SDF_PSEUDO_DISTANCE 1\n"
219                              << glyphy_sdf_glsl
220                              << FRAGMENT_SHADER_MAIN;
221
222   Shader shaderEffectCustom = Shader::New( vertexShaderStringStream.str(),
223                                            fragmentShaderStringStream.str(),
224                                            Shader::ShaderHints( Shader::HINT_OUTPUT_IS_TRANSPARENT ) );
225
226   GlyphyShader handle( shaderEffectCustom );
227
228   handle.RegisterProperty( "u_atlas_info",  atlasInfo );
229   handle.RegisterProperty( "u_contrast",          1.f );
230   handle.RegisterProperty( "u_gamma_adjust",      1.f );
231   handle.RegisterProperty( "u_outline_thickness", 1.f );
232   handle.RegisterProperty( "u_outline",           1.f );
233   handle.RegisterProperty( "u_boldness",          0.f );
234
235   return handle;
236 }
237
238 } // namespace Text
239
240 } // namespace Toolkit
241
242 } // namespace Dali