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