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