]> git.sesse.net Git - vlc/blob - modules/video_output/egl.c
EGL: compile fix
[vlc] / modules / video_output / egl.c
1 /**
2  * @file egl.c
3  * @brief EGL video output module
4  */
5 /*****************************************************************************
6  * Copyright © 2010 Rémi Denis-Courmont
7  *
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.
12  *
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.
17  *
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  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <EGL/egl.h>
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_display.h>
34 #include <vlc_vout_opengl.h>
35 #include "opengl.h"
36 #ifdef __unix__
37 # include <vlc_xlib.h>
38 #endif
39
40 #if USE_OPENGL_ES
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
45 # else
46 #  define VLC_RENDERABLE_BIT EGL_OPENGL_ES_BIT
47 # endif
48 #else
49 # define VLC_API_NAME "OpenGL"
50 # define VLC_API EGL_OPENGL_API
51 # define VLC_RENDERABLE_BIT EGL_OPENGL_BIT
52 #endif
53
54 #ifdef __unix__
55 # include <dlfcn.h>
56 #endif
57
58 /* Plugin callbacks */
59 static int Open (vlc_object_t *);
60 static void Close (vlc_object_t *);
61
62 vlc_module_begin ()
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)
69 vlc_module_end ()
70
71 struct vout_display_sys_t
72 {
73     EGLDisplay display;
74     EGLSurface surface;
75
76     vout_opengl_t gl;
77     vout_display_opengl_t vgl;
78
79     picture_pool_t *pool;
80     vout_window_t *window;
81 };
82
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);
90
91 static bool CheckAPI (EGLDisplay dpy, const char *api)
92 {
93     const char *apis = eglQueryString (dpy, EGL_CLIENT_APIS);
94     size_t apilen = strlen (api);
95
96     /* Cannot use strtok_r() on constant string... */
97     do
98     {
99         if (!strncmp (apis, api, apilen)
100           && (memchr (" ", apis[apilen], 2) != NULL))
101             return true;
102
103         apis = strchr (apis, ' ');
104     }
105     while (apis != NULL);
106
107     return false;
108 }
109
110 static vout_window_t *MakeWindow (vout_display_t *vd, EGLNativeWindowType *id)
111 {
112     vout_window_cfg_t wnd_cfg;
113
114     memset (&wnd_cfg, 0, sizeof (wnd_cfg));
115 #if defined (WIN32)
116     wnd_cfg.type = VOUT_WINDOW_TYPE_HWND;
117 #elif defined (__unix__)
118     wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
119 #else
120 # error Unknown native window type!
121 #endif
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;
126
127     vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
128     if (wnd != NULL)
129 #if defined (WIN32)
130         *id = wnd->handle.hwnd;
131 #elif defined (__unix__)
132         *id = wnd->handle.xid;
133 #endif
134     else
135         msg_Err (vd, "parent window not available");
136     return wnd;
137 }
138
139 /**
140  * Probe EGL display availability
141  */
142 static int Open (vlc_object_t *obj)
143 {
144 #ifdef __unix__
145     if (!vlc_xlib_init (obj))
146         return VLC_EGENERIC;
147 #endif
148     vout_display_t *vd = (vout_display_t *)obj;
149
150     /* Initialize EGL display */
151     /* TODO: support various display types */
152     EGLDisplay dpy = eglGetDisplay (EGL_DEFAULT_DISPLAY);
153     if (dpy == EGL_NO_DISPLAY)
154         return VLC_EGENERIC;
155
156     vout_display_sys_t *sys = malloc (sizeof (*sys));
157     if (unlikely(sys == NULL))
158         return VLC_ENOMEM;
159     vd->sys = sys;
160     sys->display = dpy;
161     sys->gl.sys = NULL;
162     sys->pool = NULL;
163
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. */
171 #ifdef __unix__
172     dlopen ("libEGL.so", RTLD_GLOBAL|RTLD_LAZY);
173 #endif
174
175     EGLint major, minor;
176     if (eglInitialize (dpy, &major, &minor) != EGL_TRUE)
177     {
178         /* No need to call eglTerminate() in this case */
179         free (sys);
180         return VLC_EGENERIC;
181     }
182
183     if (major != 1)
184         goto abort;
185 #if USE_OPENGL_ES == 2
186     if (minor < 3) /* Well well, this wouldn't compile with 1.2 anyway */
187         goto abort;
188 #elif USE_OPENGL_ES == 0
189     if (minor < 4)
190         goto abort;
191 #endif
192
193     if (!CheckAPI (dpy, VLC_API_NAME))
194         goto abort;
195
196     msg_Dbg (obj, "EGL version %s by %s", eglQueryString (dpy, EGL_VERSION),
197              eglQueryString (dpy, EGL_VENDOR));
198     {
199         const char *ext = eglQueryString (dpy, EGL_EXTENSIONS);
200         if (*ext)
201             msg_Dbg (obj, " extensions: %s", ext);
202     }
203
204     static const EGLint conf_attr[] = {
205         EGL_RED_SIZE, 5,
206         EGL_GREEN_SIZE, 5,
207         EGL_BLUE_SIZE, 5,
208         EGL_RENDERABLE_TYPE, VLC_RENDERABLE_BIT,
209         EGL_NONE
210     };
211     EGLConfig cfgv[1];
212     EGLint cfgc;
213
214     if (eglChooseConfig (dpy, conf_attr, cfgv, 1, &cfgc) != EGL_TRUE
215      || cfgc == 0)
216         goto abort;
217
218     /* Create a drawing surface */
219     EGLNativeWindowType win;
220     sys->window = MakeWindow (vd, &win);
221     if (sys->window == NULL)
222         goto abort;
223
224     EGLSurface surface = eglCreateWindowSurface (dpy, cfgv[0], win, NULL);
225     if (surface == EGL_NO_SURFACE)
226     {
227         msg_Err (obj, "cannot create EGL window surface");
228         goto error;
229     }
230     sys->surface = surface;
231
232     if (eglBindAPI (VLC_API) != EGL_TRUE)
233     {
234         msg_Err (obj, "cannot bind EGL API");
235         goto error;
236     }
237
238     static const EGLint ctx_attr[] = {
239 #if USE_OPENGL_ES
240         EGL_CONTEXT_CLIENT_VERSION, USE_OPENGL_ES,
241 #endif
242         EGL_NONE
243     };   
244
245     EGLContext ctx = eglCreateContext (dpy, cfgv[0], EGL_NO_CONTEXT,
246                                        ctx_attr);
247     if (ctx == EGL_NO_CONTEXT)
248     {
249         msg_Err (obj, "cannot create EGL context");
250         goto error;
251     }
252
253     if (eglMakeCurrent (dpy, surface, surface, ctx) != EGL_TRUE)
254         goto error;
255
256     /* Initialize OpenGL callbacks */
257     sys->gl.lock = NULL;
258     sys->gl.unlock = NULL;
259     sys->gl.swap = SwapBuffers;
260     sys->gl.sys = sys;
261
262     if (vout_display_opengl_Init (&sys->vgl, &vd->fmt, &sys->gl))
263         goto error;
264
265     /* Initialize video display */
266     vd->info.has_pictures_invalid = false;
267     vd->info.has_event_thread = false;
268     vd->pool = Pool;
269     vd->prepare = PictureRender;
270     vd->display = PictureDisplay;
271     vd->control = Control;
272     vd->manage = NULL;
273
274     return VLC_SUCCESS;
275 error:
276     vout_display_DeleteWindow (vd, sys->window);
277 abort:
278     eglTerminate (dpy);
279     free (sys);
280     return VLC_EGENERIC;
281 }
282
283
284 /**
285  * Destrisconnect from the X server.
286  */
287 static void Close (vlc_object_t *obj)
288 {
289     vout_display_t *vd = (vout_display_t *)obj;
290     vout_display_sys_t *sys = vd->sys;
291     EGLDisplay dpy = sys->display;
292
293     if (sys->gl.sys != NULL)
294         vout_display_opengl_Clean (&sys->vgl);
295     eglTerminate (dpy);
296     vout_display_DeleteWindow (vd, sys->window);
297     free (sys);
298 }
299
300 static void SwapBuffers (vout_opengl_t *gl)
301 {
302     vout_display_sys_t *sys = gl->sys;
303
304     eglSwapBuffers (sys->display, sys->surface);
305 }
306
307 /**
308  * Return a direct buffer
309  */
310 static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
311 {
312     vout_display_sys_t *sys = vd->sys;
313
314     if (!sys->pool)
315         sys->pool = vout_display_opengl_GetPool (&sys->vgl);
316     (void) count;
317     return sys->pool;
318 }
319
320 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
321 {
322     vout_display_sys_t *sys = vd->sys;
323
324     vout_display_opengl_Prepare (&sys->vgl, pic);
325     (void)subpicture;
326 }
327
328 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
329 {
330     vout_display_sys_t *sys = vd->sys;
331
332     vout_display_opengl_Display (&sys->vgl, &vd->source);
333     picture_Release (pic);
334     (void)subpicture;
335 }
336
337 static int Control (vout_display_t *vd, int query, va_list ap)
338 {
339     vout_display_sys_t *sys = vd->sys;
340
341     switch (query)
342     {
343       case VOUT_DISPLAY_HIDE_MOUSE:
344       case VOUT_DISPLAY_RESET_PICTURES: // not needed?
345           break;
346
347       case VOUT_DISPLAY_CHANGE_FULLSCREEN:
348       {
349         const vout_display_cfg_t *cfg =
350             va_arg (ap, const vout_display_cfg_t *);
351
352         return vout_window_SetFullScreen (sys->window, cfg->is_fullscreen);
353       }
354
355       case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
356       {
357         unsigned state = va_arg (ap, unsigned);
358
359         return vout_window_SetState (sys->window, state);
360       }
361
362       case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
363       case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
364       case VOUT_DISPLAY_CHANGE_ZOOM:
365       {
366         const vout_display_cfg_t *cfg = va_arg (ap, const vout_display_cfg_t *);
367         const video_format_t *src = &vd->source;
368
369         if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
370         {
371             bool force = false;
372
373             force = va_arg (ap, int);
374             if (force
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))
379                 return VLC_EGENERIC;
380         }
381
382         vout_display_place_t place;
383
384         vout_display_PlacePicture (&place, src, cfg, false);
385         glViewport (0, 0, place.width, place.height);
386         return VLC_SUCCESS;
387       }
388
389       case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
390       case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
391       {
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;
395
396         vout_display_PlacePicture (&place, src, cfg, false);
397         glViewport (0, 0, place.width, place.height);
398         return VLC_SUCCESS;
399       }
400
401       case VOUT_DISPLAY_GET_OPENGL:
402       {
403         vout_opengl_t **gl = va_arg (ap, vout_opengl_t **);
404
405         *gl = &sys->gl;
406         return VLC_SUCCESS;
407       }
408
409       default:
410         msg_Err (vd, "Unknown request %d", query);
411     }
412     return VLC_EGENERIC;
413 }
414