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>
38 # define VLC_API_NAME "OpenGL_ES"
39 # define VLC_API EGL_OPENGL_ES_API
40 # if USE_OPENGL_ES == 2
41 # define VLC_RENDERABLE_BIT EGL_OPENGL_ES2_BIT
43 # define VLC_RENDERABLE_BIT EGL_OPENGL_ES_BIT
46 # define VLC_API_NAME "OpenGL"
47 # define VLC_API EGL_OPENGL_API
48 # define VLC_RENDERABLE_BIT EGL_OPENGL_BIT
51 /* Plugin callbacks */
52 static int Open (vlc_object_t *);
53 static void Close (vlc_object_t *);
56 set_shortname (N_("EGL"))
57 set_description (N_("EGL video output"))
58 set_category (CAT_VIDEO)
59 set_subcategory (SUBCAT_VIDEO_VOUT)
60 set_capability ("vout display", 0)
61 set_callbacks (Open, Close)
64 struct vout_display_sys_t
70 vout_display_opengl_t vgl;
73 vout_window_t *window;
76 /* Display callbacks */
77 static picture_pool_t *Pool (vout_display_t *, unsigned);
78 static void PictureRender (vout_display_t *, picture_t *);
79 static void PictureDisplay (vout_display_t *, picture_t *);
80 static int Control (vout_display_t *, int, va_list);
81 static void Manage (vout_display_t *);
82 /* OpenGL callbacks */
83 static void SwapBuffers (vout_opengl_t *gl);
85 static bool CheckAPI (EGLDisplay dpy, const char *api)
87 const char *apis = eglQueryString (dpy, EGL_CLIENT_APIS);
88 size_t apilen = strlen (api);
90 /* Cannot use strtok_r() on constant string... */
93 if (!strncmp (apis, api, apilen)
94 && (memchr (" ", apis[apilen], 2) != NULL))
97 apis = strchr (apis, ' ');
104 static vout_window_t *MakeWindow (vout_display_t *vd, EGLNativeWindowType *id)
106 vout_window_cfg_t wnd_cfg;
108 memset (&wnd_cfg, 0, sizeof (wnd_cfg));
110 wnd_cfg.type = VOUT_WINDOW_TYPE_HWND;
111 #elif defined (__unix__)
112 wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
114 # error Unknown native window type!
116 wnd_cfg.x = var_InheritInteger (vd, "video-x");
117 wnd_cfg.y = var_InheritInteger (vd, "video-y");
118 wnd_cfg.width = vd->cfg->display.width;
119 wnd_cfg.height = vd->cfg->display.height;
121 vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
124 *id = wnd->handle.hwnd;
125 #elif defined (__unix__)
126 *id = wnd->handle.xid;
129 msg_Err (vd, "parent window not available");
134 * Probe EGL display availability
136 static int Open (vlc_object_t *obj)
138 vout_display_t *vd = (vout_display_t *)obj;
140 /* Initialize EGL display */
141 /* TODO: support various display types */
142 EGLDisplay dpy = eglGetDisplay (EGL_DEFAULT_DISPLAY);
143 if (dpy == EGL_NO_DISPLAY)
146 vout_display_sys_t *sys = malloc (sizeof (*sys));
147 if (unlikely(sys == NULL))
155 if (eglInitialize (dpy, &major, &minor) != EGL_TRUE)
157 /* No need to call eglTerminate() in this case */
164 #if USE_OPENGL_ES == 2
165 if (minor < 3) /* Well well, this wouldn't compile with 1.2 anyway */
167 #elif USE_OPENGL_ES == 0
172 if (!CheckAPI (dpy, VLC_API_NAME))
175 msg_Dbg (obj, "EGL version %s by %s", eglQueryString (dpy, EGL_VERSION),
176 eglQueryString (dpy, EGL_VENDOR));
178 const char *ext = eglQueryString (dpy, EGL_EXTENSIONS);
180 msg_Dbg (obj, " extensions: %s", ext);
183 static const EGLint conf_attr[] = {
187 EGL_RENDERABLE_TYPE, VLC_RENDERABLE_BIT,
193 if (eglChooseConfig (dpy, conf_attr, cfgv, 1, &cfgc) != EGL_TRUE
197 /* Create a drawing surface */
198 EGLNativeWindowType win;
199 sys->window = MakeWindow (vd, &win);
200 if (sys->window == NULL)
203 EGLSurface surface = eglCreateWindowSurface (dpy, cfgv[0], win, NULL);
204 if (surface == EGL_NO_SURFACE)
206 msg_Err (obj, "cannot create EGL window surface");
209 sys->surface = surface;
211 if (eglBindAPI (VLC_API) != EGL_TRUE)
213 msg_Err (obj, "cannot bind EGL API");
217 static const EGLint ctx_attr[] = {
219 EGL_CONTEXT_CLIENT_VERSION, USE_OPENGL_ES,
224 EGLContext ctx = eglCreateContext (dpy, cfgv[0], EGL_NO_CONTEXT,
226 if (ctx == EGL_NO_CONTEXT)
228 msg_Err (obj, "cannot create EGL context");
232 if (eglMakeCurrent (dpy, surface, surface, ctx) != EGL_TRUE)
235 /* Initialize OpenGL callbacks */
237 sys->gl.unlock = NULL;
238 sys->gl.swap = SwapBuffers;
241 if (vout_display_opengl_Init (&sys->vgl, &vd->fmt, &sys->gl))
244 /* Initialize video display */
245 vd->info.has_pictures_invalid = false;
246 vd->info.has_event_thread = false;
248 vd->prepare = PictureRender;
249 vd->display = PictureDisplay;
250 vd->control = Control;
255 vout_display_DeleteWindow (vd, sys->window);
264 * Destrisconnect from the X server.
266 static void Close (vlc_object_t *obj)
268 vout_display_t *vd = (vout_display_t *)obj;
269 vout_display_sys_t *sys = vd->sys;
270 EGLDisplay dpy = sys->display;
272 if (sys->gl.sys != NULL)
273 vout_display_opengl_Clean (&sys->vgl);
275 vout_display_DeleteWindow (vd, sys->window);
279 static void SwapBuffers (vout_opengl_t *gl)
281 vout_display_sys_t *sys = gl->sys;
283 eglSwapBuffers (sys->display, sys->surface);
287 * Return a direct buffer
289 static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
291 vout_display_sys_t *sys = vd->sys;
294 sys->pool = vout_display_opengl_GetPool (&sys->vgl);
299 static void PictureRender (vout_display_t *vd, picture_t *pic)
301 vout_display_sys_t *sys = vd->sys;
303 vout_display_opengl_Prepare (&sys->vgl, pic);
306 static void PictureDisplay (vout_display_t *vd, picture_t *pic)
308 vout_display_sys_t *sys = vd->sys;
310 vout_display_opengl_Display (&sys->vgl, &vd->source);
311 picture_Release (pic);
314 static int Control (vout_display_t *vd, int query, va_list ap)
316 vout_display_sys_t *sys = vd->sys;
320 case VOUT_DISPLAY_HIDE_MOUSE:
321 case VOUT_DISPLAY_RESET_PICTURES: // not needed?
324 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
326 const vout_display_cfg_t *cfg =
327 va_arg (ap, const vout_display_cfg_t *);
329 return vout_window_SetFullScreen (sys->window, cfg->is_fullscreen);
332 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
334 unsigned state = va_arg (ap, unsigned);
336 return vout_window_SetState (sys->window, state);
339 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
340 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
341 case VOUT_DISPLAY_CHANGE_ZOOM:
343 const vout_display_cfg_t *cfg = va_arg (ap, const vout_display_cfg_t *);
344 const video_format_t *src = &vd->source;
346 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
350 force = va_arg (ap, int);
352 && (cfg->display.width != vd->cfg->display.width
353 || cfg->display.height != vd->cfg->display.height)
354 && vout_window_SetSize (sys->window,
355 cfg->display.width, cfg->display.height))
359 vout_display_place_t place;
361 vout_display_PlacePicture (&place, src, cfg, false);
362 glViewport (0, 0, place.width, place.height);
366 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
367 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
369 const vout_display_cfg_t *cfg = vd->cfg;
370 const video_format_t *src = va_arg (ap, const video_format_t *);
371 vout_display_place_t place;
373 vout_display_PlacePicture (&place, src, cfg, false);
374 glViewport (0, 0, place.width, place.height);
378 case VOUT_DISPLAY_GET_OPENGL:
380 vout_opengl_t **gl = va_arg (ap, vout_opengl_t **);
387 msg_Err (vd, "Unknown request %d", query);
392 static void Manage (vout_display_t *vd)
394 //vout_display_sys_t *sys = vd->sys;