summaryrefslogtreecommitdiff
path: root/test/render-glyphs.c
blob: 8822e36a3aa7ee20e5eb4104476b03aa2f168e9e (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>

#include <X11/Xutil.h> /* for XDestroyImage */
#include <pixman.h> /* for pixman blt functions */

#include "test.h"

static const XRenderColor colors[] = {
	/* red, green, blue, alpha */
	{ 0 },
	{ 0, 0, 0, 0xffff },
	{ 0xffff, 0, 0, 0xffff },
	{ 0, 0xffff, 0, 0xffff },
	{ 0, 0, 0xffff, 0xffff },
	{ 0xffff, 0xffff, 0xffff, 0xffff },
};

static struct clip {
	void *func;
} clips[] = {
	{ NULL },
};

static int _x_error_occurred;

static int
_check_error_handler(Display     *display,
		     XErrorEvent *event)
{
	_x_error_occurred = 1;
	return False; /* ignored */
}

static void clear(struct test_display *dpy,
		  struct test_target *tt,
		  const XRenderColor *c)
{
	XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, c,
			     0, 0, tt->width, tt->height);
}

static bool check_op(struct test_display *dpy, int op, struct test_target *tt)
{
	XRenderColor render_color = {0};

	XSync(dpy->dpy, True);
	_x_error_occurred = 0;

	XRenderFillRectangle(dpy->dpy, op,
			     tt->picture, &render_color,
			     0, 0, 0, 0);

	XSync(dpy->dpy, True);
	return _x_error_occurred == 0;
}

struct glyph_iter {
	enum {
		GLYPHS, OP, DST, SRC, MASK, CLIP,
	} stage;

	int glyph_format;
	int op;
	int dst_color;
	int src_color;
	int mask_format;
	int clip;

	struct {
		struct test_display *dpy;
		struct test_target tt;
		GlyphSet glyphset;
		Picture src;
		XRenderPictFormat *mask_format;
	} ref, out;
};

static void glyph_iter_init(struct glyph_iter *gi,
			    struct test *t, enum target target)
{
	memset(gi, 0, sizeof(*gi));

	gi->out.dpy = &t->out;
	test_target_create_render(&t->out, target, &gi->out.tt);

	gi->ref.dpy = &t->ref;
	test_target_create_render(&t->ref, target, &gi->ref.tt);

	gi->stage = GLYPHS;
	gi->glyph_format = -1;
	gi->op = -1;
	gi->dst_color = -1;
	gi->src_color = -1;
	gi->mask_format = -1;
	gi->clip = -1;
}

static void render_clear(char *image, int image_size, int bpp)
{
	memset(image, 0, image_size);
}

static void render_black(char *image, int image_size, int bpp)
{
	if (bpp == 4) {
		uint32_t *p = (uint32_t *)image;
		image_size /= 4;
		while (image_size--)
			*p++ = 0x000000ff;
	} else
		memset(image, 0x55, image_size);
}

static void render_green(char *image, int image_size, int bpp)
{
	if (bpp == 4) {
		uint32_t *p = (uint32_t *)image;
		image_size /= 4;
		while (image_size--)
			*p++ = 0xffff0000;
	} else
		memset(image, 0xaa, image_size);
}

static void render_white(char *image, int image_size, int bpp)
{
	memset(image, 0xff, image_size);
}

static GlyphSet create_glyphs(Display *dpy, int format_id)
{
#define N_GLYPHS 4
	XRenderPictFormat *format;
	XGlyphInfo glyph = { 8, 8, 0, 0, 8, 0 };
	char image[4*8*8];
	GlyphSet glyphset;
	Glyph gid;
	int image_size;
	int bpp;
	int n;

	format = XRenderFindStandardFormat(dpy, format_id);
	if (format == NULL)
		return 0;

	switch (format_id) {
	case PictStandardARGB32:
	case PictStandardRGB24:
		image_size = 4 * 8 * 8;
		bpp = 4;
		break;
	case PictStandardA8:
	case PictStandardA4:
		image_size = 8 * 8;
		bpp = 1;
		break;
	case PictStandardA1:
		image_size = 8;
		bpp = 0;
		break;
	default:
		return 0;
	}

	glyphset = XRenderCreateGlyphSet(dpy, format);
	for (n = 0; n < N_GLYPHS; n++) {
		gid = n;

		switch (n) {
		case 0: render_clear(image, image_size, bpp); break;
		case 1: render_black(image, image_size, bpp); break;
		case 2: render_green(image, image_size, bpp); break;
		case 3: render_white(image, image_size, bpp); break;
		}

		XRenderAddGlyphs(dpy, glyphset,
				 &gid, &glyph, 1, image, image_size);
	}

	return glyphset;
}

