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 (const char *name,
148 cairo_content_t content,
153 cairo_boilerplate_mode_t mode,
156 int rgba_attribs[] = { GLX_RGBA,
164 int msaa_attribs[] = { GLX_RGBA,
171 GLX_SAMPLE_BUFFERS, 1,
177 gl_target_closure_t *gltc;
178 cairo_surface_t *surface;
180 XSetWindowAttributes attr;
182 gltc = calloc (1, sizeof (gl_target_closure_t));
185 width = ceil (width);
186 height = ceil (height);
193 dpy = XOpenDisplay (NULL);
196 fprintf (stderr, "Failed to open display: %s\n", XDisplayName(0));
201 if (mode == CAIRO_BOILERPLATE_MODE_TEST)
202 XSynchronize (gltc->dpy, 1);
204 vi = glXChooseVisual (dpy, DefaultScreen (dpy), msaa_attribs);
207 vi = glXChooseVisual (dpy, DefaultScreen (dpy), rgba_attribs);
210 fprintf (stderr, "Failed to create RGBA, double-buffered visual\n");
216 attr.colormap = XCreateColormap (dpy,
217 RootWindow (dpy, vi->screen),
220 attr.border_pixel = 0;
221 attr.override_redirect = True;
222 gltc->drawable = XCreateWindow (dpy, DefaultRootWindow (dpy), 0, 0,
223 width, height, 0, vi->depth,
224 InputOutput, vi->visual,
225 CWOverrideRedirect | CWBorderPixel | CWColormap,
227 XMapWindow (dpy, gltc->drawable);
229 ctx = glXCreateContext (dpy, vi, NULL, True);
233 gltc->device = cairo_glx_device_create (dpy, ctx);
235 gltc->surface = surface = cairo_gl_surface_create_for_window (gltc->device,
238 if (cairo_surface_status (surface))
239 _cairo_boilerplate_gl_cleanup (gltc);
244 static cairo_surface_t *
245 _cairo_boilerplate_gl_create_window_db (const char *name,
246 cairo_content_t content,
251 cairo_boilerplate_mode_t mode,
254 int rgba_attribs[] = { GLX_RGBA,
262 int msaa_attribs[] = { GLX_RGBA,
269 GLX_SAMPLE_BUFFERS, 1,
275 gl_target_closure_t *gltc;
276 cairo_surface_t *surface;
278 XSetWindowAttributes attr;
279 cairo_status_t status;
281 gltc = calloc (1, sizeof (gl_target_closure_t));
284 width = ceil (width);
285 height = ceil (height);
292 dpy = XOpenDisplay (NULL);
295 fprintf (stderr, "Failed to open display: %s\n", XDisplayName(0));
300 if (mode == CAIRO_BOILERPLATE_MODE_TEST)
301 XSynchronize (gltc->dpy, 1);
303 vi = glXChooseVisual (dpy, DefaultScreen (dpy), msaa_attribs);
306 vi = glXChooseVisual (dpy, DefaultScreen (dpy), rgba_attribs);
309 fprintf (stderr, "Failed to create RGBA, double-buffered visual\n");
315 attr.colormap = XCreateColormap (dpy,
316 RootWindow (dpy, vi->screen),
319 attr.border_pixel = 0;
320 attr.override_redirect = True;
321 gltc->drawable = XCreateWindow (dpy, DefaultRootWindow (dpy), 0, 0,
322 width, height, 0, vi->depth,
323 InputOutput, vi->visual,
324 CWOverrideRedirect | CWBorderPixel | CWColormap,
326 XMapWindow (dpy, gltc->drawable);
328 ctx = glXCreateContext (dpy, vi, NULL, True);
332 gltc->device = cairo_glx_device_create (dpy, ctx);
334 gltc->surface = cairo_gl_surface_create_for_window (gltc->device,
337 surface = cairo_surface_create_similar (gltc->surface, content, width, height);
338 status = cairo_surface_set_user_data (surface, &gl_closure_key, gltc, NULL);
339 if (status == CAIRO_STATUS_SUCCESS)
342 cairo_surface_destroy (surface);
343 _cairo_boilerplate_gl_cleanup (gltc);
344 return cairo_boilerplate_surface_create_in_error (status);
347 static cairo_status_t
348 _cairo_boilerplate_gl_finish_window (cairo_surface_t *surface)
350 gl_target_closure_t *gltc = cairo_surface_get_user_data (surface,
353 if (gltc != NULL && gltc->surface != NULL) {
356 cr = cairo_create (gltc->surface);
357 cairo_surface_set_device_offset (surface, 0, 0);
358 cairo_set_source_surface (cr, surface, 0, 0);
359 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
363 surface = gltc->surface;
366 cairo_gl_surface_swapbuffers (surface);
367 return CAIRO_STATUS_SUCCESS;
371 _cairo_boilerplate_gl_synchronize (void *closure)
373 gl_target_closure_t *gltc = closure;
375 if (cairo_device_acquire (gltc->device))
380 cairo_device_release (gltc->device);
384 _cairo_boilerplate_gl_describe (void *closure)
386 gl_target_closure_t *gltc = closure;
388 const GLubyte *vendor, *renderer, *version;
390 if (cairo_device_acquire (gltc->device))
393 vendor = glGetString (GL_VENDOR);
394 renderer = glGetString (GL_RENDERER);
395 version = glGetString (GL_VERSION);
397 xasprintf (&s, "%s %s %s", vendor, renderer, version);
399 cairo_device_release (gltc->device);
404 static const cairo_boilerplate_target_t targets[] = {
406 "gl", "gl", NULL, NULL,
407 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
408 "cairo_gl_surface_create",
409 _cairo_boilerplate_gl_create_surface,
410 cairo_surface_create_similar,
412 _cairo_boilerplate_get_image_surface,
413 cairo_surface_write_to_png,
414 _cairo_boilerplate_gl_cleanup,
415 _cairo_boilerplate_gl_synchronize,
416 _cairo_boilerplate_gl_describe,
420 "gl", "gl", NULL, NULL,
421 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR, 1,
422 "cairo_gl_surface_create",
423 _cairo_boilerplate_gl_create_surface,
424 cairo_surface_create_similar,
426 _cairo_boilerplate_get_image_surface,
427 cairo_surface_write_to_png,
428 _cairo_boilerplate_gl_cleanup,
429 _cairo_boilerplate_gl_synchronize,
430 _cairo_boilerplate_gl_describe,
434 "gl-window", "gl", NULL, NULL,
435 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
436 "cairo_gl_surface_create_for_window",
437 _cairo_boilerplate_gl_create_window,
438 cairo_surface_create_similar,
440 _cairo_boilerplate_gl_finish_window,
441 _cairo_boilerplate_get_image_surface,
442 cairo_surface_write_to_png,
443 _cairo_boilerplate_gl_cleanup,
444 _cairo_boilerplate_gl_synchronize,
445 _cairo_boilerplate_gl_describe,
449 "gl-window&", "gl", NULL, NULL,
450 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
451 "cairo_gl_surface_create_for_window",
452 _cairo_boilerplate_gl_create_window_db,
453 cairo_surface_create_similar,
455 _cairo_boilerplate_gl_finish_window,
456 _cairo_boilerplate_get_image_surface,
457 cairo_surface_write_to_png,
458 _cairo_boilerplate_gl_cleanup,
459 _cairo_boilerplate_gl_synchronize,
460 _cairo_boilerplate_gl_describe,
464 CAIRO_BOILERPLATE (gl, targets)