1
/*
2
 * Copyright © 2008 Chris Wilson
3
 *
4
 * Permission to use, copy, modify, distribute, and sell this software
5
 * and its documentation for any purpose is hereby granted without
6
 * fee, provided that the above copyright notice appear in all copies
7
 * and that both that copyright notice and this permission notice
8
 * appear in supporting documentation, and that the name of
9
 * Chris Wilson not be used in advertising or publicity pertaining to
10
 * distribution of the software without specific, written prior
11
 * permission. Chris Wilson makes no representations about the
12
 * suitability of this software for any purpose.  It is provided "as
13
 * is" without express or implied warranty.
14
 *
15
 * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17
 * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
18
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
 *
23
 * Author: Chris Wilson <chris@chris-wilson.co.uk>
24
 */
25

            
26
#include "cairo-test.h"
27
#include <cairo-xlib.h>
28
#if CAIRO_HAS_XLIB_XRENDER_SURFACE
29
#include <cairo-xlib-xrender.h>
30
#endif
31

            
32
#include "surface-source.c"
33

            
34
static cairo_user_data_key_t closure_key;
35

            
36
struct closure {
37
    cairo_device_t *device;
38
    Display *dpy;
39
    Pixmap pix;
40
};
41

            
42
static void
43
4
cleanup (void *data)
44
{
45
4
    struct closure *arg = data;
46

            
47
4
    cairo_device_finish (arg->device);
48
4
    cairo_device_destroy (arg->device);
49

            
50
4
    XFreePixmap (arg->dpy, arg->pix);
51
4
    XCloseDisplay (arg->dpy);
52

            
53
4
    free (arg);
54
4
}
55

            
56
static cairo_surface_t *
57
4
create_source_surface (int size)
58
{
59
#if CAIRO_HAS_XLIB_XRENDER_SURFACE
60
    XRenderPictFormat *xrender_format;
61
    struct closure *data;
62
    cairo_surface_t *surface;
63

            
64
4
    data = xmalloc (sizeof (struct closure));
65

            
66
4
    data->dpy = XOpenDisplay (NULL);
67
4
    if (!data->dpy) {
68
	return NULL;
69
    }
70

            
71
4
    xrender_format = XRenderFindStandardFormat (data->dpy, PictStandardARGB32);
72

            
73
8
    data->pix = XCreatePixmap (data->dpy, DefaultRootWindow (data->dpy),
74
4
			       size, size, xrender_format->depth);
75

            
76
4
    surface = cairo_xlib_surface_create_with_xrender_format (data->dpy,
77
	                                                     data->pix,
78
4
							     DefaultScreenOfDisplay (data->dpy),
79
							     xrender_format,
80
							     size, size);
81
4
    data->device = cairo_device_reference (cairo_surface_get_device (surface));
82
4
    if (cairo_surface_set_user_data (surface, &closure_key, data, cleanup)) {
83
	cairo_surface_finish (surface);
84
	cairo_surface_destroy (surface);
85
	cleanup (data);
86
	return NULL;
87
    }
88

            
89
4
    return surface;
90
#else
91
    return NULL;
92
#endif
93
}
94

            
95
1
CAIRO_TEST (xlib_surface_source,
96
	    "Test using a Xlib surface as the source",
97
	    "source", /* keywords */
98
	    NULL, /* requirements */
99
	    SIZE, SIZE,
100
	    preamble, draw)