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 *);
86 static void PictureDisplay (vout_display_t *, picture_t *);
87 static int Control (vout_display_t *, int, va_list);
88 static void Manage (vout_display_t *);
89 /* OpenGL callbacks */
90 static void SwapBuffers (vout_opengl_t *gl);
92 static bool CheckAPI (EGLDisplay dpy, const char *api)
94 const char *apis = eglQueryString (dpy, EGL_CLIENT_APIS);
95 size_t apilen = strlen (api);
97 /* Cannot use strtok_r() on constant string... */
100 if (!strncmp (apis, api, apilen)
101 && (memchr (" ", apis[apilen], 2) != NULL))
104 apis = strchr (apis, ' ');
106 while (apis != NULL);
111 static vout_window_t *MakeWindow (vout_display_t *vd, EGLNativeWindowType *id)
113 vout_window_cfg_t wnd_cfg;
115 memset (&wnd_cfg, 0, sizeof (wnd_cfg));
117 wnd_cfg.type = VOUT_WINDOW_TYPE_HWND;
118 #elif defined (__unix__)
119 wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
121 # error Unknown native window type!
123 wnd_cfg.x = var_InheritInteger (vd, "video-x");
124 wnd_cfg.y = var_InheritInteger (vd, "video-y");
125 wnd_cfg.width = vd->cfg->display.width;
126 wnd_cfg.height = vd->cfg->display.height;
128 vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
131 *id = wnd->handle.hwnd;
132 #elif defined (__unix__)
133 *id = wnd->handle.xid;
136 msg_Err (vd, "parent window not available");
141 * Probe EGL display availability
143 static int Open (vlc_object_t *obj)
146 if (!vlc_xlib_init (obj))
149 vout_display_t *vd = (vout_display_t *)obj;
151 /* Initialize EGL display */
152 /* TODO: support various display types */
153 EGLDisplay dpy = eglGetDisplay (EGL_DEFAULT_DISPLAY);
154 if (dpy == EGL_NO_DISPLAY)
157 vout_display_sys_t *sys = malloc (sizeof (*sys));
158 if (unlikely(sys == NULL))
165 /* XXX Explicit hack!
166 * Mesa EGL plugins (as of version 7.8.2) are not properly linked to
167 * libEGL.so even though they import some of its symbols. This is
168 * typically not a problem. Unfortunately, LibVLC loads plugins as
169 * RTLD_LOCAL so that they do not pollute the namespace. Then the
170 * libEGL symbols are not visible to EGL plugins, and the run-time
171 * linker exits the whole process. */
173 dlopen ("libEGL.so", RTLD_GLOBAL|RTLD_LAZY);
177 if (eglInitialize (dpy, &major, &minor) != EGL_TRUE)
179 /* No need to call eglTerminate() in this case */
186 #if USE_OPENGL_ES == 2
187 if (minor < 3) /* Well well, this wouldn't compile with 1.2 anyway */
189 #elif USE_OPENGL_ES == 0
194 if (!CheckAPI (dpy, VLC_API_NAME))
197 msg_Dbg (obj, "EGL version %s by %s", eglQueryString (dpy, EGL_VERSION),
198 eglQueryString (dpy, EGL_VENDOR));
200 const char *ext = eglQueryString (dpy, EGL_EXTENSIONS);
202 msg_Dbg (obj, " extensions: %s", ext);
205 static const EGLint conf_attr[] = {
209 EGL_RENDERABLE_TYPE, VLC_RENDERABLE_BIT,
215 if (eglChooseConfig (dpy, conf_attr, cfgv, 1, &cfgc) != EGL_TRUE
219 /* Create a drawing surface */
220 EGLNativeWindowType win;
221 sys->window = MakeWindow (vd, &win);
222 if (sys->window == NULL)
225 EGLSurface surface = eglCreateWindowSurface (dpy, cfgv[0], win, NULL);
226 if (surface == EGL_NO_SURFACE)
228 msg_Err (obj, "cannot create EGL window surface");
231 sys->surface = surface;
233 if (eglBindAPI (VLC_API) != EGL_TRUE)
235 msg_Err (obj, "cannot bind EGL API");
239 static const EGLint ctx_attr[] = {
241 EGL_CONTEXT_CLIENT_VERSION, USE_OPENGL_ES,
246 EGLContext ctx = eglCreateContext (dpy, cfgv[0], EGL_NO_CONTEXT,
248 if (ctx == EGL_NO_CONTEXT)
250 msg_Err (obj, "cannot create EGL context");
254 if (eglMakeCurrent (dpy, surface, surface, ctx) != EGL_TRUE)
257 /* Initialize OpenGL callbacks */
259 sys->gl.unlock = NULL;
260 sys->gl.swap = SwapBuffers;
263 if (vout_display_opengl_Init (&sys->vgl, &vd->fmt, &sys->gl))
266 /* Initialize video display */
267 vd->info.has_pictures_invalid = false;
268 vd->info.has_event_thread = false;
270 vd->prepare = PictureRender;
271 vd->display = PictureDisplay;
272 vd->control = Control;
277 vout_display_DeleteWindow (vd, sys->window);
286 * Destrisconnect from the X server.
288 static void Close (vlc_object_t *obj)
290 vout_display_t *vd = (vout_display_t *)obj;
291 vout_display_sys_t *sys = vd->sys;
292 EGLDisplay dpy = sys->display;
294 if (sys->gl.sys != NULL)
295 vout_display_opengl_Clean (&sys->vgl);
297 vout_display_DeleteWindow (vd, sys->window);
301 static void SwapBuffers (vout_opengl_t *gl)
303 vout_display_sys_t *sys = gl->sys;
305 eglSwapBuffers (sys->display, sys->surface);
309 * Return a direct buffer
311 static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
313 vout_display_sys_t *sys = vd->sys;
316 sys->pool = vout_display_opengl_GetPool (&sys->vgl);
321 static void PictureRender (vout_display_t *vd, picture_t *pic)
323 vout_display_sys_t *sys = vd->sys;
325 vout_display_opengl_Prepare (&sys->vgl, pic);
328 static void PictureDisplay (vout_display_t *vd, picture_t *pic)
330 vout_display_sys_t *sys = vd->sys;
332 vout_display_opengl_Display (&sys->vgl, &vd->source);
333 picture_Release (pic);
336 static int Control (vout_display_t *vd, int query, va_list ap)
338 vout_display_sys_t *sys = vd->sys;
342 case VOUT_DISPLAY_HIDE_MOUSE:
343 case VOUT_DISPLAY_RESET_PICTURES: // not needed?
346 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
348 const vout_display_cfg_t *cfg =
349 va_arg (ap, const vout_display_cfg_t *);
351 return vout_window_SetFullScreen (sys->window, cfg->is_fullscreen);
354 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
356 unsigned state = va_arg (ap, unsigned);
358 return vout_window_SetState (sys->window, state);
361 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
362 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
363 case VOUT_DISPLAY_CHANGE_ZOOM:
365 const vout_display_cfg_t *cfg = va_arg (ap, const vout_display_cfg_t *);
366 const video_format_t *src = &vd->source;
368 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
372 force = va_arg (ap, int);
374 && (cfg->display.width != vd->cfg->display.width
375 || cfg->display.height != vd->cfg->display.height)
376 && vout_window_SetSize (sys->window,
377 cfg->display.width, cfg->display.height))
381 vout_display_place_t place;
383 vout_display_PlacePicture (&place, src, cfg, false);
384 glViewport (0, 0, place.width, place.height);
388 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
389 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
391 const vout_display_cfg_t *cfg = vd->cfg;
392 const video_format_t *src = va_arg (ap, const video_format_t *);
393 vout_display_place_t place;
395 vout_display_PlacePicture (&place, src, cfg, false);
396 glViewport (0, 0, place.width, place.height);
400 case VOUT_DISPLAY_GET_OPENGL:
402 vout_opengl_t **gl = va_arg (ap, vout_opengl_t **);
409 msg_Err (vd, "Unknown request %d", query);
414 static void Manage (vout_display_t *vd)
416 //vout_display_sys_t *sys = vd->sys;