glamor_render.c 61.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright © 2009 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
25 26
 *    Zhigang Gong <zhigang.gong@linux.intel.com>
 *    Junyan He <junyan.he@linux.intel.com>
27 28 29 30 31 32 33 34 35 36 37 38
 *
 */

/** @file glamor_render.c
 *
 * Render acceleration implementation
 */

#include "glamor_priv.h"

#ifdef RENDER
#include "mipict.h"
39
#include "fbpict.h"
40 41 42 43 44 45
#if 0
//#define DEBUGF(str, ...)  do {} while(0)
#define DEBUGF(str, ...) ErrorF(str, ##__VA_ARGS__)
//#define DEBUGRegionPrint(x) do {} while (0)
#define DEBUGRegionPrint RegionPrint
#endif
46
static struct blendinfo composite_op_info[] = {
Zhigang Gong's avatar
Zhigang Gong committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60
	[PictOpClear] = {0, 0, GL_ZERO, GL_ZERO},
	[PictOpSrc] = {0, 0, GL_ONE, GL_ZERO},
	[PictOpDst] = {0, 0, GL_ZERO, GL_ONE},
	[PictOpOver] = {0, 1, GL_ONE, GL_ONE_MINUS_SRC_ALPHA},
	[PictOpOverReverse] = {1, 0, GL_ONE_MINUS_DST_ALPHA, GL_ONE},
	[PictOpIn] = {1, 0, GL_DST_ALPHA, GL_ZERO},
	[PictOpInReverse] = {0, 1, GL_ZERO, GL_SRC_ALPHA},
	[PictOpOut] = {1, 0, GL_ONE_MINUS_DST_ALPHA, GL_ZERO},
	[PictOpOutReverse] = {0, 1, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA},
	[PictOpAtop] = {1, 1, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
	[PictOpAtopReverse] = {1, 1, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA},
	[PictOpXor] =
	    {1, 1, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
	[PictOpAdd] = {0, 0, GL_ONE, GL_ONE},
61
};
62
#define RepeatFix			10
63
static GLuint
Zhigang Gong's avatar
Zhigang Gong committed
64 65
glamor_create_composite_fs(glamor_gl_dispatch * dispatch,
			   struct shader_key *key)
66
{
67 68 69 70 71
	const char *repeat_define =
	    "#define RepeatNone               	      0\n"
	    "#define RepeatNormal                     1\n"
	    "#define RepeatPad                        2\n"
	    "#define RepeatReflect                    3\n"
72
	    "#define RepeatFix		      	      10\n"
73 74
	    "uniform int 			source_repeat_mode;\n"
	    "uniform int 			mask_repeat_mode;\n";
75 76
	const char *relocate_texture =
	    GLAMOR_DEFAULT_PRECISION
77
	    "vec2 rel_tex_coord(vec2 texture, vec4 wh, int repeat) \n"
78 79
	    "{\n"
	    "   vec2 rel_tex; \n"
80
	    "   rel_tex = texture * wh.xy; \n"
81 82 83
	    "	if (repeat == RepeatNone)\n"
	    "		return rel_tex; \n"
	    "   else if (repeat == RepeatNormal) \n"
84
	    "   	rel_tex = floor(rel_tex) + (fract(rel_tex) / wh.xy); 	\n"
85
	    "   else if(repeat == RepeatPad) { \n"
86 87 88
	    "           if (rel_tex.x >= 1.0) rel_tex.x = 1.0 - wh.z * wh.x / 2.;  	\n"
	    "		else if(rel_tex.x < 0.0) rel_tex.x = 0.0;	  	\n"
	    "           if (rel_tex.y >= 1.0) rel_tex.y = 1.0 - wh.w * wh.y / 2.;	\n"
89
	    "		else if(rel_tex.y < 0.0) rel_tex.y = 0.0;	\n"
90
	    "   	rel_tex = rel_tex / wh.xy; \n"
91 92 93 94 95 96 97 98 99 100 101
	    "    } \n"
	    "   else if(repeat == RepeatReflect) {\n"
	    "		if ((1.0 - mod(abs(floor(rel_tex.x)), 2.0)) < 0.001)\n"
	    "			rel_tex.x = 2.0 - (1.0 - fract(rel_tex.x))/wh.x;\n"
	    "		else \n"
	    "			rel_tex.x = fract(rel_tex.x)/wh.x;\n"
	    "		if ((1.0 - mod(abs(floor(rel_tex.y)), 2.0)) < 0.001)\n"
	    "			rel_tex.y = 2.0 - (1.0 - fract(rel_tex.y))/wh.y;\n"
	    "		else \n"
	    "			rel_tex.y = fract(rel_tex.y)/wh.y;\n"
	    "    } \n"
102 103
            "   return rel_tex; \n"
	    "}\n";
104 105 106
	/* The texture and the pixmap size is not match eaxctly, so can't sample it directly.
	 * rel_sampler will recalculate the texture coords.*/
	const char *rel_sampler =
107
	    " vec4 rel_sampler(sampler2D tex_image, vec2 tex, vec4 wh, int repeat, int set_alpha)\n"
108 109 110
	    "{\n"
	    "	tex = rel_tex_coord(tex, wh, repeat - RepeatFix);\n"
	    "   if (repeat == RepeatFix) {\n"
111 112
	    "		if (!(tex.x >= 0.0 && tex.x < 1.0 \n"
	    "		    && tex.y >= 0.0 && tex.y < 1.0))\n"
113
	    "			return vec4(0.0, 0.0, 0.0, set_alpha);\n"
114
	    "		tex = (fract(tex) / wh.xy);\n"
115 116 117 118 119 120 121
	    "	}\n"
	    "	if (set_alpha != 1)\n"
	    "		return texture2D(tex_image, tex);\n"
	    "	else\n"
	    "		return vec4(texture2D(tex_image, tex).rgb, 1.0);\n"
	    "}\n";

Zhigang Gong's avatar
Zhigang Gong committed
122 123 124 125 126 127 128 129
	const char *source_solid_fetch =
	    GLAMOR_DEFAULT_PRECISION
	    "uniform vec4 source;\n"
	    "vec4 get_source()\n" "{\n" "	return source;\n" "}\n";
	const char *source_alpha_pixmap_fetch =
	    GLAMOR_DEFAULT_PRECISION
	    "varying vec2 source_texture;\n"
	    "uniform sampler2D source_sampler;\n"
130
	    "uniform vec4 source_wh;"
Zhigang Gong's avatar
Zhigang Gong committed
131
	    "vec4 get_source()\n"
132
	    "{\n"
133
	    "   if (source_repeat_mode < RepeatFix)\n"
134
	    "		return texture2D(source_sampler, source_texture);\n"
135 136 137
	    "   else \n"
	    "		return rel_sampler(source_sampler, source_texture,\n"
	    "				   source_wh, source_repeat_mode, 0);\n"
Zhigang Gong's avatar
Zhigang Gong committed
138 139 140
	    "}\n";
	const char *source_pixmap_fetch =
	    GLAMOR_DEFAULT_PRECISION "varying vec2 source_texture;\n"
141
	    "uniform sampler2D source_sampler;\n"
142
	    "uniform vec4 source_wh;\n"
143
	    "vec4 get_source()\n"
Zhigang Gong's avatar
Zhigang Gong committed
144
	    "{\n"
145
	    "   if (source_repeat_mode < RepeatFix) \n"
146 147
	    "		return vec4(texture2D(source_sampler, source_texture).rgb, 1);\n"
	    "	else \n"
148 149
	    "		return rel_sampler(source_sampler, source_texture,\n"
	    "				   source_wh, source_repeat_mode, 1);\n"
Zhigang Gong's avatar
Zhigang Gong committed
150 151 152 153 154 155
	    "}\n";
	const char *mask_solid_fetch =
	    GLAMOR_DEFAULT_PRECISION "uniform vec4 mask;\n"
	    "vec4 get_mask()\n" "{\n" "	return mask;\n" "}\n";
	const char *mask_alpha_pixmap_fetch =
	    GLAMOR_DEFAULT_PRECISION "varying vec2 mask_texture;\n"
156
	    "uniform sampler2D mask_sampler;\n"
157
	    "uniform vec4 mask_wh;\n"
158 159
	    "vec4 get_mask()\n"
	    "{\n"
160
	    "   if (mask_repeat_mode < RepeatFix) \n"
161 162
	    "		return texture2D(mask_sampler, mask_texture);\n"
	    "   else \n"
163 164
	    "		return rel_sampler(mask_sampler, mask_texture,\n"
	    "				   mask_wh, mask_repeat_mode, 0);\n"
165
	    "}\n";
Zhigang Gong's avatar
Zhigang Gong committed
166 167
	const char *mask_pixmap_fetch =
	    GLAMOR_DEFAULT_PRECISION "varying vec2 mask_texture;\n"
168
	    "uniform sampler2D mask_sampler;\n"
169
	    "uniform vec4 mask_wh;\n"
170 171
	    "vec4 get_mask()\n"
	    "{\n"
172
	    "   if (mask_repeat_mode < RepeatFix) \n"
173 174
	    "   	return vec4(texture2D(mask_sampler, mask_texture).rgb, 1);\n"
	    "   else \n"
175 176
	    "		return rel_sampler(mask_sampler, mask_texture,\n"
	    "				   mask_wh, mask_repeat_mode, 1);\n"
Zhigang Gong's avatar
Zhigang Gong committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	    "}\n";
	const char *in_source_only =
	    GLAMOR_DEFAULT_PRECISION "void main()\n" "{\n"
	    "	gl_FragColor = get_source();\n" "}\n";
	const char *in_normal =
	    GLAMOR_DEFAULT_PRECISION "void main()\n" "{\n"
	    "	gl_FragColor = get_source() * get_mask().a;\n" "}\n";
	const char *in_ca_source =
	    GLAMOR_DEFAULT_PRECISION "void main()\n" "{\n"
	    "	gl_FragColor = get_source() * get_mask();\n" "}\n";
	const char *in_ca_alpha =
	    GLAMOR_DEFAULT_PRECISION "void main()\n" "{\n"
	    "	gl_FragColor = get_source().a * get_mask();\n" "}\n";
	char *source;
	const char *source_fetch;
	const char *mask_fetch = "";
	const char *in;
	GLuint prog;

	switch (key->source) {
	case SHADER_SOURCE_SOLID:
		source_fetch = source_solid_fetch;
		break;
	case SHADER_SOURCE_TEXTURE_ALPHA:
		source_fetch = source_alpha_pixmap_fetch;
		break;
	case SHADER_SOURCE_TEXTURE:
		source_fetch = source_pixmap_fetch;
		break;
	default:
		FatalError("Bad composite shader source");
	}

	switch (key->mask) {
	case SHADER_MASK_NONE:
		break;
	case SHADER_MASK_SOLID:
		mask_fetch = mask_solid_fetch;
		break;
	case SHADER_MASK_TEXTURE_ALPHA:
		mask_fetch = mask_alpha_pixmap_fetch;
		break;
	case SHADER_MASK_TEXTURE:
		mask_fetch = mask_pixmap_fetch;
		break;
	default:
		FatalError("Bad composite shader mask");
	}

	switch (key->in) {
	case SHADER_IN_SOURCE_ONLY:
		in = in_source_only;
		break;
	case SHADER_IN_NORMAL:
		in = in_normal;
		break;
	case SHADER_IN_CA_SOURCE:
		in = in_ca_source;
		break;
	case SHADER_IN_CA_ALPHA:
		in = in_ca_alpha;
		break;
	default:
		FatalError("Bad composite IN type");
	}

243
	XNFasprintf(&source, "%s%s%s%s%s%s", repeat_define, relocate_texture, rel_sampler,source_fetch, mask_fetch, in);
Zhigang Gong's avatar
Zhigang Gong committed
244 245 246 247 248 249 250


	prog = glamor_compile_glsl_prog(dispatch, GL_FRAGMENT_SHADER,
					source);
	free(source);

	return prog;
251 252 253
}

static GLuint
Zhigang Gong's avatar
Zhigang Gong committed
254 255
glamor_create_composite_vs(glamor_gl_dispatch * dispatch,
			   struct shader_key *key)
256
{
Zhigang Gong's avatar
Zhigang Gong committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	const char *main_opening =
	    "attribute vec4 v_position;\n"
	    "attribute vec4 v_texcoord0;\n"
	    "attribute vec4 v_texcoord1;\n"
	    "varying vec2 source_texture;\n"
	    "varying vec2 mask_texture;\n"
	    "void main()\n" "{\n" "	gl_Position = v_position;\n";
	const char *source_coords =
	    "	source_texture = v_texcoord0.xy;\n";
	const char *mask_coords = "	mask_texture = v_texcoord1.xy;\n";
	const char *main_closing = "}\n";
	const char *source_coords_setup = "";
	const char *mask_coords_setup = "";
	char *source;
	GLuint prog;

	if (key->source != SHADER_SOURCE_SOLID)
		source_coords_setup = source_coords;

	if (key->mask != SHADER_MASK_NONE
	    && key->mask != SHADER_MASK_SOLID)
		mask_coords_setup = mask_coords;

	XNFasprintf(&source,
		    "%s%s%s%s",
		    main_opening,
		    source_coords_setup, mask_coords_setup, main_closing);

	prog =
	    glamor_compile_glsl_prog(dispatch, GL_VERTEX_SHADER, source);
	free(source);

	return prog;
290 291 292
}

static void
293
glamor_create_composite_shader(ScreenPtr screen, struct shader_key *key,
Zhigang Gong's avatar
Zhigang Gong committed
294
			       glamor_composite_shader * shader)
295
{
Zhigang Gong's avatar
Zhigang Gong committed
296 297 298
	GLuint vs, fs, prog;
	GLint source_sampler_uniform_location,
	    mask_sampler_uniform_location;
Chris Wilson's avatar
Chris Wilson committed
299 300
	glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
	glamor_gl_dispatch *dispatch;
Zhigang Gong's avatar
Zhigang Gong committed
301

Chris Wilson's avatar
Chris Wilson committed
302
	dispatch = glamor_get_dispatch(glamor_priv);
Zhigang Gong's avatar
Zhigang Gong committed
303 304
	vs = glamor_create_composite_vs(dispatch, key);
	if (vs == 0)
Chris Wilson's avatar
Chris Wilson committed
305
		goto out;
Zhigang Gong's avatar
Zhigang Gong committed
306 307
	fs = glamor_create_composite_fs(dispatch, key);
	if (fs == 0)
Chris Wilson's avatar
Chris Wilson committed
308
		goto out;
Zhigang Gong's avatar
Zhigang Gong committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

	prog = dispatch->glCreateProgram();
	dispatch->glAttachShader(prog, vs);
	dispatch->glAttachShader(prog, fs);

	dispatch->glBindAttribLocation(prog, GLAMOR_VERTEX_POS,
				       "v_position");
	dispatch->glBindAttribLocation(prog, GLAMOR_VERTEX_SOURCE,
				       "v_texcoord0");
	dispatch->glBindAttribLocation(prog, GLAMOR_VERTEX_MASK,
				       "v_texcoord1");

	glamor_link_glsl_prog(dispatch, prog);

	shader->prog = prog;

	dispatch->glUseProgram(prog);

	if (key->source == SHADER_SOURCE_SOLID) {
		shader->source_uniform_location =
		    dispatch->glGetUniformLocation(prog, "source");
	} else {
		source_sampler_uniform_location =
		    dispatch->glGetUniformLocation(prog, "source_sampler");
		dispatch->glUniform1i(source_sampler_uniform_location, 0);
334
		shader->source_wh = dispatch->glGetUniformLocation(prog, "source_wh");
335
		shader->source_repeat_mode = dispatch->glGetUniformLocation(prog, "source_repeat_mode");
Zhigang Gong's avatar
Zhigang Gong committed
336 337 338 339 340 341 342 343 344 345 346 347
	}

	if (key->mask != SHADER_MASK_NONE) {
		if (key->mask == SHADER_MASK_SOLID) {
			shader->mask_uniform_location =
			    dispatch->glGetUniformLocation(prog, "mask");
		} else {
			mask_sampler_uniform_location =
			    dispatch->glGetUniformLocation(prog,
							   "mask_sampler");
			dispatch->glUniform1i
			    (mask_sampler_uniform_location, 1);
348
			shader->mask_wh = dispatch->glGetUniformLocation(prog, "mask_wh");
349
			shader->mask_repeat_mode = dispatch->glGetUniformLocation(prog, "mask_repeat_mode");
Zhigang Gong's avatar
Zhigang Gong committed
350 351
		}
	}
Chris Wilson's avatar
Chris Wilson committed
352 353 354

out:
	glamor_put_dispatch(glamor_priv);
355 356
}

357
static glamor_composite_shader *
Zhigang Gong's avatar
Zhigang Gong committed
358 359 360
glamor_lookup_composite_shader(ScreenPtr screen, struct
			       shader_key
			       *key)
361
{
Zhigang Gong's avatar
Zhigang Gong committed
362 363 364
	glamor_screen_private *glamor_priv =
	    glamor_get_screen_private(screen);
	glamor_composite_shader *shader;
365

Zhigang Gong's avatar
Zhigang Gong committed
366 367 368 369 370
	shader =
	    &glamor_priv->composite_shader[key->source][key->
							mask][key->in];
	if (shader->prog == 0)
		glamor_create_composite_shader(screen, key, shader);
371

Zhigang Gong's avatar
Zhigang Gong committed
372
	return shader;
373
}
Zhigang Gong's avatar
Zhigang Gong committed
374

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
static void
glamor_init_eb(unsigned short *eb, int vert_cnt)
{
	int i, j;
	for(i = 0, j = 0; j < vert_cnt; i += 6, j += 4)
	{
		eb[i] = j;
		eb[i + 1] = j + 1;
		eb[i + 2] = j + 2;
		eb[i + 3] = j;
		eb[i + 4] = j + 2;
		eb[i + 5] = j + 3;
	}
}

390 391 392
void
glamor_init_composite_shaders(ScreenPtr screen)
{
393 394
	glamor_screen_private *glamor_priv;
	glamor_gl_dispatch *dispatch;
395
	unsigned short *eb;
396
	float *vb = NULL;
397
	int eb_size;
398

399
	glamor_priv = glamor_get_screen_private(screen);
Chris Wilson's avatar
Chris Wilson committed
400
	dispatch = glamor_get_dispatch(glamor_priv);
401 402 403
	dispatch->glGenBuffers(1, &glamor_priv->vbo);
	dispatch->glGenBuffers(1, &glamor_priv->ebo);
	dispatch->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, glamor_priv->ebo);
404

405 406 407 408 409 410 411 412 413 414
	eb_size = GLAMOR_COMPOSITE_VBO_VERT_CNT * sizeof(short) * 2;

	if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
		dispatch->glBufferData(GL_ELEMENT_ARRAY_BUFFER,
				       eb_size,
				       NULL, GL_DYNAMIC_DRAW);
		eb = dispatch->glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
	}
	else {
		vb = malloc(GLAMOR_COMPOSITE_VBO_VERT_CNT * sizeof(float) * 2);
Chris Wilson's avatar
Chris Wilson committed
415 416
		if (vb == NULL)
			FatalError("Failed to allocate vb memory.\n");
417 418
		eb = malloc(eb_size);
	}
419

Chris Wilson's avatar
Chris Wilson committed
420
	if (eb == NULL)
421
		FatalError("fatal error, fail to get element buffer. GL context may be not created correctly.\n");
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	glamor_init_eb(eb, GLAMOR_COMPOSITE_VBO_VERT_CNT);

