Merge "Add WebView SetTtsFocus" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / color-visual-shader.frag
1 #ifndef IS_REQUIRED_ROUNDED_CORNER
2 #define IS_REQUIRED_ROUNDED_CORNER 0
3 #endif
4 #ifndef IS_REQUIRED_BORDERLINE
5 #define IS_REQUIRED_BORDERLINE 0
6 #endif
7 #ifndef IS_REQUIRED_BLUR
8 #define IS_REQUIRED_BLUR 0
9 #endif
10
11 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE || IS_REQUIRED_BLUR
12 INPUT mediump vec2 vPosition;
13 INPUT mediump vec2 vRectSize;
14 INPUT mediump vec2 vOptRectSize;
15 #if IS_REQUIRED_ROUNDED_CORNER
16 INPUT mediump vec4 vCornerRadius;
17 #endif
18 #endif
19
20 uniform lowp vec4 uColor;
21 uniform lowp vec3 mixColor;
22 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
23 uniform mediump float borderlineWidth;
24 uniform mediump float borderlineOffset;
25 uniform lowp vec4 borderlineColor;
26 #endif
27 #if IS_REQUIRED_BLUR
28 uniform mediump float blurRadius;
29 #endif
30
31
32 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE || IS_REQUIRED_BLUR
33 // Global values both rounded corner and borderline use
34
35 // radius of rounded corner on this quadrant
36 mediump float gRadius = 0.0;
37
38 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
39 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
40 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
41 mediump float gCenterPosition = 0.0;
42 // relative coordinate of gFragmentPosition from gCenterPosition.
43 mediump vec2 gDiff = vec2(0.0, 0.0);
44 // potential value what our algorithm use.
45 mediump float gPotential = 0.0;
46
47 // threshold of potential
48 mediump float gPotentialRange = 0.0;
49 mediump float gMaxOutlinePotential = 0.0;
50 mediump float gMinOutlinePotential = 0.0;
51 mediump float gMaxInlinePotential = 0.0;
52 mediump float gMinInlinePotential = 0.0;
53
54 void calculateCornerRadius()
55 {
56 #if IS_REQUIRED_ROUNDED_CORNER
57   gRadius =
58   mix(
59     mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
60     mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
61     sign(vPosition.y) * 0.5 + 0.5
62   );
63 #endif
64 }
65
66 void calculatePosition()
67 {
68   gFragmentPosition = abs(vPosition) - vRectSize;
69   gCenterPosition = -gRadius;
70 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
71   gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
72 #endif
73   gDiff = gFragmentPosition - gCenterPosition;
74 }
75
76 void calculatePotential()
77 {
78   gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
79 }
80
81 void setupMinMaxPotential()
82 {
83   gPotentialRange = 1.0;
84
85   gMaxOutlinePotential = gRadius + gPotentialRange;
86   gMinOutlinePotential = gRadius - gPotentialRange;
87
88 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
89   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
90   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
91 #else
92   gMaxInlinePotential = gMaxOutlinePotential;
93   gMinInlinePotential = gMinOutlinePotential;
94 #endif
95
96   // reduce defect near edge of rounded corner.
97   gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
98   gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
99 }
100
101 void PreprocessPotential()
102 {
103   calculateCornerRadius();
104   calculatePosition();
105   calculatePotential();
106
107   setupMinMaxPotential();
108 }
109 #endif
110
111 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
112 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
113 {
114   mediump float potential = gPotential;
115
116   // default opacity of borderline is 0.0
117   mediump float borderlineOpacity = 0.0;
118
119   // calculate borderline opacity by potential
120   if(potential > gMinInlinePotential)
121   {
122     // potential is inside borderline range.
123     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
124   }
125
126   //calculate inside of borderline when outilneColor.a < 1.0
127   if(borderlineColor.a < 1.0)
128   {
129     mediump float tCornerRadius = -gCenterPosition;
130     mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
131     mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
132     if(potential > MaxTexturelinePotential)
133     {
134       // potential is out of texture range. use borderline color instead of texture
135       textureColor = vec4(borderlineColor.xyz, 0.0);
136     }
137     else if(potential > MinTexturelinePotential)
138     {
139       // potential is in texture range
140       textureColor = mix(textureColor, vec4(borderlineColor.xyz, 0.0), smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
141     }
142     borderlineOpacity *= borderlineColor.a;
143     return mix(textureColor, vec4(borderlineColor.xyz, 1.0), borderlineOpacity);
144   }
145   return mix(textureColor, borderlineColor, borderlineOpacity);
146 }
147 #endif
148
149 #if !IS_REQUIRED_BLUR && IS_REQUIRED_ROUNDED_CORNER
150 mediump float calculateCornerOpacity()
151 {
152   mediump float potential = gPotential;
153
154   // default opacity is 1.0
155   mediump float opacity = 1.0;
156
157   // calculate borderline opacity by potential
158   if(potential > gMaxOutlinePotential)
159   {
160     // potential is out of borderline range. just discard here
161     discard;
162   }
163   else if(potential > gMinOutlinePotential)
164   {
165     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
166   }
167   return opacity;
168 }
169 #endif
170
171 #if IS_REQUIRED_BLUR
172 mediump float calculateBlurOpacity()
173 {
174 // Don't use borderline!
175   mediump vec2 v = gDiff;
176   mediump float cy = gRadius + blurRadius;
177   mediump float cr = gRadius + blurRadius;
178
179 #if IS_REQUIRED_ROUNDED_CORNER
180   // This routine make perfect circle. If corner radius is not exist, we don't consider prefect circle.
181   cy = min(cy, min(vRectSize.x, vRectSize.y) - gRadius);
182 #endif
183   v = vec2(min(v.x, v.y), max(v.x, v.y));
184   v = v + cy;
185
186   mediump float potential = 0.0;
187   mediump float alias = min(gRadius, 1.0);
188   mediump float potentialMin = cy + gRadius - blurRadius - alias;
189   mediump float potentialMax = cy + gRadius + blurRadius + alias;
190
191   // move center of circles for reduce defact
192   mediump float cyDiff = min(cy, 0.2 * blurRadius);
193   cy -= cyDiff;
194   cr += cyDiff;
195
196   mediump float diffFromBaseline = cy * v.y - (cy + cr) * v.x;
197
198   if(diffFromBaseline > 0.0)
199   {
200     // out of calculation bound.
201     potential = v.y;
202
203     // for anti-alias when blurRaidus = 0.0
204     mediump float heuristicBaselineScale = max(1.0 , cr * (cr + cy));
205     mediump float potentialDiff = min(alias, diffFromBaseline / heuristicBaselineScale);
206     potentialMin += potentialDiff;
207     potentialMax -= potentialDiff;
208   }
209   else
210   {
211     // get some circle centered (x, x) and radius (r = cr / cy * x)
212     // s.t. point v is on that circle
213     // highest point of that circle is (x, x + r) and potential is x + r
214
215     // solve (v.x - x)^2 + (v.y - x)^2 = (cr / cy * x)^2
216 #if IS_REQUIRED_ROUNDED_CORNER
217     // NOTE : lowspec HW cannot calculate here. need to reduce numeric error
218     mediump float A = (cr * cr - 2.0 * cy * cy);
219     mediump float B = cy * (v.x + v.y);
220     mediump float V = dot(v,v);
221     mediump float D = B * B + A * V;
222     potential = V * (cr + cy) / (sqrt(D) + B);
223 #else
224     // We can simplify this value cause cy = 0.8 * blurRadius, cr = 1.2 * blurRadius
225     // potential = 5.0*(sqrt(4.0*(v.x+v.y)^2 + dot(v,v)) - 2.0*(v.x+v.y));
226     //           = 10.0*(v.x+v.y) * (sqrt(1.0 + (length(v) / (2.0*(v.x+v.y)))^2) - 1.0);
227     //           = 10.0*(v.x+v.y) * (sqrt(1.25 - x + x^2) - 1.0);
228     //          ~= 10.0*(v.x+v.y) * (0.11803399 - 0.44721360x + 0.35777088x^2 - 0.14310x^3 + O(x^4)) (Taylor series)
229     //          ~= -1.0557281 * (v.x + v.y) + 2.236068 * length(v) - ~~~ (here, x <= 0.5 * (1.0 - sqrt(0.5)) < 0.1464467)
230     // Note : This simplify need cause we should use it on lowspec HW.
231     mediump float x = 0.5 * (1.0 - length(v) / (v.x + v.y));
232     potential = -1.0557281 * (v.x + v.y) + 2.236068 * length(v) + 10.0 * (v.x + v.y) * (0.35777088 - 0.14310 * x) * x * x;
233 #endif
234   }
235
236   return 1.0 - smoothstep(potentialMin, potentialMax, potential);
237 }
238 #endif
239
240 void main()
241 {
242   lowp vec4 targetColor = vec4(mixColor, 1.0);
243
244 #if IS_REQUIRED_BLUR || IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
245   // skip most potential calculate for performance
246   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
247   {
248     OUT_COLOR = targetColor * uColor;
249     return;
250   }
251   PreprocessPotential();
252 #endif
253
254 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
255   targetColor = convertBorderlineColor(targetColor);
256 #endif
257   OUT_COLOR = targetColor * uColor;
258
259 #if IS_REQUIRED_BLUR
260   mediump float opacity = calculateBlurOpacity();
261   OUT_COLOR.a *= opacity;
262 #elif IS_REQUIRED_ROUNDED_CORNER
263   mediump float opacity = calculateCornerOpacity();
264   OUT_COLOR.a *= opacity;
265 #endif
266 }