3 * @brief EGL video output module
5 /*****************************************************************************
6 * Copyright © 2010 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1
11 * of the License, or (at your option) any later version.
13 * This library 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 General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_display.h>
34 #include <vlc_vout_opengl.h>
37 # include <vlc_xlib.h>
41 # define VLC_API_NAME "OpenGL_ES"
42 # define VLC_API EGL_OPENGL_ES_API
43 # if USE_OPENGL_ES == 2
44 # define VLC_RENDERABLE_BIT EGL_OPENGL_ES2_BIT
46 # define VLC_RENDERABLE_BIT EGL_OPENGL_ES_BIT
49 # define VLC_API_NAME "OpenGL"
50 # define VLC_API EGL_OPENGL_API
51 # define VLC_RENDERABLE_BIT EGL_OPENGL_BIT
58 /* Plugin callbacks */
59 static int Open (vlc_object_t *);
60 static void Close (vlc_object_t *);
63 set_shortname (N_("EGL"))
64 set_description (N_("EGL video output"))
65 set_category (CAT_VIDEO)
66 set_subcategory (SUBCAT_VIDEO_VOUT)
67 set_capability ("vout display", 0)
68 set_callbacks (Open, Close)
71 struct vout_display_sys_t
77 vout_display_opengl_t vgl;
80 vout_window_t *window;
83 /* Display callbacks */
84 static picture_pool_t *Pool (vout_display_t *, unsigned);
85 static void PictureRender (vout_display_t *, picture_t *, subpicture_t *);
86 static void PictureDisplay (vout_display_t *, picture_t *, subpicture_t *);
87 static int Control (vout_display_t *, int, va_list);
88 /* OpenGL callbacks */
89 static void SwapBuffers (vout_opengl_t *gl);
91 static bool CheckAPI (EGLDisplay dpy, const char *api)
93 const char *apis = eglQueryString (dpy, EGL_CLIENT_APIS);
94 size_t apilen = strlen (api);
96 /* Cannot use strtok_r() on constant string... */
99 if (!strncmp (apis, api, apilen)
100 && (memchr (" ", apis[apilen], 2) != NULL))
103 apis = strchr (apis, ' ');
105 while (apis != NULL);
110 static vout_window_t *MakeWindow (vout_display_t *vd, EGLNativeWindowType *id)
112 vout_window_cfg_t wnd_cfg;
114 memset (&wnd_cfg, 0, sizeof (wnd_cfg));
116 wnd_cfg.type = VOUT_WINDOW_TYPE_HWND;
117 #elif defined (__unix__)
118 wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
120 # error Unknown native window type!
122 wnd_cfg.x = var_InheritInteger (vd, "video-x");
123 wnd_cfg.y = var_InheritInteger (vd, "video-y");
124 wnd_cfg.width = vd->cfg->display.width;
125 wnd_cfg.height = vd->cfg->display.height;
127 vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
130 *id = wnd->handle.hwnd;
131 #elif defined (__unix__)
132 *id = wnd->handle.xid;
135 msg_Err (vd, "parent window not available");
140 * Probe EGL display availability
142 static int Open (vlc_object_t *obj)
145 if (!vlc_xlib_init (obj))
148 vout_display_t *vd = (vout_display_t *)obj;
150 /* Initialize EGL display */
151 /* TODO: support various display types */
152 EGLDisplay dpy = eglGetDisplay (EGL_DEFAULT_DISPLAY);
153 if (dpy == EGL_NO_DISPLAY)
156 vout_display_sys_t *sys = malloc (sizeof (*sys));
157 if (unlikely(sys == NULL))
164 /* XXX Explicit hack!
165 * Mesa EGL plugins (as of version 7.8.2) are not properly linked to
166 * libEGL.so even though they import some of its symbols. This is
167 * typically not a problem. Unfortunately, LibVLC loads plugins as
168 * RTLD_LOCAL so that they do not pollute the namespace. Then the
169 * libEGL symbols are not visible to EGL plugins, and the run-time
170 * linker exits the whole process. */
172 dlopen ("libEGL.so", RTLD_GLOBAL|RTLD_LAZY);
176 if (eglInitialize (dpy, &major, &minor) != EGL_TRUE)
178 /* No need to call eglTerminate() in this case */
185 #if USE_OPENGL_ES == 2
186 if (minor < 3) /* Well well, this wouldn't compile with 1.2 anyway */
188 #elif USE_OPENGL_ES == 0
193 if (!CheckAPI (dpy, VLC_API_NAME))
196 msg_Dbg (obj, "EGL version %s by %s", eglQueryString (dpy, EGL_VERSION),
197 eglQueryString (dpy, EGL_VENDOR));
199 const char *ext = eglQueryString (dpy, EGL_EXTENSIONS);
201 msg_Dbg (obj, " extensions: %s", ext);
204 static const EGLint conf_attr[] = {
208 EGL_RENDERABLE_TYPE, VLC_RENDERABLE_BIT,
214 if (eglChooseConfig (dpy, conf_attr, cfgv, 1, &cfgc) != EGL_TRUE
218 /* Create a drawing surface */
219 EGLNativeWindowType win;
220 sys->window = MakeWindow (vd, &win);
221 if (sys->window == NULL)
224 EGLSurface surface = eglCreateWindowSurface (dpy, cfgv[0], win, NULL);
225 if (surface == EGL_NO_SURFACE)
227 msg_Err (obj, "cannot create EGL window surface");
230 sys->surface = surface;
232 if (eglBindAPI (VLC_API) != EGL_TRUE)
234 msg_Err (obj, "cannot bind EGL API");
238 static const EGLint ctx_attr[] = {
240 EGL_CONTEXT_CLIENT_VERSION, USE_OPENGL_ES,
245 EGLContext ctx = eglCreateContext (dpy, cfgv[0], EGL_NO_CONTEXT,
247 if (ctx == EGL_NO_CONTEXT)
249 msg_Err (obj, "cannot create EGL context");
253 if (eglMakeCurrent (dpy, surface, surface, ctx) != EGL_TRUE)
256 /* Initialize OpenGL callbacks */
258 sys->gl.unlock = NULL;
259 sys->gl.swap = SwapBuffers;
262 if (vout_display_opengl_Init (&sys->vgl, &vd->fmt, &sys->gl))
265 /* Initialize video display */
266 vd->info.has_pictures_invalid = false;
267 vd->info.has_event_thread = false;
269 vd->prepare = PictureRender;
270 vd->display = PictureDisplay;
271 vd->control = Control;
276 vout_display_DeleteWindow (vd, sys->window);
285 * Destrisconnect from the X server.
287 static void Close (vlc_object_t *obj)
289 vout_display_t *vd = (vout_display_t *)obj;
290 vout_display_sys_t *sys = vd->sys;
291 EGLDisplay dpy = sys->display;
293 if (sys->gl.sys != NULL)
294 vout_display_opengl_Clean (&sys->vgl);
296 vout_display_DeleteWindow (vd, sys->window);
300 static void SwapBuffers (vout_opengl_t *gl)
302 vout_display_sys_t *sys = gl->sys;
304 eglSwapBuffers (sys->display, sys->surface);
308 * Return a direct buffer
310 static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
312 vout_display_sys_t *sys = vd->sys;
315 sys->pool = vout_display_opengl_GetPool (&sys->vgl);
320 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
322 vout_display_sys_t *sys = vd->sys;
324 vout_display_opengl_Prepare (&sys->vgl, pic);
328 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
330 vout_display_sys_t *sys = vd->sys;
332 vout_display_opengl_Display (&sys->vgl, &vd->source);
333 picture_Release (pic);
337 static int Control (vout_display_t *vd, int query, va_list ap)
339 vout_display_sys_t *sys = vd->sys;
343 case VOUT_DISPLAY_HIDE_MOUSE:
344 case VOUT_DISPLAY_RESET_PICTURES: // not needed?
347 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
349 const vout_display_cfg_t *cfg =
350 va_arg (ap, const vout_display_cfg_t *);
352 return vout_window_SetFullScreen (sys->window, cfg->is_fullscreen);
355 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
357 unsigned state = va_arg (ap, unsigned);
359 return vout_window_SetState (sys->window, state);
362 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
363 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
364 case VOUT_DISPLAY_CHANGE_ZOOM:
366 const vout_display_cfg_t *cfg = va_arg (ap, const vout_display_cfg_t *);
367 const video_format_t *src = &vd->source;
369 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
373 force = va_arg (ap, int);
375 && (cfg->display.width != vd->cfg->display.width
376 || cfg->display.height != vd->cfg->display.height)
377 && vout_window_SetSize (sys->window,
378 cfg->display.width, cfg->display.height))
382 vout_display_place_t place;
384 vout_display_PlacePicture (&place, src, cfg, false);
385 glViewport (0, 0, place.width, place.height);
389 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
390 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
392 const vout_display_cfg_t *cfg = vd->cfg;
393 const video_format_t *src = va_arg (ap, const video_format_t *);
394 vout_display_place_t place;
396 vout_display_PlacePicture (&place, src, cfg, false);
397 glViewport (0, 0, place.width, place.height);
401 case VOUT_DISPLAY_GET_OPENGL:
403 vout_opengl_t **gl = va_arg (ap, vout_opengl_t **);
410 msg_Err (vd, "Unknown request %d", query);