glamor_gradient.c 56 KB

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