1 /* Cairo - a vector graphics library with display and print output
3 * Copyright © 2009 Chris Wilson
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is Chris Wilson.
33 #include "cairo-boilerplate-private.h"
38 #include <X11/Xutil.h> /* for XDestroyImage */
40 static const cairo_user_data_key_t gl_closure_key;
42 typedef struct _gl_target_closure {
48 cairo_device_t *device;
49 cairo_surface_t *surface;
50 } gl_target_closure_t;
53 _cairo_boilerplate_gl_cleanup (void *closure)
55 gl_target_closure_t *gltc = closure;
57 cairo_device_finish (gltc->device);
58 cairo_device_destroy (gltc->device);
60 glXDestroyContext (gltc->dpy, gltc->ctx);
63 XDestroyWindow (gltc->dpy, gltc->drawable);
64 XCloseDisplay (gltc->dpy);
69 static cairo_surface_t *
70 _cairo_boilerplate_gl_create_surface (const char *name,
71 cairo_content_t content,
76 cairo_boilerplate_mode_t mode,
79 int rgba_attribs[] = { GLX_RGBA,
86 int rgb_attribs[] = { GLX_RGBA,
94 gl_target_closure_t *gltc;
95 cairo_surface_t *surface;
98 gltc = calloc (1, sizeof (gl_target_closure_t));
101 width = ceil (width);
102 height = ceil (height);
109 dpy = XOpenDisplay (NULL);
112 fprintf (stderr, "Failed to open display: %s\n", XDisplayName(0));
117 if (mode == CAIRO_BOILERPLATE_MODE_TEST)
118 XSynchronize (gltc->dpy, 1);
120 if (content == CAIRO_CONTENT_COLOR)
121 visinfo = glXChooseVisual (dpy, DefaultScreen (dpy), rgb_attribs);
123 visinfo = glXChooseVisual (dpy, DefaultScreen (dpy), rgba_attribs);
125 if (visinfo == NULL) {
126 fprintf (stderr, "Failed to create RGB, double-buffered visual\n");
132 ctx = glXCreateContext (dpy, visinfo, NULL, True);
136 gltc->device = cairo_glx_device_create (dpy, ctx);
138 gltc->surface = surface = cairo_gl_surface_create (gltc->device,
139 content, width, height);
140 if (cairo_surface_status (surface))
141 _cairo_boilerplate_gl_cleanup (gltc);
146 static cairo_surface_t *
147 _cairo_boilerplate_gl_create_window_common (int rgba_attribs[],
148 cairo_content_t content,
153 cairo_boilerplate_mode_t mode,
154 gl_target_closure_t *gltc)
158 cairo_surface_t *surface;
160 XSetWindowAttributes attr;
162 width = ceil (width);
163 height = ceil (height);
170 dpy = XOpenDisplay (NULL);
173 fprintf (stderr, "Failed to open display: %s\n", XDisplayName(0));
178 if (mode == CAIRO_BOILERPLATE_MODE_TEST)
179 XSynchronize (gltc->dpy, 1);
181 vi = glXChooseVisual (dpy, DefaultScreen (dpy), rgba_attribs);
183 fprintf (stderr, "Failed to create RGBA, double-buffered visual\n");
189 attr.colormap = XCreateColormap (dpy,
190 RootWindow (dpy, vi->screen),
193 attr.border_pixel = 0;
194 attr.override_redirect = True;
195 gltc->drawable = XCreateWindow (dpy, DefaultRootWindow (dpy), 0, 0,
196 width, height, 0, vi->depth,
197 InputOutput, vi->visual,
198 CWOverrideRedirect | CWBorderPixel | CWColormap,
200 XMapWindow (dpy, gltc->drawable);
202 ctx = glXCreateContext (dpy, vi, NULL, True);
206 gltc->device = cairo_glx_device_create (dpy, ctx);
208 gltc->surface = surface = cairo_gl_surface_create_for_window (gltc->device,
211 if (cairo_surface_status (surface)) {
212 _cairo_boilerplate_gl_cleanup (gltc);
218 static cairo_surface_t *
219 _cairo_boilerplate_gl_create_window (const char *name,
220 cairo_content_t content,
225 cairo_boilerplate_mode_t mode,
228 gl_target_closure_t *gltc;
230 int rgba_attribs[] = { GLX_RGBA,
238 gltc = calloc (1, sizeof (gl_target_closure_t));
241 return _cairo_boilerplate_gl_create_window_common (rgba_attribs, content,
243 max_width, max_height,
247 static cairo_surface_t *
248 _cairo_boilerplate_gl_create_window_msaa (const char *name,
249 cairo_content_t content,
254 cairo_boilerplate_mode_t mode,
257 gl_target_closure_t *gltc;
259 int rgba_attribs[] = { GLX_RGBA,
266 GLX_SAMPLE_BUFFERS, 1,
270 gltc = calloc (1, sizeof (gl_target_closure_t));
272 return _cairo_boilerplate_gl_create_window_common (rgba_attribs, content,
274 max_width, max_height,
279 static cairo_surface_t *
280 _cairo_boilerplate_gl_create_window_db (const char *name,
281 cairo_content_t content,
286 cairo_boilerplate_mode_t mode,
289 cairo_status_t status;
290 cairo_surface_t *surface;
291 gl_target_closure_t *gltc;
293 int rgba_attribs[] = { GLX_RGBA,
301 gltc = calloc (1, sizeof (gl_target_closure_t));
304 surface = _cairo_boilerplate_gl_create_window_common (rgba_attribs, content,
306 max_width, max_height,
312 surface = cairo_surface_create_similar (gltc->surface, content, width, height);
313 status = cairo_surface_set_user_data (surface, &gl_closure_key, gltc, NULL);
314 if (status == CAIRO_STATUS_SUCCESS)
317 cairo_surface_destroy (surface);
318 _cairo_boilerplate_gl_cleanup (gltc);
319 return cairo_boilerplate_surface_create_in_error (status);
322 static cairo_status_t
323 _cairo_boilerplate_gl_finish_window (cairo_surface_t *surface)
325 gl_target_closure_t *gltc = cairo_surface_get_user_data (surface,
328 if (gltc != NULL && gltc->surface != NULL) {
331 cr = cairo_create (gltc->surface);
332 cairo_surface_set_device_offset (surface, 0, 0);
333 cairo_set_source_surface (cr, surface, 0, 0);
334 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
338 surface = gltc->surface;
341 cairo_gl_surface_swapbuffers (surface);
342 return CAIRO_STATUS_SUCCESS;
346 _cairo_boilerplate_gl_synchronize (void *closure)
348 gl_target_closure_t *gltc = closure;
350 if (cairo_device_acquire (gltc->device))
355 cairo_device_release (gltc->device);
359 _cairo_boilerplate_gl_describe (void *closure)
361 gl_target_closure_t *gltc = closure;
363 const GLubyte *vendor, *renderer, *version;
365 if (cairo_device_acquire (gltc->device))
368 vendor = glGetString (GL_VENDOR);
369 renderer = glGetString (GL_RENDERER);
370 version = glGetString (GL_VERSION);
372 xasprintf (&s, "%s %s %s", vendor, renderer, version);
374 cairo_device_release (gltc->device);
379 static const cairo_boilerplate_target_t targets[] = {
381 "gl", "gl", NULL, NULL,
382 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
383 "cairo_gl_surface_create",
384 _cairo_boilerplate_gl_create_surface,
385 cairo_surface_create_similar,
387 _cairo_boilerplate_get_image_surface,
388 cairo_surface_write_to_png,
389 _cairo_boilerplate_gl_cleanup,
390 _cairo_boilerplate_gl_synchronize,
391 _cairo_boilerplate_gl_describe,
395 "gl", "gl", NULL, NULL,
396 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR, 1,
397 "cairo_gl_surface_create",
398 _cairo_boilerplate_gl_create_surface,
399 cairo_surface_create_similar,
401 _cairo_boilerplate_get_image_surface,
402 cairo_surface_write_to_png,
403 _cairo_boilerplate_gl_cleanup,
404 _cairo_boilerplate_gl_synchronize,
405 _cairo_boilerplate_gl_describe,
409 "gl-window", "gl", NULL, NULL,
410 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
411 "cairo_gl_surface_create_for_window",
412 _cairo_boilerplate_gl_create_window,
413 cairo_surface_create_similar,
415 _cairo_boilerplate_gl_finish_window,
416 _cairo_boilerplate_get_image_surface,
417 cairo_surface_write_to_png,
418 _cairo_boilerplate_gl_cleanup,
419 _cairo_boilerplate_gl_synchronize,
420 _cairo_boilerplate_gl_describe,
424 "gl-window-msaa", "gl", NULL, NULL,
425 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
426 "cairo_gl_surface_create_for_window",
427 _cairo_boilerplate_gl_create_window_msaa,
428 cairo_surface_create_similar,
430 _cairo_boilerplate_gl_finish_window,
431 _cairo_boilerplate_get_image_surface,
432 cairo_surface_write_to_png,
433 _cairo_boilerplate_gl_cleanup,
434 _cairo_boilerplate_gl_synchronize,
435 _cairo_boilerplate_gl_describe,
439 "gl-window&", "gl", NULL, NULL,
440 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
441 "cairo_gl_surface_create_for_window",
442 _cairo_boilerplate_gl_create_window_db,
443 cairo_surface_create_similar,
445 _cairo_boilerplate_gl_finish_window,
446 _cairo_boilerplate_get_image_surface,
447 cairo_surface_write_to_png,
448 _cairo_boilerplate_gl_cleanup,
449 _cairo_boilerplate_gl_synchronize,
450 _cairo_boilerplate_gl_describe,
454 CAIRO_BOILERPLATE (gl, targets)