summaryrefslogtreecommitdiff
path: root/t_srccoords.c
blob: c29a96059d97e7a9e5263a487cb8a1d2b2fa5a44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright © 2005 Eric Anholt
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Eric Anholt not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Eric Anholt makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * ERIC ANHOLT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL ERIC ANHOLT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>

#include "rendercheck.h"

/* Originally a bullseye, this pattern has been modified so that flipping
 * will result in errors as well.
 */
static int target_colors[5][5] = {
	{1, 1, 1, 1, 1},
	{1, 0, 0, 0, 1},
	{1, 1, 1, 0, 1},
	{1, 0, 0, 1, 1},
	{1, 1, 1, 1, 1},
};

static picture_info *create_target_picture(Display *dpy)
{
	picture_info *p;
	int i;

	p = malloc(sizeof(picture_info));

	p->d = XCreatePixmap(dpy, RootWindow(dpy, 0), 5, 5, 32);
	p->format = XRenderFindStandardFormat(dpy, PictStandardARGB32);
	p->pict = XRenderCreatePicture(dpy, p->d, p->format, 0, NULL);
	p->name = "target picture";

	for (i = 0; i < 25; i++) {
		int x = i % 5;
		int y = i / 5;
		color4d *c = &colors[target_colors[x][y]];

		argb_fill(dpy, p, x, y, 1, 1, c->a, c->r, c->g, c->b);
	}

	return p;	
}

static void destroy_target_picture(Display *dpy, picture_info *p)
{
	XRenderFreePicture(dpy, p->pict);
	XFreePixmap(dpy, p->d);
	free(p);
}

/* Test source or mask coordinates by drawing from a 5x5 picture into the 0,0
 * pixel.
 * XXX: Something besides PictOpSrc should also be used, at least in the
 * !test_mask case, to avoid getting CopyArea acceleration (easy to implement)
 * rather than a more general Composite implementation.
 */
Bool
srccoords_test(Display *dpy, picture_info *win, picture_info *white,
    Bool test_mask)
{
	color4d expected, tested;
	int i;
	XRenderPictureAttributes pa;
	Bool failed = FALSE;
	int tested_colors[5][5];
	picture_info *src;

	src = create_target_picture(dpy);
	if (src == NULL) {
		fprintf(stderr, "couldn't allocate picture for test\n");
		return FALSE;
	}

	for (i = 0; i < 25; i++) {
		char name[20];
		int x = i % 5, y = i / 5;

		if (!test_mask)
			XRenderComposite(dpy, PictOpSrc, src->pict, 0,
			    win->pict, x, y, 0, 0, 0, 0, 1, 1);
		else {
			/* Using PictOpSrc, color 0 (white), and component
			 * alpha, the mask color should be written to the
			 * destination.
			 */
			pa.component_alpha = TRUE;
			XRenderChangePicture(dpy, src->pict, CPComponentAlpha,
			    &pa);
			XRenderComposite(dpy, PictOpSrc, white->pict, src->pict,
			    win->pict, 0, 0, x, y, 0, 0, 1, 1);
			pa.component_alpha = FALSE;
			XRenderChangePicture(dpy, src->pict, CPComponentAlpha,
			    &pa);
		}
		get_pixel(dpy, win, 0, 0, &tested);

		expected = colors[target_colors[x][y]];
		color_correct(win, &expected);
		if (tested.r == 1.0) {
			if (tested.g == 1.0 && tested.b == 1.0)
				tested_colors[x][y] = 0;
			else if (tested.g == 0.0 && tested.b == 0.0)
				tested_colors[x][y] = 1;
			else tested_colors[x][y] = 9;
		} else
			tested_colors[x][y] = 9;

		if (test_mask)
			snprintf(name, 20, "mask coords");
		else
			snprintf(name, 20, "src coords");
		if (!eval_diff(name, &expected, &tested, x, y,
		    is_verbose))
			failed = TRUE;
	}
	if (failed) {
		int j;

		printf("expected vs tested:\n");
		for (i = 0; i < 5; i++) {
			for (j = 0; j < 5; j++)
				printf("%d", target_colors[j][i]);
			printf(" ");
			for (j = 0; j < 5; j++)
				printf("%d", tested_colors[j][i]);
			printf("\n");
		}
	}

	destroy_target_picture(dpy, src);

	return !failed;
}