	if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
		dispatch->glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
		dispatch->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	} else {
		dispatch->glBufferData(GL_ELEMENT_ARRAY_BUFFER,
				       eb_size,
				       eb, GL_DYNAMIC_DRAW);
		dispatch->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

		dispatch->glBindBuffer(GL_ARRAY_BUFFER, glamor_priv->vbo);
		dispatch->glBufferData(GL_ARRAY_BUFFER,
				       GLAMOR_COMPOSITE_VBO_VERT_CNT * sizeof(float) * 2,
				       NULL, GL_DYNAMIC_DRAW);
		dispatch->glBindBuffer(GL_ARRAY_BUFFER, 0);

		free(eb);
		glamor_priv->vb = (char*)vb;
	}
Chris Wilson's avatar
Chris Wilson committed
442 443

	glamor_put_dispatch(glamor_priv);
444
}
445 446 447 448 449 450 451 452 453 454

void
glamor_fini_composite_shaders(ScreenPtr screen)
{
	glamor_screen_private *glamor_priv;
	glamor_gl_dispatch *dispatch;
	glamor_composite_shader *shader;
	int i,j,k;

	glamor_priv = glamor_get_screen_private(screen);
Chris Wilson's avatar
Chris Wilson committed
455
	dispatch = glamor_get_dispatch(glamor_priv);
456 457 458 459 460 461 462 463 464 465 466
	dispatch->glDeleteBuffers(1, &glamor_priv->vbo);
	dispatch->glDeleteBuffers(1, &glamor_priv->ebo);

	for(i = 0; i < SHADER_SOURCE_COUNT; i++)
		for(j = 0; j < SHADER_MASK_COUNT; j++)
			for(k = 0; k < SHADER_IN_COUNT; k++)
			{
				shader = &glamor_priv->composite_shader[i][j][k];
				if (shader->prog)
					dispatch->glDeleteProgram(shader->prog);
			}
467 468 469
	if (glamor_priv->gl_flavor != GLAMOR_GL_DESKTOP
	    && glamor_priv->vb)
		free(glamor_priv->vb);
470

Chris Wilson's avatar
Chris Wilson committed
471 472
	glamor_put_dispatch(glamor_priv);
}
473

