1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2009 Eric Anholt
4 * Copyright © 2009 Chris Wilson
5 * Copyright © 2005 Red Hat, Inc
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.
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
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/
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.
30 * The Original Code is the cairo graphics library.
32 * The Initial Developer of the Original Code is Red Hat, Inc.
35 * Carl Worth <cworth@cworth.org>
36 * Chris Wilson <chris@chris-wilson.co.uk>
41 #include "cairo-gl-private.h"
43 #include "cairo-error-private.h"
45 typedef struct _cairo_egl_context {
46 cairo_gl_context_t base;
51 EGLSurface dummy_surface;
53 EGLDisplay previous_display;
54 EGLContext previous_context;
55 EGLSurface previous_surface;
56 } cairo_egl_context_t;
58 typedef struct _cairo_egl_surface {
59 cairo_gl_surface_t base;
62 } cairo_egl_surface_t;
66 _context_acquisition_changed_egl_state (cairo_egl_context_t *ctx,
67 EGLSurface current_surface)
69 return !(ctx->previous_display == ctx->display &&
70 ctx->previous_surface == current_surface &&
71 ctx->previous_context == ctx->context);
75 _egl_get_current_surface (cairo_egl_context_t *ctx)
77 if (ctx->base.current_target == NULL ||
78 _cairo_gl_surface_is_texture (ctx->base.current_target)) {
79 return ctx->dummy_surface;
82 return ((cairo_egl_surface_t *) ctx->base.current_target)->egl;
86 _egl_query_current_state (cairo_egl_context_t * ctx)
88 ctx->previous_display = eglGetCurrentDisplay ();
89 ctx->previous_surface = eglGetCurrentSurface (EGL_DRAW);
90 ctx->previous_context = eglGetCurrentContext ();
94 _egl_acquire (void *abstract_ctx)
96 cairo_egl_context_t *ctx = abstract_ctx;
97 EGLSurface current_surface = _egl_get_current_surface (ctx);
99 _egl_query_current_state (ctx);
100 if (!_context_acquisition_changed_egl_state (ctx, current_surface))
103 eglMakeCurrent (ctx->display,
104 current_surface, current_surface, ctx->context);
108 _egl_release (void *abstract_ctx)
110 cairo_egl_context_t *ctx = abstract_ctx;
111 if (!ctx->base.thread_aware)
114 eglMakeCurrent (ctx->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
120 _egl_make_current (void *abstract_ctx,
121 cairo_gl_surface_t *abstract_surface)
123 cairo_egl_context_t *ctx = abstract_ctx;
124 cairo_egl_surface_t *surface = (cairo_egl_surface_t *) abstract_surface;
126 eglMakeCurrent(ctx->display, surface->egl, surface->egl, ctx->context);
130 _egl_swap_buffers (void *abstract_ctx,
131 cairo_gl_surface_t *abstract_surface)
133 cairo_egl_context_t *ctx = abstract_ctx;
134 cairo_egl_surface_t *surface = (cairo_egl_surface_t *) abstract_surface;
136 eglSwapBuffers (ctx->display, surface->egl);
140 _egl_destroy (void *abstract_ctx)
142 cairo_egl_context_t *ctx = abstract_ctx;
144 eglMakeCurrent (ctx->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
147 if (ctx->dummy_surface != EGL_NO_SURFACE)
148 eglDestroySurface (ctx->display, ctx->dummy_surface);
152 _egl_make_current_surfaceless(cairo_egl_context_t *ctx)
154 const char *extensions;
156 extensions = eglQueryString(ctx->display, EGL_EXTENSIONS);
157 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl"))
159 if (!eglMakeCurrent(ctx->display,
160 EGL_NO_SURFACE, EGL_NO_SURFACE, ctx->context))
167 cairo_egl_device_create (EGLDisplay dpy, EGLContext egl)
169 cairo_egl_context_t *ctx;
170 cairo_status_t status;
179 ctx = calloc (1, sizeof (cairo_egl_context_t));
180 if (unlikely (ctx == NULL))
181 return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
186 ctx->base.acquire = _egl_acquire;
187 ctx->base.release = _egl_release;
188 ctx->base.make_current = _egl_make_current;
189 ctx->base.swap_buffers = _egl_swap_buffers;
190 ctx->base.destroy = _egl_destroy;
192 if (!_egl_make_current_surfaceless (ctx)) {
193 /* Fall back to dummy surface, meh. */
194 EGLint config_attribs[] = {
200 * In order to be able to make an egl context current when using a
201 * pbuffer surface, that surface must have been created with a config
202 * that is compatible with the context config. For Mesa, this means
203 * that the configs must be the same.
205 eglQueryContext (dpy, egl, EGL_CONFIG_ID, &config_attribs[1]);
206 eglChooseConfig (dpy, config_attribs, &config, 1, &numConfigs);
208 ctx->dummy_surface = eglCreatePbufferSurface (dpy, config, attribs);
210 if (ctx->dummy_surface == NULL) {
212 return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
215 if (!eglMakeCurrent (dpy, ctx->dummy_surface, ctx->dummy_surface, egl)) {
217 return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
221 status = _cairo_gl_dispatch_init (&ctx->base.dispatch, eglGetProcAddress);
222 if (unlikely (status)) {
224 return _cairo_gl_context_create_in_error (status);
227 status = _cairo_gl_context_init (&ctx->base);
228 if (unlikely (status)) {
229 if (ctx->dummy_surface != EGL_NO_SURFACE)
230 eglDestroySurface (dpy, ctx->dummy_surface);
232 return _cairo_gl_context_create_in_error (status);
235 ctx->base.release (ctx);
237 return &ctx->base.base;
241 cairo_gl_surface_create_for_egl (cairo_device_t *device,
246 cairo_egl_surface_t *surface;
248 if (unlikely (device->status))
249 return _cairo_surface_create_in_error (device->status);
251 if (device->backend->type != CAIRO_DEVICE_TYPE_GL)
252 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
254 if (width <= 0 || height <= 0)
255 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
257 surface = calloc (1, sizeof (cairo_egl_surface_t));
258 if (unlikely (surface == NULL))
259 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
261 _cairo_gl_surface_init (device, &surface->base,
262 CAIRO_CONTENT_COLOR_ALPHA, width, height);
265 return &surface->base.base;