summaryrefslogtreecommitdiff
path: root/boilerplate/cairo-boilerplate-xlib.c
blob: fe54d988565f170b9292980a1e4c603882d33504 (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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/*
 * Copyright © 2004,2006 Red Hat, Inc.
 *
 * 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
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Red Hat, Inc. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. 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.
 *
 * Author: Carl D. Worth <cworth@cworth.org>
 */

#include "cairo-boilerplate-private.h"
#include "cairo-boilerplate-xlib.h"

#include <cairo-xlib.h>
#if CAIRO_HAS_XLIB_XRENDER_SURFACE
#include <cairo-xlib-xrender.h>
#endif
#include <cairo-xlib-surface-private.h>

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

typedef struct _xlib_target_closure {
    Display *dpy;
    Drawable drawable;
    cairo_bool_t drawable_is_pixmap;
} xlib_target_closure_t;

static void
_cairo_boilerplate_xlib_cleanup (void *closure)
{
    xlib_target_closure_t *xtc = closure;

    if (xtc->drawable) {
	if (xtc->drawable_is_pixmap)
	    XFreePixmap (xtc->dpy, xtc->drawable);
	else
	    XDestroyWindow (xtc->dpy, xtc->drawable);
    }
    XCloseDisplay (xtc->dpy);
    free (xtc);
}

static void
_cairo_boilerplate_xlib_synchronize (void *closure)
{
    xlib_target_closure_t *xtc = closure;
    XImage *ximage;

    ximage = XGetImage (xtc->dpy, xtc->drawable,
			0, 0, 1, 1, AllPlanes, ZPixmap);
    if (ximage != NULL)
	XDestroyImage (ximage);
}

static cairo_bool_t
_cairo_boilerplate_xlib_check_screen_size (Display	*dpy,
	                                   int		 screen,
					   int		 width,
					   int		 height)
{
    Screen *scr = XScreenOfDisplay (dpy, screen);
    return width <= WidthOfScreen (scr) && height <= HeightOfScreen (scr);
}

#if CAIRO_HAS_XLIB_XRENDER_SURFACE
/* For the xlib backend we distinguish between TEST and PERF mode in a
 * couple of ways.
 *
 * For TEST, we always test against pixmaps of depth 32 (for
 * COLOR_ALPHA) or 24 (for COLOR) and we use XSynchronize to make it
 * easier to debug problems.
 *
 * For PERF, we test against 32-bit pixmaps for COLOR_ALPHA, but for
 * COLOR we test against _windows_ at the depth of the default visual.
 * For obvious reasons, we don't use XSynchronize.
 */
static cairo_surface_t *
_cairo_boilerplate_xlib_test_create_surface (Display			*dpy,
					     cairo_content_t		 content,
					     int			 width,
					     int			 height,
					     xlib_target_closure_t	*xtc)
{
    XRenderPictFormat *xrender_format;

    /* This kills performance, but it makes debugging much
     * easier. That's why we have it here when in TEST mode, but not
     * over in PERF mode. */
    XSynchronize (xtc->dpy, 1);

    /* XXX: Currently we don't do any xlib testing when the X server
     * doesn't have the Render extension. We could do better here,
     * (perhaps by converting the tests from ARGB32 to RGB24). One
     * step better would be to always test the non-Render fallbacks
     * for each test even if the server does have the Render
     * extension. That would probably be through another
     * cairo_boilerplate_target which would use an extended version of
     * cairo_test_xlib_disable_render.  */
    switch (content) {
    case CAIRO_CONTENT_COLOR_ALPHA:
	xrender_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
	break;
    case CAIRO_CONTENT_COLOR:
	xrender_format = XRenderFindStandardFormat (dpy, PictStandardRGB24);
	break;
    case CAIRO_CONTENT_ALPHA:
    default:
	CAIRO_BOILERPLATE_DEBUG (("Invalid content for xlib test: %d\n", content));
	return NULL;
    }
    if (xrender_format == NULL) {
	CAIRO_BOILERPLATE_DEBUG (("X server does not have the Render extension.\n"));
	return NULL;
    }

    xtc->drawable = XCreatePixmap (dpy, DefaultRootWindow (dpy),
				   width, height, xrender_format->depth);
    xtc->drawable_is_pixmap = TRUE;

    return cairo_xlib_surface_create_with_xrender_format (dpy, xtc->drawable,
							  DefaultScreenOfDisplay (dpy),
							  xrender_format,
							  width, height);
}

static cairo_surface_t *
_cairo_boilerplate_xlib_perf_create_surface (Display			*dpy,
					     cairo_content_t		 content,
					     int			 width,
					     int			 height,
					     xlib_target_closure_t	*xtc)
{
    XSetWindowAttributes attr;
    XRenderPictFormat *xrender_format;
    Visual *visual;

    switch (content) {
    case CAIRO_CONTENT_COLOR_ALPHA:
	xrender_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
	if (xrender_format == NULL) {
	    CAIRO_BOILERPLATE_DEBUG (("X server does not have the Render extension.\n"));
	    return NULL;
	}

	xtc->drawable = XCreatePixmap (dpy, DefaultRootWindow (dpy),
				       width, height, xrender_format->depth);
	xtc->drawable_is_pixmap = TRUE;
	break;

    case CAIRO_CONTENT_COLOR:
	if (! _cairo_boilerplate_xlib_check_screen_size (dpy,
		                                         DefaultScreen (dpy),
		                                         width, height)) {
	    CAIRO_BOILERPLATE_DEBUG (("Surface is larger than the Screen.\n"));
	    return NULL;
	}

	visual = DefaultVisual (dpy, DefaultScreen (dpy));
	xrender_format = XRenderFindVisualFormat (dpy, visual);
	if (xrender_format == NULL) {
	    CAIRO_BOILERPLATE_DEBUG (("X server does not have the Render extension.\n"));
	    return NULL;
	}

	attr.override_redirect = True;
	xtc->drawable = XCreateWindow (dpy, DefaultRootWindow (dpy), 0, 0,
				       width, height, 0, xrender_format->depth,
				       InputOutput, visual, CWOverrideRedirect, &attr);
	XMapWindow (dpy, xtc->drawable);
	xtc->drawable_is_pixmap = FALSE;
	break;

    case CAIRO_CONTENT_ALPHA:
    default:
	CAIRO_BOILERPLATE_DEBUG (("Invalid content for xlib test: %d\n", content));
	return NULL;
    }

    return cairo_xlib_surface_create_with_xrender_format (dpy, xtc->drawable,
							  DefaultScreenOfDisplay (dpy),
							  xrender_format,
							  width, height);
}

static cairo_surface_t *
_cairo_boilerplate_xlib_create_surface (const char			 *name,
					cairo_content_t			  content,
					double				  width,
					double				  height,
					double				  max_width,
					double				  max_height,
					cairo_boilerplate_mode_t	  mode,
					int                               id,
					void				**closure)
{
    xlib_target_closure_t *xtc;
    Display *dpy;
    cairo_surface_t *surface;

    *closure = xtc = xcalloc (1, sizeof (xlib_target_closure_t));

    width = ceil (width);
    if (width < 1)
	width = 1;

    height = ceil (height);
    if (height < 1)
	height = 1;

    xtc->dpy = dpy = XOpenDisplay (NULL);
    if (xtc->dpy == NULL) {
	free (xtc);
	CAIRO_BOILERPLATE_DEBUG (("Failed to open display: %s\n", XDisplayName(0)));
	return NULL;
    }

    if (mode == CAIRO_BOILERPLATE_MODE_TEST)
	surface = _cairo_boilerplate_xlib_test_create_surface (dpy, content, width, height, xtc);
    else /* mode == CAIRO_BOILERPLATE_MODE_PERF */
	surface = _cairo_boilerplate_xlib_perf_create_surface (dpy, content, width, height, xtc);

    if (surface == NULL || cairo_surface_status (surface))
	_cairo_boilerplate_xlib_cleanup (xtc);

    return surface;
}

cairo_status_t
cairo_boilerplate_xlib_surface_disable_render (cairo_surface_t *abstract_surface)
{
    cairo_xlib_surface_t *surface = (cairo_xlib_surface_t*) abstract_surface;

    if (cairo_surface_get_type (abstract_surface) != CAIRO_SURFACE_TYPE_XLIB)
	return CAIRO_STATUS_SURFACE_TYPE_MISMATCH;

    surface->render_major = surface->render_minor = -1;
    surface->xrender_format = NULL;

    /* The content type is forced by _xrender_format_to_content() during
     * non-Render surface creation, so repeat the procedure here. */
    surface->base.content = CAIRO_CONTENT_COLOR;

    /* These flags are set based on known bugs and lack of RENDER support */
#if CAIRO_XLIB_SURFACE_HAS_BUGGY_GRADIENTS
    surface->buggy_gradients = TRUE;
#endif
#if CAIRO_XLIB_SURFACE_HAS_BUGGY_PAD_REFLECT
    surface->buggy_pad_reflect = TRUE;
#endif
#if CAIRO_XLIB_SURFACE_HAS_BUGGY_REPEAT
    surface->buggy_repeat = TRUE;
#endif

    return CAIRO_STATUS_SUCCESS;
}


/* The xlib-fallback target differs from the xlib target in two ways:
 *
 * 1. It creates its surfaces without relying on the Render extension
 *
 * 2. It disables use of the Render extension for its surfaces
 *
 * This provides testing of the non-Render fallback paths we have in
 * cairo-xlib-surface.c
 */
static cairo_surface_t *
_cairo_boilerplate_xlib_fallback_create_surface (const char			 *name,
						 cairo_content_t		  content,
						 double				  width,
						 double				  height,
						 double				  max_width,
						 double				  max_height,
						 cairo_boilerplate_mode_t	  mode,
						 int				  id,
						 void				**closure)
{
    xlib_target_closure_t *xtc;
    Display *dpy;
    Screen *scr;
    int screen, x, y;
    XSetWindowAttributes attr;
    cairo_surface_t *surface;

    /* We're not yet bothering to support perf mode for the
     * xlib-fallback surface. */
    if (mode == CAIRO_BOILERPLATE_MODE_PERF)
	return NULL;

    /* We also don't support drawing with destination-alpha in the
     * xlib-fallback surface. */
    if (content == CAIRO_CONTENT_COLOR_ALPHA)
	return NULL;

    *closure = xtc = xmalloc (sizeof (xlib_target_closure_t));

    width = ceil (width);
    if (width < 1)
	width = 1;

    height = ceil (height);
    if (height < 1)
	height = 1;

    xtc->dpy = dpy = XOpenDisplay (NULL);
    if (xtc->dpy == NULL) {
	CAIRO_BOILERPLATE_DEBUG (("Failed to open display: %s\n", XDisplayName(0)));
	free (xtc);
	return NULL;
    }

    /* This kills performance, but it makes debugging much
     * easier. That's why we have it here only after explicitly not
     * supporting PERF mode.*/
    XSynchronize (dpy, 1);

    screen = DefaultScreen (dpy);
    if (! _cairo_boilerplate_xlib_check_screen_size (dpy, screen,
		                                     width, height)) {
	CAIRO_BOILERPLATE_DEBUG (("Surface is larger than the Screen.\n"));
	XCloseDisplay (dpy);
	free (xtc);
	return NULL;
    }

    /* tile the windows so threads do not overlap */
    scr = XScreenOfDisplay (dpy, screen);
    x = y = 0;
    if (id-- > 1) do {
	x += max_width;
	if (x + max_width > WidthOfScreen (scr)) {
	    x = 0;
	    y += max_height;
	    if (y + max_height > HeightOfScreen (scr)) {
		XCloseDisplay (dpy);
		free (xtc);
		return NULL;
	    }
	}
    } while (--id);

    attr.override_redirect = True;
    xtc->drawable = XCreateWindow (dpy, DefaultRootWindow (dpy),
				   x, y,
				   width, height, 0,
				   DefaultDepth (dpy, screen),
				   InputOutput,
				   DefaultVisual (dpy, screen),
				   CWOverrideRedirect, &attr);
    XMapWindow (dpy, xtc->drawable);
    xtc->drawable_is_pixmap = FALSE;

    surface = cairo_xlib_surface_create (dpy, xtc->drawable,
					 DefaultVisual (dpy, screen),
					 width, height);
    if (cairo_surface_status (surface))
	_cairo_boilerplate_xlib_cleanup (xtc);
    else
	cairo_boilerplate_xlib_surface_disable_render (surface);

    return surface;
}

static cairo_surface_t *
_cairo_boilerplate_xlib_reference_create_surface (const char			 *name,
						  cairo_content_t			  content,
						  double				  width,
						  double				  height,
						  double				  max_width,
						  double				  max_height,
						  cairo_boilerplate_mode_t	  mode,
						  int                               id,
						  void				**closure)
{
    xlib_target_closure_t *xtc;
    Display *dpy;
    cairo_surface_t *surface;
    const char *display;

    display = getenv ("CAIRO_REFERENCE_DISPLAY");
    if (display == NULL) {
	return _cairo_boilerplate_xlib_fallback_create_surface (name, content,
								width, height,
								max_width,
								max_height,
								mode, id,
								closure);
    }

    *closure = xtc = xcalloc (1, sizeof (xlib_target_closure_t));

    width = ceil (width);
    if (width < 1)
	width = 1;

    height = ceil (height);
    if (height < 1)
	height = 1;

    xtc->dpy = dpy = XOpenDisplay (display);
    if (xtc->dpy == NULL) {
	free (xtc);
	CAIRO_BOILERPLATE_DEBUG (("Failed to open display: %s\n", display));
	return NULL;
    }

    if (mode == CAIRO_BOILERPLATE_MODE_TEST)
	surface = _cairo_boilerplate_xlib_test_create_surface (dpy, content, width, height, xtc);
    else /* mode == CAIRO_BOILERPLATE_MODE_PERF */
	surface = _cairo_boilerplate_xlib_perf_create_surface (dpy, content, width, height, xtc);

    if (surface == NULL || cairo_surface_status (surface))
	_cairo_boilerplate_xlib_cleanup (xtc);

    return surface;
}
#endif

static const cairo_boilerplate_target_t targets[] = {
#if CAIRO_HAS_XLIB_XRENDER_SURFACE
    /* Acceleration architectures may make the results differ by a
     * bit, so we set the error tolerance to 1. */
    {
	"xlib", "xlib", NULL, "xlib-reference",
	CAIRO_SURFACE_TYPE_XLIB, CAIRO_CONTENT_COLOR_ALPHA, 1,
	"cairo_xlib_surface_create_with_xrender_format",
	_cairo_boilerplate_xlib_create_surface,
	NULL, NULL,
	_cairo_boilerplate_get_image_surface,
	cairo_surface_write_to_png,
	_cairo_boilerplate_xlib_cleanup,
	_cairo_boilerplate_xlib_synchronize
    },
    {
	"xlib", "xlib", NULL, "xlib-reference",
	CAIRO_SURFACE_TYPE_XLIB, CAIRO_CONTENT_COLOR, 1,
	"cairo_xlib_surface_create_with_xrender_format",
	_cairo_boilerplate_xlib_create_surface,
	NULL, NULL,
	_cairo_boilerplate_get_image_surface,
	cairo_surface_write_to_png,
	_cairo_boilerplate_xlib_cleanup,
	_cairo_boilerplate_xlib_synchronize
    },
    {
	"xlib-reference", "xlib", NULL, NULL,
	CAIRO_SURFACE_TYPE_XLIB, CAIRO_CONTENT_COLOR, 1,
	"cairo_xlib_surface_create",
	_cairo_boilerplate_xlib_reference_create_surface,
	NULL, NULL,
	NULL, /* get_image */
	cairo_surface_write_to_png,
	_cairo_boilerplate_xlib_cleanup,
	_cairo_boilerplate_xlib_synchronize
    },
#endif
#if CAIRO_HAS_XLIB_SURFACE
    /* This is a fallback surface which uses xlib fallbacks instead of
     * the Render extension. */
    {
	"xlib-fallback", "xlib", NULL, NULL,
	CAIRO_SURFACE_TYPE_XLIB, CAIRO_CONTENT_COLOR, 1,
	"cairo_xlib_surface_create",
	_cairo_boilerplate_xlib_fallback_create_surface,
	NULL, NULL,
	_cairo_boilerplate_get_image_surface,
	cairo_surface_write_to_png,
	_cairo_boilerplate_xlib_cleanup,
	_cairo_boilerplate_xlib_synchronize
    },
#endif
};
CAIRO_BOILERPLATE (xlib, targets)