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