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