e182e0d9c6c5c39fb41e50bfd33a8cedf703a2b6
[framework/graphics/cairo.git] / src / cairo-glx-context.c
1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2009 Eric Anholt
4  * Copyright © 2009 Chris Wilson
5  * Copyright © 2005 Red Hat, Inc
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it either under the terms of the GNU Lesser General Public
9  * License version 2.1 as published by the Free Software Foundation
10  * (the "LGPL") or, at your option, under the terms of the Mozilla
11  * Public License Version 1.1 (the "MPL"). If you do not alter this
12  * notice, a recipient may use your version of this file under either
13  * the MPL or the LGPL.
14  *
15  * You should have received a copy of the LGPL along with this library
16  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18  * You should have received a copy of the MPL along with this library
19  * in the file COPYING-MPL-1.1
20  *
21  * The contents of this file are subject to the Mozilla Public License
22  * Version 1.1 (the "License"); you may not use this file except in
23  * compliance with the License. You may obtain a copy of the License at
24  * http://www.mozilla.org/MPL/
25  *
26  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28  * the specific language governing rights and limitations.
29  *
30  * The Original Code is the cairo graphics library.
31  *
32  * The Initial Developer of the Original Code is Red Hat, Inc.
33  *
34  * Contributor(s):
35  *      Carl Worth <cworth@cworth.org>
36  *      Chris Wilson <chris@chris-wilson.co.uk>
37  */
38
39 #include "cairoint.h"
40
41 #include "cairo-gl-private.h"
42
43 #include "cairo-error-private.h"
44
45 #include <X11/Xutil.h>
46
47 /* XXX needs hooking into XCloseDisplay() */
48
49 typedef struct _cairo_glx_context {
50     cairo_gl_context_t base;
51
52     Display *display;
53     Window dummy_window;
54     GLXContext context;
55
56     Display *previous_display;
57     GLXDrawable previous_drawable;
58     GLXContext previous_context;
59
60     cairo_bool_t has_multithread_makecurrent;
61 } cairo_glx_context_t;
62
63 typedef struct _cairo_glx_surface {
64     cairo_gl_surface_t base;
65
66     Window win;
67 } cairo_glx_surface_t;
68
69 static cairo_bool_t
70 _context_acquisition_changed_glx_state (cairo_glx_context_t *ctx,
71                                         GLXDrawable current_drawable)
72 {
73     return !(ctx->previous_display == ctx->display &&
74              ctx->previous_drawable == current_drawable &&
75              ctx->previous_context == ctx->context);
76 }
77
78 static GLXDrawable
79 _glx_get_current_drawable (cairo_glx_context_t *ctx)
80 {
81     if (ctx->base.current_target == NULL ||
82         _cairo_gl_surface_is_texture (ctx->base.current_target)) {
83         return ctx->dummy_window;
84     }
85
86     return ((cairo_glx_surface_t *) ctx->base.current_target)->win;
87 }
88
89 static void
90 _glx_query_current_state (cairo_glx_context_t * ctx)
91 {
92     ctx->previous_drawable = glXGetCurrentDrawable ();
93     ctx->previous_display = glXGetCurrentDisplay ();
94     ctx->previous_context = glXGetCurrentContext ();
95 }
96
97 static void
98 _glx_acquire (void *abstract_ctx)
99 {
100     cairo_glx_context_t *ctx = abstract_ctx;
101     GLXDrawable current_drawable = _glx_get_current_drawable (ctx);
102
103     _glx_query_current_state (ctx);
104     if (!_context_acquisition_changed_glx_state (ctx, current_drawable))
105         return;
106
107     glXMakeCurrent (ctx->display, current_drawable, ctx->context);
108 }
109
110 static void
111 _glx_make_current (void *abstract_ctx, cairo_gl_surface_t *abstract_surface)
112 {
113     cairo_glx_context_t *ctx = abstract_ctx;
114     cairo_glx_surface_t *surface = (cairo_glx_surface_t *) abstract_surface;
115
116     /* Set the window as the target of our context. */
117     glXMakeCurrent (ctx->display, surface->win, ctx->context);
118 }
119
120 static void
121 _glx_release (void *abstract_ctx)
122 {
123     cairo_glx_context_t *ctx = abstract_ctx;
124
125     if (ctx->has_multithread_makecurrent || !ctx->base.thread_aware)
126         return;
127
128     glXMakeCurrent (ctx->display, None, None);
129 }
130
131 static void
132 _glx_swap_buffers (void *abstract_ctx,
133                    cairo_gl_surface_t *abstract_surface)
134 {
135     cairo_glx_context_t *ctx = abstract_ctx;
136     cairo_glx_surface_t *surface = (cairo_glx_surface_t *) abstract_surface;
137
138     glXSwapBuffers (ctx->display, surface->win);
139 }
140
141 static void
142 _glx_destroy (void *abstract_ctx)
143 {
144     cairo_glx_context_t *ctx = abstract_ctx;
145
146     if (ctx->dummy_window != None)
147         XDestroyWindow (ctx->display, ctx->dummy_window);
148
149     glXMakeCurrent (ctx->display, None, None);
150 }
151
152 static cairo_status_t
153 _glx_dummy_ctx (Display *dpy, GLXContext gl_ctx, Window *dummy)
154 {
155     int attr[3] = { GLX_FBCONFIG_ID, 0, None };
156     GLXFBConfig *config;
157     XVisualInfo *vi;
158     Colormap cmap;
159     XSetWindowAttributes swa;
160     Window win = None;
161     int cnt;
162
163     /* Create a dummy window created for the target GLX context that we can
164      * use to query the available GL/GLX extensions.
165      */
166     glXQueryContext (dpy, gl_ctx, GLX_FBCONFIG_ID, &attr[1]);
167
168     cnt = 0;
169     config = glXChooseFBConfig (dpy, DefaultScreen (dpy), attr, &cnt);
170     if (unlikely (cnt == 0))
171         return _cairo_error (CAIRO_STATUS_INVALID_FORMAT);
172
173     vi = glXGetVisualFromFBConfig (dpy, config[0]);
174     XFree (config);
175
176     if (unlikely (vi == NULL))
177         return _cairo_error (CAIRO_STATUS_INVALID_FORMAT);
178
179     cmap = XCreateColormap (dpy,
180                             RootWindow (dpy, vi->screen),
181                             vi->visual,
182                             AllocNone);
183     swa.colormap = cmap;
184     swa.border_pixel = 0;
185     win = XCreateWindow (dpy, RootWindow (dpy, vi->screen),
186                          -1, -1, 1, 1, 0,
187                          vi->depth,
188                          InputOutput,
189                          vi->visual,
190                          CWBorderPixel | CWColormap, &swa);
191     XFreeColormap (dpy, cmap);
192     XFree (vi);
193
194     XFlush (dpy);
195     if (unlikely (! glXMakeCurrent (dpy, win, gl_ctx))) {
196         XDestroyWindow (dpy, win);
197         return _cairo_error (CAIRO_STATUS_NO_MEMORY);
198     }
199
200     *dummy = win;
201     return CAIRO_STATUS_SUCCESS;
202 }
203
204 cairo_device_t *
205 cairo_glx_device_create (Display *dpy, GLXContext gl_ctx)
206 {
207     cairo_glx_context_t *ctx;
208     cairo_status_t status;
209     Window dummy = None;
210     const char *glx_extensions;
211
212     ctx = calloc (1, sizeof (cairo_glx_context_t));
213     if (unlikely (ctx == NULL))
214         return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
215
216     status = _glx_dummy_ctx (dpy, gl_ctx, &dummy);
217     if (unlikely (status)) {
218         free (ctx);
219         return _cairo_gl_context_create_in_error (status);
220     }
221
222     ctx->display = dpy;
223     ctx->dummy_window = dummy;
224     ctx->context = gl_ctx;
225
226     ctx->base.acquire = _glx_acquire;
227     ctx->base.release = _glx_release;
228     ctx->base.make_current = _glx_make_current;
229     ctx->base.swap_buffers = _glx_swap_buffers;
230     ctx->base.destroy = _glx_destroy;
231
232     status = _cairo_gl_dispatch_init (&ctx->base.dispatch,
233                                       (cairo_gl_get_proc_addr_func_t) glXGetProcAddress);
234     if (unlikely (status)) {
235         free (ctx);
236         return _cairo_gl_context_create_in_error (status);
237     }
238
239     status = _cairo_gl_context_init (&ctx->base);
240     if (unlikely (status)) {
241         free (ctx);
242         return _cairo_gl_context_create_in_error (status);
243     }
244
245     glx_extensions = glXQueryExtensionsString (dpy, DefaultScreen (dpy));
246     if (strstr(glx_extensions, "GLX_MESA_multithread_makecurrent")) {
247         ctx->has_multithread_makecurrent = TRUE;
248     }
249
250     ctx->base.release (ctx);
251
252     return &ctx->base.base;
253 }
254
255 Display *
256 cairo_glx_device_get_display (cairo_device_t *device)
257 {
258     cairo_glx_context_t *ctx;
259
260     if (device->backend->type != CAIRO_DEVICE_TYPE_GL) {
261         _cairo_error_throw (CAIRO_STATUS_DEVICE_TYPE_MISMATCH);
262         return NULL;
263     }
264
265     ctx = (cairo_glx_context_t *) device;
266
267     return ctx->display;
268 }
269
270 GLXContext
271 cairo_glx_device_get_context (cairo_device_t *device)
272 {
273     cairo_glx_context_t *ctx;
274
275     if (device->backend->type != CAIRO_DEVICE_TYPE_GL) {
276         _cairo_error_throw (CAIRO_STATUS_DEVICE_TYPE_MISMATCH);
277         return NULL;
278     }
279
280     ctx = (cairo_glx_context_t *) device;
281
282     return ctx->context;
283 }
284
285 cairo_surface_t *
286 cairo_gl_surface_create_for_window (cairo_device_t      *device,
287                                     Window               win,
288                                     int                  width,
289                                     int                  height)
290 {
291     cairo_glx_surface_t *surface;
292
293     if (unlikely (device->status))
294         return _cairo_surface_create_in_error (device->status);
295
296     if (device->backend->type != CAIRO_DEVICE_TYPE_GL)
297         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
298
299     if (width <= 0 || height <= 0)
300         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
301
302     surface = calloc (1, sizeof (cairo_glx_surface_t));
303     if (unlikely (surface == NULL))
304         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
305
306     _cairo_gl_surface_init (device, &surface->base,
307                             CAIRO_CONTENT_COLOR_ALPHA, width, height);
308     surface->win = win;
309
310     return &surface->base.base;
311 }