static const char *glyph_name(int n)
{
	switch (n) {
	case 0: return "clear";
	case 1: return "black";
	case 2: return "green";
	case 3: return "white";
	default: return "unknown";
	}
}

static bool glyph_iter_next(struct glyph_iter *gi)
{
restart:
	if (gi->stage == GLYPHS) {
		if (++gi->glyph_format == PictStandardNUM)
			return false;

		if (gi->out.glyphset)
			XRenderFreeGlyphSet(gi->out.dpy->dpy,
					    gi->out.glyphset);
		gi->out.glyphset = create_glyphs(gi->out.dpy->dpy,
					       gi->glyph_format);

		if (gi->ref.glyphset)
			XRenderFreeGlyphSet(gi->ref.dpy->dpy,
					    gi->ref.glyphset);
		gi->ref.glyphset = create_glyphs(gi->ref.dpy->dpy,
					       gi->glyph_format);

		gi->stage++;
	}

	if (gi->stage == OP) {
		do {
			if (++gi->op == 255)
				goto reset_op;
		} while (!check_op(gi->out.dpy, gi->op, &gi->out.tt) ||
			 !check_op(gi->ref.dpy, gi->op, &gi->ref.tt));

		gi->stage++;
	}

	if (gi->stage == DST) {
		if (++gi->dst_color == ARRAY_SIZE(colors))
			goto reset_dst;

		gi->stage++;
	}

	if (gi->stage == SRC) {
		if (++gi->src_color == ARRAY_SIZE(colors))
			goto reset_src;

		if (gi->ref.src)
			XRenderFreePicture(gi->ref.dpy->dpy, gi->ref.src);
		gi->ref.src = XRenderCreateSolidFill(gi->ref.dpy->dpy,
						     &colors[gi->src_color]);

		if (gi->out.src)
			XRenderFreePicture(gi->out.dpy->dpy, gi->out.src);
		gi->out.src = XRenderCreateSolidFill(gi->out.dpy->dpy,
						     &colors[gi->src_color]);

		gi->stage++;
	}

	if (gi->stage == MASK) {
		if (++gi->mask_format > PictStandardNUM)
			goto reset_mask;

		if (gi->mask_format == PictStandardRGB24)
			gi->mask_format++;

		if (gi->mask_format < PictStandardNUM) {
			gi->out.mask_format = XRenderFindStandardFormat(gi->out.dpy->dpy,
									gi->mask_format);
			gi->ref.mask_format = XRenderFindStandardFormat(gi->ref.dpy->dpy,
									gi->mask_format);
		} else {
			gi->out.mask_format = NULL;
			gi->ref.mask_format = NULL;
		}

		gi->stage++;
	}

	if (gi->stage == CLIP) {
		if (++gi->clip == ARRAY_SIZE(clips))
			goto reset_clip;

		gi->stage++;
	}

	gi->stage--;
	return true;

reset_op:
	gi->op = -1;
reset_dst:
	gi->dst_color = -1;
reset_src:
	gi->src_color = -1;
reset_mask:
	gi->mask_format = -1;
reset_clip:
	gi->clip = -1;
	gi->stage--;
	goto restart;
}

static void glyph_iter_fini(struct glyph_iter *gi)
{
	if (gi->out.glyphset)
		XRenderFreeGlyphSet (gi->out.dpy->dpy, gi->out.glyphset);
	if (gi->ref.glyphset)
		XRenderFreeGlyphSet (gi->ref.dpy->dpy, gi->ref.glyphset);

	test_target_destroy_render(gi->out.dpy, &gi->out.tt);
	test_target_destroy_render(gi->ref.dpy, &gi->ref.tt);
}

