74565e5390d3e8967feecf6138d7d5aa4729b458
[framework/graphics/cairo.git] / src / cairo-egl-context.c
1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2009 Eric Anholt
4  * Copyright © 2009 Chris Wilson
5  * Copyright © 2005 Red Hat, Inc
6  *
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.
14  *
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
20  *
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/
25  *
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.
29  *
30  * The Original Code is the cairo graphics library.
31  *
32  * The Initial Developer of the Original Code is Red Hat, Inc.
33  *
34  * Contributor(s):
35  *      Carl Worth <cworth@cworth.org>
36  *      Chris Wilson <chris@chris-wilson.co.uk>
37  */
38
39 #include "cairoint.h"
40
41 #include "cairo-gl-private.h"
42
43 #include "cairo-error-private.h"
44
45 typedef struct _cairo_egl_context {
46     cairo_gl_context_t base;
47
48     EGLDisplay display;
49     EGLContext context;
50
51     EGLSurface dummy_surface;
52
53     EGLDisplay previous_display;
54     EGLContext previous_context;
55     EGLSurface previous_surface;
56 } cairo_egl_context_t;
57
58 typedef struct _cairo_egl_surface {
59     cairo_gl_surface_t base;
60
61     EGLSurface egl;
62 } cairo_egl_surface_t;
63
64
65 static cairo_bool_t
66 _context_acquisition_changed_egl_state (cairo_egl_context_t *ctx,
67                                         EGLSurface current_surface)
68 {
69     return !(ctx->previous_display == ctx->display &&
70              ctx->previous_surface == current_surface &&
71              ctx->previous_context == ctx->context);
72 }
73
74 static EGLSurface
75 _egl_get_current_surface (cairo_egl_context_t *ctx)
76 {
77     if (ctx->base.current_target == NULL ||
78         _cairo_gl_surface_is_texture (ctx->base.current_target)) {
79         return  ctx->dummy_surface;
80     }
81
82     return ((cairo_egl_surface_t *) ctx->base.current_target)->egl;
83 }
84
85 static void
86 _egl_query_current_state (cairo_egl_context_t * ctx)
87 {
88     ctx->previous_display = eglGetCurrentDisplay ();
89     ctx->previous_surface = eglGetCurrentSurface (EGL_DRAW);
90     ctx->previous_context = eglGetCurrentContext ();
91 }
92
93 static void
94 _egl_acquire (void *abstract_ctx)
95 {
96     cairo_egl_context_t *ctx = abstract_ctx;
97     EGLSurface current_surface = _egl_get_current_surface (ctx);
98
99     _egl_query_current_state (ctx);
100     if (!_context_acquisition_changed_egl_state (ctx, current_surface))
101         return;
102
103     eglMakeCurrent (ctx->display,
104                     current_surface, current_surface, ctx->context);
105 }
106
107 static void
108 _egl_release (void *abstract_ctx)
109 {
110     cairo_egl_context_t *ctx = abstract_ctx;
111     if (!ctx->base.thread_aware)
112         return;
113
114     eglMakeCurrent (ctx->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
115                     EGL_NO_CONTEXT);
116
117 }
118
119 static void
120 _egl_make_current (void *abstract_ctx,
121                    cairo_gl_surface_t *abstract_surface)
122 {
123     cairo_egl_context_t *ctx = abstract_ctx;
124     cairo_egl_surface_t *surface = (cairo_egl_surface_t *) abstract_surface;
125
126     eglMakeCurrent(ctx->display, surface->egl, surface->egl, ctx->context);
127 }
128
129 static void
130 _egl_swap_buffers (void *abstract_ctx,
131                    cairo_gl_surface_t *abstract_surface)
132 {
133     cairo_egl_context_t *ctx = abstract_ctx;
134     cairo_egl_surface_t *surface = (cairo_egl_surface_t *) abstract_surface;
135
136     eglSwapBuffers (ctx->display, surface->egl);
137 }
138
139 static void
140 _egl_destroy (void *abstract_ctx)
141 {
142     cairo_egl_context_t *ctx = abstract_ctx;
143
144     eglMakeCurrent (ctx->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
145                     EGL_NO_CONTEXT);
146
147     if (ctx->dummy_surface != EGL_NO_SURFACE)
148         eglDestroySurface (ctx->display, ctx->dummy_surface);
149 }
150
151 static cairo_bool_t
152 _egl_make_current_surfaceless(cairo_egl_context_t *ctx)
153 {
154     const char *extensions;
155
156     extensions = eglQueryString(ctx->display, EGL_EXTENSIONS);
157     if (!strstr(extensions, "EGL_KHR_surfaceless_opengl"))
158         return FALSE;
159     if (!eglMakeCurrent(ctx->display,
160                         EGL_NO_SURFACE, EGL_NO_SURFACE, ctx->context))
161         return FALSE;
162
163     return TRUE;
164 }
165
166 cairo_device_t *
167 cairo_egl_device_create (EGLDisplay dpy, EGLContext egl)
168 {
169     cairo_egl_context_t *ctx;
170     cairo_status_t status;
171     int attribs[] = {
172         EGL_WIDTH, 1,
173         EGL_HEIGHT, 1,
174         EGL_NONE,
175     };
176     EGLConfig config;
177     EGLint numConfigs;
178
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);
182
183     ctx->display = dpy;
184     ctx->context = egl;
185
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;
191
192     if (!_egl_make_current_surfaceless (ctx)) {
193         /* Fall back to dummy surface, meh. */
194         EGLint config_attribs[] = {
195             EGL_CONFIG_ID, 0,
196             EGL_NONE
197         };
198
199         /*
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.
204          */
205         eglQueryContext (dpy, egl, EGL_CONFIG_ID, &config_attribs[1]);
206         eglChooseConfig (dpy, config_attribs, &config, 1, &numConfigs);
207
208         ctx->dummy_surface = eglCreatePbufferSurface (dpy, config, attribs);
209
210         if (ctx->dummy_surface == NULL) {
211             free (ctx);
212             return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
213         }
214
215         if (!eglMakeCurrent (dpy, ctx->dummy_surface, ctx->dummy_surface, egl)) {
216             free (ctx);
217             return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
218         }
219     }
220
221     status = _cairo_gl_dispatch_init (&ctx->base.dispatch, eglGetProcAddress);
222     if (unlikely (status)) {
223         free (ctx);
224         return _cairo_gl_context_create_in_error (status);
225     }
226
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);
231         free (ctx);
232         return _cairo_gl_context_create_in_error (status);
233     }
234
235     ctx->base.release (ctx);
236
237     return &ctx->base.base;
238 }
239
240 cairo_surface_t *
241 cairo_gl_surface_create_for_egl (cairo_device_t *device,
242                                  EGLSurface      egl,
243                                  int             width,
244                                  int             height)
245 {
246     cairo_egl_surface_t *surface;
247
248     if (unlikely (device->status))
249         return _cairo_surface_create_in_error (device->status);
250
251     if (device->backend->type != CAIRO_DEVICE_TYPE_GL)
252         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
253
254     if (width <= 0 || height <= 0)
255         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
256
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));
260
261     _cairo_gl_surface_init (device, &surface->base,
262                             CAIRO_CONTENT_COLOR_ALPHA, width, height);
263     surface->egl = egl;
264
265     return &surface->base.base;
266 }