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 } cairo_glx_context_t;
56
57 typedef struct _cairo_glx_surface {
58 cairo_gl_surface_t base;
59
60 Window win;
61 } cairo_glx_surface_t;
62
63 static void
_glx_acquire(void * abstract_ctx)64 _glx_acquire (void *abstract_ctx)
65 {
66 cairo_glx_context_t *ctx = abstract_ctx;
67 GLXDrawable current_drawable;
68
69 if (ctx->base.current_target == NULL ||
70 _cairo_gl_surface_is_texture (ctx->base.current_target)) {
71 current_drawable = ctx->dummy_window;
72 } else {
73 cairo_glx_surface_t *surface = (cairo_glx_surface_t *) ctx->base.current_target;
74 current_drawable = surface->win;
75 }
76
77 glXMakeCurrent (ctx->display, current_drawable, ctx->context);
78 }
79
80 static void
_glx_make_current(void * abstract_ctx,cairo_gl_surface_t * abstract_surface)81 _glx_make_current (void *abstract_ctx, cairo_gl_surface_t *abstract_surface)
82 {
83 cairo_glx_context_t *ctx = abstract_ctx;
84 cairo_glx_surface_t *surface = (cairo_glx_surface_t *) abstract_surface;
85
86 /* Set the window as the target of our context. */
87 glXMakeCurrent (ctx->display, surface->win, ctx->context);
88 }
89
90 static void
_glx_release(void * abstract_ctx)91 _glx_release (void *abstract_ctx)
92 {
93 cairo_glx_context_t *ctx = abstract_ctx;
94
95 glXMakeCurrent (ctx->display, None, None);
96 }
97
98 static void
_glx_swap_buffers(void * abstract_ctx,cairo_gl_surface_t * abstract_surface)99 _glx_swap_buffers (void *abstract_ctx,
100 cairo_gl_surface_t *abstract_surface)
101 {
102 cairo_glx_context_t *ctx = abstract_ctx;
103 cairo_glx_surface_t *surface = (cairo_glx_surface_t *) abstract_surface;
104
105 glXSwapBuffers (ctx->display, surface->win);
106 }
107
108 static void
_glx_destroy(void * abstract_ctx)109 _glx_destroy (void *abstract_ctx)
110 {
111 cairo_glx_context_t *ctx = abstract_ctx;
112
113 if (ctx->dummy_window != None)
114 XDestroyWindow (ctx->display, ctx->dummy_window);
115
116 glXMakeCurrent (ctx->display, 0, 0);
117 }
118
119 static cairo_status_t
_glx_dummy_ctx(Display * dpy,GLXContext gl_ctx,Window * dummy)120 _glx_dummy_ctx (Display *dpy, GLXContext gl_ctx, Window *dummy)
121 {
122 int attr[3] = { GLX_FBCONFIG_ID, 0, None };
123 GLXFBConfig *config;
124 XVisualInfo *vi;
125 Colormap cmap;
126 XSetWindowAttributes swa;
127 Window win = None;
128 int cnt;
129
130 /* Create a dummy window created for the target GLX context that we can
131 * use to query the available GL/GLX extensions.
132 */
133 glXQueryContext (dpy, gl_ctx, GLX_FBCONFIG_ID, &attr[1]);
134
135 cnt = 0;
136 config = glXChooseFBConfig (dpy, DefaultScreen (dpy), attr, &cnt);
137 if (unlikely (cnt == 0))
138 return _cairo_error (CAIRO_STATUS_INVALID_FORMAT);
139
140 vi = glXGetVisualFromFBConfig (dpy, config[0]);
141 XFree (config);
142
143 if (unlikely (vi == NULL))
144 return _cairo_error (CAIRO_STATUS_INVALID_FORMAT);
145
146 cmap = XCreateColormap (dpy,
147 RootWindow (dpy, vi->screen),
148 vi->visual,
149 AllocNone);
150 swa.colormap = cmap;
151 swa.border_pixel = 0;
152 win = XCreateWindow (dpy, RootWindow (dpy, vi->screen),
153 -1, -1, 1, 1, 0,
154 vi->depth,
155 InputOutput,
156 vi->visual,
157 CWBorderPixel | CWColormap, &swa);
158 XFreeColormap (dpy, cmap);
159 XFree (vi);
160
161 XFlush (dpy);
162 if (unlikely (! glXMakeCurrent (dpy, win, gl_ctx))) {
163 XDestroyWindow (dpy, win);
164 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
165 }
166
167 *dummy = win;
168 return CAIRO_STATUS_SUCCESS;
169 }
170
171 cairo_device_t *
cairo_glx_device_create(Display * dpy,GLXContext gl_ctx)172 cairo_glx_device_create (Display *dpy, GLXContext gl_ctx)
173 {
174 cairo_glx_context_t *ctx;
175 cairo_status_t status;
176 Window dummy = None;
177
178 status = _glx_dummy_ctx (dpy, gl_ctx, &dummy);
179 if (unlikely (status))
180 return _cairo_gl_context_create_in_error (status);
181
182 ctx = calloc (1, sizeof (cairo_glx_context_t));
183 if (unlikely (ctx == NULL))
184 return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
185
186 ctx->display = dpy;
187 ctx->dummy_window = dummy;
188 ctx->context = gl_ctx;
189
190 ctx->base.acquire = _glx_acquire;
191 ctx->base.release = _glx_release;
192 ctx->base.make_current = _glx_make_current;
193 ctx->base.swap_buffers = _glx_swap_buffers;
194 ctx->base.destroy = _glx_destroy;
195
196 status = _cairo_gl_context_init (&ctx->base);
197 if (unlikely (status)) {
198 free (ctx);
199 return _cairo_gl_context_create_in_error (status);
200 }
201
202 ctx->base.release (ctx);
203
204 return &ctx->base.base;
205 }
206
207 Display *
cairo_glx_device_get_display(cairo_device_t * device)208 cairo_glx_device_get_display (cairo_device_t *device)
209 {
210 cairo_glx_context_t *ctx;
211
212 if (device->backend->type != CAIRO_DEVICE_TYPE_GL) {
213 _cairo_error_throw (CAIRO_STATUS_DEVICE_TYPE_MISMATCH);
214 return NULL;
215 }
216
217 ctx = (cairo_glx_context_t *) device;
218
219 return ctx->display;
220 }
221
222 GLXContext
cairo_glx_device_get_context(cairo_device_t * device)223 cairo_glx_device_get_context (cairo_device_t *device)
224 {
225 cairo_glx_context_t *ctx;
226
227 if (device->backend->type != CAIRO_DEVICE_TYPE_GL) {
228 _cairo_error_throw (CAIRO_STATUS_DEVICE_TYPE_MISMATCH);
229 return NULL;
230 }
231
232 ctx = (cairo_glx_context_t *) device;
233
234 return ctx->context;
235 }
236
237 cairo_surface_t *
cairo_gl_surface_create_for_window(cairo_device_t * device,Window win,int width,int height)238 cairo_gl_surface_create_for_window (cairo_device_t *device,
239 Window win,
240 int width,
241 int height)
242 {
243 cairo_glx_surface_t *surface;
244
245 if (unlikely (device->status))
246 return _cairo_surface_create_in_error (device->status);
247
248 if (device->backend->type != CAIRO_DEVICE_TYPE_GL)
249 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
250
251 surface = calloc (1, sizeof (cairo_glx_surface_t));
252 if (unlikely (surface == NULL))
253 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
254
255 _cairo_gl_surface_init (device, &surface->base,
256 CAIRO_CONTENT_COLOR_ALPHA, width, height);
257 surface->win = win;
258
259 return &surface->base.base;
260 }
261