xref: /OK3568_Linux_fs/external/xserver/glamor/glamor_gradient.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2009 Intel Corporation
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun  * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun  * Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*4882a593Smuzhiyun  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*4882a593Smuzhiyun  * IN THE SOFTWARE.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Authors:
24*4882a593Smuzhiyun  *    Junyan He <junyan.he@linux.intel.com>
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /** @file glamor_gradient.c
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * Gradient acceleration implementation
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include "glamor_priv.h"
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #define LINEAR_SMALL_STOPS (6 + 2)
36*4882a593Smuzhiyun #define LINEAR_LARGE_STOPS (16 + 2)
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define RADIAL_SMALL_STOPS (6 + 2)
39*4882a593Smuzhiyun #define RADIAL_LARGE_STOPS (16 + 2)
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun static char *
_glamor_create_getcolor_fs_source(ScreenPtr screen,int stops_count,int use_array)42*4882a593Smuzhiyun _glamor_create_getcolor_fs_source(ScreenPtr screen, int stops_count,
43*4882a593Smuzhiyun                                   int use_array)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun     char *gradient_fs = NULL;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define gradient_fs_getcolor\
48*4882a593Smuzhiyun 	    GLAMOR_DEFAULT_PRECISION\
49*4882a593Smuzhiyun 	    "uniform int n_stop;\n"\
50*4882a593Smuzhiyun 	    "uniform float stops[%d];\n"\
51*4882a593Smuzhiyun 	    "uniform vec4 stop_colors[%d];\n"\
52*4882a593Smuzhiyun 	    "vec4 get_color(float stop_len)\n"\
53*4882a593Smuzhiyun 	    "{\n"\
54*4882a593Smuzhiyun 	    "    int i = 0;\n"\
55*4882a593Smuzhiyun 	    "    vec4 stop_color_before;\n"\
56*4882a593Smuzhiyun 	    "    vec4 gradient_color;\n"\
57*4882a593Smuzhiyun 	    "    float stop_delta;\n"\
58*4882a593Smuzhiyun 	    "    float percentage; \n"\
59*4882a593Smuzhiyun 	    "    \n"\
60*4882a593Smuzhiyun 	    "    if(stop_len < stops[0])\n"\
61*4882a593Smuzhiyun 	    "        return vec4(0.0, 0.0, 0.0, 0.0); \n"\
62*4882a593Smuzhiyun 	    "    for(i = 1; i < n_stop; i++) {\n"\
63*4882a593Smuzhiyun 	    "        if(stop_len < stops[i])\n"\
64*4882a593Smuzhiyun 	    "            break; \n"\
65*4882a593Smuzhiyun 	    "    }\n"\
66*4882a593Smuzhiyun 	    "    if(i == n_stop)\n"\
67*4882a593Smuzhiyun 	    "        return vec4(0.0, 0.0, 0.0, 0.0); \n"\
68*4882a593Smuzhiyun 	    "    \n"\
69*4882a593Smuzhiyun 	    "    stop_color_before = stop_colors[i-1];\n"\
70*4882a593Smuzhiyun 	    "    stop_delta = stops[i] - stops[i-1];\n"\
71*4882a593Smuzhiyun 	    "    if(stop_delta > 2.0)\n"\
72*4882a593Smuzhiyun 	    "        percentage = 0.0;\n" /*For comply with pixman, walker->stepper overflow.*/\
73*4882a593Smuzhiyun 	    "    else if(stop_delta < 0.000001)\n"\
74*4882a593Smuzhiyun 	    "        percentage = 0.0;\n"\
75*4882a593Smuzhiyun 	    "    else \n"\
76*4882a593Smuzhiyun 	    "        percentage = (stop_len - stops[i-1])/stop_delta;\n"\
77*4882a593Smuzhiyun 	    "    \n"\
78*4882a593Smuzhiyun 	    "    gradient_color = stop_color_before;\n"\
79*4882a593Smuzhiyun 	    "    if(percentage != 0.0)\n"\
80*4882a593Smuzhiyun 	    "        gradient_color += (stop_colors[i] - gradient_color)*percentage;\n"\
81*4882a593Smuzhiyun 	    "    return vec4(gradient_color.rgb * gradient_color.a, gradient_color.a);\n"\
82*4882a593Smuzhiyun 	    "}\n"
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun     /* Because the array access for shader is very slow, the performance is very low
85*4882a593Smuzhiyun        if use array. So use global uniform to replace for it if the number of n_stops is small. */
86*4882a593Smuzhiyun     const char *gradient_fs_getcolor_no_array =
87*4882a593Smuzhiyun         GLAMOR_DEFAULT_PRECISION
88*4882a593Smuzhiyun         "uniform int n_stop;\n"
89*4882a593Smuzhiyun         "uniform float stop0;\n"
90*4882a593Smuzhiyun         "uniform float stop1;\n"
91*4882a593Smuzhiyun         "uniform float stop2;\n"
92*4882a593Smuzhiyun         "uniform float stop3;\n"
93*4882a593Smuzhiyun         "uniform float stop4;\n"
94*4882a593Smuzhiyun         "uniform float stop5;\n"
95*4882a593Smuzhiyun         "uniform float stop6;\n"
96*4882a593Smuzhiyun         "uniform float stop7;\n"
97*4882a593Smuzhiyun         "uniform vec4 stop_color0;\n"
98*4882a593Smuzhiyun         "uniform vec4 stop_color1;\n"
99*4882a593Smuzhiyun         "uniform vec4 stop_color2;\n"
100*4882a593Smuzhiyun         "uniform vec4 stop_color3;\n"
101*4882a593Smuzhiyun         "uniform vec4 stop_color4;\n"
102*4882a593Smuzhiyun         "uniform vec4 stop_color5;\n"
103*4882a593Smuzhiyun         "uniform vec4 stop_color6;\n"
104*4882a593Smuzhiyun         "uniform vec4 stop_color7;\n"
105*4882a593Smuzhiyun         "\n"
106*4882a593Smuzhiyun         "vec4 get_color(float stop_len)\n"
107*4882a593Smuzhiyun         "{\n"
108*4882a593Smuzhiyun         "    vec4 stop_color_before;\n"
109*4882a593Smuzhiyun         "    vec4 stop_color_after;\n"
110*4882a593Smuzhiyun         "    vec4 gradient_color;\n"
111*4882a593Smuzhiyun         "    float stop_before;\n"
112*4882a593Smuzhiyun         "    float stop_delta;\n"
113*4882a593Smuzhiyun         "    float percentage; \n"
114*4882a593Smuzhiyun         "    \n"
115*4882a593Smuzhiyun         "    if((stop_len < stop0) && (n_stop >= 1)) {\n"
116*4882a593Smuzhiyun         "        stop_color_before = vec4(0.0, 0.0, 0.0, 0.0);\n"
117*4882a593Smuzhiyun         "        stop_delta = 0.0;\n"
118*4882a593Smuzhiyun         "    } else if((stop_len < stop1) && (n_stop >= 2)) {\n"
119*4882a593Smuzhiyun         "        stop_color_before = stop_color0;\n"
120*4882a593Smuzhiyun         "        stop_color_after = stop_color1;\n"
121*4882a593Smuzhiyun         "        stop_before = stop0;\n"
122*4882a593Smuzhiyun         "        stop_delta = stop1 - stop0;\n"
123*4882a593Smuzhiyun         "    } else if((stop_len < stop2) && (n_stop >= 3)) {\n"
124*4882a593Smuzhiyun         "        stop_color_before = stop_color1;\n"
125*4882a593Smuzhiyun         "        stop_color_after = stop_color2;\n"
126*4882a593Smuzhiyun         "        stop_before = stop1;\n"
127*4882a593Smuzhiyun         "        stop_delta = stop2 - stop1;\n"
128*4882a593Smuzhiyun         "    } else if((stop_len < stop3) && (n_stop >= 4)){\n"
129*4882a593Smuzhiyun         "        stop_color_before = stop_color2;\n"
130*4882a593Smuzhiyun         "        stop_color_after = stop_color3;\n"
131*4882a593Smuzhiyun         "        stop_before = stop2;\n"
132*4882a593Smuzhiyun         "        stop_delta = stop3 - stop2;\n"
133*4882a593Smuzhiyun         "    } else if((stop_len < stop4) && (n_stop >= 5)){\n"
134*4882a593Smuzhiyun         "        stop_color_before = stop_color3;\n"
135*4882a593Smuzhiyun         "        stop_color_after = stop_color4;\n"
136*4882a593Smuzhiyun         "        stop_before = stop3;\n"
137*4882a593Smuzhiyun         "        stop_delta = stop4 - stop3;\n"
138*4882a593Smuzhiyun         "    } else if((stop_len < stop5) && (n_stop >= 6)){\n"
139*4882a593Smuzhiyun         "        stop_color_before = stop_color4;\n"
140*4882a593Smuzhiyun         "        stop_color_after = stop_color5;\n"
141*4882a593Smuzhiyun         "        stop_before = stop4;\n"
142*4882a593Smuzhiyun         "        stop_delta = stop5 - stop4;\n"
143*4882a593Smuzhiyun         "    } else if((stop_len < stop6) && (n_stop >= 7)){\n"
144*4882a593Smuzhiyun         "        stop_color_before = stop_color5;\n"
145*4882a593Smuzhiyun         "        stop_color_after = stop_color6;\n"
146*4882a593Smuzhiyun         "        stop_before = stop5;\n"
147*4882a593Smuzhiyun         "        stop_delta = stop6 - stop5;\n"
148*4882a593Smuzhiyun         "    } else if((stop_len < stop7) && (n_stop >= 8)){\n"
149*4882a593Smuzhiyun         "        stop_color_before = stop_color6;\n"
150*4882a593Smuzhiyun         "        stop_color_after = stop_color7;\n"
151*4882a593Smuzhiyun         "        stop_before = stop6;\n"
152*4882a593Smuzhiyun         "        stop_delta = stop7 - stop6;\n"
153*4882a593Smuzhiyun         "    } else {\n"
154*4882a593Smuzhiyun         "        stop_color_before = vec4(0.0, 0.0, 0.0, 0.0);\n"
155*4882a593Smuzhiyun         "        stop_delta = 0.0;\n"
156*4882a593Smuzhiyun         "    }\n"
157*4882a593Smuzhiyun         "    if(stop_delta > 2.0)\n"
158*4882a593Smuzhiyun         "        percentage = 0.0;\n" //For comply with pixman, walker->stepper overflow.
159*4882a593Smuzhiyun         "    else if(stop_delta < 0.000001)\n"
160*4882a593Smuzhiyun         "        percentage = 0.0;\n"
161*4882a593Smuzhiyun         "    else\n"
162*4882a593Smuzhiyun         "        percentage = (stop_len - stop_before)/stop_delta;\n"
163*4882a593Smuzhiyun         "    \n"
164*4882a593Smuzhiyun         "    gradient_color = stop_color_before;\n"
165*4882a593Smuzhiyun         "    if(percentage != 0.0)\n"
166*4882a593Smuzhiyun         "        gradient_color += (stop_color_after - gradient_color)*percentage;\n"
167*4882a593Smuzhiyun         "    return vec4(gradient_color.rgb * gradient_color.a, gradient_color.a);\n"
168*4882a593Smuzhiyun         "}\n";
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun     if (use_array) {
171*4882a593Smuzhiyun         XNFasprintf(&gradient_fs,
172*4882a593Smuzhiyun                     gradient_fs_getcolor, stops_count, stops_count);
173*4882a593Smuzhiyun         return gradient_fs;
174*4882a593Smuzhiyun     }
175*4882a593Smuzhiyun     else {
176*4882a593Smuzhiyun         return XNFstrdup(gradient_fs_getcolor_no_array);
177*4882a593Smuzhiyun     }
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun static void
_glamor_create_radial_gradient_program(ScreenPtr screen,int stops_count,int dyn_gen)181*4882a593Smuzhiyun _glamor_create_radial_gradient_program(ScreenPtr screen, int stops_count,
182*4882a593Smuzhiyun                                        int dyn_gen)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun     glamor_screen_private *glamor_priv;
185*4882a593Smuzhiyun     int index;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun     GLint gradient_prog = 0;
188*4882a593Smuzhiyun     char *gradient_fs = NULL;
189*4882a593Smuzhiyun     GLint fs_prog, vs_prog;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun     const char *gradient_vs =
192*4882a593Smuzhiyun         GLAMOR_DEFAULT_PRECISION
193*4882a593Smuzhiyun         "attribute vec4 v_position;\n"
194*4882a593Smuzhiyun         "attribute vec4 v_texcoord;\n"
195*4882a593Smuzhiyun         "varying vec2 source_texture;\n"
196*4882a593Smuzhiyun         "\n"
197*4882a593Smuzhiyun         "void main()\n"
198*4882a593Smuzhiyun         "{\n"
199*4882a593Smuzhiyun         "    gl_Position = v_position;\n"
200*4882a593Smuzhiyun         "    source_texture = v_texcoord.xy;\n"
201*4882a593Smuzhiyun         "}\n";
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun     /*
204*4882a593Smuzhiyun      *     Refer to pixman radial gradient.
205*4882a593Smuzhiyun      *
206*4882a593Smuzhiyun      *     The problem is given the two circles of c1 and c2 with the radius of r1 and
207*4882a593Smuzhiyun      *     r1, we need to caculate the t, which is used to do interpolate with stops,
208*4882a593Smuzhiyun      *     using the fomula:
209*4882a593Smuzhiyun      *     length((1-t)*c1 + t*c2 - p) = (1-t)*r1 + t*r2
210*4882a593Smuzhiyun      *     expand the fomula with xy coond, get the following:
211*4882a593Smuzhiyun      *     sqrt(sqr((1-t)*c1.x + t*c2.x - p.x) + sqr((1-t)*c1.y + t*c2.y - p.y))
212*4882a593Smuzhiyun      *           = (1-t)r1 + t*r2
213*4882a593Smuzhiyun      *     <====> At*t- 2Bt + C = 0
214*4882a593Smuzhiyun      *     where A = sqr(c2.x - c1.x) + sqr(c2.y - c1.y) - sqr(r2 -r1)
215*4882a593Smuzhiyun      *           B = (p.x - c1.x)*(c2.x - c1.x) + (p.y - c1.y)*(c2.y - c1.y) + r1*(r2 -r1)
216*4882a593Smuzhiyun      *           C = sqr(p.x - c1.x) + sqr(p.y - c1.y) - r1*r1
217*4882a593Smuzhiyun      *
218*4882a593Smuzhiyun      *     solve the fomula and we get the result of
219*4882a593Smuzhiyun      *     t = (B + sqrt(B*B - A*C)) / A  or
220*4882a593Smuzhiyun      *     t = (B - sqrt(B*B - A*C)) / A  (quadratic equation have two solutions)
221*4882a593Smuzhiyun      *
222*4882a593Smuzhiyun      *     The solution we are going to prefer is the bigger one, unless the
223*4882a593Smuzhiyun      *     radius associated to it is negative (or it falls outside the valid t range)
224*4882a593Smuzhiyun      */
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun #define gradient_radial_fs_template\
227*4882a593Smuzhiyun 	    GLAMOR_DEFAULT_PRECISION\
228*4882a593Smuzhiyun 	    "uniform mat3 transform_mat;\n"\
229*4882a593Smuzhiyun 	    "uniform int repeat_type;\n"\
230*4882a593Smuzhiyun 	    "uniform float A_value;\n"\
231*4882a593Smuzhiyun 	    "uniform vec2 c1;\n"\
232*4882a593Smuzhiyun 	    "uniform float r1;\n"\
233*4882a593Smuzhiyun 	    "uniform vec2 c2;\n"\
234*4882a593Smuzhiyun 	    "uniform float r2;\n"\
235*4882a593Smuzhiyun 	    "varying vec2 source_texture;\n"\
236*4882a593Smuzhiyun 	    "\n"\
237*4882a593Smuzhiyun 	    "vec4 get_color(float stop_len);\n"\
238*4882a593Smuzhiyun 	    "\n"\
239*4882a593Smuzhiyun 	    "int t_invalid;\n"\
240*4882a593Smuzhiyun 	    "\n"\
241*4882a593Smuzhiyun 	    "float get_stop_len()\n"\
242*4882a593Smuzhiyun 	    "{\n"\
243*4882a593Smuzhiyun 	    "    float t = 0.0;\n"\
244*4882a593Smuzhiyun 	    "    float sqrt_value;\n"\
245*4882a593Smuzhiyun 	    "    t_invalid = 0;\n"\
246*4882a593Smuzhiyun 	    "    \n"\
247*4882a593Smuzhiyun 	    "    vec3 tmp = vec3(source_texture.x, source_texture.y, 1.0);\n"\
248*4882a593Smuzhiyun 	    "    vec3 source_texture_trans = transform_mat * tmp;\n"\
249*4882a593Smuzhiyun 	    "    source_texture_trans.xy = source_texture_trans.xy/source_texture_trans.z;\n"\
250*4882a593Smuzhiyun 	    "    float B_value = (source_texture_trans.x - c1.x) * (c2.x - c1.x)\n"\
251*4882a593Smuzhiyun 	    "                     + (source_texture_trans.y - c1.y) * (c2.y - c1.y)\n"\
252*4882a593Smuzhiyun 	    "                     + r1 * (r2 - r1);\n"\
253*4882a593Smuzhiyun 	    "    float C_value = (source_texture_trans.x - c1.x) * (source_texture_trans.x - c1.x)\n"\
254*4882a593Smuzhiyun 	    "                     + (source_texture_trans.y - c1.y) * (source_texture_trans.y - c1.y)\n"\
255*4882a593Smuzhiyun 	    "                     - r1*r1;\n"\
256*4882a593Smuzhiyun 	    "    if(abs(A_value) < 0.00001) {\n"\
257*4882a593Smuzhiyun 	    "        if(B_value == 0.0) {\n"\
258*4882a593Smuzhiyun 	    "            t_invalid = 1;\n"\
259*4882a593Smuzhiyun 	    "            return t;\n"\
260*4882a593Smuzhiyun 	    "        }\n"\
261*4882a593Smuzhiyun 	    "        t = 0.5 * C_value / B_value;"\
262*4882a593Smuzhiyun 	    "    } else {\n"\
263*4882a593Smuzhiyun 	    "        sqrt_value = B_value * B_value - A_value * C_value;\n"\
264*4882a593Smuzhiyun 	    "        if(sqrt_value < 0.0) {\n"\
265*4882a593Smuzhiyun 	    "            t_invalid = 1;\n"\
266*4882a593Smuzhiyun 	    "            return t;\n"\
267*4882a593Smuzhiyun 	    "        }\n"\
268*4882a593Smuzhiyun 	    "        sqrt_value = sqrt(sqrt_value);\n"\
269*4882a593Smuzhiyun 	    "        t = (B_value + sqrt_value) / A_value;\n"\
270*4882a593Smuzhiyun 	    "    }\n"\
271*4882a593Smuzhiyun 	    "    if(repeat_type == %d) {\n" /* RepeatNone case. */\
272*4882a593Smuzhiyun 	    "        if((t <= 0.0) || (t > 1.0))\n"\
273*4882a593Smuzhiyun 	    /*           try another if first one invalid*/\
274*4882a593Smuzhiyun 	    "            t = (B_value - sqrt_value) / A_value;\n"\
275*4882a593Smuzhiyun 	    "        \n"\
276*4882a593Smuzhiyun 	    "        if((t <= 0.0) || (t > 1.0)) {\n" /*still invalid, return.*/\
277*4882a593Smuzhiyun 	    "            t_invalid = 1;\n"\
278*4882a593Smuzhiyun 	    "            return t;\n"\
279*4882a593Smuzhiyun 	    "        }\n"\
280*4882a593Smuzhiyun 	    "    } else {\n"\
281*4882a593Smuzhiyun 	    "        if(t * (r2 - r1) <= -1.0 * r1)\n"\
282*4882a593Smuzhiyun 	    /*           try another if first one invalid*/\
283*4882a593Smuzhiyun 	    "            t = (B_value - sqrt_value) / A_value;\n"\
284*4882a593Smuzhiyun 	    "        \n"\
285*4882a593Smuzhiyun 	    "        if(t * (r2 -r1) <= -1.0 * r1) {\n" /*still invalid, return.*/\
286*4882a593Smuzhiyun 	    "            t_invalid = 1;\n"\
287*4882a593Smuzhiyun 	    "            return t;\n"\
288*4882a593Smuzhiyun 	    "        }\n"\
289*4882a593Smuzhiyun 	    "    }\n"\
290*4882a593Smuzhiyun 	    "    \n"\
291*4882a593Smuzhiyun 	    "    if(repeat_type == %d){\n" /* repeat normal*/\
292*4882a593Smuzhiyun 	    "        t = fract(t);\n"\
293*4882a593Smuzhiyun 	    "    }\n"\
294*4882a593Smuzhiyun 	    "    \n"\
295*4882a593Smuzhiyun 	    "    if(repeat_type == %d) {\n" /* repeat reflect*/\
296*4882a593Smuzhiyun 	    "        t = abs(fract(t * 0.5 + 0.5) * 2.0 - 1.0);\n"\
297*4882a593Smuzhiyun 	    "    }\n"\
298*4882a593Smuzhiyun 	    "    \n"\
299*4882a593Smuzhiyun 	    "    return t;\n"\
300*4882a593Smuzhiyun 	    "}\n"\
301*4882a593Smuzhiyun 	    "\n"\
302*4882a593Smuzhiyun 	    "void main()\n"\
303*4882a593Smuzhiyun 	    "{\n"\
304*4882a593Smuzhiyun 	    "    float stop_len = get_stop_len();\n"\
305*4882a593Smuzhiyun 	    "    if(t_invalid == 1) {\n"\
306*4882a593Smuzhiyun 	    "        gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);\n"\
307*4882a593Smuzhiyun 	    "    } else {\n"\
308*4882a593Smuzhiyun 	    "        gl_FragColor = get_color(stop_len);\n"\
309*4882a593Smuzhiyun 	    "    }\n"\
310*4882a593Smuzhiyun 	    "}\n"\
311*4882a593Smuzhiyun 	    "\n"\
312*4882a593Smuzhiyun             "%s\n" /* fs_getcolor_source */
313*4882a593Smuzhiyun     char *fs_getcolor_source;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun     glamor_priv = glamor_get_screen_private(screen);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun     if ((glamor_priv->radial_max_nstops >= stops_count) && (dyn_gen)) {
318*4882a593Smuzhiyun         /* Very Good, not to generate again. */
319*4882a593Smuzhiyun         return;
320*4882a593Smuzhiyun     }
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun     glamor_make_current(glamor_priv);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun     if (dyn_gen && glamor_priv->gradient_prog[SHADER_GRADIENT_RADIAL][2]) {
325*4882a593Smuzhiyun         glDeleteProgram(glamor_priv->gradient_prog[SHADER_GRADIENT_RADIAL][2]);
326*4882a593Smuzhiyun         glamor_priv->gradient_prog[SHADER_GRADIENT_RADIAL][2] = 0;
327*4882a593Smuzhiyun     }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun     gradient_prog = glCreateProgram();
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun     vs_prog = glamor_compile_glsl_prog(GL_VERTEX_SHADER, gradient_vs);
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun     fs_getcolor_source =
334*4882a593Smuzhiyun         _glamor_create_getcolor_fs_source(screen, stops_count,
335*4882a593Smuzhiyun                                           (stops_count > 0));
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun     XNFasprintf(&gradient_fs,
338*4882a593Smuzhiyun                 gradient_radial_fs_template,
339*4882a593Smuzhiyun                 PIXMAN_REPEAT_NONE, PIXMAN_REPEAT_NORMAL,
340*4882a593Smuzhiyun                 PIXMAN_REPEAT_REFLECT,
341*4882a593Smuzhiyun                 fs_getcolor_source);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun     fs_prog = glamor_compile_glsl_prog(GL_FRAGMENT_SHADER, gradient_fs);
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun     free(gradient_fs);
346*4882a593Smuzhiyun     free(fs_getcolor_source);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun     glAttachShader(gradient_prog, vs_prog);
349*4882a593Smuzhiyun     glAttachShader(gradient_prog, fs_prog);
350*4882a593Smuzhiyun     glDeleteShader(vs_prog);
351*4882a593Smuzhiyun     glDeleteShader(fs_prog);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun     glBindAttribLocation(gradient_prog, GLAMOR_VERTEX_POS, "v_position");
354*4882a593Smuzhiyun     glBindAttribLocation(gradient_prog, GLAMOR_VERTEX_SOURCE, "v_texcoord");
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun     glamor_link_glsl_prog(screen, gradient_prog, "radial gradient");
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun     if (dyn_gen) {
359*4882a593Smuzhiyun         index = 2;
360*4882a593Smuzhiyun         glamor_priv->radial_max_nstops = stops_count;
361*4882a593Smuzhiyun     }
362*4882a593Smuzhiyun     else if (stops_count) {
363*4882a593Smuzhiyun         index = 1;
364*4882a593Smuzhiyun     }
365*4882a593Smuzhiyun     else {
366*4882a593Smuzhiyun         index = 0;
367*4882a593Smuzhiyun     }
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun     glamor_priv->gradient_prog[SHADER_GRADIENT_RADIAL][index] = gradient_prog;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun static void
_glamor_create_linear_gradient_program(ScreenPtr screen,int stops_count,int dyn_gen)373*4882a593Smuzhiyun _glamor_create_linear_gradient_program(ScreenPtr screen, int stops_count,
374*4882a593Smuzhiyun                                        int dyn_gen)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun     glamor_screen_private *glamor_priv;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun     int index = 0;
379*4882a593Smuzhiyun     GLint gradient_prog = 0;
380*4882a593Smuzhiyun     char *gradient_fs = NULL;
381*4882a593Smuzhiyun     GLint fs_prog, vs_prog;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun     const char *gradient_vs =
384*4882a593Smuzhiyun         GLAMOR_DEFAULT_PRECISION
385*4882a593Smuzhiyun         "attribute vec4 v_position;\n"
386*4882a593Smuzhiyun         "attribute vec4 v_texcoord;\n"
387*4882a593Smuzhiyun         "varying vec2 source_texture;\n"
388*4882a593Smuzhiyun         "\n"
389*4882a593Smuzhiyun         "void main()\n"
390*4882a593Smuzhiyun         "{\n"
391*4882a593Smuzhiyun         "    gl_Position = v_position;\n"
392*4882a593Smuzhiyun         "    source_texture = v_texcoord.xy;\n"
393*4882a593Smuzhiyun         "}\n";
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun     /*
396*4882a593Smuzhiyun      *                                      |
397*4882a593Smuzhiyun      *                                      |\
398*4882a593Smuzhiyun      *                                      | \
399*4882a593Smuzhiyun      *                                      |  \
400*4882a593Smuzhiyun      *                                      |   \
401*4882a593Smuzhiyun      *                                      |\   \
402*4882a593Smuzhiyun      *                                      | \   \
403*4882a593Smuzhiyun      *     cos_val =                        |\ p1d \   /
404*4882a593Smuzhiyun      *      sqrt(1/(slope*slope+1.0))  ------>\ \   \ /
405*4882a593Smuzhiyun      *                                      |  \ \   \
406*4882a593Smuzhiyun      *                                      |   \ \ / \
407*4882a593Smuzhiyun      *                                      |    \ *Pt1\
408*4882a593Smuzhiyun      *         *p1                          |     \     \     *P
409*4882a593Smuzhiyun      *          \                           |    / \     \   /
410*4882a593Smuzhiyun      *           \                          |   /   \     \ /
411*4882a593Smuzhiyun      *            \                         |       pd     \
412*4882a593Smuzhiyun      *             \                        |         \   / \
413*4882a593Smuzhiyun      *            p2*                       |          \ /   \       /
414*4882a593Smuzhiyun      *        slope = (p2.y - p1.y) /       |           /     p2d   /
415*4882a593Smuzhiyun      *                    (p2.x - p1.x)     |          /       \   /
416*4882a593Smuzhiyun      *                                      |         /         \ /
417*4882a593Smuzhiyun      *                                      |        /           /
418*4882a593Smuzhiyun      *                                      |       /           /
419*4882a593Smuzhiyun      *                                      |      /           *Pt2
420*4882a593Smuzhiyun      *                                      |                 /
421*4882a593Smuzhiyun      *                                      |                /
422*4882a593Smuzhiyun      *                                      |               /
423*4882a593Smuzhiyun      *                                      |              /
424*4882a593Smuzhiyun      *                                      |             /
425*4882a593Smuzhiyun      *                               -------+---------------------------------
426*4882a593Smuzhiyun      *                                     O|
427*4882a593Smuzhiyun      *                                      |
428*4882a593Smuzhiyun      *                                      |
429*4882a593Smuzhiyun      *
430*4882a593Smuzhiyun      *      step 1: compute the distance of p, pt1 and pt2 in the slope direction.
431*4882a593Smuzhiyun      *              Caculate the distance on Y axis first and multiply cos_val to
432*4882a593Smuzhiyun      *              get the value on slope direction(pd, p1d and p2d represent the
433*4882a593Smuzhiyun      *              distance of p, pt1, and pt2 respectively).
434*4882a593Smuzhiyun      *
435*4882a593Smuzhiyun      *      step 2: caculate the percentage of (pd - p1d)/(p2d - p1d).
436*4882a593Smuzhiyun      *              If (pd - p1d) > (p2d - p1d) or < 0, then sub or add (p2d - p1d)
437*4882a593Smuzhiyun      *              to make it in the range of [0, (p2d - p1d)].
438*4882a593Smuzhiyun      *
439*4882a593Smuzhiyun      *      step 3: compare the percentage to every stop and find the stpos just
440*4882a593Smuzhiyun      *              before and after it. Use the interpolation fomula to compute RGBA.
441*4882a593Smuzhiyun      */
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun #define gradient_fs_template	\
444*4882a593Smuzhiyun 	    GLAMOR_DEFAULT_PRECISION\
445*4882a593Smuzhiyun 	    "uniform mat3 transform_mat;\n"\
446*4882a593Smuzhiyun 	    "uniform int repeat_type;\n"\
447*4882a593Smuzhiyun 	    "uniform int hor_ver;\n"\
448*4882a593Smuzhiyun 	    "uniform float pt_slope;\n"\
449*4882a593Smuzhiyun 	    "uniform float cos_val;\n"\
450*4882a593Smuzhiyun 	    "uniform float p1_distance;\n"\
451*4882a593Smuzhiyun 	    "uniform float pt_distance;\n"\
452*4882a593Smuzhiyun 	    "varying vec2 source_texture;\n"\
453*4882a593Smuzhiyun 	    "\n"\
454*4882a593Smuzhiyun 	    "vec4 get_color(float stop_len);\n"\
455*4882a593Smuzhiyun 	    "\n"\
456*4882a593Smuzhiyun 	    "float get_stop_len()\n"\
457*4882a593Smuzhiyun 	    "{\n"\
458*4882a593Smuzhiyun 	    "    vec3 tmp = vec3(source_texture.x, source_texture.y, 1.0);\n"\
459*4882a593Smuzhiyun 	    "    float distance;\n"\
460*4882a593Smuzhiyun 	    "    float _p1_distance;\n"\
461*4882a593Smuzhiyun 	    "    float _pt_distance;\n"\
462*4882a593Smuzhiyun 	    "    float y_dist;\n"\
463*4882a593Smuzhiyun 	    "    vec3 source_texture_trans = transform_mat * tmp;\n"\
464*4882a593Smuzhiyun 	    "    \n"\
465*4882a593Smuzhiyun 	    "    if(hor_ver == 0) { \n" /*Normal case.*/\
466*4882a593Smuzhiyun 	    "        y_dist = source_texture_trans.y - source_texture_trans.x*pt_slope;\n"\
467*4882a593Smuzhiyun 	    "        distance = y_dist * cos_val;\n"\
468*4882a593Smuzhiyun 	    "        _p1_distance = p1_distance * source_texture_trans.z;\n"\
469*4882a593Smuzhiyun 	    "        _pt_distance = pt_distance * source_texture_trans.z;\n"\
470*4882a593Smuzhiyun 	    "        \n"\
471*4882a593Smuzhiyun 	    "    } else if (hor_ver == 1) {\n"/*horizontal case.*/\
472*4882a593Smuzhiyun 	    "        distance = source_texture_trans.x;\n"\
473*4882a593Smuzhiyun 	    "        _p1_distance = p1_distance * source_texture_trans.z;\n"\
474*4882a593Smuzhiyun 	    "        _pt_distance = pt_distance * source_texture_trans.z;\n"\
475*4882a593Smuzhiyun 	    "    } \n"\
476*4882a593Smuzhiyun 	    "    \n"\
477*4882a593Smuzhiyun 	    "    distance = (distance - _p1_distance) / _pt_distance;\n"\
478*4882a593Smuzhiyun 	    "    \n"\
479*4882a593Smuzhiyun 	    "    if(repeat_type == %d){\n" /* repeat normal*/\
480*4882a593Smuzhiyun 	    "        distance = fract(distance);\n"\
481*4882a593Smuzhiyun 	    "    }\n"\
482*4882a593Smuzhiyun 	    "    \n"\
483*4882a593Smuzhiyun 	    "    if(repeat_type == %d) {\n" /* repeat reflect*/\
484*4882a593Smuzhiyun 	    "        distance = abs(fract(distance * 0.5 + 0.5) * 2.0 - 1.0);\n"\
485*4882a593Smuzhiyun 	    "    }\n"\
486*4882a593Smuzhiyun 	    "    \n"\
487*4882a593Smuzhiyun 	    "    return distance;\n"\
488*4882a593Smuzhiyun 	    "}\n"\
489*4882a593Smuzhiyun 	    "\n"\
490*4882a593Smuzhiyun 	    "void main()\n"\
491*4882a593Smuzhiyun 	    "{\n"\
492*4882a593Smuzhiyun 	    "    float stop_len = get_stop_len();\n"\
493*4882a593Smuzhiyun 	    "    gl_FragColor = get_color(stop_len);\n"\
494*4882a593Smuzhiyun 	    "}\n"\
495*4882a593Smuzhiyun 	    "\n"\
496*4882a593Smuzhiyun             "%s" /* fs_getcolor_source */
497*4882a593Smuzhiyun     char *fs_getcolor_source;
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun     glamor_priv = glamor_get_screen_private(screen);
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun     if ((glamor_priv->linear_max_nstops >= stops_count) && (dyn_gen)) {
502*4882a593Smuzhiyun         /* Very Good, not to generate again. */
503*4882a593Smuzhiyun         return;
504*4882a593Smuzhiyun     }
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun     glamor_make_current(glamor_priv);
507*4882a593Smuzhiyun     if (dyn_gen && glamor_priv->gradient_prog[SHADER_GRADIENT_LINEAR][2]) {
508*4882a593Smuzhiyun         glDeleteProgram(glamor_priv->gradient_prog[SHADER_GRADIENT_LINEAR][2]);
509*4882a593Smuzhiyun         glamor_priv->gradient_prog[SHADER_GRADIENT_LINEAR][2] = 0;
510*4882a593Smuzhiyun     }
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun     gradient_prog = glCreateProgram();
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun     vs_prog = glamor_compile_glsl_prog(GL_VERTEX_SHADER, gradient_vs);
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun     fs_getcolor_source =
517*4882a593Smuzhiyun         _glamor_create_getcolor_fs_source(screen, stops_count, stops_count > 0);
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun     XNFasprintf(&gradient_fs,
520*4882a593Smuzhiyun                 gradient_fs_template,
521*4882a593Smuzhiyun                 PIXMAN_REPEAT_NORMAL, PIXMAN_REPEAT_REFLECT,
522*4882a593Smuzhiyun                 fs_getcolor_source);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun     fs_prog = glamor_compile_glsl_prog(GL_FRAGMENT_SHADER, gradient_fs);
525*4882a593Smuzhiyun     free(gradient_fs);
526*4882a593Smuzhiyun     free(fs_getcolor_source);
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun     glAttachShader(gradient_prog, vs_prog);
529*4882a593Smuzhiyun     glAttachShader(gradient_prog, fs_prog);
530*4882a593Smuzhiyun     glDeleteShader(vs_prog);
531*4882a593Smuzhiyun     glDeleteShader(fs_prog);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun     glBindAttribLocation(gradient_prog, GLAMOR_VERTEX_POS, "v_position");
534*4882a593Smuzhiyun     glBindAttribLocation(gradient_prog, GLAMOR_VERTEX_SOURCE, "v_texcoord");
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun     glamor_link_glsl_prog(screen, gradient_prog, "linear gradient");
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun     if (dyn_gen) {
539*4882a593Smuzhiyun         index = 2;
540*4882a593Smuzhiyun         glamor_priv->linear_max_nstops = stops_count;
541*4882a593Smuzhiyun     }
542*4882a593Smuzhiyun     else if (stops_count) {
543*4882a593Smuzhiyun         index = 1;
544*4882a593Smuzhiyun     }
545*4882a593Smuzhiyun     else {
546*4882a593Smuzhiyun         index = 0;
547*4882a593Smuzhiyun     }
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun     glamor_priv->gradient_prog[SHADER_GRADIENT_LINEAR][index] = gradient_prog;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun void
glamor_init_gradient_shader(ScreenPtr screen)553*4882a593Smuzhiyun glamor_init_gradient_shader(ScreenPtr screen)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun     glamor_screen_private *glamor_priv;
556*4882a593Smuzhiyun     int i;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun     glamor_priv = glamor_get_screen_private(screen);
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun     for (i = 0; i < 3; i++) {
561*4882a593Smuzhiyun         glamor_priv->gradient_prog[SHADER_GRADIENT_LINEAR][i] = 0;
562*4882a593Smuzhiyun         glamor_priv->gradient_prog[SHADER_GRADIENT_RADIAL][i] = 0;
563*4882a593Smuzhiyun     }
564*4882a593Smuzhiyun     glamor_priv->linear_max_nstops = 0;
565*4882a593Smuzhiyun     glamor_priv->radial_max_nstops = 0;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun     _glamor_create_linear_gradient_program(screen, 0, 0);
568*4882a593Smuzhiyun     _glamor_create_linear_gradient_program(screen, LINEAR_LARGE_STOPS, 0);
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun     _glamor_create_radial_gradient_program(screen, 0, 0);
571*4882a593Smuzhiyun     _glamor_create_radial_gradient_program(screen, RADIAL_LARGE_STOPS, 0);
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun static void
_glamor_gradient_convert_trans_matrix(PictTransform * from,float to[3][3],int width,int height,int normalize)575*4882a593Smuzhiyun _glamor_gradient_convert_trans_matrix(PictTransform *from, float to[3][3],
576*4882a593Smuzhiyun                                       int width, int height, int normalize)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun     /*
579*4882a593Smuzhiyun      * Because in the shader program, we normalize all the pixel cood to [0, 1],
580*4882a593Smuzhiyun      * so with the transform matrix, the correct logic should be:
581*4882a593Smuzhiyun      * v_s = A*T*v
582*4882a593Smuzhiyun      * v_s: point vector in shader after normalized.
583*4882a593Smuzhiyun      * A: The transition matrix from   width X height --> 1.0 X 1.0
584*4882a593Smuzhiyun      * T: The transform matrix.
585*4882a593Smuzhiyun      * v: point vector in width X height space.
586*4882a593Smuzhiyun      *
587*4882a593Smuzhiyun      * result is OK if we use this fomula. But for every point in width X height space,
588*4882a593Smuzhiyun      * we can just use their normalized point vector in shader, namely we can just
589*4882a593Smuzhiyun      * use the result of A*v in shader. So we have no chance to insert T in A*v.
590*4882a593Smuzhiyun      * We can just convert v_s = A*T*v to v_s = A*T*inv(A)*A*v, where inv(A) is the
591*4882a593Smuzhiyun      * inverse matrix of A. Now, v_s = (A*T*inv(A)) * (A*v)
592*4882a593Smuzhiyun      * So, to get the correct v_s, we need to cacula1 the matrix: (A*T*inv(A)), and
593*4882a593Smuzhiyun      * we name this matrix T_s.
594*4882a593Smuzhiyun      *
595*4882a593Smuzhiyun      * Firstly, because A is for the scale conversion, we find
596*4882a593Smuzhiyun      *      --         --
597*4882a593Smuzhiyun      *      |1/w  0   0 |
598*4882a593Smuzhiyun      * A =  | 0  1/h  0 |
599*4882a593Smuzhiyun      *      | 0   0  1.0|
600*4882a593Smuzhiyun      *      --         --
601*4882a593Smuzhiyun      * so T_s = A*T*inv(a) and result
602*4882a593Smuzhiyun      *
603*4882a593Smuzhiyun      *       --                      --
604*4882a593Smuzhiyun      *       | t11      h*t12/w  t13/w|
605*4882a593Smuzhiyun      * T_s = | w*t21/h  t22      t23/h|
606*4882a593Smuzhiyun      *       | w*t31    h*t32    t33  |
607*4882a593Smuzhiyun      *       --                      --
608*4882a593Smuzhiyun      */
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun     to[0][0] = (float) pixman_fixed_to_double(from->matrix[0][0]);
611*4882a593Smuzhiyun     to[0][1] = (float) pixman_fixed_to_double(from->matrix[0][1])
612*4882a593Smuzhiyun         * (normalize ? (((float) height) / ((float) width)) : 1.0);
613*4882a593Smuzhiyun     to[0][2] = (float) pixman_fixed_to_double(from->matrix[0][2])
614*4882a593Smuzhiyun         / (normalize ? ((float) width) : 1.0);
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun     to[1][0] = (float) pixman_fixed_to_double(from->matrix[1][0])
617*4882a593Smuzhiyun         * (normalize ? (((float) width) / ((float) height)) : 1.0);
618*4882a593Smuzhiyun     to[1][1] = (float) pixman_fixed_to_double(from->matrix[1][1]);
619*4882a593Smuzhiyun     to[1][2] = (float) pixman_fixed_to_double(from->matrix[1][2])
620*4882a593Smuzhiyun         / (normalize ? ((float) height) : 1.0);
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun     to[2][0] = (float) pixman_fixed_to_double(from->matrix[2][0])
623*4882a593Smuzhiyun         * (normalize ? ((float) width) : 1.0);
624*4882a593Smuzhiyun     to[2][1] = (float) pixman_fixed_to_double(from->matrix[2][1])
625*4882a593Smuzhiyun         * (normalize ? ((float) height) : 1.0);
626*4882a593Smuzhiyun     to[2][2] = (float) pixman_fixed_to_double(from->matrix[2][2]);
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun     DEBUGF("the transform matrix is:\n%f\t%f\t%f\n%f\t%f\t%f\n%f\t%f\t%f\n",
629*4882a593Smuzhiyun            to[0][0], to[0][1], to[0][2],
630*4882a593Smuzhiyun            to[1][0], to[1][1], to[1][2], to[2][0], to[2][1], to[2][2]);
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun static int
_glamor_gradient_set_pixmap_destination(ScreenPtr screen,glamor_screen_private * glamor_priv,PicturePtr dst_picture,GLfloat * xscale,GLfloat * yscale,int x_source,int y_source,int tex_normalize)634*4882a593Smuzhiyun _glamor_gradient_set_pixmap_destination(ScreenPtr screen,
635*4882a593Smuzhiyun                                         glamor_screen_private *glamor_priv,
636*4882a593Smuzhiyun                                         PicturePtr dst_picture,
637*4882a593Smuzhiyun                                         GLfloat *xscale, GLfloat *yscale,
638*4882a593Smuzhiyun                                         int x_source, int y_source,
639*4882a593Smuzhiyun                                         int tex_normalize)
640*4882a593Smuzhiyun {
641*4882a593Smuzhiyun     glamor_pixmap_private *pixmap_priv;
642*4882a593Smuzhiyun     PixmapPtr pixmap = NULL;
643*4882a593Smuzhiyun     GLfloat *v;
644*4882a593Smuzhiyun     char *vbo_offset;
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun     pixmap = glamor_get_drawable_pixmap(dst_picture->pDrawable);
647*4882a593Smuzhiyun     pixmap_priv = glamor_get_pixmap_private(pixmap);
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun     if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv)) {     /* should always have here. */
650*4882a593Smuzhiyun         return 0;
651*4882a593Smuzhiyun     }
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun     glamor_set_destination_pixmap_priv_nc(glamor_priv, pixmap, pixmap_priv);
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun     pixmap_priv_get_dest_scale(pixmap, pixmap_priv, xscale, yscale);
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun     DEBUGF("xscale = %f, yscale = %f,"
658*4882a593Smuzhiyun            " x_source = %d, y_source = %d, width = %d, height = %d\n",
659*4882a593Smuzhiyun            *xscale, *yscale, x_source, y_source,
660*4882a593Smuzhiyun            dst_picture->pDrawable->width, dst_picture->pDrawable->height);
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun     v = glamor_get_vbo_space(screen, 16 * sizeof(GLfloat), &vbo_offset);
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun     glamor_set_normalize_vcoords_tri_strip(*xscale, *yscale,
665*4882a593Smuzhiyun                                            0, 0,
666*4882a593Smuzhiyun                                            (INT16) (dst_picture->pDrawable->
667*4882a593Smuzhiyun                                                     width),
668*4882a593Smuzhiyun                                            (INT16) (dst_picture->pDrawable->
669*4882a593Smuzhiyun                                                     height),
670*4882a593Smuzhiyun                                            v);
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun     if (tex_normalize) {
673*4882a593Smuzhiyun         glamor_set_normalize_tcoords_tri_stripe(*xscale, *yscale,
674*4882a593Smuzhiyun                                                 x_source, y_source,
675*4882a593Smuzhiyun                                                 (INT16) (dst_picture->
676*4882a593Smuzhiyun                                                          pDrawable->width +
677*4882a593Smuzhiyun                                                          x_source),
678*4882a593Smuzhiyun                                                 (INT16) (dst_picture->
679*4882a593Smuzhiyun                                                          pDrawable->height +
680*4882a593Smuzhiyun                                                          y_source),
681*4882a593Smuzhiyun                                                 &v[8]);
682*4882a593Smuzhiyun     }
683*4882a593Smuzhiyun     else {
684*4882a593Smuzhiyun         glamor_set_tcoords_tri_strip(x_source, y_source,
685*4882a593Smuzhiyun                                      (INT16) (dst_picture->pDrawable->width) +
686*4882a593Smuzhiyun                                      x_source,
687*4882a593Smuzhiyun                                      (INT16) (dst_picture->pDrawable->height) +
688*4882a593Smuzhiyun                                      y_source,
689*4882a593Smuzhiyun                                      &v[8]);
690*4882a593Smuzhiyun     }
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun     DEBUGF("vertices --> leftup : %f X %f, rightup: %f X %f,"
693*4882a593Smuzhiyun            "rightbottom: %f X %f, leftbottom : %f X %f\n",
694*4882a593Smuzhiyun            v[0], v[1], v[2], v[3],
695*4882a593Smuzhiyun            v[4], v[5], v[6], v[7]);
696*4882a593Smuzhiyun     DEBUGF("tex_vertices --> leftup : %f X %f, rightup: %f X %f,"
697*4882a593Smuzhiyun            "rightbottom: %f X %f, leftbottom : %f X %f\n",
698*4882a593Smuzhiyun            v[8], v[9], v[10], v[11],
699*4882a593Smuzhiyun            v[12], v[13], v[14], v[15]);
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun     glamor_make_current(glamor_priv);
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun     glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT,
704*4882a593Smuzhiyun                           GL_FALSE, 0, vbo_offset);
705*4882a593Smuzhiyun     glVertexAttribPointer(GLAMOR_VERTEX_SOURCE, 2, GL_FLOAT,
706*4882a593Smuzhiyun                           GL_FALSE, 0, vbo_offset + 8 * sizeof(GLfloat));
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun     glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
709*4882a593Smuzhiyun     glEnableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun     glamor_put_vbo_space(screen);
712*4882a593Smuzhiyun     return 1;
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun static int
_glamor_gradient_set_stops(PicturePtr src_picture,PictGradient * pgradient,GLfloat * stop_colors,GLfloat * n_stops)716*4882a593Smuzhiyun _glamor_gradient_set_stops(PicturePtr src_picture, PictGradient *pgradient,
717*4882a593Smuzhiyun                            GLfloat *stop_colors, GLfloat *n_stops)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun     int i;
720*4882a593Smuzhiyun     int count = 1;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun     for (i = 0; i < pgradient->nstops; i++) {
723*4882a593Smuzhiyun         stop_colors[count * 4] =
724*4882a593Smuzhiyun             pixman_fixed_to_double(pgradient->stops[i].color.red);
725*4882a593Smuzhiyun         stop_colors[count * 4 + 1] =
726*4882a593Smuzhiyun             pixman_fixed_to_double(pgradient->stops[i].color.green);
727*4882a593Smuzhiyun         stop_colors[count * 4 + 2] =
728*4882a593Smuzhiyun             pixman_fixed_to_double(pgradient->stops[i].color.blue);
729*4882a593Smuzhiyun         stop_colors[count * 4 + 3] =
730*4882a593Smuzhiyun             pixman_fixed_to_double(pgradient->stops[i].color.alpha);
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun         n_stops[count] =
733*4882a593Smuzhiyun             (GLfloat) pixman_fixed_to_double(pgradient->stops[i].x);
734*4882a593Smuzhiyun         count++;
735*4882a593Smuzhiyun     }
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun     /* for the end stop. */
738*4882a593Smuzhiyun     count++;
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun     switch (src_picture->repeatType) {
741*4882a593Smuzhiyun #define REPEAT_FILL_STOPS(m, n) \
742*4882a593Smuzhiyun 			stop_colors[(m)*4 + 0] = stop_colors[(n)*4 + 0]; \
743*4882a593Smuzhiyun 			stop_colors[(m)*4 + 1] = stop_colors[(n)*4 + 1]; \
744*4882a593Smuzhiyun 			stop_colors[(m)*4 + 2] = stop_colors[(n)*4 + 2]; \
745*4882a593Smuzhiyun 			stop_colors[(m)*4 + 3] = stop_colors[(n)*4 + 3];
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun     default:
748*4882a593Smuzhiyun     case PIXMAN_REPEAT_NONE:
749*4882a593Smuzhiyun         stop_colors[0] = 0.0;   //R
750*4882a593Smuzhiyun         stop_colors[1] = 0.0;   //G
751*4882a593Smuzhiyun         stop_colors[2] = 0.0;   //B
752*4882a593Smuzhiyun         stop_colors[3] = 0.0;   //Alpha
753*4882a593Smuzhiyun         n_stops[0] = n_stops[1];
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun         stop_colors[0 + (count - 1) * 4] = 0.0; //R
756*4882a593Smuzhiyun         stop_colors[1 + (count - 1) * 4] = 0.0; //G
757*4882a593Smuzhiyun         stop_colors[2 + (count - 1) * 4] = 0.0; //B
758*4882a593Smuzhiyun         stop_colors[3 + (count - 1) * 4] = 0.0; //Alpha
759*4882a593Smuzhiyun         n_stops[count - 1] = n_stops[count - 2];
760*4882a593Smuzhiyun         break;
761*4882a593Smuzhiyun     case PIXMAN_REPEAT_NORMAL:
762*4882a593Smuzhiyun         REPEAT_FILL_STOPS(0, count - 2);
763*4882a593Smuzhiyun         n_stops[0] = n_stops[count - 2] - 1.0;
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun         REPEAT_FILL_STOPS(count - 1, 1);
766*4882a593Smuzhiyun         n_stops[count - 1] = n_stops[1] + 1.0;
767*4882a593Smuzhiyun         break;
768*4882a593Smuzhiyun     case PIXMAN_REPEAT_REFLECT:
769*4882a593Smuzhiyun         REPEAT_FILL_STOPS(0, 1);
770*4882a593Smuzhiyun         n_stops[0] = -n_stops[1];
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun         REPEAT_FILL_STOPS(count - 1, count - 2);
773*4882a593Smuzhiyun         n_stops[count - 1] = 1.0 + 1.0 - n_stops[count - 2];
774*4882a593Smuzhiyun         break;
775*4882a593Smuzhiyun     case PIXMAN_REPEAT_PAD:
776*4882a593Smuzhiyun         REPEAT_FILL_STOPS(0, 1);
777*4882a593Smuzhiyun         n_stops[0] = -(float) INT_MAX;
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun         REPEAT_FILL_STOPS(count - 1, count - 2);
780*4882a593Smuzhiyun         n_stops[count - 1] = (float) INT_MAX;
781*4882a593Smuzhiyun         break;
782*4882a593Smuzhiyun #undef REPEAT_FILL_STOPS
783*4882a593Smuzhiyun     }
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun     for (i = 0; i < count; i++) {
786*4882a593Smuzhiyun         DEBUGF("n_stops[%d] = %f, color = r:%f g:%f b:%f a:%f\n",
787*4882a593Smuzhiyun                i, n_stops[i],
788*4882a593Smuzhiyun                stop_colors[i * 4], stop_colors[i * 4 + 1],
789*4882a593Smuzhiyun                stop_colors[i * 4 + 2], stop_colors[i * 4 + 3]);
790*4882a593Smuzhiyun     }
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun     return count;
793*4882a593Smuzhiyun }
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun PicturePtr
glamor_generate_radial_gradient_picture(ScreenPtr screen,PicturePtr src_picture,int x_source,int y_source,int width,int height,PictFormatShort format)796*4882a593Smuzhiyun glamor_generate_radial_gradient_picture(ScreenPtr screen,
797*4882a593Smuzhiyun                                         PicturePtr src_picture,
798*4882a593Smuzhiyun                                         int x_source, int y_source,
799*4882a593Smuzhiyun                                         int width, int height,
800*4882a593Smuzhiyun                                         PictFormatShort format)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun     glamor_screen_private *glamor_priv;
803*4882a593Smuzhiyun     PicturePtr dst_picture = NULL;
804*4882a593Smuzhiyun     PixmapPtr pixmap = NULL;
805*4882a593Smuzhiyun     GLint gradient_prog = 0;
806*4882a593Smuzhiyun     int error;
807*4882a593Smuzhiyun     int stops_count = 0;
808*4882a593Smuzhiyun     int count = 0;
809*4882a593Smuzhiyun     GLfloat *stop_colors = NULL;
810*4882a593Smuzhiyun     GLfloat *n_stops = NULL;
811*4882a593Smuzhiyun     GLfloat xscale, yscale;
812*4882a593Smuzhiyun     float transform_mat[3][3];
813*4882a593Smuzhiyun     static const float identity_mat[3][3] = { {1.0, 0.0, 0.0},
814*4882a593Smuzhiyun     {0.0, 1.0, 0.0},
815*4882a593Smuzhiyun     {0.0, 0.0, 1.0}
816*4882a593Smuzhiyun     };
817*4882a593Smuzhiyun     GLfloat stop_colors_st[RADIAL_SMALL_STOPS * 4];
818*4882a593Smuzhiyun     GLfloat n_stops_st[RADIAL_SMALL_STOPS];
819*4882a593Smuzhiyun     GLfloat A_value;
820*4882a593Smuzhiyun     GLfloat cxy[4];
821*4882a593Smuzhiyun     float c1x, c1y, c2x, c2y, r1, r2;
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun     GLint transform_mat_uniform_location = 0;
824*4882a593Smuzhiyun     GLint repeat_type_uniform_location = 0;
825*4882a593Smuzhiyun     GLint n_stop_uniform_location = 0;
826*4882a593Smuzhiyun     GLint stops_uniform_location = 0;
827*4882a593Smuzhiyun     GLint stop_colors_uniform_location = 0;
828*4882a593Smuzhiyun     GLint stop0_uniform_location = 0;
829*4882a593Smuzhiyun     GLint stop1_uniform_location = 0;
830*4882a593Smuzhiyun     GLint stop2_uniform_location = 0;
831*4882a593Smuzhiyun     GLint stop3_uniform_location = 0;
832*4882a593Smuzhiyun     GLint stop4_uniform_location = 0;
833*4882a593Smuzhiyun     GLint stop5_uniform_location = 0;
834*4882a593Smuzhiyun     GLint stop6_uniform_location = 0;
835*4882a593Smuzhiyun     GLint stop7_uniform_location = 0;
836*4882a593Smuzhiyun     GLint stop_color0_uniform_location = 0;
837*4882a593Smuzhiyun     GLint stop_color1_uniform_location = 0;
838*4882a593Smuzhiyun     GLint stop_color2_uniform_location = 0;
839*4882a593Smuzhiyun     GLint stop_color3_uniform_location = 0;
840*4882a593Smuzhiyun     GLint stop_color4_uniform_location = 0;
841*4882a593Smuzhiyun     GLint stop_color5_uniform_location = 0;
842*4882a593Smuzhiyun     GLint stop_color6_uniform_location = 0;
843*4882a593Smuzhiyun     GLint stop_color7_uniform_location = 0;
844*4882a593Smuzhiyun     GLint A_value_uniform_location = 0;
845*4882a593Smuzhiyun     GLint c1_uniform_location = 0;
846*4882a593Smuzhiyun     GLint r1_uniform_location = 0;
847*4882a593Smuzhiyun     GLint c2_uniform_location = 0;
848*4882a593Smuzhiyun     GLint r2_uniform_location = 0;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun     glamor_priv = glamor_get_screen_private(screen);
851*4882a593Smuzhiyun     glamor_make_current(glamor_priv);
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun     /* Create a pixmap with VBO. */
854*4882a593Smuzhiyun     pixmap = glamor_create_pixmap(screen,
855*4882a593Smuzhiyun                                   width, height,
856*4882a593Smuzhiyun                                   PIXMAN_FORMAT_DEPTH(format), 0);
857*4882a593Smuzhiyun     if (!pixmap)
858*4882a593Smuzhiyun         goto GRADIENT_FAIL;
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun     dst_picture = CreatePicture(0, &pixmap->drawable,
861*4882a593Smuzhiyun                                 PictureMatchFormat(screen,
862*4882a593Smuzhiyun                                                    PIXMAN_FORMAT_DEPTH(format),
863*4882a593Smuzhiyun                                                    format), 0, 0, serverClient,
864*4882a593Smuzhiyun                                 &error);
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun     /* Release the reference, picture will hold the last one. */
867*4882a593Smuzhiyun     glamor_destroy_pixmap(pixmap);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun     if (!dst_picture)
870*4882a593Smuzhiyun         goto GRADIENT_FAIL;
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun     ValidatePicture(dst_picture);
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun     stops_count = src_picture->pSourcePict->radial.nstops + 2;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun     /* Because the max value of nstops is unknown, so create a program
877*4882a593Smuzhiyun        when nstops > LINEAR_LARGE_STOPS. */
878*4882a593Smuzhiyun     if (stops_count <= RADIAL_SMALL_STOPS) {
879*4882a593Smuzhiyun         gradient_prog = glamor_priv->gradient_prog[SHADER_GRADIENT_RADIAL][0];
880*4882a593Smuzhiyun     }
881*4882a593Smuzhiyun     else if (stops_count <= RADIAL_LARGE_STOPS) {
882*4882a593Smuzhiyun         gradient_prog = glamor_priv->gradient_prog[SHADER_GRADIENT_RADIAL][1];
883*4882a593Smuzhiyun     }
884*4882a593Smuzhiyun     else {
885*4882a593Smuzhiyun         _glamor_create_radial_gradient_program(screen,
886*4882a593Smuzhiyun                                                src_picture->pSourcePict->linear.
887*4882a593Smuzhiyun                                                nstops + 2, 1);
888*4882a593Smuzhiyun         gradient_prog = glamor_priv->gradient_prog[SHADER_GRADIENT_RADIAL][2];
889*4882a593Smuzhiyun     }
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun     /* Bind all the uniform vars . */
892*4882a593Smuzhiyun     transform_mat_uniform_location = glGetUniformLocation(gradient_prog,
893*4882a593Smuzhiyun                                                           "transform_mat");
894*4882a593Smuzhiyun     repeat_type_uniform_location = glGetUniformLocation(gradient_prog,
895*4882a593Smuzhiyun                                                         "repeat_type");
896*4882a593Smuzhiyun     n_stop_uniform_location = glGetUniformLocation(gradient_prog, "n_stop");
897*4882a593Smuzhiyun     A_value_uniform_location = glGetUniformLocation(gradient_prog, "A_value");
898*4882a593Smuzhiyun     c1_uniform_location = glGetUniformLocation(gradient_prog, "c1");
899*4882a593Smuzhiyun     r1_uniform_location = glGetUniformLocation(gradient_prog, "r1");
900*4882a593Smuzhiyun     c2_uniform_location = glGetUniformLocation(gradient_prog, "c2");
901*4882a593Smuzhiyun     r2_uniform_location = glGetUniformLocation(gradient_prog, "r2");
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun     if (src_picture->pSourcePict->radial.nstops + 2 <= RADIAL_SMALL_STOPS) {
904*4882a593Smuzhiyun         stop0_uniform_location =
905*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop0");
906*4882a593Smuzhiyun         stop1_uniform_location =
907*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop1");
908*4882a593Smuzhiyun         stop2_uniform_location =
909*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop2");
910*4882a593Smuzhiyun         stop3_uniform_location =
911*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop3");
912*4882a593Smuzhiyun         stop4_uniform_location =
913*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop4");
914*4882a593Smuzhiyun         stop5_uniform_location =
915*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop5");
916*4882a593Smuzhiyun         stop6_uniform_location =
917*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop6");
918*4882a593Smuzhiyun         stop7_uniform_location =
919*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop7");
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun         stop_color0_uniform_location =
922*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color0");
923*4882a593Smuzhiyun         stop_color1_uniform_location =
924*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color1");
925*4882a593Smuzhiyun         stop_color2_uniform_location =
926*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color2");
927*4882a593Smuzhiyun         stop_color3_uniform_location =
928*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color3");
929*4882a593Smuzhiyun         stop_color4_uniform_location =
930*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color4");
931*4882a593Smuzhiyun         stop_color5_uniform_location =
932*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color5");
933*4882a593Smuzhiyun         stop_color6_uniform_location =
934*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color6");
935*4882a593Smuzhiyun         stop_color7_uniform_location =
936*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color7");
937*4882a593Smuzhiyun     }
938*4882a593Smuzhiyun     else {
939*4882a593Smuzhiyun         stops_uniform_location =
940*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stops");
941*4882a593Smuzhiyun         stop_colors_uniform_location =
942*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_colors");
943*4882a593Smuzhiyun     }
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun     glUseProgram(gradient_prog);
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun     glUniform1i(repeat_type_uniform_location, src_picture->repeatType);
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun     if (src_picture->transform) {
950*4882a593Smuzhiyun         _glamor_gradient_convert_trans_matrix(src_picture->transform,
951*4882a593Smuzhiyun                                               transform_mat, width, height, 0);
952*4882a593Smuzhiyun         glUniformMatrix3fv(transform_mat_uniform_location,
953*4882a593Smuzhiyun                            1, 1, &transform_mat[0][0]);
954*4882a593Smuzhiyun     }
955*4882a593Smuzhiyun     else {
956*4882a593Smuzhiyun         glUniformMatrix3fv(transform_mat_uniform_location,
957*4882a593Smuzhiyun                            1, 1, &identity_mat[0][0]);
958*4882a593Smuzhiyun     }
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun     if (!_glamor_gradient_set_pixmap_destination
961*4882a593Smuzhiyun         (screen, glamor_priv, dst_picture, &xscale, &yscale, x_source, y_source,
962*4882a593Smuzhiyun          0))
963*4882a593Smuzhiyun         goto GRADIENT_FAIL;
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun     glamor_set_alu(screen, GXcopy);
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun     /* Set all the stops and colors to shader. */
968*4882a593Smuzhiyun     if (stops_count > RADIAL_SMALL_STOPS) {
969*4882a593Smuzhiyun         stop_colors = xallocarray(stops_count, 4 * sizeof(float));
970*4882a593Smuzhiyun         if (stop_colors == NULL) {
971*4882a593Smuzhiyun             ErrorF("Failed to allocate stop_colors memory.\n");
972*4882a593Smuzhiyun             goto GRADIENT_FAIL;
973*4882a593Smuzhiyun         }
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun         n_stops = xallocarray(stops_count, sizeof(float));
976*4882a593Smuzhiyun         if (n_stops == NULL) {
977*4882a593Smuzhiyun             ErrorF("Failed to allocate n_stops memory.\n");
978*4882a593Smuzhiyun             goto GRADIENT_FAIL;
979*4882a593Smuzhiyun         }
980*4882a593Smuzhiyun     }
981*4882a593Smuzhiyun     else {
982*4882a593Smuzhiyun         stop_colors = stop_colors_st;
983*4882a593Smuzhiyun         n_stops = n_stops_st;
984*4882a593Smuzhiyun     }
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun     count =
987*4882a593Smuzhiyun         _glamor_gradient_set_stops(src_picture,
988*4882a593Smuzhiyun                                    &src_picture->pSourcePict->gradient,
989*4882a593Smuzhiyun                                    stop_colors, n_stops);
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun     if (src_picture->pSourcePict->linear.nstops + 2 <= RADIAL_SMALL_STOPS) {
992*4882a593Smuzhiyun         int j = 0;
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun         glUniform4f(stop_color0_uniform_location,
995*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
996*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
997*4882a593Smuzhiyun         j++;
998*4882a593Smuzhiyun         glUniform4f(stop_color1_uniform_location,
999*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1000*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1001*4882a593Smuzhiyun         j++;
1002*4882a593Smuzhiyun         glUniform4f(stop_color2_uniform_location,
1003*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1004*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1005*4882a593Smuzhiyun         j++;
1006*4882a593Smuzhiyun         glUniform4f(stop_color3_uniform_location,
1007*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1008*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1009*4882a593Smuzhiyun         j++;
1010*4882a593Smuzhiyun         glUniform4f(stop_color4_uniform_location,
1011*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1012*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1013*4882a593Smuzhiyun         j++;
1014*4882a593Smuzhiyun         glUniform4f(stop_color5_uniform_location,
1015*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1016*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1017*4882a593Smuzhiyun         j++;
1018*4882a593Smuzhiyun         glUniform4f(stop_color6_uniform_location,
1019*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1020*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1021*4882a593Smuzhiyun         j++;
1022*4882a593Smuzhiyun         glUniform4f(stop_color7_uniform_location,
1023*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1024*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun         j = 0;
1027*4882a593Smuzhiyun         glUniform1f(stop0_uniform_location, n_stops[j++]);
1028*4882a593Smuzhiyun         glUniform1f(stop1_uniform_location, n_stops[j++]);
1029*4882a593Smuzhiyun         glUniform1f(stop2_uniform_location, n_stops[j++]);
1030*4882a593Smuzhiyun         glUniform1f(stop3_uniform_location, n_stops[j++]);
1031*4882a593Smuzhiyun         glUniform1f(stop4_uniform_location, n_stops[j++]);
1032*4882a593Smuzhiyun         glUniform1f(stop5_uniform_location, n_stops[j++]);
1033*4882a593Smuzhiyun         glUniform1f(stop6_uniform_location, n_stops[j++]);
1034*4882a593Smuzhiyun         glUniform1f(stop7_uniform_location, n_stops[j++]);
1035*4882a593Smuzhiyun         glUniform1i(n_stop_uniform_location, count);
1036*4882a593Smuzhiyun     }
1037*4882a593Smuzhiyun     else {
1038*4882a593Smuzhiyun         glUniform4fv(stop_colors_uniform_location, count, stop_colors);
1039*4882a593Smuzhiyun         glUniform1fv(stops_uniform_location, count, n_stops);
1040*4882a593Smuzhiyun         glUniform1i(n_stop_uniform_location, count);
1041*4882a593Smuzhiyun     }
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun     c1x = (float) pixman_fixed_to_double(src_picture->pSourcePict->radial.c1.x);
1044*4882a593Smuzhiyun     c1y = (float) pixman_fixed_to_double(src_picture->pSourcePict->radial.c1.y);
1045*4882a593Smuzhiyun     c2x = (float) pixman_fixed_to_double(src_picture->pSourcePict->radial.c2.x);
1046*4882a593Smuzhiyun     c2y = (float) pixman_fixed_to_double(src_picture->pSourcePict->radial.c2.y);
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun     r1 = (float) pixman_fixed_to_double(src_picture->pSourcePict->radial.c1.
1049*4882a593Smuzhiyun                                         radius);
1050*4882a593Smuzhiyun     r2 = (float) pixman_fixed_to_double(src_picture->pSourcePict->radial.c2.
1051*4882a593Smuzhiyun                                         radius);
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun     glamor_set_circle_centre(width, height, c1x, c1y, cxy);
1054*4882a593Smuzhiyun     glUniform2fv(c1_uniform_location, 1, cxy);
1055*4882a593Smuzhiyun     glUniform1f(r1_uniform_location, r1);
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun     glamor_set_circle_centre(width, height, c2x, c2y, cxy);
1058*4882a593Smuzhiyun     glUniform2fv(c2_uniform_location, 1, cxy);
1059*4882a593Smuzhiyun     glUniform1f(r2_uniform_location, r2);
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun     A_value =
1062*4882a593Smuzhiyun         (c2x - c1x) * (c2x - c1x) + (c2y - c1y) * (c2y - c1y) - (r2 -
1063*4882a593Smuzhiyun                                                                  r1) * (r2 -
1064*4882a593Smuzhiyun                                                                         r1);
1065*4882a593Smuzhiyun     glUniform1f(A_value_uniform_location, A_value);
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun     DEBUGF("C1:(%f, %f) R1:%f\nC2:(%f, %f) R2:%f\nA = %f\n",
1068*4882a593Smuzhiyun            c1x, c1y, r1, c2x, c2y, r2, A_value);
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun     /* Now rendering. */
1071*4882a593Smuzhiyun     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun     /* Do the clear logic. */
1074*4882a593Smuzhiyun     if (stops_count > RADIAL_SMALL_STOPS) {
1075*4882a593Smuzhiyun         free(n_stops);
1076*4882a593Smuzhiyun         free(stop_colors);
1077*4882a593Smuzhiyun     }
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
1080*4882a593Smuzhiyun     glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun     glamor_pixmap_invalid(pixmap);
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun     return dst_picture;
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun  GRADIENT_FAIL:
1087*4882a593Smuzhiyun     if (dst_picture) {
1088*4882a593Smuzhiyun         FreePicture(dst_picture, 0);
1089*4882a593Smuzhiyun     }
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun     if (stops_count > RADIAL_SMALL_STOPS) {
1092*4882a593Smuzhiyun         if (n_stops)
1093*4882a593Smuzhiyun             free(n_stops);
1094*4882a593Smuzhiyun         if (stop_colors)
1095*4882a593Smuzhiyun             free(stop_colors);
1096*4882a593Smuzhiyun     }
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
1099*4882a593Smuzhiyun     glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
1100*4882a593Smuzhiyun     return NULL;
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun PicturePtr
glamor_generate_linear_gradient_picture(ScreenPtr screen,PicturePtr src_picture,int x_source,int y_source,int width,int height,PictFormatShort format)1104*4882a593Smuzhiyun glamor_generate_linear_gradient_picture(ScreenPtr screen,
1105*4882a593Smuzhiyun                                         PicturePtr src_picture,
1106*4882a593Smuzhiyun                                         int x_source, int y_source,
1107*4882a593Smuzhiyun                                         int width, int height,
1108*4882a593Smuzhiyun                                         PictFormatShort format)
1109*4882a593Smuzhiyun {
1110*4882a593Smuzhiyun     glamor_screen_private *glamor_priv;
1111*4882a593Smuzhiyun     PicturePtr dst_picture = NULL;
1112*4882a593Smuzhiyun     PixmapPtr pixmap = NULL;
1113*4882a593Smuzhiyun     GLint gradient_prog = 0;
1114*4882a593Smuzhiyun     int error;
1115*4882a593Smuzhiyun     float pt_distance;
1116*4882a593Smuzhiyun     float p1_distance;
1117*4882a593Smuzhiyun     GLfloat cos_val;
1118*4882a593Smuzhiyun     int stops_count = 0;
1119*4882a593Smuzhiyun     GLfloat *stop_colors = NULL;
1120*4882a593Smuzhiyun     GLfloat *n_stops = NULL;
1121*4882a593Smuzhiyun     int count = 0;
1122*4882a593Smuzhiyun     float slope;
1123*4882a593Smuzhiyun     GLfloat xscale, yscale;
1124*4882a593Smuzhiyun     GLfloat pt1[2], pt2[2];
1125*4882a593Smuzhiyun     float transform_mat[3][3];
1126*4882a593Smuzhiyun     static const float identity_mat[3][3] = { {1.0, 0.0, 0.0},
1127*4882a593Smuzhiyun     {0.0, 1.0, 0.0},
1128*4882a593Smuzhiyun     {0.0, 0.0, 1.0}
1129*4882a593Smuzhiyun     };
1130*4882a593Smuzhiyun     GLfloat stop_colors_st[LINEAR_SMALL_STOPS * 4];
1131*4882a593Smuzhiyun     GLfloat n_stops_st[LINEAR_SMALL_STOPS];
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun     GLint transform_mat_uniform_location = 0;
1134*4882a593Smuzhiyun     GLint n_stop_uniform_location = 0;
1135*4882a593Smuzhiyun     GLint stops_uniform_location = 0;
1136*4882a593Smuzhiyun     GLint stop0_uniform_location = 0;
1137*4882a593Smuzhiyun     GLint stop1_uniform_location = 0;
1138*4882a593Smuzhiyun     GLint stop2_uniform_location = 0;
1139*4882a593Smuzhiyun     GLint stop3_uniform_location = 0;
1140*4882a593Smuzhiyun     GLint stop4_uniform_location = 0;
1141*4882a593Smuzhiyun     GLint stop5_uniform_location = 0;
1142*4882a593Smuzhiyun     GLint stop6_uniform_location = 0;
1143*4882a593Smuzhiyun     GLint stop7_uniform_location = 0;
1144*4882a593Smuzhiyun     GLint stop_colors_uniform_location = 0;
1145*4882a593Smuzhiyun     GLint stop_color0_uniform_location = 0;
1146*4882a593Smuzhiyun     GLint stop_color1_uniform_location = 0;
1147*4882a593Smuzhiyun     GLint stop_color2_uniform_location = 0;
1148*4882a593Smuzhiyun     GLint stop_color3_uniform_location = 0;
1149*4882a593Smuzhiyun     GLint stop_color4_uniform_location = 0;
1150*4882a593Smuzhiyun     GLint stop_color5_uniform_location = 0;
1151*4882a593Smuzhiyun     GLint stop_color6_uniform_location = 0;
1152*4882a593Smuzhiyun     GLint stop_color7_uniform_location = 0;
1153*4882a593Smuzhiyun     GLint pt_slope_uniform_location = 0;
1154*4882a593Smuzhiyun     GLint repeat_type_uniform_location = 0;
1155*4882a593Smuzhiyun     GLint hor_ver_uniform_location = 0;
1156*4882a593Smuzhiyun     GLint cos_val_uniform_location = 0;
1157*4882a593Smuzhiyun     GLint p1_distance_uniform_location = 0;
1158*4882a593Smuzhiyun     GLint pt_distance_uniform_location = 0;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun     glamor_priv = glamor_get_screen_private(screen);
1161*4882a593Smuzhiyun     glamor_make_current(glamor_priv);
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun     /* Create a pixmap with VBO. */
1164*4882a593Smuzhiyun     pixmap = glamor_create_pixmap(screen,
1165*4882a593Smuzhiyun                                   width, height,
1166*4882a593Smuzhiyun                                   PIXMAN_FORMAT_DEPTH(format), 0);
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun     if (!pixmap)
1169*4882a593Smuzhiyun         goto GRADIENT_FAIL;
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun     dst_picture = CreatePicture(0, &pixmap->drawable,
1172*4882a593Smuzhiyun                                 PictureMatchFormat(screen,
1173*4882a593Smuzhiyun                                                    PIXMAN_FORMAT_DEPTH(format),
1174*4882a593Smuzhiyun                                                    format), 0, 0, serverClient,
1175*4882a593Smuzhiyun                                 &error);
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun     /* Release the reference, picture will hold the last one. */
1178*4882a593Smuzhiyun     glamor_destroy_pixmap(pixmap);
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun     if (!dst_picture)
1181*4882a593Smuzhiyun         goto GRADIENT_FAIL;
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun     ValidatePicture(dst_picture);
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun     stops_count = src_picture->pSourcePict->linear.nstops + 2;
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun     /* Because the max value of nstops is unknown, so create a program
1188*4882a593Smuzhiyun        when nstops > LINEAR_LARGE_STOPS. */
1189*4882a593Smuzhiyun     if (stops_count <= LINEAR_SMALL_STOPS) {
1190*4882a593Smuzhiyun         gradient_prog = glamor_priv->gradient_prog[SHADER_GRADIENT_LINEAR][0];
1191*4882a593Smuzhiyun     }
1192*4882a593Smuzhiyun     else if (stops_count <= LINEAR_LARGE_STOPS) {
1193*4882a593Smuzhiyun         gradient_prog = glamor_priv->gradient_prog[SHADER_GRADIENT_LINEAR][1];
1194*4882a593Smuzhiyun     }
1195*4882a593Smuzhiyun     else {
1196*4882a593Smuzhiyun         _glamor_create_linear_gradient_program(screen,
1197*4882a593Smuzhiyun                                                src_picture->pSourcePict->linear.
1198*4882a593Smuzhiyun                                                nstops + 2, 1);
1199*4882a593Smuzhiyun         gradient_prog = glamor_priv->gradient_prog[SHADER_GRADIENT_LINEAR][2];
1200*4882a593Smuzhiyun     }
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun     /* Bind all the uniform vars . */
1203*4882a593Smuzhiyun     n_stop_uniform_location =
1204*4882a593Smuzhiyun         glGetUniformLocation(gradient_prog, "n_stop");
1205*4882a593Smuzhiyun     pt_slope_uniform_location =
1206*4882a593Smuzhiyun         glGetUniformLocation(gradient_prog, "pt_slope");
1207*4882a593Smuzhiyun     repeat_type_uniform_location =
1208*4882a593Smuzhiyun         glGetUniformLocation(gradient_prog, "repeat_type");
1209*4882a593Smuzhiyun     hor_ver_uniform_location =
1210*4882a593Smuzhiyun         glGetUniformLocation(gradient_prog, "hor_ver");
1211*4882a593Smuzhiyun     transform_mat_uniform_location =
1212*4882a593Smuzhiyun         glGetUniformLocation(gradient_prog, "transform_mat");
1213*4882a593Smuzhiyun     cos_val_uniform_location =
1214*4882a593Smuzhiyun         glGetUniformLocation(gradient_prog, "cos_val");
1215*4882a593Smuzhiyun     p1_distance_uniform_location =
1216*4882a593Smuzhiyun         glGetUniformLocation(gradient_prog, "p1_distance");
1217*4882a593Smuzhiyun     pt_distance_uniform_location =
1218*4882a593Smuzhiyun         glGetUniformLocation(gradient_prog, "pt_distance");
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun     if (src_picture->pSourcePict->linear.nstops + 2 <= LINEAR_SMALL_STOPS) {
1221*4882a593Smuzhiyun         stop0_uniform_location =
1222*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop0");
1223*4882a593Smuzhiyun         stop1_uniform_location =
1224*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop1");
1225*4882a593Smuzhiyun         stop2_uniform_location =
1226*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop2");
1227*4882a593Smuzhiyun         stop3_uniform_location =
1228*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop3");
1229*4882a593Smuzhiyun         stop4_uniform_location =
1230*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop4");
1231*4882a593Smuzhiyun         stop5_uniform_location =
1232*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop5");
1233*4882a593Smuzhiyun         stop6_uniform_location =
1234*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop6");
1235*4882a593Smuzhiyun         stop7_uniform_location =
1236*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop7");
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun         stop_color0_uniform_location =
1239*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color0");
1240*4882a593Smuzhiyun         stop_color1_uniform_location =
1241*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color1");
1242*4882a593Smuzhiyun         stop_color2_uniform_location =
1243*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color2");
1244*4882a593Smuzhiyun         stop_color3_uniform_location =
1245*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color3");
1246*4882a593Smuzhiyun         stop_color4_uniform_location =
1247*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color4");
1248*4882a593Smuzhiyun         stop_color5_uniform_location =
1249*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color5");
1250*4882a593Smuzhiyun         stop_color6_uniform_location =
1251*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color6");
1252*4882a593Smuzhiyun         stop_color7_uniform_location =
1253*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_color7");
1254*4882a593Smuzhiyun     }
1255*4882a593Smuzhiyun     else {
1256*4882a593Smuzhiyun         stops_uniform_location =
1257*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stops");
1258*4882a593Smuzhiyun         stop_colors_uniform_location =
1259*4882a593Smuzhiyun             glGetUniformLocation(gradient_prog, "stop_colors");
1260*4882a593Smuzhiyun     }
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun     glUseProgram(gradient_prog);
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun     glUniform1i(repeat_type_uniform_location, src_picture->repeatType);
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun     /* set the transform matrix. */
1267*4882a593Smuzhiyun     if (src_picture->transform) {
1268*4882a593Smuzhiyun         _glamor_gradient_convert_trans_matrix(src_picture->transform,
1269*4882a593Smuzhiyun                                               transform_mat, width, height, 1);
1270*4882a593Smuzhiyun         glUniformMatrix3fv(transform_mat_uniform_location,
1271*4882a593Smuzhiyun                            1, 1, &transform_mat[0][0]);
1272*4882a593Smuzhiyun     }
1273*4882a593Smuzhiyun     else {
1274*4882a593Smuzhiyun         glUniformMatrix3fv(transform_mat_uniform_location,
1275*4882a593Smuzhiyun                            1, 1, &identity_mat[0][0]);
1276*4882a593Smuzhiyun     }
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun     if (!_glamor_gradient_set_pixmap_destination
1279*4882a593Smuzhiyun         (screen, glamor_priv, dst_picture, &xscale, &yscale, x_source, y_source,
1280*4882a593Smuzhiyun          1))
1281*4882a593Smuzhiyun         goto GRADIENT_FAIL;
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun     glamor_set_alu(screen, GXcopy);
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun     /* Normalize the PTs. */
1286*4882a593Smuzhiyun     glamor_set_normalize_pt(xscale, yscale,
1287*4882a593Smuzhiyun                             pixman_fixed_to_double(src_picture->pSourcePict->
1288*4882a593Smuzhiyun                                                    linear.p1.x),
1289*4882a593Smuzhiyun                             pixman_fixed_to_double(src_picture->pSourcePict->
1290*4882a593Smuzhiyun                                                    linear.p1.y),
1291*4882a593Smuzhiyun                             pt1);
1292*4882a593Smuzhiyun     DEBUGF("pt1:(%f, %f) ---> (%f %f)\n",
1293*4882a593Smuzhiyun            pixman_fixed_to_double(src_picture->pSourcePict->linear.p1.x),
1294*4882a593Smuzhiyun            pixman_fixed_to_double(src_picture->pSourcePict->linear.p1.y),
1295*4882a593Smuzhiyun            pt1[0], pt1[1]);
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun     glamor_set_normalize_pt(xscale, yscale,
1298*4882a593Smuzhiyun                             pixman_fixed_to_double(src_picture->pSourcePict->
1299*4882a593Smuzhiyun                                                    linear.p2.x),
1300*4882a593Smuzhiyun                             pixman_fixed_to_double(src_picture->pSourcePict->
1301*4882a593Smuzhiyun                                                    linear.p2.y),
1302*4882a593Smuzhiyun                             pt2);
1303*4882a593Smuzhiyun     DEBUGF("pt2:(%f, %f) ---> (%f %f)\n",
1304*4882a593Smuzhiyun            pixman_fixed_to_double(src_picture->pSourcePict->linear.p2.x),
1305*4882a593Smuzhiyun            pixman_fixed_to_double(src_picture->pSourcePict->linear.p2.y),
1306*4882a593Smuzhiyun            pt2[0], pt2[1]);
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun     /* Set all the stops and colors to shader. */
1309*4882a593Smuzhiyun     if (stops_count > LINEAR_SMALL_STOPS) {
1310*4882a593Smuzhiyun         stop_colors = xallocarray(stops_count, 4 * sizeof(float));
1311*4882a593Smuzhiyun         if (stop_colors == NULL) {
1312*4882a593Smuzhiyun             ErrorF("Failed to allocate stop_colors memory.\n");
1313*4882a593Smuzhiyun             goto GRADIENT_FAIL;
1314*4882a593Smuzhiyun         }
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun         n_stops = xallocarray(stops_count, sizeof(float));
1317*4882a593Smuzhiyun         if (n_stops == NULL) {
1318*4882a593Smuzhiyun             ErrorF("Failed to allocate n_stops memory.\n");
1319*4882a593Smuzhiyun             goto GRADIENT_FAIL;
1320*4882a593Smuzhiyun         }
1321*4882a593Smuzhiyun     }
1322*4882a593Smuzhiyun     else {
1323*4882a593Smuzhiyun         stop_colors = stop_colors_st;
1324*4882a593Smuzhiyun         n_stops = n_stops_st;
1325*4882a593Smuzhiyun     }
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun     count =
1328*4882a593Smuzhiyun         _glamor_gradient_set_stops(src_picture,
1329*4882a593Smuzhiyun                                    &src_picture->pSourcePict->gradient,
1330*4882a593Smuzhiyun                                    stop_colors, n_stops);
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun     if (src_picture->pSourcePict->linear.nstops + 2 <= LINEAR_SMALL_STOPS) {
1333*4882a593Smuzhiyun         int j = 0;
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun         glUniform4f(stop_color0_uniform_location,
1336*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1337*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1338*4882a593Smuzhiyun         j++;
1339*4882a593Smuzhiyun         glUniform4f(stop_color1_uniform_location,
1340*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1341*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1342*4882a593Smuzhiyun         j++;
1343*4882a593Smuzhiyun         glUniform4f(stop_color2_uniform_location,
1344*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1345*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1346*4882a593Smuzhiyun         j++;
1347*4882a593Smuzhiyun         glUniform4f(stop_color3_uniform_location,
1348*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1349*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1350*4882a593Smuzhiyun         j++;
1351*4882a593Smuzhiyun         glUniform4f(stop_color4_uniform_location,
1352*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1353*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1354*4882a593Smuzhiyun         j++;
1355*4882a593Smuzhiyun         glUniform4f(stop_color5_uniform_location,
1356*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1357*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1358*4882a593Smuzhiyun         j++;
1359*4882a593Smuzhiyun         glUniform4f(stop_color6_uniform_location,
1360*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1361*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1362*4882a593Smuzhiyun         j++;
1363*4882a593Smuzhiyun         glUniform4f(stop_color7_uniform_location,
1364*4882a593Smuzhiyun                     stop_colors[4 * j + 0], stop_colors[4 * j + 1],
1365*4882a593Smuzhiyun                     stop_colors[4 * j + 2], stop_colors[4 * j + 3]);
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun         j = 0;
1368*4882a593Smuzhiyun         glUniform1f(stop0_uniform_location, n_stops[j++]);
1369*4882a593Smuzhiyun         glUniform1f(stop1_uniform_location, n_stops[j++]);
1370*4882a593Smuzhiyun         glUniform1f(stop2_uniform_location, n_stops[j++]);
1371*4882a593Smuzhiyun         glUniform1f(stop3_uniform_location, n_stops[j++]);
1372*4882a593Smuzhiyun         glUniform1f(stop4_uniform_location, n_stops[j++]);
1373*4882a593Smuzhiyun         glUniform1f(stop5_uniform_location, n_stops[j++]);
1374*4882a593Smuzhiyun         glUniform1f(stop6_uniform_location, n_stops[j++]);
1375*4882a593Smuzhiyun         glUniform1f(stop7_uniform_location, n_stops[j++]);
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun         glUniform1i(n_stop_uniform_location, count);
1378*4882a593Smuzhiyun     }
1379*4882a593Smuzhiyun     else {
1380*4882a593Smuzhiyun         glUniform4fv(stop_colors_uniform_location, count, stop_colors);
1381*4882a593Smuzhiyun         glUniform1fv(stops_uniform_location, count, n_stops);
1382*4882a593Smuzhiyun         glUniform1i(n_stop_uniform_location, count);
1383*4882a593Smuzhiyun     }
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun     if (src_picture->pSourcePict->linear.p2.y == src_picture->pSourcePict->linear.p1.y) {       // The horizontal case.
1386*4882a593Smuzhiyun         glUniform1i(hor_ver_uniform_location, 1);
1387*4882a593Smuzhiyun         DEBUGF("p1.y: %f, p2.y: %f, enter the horizontal case\n",
1388*4882a593Smuzhiyun                pt1[1], pt2[1]);
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun         p1_distance = pt1[0];
1391*4882a593Smuzhiyun         pt_distance = (pt2[0] - p1_distance);
1392*4882a593Smuzhiyun         glUniform1f(p1_distance_uniform_location, p1_distance);
1393*4882a593Smuzhiyun         glUniform1f(pt_distance_uniform_location, pt_distance);
1394*4882a593Smuzhiyun     }
1395*4882a593Smuzhiyun     else {
1396*4882a593Smuzhiyun         /* The slope need to compute here. In shader, the viewport set will change
1397*4882a593Smuzhiyun            the original slope and the slope which is vertical to it will not be correct. */
1398*4882a593Smuzhiyun         slope = -(float) (src_picture->pSourcePict->linear.p2.x
1399*4882a593Smuzhiyun                           - src_picture->pSourcePict->linear.p1.x) /
1400*4882a593Smuzhiyun             (float) (src_picture->pSourcePict->linear.p2.y
1401*4882a593Smuzhiyun                      - src_picture->pSourcePict->linear.p1.y);
1402*4882a593Smuzhiyun         slope = slope * yscale / xscale;
1403*4882a593Smuzhiyun         glUniform1f(pt_slope_uniform_location, slope);
1404*4882a593Smuzhiyun         glUniform1i(hor_ver_uniform_location, 0);
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun         cos_val = sqrt(1.0 / (slope * slope + 1.0));
1407*4882a593Smuzhiyun         glUniform1f(cos_val_uniform_location, cos_val);
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun         p1_distance = (pt1[1] - pt1[0] * slope) * cos_val;
1410*4882a593Smuzhiyun         pt_distance = (pt2[1] - pt2[0] * slope) * cos_val - p1_distance;
1411*4882a593Smuzhiyun         glUniform1f(p1_distance_uniform_location, p1_distance);
1412*4882a593Smuzhiyun         glUniform1f(pt_distance_uniform_location, pt_distance);
1413*4882a593Smuzhiyun     }
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun     /* Now rendering. */
1416*4882a593Smuzhiyun     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun     /* Do the clear logic. */
1419*4882a593Smuzhiyun     if (stops_count > LINEAR_SMALL_STOPS) {
1420*4882a593Smuzhiyun         free(n_stops);
1421*4882a593Smuzhiyun         free(stop_colors);
1422*4882a593Smuzhiyun     }
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
1425*4882a593Smuzhiyun     glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun     glamor_pixmap_invalid(pixmap);
1428*4882a593Smuzhiyun 
1429*4882a593Smuzhiyun     return dst_picture;
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun  GRADIENT_FAIL:
1432*4882a593Smuzhiyun     if (dst_picture) {
1433*4882a593Smuzhiyun         FreePicture(dst_picture, 0);
1434*4882a593Smuzhiyun     }
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun     if (stops_count > LINEAR_SMALL_STOPS) {
1437*4882a593Smuzhiyun         if (n_stops)
1438*4882a593Smuzhiyun             free(n_stops);
1439*4882a593Smuzhiyun         if (stop_colors)
1440*4882a593Smuzhiyun             free(stop_colors);
1441*4882a593Smuzhiyun     }
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
1444*4882a593Smuzhiyun     glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
1445*4882a593Smuzhiyun     return NULL;
1446*4882a593Smuzhiyun }
1447