474 475
static Bool
glamor_set_composite_op(ScreenPtr screen,
476 477
			CARD8 op, struct blendinfo *op_info_result,
			PicturePtr dest, PicturePtr mask)
478
{
Zhigang Gong's avatar
Zhigang Gong committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	GLenum source_blend, dest_blend;
	struct blendinfo *op_info;

	if (op >= ARRAY_SIZE(composite_op_info)) {
		glamor_fallback("unsupported render op %d \n", op);
		return GL_FALSE;
	}
	op_info = &composite_op_info[op];

	source_blend = op_info->source_blend;
	dest_blend = op_info->dest_blend;

	/* If there's no dst alpha channel, adjust the blend op so that we'll treat
	 * it as always 1.
	 */
	if (PICT_FORMAT_A(dest->format) == 0 && op_info->dest_alpha) {
		if (source_blend == GL_DST_ALPHA)
			source_blend = GL_ONE;
		else if (source_blend == GL_ONE_MINUS_DST_ALPHA)
			source_blend = GL_ZERO;
	}

	/* Set up the source alpha value for blending in component alpha mode. */
	if (mask && mask->componentAlpha
	    && PICT_FORMAT_RGB(mask->format) != 0 && op_info->source_alpha)
	{
		if (dest_blend == GL_SRC_ALPHA)
			dest_blend = GL_SRC_COLOR;
		else if (dest_blend == GL_ONE_MINUS_SRC_ALPHA)
			dest_blend = GL_ONE_MINUS_SRC_COLOR;
	}

511 512 513 514 515
	op_info_result->source_blend = source_blend;
	op_info_result->dest_blend = dest_blend;
	op_info_result->source_alpha = op_info->source_alpha;
	op_info_result->dest_alpha = op_info->dest_alpha;

Zhigang Gong's avatar
Zhigang Gong committed
516
	return TRUE;
517 518 519
}

