* @brief EGL OpenGL extension module
*/
/*****************************************************************************
- * Copyright © 2010-2011 Rémi Denis-Courmont
+ * Copyright © 2010-2014 Rémi Denis-Courmont
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
#include <stdlib.h>
#include <assert.h>
#include <EGL/egl.h>
+#include <EGL/eglext.h>
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_opengl.h>
#include <vlc_vout_window.h>
-#ifdef __unix__
+#ifdef USE_PLATFORM_X11
# include <vlc_xlib.h>
#endif
-
-/* Plugin callbacks */
-static int OpenGLES2 (vlc_object_t *);
-static int OpenGLES (vlc_object_t *);
-static int OpenGL (vlc_object_t *);
-static void Close (vlc_object_t *);
-
-vlc_module_begin ()
- set_shortname (N_("EGL"))
- set_description (N_("EGL extension for OpenGL"))
- set_category (CAT_VIDEO)
- set_subcategory (SUBCAT_VIDEO_VOUT)
- set_capability ("opengl", 50)
- set_callbacks (OpenGL, Close)
-
- add_submodule ()
- set_capability ("opengl es2", 50)
- set_callbacks (OpenGLES2, Close)
-
- add_submodule ()
- set_capability ("opengl es", 50)
- set_callbacks (OpenGLES, Close)
-
-vlc_module_end ()
+#ifdef USE_PLATFORM_WAYLAND
+# include <wayland-egl.h>
+#endif
typedef struct vlc_gl_sys_t
{
EGLDisplay display;
EGLSurface surface;
EGLContext context;
+#if defined (USE_PLATFORM_X11)
+ Display *x11;
+#endif
+#if defined (USE_PLATFORM_WAYLAND)
+ struct wl_egl_window *window;
+ unsigned width, height;
+#endif
} vlc_gl_sys_t;
-/* OpenGL callbacks */
-static int MakeCurrent (vlc_gl_t *);
-static void SwapBuffers (vlc_gl_t *);
-static void *GetSymbol(vlc_gl_t *, const char *);
+static int MakeCurrent (vlc_gl_t *gl)
+{
+ vlc_gl_sys_t *sys = gl->sys;
-static bool CheckAPI (EGLDisplay dpy, const char *api)
+ if (eglMakeCurrent (sys->display, sys->surface, sys->surface,
+ sys->context) != EGL_TRUE)
+ return VLC_EGENERIC;
+ return VLC_SUCCESS;
+}
+
+static void ReleaseCurrent (vlc_gl_t *gl)
{
- const char *apis = eglQueryString (dpy, EGL_CLIENT_APIS);
- size_t apilen = strlen (api);
+ vlc_gl_sys_t *sys = gl->sys;
- /* Cannot use strtok_r() on constant string... */
- do
+ eglMakeCurrent (sys->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
+ EGL_NO_CONTEXT);
+}
+
+#ifdef USE_PLATFORM_WAYLAND
+static void Resize (vlc_gl_t *gl, unsigned width, unsigned height)
+{
+ vlc_gl_sys_t *sys = gl->sys;
+
+ wl_egl_window_resize(sys->window, width, height,
+ (sys->width - width) / 2,
+ (sys->height - height) / 2);
+ sys->width = width;
+ sys->height = height;
+}
+#else
+# define Resize (NULL)
+#endif
+
+static void SwapBuffers (vlc_gl_t *gl)
+{
+ vlc_gl_sys_t *sys = gl->sys;
+
+ eglSwapBuffers (sys->display, sys->surface);
+}
+
+static void *GetSymbol(vlc_gl_t *gl, const char *procname)
+{
+ (void) gl;
+ return (void *)eglGetProcAddress (procname);
+}
+
+static bool CheckToken(const char *haystack, const char *needle)
+{
+ size_t len = strlen(needle);
+
+ while (haystack != NULL)
{
- while (*apis == ' ')
- apis++;
- if (!strncmp (apis, api, apilen)
- && (memchr (" ", apis[apilen], 2) != NULL))
+ while (*haystack == ' ')
+ haystack++;
+ if (!strncmp(haystack, needle, len)
+ && (memchr(" ", haystack[len], 2) != NULL))
return true;
- apis = strchr (apis, ' ');
+ haystack = strchr(haystack, ' ');
}
- while (apis != NULL);
-
return false;
}
+static bool CheckAPI (EGLDisplay dpy, const char *api)
+{
+ const char *apis = eglQueryString (dpy, EGL_CLIENT_APIS);
+ return CheckToken(apis, api);
+}
+
+static bool CheckClientExt(const char *name)
+{
+ const char *exts = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
+ return CheckToken(exts, name);
+}
+
struct gl_api
{
const char name[10];
EGLint attr[3];
};
-/**
- * Probe EGL display availability
- */
-static int Open (vlc_object_t *obj, const struct gl_api *api)
+#ifdef EGL_EXT_platform_base
+static EGLDisplay GetDisplayEXT(EGLenum plat, void *dpy, const EGLint *attrs)
{
- vlc_gl_t *gl = (vlc_gl_t *)obj;
+ PFNEGLGETPLATFORMDISPLAYEXTPROC getDisplay =
+ (PFNEGLGETPLATFORMDISPLAYEXTPROC)
+ eglGetProcAddress("eglGetPlatformDisplayEXT");
- /* <EGL/eglplatform.h> defines the list and order of platforms */
-#if defined(_WIN32) || defined(__VC32__) \
- && !defined(__CYGWIN__) && !defined(__SCITECH_SNAP__)
-# define vlc_eglGetWindow(w) ((w)->handle.hwnd)
+ assert(getDisplay != NULL);
+ return getDisplay(plat, dpy, attrs);
+}
-#elif defined(__WINSCW__) || defined(__SYMBIAN32__) /* Symbian */
-# error Symbian EGL not supported.
+static EGLSurface CreateWindowSurfaceEXT(EGLDisplay dpy, EGLConfig config,
+ void *window, const EGLint *attrs)
+{
+ PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC createSurface =
+ (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)
+ eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
-#elif defined(WL_EGL_PLATFORM)
-# error Wayland EGL not supported.
+ assert(createSurface != NULL);
+ return createSurface(dpy, config, window, attrs);
+}
+#endif
-#elif defined(__GBM__)
-# error Glamor EGL not supported.
+static EGLSurface CreateWindowSurface(EGLDisplay dpy, EGLConfig config,
+ void *window, const EGLint *attrs)
+{
+ EGLNativeWindowType *native = window;
-#elif defined(ANDROID)
-# error Android EGL not supported
+ return eglCreateWindowSurface(dpy, config, *native, attrs);
+}
-#elif defined(__unix__) /* X11 */
-# define vlc_eglGetWindow(w) ((w)->handle.xid)
- /* EGL can only use the default X11 display */
- if (gl->surface->display.x11 != NULL)
- return VLC_EGENERIC;
- if (!vlc_xlib_init (obj))
- return VLC_EGENERIC;
+static void Close (vlc_object_t *obj)
+{
+ vlc_gl_t *gl = (vlc_gl_t *)obj;
+ vlc_gl_sys_t *sys = gl->sys;
-#else
-# error EGL platform not recognized.
+ if (sys->display != EGL_NO_DISPLAY)
+ {
+ if (sys->surface != EGL_NO_SURFACE)
+ eglDestroySurface(sys->display, sys->surface);
+ eglTerminate(sys->display);
+ }
+#ifdef USE_PLATFORM_X11
+ if (sys->x11 != NULL)
+ XCloseDisplay(sys->x11);
#endif
+#ifdef USE_PLATFORM_WAYLAND
+ if (sys->window != NULL)
+ wl_egl_window_destroy(sys->window);
+#endif
+ free (sys);
+}
- /* Initialize EGL display */
- /* TODO: support various display types */
- EGLDisplay dpy = eglGetDisplay (EGL_DEFAULT_DISPLAY);
- if (dpy == EGL_NO_DISPLAY)
- return VLC_EGENERIC;
-
- vlc_gl_sys_t *sys = malloc (sizeof (*sys));
+/**
+ * Probe EGL display availability
+ */
+static int Open (vlc_object_t *obj, const struct gl_api *api)
+{
+ vlc_gl_t *gl = (vlc_gl_t *)obj;
+ vlc_gl_sys_t *sys = malloc(sizeof (*sys));
if (unlikely(sys == NULL))
return VLC_ENOMEM;
+
gl->sys = sys;
- sys->display = dpy;
+ sys->display = EGL_NO_DISPLAY;
+ sys->surface = EGL_NO_SURFACE;
- EGLint major, minor;
- if (eglInitialize (dpy, &major, &minor) != EGL_TRUE)
+ vout_window_t *wnd = gl->surface;
+ EGLSurface (*createSurface)(EGLDisplay, EGLConfig, void *, const EGLint *)
+ = CreateWindowSurface;
+ void *window;
+
+#ifdef USE_PLATFORM_X11
+ sys->x11 = NULL;
+
+ if (wnd->type != VOUT_WINDOW_TYPE_XID || !vlc_xlib_init(obj))
+ goto error;
+
+ window = &wnd->handle.xid;
+ sys->x11 = XOpenDisplay(wnd->display.x11);
+ if (sys->x11 == NULL)
+ goto error;
+
+ int snum;
{
- /* No need to call eglTerminate() in this case */
- free (sys);
- return VLC_EGENERIC;
+ XWindowAttributes wa;
+
+ if (!XGetWindowAttributes(sys->x11, wnd->handle.xid, &wa))
+ goto error;
+ snum = XScreenNumberOfScreen(wa.screen);
}
+# ifdef EGL_EXT_platform_x11
+ if (CheckClientExt("EGL_EXT_platform_x11"))
+ {
+ const EGLint attrs[] = {
+ EGL_PLATFORM_X11_SCREEN_EXT, snum,
+ EGL_NONE
+ };
+ sys->display = GetDisplayEXT(EGL_PLATFORM_X11_EXT, sys->x11, attrs);
+ createSurface = CreateWindowSurfaceEXT;
+ }
+ else
+# endif
+ {
+# ifdef __unix__
+ if (snum == XDefaultScreen(sys->x11))
+ sys->display = eglGetDisplay(sys->x11);
+ }
+# endif
+
+#elif defined (USE_PLATFORM_WAYLAND)
+ sys->window = NULL;
+
+ if (wnd->type != VOUT_WINDOW_TYPE_WAYLAND)
+ goto error;
+
+# ifdef EGL_EXT_platform_wayland
+ if (!CheckClientExt("EGL_EXT_platform_wayland"))
+ goto error;
+
+ /* Resize() should be called with the proper size before Swap() */
+ window = wl_egl_window_create(wnd->handle.wl, 1, 1);
+ if (window == NULL)
+ goto error;
+ sys->window = window;
+
+ sys->display = GetDisplayEXT(EGL_PLATFORM_WAYLAND_EXT, wnd->display.wl,
+ NULL);
+ createSurface = CreateWindowSurfaceEXT;
+
+# endif
+
+#elif defined (USE_PLATFORM_WIN32)
+ if (wnd->type != VOUT_WINDOW_TYPE_HWND)
+ goto error;
+
+ window = &wnd->handle.hwnd;
+# if defined (_WIN32) || defined (__VC32__) \
+ && !defined (__CYGWIN__) && !defined (__SCITECH_SNAP__)
+ sys->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+# endif
+
+#elif defined (USE_PLATFORM_ANDROID)
+ if (wnd->type != VOUT_WINDOW_TYPE_ANDROID_NATIVE)
+ goto error;
+
+ window = &wnd->handle.anativewindow;
+# if defined (__ANDROID__) || defined (ANDROID)
+ sys->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+# endif
- if (major != 1 || minor < api->min_minor || !CheckAPI (dpy, api->name))
+#endif
+
+ if (sys->display == EGL_NO_DISPLAY)
+ goto error;
+
+ /* Initialize EGL display */
+ EGLint major, minor;
+ if (eglInitialize(sys->display, &major, &minor) != EGL_TRUE)
goto error;
+ msg_Dbg(obj, "EGL version %s by %s",
+ eglQueryString(sys->display, EGL_VERSION),
+ eglQueryString(sys->display, EGL_VENDOR));
- msg_Dbg (obj, "EGL version %s by %s", eglQueryString (dpy, EGL_VERSION),
- eglQueryString (dpy, EGL_VENDOR));
+ const char *ext = eglQueryString(sys->display, EGL_EXTENSIONS);
+ if (*ext)
+ msg_Dbg(obj, " extensions: %s", ext);
+
+ if (major != 1 || minor < api->min_minor
+ || !CheckAPI(sys->display, api->name))
{
- const char *ext = eglQueryString (dpy, EGL_EXTENSIONS);
- if (*ext)
- msg_Dbg (obj, " extensions: %s", ext);
+ msg_Err(obj, "cannot select %s API", api->name);
+ goto error;
}
const EGLint conf_attr[] = {
EGLConfig cfgv[1];
EGLint cfgc;
- if (eglChooseConfig (dpy, conf_attr, cfgv, 1, &cfgc) != EGL_TRUE
+ if (eglChooseConfig(sys->display, conf_attr, cfgv, 1, &cfgc) != EGL_TRUE
|| cfgc == 0)
{
msg_Err (obj, "cannot choose EGL configuration");
}
/* Create a drawing surface */
- EGLNativeWindowType win = vlc_eglGetWindow(gl->surface);
- EGLSurface surface = eglCreateWindowSurface (dpy, cfgv[0], win, NULL);
- if (surface == EGL_NO_SURFACE)
+ sys->surface = createSurface(sys->display, cfgv[0], window, NULL);
+ if (sys->surface == EGL_NO_SURFACE)
{
msg_Err (obj, "cannot create EGL window surface");
goto error;
}
- sys->surface = surface;
if (eglBindAPI (api->api) != EGL_TRUE)
{
goto error;
}
- EGLContext ctx = eglCreateContext (dpy, cfgv[0], EGL_NO_CONTEXT,
- api->attr);
+ EGLContext ctx = eglCreateContext(sys->display, cfgv[0], EGL_NO_CONTEXT,
+ api->attr);
if (ctx == EGL_NO_CONTEXT)
{
msg_Err (obj, "cannot create EGL context");
sys->context = ctx;
/* Initialize OpenGL callbacks */
- gl->sys = sys;
gl->makeCurrent = MakeCurrent;
+ gl->releaseCurrent = ReleaseCurrent;
+ gl->resize = Resize;
gl->swap = SwapBuffers;
gl->getProcAddress = GetSymbol;
- gl->lock = NULL;
- gl->unlock = NULL;
return VLC_SUCCESS;
error:
return Open (obj, &api);
}
-static void Close (vlc_object_t *obj)
-{
- vlc_gl_t *gl = (vlc_gl_t *)obj;
- vlc_gl_sys_t *sys = gl->sys;
-
- eglTerminate (sys->display);
- free (sys);
-}
-
-static int MakeCurrent (vlc_gl_t *gl)
-{
- vlc_gl_sys_t *sys = gl->sys;
-
- if (eglMakeCurrent (sys->display, sys->surface, sys->surface,
- sys->context) != EGL_TRUE)
- return VLC_EGENERIC;
- return VLC_SUCCESS;
-}
+vlc_module_begin ()
+ set_shortname (N_("EGL"))
+ set_description (N_("EGL extension for OpenGL"))
+ set_category (CAT_VIDEO)
+ set_subcategory (SUBCAT_VIDEO_VOUT)
+ set_capability ("opengl", 50)
+ set_callbacks (OpenGL, Close)
+ add_shortcut ("egl")
-static void SwapBuffers (vlc_gl_t *gl)
-{
- vlc_gl_sys_t *sys = gl->sys;
+ add_submodule ()
+ set_capability ("opengl es2", 50)
+ set_callbacks (OpenGLES2, Close)
+ add_shortcut ("egl")
- eglSwapBuffers (sys->display, sys->surface);
-}
+ add_submodule ()
+ set_capability ("opengl es", 50)
+ set_callbacks (OpenGLES, Close)
+ add_shortcut ("egl")
-static void *GetSymbol(vlc_gl_t *gl, const char *procname)
-{
- (void) gl;
- return (void *)eglGetProcAddress (procname);
-}
+vlc_module_end ()