1 /*
2  * Copyright © 2012 Igalia S.L.
3  * Copyright © 2009 Eric Anholt
4  * Copyright © 2009 Chris Wilson
5  * Copyright © 2005 Red Hat, Inc
6  *
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.
17  *
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.
25  *
26  * Author: Martin Robinson <mrobinson@igalia.com>
27  */
28 
29 #include "cairo-test.h"
30 #include <cairo-gl.h>
31 #include <assert.h>
32 
33 static Window
create_test_window(Display * display,GLXContext glx_context,XVisualInfo * visual_info)34 create_test_window (Display *display,
35 		    GLXContext glx_context,
36 		    XVisualInfo *visual_info)
37 {
38     Colormap colormap;
39     XSetWindowAttributes window_attributes;
40     Window window = None;
41 
42     colormap = XCreateColormap (display,
43 			    RootWindow (display, visual_info->screen),
44 			    visual_info->visual,
45 			    AllocNone);
46     window_attributes.colormap = colormap;
47     window_attributes.border_pixel = 0;
48     window = XCreateWindow (display, RootWindow (display, visual_info->screen),
49 			    -1, -1, 1, 1, 0,
50 			    visual_info->depth,
51 			    InputOutput,
52 			    visual_info->visual,
53 			    CWBorderPixel | CWColormap, &window_attributes);
54     XFreeColormap (display, colormap);
55 
56     XFlush (display);
57     return window;
58 }
59 
60 static cairo_bool_t
multithread_makecurrent_available(Display * display)61 multithread_makecurrent_available (Display *display)
62 {
63     const char *extensions = glXQueryExtensionsString (display,
64 						       DefaultScreen (display));
65     return !! strstr(extensions, "GLX_MESA_multithread_makecurrent");
66 }
67 
68 static void
draw_to_surface(cairo_surface_t * surface)69 draw_to_surface (cairo_surface_t *surface)
70 {
71     cairo_t *cr = cairo_create (surface);
72     cairo_paint (cr);
73     cairo_destroy (cr);
74 }
75 
76 static cairo_test_status_t
preamble(cairo_test_context_t * test_ctx)77 preamble (cairo_test_context_t *test_ctx)
78 {
79     int rgba_attribs[] = {
80 	GLX_RGBA,
81 	GLX_RED_SIZE, 1,
82 	GLX_GREEN_SIZE, 1,
83 	GLX_BLUE_SIZE, 1,
84 	GLX_ALPHA_SIZE, 1,
85 	GLX_DOUBLEBUFFER,
86 	None
87     };
88 
89     XVisualInfo *visual_info;
90     GLXContext glx_context;
91     cairo_device_t *device;
92     Display *display;
93     Window test_window;
94     cairo_surface_t *window_surface;
95     cairo_bool_t has_multithread_makecurrent;
96 
97     display = XOpenDisplay (NULL);
98     if (display == NULL)
99 	return CAIRO_TEST_UNTESTED;
100 
101     visual_info = glXChooseVisual (display, DefaultScreen (display), rgba_attribs);
102     if (visual_info == NULL) {
103 	XCloseDisplay (display);
104 	return CAIRO_TEST_UNTESTED;
105     }
106 
107     glx_context = glXCreateContext (display, visual_info, NULL, True);
108     if (glx_context == NULL) {
109 	XCloseDisplay (display);
110 	return CAIRO_TEST_UNTESTED;
111     }
112 
113     test_window = create_test_window (display, glx_context, visual_info);
114     XFree (visual_info);
115     if (test_window == None) {
116 	XCloseDisplay (display);
117 	return CAIRO_TEST_UNTESTED;
118     }
119 
120     has_multithread_makecurrent = multithread_makecurrent_available (display);
121 
122     glXMakeCurrent (display, None, None);
123 
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);
127 
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 ());
135     } else {
136 	assert (None == glXGetCurrentDrawable ());
137 	assert (None == glXGetCurrentDisplay ());
138 	assert (None == glXGetCurrentContext ());
139     }
140 
141     window_surface = cairo_gl_surface_create_for_window (device, test_window,
142 							 1, 1);
143     assert (cairo_surface_status (window_surface) == CAIRO_STATUS_SUCCESS);
144 
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 ());
150     } else {
151 	assert (None == glXGetCurrentDrawable ());
152 	assert (None == glXGetCurrentDisplay ());
153 	assert (None == glXGetCurrentContext ());
154     }
155 
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 ());
163 
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 ());
169 
170     glXDestroyContext(display, glx_context);
171     XDestroyWindow (display, test_window);
172     XCloseDisplay (display);
173 
174     return CAIRO_TEST_SUCCESS;
175 }
176 
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",
179 	    "gl", /* keywords */
180 	    NULL, /* requirements */
181 	    0, 0,
182 	    preamble, NULL)
183