static void
520
glamor_set_composite_texture(glamor_screen_private *glamor_priv, int unit,
Zhigang Gong's avatar
Zhigang Gong committed
521
			     PicturePtr picture,
522
			     glamor_pixmap_private * pixmap_priv,
523
			     GLuint wh_location, GLuint repeat_location)
524
{
Chris Wilson's avatar
Chris Wilson committed
525
	glamor_gl_dispatch *dispatch;
526
	float wh[4];
527
	int repeat_type;
Chris Wilson's avatar
Chris Wilson committed
528 529

	dispatch = glamor_get_dispatch(glamor_priv);
Zhigang Gong's avatar
Zhigang Gong committed
530
	dispatch->glActiveTexture(GL_TEXTURE0 + unit);
531
	dispatch->glBindTexture(GL_TEXTURE_2D, pixmap_priv->base.fbo->tex);
532
	repeat_type = picture->repeatType;
Zhigang Gong's avatar
Zhigang Gong committed
533 534
	switch (picture->repeatType) {
	case RepeatNone:
535
#ifndef GLAMOR_GLES2
Zhigang Gong's avatar
Zhigang Gong committed
536 537 538 539 540
		/* XXX  GLES2 doesn't support GL_CLAMP_TO_BORDER. */
		dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
					  GL_CLAMP_TO_BORDER);
		dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
					  GL_CLAMP_TO_BORDER);
541 542 543 544 545
#else
		dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
					  GL_CLAMP_TO_EDGE);
		dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
					  GL_CLAMP_TO_EDGE);
546
#endif
Zhigang Gong's avatar
Zhigang Gong committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
		break;
	case RepeatNormal:
		dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
					  GL_REPEAT);
		dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
					  GL_REPEAT);
		break;
	case RepeatPad:
		dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
					  GL_CLAMP_TO_EDGE);
		dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
					  GL_CLAMP_TO_EDGE);
		break;
	case RepeatReflect:
		dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
					  GL_MIRRORED_REPEAT);
		dispatch->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
					  GL_MIRRORED_REPEAT);
		break;
	}

	switch (picture->filter) {
569
	default:
Zhigang Gong's avatar
Zhigang Gong committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
	case PictFilterNearest:
		dispatch->glTexParameteri(GL_TEXTURE_2D,
					  GL_TEXTURE_MIN_FILTER,
					  GL_NEAREST);
		dispatch->glTexParameteri(GL_TEXTURE_2D,
					  GL_TEXTURE_MAG_FILTER,
					  GL_NEAREST);
		break;
	case PictFilterBilinear:
		dispatch->glTexParameteri(GL_TEXTURE_2D,
					  GL_TEXTURE_MIN_FILTER,
					  GL_LINEAR);
		dispatch->glTexParameteri(GL_TEXTURE_2D,
					  GL_TEXTURE_MAG_FILTER,
					  GL_LINEAR);
		break;
	}
587
#ifndef GLAMOR_GLES2
Zhigang Gong's avatar
Zhigang Gong committed
588
	dispatch->glEnable(GL_TEXTURE_2D);
589
#endif
590 591 592 593

	/*
	 *  GLES2 doesn't support RepeatNone. We need to fix it anyway.
	 *
594
	 **/
595 596 597 598 599 600
	if (repeat_type != RepeatNone)
		repeat_type += RepeatFix;
	else if (glamor_priv->gl_flavor == GLAMOR_GL_ES2
		 || pixmap_priv->type == GLAMOR_TEXTURE_LARGE) {
		if (picture->transform
		   || (GLAMOR_PIXMAP_FBO_NOT_EAXCT_SIZE(pixmap_priv)))
601
			repeat_type += RepeatFix;
602 603 604
	}
	if (repeat_type >= RepeatFix) {
		glamor_pixmap_fbo_fix_wh_ratio(wh, pixmap_priv);
605 606 607
		if ((wh[0] != 1.0 || wh[1] != 1.0 )
		     || (glamor_priv->gl_flavor == GLAMOR_GL_ES2
			  && repeat_type == RepeatFix))
608 609 610
			dispatch->glUniform4fv(wh_location, 1, wh);
		else
			repeat_type -= RepeatFix;
611
	}
612
	dispatch->glUniform1i(repeat_location, repeat_type);
Chris Wilson's avatar
Chris Wilson committed
613
	glamor_put_dispatch(glamor_priv);
614 615 616
}

static void
Zhigang Gong's avatar
Zhigang Gong committed
617 618
glamor_set_composite_solid(glamor_gl_dispatch * dispatch, float *color,
			   GLint uniform_location)
619
{
Zhigang Gong's avatar
Zhigang Gong committed
620
	dispatch->glUniform4fv(uniform_location, 1, color);
621 622
}

623
static int
Zhigang Gong's avatar
Zhigang Gong committed
624
compatible_formats(CARD8 op, PicturePtr dst, PicturePtr src)
625
{
Zhigang Gong's avatar
Zhigang Gong committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
	if (op == PictOpSrc) {
		if (src->format == dst->format)
			return 1;

		if (src->format == PICT_a8r8g8b8
		    && dst->format == PICT_x8r8g8b8)
			return 1;

		if (src->format == PICT_a8b8g8r8
		    && dst->format == PICT_x8b8g8r8)
			return 1;
	} else if (op == PictOpOver) {
		if (src->alphaMap || dst->alphaMap)
			return 0;

		if (src->format != dst->format)
			return 0;

		if (src->format == PICT_x8r8g8b8
		    || src->format == PICT_x8b8g8r8)
			return 1;
	}

	return 0;
650 651
}

652 653 654
static char
glamor_get_picture_location(PicturePtr picture)
{
Zhigang Gong's avatar
Zhigang Gong committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
	if (picture == NULL)
		return ' ';

	if (picture->pDrawable == NULL) {
		switch (picture->pSourcePict->type) {
		case SourcePictTypeSolidFill:
			return 'c';
		case SourcePictTypeLinear:
			return 'l';
		case SourcePictTypeRadial:
			return 'r';
		default:
			return '?';
		}
	}
	return glamor_get_drawable_location(picture->pDrawable);
671 672
}

673
static Bool
674 675 676 677 678 679
glamor_composite_with_copy(CARD8 op,
			   PicturePtr source,
			   PicturePtr dest,
			   INT16 x_source,
			   INT16 y_source,
			   INT16 x_dest,
680 681
			   INT16 y_dest,
			   RegionPtr region)