static const char *stdformat_to_str(int id)
{
	switch (id) {
	case PictStandardARGB32: return "ARGB32";
	case PictStandardRGB24: return "RGB24";
	case PictStandardA8: return "A8";
	case PictStandardA4: return "A4";
	case PictStandardA1: return "A1";
	default: return "none";
	}
}

static char *glyph_iter_to_string(struct glyph_iter *gi,
				  const char *format,
				  ...)
{
	static char buf[100];
	va_list ap;
	int len;

	len = sprintf(buf, "glyphs=%s, op=%d, dst=%08x, src=%08x, mask=%s",
		      stdformat_to_str(gi->glyph_format), gi->op,
		      xrender_color(&colors[gi->dst_color]),
		      xrender_color(&colors[gi->src_color]),
		      stdformat_to_str(gi->mask_format));

	if (format) {
		buf[len++] = ' ';
		va_start(ap, format);
		vsprintf(buf+len, format, ap);
		va_end(ap);
	}

	return buf;
}

static void single(struct test *t, enum target target)
{
	struct glyph_iter gi;
	int n;

	printf("Testing single glyph (%s): ", test_target_name(target));
	fflush(stdout);

	glyph_iter_init(&gi, t, target);
	while (glyph_iter_next(&gi)) {
		XGlyphElt8 elt;
		char id[N_GLYPHS];

		for (n = 0; n < N_GLYPHS; n++) {
			id[n] = n;

			elt.chars = &id[n];
			elt.nchars = 1;
			elt.xOff = 0;
			elt.yOff = 0;

			clear(gi.out.dpy, &gi.out.tt, &colors[gi.dst_color]);
			elt.glyphset = gi.out.glyphset;
			XRenderCompositeText8 (gi.out.dpy->dpy, gi.op,
					       gi.out.src,
					       gi.out.tt.picture,
					       gi.out.mask_format,
					       0, 0,
					       0, 8,
					       &elt, 1);

			clear(gi.ref.dpy, &gi.ref.tt, &colors[gi.dst_color]);
			elt.glyphset = gi.ref.glyphset;
			XRenderCompositeText8 (gi.ref.dpy->dpy, gi.op,
					       gi.ref.src,
					       gi.ref.tt.picture,
					       gi.ref.mask_format,
					       0, 0,
					       0, 8,
					       &elt, 1);
			test_compare(t,
				     gi.out.tt.draw, gi.out.tt.format,
				     gi.ref.tt.draw, gi.ref.tt.format,
				     0, 0, gi.out.tt.width, gi.out.tt.height,
				     glyph_iter_to_string(&gi,
							  "glyph=%s",
							  glyph_name(n)));
		}

		elt.chars = &id[0];
		elt.nchars = n;
		clear(gi.out.dpy, &gi.out.tt, &colors[gi.dst_color]);
		elt.glyphset = gi.out.glyphset;
		XRenderCompositeText8 (gi.out.dpy->dpy, gi.op,
				       gi.out.src,
				       gi.out.tt.picture,
				       gi.out.mask_format,
				       0, 0,
				       0, 8,
				       &elt, 1);

		clear(gi.ref.dpy, &gi.ref.tt, &colors[gi.dst_color]);
		elt.glyphset = gi.ref.glyphset;
		XRenderCompositeText8 (gi.ref.dpy->dpy, gi.op,
				       gi.ref.src,
				       gi.ref.tt.picture,
				       gi.ref.mask_format,
				       0, 0,
				       0, 8,
				       &elt, 1);
		test_compare(t,
			     gi.out.tt.draw, gi.out.tt.format,
			     gi.ref.tt.draw, gi.ref.tt.format,
			     0, 0, gi.out.tt.width, gi.out.tt.height,
			     glyph_iter_to_string(&gi, "all"));
	}
	glyph_iter_fini(&gi);
}

int main(int argc, char **argv)
{
	struct test test;
	int t;

	test_init(&test, argc, argv);
	XSetErrorHandler(_check_error_handler);

	for (t = TARGET_FIRST; t <= TARGET_LAST; t++) {
		single(&test, t);
		//overlapping(&test, t);
		//gap(&test, t);
		//mixed(&test, t);
	}

	return 0;
}