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));
106 dpy = XOpenDisplay (NULL);
109 fprintf (stderr, "Failed to open display: %s\n", XDisplayName(0));
114 if (mode == CAIRO_BOILERPLATE_MODE_TEST)
115 XSynchronize (gltc->dpy, 1);
117 if (content == CAIRO_CONTENT_COLOR)
118 visinfo = glXChooseVisual (dpy, DefaultScreen (dpy), rgb_attribs);
120 visinfo = glXChooseVisual (dpy, DefaultScreen (dpy), rgba_attribs);
122 if (visinfo == NULL) {
123 fprintf (stderr, "Failed to create RGB, double-buffered visual\n");
129 ctx = glXCreateContext (dpy, visinfo, NULL, True);
133 gltc->device = cairo_glx_device_create (dpy, ctx);
135 gltc->surface = surface = cairo_gl_surface_create (gltc->device,
139 if (cairo_surface_status (surface))
140 _cairo_boilerplate_gl_cleanup (gltc);
145 static cairo_surface_t *
146 _cairo_boilerplate_gl_create_window (const char *name,
147 cairo_content_t content,
152 cairo_boilerplate_mode_t mode,
155 int rgba_attribs[] = { GLX_RGBA,
163 int msaa_attribs[] = { GLX_RGBA,
170 GLX_SAMPLE_BUFFERS, 1,
176 gl_target_closure_t *gltc;
177 cairo_surface_t *surface;
179 XSetWindowAttributes attr;
181 gltc = calloc (1, sizeof (gl_target_closure_t));
189 dpy = XOpenDisplay (NULL);
192 fprintf (stderr, "Failed to open display: %s\n", XDisplayName(0));
197 if (mode == CAIRO_BOILERPLATE_MODE_TEST)
198 XSynchronize (gltc->dpy, 1);
200 vi = glXChooseVisual (dpy, DefaultScreen (dpy), msaa_attribs);
203 vi = glXChooseVisual (dpy, DefaultScreen (dpy), rgba_attribs);
206 fprintf (stderr, "Failed to create RGBA, double-buffered visual\n");
212 attr.colormap = XCreateColormap (dpy,
213 RootWindow (dpy, vi->screen),
216 attr.border_pixel = 0;
217 attr.override_redirect = True;
218 gltc->drawable = XCreateWindow (dpy, DefaultRootWindow (dpy), 0, 0,
219 width, height, 0, vi->depth,
220 InputOutput, vi->visual,
221 CWOverrideRedirect | CWBorderPixel | CWColormap,
223 XMapWindow (dpy, gltc->drawable);
225 ctx = glXCreateContext (dpy, vi, NULL, True);
229 gltc->device = cairo_glx_device_create (dpy, ctx);
231 gltc->surface = surface = cairo_gl_surface_create_for_window (gltc->device,
235 if (cairo_surface_status (surface))
236 _cairo_boilerplate_gl_cleanup (gltc);
241 static cairo_surface_t *
242 _cairo_boilerplate_gl_create_window_db (const char *name,
243 cairo_content_t content,
248 cairo_boilerplate_mode_t mode,
251 int rgba_attribs[] = { GLX_RGBA,
259 int msaa_attribs[] = { GLX_RGBA,
266 GLX_SAMPLE_BUFFERS, 1,
272 gl_target_closure_t *gltc;
273 cairo_surface_t *surface;
275 XSetWindowAttributes attr;
276 cairo_status_t status;
278 gltc = calloc (1, sizeof (gl_target_closure_t));
286 dpy = XOpenDisplay (NULL);
289 fprintf (stderr, "Failed to open display: %s\n", XDisplayName(0));
294 if (mode == CAIRO_BOILERPLATE_MODE_TEST)
295 XSynchronize (gltc->dpy, 1);
297 vi = glXChooseVisual (dpy, DefaultScreen (dpy), msaa_attribs);
300 vi = glXChooseVisual (dpy, DefaultScreen (dpy), rgba_attribs);
303 fprintf (stderr, "Failed to create RGBA, double-buffered visual\n");
309 attr.colormap = XCreateColormap (dpy,
310 RootWindow (dpy, vi->screen),
313 attr.border_pixel = 0;
314 attr.override_redirect = True;
315 gltc->drawable = XCreateWindow (dpy, DefaultRootWindow (dpy), 0, 0,
316 width, height, 0, vi->depth,
317 InputOutput, vi->visual,
318 CWOverrideRedirect | CWBorderPixel | CWColormap,
320 XMapWindow (dpy, gltc->drawable);
322 ctx = glXCreateContext (dpy, vi, NULL, True);
326 gltc->device = cairo_glx_device_create (dpy, ctx);
328 gltc->surface = cairo_gl_surface_create_for_window (gltc->device,
332 surface = cairo_surface_create_similar (gltc->surface, content, width, height);
333 status = cairo_surface_set_user_data (surface, &gl_closure_key, gltc, NULL);
334 if (status == CAIRO_STATUS_SUCCESS)
337 cairo_surface_destroy (surface);
338 _cairo_boilerplate_gl_cleanup (gltc);
339 return cairo_boilerplate_surface_create_in_error (status);
342 static cairo_status_t
343 _cairo_boilerplate_gl_finish_window (cairo_surface_t *surface)
345 gl_target_closure_t *gltc = cairo_surface_get_user_data (surface,
348 if (gltc != NULL && gltc->surface != NULL) {
351 cr = cairo_create (gltc->surface);
352 cairo_surface_set_device_offset (surface, 0, 0);
353 cairo_set_source_surface (cr, surface, 0, 0);
354 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
358 surface = gltc->surface;
361 cairo_gl_surface_swapbuffers (surface);
362 return CAIRO_STATUS_SUCCESS;
366 _cairo_boilerplate_gl_synchronize (void *closure)
368 gl_target_closure_t *gltc = closure;
370 if (cairo_device_acquire (gltc->device))
375 cairo_device_release (gltc->device);
379 _cairo_boilerplate_gl_describe (void *closure)
381 gl_target_closure_t *gltc = closure;
383 const GLubyte *vendor, *renderer, *version;
385 if (cairo_device_acquire (gltc->device))
388 vendor = glGetString (GL_VENDOR);
389 renderer = glGetString (GL_RENDERER);
390 version = glGetString (GL_VERSION);
392 xasprintf (&s, "%s %s %s", vendor, renderer, version);
394 cairo_device_release (gltc->device);
399 static const cairo_boilerplate_target_t targets[] = {
401 "gl", "gl", NULL, NULL,
402 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
403 "cairo_gl_surface_create",
404 _cairo_boilerplate_gl_create_surface,
405 cairo_surface_create_similar,
407 _cairo_boilerplate_get_image_surface,
408 cairo_surface_write_to_png,
409 _cairo_boilerplate_gl_cleanup,
410 _cairo_boilerplate_gl_synchronize,
411 _cairo_boilerplate_gl_describe,
415 "gl", "gl", NULL, NULL,
416 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR, 1,
417 "cairo_gl_surface_create",
418 _cairo_boilerplate_gl_create_surface,
419 cairo_surface_create_similar,
421 _cairo_boilerplate_get_image_surface,
422 cairo_surface_write_to_png,
423 _cairo_boilerplate_gl_cleanup,
424 _cairo_boilerplate_gl_synchronize,
425 _cairo_boilerplate_gl_describe,
429 "gl-window", "gl", NULL, NULL,
430 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
431 "cairo_gl_surface_create_for_window",
432 _cairo_boilerplate_gl_create_window,
433 cairo_surface_create_similar,
435 _cairo_boilerplate_gl_finish_window,
436 _cairo_boilerplate_get_image_surface,
437 cairo_surface_write_to_png,
438 _cairo_boilerplate_gl_cleanup,
439 _cairo_boilerplate_gl_synchronize,
440 _cairo_boilerplate_gl_describe,
444 "gl-window&", "gl", NULL, NULL,
445 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
446 "cairo_gl_surface_create_for_window",
447 _cairo_boilerplate_gl_create_window_db,
448 cairo_surface_create_similar,
450 _cairo_boilerplate_gl_finish_window,
451 _cairo_boilerplate_get_image_surface,
452 cairo_surface_write_to_png,
453 _cairo_boilerplate_gl_cleanup,
454 _cairo_boilerplate_gl_synchronize,
455 _cairo_boilerplate_gl_describe,
459 CAIRO_BOILERPLATE (gl, targets)