682
{
683
	int ret = FALSE;
Zhigang Gong's avatar
Zhigang Gong committed
684 685 686 687 688 689
	if (!source->pDrawable)
		return FALSE;

	if (!compatible_formats(op, dest, source))
		return FALSE;

690
	if (source->repeat || source->transform) {
Zhigang Gong's avatar
Zhigang Gong committed
691
		return FALSE;
692
	}
Zhigang Gong's avatar
Zhigang Gong committed
693 694 695 696 697

	x_dest += dest->pDrawable->x;
	y_dest += dest->pDrawable->y;
	x_source += source->pDrawable->x;
	y_source += source->pDrawable->y;
698 699 700 701
	if (PICT_FORMAT_A(source->format) == 0) {
		/* Fallback if we sample outside the source so that we
		 * swizzle the correct clear color for out-of-bounds texels.
		 */
702
		if (region->extents.x1 + x_source - x_dest < 0)
703
			goto cleanup_region;
704
		if (region->extents.x2 + x_source - x_dest > source->pDrawable->width)
705 706
			goto cleanup_region;

707
		if (region->extents.y1 + y_source - y_dest < 0)
708
			goto cleanup_region;
709
		if (region->extents.y2 + y_source - y_dest > source->pDrawable->height)
710 711
			goto cleanup_region;
	}
712 713
	ret = glamor_copy_n_to_n_nf(source->pDrawable,
				    dest->pDrawable, NULL,
714
				    RegionRects(region), RegionNumRects(region),
715 716 717
				    x_source - x_dest, y_source - y_dest,
				    FALSE, FALSE, 0, NULL);
cleanup_region:
718
	return ret;
719 720
}

721
void
722
glamor_setup_composite_vbo(ScreenPtr screen, int n_verts)
723
{
Zhigang Gong's avatar
Zhigang Gong committed
724 725
	glamor_screen_private *glamor_priv =
	    glamor_get_screen_private(screen);
Chris Wilson's avatar
Chris Wilson committed
726
	glamor_gl_dispatch *dispatch;
Zhigang Gong's avatar
Zhigang Gong committed
727

728 729 730 731
	glamor_priv->vbo_offset = 0;
	glamor_priv->render_nr_verts = 0;
	glamor_priv->vbo_size = n_verts * sizeof(float) * 2;

Zhigang Gong's avatar
Zhigang Gong committed
732 733 734 735 736 737
	glamor_priv->vb_stride = 2 * sizeof(float);
	if (glamor_priv->has_source_coords)
		glamor_priv->vb_stride += 2 * sizeof(float);
	if (glamor_priv->has_mask_coords)
		glamor_priv->vb_stride += 2 * sizeof(float);

Chris Wilson's avatar
Chris Wilson committed
738
	dispatch = glamor_get_dispatch(glamor_priv);
Zhigang Gong's avatar
Zhigang Gong committed
739
	dispatch->glBindBuffer(GL_ARRAY_BUFFER, glamor_priv->vbo);
740 741 742 743
	if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
		dispatch->glBufferData(GL_ARRAY_BUFFER,
				       n_verts * sizeof(float) * 2,
				       NULL, GL_DYNAMIC_DRAW);
744
		glamor_priv->vb = dispatch->glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
745
	}
746
	dispatch->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, glamor_priv->ebo);
Zhigang Gong's avatar
Zhigang Gong committed
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777

	dispatch->glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT,
					GL_FALSE, glamor_priv->vb_stride,
					(void *) ((long)
						  glamor_priv->vbo_offset));
	dispatch->glEnableVertexAttribArray(GLAMOR_VERTEX_POS);

	if (glamor_priv->has_source_coords) {
		dispatch->glVertexAttribPointer(GLAMOR_VERTEX_SOURCE, 2,
						GL_FLOAT, GL_FALSE,
						glamor_priv->vb_stride,
						(void *) ((long)
							  glamor_priv->vbo_offset
							  +
							  2 *
							  sizeof(float)));
		dispatch->glEnableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
	}

	if (glamor_priv->has_mask_coords) {
		dispatch->glVertexAttribPointer(GLAMOR_VERTEX_MASK, 2,
						GL_FLOAT, GL_FALSE,
						glamor_priv->vb_stride,
						(void *) ((long)
							  glamor_priv->vbo_offset
							  +
							  (glamor_priv->has_source_coords
							   ? 4 : 2) *
							  sizeof(float)));
		dispatch->glEnableVertexAttribArray(GLAMOR_VERTEX_MASK);
	}
Chris Wilson's avatar
Chris Wilson committed
778
	glamor_put_dispatch(glamor_priv);
779 780
}

781
void
782 783 784
glamor_emit_composite_vert(ScreenPtr screen,
			   const float *src_coords,
			   const float *mask_coords,
Zhigang Gong's avatar
Zhigang Gong committed
785
			   const float *dst_coords, int i)
786
{
Zhigang Gong's avatar
Zhigang Gong committed
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
	glamor_screen_private *glamor_priv =
	    glamor_get_screen_private(screen);
	float *vb = (float *) (glamor_priv->vb + glamor_priv->vbo_offset);
	int j = 0;

	vb[j++] = dst_coords[i * 2 + 0];
	vb[j++] = dst_coords[i * 2 + 1];
	if (glamor_priv->has_source_coords) {
		vb[j++] = src_coords[i * 2 + 0];
		vb[j++] = src_coords[i * 2 + 1];
	}
	if (glamor_priv->has_mask_coords) {
		vb[j++] = mask_coords[i * 2 + 0];
		vb[j++] = mask_coords[i * 2 + 1];
	}

	glamor_priv->render_nr_verts++;
	glamor_priv->vbo_offset += glamor_priv->vb_stride;
805 806
}

807 808


809 810 811
static void
glamor_flush_composite_rects(ScreenPtr screen)
{
Zhigang Gong's avatar
Zhigang Gong committed
812 813
	glamor_screen_private *glamor_priv =
	    glamor_get_screen_private(screen);
Chris Wilson's avatar
Chris Wilson committed
814 815
	glamor_gl_dispatch *dispatch;

Zhigang Gong's avatar
Zhigang Gong committed
816 817
	if (!glamor_priv->render_nr_verts)
		return;
Chris Wilson's avatar
Chris Wilson committed
818 819

	dispatch = glamor_get_dispatch(glamor_priv);
820 821 822 823 824 825 826 827 828 829
	if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP)
		dispatch->glUnmapBuffer(GL_ARRAY_BUFFER);
	else {

		dispatch->glBindBuffer(GL_ARRAY_BUFFER, glamor_priv->vbo);
		dispatch->glBufferData(GL_ARRAY_BUFFER,
				       glamor_priv->vbo_offset,
				       glamor_priv->vb, GL_DYNAMIC_DRAW);
	}

830 831
	dispatch->glDrawElements(GL_TRIANGLES, (glamor_priv->render_nr_verts * 3) / 2,
				 GL_UNSIGNED_SHORT, NULL);
Chris Wilson's avatar
Chris Wilson committed
832
	glamor_put_dispatch(glamor_priv);
833 834
}

Zhigang Gong's avatar
Zhigang Gong committed
835 836 837 838
int pict_format_combine_tab[][3] = {
	{PICT_TYPE_ARGB, PICT_TYPE_A, PICT_TYPE_ARGB},
	{PICT_TYPE_ABGR, PICT_TYPE_A, PICT_TYPE_ABGR},
};
839

