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"
36 #if CAIRO_HAS_GL_SURFACE
38 #elif CAIRO_HAS_GLESV2_SURFACE
39 #include <GLES2/gl2.h>
42 static const cairo_user_data_key_t gl_closure_key;
44 typedef struct _egl_target_closure {
48 cairo_device_t *device;
49 cairo_surface_t *surface;
50 } egl_target_closure_t;
53 _cairo_boilerplate_egl_cleanup (void *closure)
55 egl_target_closure_t *gltc = closure;
57 cairo_device_finish (gltc->device);
58 cairo_device_destroy (gltc->device);
60 eglDestroyContext (gltc->dpy, gltc->ctx);
61 eglMakeCurrent (gltc->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
62 eglTerminate (gltc->dpy);
67 static cairo_surface_t *
68 _cairo_boilerplate_egl_create_surface (const char *name,
69 cairo_content_t content,
74 cairo_boilerplate_mode_t mode,
77 egl_target_closure_t *gltc;
78 cairo_surface_t *surface;
82 EGLint config_attribs[] = {
87 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
88 #if CAIRO_HAS_GL_SURFACE
89 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
90 #elif CAIRO_HAS_GLESV2_SURFACE
91 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
95 const EGLint ctx_attribs[] = {
96 #if CAIRO_HAS_GLESV2_SURFACE
97 EGL_CONTEXT_CLIENT_VERSION, 2,
102 gltc = xcalloc (1, sizeof (egl_target_closure_t));
105 gltc->dpy = eglGetDisplay (EGL_DEFAULT_DISPLAY);
107 if (! eglInitialize (gltc->dpy, &major, &minor)) {
112 eglChooseConfig (gltc->dpy, config_attribs, &config, 1, &numConfigs);
113 if (numConfigs == 0) {
118 #if CAIRO_HAS_GL_SURFACE
119 eglBindAPI (EGL_OPENGL_API);
120 #elif CAIRO_HAS_GLESV2_SURFACE
121 eglBindAPI (EGL_OPENGL_ES_API);
124 gltc->ctx = eglCreateContext (gltc->dpy, config, EGL_NO_CONTEXT,
126 if (gltc->ctx == EGL_NO_CONTEXT) {
127 eglTerminate (gltc->dpy);
132 gltc->device = cairo_egl_device_create (gltc->dpy, gltc->ctx);
133 if (mode == CAIRO_BOILERPLATE_MODE_PERF)
134 cairo_gl_device_set_thread_aware(gltc->device, FALSE);
141 gltc->surface = surface = cairo_gl_surface_create (gltc->device,
145 if (cairo_surface_status (surface))
146 _cairo_boilerplate_egl_cleanup (gltc);
152 _cairo_boilerplate_egl_synchronize (void *closure)
154 egl_target_closure_t *gltc = closure;
156 if (cairo_device_acquire (gltc->device))
161 cairo_device_release (gltc->device);
164 static const cairo_boilerplate_target_t targets[] = {
166 "egl", "gl", NULL, NULL,
167 CAIRO_SURFACE_TYPE_GL, CAIRO_CONTENT_COLOR_ALPHA, 1,
168 "cairo_egl_device_create",
169 _cairo_boilerplate_egl_create_surface,
170 cairo_surface_create_similar,
172 _cairo_boilerplate_get_image_surface,
173 cairo_surface_write_to_png,
174 _cairo_boilerplate_egl_cleanup,
175 _cairo_boilerplate_egl_synchronize,
180 CAIRO_BOILERPLATE (egl, targets)