Tizen 2.0 Release
[framework/graphics/cairo.git] / test / gl-surface-source.c
1 /*
2  * Copyright © 2008 Chris Wilson
3  * Copyright © 2010 Intel Corporation
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of
10  * Chris Wilson not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. Chris Wilson makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as
14  * is" without express or implied warranty.
15  *
16  * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
19  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Author: Chris Wilson <chris@chris-wilson.co.uk>
25  */
26
27 #include "cairo-test.h"
28 #include <cairo-gl.h>
29
30 #include "surface-source.c"
31
32 struct closure {
33     Display *dpy;
34     GLXContext ctx;
35 };
36
37 static void
38 cleanup (void *data)
39 {
40     struct closure *arg = data;
41
42     glXDestroyContext (arg->dpy, arg->ctx);
43     XCloseDisplay (arg->dpy);
44
45     free (arg);
46 }
47
48 static cairo_surface_t *
49 create_source_surface (int size)
50 {
51     int rgba_attribs[] = {
52         GLX_RGBA,
53         GLX_RED_SIZE, 1,
54         GLX_GREEN_SIZE, 1,
55         GLX_BLUE_SIZE, 1,
56         GLX_ALPHA_SIZE, 1,
57         GLX_DOUBLEBUFFER,
58         None
59     };
60     XVisualInfo *visinfo;
61     GLXContext ctx;
62     struct closure *arg;
63     cairo_device_t *device;
64     cairo_surface_t *surface;
65     Display *dpy;
66
67     dpy = XOpenDisplay (NULL);
68     if (dpy == NULL)
69         return NULL;
70
71     visinfo = glXChooseVisual (dpy, DefaultScreen (dpy), rgba_attribs);
72     if (visinfo == NULL) {
73         XCloseDisplay (dpy);
74         return NULL;
75     }
76
77     ctx = glXCreateContext (dpy, visinfo, NULL, True);
78     XFree (visinfo);
79
80     if (ctx == NULL) {
81         XCloseDisplay (dpy);
82         return NULL;
83     }
84
85     arg = xmalloc (sizeof (struct closure));
86     arg->dpy = dpy;
87     arg->ctx = ctx;
88     device = cairo_glx_device_create (dpy, ctx);
89     if (cairo_device_set_user_data (device,
90                                     (cairo_user_data_key_t *) cleanup,
91                                     arg,
92                                     cleanup))
93     {
94         cleanup (arg);
95         return NULL;
96     }
97
98     surface = cairo_gl_surface_create (device,
99                                        CAIRO_CONTENT_COLOR_ALPHA,
100                                        size, size);
101     cairo_device_destroy (device);
102
103     return surface;
104 }
105
106 CAIRO_TEST (gl_surface_source,
107             "Test using a GL surface as the source",
108             "source", /* keywords */
109             NULL, /* requirements */
110             SIZE, SIZE,
111             preamble, draw)