Zhigang Gong's avatar
Zhigang Gong committed
840 841 842
static Bool
combine_pict_format(PictFormatShort * des, const PictFormatShort src,
		    const PictFormatShort mask, enum shader_in in_ca)
843
{
Zhigang Gong's avatar
Zhigang Gong committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
	PictFormatShort new_vis;
	int src_type, mask_type, src_bpp, mask_bpp;
	int i;
	if (src == mask) {
		*des = src;
		return TRUE;
	}
	src_bpp = PICT_FORMAT_BPP(src);
	mask_bpp = PICT_FORMAT_BPP(mask);

	assert(src_bpp == mask_bpp);

	new_vis = PICT_FORMAT_VIS(src) | PICT_FORMAT_VIS(mask);

	switch (in_ca) {
	case SHADER_IN_SOURCE_ONLY:
		return TRUE;
	case SHADER_IN_NORMAL:
		src_type = PICT_FORMAT_TYPE(src);
		mask_type = PICT_TYPE_A;
		break;
	case SHADER_IN_CA_SOURCE:
		src_type = PICT_FORMAT_TYPE(src);
		mask_type = PICT_FORMAT_TYPE(mask);
		break;
	case SHADER_IN_CA_ALPHA:
		src_type = PICT_TYPE_A;
		mask_type = PICT_FORMAT_TYPE(mask);
		break;
	default:
		return FALSE;
	}

	if (src_type == mask_type) {
		*des = PICT_VISFORMAT(src_bpp, src_type, new_vis);
		return TRUE;
	}

	for (i = 0;
	     i <
	     sizeof(pict_format_combine_tab) /
	     sizeof(pict_format_combine_tab[0]); i++) {
		if ((src_type == pict_format_combine_tab[i][0]
		     && mask_type == pict_format_combine_tab[i][1])
		    || (src_type == pict_format_combine_tab[i][1]
			&& mask_type == pict_format_combine_tab[i][0])) {
			*des = PICT_VISFORMAT(src_bpp,
					      pict_format_combine_tab[i]
					      [2], new_vis);
			return TRUE;
		}
	}
	return FALSE;
897 898
}

899 900 901 902 903 904
static void
glamor_set_normalize_tcoords_generic(glamor_pixmap_private *priv,
				     int repeat_type,
				     float *matrix,
				     float xscale, float yscale,
				     int x1, int y1, int x2, int y2,
905 906
				     int yInverted, float *texcoords,
				     int stride)
907 908
{
	if (!matrix && repeat_type == RepeatNone)
909
		glamor_set_normalize_tcoords_ext(priv, xscale, yscale,
910 911 912
					     x1, y1,
					     x2, y2,
					     yInverted,
913
					     texcoords, stride);
914
	else if (matrix && repeat_type == RepeatNone)
915
		glamor_set_transformed_normalize_tcoords_ext(priv, matrix, xscale,
916 917 918
							 yscale, x1, y1,
							 x2, y2,
							 yInverted,
919
							 texcoords, stride);
920
	else if (!matrix && repeat_type != RepeatNone)
921
		glamor_set_repeat_normalize_tcoords_ext(priv, repeat_type,
922 923 924 925
						    xscale, yscale,
						    x1, y1,
						    x2, y2,
						    yInverted,
926
						    texcoords, stride);
927
	else if (matrix && repeat_type != RepeatNone)
928
		glamor_set_repeat_transformed_normalize_tcoords_ext(priv, repeat_type,
929 930 931 932
								matrix, xscale, yscale,
								x1, y1,
								x2, y2,
								yInverted,
933
								texcoords, stride);
934 935
}

936 937 938 939 940 941 942 943
Bool glamor_composite_choose_shader(CARD8 op,
                                    PicturePtr source,
                                    PicturePtr mask,
                                    PicturePtr dest,
			     	    glamor_pixmap_private *source_pixmap_priv,
			     	    glamor_pixmap_private *mask_pixmap_priv,
			     	    glamor_pixmap_private *dest_pixmap_priv,
                                    struct shader_key *s_key,
944 945
				    glamor_composite_shader **shader,
				    struct blendinfo *op_info,
946
                                    PictFormatShort *psaved_source_format)
947
{
Zhigang Gong's avatar
Zhigang Gong committed
948
	ScreenPtr screen = dest->pDrawable->pScreen;
949
	PixmapPtr dest_pixmap = dest_pixmap_priv->base.pixmap;
950 951
	PixmapPtr source_pixmap = NULL;
	PixmapPtr mask_pixmap = NULL;
Zhigang Gong's avatar
Zhigang Gong committed
952 953 954
	enum glamor_pixmap_status source_status = GLAMOR_NONE;
	enum glamor_pixmap_status mask_status = GLAMOR_NONE;
	PictFormatShort saved_source_format = 0;
955 956 957
	struct shader_key key;
	GLfloat source_solid_color[4];
	GLfloat mask_solid_color[4];
Zhigang Gong's avatar
Zhigang Gong committed
958
	Bool ret = FALSE;
Zhigang Gong's avatar
Zhigang Gong committed
959

960
	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dest_pixmap_priv)) {
Zhigang Gong's avatar
Zhigang Gong committed
961 962 963
		glamor_fallback("dest has no fbo.\n");
		goto fail;
	}
964

Zhigang Gong's avatar
Zhigang Gong committed
965
	memset(&key, 0, sizeof(key));
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
	if (!source) {
		key.source = SHADER_SOURCE_SOLID;
		source_solid_color[0] = 0.0;
		source_solid_color[1] = 0.0;
		source_solid_color[2] = 0.0;
		source_solid_color[3] = 0.0;
	} else if (!source->pDrawable) {
			if (source->pSourcePict->type == SourcePictTypeSolidFill) {
				key.source = SHADER_SOURCE_SOLID;
				glamor_get_rgba_from_pixel(source->
							   pSourcePict->solidFill.
							   color,
							   &source_solid_color[0],
							   &source_solid_color[1],
							   &source_solid_color[2],
							   &source_solid_color[3],
							   PICT_a8r8g8b8);
983 984
		} else
			goto fail;
Zhigang Gong's avatar
Zhigang Gong committed
985 986 987
	} else {
		key.source = SHADER_SOURCE_TEXTURE_ALPHA;
	}
988

Zhigang Gong's avatar
Zhigang Gong committed
989 990 991
	if (mask) {
		if (!mask->pDrawable) {
			if (mask->pSourcePict->type ==
992
			     SourcePictTypeSolidFill) {
Zhigang Gong's avatar
Zhigang Gong committed
993 994 995 996 997 998 999
				key.mask = SHADER_MASK_SOLID;
				glamor_get_rgba_from_pixel
				    (mask->pSourcePict->solidFill.color,
				     &mask_solid_color[0],
				     &mask_solid_color[1],
				     &mask_solid_color[2],
				     &mask_solid_color[3], PICT_a8r8g8b8);
1000 1001
			} else
				goto fail;
Zhigang Gong's avatar
Zhigang Gong committed
1002 1003 1004 1005 1006 1007 1008
		} else {
			key.mask = SHADER_MASK_TEXTURE_ALPHA;
		}

		if (!mask->componentAlpha) {
			key.in = SHADER_IN_NORMAL;
		} else {
1009 1010 1011
			if (op == PictOpClear)
				key.mask = SHADER_MASK_NONE;
			else if (op == PictOpSrc || op == PictOpAdd
1012 1013
			         || op == PictOpIn || op == PictOpOut
			         || op == PictOpOverReverse)
Zhigang Gong's avatar
Zhigang Gong committed
1014
				key.in = SHADER_IN_CA_SOURCE;
1015
			else if (op == PictOpOutReverse || op == PictOpInReverse) {
Zhigang Gong's avatar
Zhigang Gong committed
1016 1017
				key.in = SHADER_IN_CA_ALPHA;
			} else {
1018
				glamor_fallback("Unsupported component alpha op: %d\n", op);
Zhigang Gong's avatar
Zhigang Gong committed
1019 1020 1021 1022 1023 1024 1025 1026
				goto fail;
			}
		}
	} else {
		key.mask = SHADER_MASK_NONE;
		key.in = SHADER_IN_SOURCE_ONLY;
	}

