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