2 * Copyright © 2012 Igalia S.L.
3 * Copyright © 2009 Eric Anholt
4 * Copyright © 2009 Chris Wilson
5 * Copyright © 2005 Red Hat, Inc
7 * Permission to use, copy, modify, distribute, and sell this software
8 * and its documentation for any purpose is hereby granted without
9 * fee, provided that the above copyright notice appear in all copies
10 * and that both that copyright notice and this permission notice
11 * appear in supporting documentation, and that the name of
12 * Chris Wilson not be used in advertising or publicity pertaining to
13 * distribution of the software without specific, written prior
14 * permission. Chris Wilson makes no representations about the
15 * suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * IGALIA S.L. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
19 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
21 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
22 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
23 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
24 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Author: Martin Robinson <mrobinson@igalia.com>
29 #include "cairo-test.h"
34 create_test_window (Display *display,
35 GLXContext glx_context,
36 XVisualInfo *visual_info)
39 XSetWindowAttributes window_attributes;
42 colormap = XCreateColormap (display,
43 RootWindow (display, visual_info->screen),
46 window_attributes.colormap = colormap;
47 window_attributes.border_pixel = 0;
48 window = XCreateWindow (display, RootWindow (display, visual_info->screen),
53 CWBorderPixel | CWColormap, &window_attributes);
54 XFreeColormap (display, colormap);
61 multithread_makecurrent_available (Display *display)
63 const char *extensions = glXQueryExtensionsString (display,
64 DefaultScreen (display));
65 return !! strstr(extensions, "GLX_MESA_multithread_makecurrent");
69 draw_to_surface (cairo_surface_t *surface)
71 cairo_t *cr = cairo_create (surface);
76 static cairo_test_status_t
77 preamble (cairo_test_context_t *test_ctx)
79 int rgba_attribs[] = {
89 XVisualInfo *visual_info;
90 GLXContext glx_context;
91 cairo_device_t *device;
94 cairo_surface_t *window_surface;
95 cairo_bool_t has_multithread_makecurrent;
97 display = XOpenDisplay (NULL);
99 return CAIRO_TEST_UNTESTED;
101 visual_info = glXChooseVisual (display, DefaultScreen (display), rgba_attribs);
102 if (visual_info == NULL) {
103 XCloseDisplay (display);
104 return CAIRO_TEST_UNTESTED;
107 glx_context = glXCreateContext (display, visual_info, NULL, True);
108 if (glx_context == NULL) {
109 XCloseDisplay (display);
110 return CAIRO_TEST_UNTESTED;
113 test_window = create_test_window (display, glx_context, visual_info);
115 if (test_window == None) {
116 XCloseDisplay (display);
117 return CAIRO_TEST_UNTESTED;
120 has_multithread_makecurrent = multithread_makecurrent_available (display);
122 glXMakeCurrent (display, None, None);
124 /* Creating the device should actually change the GL context, because of
125 * the creation/activation of a dummy window used for texture surfaces. */
126 device = cairo_glx_device_create (display, glx_context);
128 /* It's important that when multithread_makecurrent isn't available the
129 * Cairo backend clears the current context, so that the dummy texture
130 * window is not active while the device is unlocked. */
131 if (has_multithread_makecurrent) {
132 assert (None != glXGetCurrentDrawable ());
133 assert (display == glXGetCurrentDisplay ());
134 assert (glx_context == glXGetCurrentContext ());
136 assert (None == glXGetCurrentDrawable ());
137 assert (None == glXGetCurrentDisplay ());
138 assert (None == glXGetCurrentContext ());
141 window_surface = cairo_gl_surface_create_for_window (device, test_window,
143 assert (cairo_surface_status (window_surface) == CAIRO_STATUS_SUCCESS);
145 draw_to_surface (window_surface);
146 if (has_multithread_makecurrent) {
147 assert (test_window == glXGetCurrentDrawable ());
148 assert (display == glXGetCurrentDisplay ());
149 assert (glx_context == glXGetCurrentContext ());
151 assert (None == glXGetCurrentDrawable ());
152 assert (None == glXGetCurrentDisplay ());
153 assert (None == glXGetCurrentContext ());
156 /* In this case, drawing to the window surface will not change the current
157 * GL context, so Cairo setting the current surface and context to none. */
158 glXMakeCurrent (display, test_window, glx_context);
159 draw_to_surface (window_surface);
160 assert (test_window == glXGetCurrentDrawable ());
161 assert (display == glXGetCurrentDisplay ());
162 assert (glx_context == glXGetCurrentContext ());
164 /* There should be no context change when destroying the device. */
165 cairo_device_destroy (device);
166 assert (test_window == glXGetCurrentDrawable ());
167 assert (display == glXGetCurrentDisplay ());
168 assert (glx_context == glXGetCurrentContext ());
170 glXDestroyContext(display, glx_context);
171 XDestroyWindow (display, test_window);
172 XCloseDisplay (display);
174 return CAIRO_TEST_SUCCESS;
177 CAIRO_TEST (gl_device_creation_changes_context,
178 "Test that using the Cairo GL backend leaves the current GL context in the appropriate state",
180 NULL, /* requirements */