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