Tizen 2.0 Release
[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     /* If any of the values were none, assume they are all none. Not all
93        drivers seem well behaved when it comes to using these values across
94        multiple threads. */
95     if (ctx->previous_surface == EGL_NO_SURFACE
96         || ctx->previous_display == EGL_NO_DISPLAY
97         || ctx->previous_context == EGL_NO_CONTEXT) {
98         ctx->previous_surface = EGL_NO_SURFACE;
99         ctx->previous_display = EGL_NO_DISPLAY;
100         ctx->previous_context = EGL_NO_CONTEXT;
101     }
102 }
103
104 static void
105 _egl_acquire (void *abstract_ctx)
106 {
107     cairo_egl_context_t *ctx = abstract_ctx;
108     EGLSurface current_surface = _egl_get_current_surface (ctx);
109
110     _egl_query_current_state (ctx);
111     if (!_context_acquisition_changed_egl_state (ctx, current_surface))
112         return;
113
114     _cairo_gl_context_reset (&ctx->base);
115
116     eglMakeCurrent (ctx->display,
117                     current_surface, current_surface, ctx->context);
118 }
119
120 static void
121 _egl_release (void *abstract_ctx)
122 {
123     cairo_egl_context_t *ctx = abstract_ctx;
124     if (!ctx->base.thread_aware ||
125         !_context_acquisition_changed_egl_state (ctx,
126                                                  _egl_get_current_surface (ctx))) {
127         return;
128     }
129
130     eglMakeCurrent (ctx->display,
131                     EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
132 }
133
134 static void
135 _egl_make_current (void *abstract_ctx,
136                    cairo_gl_surface_t *abstract_surface)
137 {
138     cairo_egl_context_t *ctx = abstract_ctx;
139     cairo_egl_surface_t *surface = (cairo_egl_surface_t *) abstract_surface;
140
141     eglMakeCurrent(ctx->display, surface->egl, surface->egl, ctx->context);
142 }
143
144 static void
145 _egl_swap_buffers (void *abstract_ctx,
146                    cairo_gl_surface_t *abstract_surface)
147 {
148     cairo_egl_context_t *ctx = abstract_ctx;
149     cairo_egl_surface_t *surface = (cairo_egl_surface_t *) abstract_surface;
150
151     eglSwapBuffers (ctx->display, surface->egl);
152 }
153
154 static void
155 _egl_destroy (void *abstract_ctx)
156 {
157     cairo_egl_context_t *ctx = abstract_ctx;
158
159     eglMakeCurrent (ctx->display,
160                     EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
161     if (ctx->dummy_surface != EGL_NO_SURFACE)
162         eglDestroySurface (ctx->display, ctx->dummy_surface);
163 }
164
165 static cairo_bool_t
166 _egl_make_current_surfaceless(cairo_egl_context_t *ctx)
167 {
168     const char *extensions;
169
170     extensions = eglQueryString(ctx->display, EGL_EXTENSIONS);
171     if (strstr(extensions, "EGL_KHR_surfaceless_context") == NULL &&
172         strstr(extensions, "EGL_KHR_surfaceless_opengl") == NULL)
173         return FALSE;
174
175     if (!eglMakeCurrent(ctx->display,
176                         EGL_NO_SURFACE, EGL_NO_SURFACE, ctx->context))
177         return FALSE;
178
179     return TRUE;
180 }
181
182 cairo_device_t *
183 cairo_egl_device_create (EGLDisplay dpy, EGLContext egl)
184 {
185     cairo_egl_context_t *ctx;
186     cairo_status_t status;
187     int attribs[] = {
188         EGL_WIDTH, 1,
189         EGL_HEIGHT, 1,
190         EGL_NONE,
191     };
192     EGLConfig config;
193     EGLint numConfigs;
194
195     ctx = calloc (1, sizeof (cairo_egl_context_t));
196     if (unlikely (ctx == NULL))
197         return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
198
199     ctx->display = dpy;
200     ctx->context = egl;
201
202     ctx->base.acquire = _egl_acquire;
203     ctx->base.release = _egl_release;
204     ctx->base.make_current = _egl_make_current;
205     ctx->base.swap_buffers = _egl_swap_buffers;
206     ctx->base.destroy = _egl_destroy;
207
208     /* We are about the change the current state of EGL, so we should
209      * query the pre-existing surface now instead of later. */
210     _egl_query_current_state (ctx);
211
212     if (!_egl_make_current_surfaceless (ctx)) {
213         /* Fall back to dummy surface, meh. */
214         EGLint config_attribs[] = {
215             EGL_CONFIG_ID, 0,
216             EGL_NONE
217         };
218
219         /*
220          * In order to be able to make an egl context current when using a
221          * pbuffer surface, that surface must have been created with a config
222          * that is compatible with the context config. For Mesa, this means
223          * that the configs must be the same.
224          */
225         eglQueryContext (dpy, egl, EGL_CONFIG_ID, &config_attribs[1]);
226         eglChooseConfig (dpy, config_attribs, &config, 1, &numConfigs);
227
228         ctx->dummy_surface = eglCreatePbufferSurface (dpy, config, attribs);
229         if (ctx->dummy_surface == NULL) {
230             free (ctx);
231             return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
232         }
233
234         if (!eglMakeCurrent (dpy, ctx->dummy_surface, ctx->dummy_surface, egl)) {
235             free (ctx);
236             return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
237         }
238     }
239
240     status = _cairo_gl_dispatch_init (&ctx->base.dispatch, eglGetProcAddress);
241     if (unlikely (status)) {
242         free (ctx);
243         return _cairo_gl_context_create_in_error (status);
244     }
245
246     status = _cairo_gl_context_init (&ctx->base);
247     if (unlikely (status)) {
248         if (ctx->dummy_surface != EGL_NO_SURFACE)
249             eglDestroySurface (dpy, ctx->dummy_surface);
250         free (ctx);
251         return _cairo_gl_context_create_in_error (status);
252     }
253
254     eglMakeCurrent (dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
255
256     return &ctx->base.base;
257 }
258
259 cairo_surface_t *
260 cairo_gl_surface_create_for_egl (cairo_device_t *device,
261                                  EGLSurface      egl,
262                                  int             width,
263                                  int             height)
264 {
265     cairo_egl_surface_t *surface;
266
267     if (unlikely (device->status))
268         return _cairo_surface_create_in_error (device->status);
269
270     if (device->backend->type != CAIRO_DEVICE_TYPE_GL)
271         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
272
273     if (width <= 0 || height <= 0)
274         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
275
276     surface = calloc (1, sizeof (cairo_egl_surface_t));
277     if (unlikely (surface == NULL))
278         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
279
280     _cairo_gl_surface_init (device, &surface->base,
281                             CAIRO_CONTENT_COLOR_ALPHA, width, height);
282     surface->egl = egl;
283
284     return &surface->base.base;
285 }