Merge "Resolve incorrect position for Ellipsis when mixed LTR & RTL languages and...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / image-visual-shader.frag
1 INPUT mediump vec2 vTexCoord;
2 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
3 INPUT mediump vec2 vPosition;
4 INPUT mediump vec2 vRectSize;
5 INPUT mediump vec2 vOptRectSize;
6 #ifdef IS_REQUIRED_ROUNDED_CORNER
7 INPUT mediump vec4 vCornerRadius;
8 #endif
9 #endif
10
11 uniform sampler2D sTexture;
12
13 #ifdef IS_REQUIRED_ALPHA_MASKING
14 uniform sampler2D sMaskTexture;
15 INPUT mediump vec2 vMaskTexCoord;
16 #endif
17
18 #ifdef ATLAS_DEFAULT_WARP
19 uniform mediump vec4 uAtlasRect;
20 #elif defined(ATLAS_CUSTOM_WARP)
21 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
22 uniform lowp vec2 wrapMode;
23 #endif
24
25 uniform lowp vec4 uColor;
26 uniform lowp vec3 mixColor;
27 uniform lowp float preMultipliedAlpha;
28 #ifdef IS_REQUIRED_BORDERLINE
29 uniform mediump float borderlineWidth;
30 uniform mediump float borderlineOffset;
31 uniform lowp vec4 borderlineColor;
32 uniform lowp vec4 uActorColor;
33 #endif
34
35 #ifdef ATLAS_CUSTOM_WARP
36 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
37 {
38   mediump float coord;
39   if( wrap > 1.5 ) /* REFLECT */
40     coord = 1.0 - abs(fract(coordinate*0.5)*2.0 - 1.0);
41   else /* warp is 0 or 1 */
42     coord = mix(coordinate, fract(coordinate), wrap);
43   return clamp(mix(range.x, range.y, coord), range.x, range.y);
44 }
45 #endif
46
47 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
48 // Global values both rounded corner and borderline use
49
50 // radius of rounded corner on this quadrant
51 mediump float gRadius = 0.0;
52
53 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
54 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
55 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
56 mediump float gCenterPosition = 0.0;
57 // relative coordinate of gFragmentPosition from gCenterPosition.
58 mediump vec2 gDiff = vec2(0.0, 0.0);
59 // potential value what our algorithm use.
60 mediump float gPotential = 0.0;
61
62 // threshold of potential
63 mediump float gPotentialRange = 0.0;
64 mediump float gMaxOutlinePotential = 0.0;
65 mediump float gMinOutlinePotential = 0.0;
66 mediump float gMaxInlinePotential = 0.0;
67 mediump float gMinInlinePotential = 0.0;
68
69 void calculateCornerRadius()
70 {
71 #ifdef IS_REQUIRED_ROUNDED_CORNER
72   gRadius =
73   mix(
74     mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
75     mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
76     sign(vPosition.y) * 0.5 + 0.5
77   );
78 #endif
79 }
80
81 void calculatePosition()
82 {
83   gFragmentPosition = abs(vPosition) - vRectSize;
84   gCenterPosition = -gRadius;
85 #ifdef IS_REQUIRED_BORDERLINE
86   gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
87 #endif
88   gDiff = gFragmentPosition - gCenterPosition;
89 }
90
91 void calculatePotential()
92 {
93   gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
94 }
95
96 void setupMinMaxPotential()
97 {
98   gPotentialRange = 1.0;
99
100   gMaxOutlinePotential = gRadius + gPotentialRange;
101   gMinOutlinePotential = gRadius - gPotentialRange;
102
103 #ifdef IS_REQUIRED_BORDERLINE
104   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
105   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
106 #else
107   gMaxInlinePotential = gMaxOutlinePotential;
108   gMinInlinePotential = gMinOutlinePotential;
109 #endif
110
111   // reduce defect near edge of rounded corner.
112   gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
113   gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
114 }
115
116 void PreprocessPotential()
117 {
118   calculateCornerRadius();
119   calculatePosition();
120   calculatePotential();
121
122   setupMinMaxPotential();
123 }
124 #endif
125
126 #ifdef IS_REQUIRED_BORDERLINE
127 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
128 {
129   mediump float potential = gPotential;
130
131   // default opacity of borderline is 0.0
132   mediump float borderlineOpacity = 0.0;
133
134   // calculate borderline opacity by potential
135   if(potential > gMinInlinePotential)
136   {
137     // potential is inside borderline range.
138     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
139
140     // Muliply borderlineWidth to resolve very thin borderline
141     borderlineOpacity *= min(1.0, borderlineWidth);
142   }
143
144   lowp vec3  borderlineColorRGB   = borderlineColor.rgb * uActorColor.rgb;
145   lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
146   borderlineColorRGB *= mix(1.0, borderlineColorAlpha, preMultipliedAlpha);
147
148   // Calculate inside of borderline when alpha is between (0.0  1.0). So we need to apply texture color.
149   // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
150   // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
151   if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
152   {
153     mediump float tCornerRadius = -gCenterPosition;
154     mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
155     mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
156     if(potential > MaxTexturelinePotential)
157     {
158       // potential is out of texture range.
159       textureColor = vec4(0.0);
160     }
161     else
162     {
163       // potential is in texture range.
164       lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
165       textureColor.a *= textureAlphaScale;
166       textureColor.rgb *= mix(textureColor.a, textureAlphaScale, preMultipliedAlpha);
167     }
168
169     borderlineColorAlpha *= borderlineOpacity;
170     borderlineColorRGB *= mix(borderlineColorAlpha, borderlineOpacity, preMultipliedAlpha);
171     // We use pre-multiplied color to reduce operations.
172     // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
173
174     // Manual blend operation with premultiplied colors.
175     // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
176     // (Final rgb * alpha) =  borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
177     // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
178     // Else, return vec4((rgb*alpha) / alpha, alpha)
179
180     lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
181     lowp vec3  finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
182     // TODO : Need to find some way without division
183     return vec4(finalMultipliedRGB * mix(1.0 / finalAlpha, 1.0, preMultipliedAlpha), finalAlpha);
184   }
185   return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
186 }
187 #endif
188
189 #ifdef IS_REQUIRED_ROUNDED_CORNER
190 mediump float calculateCornerOpacity()
191 {
192   mediump float potential = gPotential;
193
194   // default opacity is 1.0
195   mediump float opacity = 1.0;
196
197   // calculate borderline opacity by potential
198   if(potential > gMaxOutlinePotential)
199   {
200     // potential is out of borderline range. just discard here
201     discard;
202   }
203   else if(potential > gMinOutlinePotential)
204   {
205     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
206   }
207   return opacity;
208 }
209 #endif
210
211 void main()
212 {
213 #ifdef ATLAS_DEFAULT_WARP
214   mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
215 #elif defined(ATLAS_CUSTOM_WARP)
216   mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
217                                 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
218 #else
219   mediump vec2 texCoord = vTexCoord;
220 #endif
221
222   lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 ) * uColor;
223
224 #ifdef IS_REQUIRED_ALPHA_MASKING
225   mediump float maskAlpha = TEXTURE(sMaskTexture, vMaskTexCoord).a;
226   textureColor.a *= maskAlpha;
227   textureColor.rgb *= mix(1.0, maskAlpha, preMultipliedAlpha);
228 #endif
229
230 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
231   // skip most potential calculate for performance
232   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
233   {
234     OUT_COLOR = textureColor;
235   }
236   else
237   {
238     PreprocessPotential();
239 #endif
240
241 #ifdef IS_REQUIRED_BORDERLINE
242     textureColor = convertBorderlineColor(textureColor);
243 #endif
244     OUT_COLOR = textureColor;
245
246 #ifdef IS_REQUIRED_ROUNDED_CORNER
247     mediump float opacity = calculateCornerOpacity();
248     OUT_COLOR.a *= opacity;
249     OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
250 #endif
251
252 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
253   }
254 #endif
255 }