xref: /OK3568_Linux_fs/external/xserver/glamor/glamor_render.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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  *    Eric Anholt <eric@anholt.net>
25  *    Zhigang Gong <zhigang.gong@linux.intel.com>
26  *    Junyan He <junyan.he@linux.intel.com>
27  *
28  */
29 
30 /** @file glamor_render.c
31  *
32  * Render acceleration implementation
33  */
34 
35 #include "glamor_priv.h"
36 
37 #include "mipict.h"
38 #include "fbpict.h"
39 #if 0
40 //#define DEBUGF(str, ...)  do {} while(0)
41 #define DEBUGF(str, ...) ErrorF(str, ##__VA_ARGS__)
42 //#define DEBUGRegionPrint(x) do {} while (0)
43 #define DEBUGRegionPrint RegionPrint
44 #endif
45 
46 static struct blendinfo composite_op_info[] = {
47     [PictOpClear] = {0, 0, GL_ZERO, GL_ZERO},
48     [PictOpSrc] = {0, 0, GL_ONE, GL_ZERO},
49     [PictOpDst] = {0, 0, GL_ZERO, GL_ONE},
50     [PictOpOver] = {0, 1, GL_ONE, GL_ONE_MINUS_SRC_ALPHA},
51     [PictOpOverReverse] = {1, 0, GL_ONE_MINUS_DST_ALPHA, GL_ONE},
52     [PictOpIn] = {1, 0, GL_DST_ALPHA, GL_ZERO},
53     [PictOpInReverse] = {0, 1, GL_ZERO, GL_SRC_ALPHA},
54     [PictOpOut] = {1, 0, GL_ONE_MINUS_DST_ALPHA, GL_ZERO},
55     [PictOpOutReverse] = {0, 1, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA},
56     [PictOpAtop] = {1, 1, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
57     [PictOpAtopReverse] = {1, 1, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA},
58     [PictOpXor] = {1, 1, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
59     [PictOpAdd] = {0, 0, GL_ONE, GL_ONE},
60 };
61 
62 #define RepeatFix			10
63 static GLuint
glamor_create_composite_fs(struct shader_key * key)64 glamor_create_composite_fs(struct shader_key *key)
65 {
66     const char *repeat_define =
67         "#define RepeatNone               	      0\n"
68         "#define RepeatNormal                     1\n"
69         "#define RepeatPad                        2\n"
70         "#define RepeatReflect                    3\n"
71         "#define RepeatFix		      	      10\n"
72         "uniform int 			source_repeat_mode;\n"
73         "uniform int 			mask_repeat_mode;\n";
74     const char *relocate_texture =
75         "vec2 rel_tex_coord(vec2 texture, vec4 wh, int repeat) \n"
76         "{\n"
77         "	vec2 rel_tex; \n"
78         "	rel_tex = texture * wh.xy; \n"
79         "	if (repeat == RepeatFix + RepeatNone)\n"
80         "		return rel_tex; \n"
81         "	else if (repeat == RepeatFix + RepeatNormal) \n"
82         "		rel_tex = floor(rel_tex) + (fract(rel_tex) / wh.xy); \n"
83         "	else if (repeat == RepeatFix + RepeatPad) { \n"
84         "		if (rel_tex.x >= 1.0) \n"
85         "			rel_tex.x = 1.0 - wh.z * wh.x / 2.; \n"
86         "		else if (rel_tex.x < 0.0) \n"
87         "			rel_tex.x = 0.0; \n"
88         "		if (rel_tex.y >= 1.0) \n"
89         "			rel_tex.y = 1.0 - wh.w * wh.y / 2.; \n"
90         "		else if (rel_tex.y < 0.0) \n"
91         "			rel_tex.y = 0.0; \n"
92         "		rel_tex = rel_tex / wh.xy; \n"
93         "	} else if (repeat == RepeatFix + RepeatReflect) {\n"
94         "		if ((1.0 - mod(abs(floor(rel_tex.x)), 2.0)) < 0.001)\n"
95         "			rel_tex.x = 2.0 - (1.0 - fract(rel_tex.x)) / wh.x;\n"
96         "		else \n"
97         "			rel_tex.x = fract(rel_tex.x) / wh.x;\n"
98         "		if ((1.0 - mod(abs(floor(rel_tex.y)), 2.0)) < 0.001)\n"
99         "			rel_tex.y = 2.0 - (1.0 - fract(rel_tex.y)) / wh.y;\n"
100         "		else \n"
101         "			rel_tex.y = fract(rel_tex.y) / wh.y;\n"
102         "	} \n"
103         "	return rel_tex; \n"
104         "}\n";
105     /* The texture and the pixmap size is not match eaxctly, so can't sample it directly.
106      * rel_sampler will recalculate the texture coords.*/
107     const char *rel_sampler =
108         " vec4 rel_sampler_rgba(sampler2D tex_image, vec2 tex, vec4 wh, int repeat)\n"
109         "{\n"
110         "	if (repeat >= RepeatFix) {\n"
111         "		tex = rel_tex_coord(tex, wh, repeat);\n"
112         "		if (repeat == RepeatFix + RepeatNone) {\n"
113         "			if (tex.x < 0.0 || tex.x >= 1.0 || \n"
114         "			    tex.y < 0.0 || tex.y >= 1.0)\n"
115         "				return vec4(0.0, 0.0, 0.0, 0.0);\n"
116         "			tex = (fract(tex) / wh.xy);\n"
117         "		}\n"
118         "	}\n"
119         "	return texture2D(tex_image, tex);\n"
120         "}\n"
121         " vec4 rel_sampler_rgbx(sampler2D tex_image, vec2 tex, vec4 wh, int repeat)\n"
122         "{\n"
123         "	if (repeat >= RepeatFix) {\n"
124         "		tex = rel_tex_coord(tex, wh, repeat);\n"
125         "		if (repeat == RepeatFix + RepeatNone) {\n"
126         "			if (tex.x < 0.0 || tex.x >= 1.0 || \n"
127         "			    tex.y < 0.0 || tex.y >= 1.0)\n"
128         "				return vec4(0.0, 0.0, 0.0, 0.0);\n"
129         "			tex = (fract(tex) / wh.xy);\n"
130         "		}\n"
131         "	}\n"
132         "	return vec4(texture2D(tex_image, tex).rgb, 1.0);\n"
133         "}\n";
134 
135     const char *source_solid_fetch =
136         "uniform vec4 source;\n"
137         "vec4 get_source()\n"
138         "{\n"
139         "	return source;\n"
140         "}\n";
141     const char *source_alpha_pixmap_fetch =
142         "varying vec2 source_texture;\n"
143         "uniform sampler2D source_sampler;\n"
144         "uniform vec4 source_wh;"
145         "vec4 get_source()\n"
146         "{\n"
147         "	return rel_sampler_rgba(source_sampler, source_texture,\n"
148         "			        source_wh, source_repeat_mode);\n"
149         "}\n";
150     const char *source_pixmap_fetch =
151         "varying vec2 source_texture;\n"
152         "uniform sampler2D source_sampler;\n"
153         "uniform vec4 source_wh;\n"
154         "vec4 get_source()\n"
155         "{\n"
156         "	return rel_sampler_rgbx(source_sampler, source_texture,\n"
157         "				source_wh, source_repeat_mode);\n"
158         "}\n";
159     const char *mask_none =
160         "vec4 get_mask()\n"
161         "{\n"
162         "	return vec4(0.0, 0.0, 0.0, 1.0);\n"
163         "}\n";
164     const char *mask_solid_fetch =
165         "uniform vec4 mask;\n"
166         "vec4 get_mask()\n"
167         "{\n"
168         "	return mask;\n"
169         "}\n";
170     const char *mask_alpha_pixmap_fetch =
171         "varying vec2 mask_texture;\n"
172         "uniform sampler2D mask_sampler;\n"
173         "uniform vec4 mask_wh;\n"
174         "vec4 get_mask()\n"
175         "{\n"
176         "	return rel_sampler_rgba(mask_sampler, mask_texture,\n"
177         "			        mask_wh, mask_repeat_mode);\n"
178         "}\n";
179     const char *mask_pixmap_fetch =
180         "varying vec2 mask_texture;\n"
181         "uniform sampler2D mask_sampler;\n"
182         "uniform vec4 mask_wh;\n"
183         "vec4 get_mask()\n"
184         "{\n"
185         "	return rel_sampler_rgbx(mask_sampler, mask_texture,\n"
186         "				mask_wh, mask_repeat_mode);\n"
187         "}\n";
188 
189     const char *dest_swizzle_default =
190         "vec4 dest_swizzle(vec4 color)\n"
191         "{"
192         "	return color;"
193         "}";
194     const char *dest_swizzle_alpha_to_red =
195         "vec4 dest_swizzle(vec4 color)\n"
196         "{"
197         "	float undef;\n"
198         "	return vec4(color.a, undef, undef, undef);"
199         "}";
200 
201     const char *in_normal =
202         "void main()\n"
203         "{\n"
204         "	gl_FragColor = dest_swizzle(get_source() * get_mask().a);\n"
205         "}\n";
206     const char *in_ca_source =
207         "void main()\n"
208         "{\n"
209         "	gl_FragColor = dest_swizzle(get_source() * get_mask());\n"
210         "}\n";
211     const char *in_ca_alpha =
212         "void main()\n"
213         "{\n"
214         "	gl_FragColor = dest_swizzle(get_source().a * get_mask());\n"
215         "}\n";
216     const char *in_ca_dual_blend =
217         "out vec4 color0;\n"
218         "out vec4 color1;\n"
219         "void main()\n"
220         "{\n"
221         "	color0 = dest_swizzle(get_source() * get_mask());\n"
222         "	color1 = dest_swizzle(get_source().a * get_mask());\n"
223         "}\n";
224     const char *header_ca_dual_blend =
225         "#version 130\n";
226 
227     char *source;
228     const char *source_fetch;
229     const char *mask_fetch = "";
230     const char *in;
231     const char *header;
232     const char *header_norm = "";
233     const char *dest_swizzle;
234     GLuint prog;
235 
236     switch (key->source) {
237     case SHADER_SOURCE_SOLID:
238         source_fetch = source_solid_fetch;
239         break;
240     case SHADER_SOURCE_TEXTURE_ALPHA:
241         source_fetch = source_alpha_pixmap_fetch;
242         break;
243     case SHADER_SOURCE_TEXTURE:
244         source_fetch = source_pixmap_fetch;
245         break;
246     default:
247         FatalError("Bad composite shader source");
248     }
249 
250     switch (key->mask) {
251     case SHADER_MASK_NONE:
252         mask_fetch = mask_none;
253         break;
254     case SHADER_MASK_SOLID:
255         mask_fetch = mask_solid_fetch;
256         break;
257     case SHADER_MASK_TEXTURE_ALPHA:
258         mask_fetch = mask_alpha_pixmap_fetch;
259         break;
260     case SHADER_MASK_TEXTURE:
261         mask_fetch = mask_pixmap_fetch;
262         break;
263     default:
264         FatalError("Bad composite shader mask");
265     }
266 
267     /* If we're storing to an a8 texture but our texture format is
268      * GL_RED because of a core context, then we need to make sure to
269      * put the alpha into the red channel.
270      */
271     switch (key->dest_swizzle) {
272     case SHADER_DEST_SWIZZLE_DEFAULT:
273         dest_swizzle = dest_swizzle_default;
274         break;
275     case SHADER_DEST_SWIZZLE_ALPHA_TO_RED:
276         dest_swizzle = dest_swizzle_alpha_to_red;
277         break;
278     default:
279         FatalError("Bad composite shader dest swizzle");
280     }
281 
282     header = header_norm;
283     switch (key->in) {
284     case glamor_program_alpha_normal:
285         in = in_normal;
286         break;
287     case glamor_program_alpha_ca_first:
288         in = in_ca_source;
289         break;
290     case glamor_program_alpha_ca_second:
291         in = in_ca_alpha;
292         break;
293     case glamor_program_alpha_dual_blend:
294         in = in_ca_dual_blend;
295         header = header_ca_dual_blend;
296         break;
297     default:
298         FatalError("Bad composite IN type");
299     }
300 
301     XNFasprintf(&source,
302                 "%s"
303                 GLAMOR_DEFAULT_PRECISION
304                 "%s%s%s%s%s%s%s", header, repeat_define, relocate_texture,
305                 rel_sampler, source_fetch, mask_fetch, dest_swizzle, in);
306 
307     prog = glamor_compile_glsl_prog(GL_FRAGMENT_SHADER, source);
308     free(source);
309 
310     return prog;
311 }
312 
313 static GLuint
glamor_create_composite_vs(struct shader_key * key)314 glamor_create_composite_vs(struct shader_key *key)
315 {
316     const char *main_opening =
317         "attribute vec4 v_position;\n"
318         "attribute vec4 v_texcoord0;\n"
319         "attribute vec4 v_texcoord1;\n"
320         "varying vec2 source_texture;\n"
321         "varying vec2 mask_texture;\n"
322         "void main()\n"
323         "{\n"
324         "	gl_Position = v_position;\n";
325     const char *source_coords = "	source_texture = v_texcoord0.xy;\n";
326     const char *mask_coords = "	mask_texture = v_texcoord1.xy;\n";
327     const char *main_closing = "}\n";
328     const char *source_coords_setup = "";
329     const char *mask_coords_setup = "";
330     char *source;
331     GLuint prog;
332 
333     if (key->source != SHADER_SOURCE_SOLID)
334         source_coords_setup = source_coords;
335 
336     if (key->mask != SHADER_MASK_NONE && key->mask != SHADER_MASK_SOLID)
337         mask_coords_setup = mask_coords;
338 
339     XNFasprintf(&source,
340                 "%s%s%s%s",
341                 main_opening,
342                 source_coords_setup, mask_coords_setup, main_closing);
343 
344     prog = glamor_compile_glsl_prog(GL_VERTEX_SHADER, source);
345     free(source);
346 
347     return prog;
348 }
349 
350 static void
glamor_create_composite_shader(ScreenPtr screen,struct shader_key * key,glamor_composite_shader * shader)351 glamor_create_composite_shader(ScreenPtr screen, struct shader_key *key,
352                                glamor_composite_shader *shader)
353 {
354     GLuint vs, fs, prog;
355     GLint source_sampler_uniform_location, mask_sampler_uniform_location;
356     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
357 
358     glamor_make_current(glamor_priv);
359     vs = glamor_create_composite_vs(key);
360     if (vs == 0)
361         return;
362     fs = glamor_create_composite_fs(key);
363     if (fs == 0)
364         return;
365 
366     prog = glCreateProgram();
367     glAttachShader(prog, vs);
368     glAttachShader(prog, fs);
369 
370     glBindAttribLocation(prog, GLAMOR_VERTEX_POS, "v_position");
371     glBindAttribLocation(prog, GLAMOR_VERTEX_SOURCE, "v_texcoord0");
372     glBindAttribLocation(prog, GLAMOR_VERTEX_MASK, "v_texcoord1");
373 
374     if (key->in == glamor_program_alpha_dual_blend) {
375         glBindFragDataLocationIndexed(prog, 0, 0, "color0");
376         glBindFragDataLocationIndexed(prog, 0, 1, "color1");
377     }
378     glamor_link_glsl_prog(screen, prog, "composite");
379 
380     shader->prog = prog;
381 
382     glUseProgram(prog);
383 
384     if (key->source == SHADER_SOURCE_SOLID) {
385         shader->source_uniform_location = glGetUniformLocation(prog, "source");
386     }
387     else {
388         source_sampler_uniform_location =
389             glGetUniformLocation(prog, "source_sampler");
390         glUniform1i(source_sampler_uniform_location, 0);
391         shader->source_wh = glGetUniformLocation(prog, "source_wh");
392         shader->source_repeat_mode =
393             glGetUniformLocation(prog, "source_repeat_mode");
394     }
395 
396     if (key->mask != SHADER_MASK_NONE) {
397         if (key->mask == SHADER_MASK_SOLID) {
398             shader->mask_uniform_location = glGetUniformLocation(prog, "mask");
399         }
400         else {
401             mask_sampler_uniform_location =
402                 glGetUniformLocation(prog, "mask_sampler");
403             glUniform1i(mask_sampler_uniform_location, 1);
404             shader->mask_wh = glGetUniformLocation(prog, "mask_wh");
405             shader->mask_repeat_mode =
406                 glGetUniformLocation(prog, "mask_repeat_mode");
407         }
408     }
409 }
410 
411 static glamor_composite_shader *
glamor_lookup_composite_shader(ScreenPtr screen,struct shader_key * key)412 glamor_lookup_composite_shader(ScreenPtr screen, struct
413                                shader_key
414                                *key)
415 {
416     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
417     glamor_composite_shader *shader;
418 
419     shader = &glamor_priv->composite_shader[key->source][key->mask][key->in][key->dest_swizzle];
420     if (shader->prog == 0)
421         glamor_create_composite_shader(screen, key, shader);
422 
423     return shader;
424 }
425 
426 static GLenum
glamor_translate_blend_alpha_to_red(GLenum blend)427 glamor_translate_blend_alpha_to_red(GLenum blend)
428 {
429     switch (blend) {
430     case GL_SRC_ALPHA:
431         return GL_SRC_COLOR;
432     case GL_DST_ALPHA:
433         return GL_DST_COLOR;
434     case GL_ONE_MINUS_SRC_ALPHA:
435         return GL_ONE_MINUS_SRC_COLOR;
436     case GL_ONE_MINUS_DST_ALPHA:
437         return GL_ONE_MINUS_DST_COLOR;
438     default:
439         return blend;
440     }
441 }
442 
443 static Bool
glamor_set_composite_op(ScreenPtr screen,CARD8 op,struct blendinfo * op_info_result,PicturePtr dest,PicturePtr mask,enum ca_state ca_state,struct shader_key * key)444 glamor_set_composite_op(ScreenPtr screen,
445                         CARD8 op, struct blendinfo *op_info_result,
446                         PicturePtr dest, PicturePtr mask,
447                         enum ca_state ca_state,
448                         struct shader_key *key)
449 {
450     GLenum source_blend, dest_blend;
451     struct blendinfo *op_info;
452 
453     if (op >= ARRAY_SIZE(composite_op_info)) {
454         glamor_fallback("unsupported render op %d \n", op);
455         return GL_FALSE;
456     }
457 
458     op_info = &composite_op_info[op];
459 
460     source_blend = op_info->source_blend;
461     dest_blend = op_info->dest_blend;
462 
463     /* If there's no dst alpha channel, adjust the blend op so that we'll treat
464      * it as always 1.
465      */
466     if (PICT_FORMAT_A(dest->format) == 0 && op_info->dest_alpha) {
467         if (source_blend == GL_DST_ALPHA)
468             source_blend = GL_ONE;
469         else if (source_blend == GL_ONE_MINUS_DST_ALPHA)
470             source_blend = GL_ZERO;
471     }
472 
473     /* Set up the source alpha value for blending in component alpha mode. */
474     if (ca_state == CA_DUAL_BLEND) {
475         switch (dest_blend) {
476         case GL_SRC_ALPHA:
477             dest_blend = GL_SRC1_COLOR;
478             break;
479         case GL_ONE_MINUS_SRC_ALPHA:
480             dest_blend = GL_ONE_MINUS_SRC1_COLOR;
481             break;
482         }
483     } else if (mask && mask->componentAlpha
484                && PICT_FORMAT_RGB(mask->format) != 0 && op_info->source_alpha) {
485         switch (dest_blend) {
486         case GL_SRC_ALPHA:
487             dest_blend = GL_SRC_COLOR;
488             break;
489         case GL_ONE_MINUS_SRC_ALPHA:
490             dest_blend = GL_ONE_MINUS_SRC_COLOR;
491             break;
492         }
493     }
494 
495     /* If we're outputting our alpha to the red channel, then any
496      * reads of alpha for blending need to come from the red channel.
497      */
498     if (key->dest_swizzle == SHADER_DEST_SWIZZLE_ALPHA_TO_RED) {
499         source_blend = glamor_translate_blend_alpha_to_red(source_blend);
500         dest_blend = glamor_translate_blend_alpha_to_red(dest_blend);
501     }
502 
503     op_info_result->source_blend = source_blend;
504     op_info_result->dest_blend = dest_blend;
505     op_info_result->source_alpha = op_info->source_alpha;
506     op_info_result->dest_alpha = op_info->dest_alpha;
507 
508     return TRUE;
509 }
510 
511 static void
glamor_set_composite_texture(glamor_screen_private * glamor_priv,int unit,PicturePtr picture,PixmapPtr pixmap,GLuint wh_location,GLuint repeat_location,glamor_pixmap_private * dest_priv)512 glamor_set_composite_texture(glamor_screen_private *glamor_priv, int unit,
513                              PicturePtr picture,
514                              PixmapPtr pixmap,
515                              GLuint wh_location, GLuint repeat_location,
516                              glamor_pixmap_private *dest_priv)
517 {
518     glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
519     glamor_pixmap_fbo *fbo = pixmap_priv->fbo;
520     float wh[4];
521     int repeat_type;
522 
523     glamor_make_current(glamor_priv);
524 
525     /* The red channel swizzling doesn't depend on whether we're using
526      * 'fbo' as source or mask as we must have the same answer in case
527      * the same fbo is being used for both. That means the mask
528      * channel will sometimes get red bits in the R channel, and
529      * sometimes get zero bits in the R channel, which is harmless.
530      */
531     glamor_bind_texture(glamor_priv, GL_TEXTURE0 + unit, fbo,
532                         dest_priv->fbo->is_red);
533     repeat_type = picture->repeatType;
534     switch (picture->repeatType) {
535     case RepeatNone:
536         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
537         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
538         break;
539     case RepeatNormal:
540         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
541         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
542         break;
543     case RepeatPad:
544         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
545         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
546         break;
547     case RepeatReflect:
548         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
549         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
550         break;
551     }
552 
553     switch (picture->filter) {
554     default:
555     case PictFilterFast:
556     case PictFilterNearest:
557         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
558         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
559         break;
560     case PictFilterGood:
561     case PictFilterBest:
562     case PictFilterBilinear:
563         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
564         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
565         break;
566     }
567 
568     /* Handle RepeatNone in the shader when the source is missing the
569      * alpha channel, as GL will return an alpha for 1 if the texture
570      * is RGB (no alpha), which we use for 16bpp textures.
571      */
572     if (glamor_pixmap_priv_is_large(pixmap_priv) ||
573         (!PICT_FORMAT_A(picture->format) &&
574          repeat_type == RepeatNone && picture->transform)) {
575         glamor_pixmap_fbo_fix_wh_ratio(wh, pixmap, pixmap_priv);
576         glUniform4fv(wh_location, 1, wh);
577 
578         repeat_type += RepeatFix;
579     }
580 
581     glUniform1i(repeat_location, repeat_type);
582 }
583 
584 static void
glamor_set_composite_solid(float * color,GLint uniform_location)585 glamor_set_composite_solid(float *color, GLint uniform_location)
586 {
587     glUniform4fv(uniform_location, 1, color);
588 }
589 
590 static char
glamor_get_picture_location(PicturePtr picture)591 glamor_get_picture_location(PicturePtr picture)
592 {
593     if (picture == NULL)
594         return ' ';
595 
596     if (picture->pDrawable == NULL) {
597         switch (picture->pSourcePict->type) {
598         case SourcePictTypeSolidFill:
599             return 'c';
600         case SourcePictTypeLinear:
601             return 'l';
602         case SourcePictTypeRadial:
603             return 'r';
604         default:
605             return '?';
606         }
607     }
608     return glamor_get_drawable_location(picture->pDrawable);
609 }
610 
611 static void *
glamor_setup_composite_vbo(ScreenPtr screen,int n_verts)612 glamor_setup_composite_vbo(ScreenPtr screen, int n_verts)
613 {
614     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
615     int vert_size;
616     char *vbo_offset;
617     float *vb;
618 
619     glamor_priv->render_nr_quads = 0;
620     glamor_priv->vb_stride = 2 * sizeof(float);
621     if (glamor_priv->has_source_coords)
622         glamor_priv->vb_stride += 2 * sizeof(float);
623     if (glamor_priv->has_mask_coords)
624         glamor_priv->vb_stride += 2 * sizeof(float);
625 
626     vert_size = n_verts * glamor_priv->vb_stride;
627 
628     glamor_make_current(glamor_priv);
629     vb = glamor_get_vbo_space(screen, vert_size, &vbo_offset);
630 
631     glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT, GL_FALSE,
632                           glamor_priv->vb_stride, vbo_offset);
633     glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
634 
635     if (glamor_priv->has_source_coords) {
636         glVertexAttribPointer(GLAMOR_VERTEX_SOURCE, 2,
637                               GL_FLOAT, GL_FALSE,
638                               glamor_priv->vb_stride,
639                               vbo_offset + 2 * sizeof(float));
640         glEnableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
641     }
642 
643     if (glamor_priv->has_mask_coords) {
644         glVertexAttribPointer(GLAMOR_VERTEX_MASK, 2, GL_FLOAT, GL_FALSE,
645                               glamor_priv->vb_stride,
646                               vbo_offset + (glamor_priv->has_source_coords ?
647                                             4 : 2) * sizeof(float));
648         glEnableVertexAttribArray(GLAMOR_VERTEX_MASK);
649     }
650 
651     return vb;
652 }
653 
654 static void
glamor_flush_composite_rects(ScreenPtr screen)655 glamor_flush_composite_rects(ScreenPtr screen)
656 {
657     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
658 
659     glamor_make_current(glamor_priv);
660 
661     if (!glamor_priv->render_nr_quads)
662         return;
663 
664     glamor_glDrawArrays_GL_QUADS(glamor_priv, glamor_priv->render_nr_quads);
665 }
666 
667 static const int pict_format_combine_tab[][3] = {
668     {PICT_TYPE_ARGB, PICT_TYPE_A, PICT_TYPE_ARGB},
669     {PICT_TYPE_ABGR, PICT_TYPE_A, PICT_TYPE_ABGR},
670 };
671 
672 static Bool
combine_pict_format(PictFormatShort * des,const PictFormatShort src,const PictFormatShort mask,glamor_program_alpha in_ca)673 combine_pict_format(PictFormatShort * des, const PictFormatShort src,
674                     const PictFormatShort mask, glamor_program_alpha in_ca)
675 {
676     PictFormatShort new_vis;
677     int src_type, mask_type, src_bpp;
678     int i;
679 
680     if (src == mask) {
681         *des = src;
682         return TRUE;
683     }
684     src_bpp = PICT_FORMAT_BPP(src);
685 
686     assert(src_bpp == PICT_FORMAT_BPP(mask));
687 
688     new_vis = PICT_FORMAT_VIS(src) | PICT_FORMAT_VIS(mask);
689 
690     switch (in_ca) {
691     case glamor_program_alpha_normal:
692         src_type = PICT_FORMAT_TYPE(src);
693         mask_type = PICT_TYPE_A;
694         break;
695     case glamor_program_alpha_ca_first:
696         src_type = PICT_FORMAT_TYPE(src);
697         mask_type = PICT_FORMAT_TYPE(mask);
698         break;
699     case glamor_program_alpha_ca_second:
700         src_type = PICT_TYPE_A;
701         mask_type = PICT_FORMAT_TYPE(mask);
702         break;
703     case glamor_program_alpha_dual_blend:
704         src_type = PICT_FORMAT_TYPE(src);
705         mask_type = PICT_FORMAT_TYPE(mask);
706         break;
707     default:
708         return FALSE;
709     }
710 
711     if (src_type == mask_type) {
712         *des = PICT_VISFORMAT(src_bpp, src_type, new_vis);
713         return TRUE;
714     }
715 
716     for (i = 0; i < ARRAY_SIZE(pict_format_combine_tab); i++) {
717         if ((src_type == pict_format_combine_tab[i][0]
718              && mask_type == pict_format_combine_tab[i][1])
719             || (src_type == pict_format_combine_tab[i][1]
720                 && mask_type == pict_format_combine_tab[i][0])) {
721             *des = PICT_VISFORMAT(src_bpp, pict_format_combine_tab[i]
722                                   [2], new_vis);
723             return TRUE;
724         }
725     }
726     return FALSE;
727 }
728 
729 static void
glamor_set_normalize_tcoords_generic(PixmapPtr pixmap,glamor_pixmap_private * priv,int repeat_type,float * matrix,float xscale,float yscale,int x1,int y1,int x2,int y2,float * texcoords,int stride)730 glamor_set_normalize_tcoords_generic(PixmapPtr pixmap,
731                                      glamor_pixmap_private *priv,
732                                      int repeat_type,
733                                      float *matrix,
734                                      float xscale, float yscale,
735                                      int x1, int y1, int x2, int y2,
736                                      float *texcoords,
737                                      int stride)
738 {
739     if (!matrix && repeat_type == RepeatNone)
740         glamor_set_normalize_tcoords_ext(priv, xscale, yscale,
741                                          x1, y1,
742                                          x2, y2, texcoords, stride);
743     else if (matrix && repeat_type == RepeatNone)
744         glamor_set_transformed_normalize_tcoords_ext(priv, matrix, xscale,
745                                                      yscale, x1, y1,
746                                                      x2, y2,
747                                                      texcoords, stride);
748     else if (!matrix && repeat_type != RepeatNone)
749         glamor_set_repeat_normalize_tcoords_ext(pixmap, priv, repeat_type,
750                                                 xscale, yscale,
751                                                 x1, y1,
752                                                 x2, y2,
753                                                 texcoords, stride);
754     else if (matrix && repeat_type != RepeatNone)
755         glamor_set_repeat_transformed_normalize_tcoords_ext(pixmap, priv, repeat_type,
756                                                             matrix, xscale,
757                                                             yscale, x1, y1, x2,
758                                                             y2,
759                                                             texcoords, stride);
760 }
761 
762 /**
763  * Returns whether the general composite path supports this picture
764  * format for a pixmap that is permanently stored in an FBO (as
765  * opposed to the dynamic upload path).
766  *
767  * We could support many more formats by using GL_ARB_texture_view to
768  * parse the same bits as different formats.  For now, we only support
769  * tweaking whether we sample the alpha bits, or just force them to 1.
770  */
771 static Bool
glamor_render_format_is_supported(PicturePtr picture)772 glamor_render_format_is_supported(PicturePtr picture)
773 {
774     PictFormatShort storage_format;
775     glamor_screen_private *glamor_priv;
776 
777     /* Source-only pictures should always work */
778     if (!picture->pDrawable)
779         return TRUE;
780 
781     glamor_priv = glamor_get_screen_private(picture->pDrawable->pScreen);
782     storage_format =
783         glamor_priv->formats[picture->pDrawable->depth].render_format;
784 
785     switch (picture->format) {
786     case PICT_a2r10g10b10:
787         return storage_format == PICT_x2r10g10b10;
788     case PICT_a8r8g8b8:
789     case PICT_x8r8g8b8:
790         return storage_format == PICT_a8r8g8b8 || storage_format == PICT_x8r8g8b8;
791     case PICT_a1r5g5b5:
792         return storage_format == PICT_x1r5g5b5;
793     default:
794         return picture->format == storage_format;
795     }
796 }
797 
798 static Bool
glamor_composite_choose_shader(CARD8 op,PicturePtr source,PicturePtr mask,PicturePtr dest,PixmapPtr source_pixmap,PixmapPtr mask_pixmap,PixmapPtr dest_pixmap,glamor_pixmap_private * source_pixmap_priv,glamor_pixmap_private * mask_pixmap_priv,glamor_pixmap_private * dest_pixmap_priv,struct shader_key * s_key,glamor_composite_shader ** shader,struct blendinfo * op_info,PictFormatShort * psaved_source_format,enum ca_state ca_state)799 glamor_composite_choose_shader(CARD8 op,
800                                PicturePtr source,
801                                PicturePtr mask,
802                                PicturePtr dest,
803                                PixmapPtr source_pixmap,
804                                PixmapPtr mask_pixmap,
805                                PixmapPtr dest_pixmap,
806                                glamor_pixmap_private *source_pixmap_priv,
807                                glamor_pixmap_private *mask_pixmap_priv,
808                                glamor_pixmap_private *dest_pixmap_priv,
809                                struct shader_key *s_key,
810                                glamor_composite_shader ** shader,
811                                struct blendinfo *op_info,
812                                PictFormatShort *psaved_source_format,
813                                enum ca_state ca_state)
814 {
815     ScreenPtr screen = dest->pDrawable->pScreen;
816     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
817     Bool source_needs_upload = FALSE;
818     Bool mask_needs_upload = FALSE;
819     PictFormatShort saved_source_format = 0;
820     struct shader_key key;
821     GLfloat source_solid_color[4];
822     GLfloat mask_solid_color[4];
823     Bool ret = FALSE;
824 
825     if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dest_pixmap_priv)) {
826         glamor_fallback("dest has no fbo.\n");
827         goto fail;
828     }
829 
830     if (!glamor_render_format_is_supported(dest)) {
831         glamor_fallback("Unsupported dest picture format.\n");
832         goto fail;
833     }
834 
835     memset(&key, 0, sizeof(key));
836     if (!source) {
837         key.source = SHADER_SOURCE_SOLID;
838         source_solid_color[0] = 0.0;
839         source_solid_color[1] = 0.0;
840         source_solid_color[2] = 0.0;
841         source_solid_color[3] = 0.0;
842     }
843     else if (!source->pDrawable) {
844         SourcePictPtr sp = source->pSourcePict;
845         if (sp->type == SourcePictTypeSolidFill) {
846             key.source = SHADER_SOURCE_SOLID;
847             glamor_get_rgba_from_color(&sp->solidFill.fullcolor,
848                                        source_solid_color);
849         }
850         else
851             goto fail;
852     }
853     else {
854         if (PICT_FORMAT_A(source->format))
855             key.source = SHADER_SOURCE_TEXTURE_ALPHA;
856         else
857             key.source = SHADER_SOURCE_TEXTURE;
858     }
859 
860     if (mask) {
861         if (!mask->pDrawable) {
862             SourcePictPtr sp = mask->pSourcePict;
863             if (sp->type == SourcePictTypeSolidFill) {
864                 key.mask = SHADER_MASK_SOLID;
865                 glamor_get_rgba_from_color(&sp->solidFill.fullcolor,
866                                            mask_solid_color);
867             }
868             else
869                 goto fail;
870         }
871         else {
872             if (PICT_FORMAT_A(mask->format))
873                 key.mask = SHADER_MASK_TEXTURE_ALPHA;
874             else
875                 key.mask = SHADER_MASK_TEXTURE;
876         }
877 
878         if (!mask->componentAlpha) {
879             key.in = glamor_program_alpha_normal;
880         }
881         else {
882             if (op == PictOpClear)
883                 key.mask = SHADER_MASK_NONE;
884             else if (glamor_priv->has_dual_blend)
885                 key.in = glamor_program_alpha_dual_blend;
886             else if (op == PictOpSrc || op == PictOpAdd
887                      || op == PictOpIn || op == PictOpOut
888                      || op == PictOpOverReverse)
889                 key.in = glamor_program_alpha_ca_second;
890             else if (op == PictOpOutReverse || op == PictOpInReverse) {
891                 key.in = glamor_program_alpha_ca_first;
892             }
893             else {
894                 glamor_fallback("Unsupported component alpha op: %d\n", op);
895                 goto fail;
896             }
897         }
898     }
899     else {
900         key.mask = SHADER_MASK_NONE;
901     }
902 
903     if (dest_pixmap->drawable.bitsPerPixel <= 8 &&
904         glamor_priv->formats[8].format == GL_RED) {
905         key.dest_swizzle = SHADER_DEST_SWIZZLE_ALPHA_TO_RED;
906     } else {
907         key.dest_swizzle = SHADER_DEST_SWIZZLE_DEFAULT;
908     }
909 
910     if (source && source->alphaMap) {
911         glamor_fallback("source alphaMap\n");
912         goto fail;
913     }
914     if (mask && mask->alphaMap) {
915         glamor_fallback("mask alphaMap\n");
916         goto fail;
917     }
918 
919     if (key.source == SHADER_SOURCE_TEXTURE ||
920         key.source == SHADER_SOURCE_TEXTURE_ALPHA) {
921         if (source_pixmap == dest_pixmap) {
922             /* XXX source and the dest share the same texture.
923              * Does it need special handle? */
924             glamor_fallback("source == dest\n");
925         }
926         if (source_pixmap_priv->gl_fbo == GLAMOR_FBO_UNATTACHED) {
927             source_needs_upload = TRUE;
928         }
929     }
930 
931     if (key.mask == SHADER_MASK_TEXTURE ||
932         key.mask == SHADER_MASK_TEXTURE_ALPHA) {
933         if (mask_pixmap == dest_pixmap) {
934             glamor_fallback("mask == dest\n");
935             goto fail;
936         }
937         if (mask_pixmap_priv->gl_fbo == GLAMOR_FBO_UNATTACHED) {
938             mask_needs_upload = TRUE;
939         }
940     }
941 
942     if (source_needs_upload && mask_needs_upload
943         && source_pixmap == mask_pixmap) {
944 
945         if (source->format != mask->format) {
946             saved_source_format = source->format;
947 
948             if (!combine_pict_format(&source->format, source->format,
949                                      mask->format, key.in)) {
950                 glamor_fallback("combine source %x mask %x failed.\n",
951                                 source->format, mask->format);
952                 goto fail;
953             }
954 
955             /* XXX
956              * By default, glamor_upload_picture_to_texture will wire alpha to 1
957              * if one picture doesn't have alpha. So we don't do that again in
958              * rendering function. But here is a special case, as source and
959              * mask share the same texture but may have different formats. For
960              * example, source doesn't have alpha, but mask has alpha. Then the
961              * texture will have the alpha value for the mask. And will not wire
962              * to 1 for the source. In this case, we have to use different shader
963              * to wire the source's alpha to 1.
964              *
965              * But this may cause a potential problem if the source's repeat mode
966              * is REPEAT_NONE, and if the source is smaller than the dest, then
967              * for the region not covered by the source may be painted incorrectly.
968              * because we wire the alpha to 1.
969              *
970              **/
971             if (!PICT_FORMAT_A(saved_source_format)
972                 && PICT_FORMAT_A(mask->format))
973                 key.source = SHADER_SOURCE_TEXTURE;
974 
975             if (!PICT_FORMAT_A(mask->format)
976                 && PICT_FORMAT_A(saved_source_format))
977                 key.mask = SHADER_MASK_TEXTURE;
978         }
979 
980         if (!glamor_upload_picture_to_texture(source)) {
981             glamor_fallback("Failed to upload source texture.\n");
982             goto fail;
983         }
984         mask_needs_upload = FALSE;
985     }
986     else {
987         if (source_needs_upload) {
988             if (!glamor_upload_picture_to_texture(source)) {
989                 glamor_fallback("Failed to upload source texture.\n");
990                 goto fail;
991             }
992         } else {
993             if (source && !glamor_render_format_is_supported(source)) {
994                 glamor_fallback("Unsupported source picture format.\n");
995                 goto fail;
996             }
997         }
998 
999         if (mask_needs_upload) {
1000             if (!glamor_upload_picture_to_texture(mask)) {
1001                 glamor_fallback("Failed to upload mask texture.\n");
1002                 goto fail;
1003             }
1004         } else if (mask) {
1005             if (!glamor_render_format_is_supported(mask)) {
1006                 glamor_fallback("Unsupported mask picture format.\n");
1007                 goto fail;
1008             }
1009         }
1010     }
1011 
1012     /* If the source and mask are two differently-formatted views of
1013      * the same pixmap bits, and the pixmap was already uploaded (so
1014      * the dynamic code above doesn't apply), then fall back to
1015      * software.  We should use texture views to fix this properly.
1016      */
1017     if (source_pixmap && source_pixmap == mask_pixmap &&
1018         source->format != mask->format) {
1019         goto fail;
1020     }
1021 
1022     if (!glamor_set_composite_op(screen, op, op_info, dest, mask, ca_state,
1023                                  &key)) {
1024         goto fail;
1025     }
1026 
1027     *shader = glamor_lookup_composite_shader(screen, &key);
1028     if ((*shader)->prog == 0) {
1029         glamor_fallback("no shader program for this render acccel mode\n");
1030         goto fail;
1031     }
1032 
1033     if (key.source == SHADER_SOURCE_SOLID)
1034         memcpy(&(*shader)->source_solid_color[0],
1035                source_solid_color, 4 * sizeof(float));
1036     else {
1037         (*shader)->source_pixmap = source_pixmap;
1038         (*shader)->source = source;
1039     }
1040 
1041     if (key.mask == SHADER_MASK_SOLID)
1042         memcpy(&(*shader)->mask_solid_color[0],
1043                mask_solid_color, 4 * sizeof(float));
1044     else {
1045         (*shader)->mask_pixmap = mask_pixmap;
1046         (*shader)->mask = mask;
1047     }
1048 
1049     ret = TRUE;
1050     memcpy(s_key, &key, sizeof(key));
1051     *psaved_source_format = saved_source_format;
1052     goto done;
1053 
1054  fail:
1055     if (saved_source_format)
1056         source->format = saved_source_format;
1057  done:
1058     return ret;
1059 }
1060 
1061 static void
glamor_composite_set_shader_blend(glamor_screen_private * glamor_priv,glamor_pixmap_private * dest_priv,struct shader_key * key,glamor_composite_shader * shader,struct blendinfo * op_info)1062 glamor_composite_set_shader_blend(glamor_screen_private *glamor_priv,
1063                                   glamor_pixmap_private *dest_priv,
1064                                   struct shader_key *key,
1065                                   glamor_composite_shader *shader,
1066                                   struct blendinfo *op_info)
1067 {
1068     glamor_make_current(glamor_priv);
1069     glUseProgram(shader->prog);
1070 
1071     if (key->source == SHADER_SOURCE_SOLID) {
1072         glamor_set_composite_solid(shader->source_solid_color,
1073                                    shader->source_uniform_location);
1074     }
1075     else {
1076         glamor_set_composite_texture(glamor_priv, 0,
1077                                      shader->source,
1078                                      shader->source_pixmap, shader->source_wh,
1079                                      shader->source_repeat_mode,
1080                                      dest_priv);
1081     }
1082 
1083     if (key->mask != SHADER_MASK_NONE) {
1084         if (key->mask == SHADER_MASK_SOLID) {
1085             glamor_set_composite_solid(shader->mask_solid_color,
1086                                        shader->mask_uniform_location);
1087         }
1088         else {
1089             glamor_set_composite_texture(glamor_priv, 1,
1090                                          shader->mask,
1091                                          shader->mask_pixmap, shader->mask_wh,
1092                                          shader->mask_repeat_mode,
1093                                          dest_priv);
1094         }
1095     }
1096 
1097     if (!glamor_priv->is_gles)
1098         glDisable(GL_COLOR_LOGIC_OP);
1099 
1100     if (op_info->source_blend == GL_ONE && op_info->dest_blend == GL_ZERO) {
1101         glDisable(GL_BLEND);
1102     }
1103     else {
1104         glEnable(GL_BLEND);
1105         glBlendFunc(op_info->source_blend, op_info->dest_blend);
1106     }
1107 }
1108 
1109 static Bool
glamor_composite_with_shader(CARD8 op,PicturePtr source,PicturePtr mask,PicturePtr dest,PixmapPtr source_pixmap,PixmapPtr mask_pixmap,PixmapPtr dest_pixmap,glamor_pixmap_private * source_pixmap_priv,glamor_pixmap_private * mask_pixmap_priv,glamor_pixmap_private * dest_pixmap_priv,int nrect,glamor_composite_rect_t * rects,enum ca_state ca_state)1110 glamor_composite_with_shader(CARD8 op,
1111                              PicturePtr source,
1112                              PicturePtr mask,
1113                              PicturePtr dest,
1114                              PixmapPtr source_pixmap,
1115                              PixmapPtr mask_pixmap,
1116                              PixmapPtr dest_pixmap,
1117                              glamor_pixmap_private *source_pixmap_priv,
1118                              glamor_pixmap_private *mask_pixmap_priv,
1119                              glamor_pixmap_private *dest_pixmap_priv,
1120                              int nrect, glamor_composite_rect_t *rects,
1121                              enum ca_state ca_state)
1122 {
1123     ScreenPtr screen = dest->pDrawable->pScreen;
1124     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
1125     GLfloat dst_xscale, dst_yscale;
1126     GLfloat mask_xscale = 1, mask_yscale = 1, src_xscale = 1, src_yscale = 1;
1127     struct shader_key key, key_ca;
1128     int dest_x_off, dest_y_off;
1129     int source_x_off, source_y_off;
1130     int mask_x_off, mask_y_off;
1131     PictFormatShort saved_source_format = 0;
1132     float src_matrix[9], mask_matrix[9];
1133     float *psrc_matrix = NULL, *pmask_matrix = NULL;
1134     int nrect_max;
1135     Bool ret = FALSE;
1136     glamor_composite_shader *shader = NULL, *shader_ca = NULL;
1137     struct blendinfo op_info, op_info_ca;
1138 
1139     if (!glamor_composite_choose_shader(op, source, mask, dest,
1140                                         source_pixmap, mask_pixmap, dest_pixmap,
1141                                         source_pixmap_priv, mask_pixmap_priv,
1142                                         dest_pixmap_priv,
1143                                         &key, &shader, &op_info,
1144                                         &saved_source_format, ca_state)) {
1145         glamor_fallback("glamor_composite_choose_shader failed\n");
1146         goto fail;
1147     }
1148     if (ca_state == CA_TWO_PASS) {
1149         if (!glamor_composite_choose_shader(PictOpAdd, source, mask, dest,
1150                                             source_pixmap, mask_pixmap, dest_pixmap,
1151                                             source_pixmap_priv,
1152                                             mask_pixmap_priv, dest_pixmap_priv,
1153                                             &key_ca, &shader_ca, &op_info_ca,
1154                                             &saved_source_format, ca_state)) {
1155             glamor_fallback("glamor_composite_choose_shader failed\n");
1156             goto fail;
1157         }
1158     }
1159 
1160     glamor_make_current(glamor_priv);
1161 
1162     glamor_set_destination_pixmap_priv_nc(glamor_priv, dest_pixmap, dest_pixmap_priv);
1163     glamor_composite_set_shader_blend(glamor_priv, dest_pixmap_priv, &key, shader, &op_info);
1164     glamor_set_alu(screen, GXcopy);
1165 
1166     glamor_priv->has_source_coords = key.source != SHADER_SOURCE_SOLID;
1167     glamor_priv->has_mask_coords = (key.mask != SHADER_MASK_NONE &&
1168                                     key.mask != SHADER_MASK_SOLID);
1169 
1170     dest_pixmap = glamor_get_drawable_pixmap(dest->pDrawable);
1171     dest_pixmap_priv = glamor_get_pixmap_private(dest_pixmap);
1172     glamor_get_drawable_deltas(dest->pDrawable, dest_pixmap,
1173                                &dest_x_off, &dest_y_off);
1174     pixmap_priv_get_dest_scale(dest_pixmap, dest_pixmap_priv, &dst_xscale, &dst_yscale);
1175 
1176     if (glamor_priv->has_source_coords) {
1177         glamor_get_drawable_deltas(source->pDrawable,
1178                                    source_pixmap, &source_x_off, &source_y_off);
1179         pixmap_priv_get_scale(source_pixmap_priv, &src_xscale, &src_yscale);
1180         if (source->transform) {
1181             psrc_matrix = src_matrix;
1182             glamor_picture_get_matrixf(source, psrc_matrix);
1183         }
1184     }
1185 
1186     if (glamor_priv->has_mask_coords) {
1187         glamor_get_drawable_deltas(mask->pDrawable, mask_pixmap,
1188                                    &mask_x_off, &mask_y_off);
1189         pixmap_priv_get_scale(mask_pixmap_priv, &mask_xscale, &mask_yscale);
1190         if (mask->transform) {
1191             pmask_matrix = mask_matrix;
1192             glamor_picture_get_matrixf(mask, pmask_matrix);
1193         }
1194     }
1195 
1196     nrect_max = MIN(nrect, GLAMOR_COMPOSITE_VBO_VERT_CNT / 4);
1197 
1198     if (nrect < 100) {
1199         BoxRec bounds = glamor_start_rendering_bounds();
1200 
1201         for (int i = 0; i < nrect; i++) {
1202             BoxRec box = {
1203                 .x1 = rects[i].x_dst,
1204                 .y1 = rects[i].y_dst,
1205                 .x2 = rects[i].x_dst + rects[i].width,
1206                 .y2 = rects[i].y_dst + rects[i].height,
1207             };
1208             glamor_bounds_union_box(&bounds, &box);
1209         }
1210 
1211         if (bounds.x1 >= bounds.x2 || bounds.y1 >= bounds.y2)
1212             goto disable_va;
1213 
1214         glEnable(GL_SCISSOR_TEST);
1215         glScissor(bounds.x1 + dest_x_off,
1216                   bounds.y1 + dest_y_off,
1217                   bounds.x2 - bounds.x1,
1218                   bounds.y2 - bounds.y1);
1219     }
1220 
1221     while (nrect) {
1222         int mrect, rect_processed;
1223         int vb_stride;
1224         float *vertices;
1225 
1226         mrect = nrect > nrect_max ? nrect_max : nrect;
1227         vertices = glamor_setup_composite_vbo(screen, mrect * 4);
1228         rect_processed = mrect;
1229         vb_stride = glamor_priv->vb_stride / sizeof(float);
1230         while (mrect--) {
1231             INT16 x_source;
1232             INT16 y_source;
1233             INT16 x_mask;
1234             INT16 y_mask;
1235             INT16 x_dest;
1236             INT16 y_dest;
1237             CARD16 width;
1238             CARD16 height;
1239 
1240             x_dest = rects->x_dst + dest_x_off;
1241             y_dest = rects->y_dst + dest_y_off;
1242             x_source = rects->x_src + source_x_off;
1243             y_source = rects->y_src + source_y_off;
1244             x_mask = rects->x_mask + mask_x_off;
1245             y_mask = rects->y_mask + mask_y_off;
1246             width = rects->width;
1247             height = rects->height;
1248 
1249             DEBUGF
1250                 ("dest(%d,%d) source(%d %d) mask (%d %d), width %d height %d \n",
1251                  x_dest, y_dest, x_source, y_source, x_mask, y_mask, width,
1252                  height);
1253 
1254             glamor_set_normalize_vcoords_ext(dest_pixmap_priv, dst_xscale,
1255                                              dst_yscale, x_dest, y_dest,
1256                                              x_dest + width, y_dest + height,
1257                                              vertices,
1258                                              vb_stride);
1259             vertices += 2;
1260             if (key.source != SHADER_SOURCE_SOLID) {
1261                 glamor_set_normalize_tcoords_generic(source_pixmap,
1262                                                      source_pixmap_priv,
1263                                                      source->repeatType,
1264                                                      psrc_matrix, src_xscale,
1265                                                      src_yscale, x_source,
1266                                                      y_source, x_source + width,
1267                                                      y_source + height,
1268                                                      vertices, vb_stride);
1269                 vertices += 2;
1270             }
1271 
1272             if (key.mask != SHADER_MASK_NONE && key.mask != SHADER_MASK_SOLID) {
1273                 glamor_set_normalize_tcoords_generic(mask_pixmap,
1274                                                      mask_pixmap_priv,
1275                                                      mask->repeatType,
1276                                                      pmask_matrix, mask_xscale,
1277                                                      mask_yscale, x_mask,
1278                                                      y_mask, x_mask + width,
1279                                                      y_mask + height,
1280                                                      vertices, vb_stride);
1281                 vertices += 2;
1282             }
1283             glamor_priv->render_nr_quads++;
1284             rects++;
1285 
1286             /* We've incremented by one of our 4 verts, now do the other 3. */
1287             vertices += 3 * vb_stride;
1288         }
1289         glamor_put_vbo_space(screen);
1290         glamor_flush_composite_rects(screen);
1291         nrect -= rect_processed;
1292         if (ca_state == CA_TWO_PASS) {
1293             glamor_composite_set_shader_blend(glamor_priv, dest_pixmap_priv,
1294                                               &key_ca, shader_ca, &op_info_ca);
1295             glamor_flush_composite_rects(screen);
1296             if (nrect)
1297                 glamor_composite_set_shader_blend(glamor_priv, dest_pixmap_priv,
1298                                                   &key, shader, &op_info);
1299         }
1300     }
1301 
1302     glDisable(GL_SCISSOR_TEST);
1303 
1304     glamor_pixmap_invalid(dest_pixmap);
1305 
1306 disable_va:
1307     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
1308     glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
1309     glDisableVertexAttribArray(GLAMOR_VERTEX_MASK);
1310     glDisable(GL_BLEND);
1311     DEBUGF("finish rendering.\n");
1312     if (saved_source_format)
1313         source->format = saved_source_format;
1314 
1315     ret = TRUE;
1316 
1317 fail:
1318     if (mask_pixmap && glamor_pixmap_is_memory(mask_pixmap))
1319         glamor_pixmap_destroy_fbo(mask_pixmap);
1320     if (source_pixmap && glamor_pixmap_is_memory(source_pixmap))
1321         glamor_pixmap_destroy_fbo(source_pixmap);
1322 
1323     return ret;
1324 }
1325 
1326 static PicturePtr
glamor_convert_gradient_picture(ScreenPtr screen,PicturePtr source,int x_source,int y_source,int width,int height)1327 glamor_convert_gradient_picture(ScreenPtr screen,
1328                                 PicturePtr source,
1329                                 int x_source,
1330                                 int y_source, int width, int height)
1331 {
1332     PixmapPtr pixmap;
1333     PicturePtr dst = NULL;
1334     int error;
1335     PictFormatPtr pFormat;
1336     PictFormatShort format;
1337 
1338     if (source->pDrawable) {
1339         pFormat = source->pFormat;
1340         format = pFormat->format;
1341     } else {
1342         format = PICT_a8r8g8b8;
1343         pFormat = PictureMatchFormat(screen, 32, format);
1344     }
1345 
1346     if (!source->pDrawable) {
1347         if (source->pSourcePict->type == SourcePictTypeLinear) {
1348             dst = glamor_generate_linear_gradient_picture(screen,
1349                                                           source, x_source,
1350                                                           y_source, width,
1351                                                           height, format);
1352         }
1353         else if (source->pSourcePict->type == SourcePictTypeRadial) {
1354             dst = glamor_generate_radial_gradient_picture(screen,
1355                                                           source, x_source,
1356                                                           y_source, width,
1357                                                           height, format);
1358         }
1359 
1360         if (dst) {
1361             return dst;
1362         }
1363     }
1364 
1365     pixmap = glamor_create_pixmap(screen,
1366                                   width,
1367                                   height,
1368                                   PIXMAN_FORMAT_DEPTH(format),
1369                                   GLAMOR_CREATE_PIXMAP_CPU);
1370 
1371     if (!pixmap)
1372         return NULL;
1373 
1374     dst = CreatePicture(0,
1375                         &pixmap->drawable, pFormat, 0, 0, serverClient, &error);
1376     glamor_destroy_pixmap(pixmap);
1377     if (!dst)
1378         return NULL;
1379 
1380     ValidatePicture(dst);
1381 
1382     fbComposite(PictOpSrc, source, NULL, dst, x_source, y_source,
1383                 0, 0, 0, 0, width, height);
1384     return dst;
1385 }
1386 
1387 Bool
glamor_composite_clipped_region(CARD8 op,PicturePtr source,PicturePtr mask,PicturePtr dest,PixmapPtr source_pixmap,PixmapPtr mask_pixmap,PixmapPtr dest_pixmap,RegionPtr region,int x_source,int y_source,int x_mask,int y_mask,int x_dest,int y_dest)1388 glamor_composite_clipped_region(CARD8 op,
1389                                 PicturePtr source,
1390                                 PicturePtr mask,
1391                                 PicturePtr dest,
1392                                 PixmapPtr source_pixmap,
1393                                 PixmapPtr mask_pixmap,
1394                                 PixmapPtr dest_pixmap,
1395                                 RegionPtr region,
1396                                 int x_source,
1397                                 int y_source,
1398                                 int x_mask, int y_mask, int x_dest, int y_dest)
1399 {
1400     glamor_pixmap_private *source_pixmap_priv = glamor_get_pixmap_private(source_pixmap);
1401     glamor_pixmap_private *mask_pixmap_priv = glamor_get_pixmap_private(mask_pixmap);
1402     glamor_pixmap_private *dest_pixmap_priv = glamor_get_pixmap_private(dest_pixmap);
1403     glamor_screen_private *glamor_priv = glamor_get_screen_private(dest_pixmap->drawable.pScreen);
1404     ScreenPtr screen = dest->pDrawable->pScreen;
1405     PicturePtr temp_src = source, temp_mask = mask;
1406     PixmapPtr temp_src_pixmap = source_pixmap;
1407     PixmapPtr temp_mask_pixmap = mask_pixmap;
1408     glamor_pixmap_private *temp_src_priv = source_pixmap_priv;
1409     glamor_pixmap_private *temp_mask_priv = mask_pixmap_priv;
1410     int x_temp_src, y_temp_src, x_temp_mask, y_temp_mask;
1411     BoxPtr extent;
1412     glamor_composite_rect_t rect[10];
1413     glamor_composite_rect_t *prect = rect;
1414     int prect_size = ARRAY_SIZE(rect);
1415     int ok = FALSE;
1416     int i;
1417     int width;
1418     int height;
1419     BoxPtr box;
1420     int nbox;
1421     enum ca_state ca_state = CA_NONE;
1422 
1423     extent = RegionExtents(region);
1424     box = RegionRects(region);
1425     nbox = RegionNumRects(region);
1426     width = extent->x2 - extent->x1;
1427     height = extent->y2 - extent->y1;
1428 
1429     x_temp_src = x_source;
1430     y_temp_src = y_source;
1431     x_temp_mask = x_mask;
1432     y_temp_mask = y_mask;
1433 
1434     DEBUGF("clipped (%d %d) (%d %d) (%d %d) width %d height %d \n",
1435            x_source, y_source, x_mask, y_mask, x_dest, y_dest, width, height);
1436 
1437     /* Is the composite operation equivalent to a copy? */
1438     if (source &&
1439         !mask && !source->alphaMap && !dest->alphaMap
1440         && source->pDrawable && !source->transform
1441         /* CopyArea is only defined with matching depths. */
1442         && dest->pDrawable->depth == source->pDrawable->depth
1443         && ((op == PictOpSrc
1444              && (source->format == dest->format
1445                  || (PICT_FORMAT_COLOR(dest->format)
1446                      && PICT_FORMAT_COLOR(source->format)
1447                      && dest->format == PICT_FORMAT(PICT_FORMAT_BPP(source->format),
1448                                                     PICT_FORMAT_TYPE(source->format),
1449                                                     0,
1450                                                     PICT_FORMAT_R(source->format),
1451                                                     PICT_FORMAT_G(source->format),
1452                                                     PICT_FORMAT_B(source->format)))))
1453             || (op == PictOpOver
1454                 && source->format == dest->format
1455                 && !PICT_FORMAT_A(source->format)))
1456         && x_source >= 0 && y_source >= 0
1457         && (x_source + width) <= source->pDrawable->width
1458         && (y_source + height) <= source->pDrawable->height) {
1459         x_source += source->pDrawable->x;
1460         y_source += source->pDrawable->y;
1461         x_dest += dest->pDrawable->x;
1462         y_dest += dest->pDrawable->y;
1463         glamor_copy(source->pDrawable, dest->pDrawable, NULL,
1464                     box, nbox, x_source - x_dest,
1465                     y_source - y_dest, FALSE, FALSE, 0, NULL);
1466         ok = TRUE;
1467         goto out;
1468     }
1469 
1470     /* XXX is it possible source mask have non-zero drawable.x/y? */
1471     if (source
1472         && ((!source->pDrawable
1473              && (source->pSourcePict->type != SourcePictTypeSolidFill))
1474             || (source->pDrawable
1475                 && !GLAMOR_PIXMAP_PRIV_HAS_FBO(source_pixmap_priv)
1476                 && (source_pixmap->drawable.width != width
1477                     || source_pixmap->drawable.height != height)))) {
1478         temp_src =
1479             glamor_convert_gradient_picture(screen, source,
1480                                             extent->x1 + x_source - x_dest - dest->pDrawable->x,
1481                                             extent->y1 + y_source - y_dest - dest->pDrawable->y,
1482                                             width, height);
1483         if (!temp_src) {
1484             temp_src = source;
1485             goto out;
1486         }
1487         temp_src_pixmap = (PixmapPtr) (temp_src->pDrawable);
1488         temp_src_priv = glamor_get_pixmap_private(temp_src_pixmap);
1489         x_temp_src = -extent->x1 + x_dest + dest->pDrawable->x;
1490         y_temp_src = -extent->y1 + y_dest + dest->pDrawable->y;
1491     }
1492 
1493     if (mask
1494         &&
1495         ((!mask->pDrawable
1496           && (mask->pSourcePict->type != SourcePictTypeSolidFill))
1497          || (mask->pDrawable && !GLAMOR_PIXMAP_PRIV_HAS_FBO(mask_pixmap_priv)
1498              && (mask_pixmap->drawable.width != width
1499                  || mask_pixmap->drawable.height != height)))) {
1500         /* XXX if mask->pDrawable is the same as source->pDrawable, we have an opportunity
1501          * to do reduce one conversion. */
1502         temp_mask =
1503             glamor_convert_gradient_picture(screen, mask,
1504                                             extent->x1 + x_mask - x_dest - dest->pDrawable->x,
1505                                             extent->y1 + y_mask - y_dest - dest->pDrawable->y,
1506                                             width, height);
1507         if (!temp_mask) {
1508             temp_mask = mask;
1509             goto out;
1510         }
1511         temp_mask_pixmap = (PixmapPtr) (temp_mask->pDrawable);
1512         temp_mask_priv = glamor_get_pixmap_private(temp_mask_pixmap);
1513         x_temp_mask = -extent->x1 + x_dest + dest->pDrawable->x;
1514         y_temp_mask = -extent->y1 + y_dest + dest->pDrawable->y;
1515     }
1516 
1517     if (mask && mask->componentAlpha) {
1518         if (glamor_priv->has_dual_blend) {
1519             ca_state = CA_DUAL_BLEND;
1520         } else {
1521             if (op == PictOpOver) {
1522                 if (glamor_pixmap_is_memory(mask_pixmap)) {
1523                     glamor_fallback("two pass not supported on memory pximaps\n");
1524                     goto out;
1525                 }
1526                 ca_state = CA_TWO_PASS;
1527                 op = PictOpOutReverse;
1528             }
1529         }
1530     }
1531 
1532     if (temp_src_pixmap == dest_pixmap) {
1533         glamor_fallback("source and dest pixmaps are the same\n");
1534         goto out;
1535     }
1536     if (temp_mask_pixmap == dest_pixmap) {
1537         glamor_fallback("mask and dest pixmaps are the same\n");
1538         goto out;
1539     }
1540 
1541     x_dest += dest->pDrawable->x;
1542     y_dest += dest->pDrawable->y;
1543     if (temp_src && temp_src->pDrawable) {
1544         x_temp_src += temp_src->pDrawable->x;
1545         y_temp_src += temp_src->pDrawable->y;
1546     }
1547     if (temp_mask && temp_mask->pDrawable) {
1548         x_temp_mask += temp_mask->pDrawable->x;
1549         y_temp_mask += temp_mask->pDrawable->y;
1550     }
1551 
1552     if (nbox > ARRAY_SIZE(rect)) {
1553         prect = calloc(nbox, sizeof(*prect));
1554         if (prect)
1555             prect_size = nbox;
1556         else {
1557             prect = rect;
1558             prect_size = ARRAY_SIZE(rect);
1559         }
1560     }
1561     while (nbox) {
1562         int box_cnt;
1563 
1564         box_cnt = nbox > prect_size ? prect_size : nbox;
1565         for (i = 0; i < box_cnt; i++) {
1566             prect[i].x_src = box[i].x1 + x_temp_src - x_dest;
1567             prect[i].y_src = box[i].y1 + y_temp_src - y_dest;
1568             prect[i].x_mask = box[i].x1 + x_temp_mask - x_dest;
1569             prect[i].y_mask = box[i].y1 + y_temp_mask - y_dest;
1570             prect[i].x_dst = box[i].x1;
1571             prect[i].y_dst = box[i].y1;
1572             prect[i].width = box[i].x2 - box[i].x1;
1573             prect[i].height = box[i].y2 - box[i].y1;
1574             DEBUGF("dest %d %d \n", prect[i].x_dst, prect[i].y_dst);
1575         }
1576         ok = glamor_composite_with_shader(op, temp_src, temp_mask, dest,
1577                                           temp_src_pixmap, temp_mask_pixmap, dest_pixmap,
1578                                           temp_src_priv, temp_mask_priv,
1579                                           dest_pixmap_priv,
1580                                           box_cnt, prect, ca_state);
1581         if (!ok)
1582             break;
1583         nbox -= box_cnt;
1584         box += box_cnt;
1585     }
1586 
1587     if (prect != rect)
1588         free(prect);
1589  out:
1590     if (temp_src != source)
1591         FreePicture(temp_src, 0);
1592     if (temp_mask != mask)
1593         FreePicture(temp_mask, 0);
1594 
1595     return ok;
1596 }
1597 
1598 void
glamor_composite(CARD8 op,PicturePtr source,PicturePtr mask,PicturePtr dest,INT16 x_source,INT16 y_source,INT16 x_mask,INT16 y_mask,INT16 x_dest,INT16 y_dest,CARD16 width,CARD16 height)1599 glamor_composite(CARD8 op,
1600                  PicturePtr source,
1601                  PicturePtr mask,
1602                  PicturePtr dest,
1603                  INT16 x_source,
1604                  INT16 y_source,
1605                  INT16 x_mask,
1606                  INT16 y_mask,
1607                  INT16 x_dest, INT16 y_dest, CARD16 width, CARD16 height)
1608 {
1609     ScreenPtr screen = dest->pDrawable->pScreen;
1610     PixmapPtr dest_pixmap = glamor_get_drawable_pixmap(dest->pDrawable);
1611     PixmapPtr source_pixmap = NULL, mask_pixmap = NULL;
1612     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
1613     RegionRec region;
1614     BoxPtr extent;
1615     int nbox, ok = FALSE;
1616     int force_clip = 0;
1617 
1618     if (!GLAMOR_PREFER_GL())
1619         goto fail;
1620 
1621     if (source->pDrawable) {
1622         source_pixmap = glamor_get_drawable_pixmap(source->pDrawable);
1623         if (glamor_pixmap_drm_only(source_pixmap))
1624             goto fail;
1625     }
1626 
1627     if (mask && mask->pDrawable) {
1628         mask_pixmap = glamor_get_drawable_pixmap(mask->pDrawable);
1629         if (glamor_pixmap_drm_only(mask_pixmap))
1630             goto fail;
1631     }
1632 
1633     DEBUGF
1634         ("source pixmap %p (%d %d) mask(%d %d) dest(%d %d) width %d height %d \n",
1635          source_pixmap, x_source, y_source, x_mask, y_mask, x_dest, y_dest,
1636          width, height);
1637 
1638     if (!glamor_pixmap_has_fbo(dest_pixmap))
1639         goto fail;
1640 
1641     if (op >= ARRAY_SIZE(composite_op_info)) {
1642         glamor_fallback("Unsupported composite op %x\n", op);
1643         goto fail;
1644     }
1645 
1646     if (mask && mask->componentAlpha && !glamor_priv->has_dual_blend) {
1647         if (op == PictOpAtop
1648             || op == PictOpAtopReverse
1649             || op == PictOpXor || op >= PictOpSaturate) {
1650             glamor_fallback("glamor_composite(): component alpha op %x\n", op);
1651             goto fail;
1652         }
1653     }
1654 
1655     if ((source && source->filter >= PictFilterConvolution)
1656         || (mask && mask->filter >= PictFilterConvolution)) {
1657         glamor_fallback("glamor_composite(): unsupported filter\n");
1658         goto fail;
1659     }
1660 
1661     if (!miComputeCompositeRegion(&region,
1662                                   source, mask, dest,
1663                                   x_source +
1664                                   (source_pixmap ? source->pDrawable->x : 0),
1665                                   y_source +
1666                                   (source_pixmap ? source->pDrawable->y : 0),
1667                                   x_mask +
1668                                   (mask_pixmap ? mask->pDrawable->x : 0),
1669                                   y_mask +
1670                                   (mask_pixmap ? mask->pDrawable->y : 0),
1671                                   x_dest + dest->pDrawable->x,
1672                                   y_dest + dest->pDrawable->y, width, height)) {
1673         return;
1674     }
1675 
1676     nbox = REGION_NUM_RECTS(&region);
1677     DEBUGF("first clipped when compositing.\n");
1678     DEBUGRegionPrint(&region);
1679     extent = RegionExtents(&region);
1680     if (nbox == 0)
1681         return;
1682 
1683     /* If destination is not a large pixmap, but the region is larger
1684      * than texture size limitation, and source or mask is memory pixmap,
1685      * then there may be need to load a large memory pixmap to a
1686      * texture, and this is not permitted. Then we force to clip the
1687      * destination and make sure latter will not upload a large memory
1688      * pixmap. */
1689     if (!glamor_check_fbo_size(glamor_priv,
1690                                extent->x2 - extent->x1, extent->y2 - extent->y1)
1691         && glamor_pixmap_is_large(dest_pixmap)
1692         && ((source_pixmap
1693              && (glamor_pixmap_is_memory(source_pixmap) ||
1694                  source->repeatType == RepeatPad))
1695             || (mask_pixmap &&
1696                 (glamor_pixmap_is_memory(mask_pixmap) ||
1697                  mask->repeatType == RepeatPad))
1698             || (!source_pixmap &&
1699                 (source->pSourcePict->type != SourcePictTypeSolidFill))
1700             || (!mask_pixmap && mask &&
1701                 mask->pSourcePict->type != SourcePictTypeSolidFill)))
1702         force_clip = 1;
1703 
1704     if (force_clip || glamor_pixmap_is_large(dest_pixmap)
1705         || (source_pixmap
1706             && glamor_pixmap_is_large(source_pixmap))
1707         || (mask_pixmap && glamor_pixmap_is_large(mask_pixmap)))
1708         ok = glamor_composite_largepixmap_region(op,
1709                                                  source, mask, dest,
1710                                                  source_pixmap,
1711                                                  mask_pixmap,
1712                                                  dest_pixmap,
1713                                                  &region, force_clip,
1714                                                  x_source, y_source,
1715                                                  x_mask, y_mask,
1716                                                  x_dest, y_dest, width, height);
1717     else
1718         ok = glamor_composite_clipped_region(op, source,
1719                                              mask, dest,
1720                                              source_pixmap,
1721                                              mask_pixmap,
1722                                              dest_pixmap,
1723                                              &region,
1724                                              x_source, y_source,
1725                                              x_mask, y_mask, x_dest, y_dest);
1726 
1727     REGION_UNINIT(dest->pDrawable->pScreen, &region);
1728 
1729     if (ok)
1730         return;
1731 
1732  fail:
1733 
1734     glamor_fallback
1735         ("from picts %p:%p %dx%d / %p:%p %d x %d (%c,%c)  to pict %p:%p %dx%d (%c)\n",
1736          source, source->pDrawable,
1737          source->pDrawable ? source->pDrawable->width : 0,
1738          source->pDrawable ? source->pDrawable->height : 0, mask,
1739          (!mask) ? NULL : mask->pDrawable,
1740          (!mask || !mask->pDrawable) ? 0 : mask->pDrawable->width,
1741          (!mask || !mask->pDrawable) ? 0 : mask->pDrawable->height,
1742          glamor_get_picture_location(source),
1743          glamor_get_picture_location(mask),
1744          dest, dest->pDrawable,
1745          dest->pDrawable->width, dest->pDrawable->height,
1746          glamor_get_picture_location(dest));
1747 
1748     if (glamor_prepare_access_picture_box(dest, GLAMOR_ACCESS_RW,
1749                                           x_dest, y_dest, width, height) &&
1750         glamor_prepare_access_picture_box(source, GLAMOR_ACCESS_RO,
1751                                           x_source, y_source, width, height) &&
1752         glamor_prepare_access_picture_box(mask, GLAMOR_ACCESS_RO,
1753                                           x_mask, y_mask, width, height))
1754     {
1755         fbComposite(op,
1756                     source, mask, dest,
1757                     x_source, y_source,
1758                     x_mask, y_mask, x_dest, y_dest, width, height);
1759     }
1760     glamor_finish_access_picture(mask);
1761     glamor_finish_access_picture(source);
1762     glamor_finish_access_picture(dest);
1763 }
1764