3 * @brief EGL OpenGL extension module
5 /*****************************************************************************
6 * Copyright © 2010-2011 Rémi Denis-Courmont
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_opengl.h>
34 #include <vlc_vout_window.h>
36 # include <vlc_xlib.h>
39 /* Plugin callbacks */
40 static int OpenGLES2 (vlc_object_t *);
41 static int OpenGLES (vlc_object_t *);
42 static int OpenGL (vlc_object_t *);
43 static void Close (vlc_object_t *);
46 set_shortname (N_("EGL"))
47 set_description (N_("EGL extension for OpenGL"))
48 set_category (CAT_VIDEO)
49 set_subcategory (SUBCAT_VIDEO_VOUT)
50 set_capability ("opengl", 50)
51 set_callbacks (OpenGL, Close)
54 set_capability ("opengl es2", 50)
55 set_callbacks (OpenGLES2, Close)
58 set_capability ("opengl es", 50)
59 set_callbacks (OpenGLES, Close)
63 typedef struct vlc_gl_sys_t
70 /* OpenGL callbacks */
71 static int MakeCurrent (vlc_gl_t *);
72 static void SwapBuffers (vlc_gl_t *);
73 static void *GetSymbol(vlc_gl_t *, const char *);
75 static bool CheckAPI (EGLDisplay dpy, const char *api)
77 const char *apis = eglQueryString (dpy, EGL_CLIENT_APIS);
78 size_t apilen = strlen (api);
80 /* Cannot use strtok_r() on constant string... */
85 if (!strncmp (apis, api, apilen)
86 && (memchr (" ", apis[apilen], 2) != NULL))
89 apis = strchr (apis, ' ');
106 * Probe EGL display availability
108 static int Open (vlc_object_t *obj, const struct gl_api *api)
110 vlc_gl_t *gl = (vlc_gl_t *)obj;
113 /* EGL can only use the default X11 display */
114 if (gl->surface->display.x11 != NULL)
116 if (!vlc_xlib_init (obj))
120 /* Initialize EGL display */
121 /* TODO: support various display types */
122 EGLDisplay dpy = eglGetDisplay (EGL_DEFAULT_DISPLAY);
123 if (dpy == EGL_NO_DISPLAY)
126 vlc_gl_sys_t *sys = malloc (sizeof (*sys));
127 if (unlikely(sys == NULL))
133 if (eglInitialize (dpy, &major, &minor) != EGL_TRUE)
135 /* No need to call eglTerminate() in this case */
140 if (major != 1 || minor < api->min_minor || !CheckAPI (dpy, api->name))
143 msg_Dbg (obj, "EGL version %s by %s", eglQueryString (dpy, EGL_VERSION),
144 eglQueryString (dpy, EGL_VENDOR));
146 const char *ext = eglQueryString (dpy, EGL_EXTENSIONS);
148 msg_Dbg (obj, " extensions: %s", ext);
151 const EGLint conf_attr[] = {
155 EGL_RENDERABLE_TYPE, api->render_bit,
161 if (eglChooseConfig (dpy, conf_attr, cfgv, 1, &cfgc) != EGL_TRUE
165 /* Create a drawing surface */
167 EGLNativeWindowType win = gl->surface->handle.hwnd;
168 #elif defined (__unix__)
169 EGLNativeWindowType win = gl->surface->handle.xid;
172 EGLSurface surface = eglCreateWindowSurface (dpy, cfgv[0], win, NULL);
173 if (surface == EGL_NO_SURFACE)
175 msg_Err (obj, "cannot create EGL window surface");
178 sys->surface = surface;
180 if (eglBindAPI (api->api) != EGL_TRUE)
182 msg_Err (obj, "cannot bind EGL API");
186 EGLContext ctx = eglCreateContext (dpy, cfgv[0], EGL_NO_CONTEXT,
188 if (ctx == EGL_NO_CONTEXT)
190 msg_Err (obj, "cannot create EGL context");
195 /* Initialize OpenGL callbacks */
197 gl->makeCurrent = MakeCurrent;
198 gl->swap = SwapBuffers;
199 gl->getProcAddress = GetSymbol;
209 static int OpenGLES2 (vlc_object_t *obj)
211 static const struct gl_api api = {
212 "OpenGL_ES", EGL_OPENGL_ES_API, 3, EGL_OPENGL_ES2_BIT,
213 { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE },
215 return Open (obj, &api);
218 static int OpenGLES (vlc_object_t *obj)
220 static const struct gl_api api = {
221 "OpenGL_ES", EGL_OPENGL_ES_API, 0, EGL_OPENGL_ES_BIT,
222 { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE },
224 return Open (obj, &api);
227 static int OpenGL (vlc_object_t *obj)
229 static const struct gl_api api = {
230 "OpenGL", EGL_OPENGL_API, 4, EGL_OPENGL_BIT,
233 return Open (obj, &api);
236 static void Close (vlc_object_t *obj)
238 vlc_gl_t *gl = (vlc_gl_t *)obj;
239 vlc_gl_sys_t *sys = gl->sys;
241 eglTerminate (sys->display);
245 static int MakeCurrent (vlc_gl_t *gl)
247 vlc_gl_sys_t *sys = gl->sys;
249 if (eglMakeCurrent (sys->display, sys->surface, sys->surface,
250 sys->context) != EGL_TRUE)
255 static void SwapBuffers (vlc_gl_t *gl)
257 vlc_gl_sys_t *sys = gl->sys;
259 eglSwapBuffers (sys->display, sys->surface);
262 static void *GetSymbol(vlc_gl_t *gl, const char *procname)
265 return (void *)eglGetProcAddress (procname);