1027
	if (source && source->alphaMap) {
Zhigang Gong's avatar
Zhigang Gong committed
1028 1029 1030 1031 1032 1033 1034
		glamor_fallback("source alphaMap\n");
		goto fail;
	}
	if (mask && mask->alphaMap) {
		glamor_fallback("mask alphaMap\n");
		goto fail;
	}
1035

Zhigang Gong's avatar
Zhigang Gong committed
1036 1037
	if (key.source == SHADER_SOURCE_TEXTURE ||
	    key.source == SHADER_SOURCE_TEXTURE_ALPHA) {
1038
		source_pixmap = source_pixmap_priv->base.pixmap;
Zhigang Gong's avatar
Zhigang Gong committed
1039
		if (source_pixmap == dest_pixmap) {
1040 1041
			/* XXX source and the dest share the same texture.
			 * Does it need special handle? */
Zhigang Gong's avatar
Zhigang Gong committed
1042 1043
			glamor_fallback("source == dest\n");
		}
1044 1045
		if (source_pixmap_priv->base.gl_fbo == 0) {
			/* XXX in Xephyr, we may have gl_fbo equal to 1 but gl_tex
Zhigang Gong's avatar
Zhigang Gong committed
1046
			 * equal to zero when the pixmap is screen pixmap. Then we may
1047
			 * refer the tex zero directly latter in the composition.
Zhigang Gong's avatar
Zhigang Gong committed
1048
			 * It seems that it works fine, but it may have potential problem*/
1049
#ifdef GLAMOR_PIXMAP_DYNAMIC_UPLOAD
Zhigang Gong's avatar
Zhigang Gong committed
1050
			source_status = GLAMOR_UPLOAD_PENDING;
1051
#else
Zhigang Gong's avatar
Zhigang Gong committed
1052 1053
			glamor_fallback("no texture in source\n");
			goto fail;
1054
#endif
Zhigang Gong's avatar
Zhigang Gong committed
1055 1056
		}
	}
1057

Zhigang Gong's avatar
Zhigang Gong committed
1058 1059
	if (key.mask == SHADER_MASK_TEXTURE ||
	    key.mask == SHADER_MASK_TEXTURE_ALPHA) {
1060
		mask_pixmap = mask_pixmap_priv->base.pixmap;
Zhigang Gong's avatar
Zhigang Gong committed
1061 1062 1063 1064
		if (mask_pixmap == dest_pixmap) {
			glamor_fallback("mask == dest\n");
			goto fail;
		}
1065
		if (mask_pixmap_priv->base.gl_fbo == 0) {
1066
#ifdef GLAMOR_PIXMAP_DYNAMIC_UPLOAD
Zhigang Gong's avatar
Zhigang Gong committed
1067
			mask_status = GLAMOR_UPLOAD_PENDING;
1068
#else
Zhigang Gong's avatar
Zhigang Gong committed
1069 1070
			glamor_fallback("no texture in mask\n");
			goto fail;
1071
#endif
Zhigang Gong's avatar
Zhigang Gong committed
1072 1073
		}
	}
1074

1075
#ifdef GLAMOR_PIXMAP_DYNAMIC_UPLOAD
Zhigang Gong's avatar
Zhigang Gong committed
1076
	if (source_status == GLAMOR_UPLOAD_PENDING
1077 1078
	     && mask_status == GLAMOR_UPLOAD_PENDING
	     && source_pixmap == mask_pixmap) {
Zhigang Gong's avatar
Zhigang Gong committed
1079 1080 1081 1082

		if (source->format != mask->format) {
			saved_source_format = source->format;

1083 1084 1085 1086
			if (!combine_pict_format(&source->format, source->format,
			                         mask->format, key.in)) {
				glamor_fallback("combine source %x mask %x failed.\n",
				                source->format, mask->format);
Zhigang Gong's avatar
Zhigang Gong committed
1087 1088 1089 1090
				goto fail;
			}

			if (source->format != saved_source_format) {
1091 1092
				glamor_picture_format_fixup(source,
				        source_pixmap_priv);
Zhigang Gong's avatar
Zhigang Gong committed
1093
			}
1094
			/* XXX
Zhigang Gong's avatar
Zhigang Gong committed
1095
			 * By default, glamor_upload_picture_to_texture will wire alpha to 1
1096
			 * if one picture doesn't have alpha. So we don't do that again in
Zhigang Gong's avatar
Zhigang Gong committed
1097
			 * rendering function. But here is a special case, as source and
1098
			 * mask share the same texture but may have different formats. For
Zhigang Gong's avatar
Zhigang Gong committed
1099 1100 1101 1102 1103
			 * example, source doesn't have alpha, but mask has alpha. Then the
			 * texture will have the alpha value for the mask. And will not wire
			 * to 1 for the source. In this case, we have to use different shader
			 * to wire the source's alpha to 1.
			 *
1104
			 * But this may cause a potential problem if the source's repeat mode
Zhigang Gong's avatar
Zhigang Gong committed
1105 1106 1107 1108 1109 1110
			 * is REPEAT_NONE, and if the source is smaller than the dest, then
			 * for the region not covered by the source may be painted incorrectly.
			 * because we wire the alpha to 1.
			 *
			 **/
			if (!PICT_FORMAT_A(saved_source_format)
1111
			     && PICT_FORMAT_A(mask->format))
Zhigang Gong's avatar
Zhigang Gong committed
1112 1113 1114
				key.source = SHADER_SOURCE_TEXTURE;

			if (!PICT_FORMAT_A(mask->format)
1115
			     && PICT_FORMAT_A(saved_source_format))
Zhigang Gong's avatar
Zhigang Gong committed
1116 1117 1118 1119 1120
				key.mask = SHADER_MASK_TEXTURE;

			mask_status = GLAMOR_NONE;
		}

1121
		source_status = glamor_upload_picture_to_texture(source);
Zhigang Gong's avatar
Zhigang Gong committed
1122
		if (source_status != GLAMOR_UPLOAD_DONE) {
1123
			glamor_fallback("Failed to upload source texture.\n");
Zhigang Gong's avatar
Zhigang Gong committed
1124 1125 1126 1127
			goto fail;
		}
	} else {
		if (source_status == GLAMOR_UPLOAD_PENDING) {
1128
			source_status = glamor_upload_picture_to_texture(source);
Zhigang Gong's avatar
Zhigang Gong committed
1129
			if (source_status != GLAMOR_UPLOAD_DONE) {
1130
				glamor_fallback("Failed to upload source texture.\n");
Zhigang Gong's avatar
Zhigang Gong committed
1131 1132 1133 1134 1135
				goto fail;
			}
		}

		if (mask_status == GLAMOR_UPLOAD_PENDING) {
1136
			mask_status = glamor_upload_picture_to_texture(mask);
Zhigang Gong's avatar
Zhigang Gong committed
1137
			if (mask_status != GLAMOR_UPLOAD_DONE) {
1138
				glamor_fallback("Failed to upload mask texture.\n");
Zhigang Gong's avatar
Zhigang Gong committed
1139 1140 1141 1142
				goto fail;
			}
		}
	}
