]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/glx.c
GLX 1.3: pick a frame buffer configuration with matching visual
[vlc] / modules / video_output / xcb / glx.c
1 /**
2  * @file glx.c
3  * @brief GLX video output module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2004 the VideoLAN team
7  * Copyright © 2009 Rémi Denis-Courmont
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  ****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <assert.h>
30
31 #include <xcb/xcb.h>
32 #include <X11/Xlib-xcb.h>
33 #include <GL/glx.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_vout_display.h>
38 #include <vlc_vout_opengl.h>
39 #include "../opengl.h"
40
41 #include "xcb_vlc.h"
42
43 static int  Open (vlc_object_t *);
44 static void Close (vlc_object_t *);
45
46 /*
47  * Module descriptor
48  */
49 vlc_module_begin ()
50     set_shortname (N_("GLX"))
51     set_description (N_("GLX video output (XCB)"))
52     set_category (CAT_VIDEO)
53     set_subcategory (SUBCAT_VIDEO_VOUT)
54     set_capability ("vout display", 20)
55     set_callbacks (Open, Close)
56
57     add_shortcut ("xcb-glx")
58     add_shortcut ("glx")
59 vlc_module_end ()
60
61 struct vout_display_sys_t
62 {
63     Display *display; /* Xlib instance */
64     vout_window_t *embed; /* VLC window (when windowed) */
65
66     xcb_cursor_t cursor; /* blank cursor */
67     xcb_window_t window; /* drawable X window */
68     xcb_window_t glwin; /* GLX window */
69     bool visible; /* whether to draw */
70     bool v1_3; /* whether GLX >= 1.3 is available */
71
72     GLXContext ctx;
73     vout_opengl_t gl;
74     vout_display_opengl_t vgl;
75     picture_pool_t *pool; /* picture pool */
76 };
77
78 static picture_t *Get (vout_display_t *);
79 static void PictureRender (vout_display_t *, picture_t *);
80 static void PictureDisplay (vout_display_t *, picture_t *);
81 static int Control (vout_display_t *, int, va_list);
82 static void Manage (vout_display_t *);
83
84 static void SwapBuffers (vout_opengl_t *gl);
85
86 static vout_window_t *MakeWindow (vout_display_t *vd)
87 {
88     vout_window_cfg_t wnd_cfg;
89
90     memset (&wnd_cfg, 0, sizeof (wnd_cfg));
91     wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
92     wnd_cfg.width  = vd->cfg->display.width;
93     wnd_cfg.height = vd->cfg->display.height;
94
95     vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
96     if (wnd == NULL)
97         msg_Err (vd, "parent window not available");
98     return wnd;
99 }
100
101 static const xcb_screen_t *
102 FindWindow (vout_display_t *vd, xcb_connection_t *conn,
103             unsigned *restrict pnum, uint8_t *restrict pdepth,
104             uint16_t *restrict pwidth, uint16_t *restrict pheight)
105 {
106     vout_display_sys_t *sys = vd->sys;
107
108     xcb_get_geometry_reply_t *geo =
109         xcb_get_geometry_reply (conn,
110             xcb_get_geometry (conn, sys->embed->xid), NULL);
111     if (geo == NULL)
112     {
113         msg_Err (vd, "parent window not valid");
114         return NULL;
115     }
116
117     xcb_window_t root = geo->root;
118     *pdepth = geo->depth;
119     *pwidth = geo->width;
120     *pheight = geo->height;
121     free (geo);
122
123     /* Find the selected screen */
124     const xcb_setup_t *setup = xcb_get_setup (conn);
125     const xcb_screen_t *screen = NULL;
126     unsigned num = 0;
127
128     for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
129          i.rem > 0;
130          xcb_screen_next (&i))
131     {
132         if (i.data->root == root)
133         {
134             screen = i.data;
135             break;
136         }
137         num++;
138     }
139
140     if (screen == NULL)
141     {
142         msg_Err (vd, "parent window screen not found");
143         return NULL;
144     }
145     msg_Dbg (vd, "using screen 0x%"PRIx32 " (number: %u)", root, num);
146     *pnum = num;
147     return screen;
148 }
149
150 static bool CheckGLX (vout_display_t *vd, Display *dpy, bool *restrict pv13)
151 {
152     int major, minor;
153     bool ok = false;
154
155     if (!glXQueryVersion (dpy, &major, &minor))
156         msg_Dbg (vd, "GLX extension not available");
157     else
158     if (major != 1)
159         msg_Dbg (vd, "GLX extension version %d.%d unknown", major, minor);
160     else
161     if (minor < 2)
162         msg_Dbg (vd, "GLX extension version %d.%d too old", major, minor);
163     else
164     {
165         msg_Dbg (vd, "using GLX extension version %d.%d", major, minor);
166         ok = true;
167         *pv13 = minor >= 3;
168     }
169     return ok;
170 }
171
172 static int CreateWindow (vout_display_t *vd, xcb_connection_t *conn,
173                          uint_fast8_t depth, xcb_visualid_t vid,
174                          uint_fast16_t width, uint_fast16_t height)
175 {
176     vout_display_sys_t *sys = vd->sys;
177     const uint32_t mask = XCB_CW_EVENT_MASK;
178     const uint32_t values[] = {
179         /* XCB_CW_EVENT_MASK */
180         XCB_EVENT_MASK_VISIBILITY_CHANGE,
181     };
182     xcb_void_cookie_t cc, cm;
183
184     cc = xcb_create_window_checked (conn, depth, sys->window,
185                                     sys->embed->xid, 0, 0,
186                                     width, height, 0,
187                                     XCB_WINDOW_CLASS_INPUT_OUTPUT,
188                                     vid, mask, values);
189     cm = xcb_map_window_checked (conn, sys->window);
190     if (CheckError (vd, conn, "cannot create X11 window", cc)
191      || CheckError (vd, conn, "cannot map X11 window", cm))
192         return VLC_EGENERIC;
193
194     msg_Dbg (vd, "using X11 window %08"PRIx32, sys->window);
195     return VLC_SUCCESS;
196 }
197
198 /**
199  * Probe the X server.
200  */
201 static int Open (vlc_object_t *obj)
202 {
203     vout_display_t *vd = (vout_display_t *)obj;
204     vout_display_sys_t *sys = malloc (sizeof (*sys));
205
206     if (sys == NULL)
207         return VLC_ENOMEM;
208
209     vd->sys = sys;
210     sys->pool = NULL;
211     sys->gl.sys = NULL;
212
213     /* Get window */
214     sys->embed = MakeWindow (vd);
215     if (sys->embed == NULL)
216     {
217         free (sys);
218         return VLC_EGENERIC;
219     }
220
221     /* Connect to X server */
222     Display *dpy = XOpenDisplay (sys->embed->x11_display);
223     if (dpy == NULL)
224     {
225         vout_display_DeleteWindow (vd, sys->embed);
226         free (sys);
227         return VLC_EGENERIC;
228     }
229     sys->display = dpy;
230     sys->ctx = NULL;
231     XSetEventQueueOwner (dpy, XCBOwnsEventQueue);
232
233     if (!CheckGLX (vd, dpy, &sys->v1_3))
234         goto error;
235
236     xcb_connection_t *conn = XGetXCBConnection (dpy);
237     assert (conn);
238     RegisterMouseEvents (obj, conn, sys->embed->xid);
239
240     /* Find window parameters */
241     unsigned snum;
242     uint8_t depth;
243     uint16_t width, height;
244     const xcb_screen_t *scr = FindWindow (vd, conn, &snum, &depth,
245                                           &width, &height);
246     if (scr == NULL)
247         goto error;
248
249     sys->window = xcb_generate_id (conn);
250
251     /* Determine our pixel format */
252     if (sys->v1_3)
253     {   /* GLX 1.3 */
254         static const int attr[] = {
255             GLX_RED_SIZE, 5,
256             GLX_GREEN_SIZE, 5,
257             GLX_BLUE_SIZE, 5,
258             GLX_DOUBLEBUFFER, True,
259             GLX_X_RENDERABLE, True,
260             GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
261             None };
262
263         xcb_get_window_attributes_reply_t *wa =
264             xcb_get_window_attributes_reply (conn,
265                 xcb_get_window_attributes (conn, sys->embed->xid), NULL);
266         if (wa == NULL)
267             goto error;
268         xcb_visualid_t visual = wa->visual;
269         free (wa);
270
271         int nelem;
272         GLXFBConfig *confs = glXChooseFBConfig (dpy, snum, attr, &nelem);
273         if (confs == NULL)
274         {
275             msg_Err (vd, "no GLX frame bufer configurations");
276             goto error;
277         }
278
279         GLXFBConfig conf;
280         bool found = false;
281
282         for (int i = 0; i < nelem && !found; i++)
283         {
284             conf = confs[i];
285
286             XVisualInfo *vi = glXGetVisualFromFBConfig (dpy, conf);
287             if (vi == NULL)
288                 continue;
289
290             if (vi->visualid == visual)
291                 found = true;
292             XFree (vi);
293         }
294         XFree (confs);
295
296         if (!found)
297         {
298             msg_Err (vd, "no matching GLX frame buffer configuration");
299             goto error;
300         }
301
302         sys->glwin = None;
303         if (!CreateWindow (vd, conn, depth, 0 /* ??? */, width, height))
304             sys->glwin = glXCreateWindow (dpy, conf, sys->window, NULL );
305         if (sys->glwin == None)
306         {
307             msg_Err (vd, "cannot create GLX window");
308             goto error;
309         }
310
311         /* Create an OpenGL context */
312         sys->ctx = glXCreateNewContext (dpy, conf, GLX_RGBA_TYPE, NULL,
313                                         True);
314         if (sys->ctx == NULL)
315         {
316             msg_Err (vd, "cannot create GLX context");
317             goto error;
318         }
319
320         if (!glXMakeContextCurrent (dpy, sys->glwin, sys->glwin, sys->ctx))
321             goto error;
322     }
323     else
324     {   /* GLX 1.2 */
325         int attr[] = {
326             GLX_RGBA,
327             GLX_RED_SIZE, 5,
328             GLX_GREEN_SIZE, 5,
329             GLX_BLUE_SIZE, 5,
330             GLX_DOUBLEBUFFER,
331             None };
332
333         XVisualInfo *vi = glXChooseVisual (dpy, snum, attr);
334         if (vi == NULL)
335         {
336             msg_Err (vd, "cannot find GLX 1.2 visual" );
337             goto error;
338         }
339         msg_Dbg (vd, "using GLX visual ID 0x%"PRIx32, (uint32_t)vi->visualid);
340
341         if (CreateWindow (vd, conn, depth, 0 /* ??? */, width, height) == 0)
342             sys->ctx = glXCreateContext (dpy, vi, 0, True);
343         XFree (vi);
344         if (sys->ctx == NULL)
345         {
346             msg_Err (vd, "cannot create GLX context");
347             goto error;
348         }
349
350         if (glXMakeCurrent (dpy, sys->window, sys->ctx) == False)
351             goto error;
352         sys->glwin = sys->window;
353     }
354
355     /* Initialize common OpenGL video display */
356     sys->gl.lock = NULL;
357     sys->gl.unlock = NULL;
358     sys->gl.swap = SwapBuffers;
359     sys->gl.sys = sys;
360
361     if (vout_display_opengl_Init (&sys->vgl, &vd->fmt, &sys->gl))
362     {
363         sys->gl.sys = NULL;
364         goto error;
365     }
366
367     sys->cursor = CreateBlankCursor (conn, scr);
368     sys->visible = false;
369
370     /* */
371     vout_display_info_t info = vd->info;
372     info.has_pictures_invalid = false;
373
374     /* Setup vout_display_t once everything is fine */
375     vd->info = info;
376
377     vd->get = Get;
378     vd->prepare = PictureRender;
379     vd->display = PictureDisplay;
380     vd->control = Control;
381     vd->manage = Manage;
382
383     /* */
384     vout_display_SendEventFullscreen (vd, false);
385     vout_display_SendEventDisplaySize (vd, width, height, false);
386
387     return VLC_SUCCESS;
388
389 error:
390     Close (obj);
391     return VLC_EGENERIC;
392 }
393
394
395 /**
396  * Disconnect from the X server.
397  */
398 static void Close (vlc_object_t *obj)
399 {
400     vout_display_t *vd = (vout_display_t *)obj;
401     vout_display_sys_t *sys = vd->sys;
402     Display *dpy = sys->display;
403
404     if (sys->gl.sys != NULL)
405         vout_display_opengl_Clean (&sys->vgl);
406
407     if (sys->ctx != NULL)
408     {
409         if (sys->v1_3)
410         {
411             glXMakeContextCurrent (dpy, None, None, NULL);
412             glXDestroyWindow (dpy, sys->glwin);
413         }
414         else
415             glXMakeCurrent (dpy, None, NULL);
416         glXDestroyContext (dpy, sys->ctx);
417     }
418     XCloseDisplay (dpy);
419     vout_display_DeleteWindow (vd, sys->embed);
420     free (sys);
421 }
422
423 static void SwapBuffers (vout_opengl_t *gl)
424 {
425     vout_display_sys_t *sys = gl->sys;
426
427     glXSwapBuffers (sys->display, sys->glwin);
428 }
429
430 /**
431  * Return a direct buffer
432  */
433 static picture_t *Get (vout_display_t *vd)
434 {
435     vout_display_sys_t *sys = vd->sys;
436
437     if (!sys->pool)
438     {
439         sys->pool = vout_display_opengl_GetPool (&sys->vgl);
440         if (!sys->pool)
441             return NULL;
442     }
443     return picture_pool_Get (sys->pool);
444 }
445
446 static void PictureRender (vout_display_t *vd, picture_t *pic)
447 {
448    vout_display_sys_t *sys = vd->sys;
449
450     vout_display_opengl_Prepare (&sys->vgl, pic);
451 }
452
453 static void PictureDisplay (vout_display_t *vd, picture_t *pic)
454 {
455     vout_display_sys_t *sys = vd->sys;
456
457     vout_display_opengl_Display (&sys->vgl, &vd->source);
458     picture_Release (pic);
459 }
460
461 static int Control (vout_display_t *vd, int query, va_list ap)
462 {
463     vout_display_sys_t *sys = vd->sys;
464
465     switch (query)
466     {
467     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
468     {
469         const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
470         return vout_window_SetFullScreen (sys->embed, c->is_fullscreen);
471     }
472
473     case VOUT_DISPLAY_CHANGE_ON_TOP:
474     {
475         int b_on_top = (int)va_arg (ap, int);
476         return vout_window_SetOnTop (sys->embed, b_on_top);
477     }
478
479     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
480     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
481     case VOUT_DISPLAY_CHANGE_ZOOM:
482     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
483     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
484     {
485         xcb_connection_t *conn = XGetXCBConnection (sys->display);
486         const vout_display_cfg_t *cfg;
487         const video_format_t *source;
488         bool is_forced = false;
489
490         if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
491          || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
492         {
493             source = (const video_format_t *)va_arg (ap, const video_format_t *);
494             cfg = vd->cfg;
495         }
496         else
497         {
498             source = &vd->source;
499             cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
500             if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
501                 is_forced = (bool)va_arg (ap, int);
502         }
503
504         /* */
505         if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
506          && is_forced
507          && (cfg->display.width  != vd->cfg->display.width
508            ||cfg->display.height != vd->cfg->display.height)
509          && vout_window_SetSize (sys->embed,
510                                  cfg->display.width, cfg->display.height))
511             return VLC_EGENERIC;
512
513         vout_display_place_t place;
514         vout_display_PlacePicture (&place, source, cfg, false);
515
516         /* Move the picture within the window */
517         const uint32_t values[] = { place.x, place.y,
518                                     place.width, place.height, };
519         xcb_void_cookie_t ck =
520             xcb_configure_window_checked (conn, sys->window,
521                             XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
522                           | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
523                               values);
524         if (CheckError (vd, conn, "cannot resize X11 window", ck))
525             return VLC_EGENERIC;
526
527         glViewport (0, 0, place.width, place.height);
528         return VLC_SUCCESS;
529     }
530
531     /* Hide the mouse. It will be send when
532      * vout_display_t::info.b_hide_mouse is false */
533     case VOUT_DISPLAY_HIDE_MOUSE:
534         xcb_change_window_attributes (XGetXCBConnection (sys->display),
535                                       sys->embed->xid,
536                                     XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
537         return VLC_SUCCESS;
538     case VOUT_DISPLAY_RESET_PICTURES:
539         assert (0);
540     default:
541         msg_Err (vd, "Unknown request in XCB vout display");
542         return VLC_EGENERIC;
543     }
544 }
545
546 static void Manage (vout_display_t *vd)
547 {
548     vout_display_sys_t *sys = vd->sys;
549     xcb_connection_t *conn = XGetXCBConnection (sys->display);
550
551     ManageEvent (vd, conn, &sys->visible);
552 }