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