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