1143
#endif
1144 1145 1146 1147 1148

	/*Before enter the rendering stage, we need to fixup
	 * transformed source and mask, if the transform is not int translate. */
	if (key.source != SHADER_SOURCE_SOLID
	    && source->transform
1149 1150
	    && !pixman_transform_is_int_translate(source->transform)
	    && source_pixmap_priv->type != GLAMOR_TEXTURE_LARGE) {
1151 1152 1153
		if (!glamor_fixup_pixmap_priv(screen, source_pixmap_priv))
			goto fail;
	}
1154
	if (key.mask != SHADER_MASK_NONE && key.mask != SHADER_MASK_SOLID
1155
	    && mask->transform
1156 1157
	    && !pixman_transform_is_int_translate(mask->transform)
	    && mask_pixmap_priv->type != GLAMOR_TEXTURE_LARGE) {
1158 1159 1160 1161
		if (!glamor_fixup_pixmap_priv(screen, mask_pixmap_priv))
			goto fail;
	}

1162

1163
	if (!glamor_set_composite_op(screen, op, op_info, dest, mask))
Zhigang Gong's avatar
Zhigang Gong committed
1164 1165
		goto fail;

1166 1167
	*shader = glamor_lookup_composite_shader(screen, &key);
	if ((*shader)->prog == 0) {
1168 1169
		glamor_fallback("no shader program for this"
		                "render acccel mode\n");
Zhigang Gong's avatar
Zhigang Gong committed
1170 1171 1172
		goto fail;
	}

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
	if (key.source == SHADER_SOURCE_SOLID)
		memcpy(&(*shader)->source_solid_color[0],
			source_solid_color, 4*sizeof(float));
	else {
		(*shader)->source_priv = source_pixmap_priv;
		(*shader)->source = source;
	}

	if (key.mask == SHADER_MASK_SOLID)
		memcpy(&(*shader)->mask_solid_color[0],
			mask_solid_color, 4*sizeof(float));
	else {
		(*shader)->mask_priv = mask_pixmap_priv;
		(*shader)->mask = mask;
	}

	ret = TRUE;
	memcpy(s_key, &key, sizeof(key));
	*psaved_source_format = saved_source_format;
	goto done;

fail:
	if (saved_source_format)
		source->format = saved_source_format;
done:
	return ret;
}

void
glamor_composite_set_shader_blend(glamor_pixmap_private *dest_priv,
				  struct shader_key *key,
				  glamor_composite_shader *shader,
				  struct blendinfo *op_info)
{
	glamor_gl_dispatch *dispatch;
	glamor_screen_private *glamor_priv;

	glamor_priv = dest_priv->base.glamor_priv;

Chris Wilson's avatar
Chris Wilson committed
1212
	dispatch = glamor_get_dispatch(glamor_priv);
Zhigang Gong's avatar
Zhigang Gong committed
1213 1214
	dispatch->glUseProgram(shader->prog);

1215 1216 1217
	if (key->source == SHADER_SOURCE_SOLID) {
		glamor_set_composite_solid(dispatch,
			shader->source_solid_color,
1218
		        shader->source_uniform_location);
Zhigang Gong's avatar
Zhigang Gong committed
1219
	} else {
1220 1221 1222
		glamor_set_composite_texture(glamor_priv, 0,
			shader->source,
		        shader->source_priv, shader->source_wh,
1223
		        shader->source_repeat_mode);
Zhigang Gong's avatar
Zhigang Gong committed
1224
	}
1225

1226 1227
	if (key->mask != SHADER_MASK_NONE) {
		if (key->mask == SHADER_MASK_SOLID) {
Zhigang Gong's avatar
Zhigang Gong committed
1228
			glamor_set_composite_solid(dispatch,
1229
			        shader->mask_solid_color,
1230
			        shader->mask_uniform_location);
Zhigang Gong's avatar
Zhigang Gong committed
1231
		} else {
1232 1233 1234
			glamor_set_composite_texture(glamor_priv, 1,
				shader->mask,
			        shader->mask_priv, shader->mask_wh,
1235
			        shader->mask_repeat_mode);
Zhigang Gong's avatar
Zhigang Gong committed
1236 1237 1238
		}
	}

1239 1240 1241 1242 1243 1244 1245 1246
	if (op_info->source_blend == GL_ONE
	    && op_info->dest_blend == GL_ZERO) {
		dispatch->glDisable(GL_BLEND);
	} else {
		dispatch->glEnable(GL_BLEND);
		dispatch->glBlendFunc(op_info->source_blend,
				      op_info->dest_blend);
	}
1247

1248
	glamor_put_dispatch(glamor_priv);
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
}

static Bool
glamor_composite_with_shader(CARD8 op,
			     PicturePtr source,
			     PicturePtr mask,
			     PicturePtr dest,
			     glamor_pixmap_private *source_pixmap_priv,
			     glamor_pixmap_private *mask_pixmap_priv,
			     glamor_pixmap_private *dest_pixmap_priv,
1259 1260
			     int nrect, glamor_composite_rect_t * rects,
			     Bool two_pass_ca)
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
{
	ScreenPtr screen = dest->pDrawable->pScreen;
	glamor_screen_private *glamor_priv = dest_pixmap_priv->base.glamor_priv;
	PixmapPtr dest_pixmap = dest_pixmap_priv->base.pixmap;
	PixmapPtr source_pixmap = NULL;
	PixmapPtr mask_pixmap = NULL;
	glamor_gl_dispatch *dispatch = NULL;
	GLfloat dst_xscale, dst_yscale;
	GLfloat mask_xscale = 1, mask_yscale = 1,
	        src_xscale = 1, src_yscale = 1;
1271
	struct shader_key key, key_ca;
1272
	float *vertices;
1273 1274 1275 1276 1277 1278 1279 1280 1281
	int dest_x_off, dest_y_off;
	int source_x_off, source_y_off;
	int mask_x_off, mask_y_off;
	PictFormatShort saved_source_format = 0;
	float src_matrix[9], mask_matrix[9];
	float *psrc_matrix = NULL, *pmask_matrix = NULL;
	int vert_stride = 4;
	int nrect_max;
	Bool ret = FALSE;
1282 1283
	glamor_composite_shader *shader = NULL, *shader_ca = NULL;
	struct blendinfo op_info, op_info_ca;
1284 1285 1286 1287

	if(!glamor_composite_choose_shader(op, source, mask, dest,
					   source_pixmap_priv, mask_pixmap_priv,
					   dest_pixmap_priv,
1288 1289
	                                   &key, &shader, &op_info,
					   &saved_source_format)) {
1290 1291 1292
		glamor_fallback("glamor_composite_choose_shader failed\n");
		return ret;
	}
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
	if (two_pass_ca) {
		if(!glamor_composite_choose_shader(PictOpAdd, source, mask, dest,
						   source_pixmap_priv, mask_pixmap_priv,
						   dest_pixmap_priv,
						   &key_ca, &shader_ca, &op_info_ca,
						   &saved_source_format)) {
			glamor_fallback("glamor_composite_choose_shader failed\n");
			return ret;
		}
	}

	glamor_set_destination_pixmap_priv_nc(dest_pixmap_priv);
	glamor_composite_set_shader_blend(dest_pixmap_priv, &key, shader, &op_info);
1306 1307 1308

	dispatch = glamor_get_dispatch(glamor_priv);