3 * @brief GLX video output module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2004 the VideoLAN team
7 * Copyright © 2009 Rémi Denis-Courmont
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.
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.
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 ****************************************************************************/
32 #include <X11/Xlib-xcb.h>
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_vout_display.h>
39 #include <vlc_vout_opengl.h>
40 #include "../opengl.h"
44 static int Open (vlc_object_t *);
45 static void Close (vlc_object_t *);
51 set_shortname (N_("GLX"))
52 set_description (N_("GLX video output (XCB)"))
53 set_category (CAT_VIDEO)
54 set_subcategory (SUBCAT_VIDEO_VOUT)
55 set_capability ("vout display", 50)
56 set_callbacks (Open, Close)
58 add_shortcut ("xcb-glx")
60 add_shortcut ("opengl")
63 struct vout_display_sys_t
65 Display *display; /* Xlib instance */
66 vout_window_t *embed; /* VLC window (when windowed) */
68 xcb_cursor_t cursor; /* blank cursor */
69 xcb_window_t window; /* drawable X window */
70 xcb_window_t glwin; /* GLX window */
71 bool visible; /* whether to draw */
72 bool v1_3; /* whether GLX >= 1.3 is available */
76 vout_display_opengl_t vgl;
77 picture_pool_t *pool; /* picture pool */
80 static picture_pool_t *Pool (vout_display_t *, unsigned);
81 static void PictureRender (vout_display_t *, picture_t *);
82 static void PictureDisplay (vout_display_t *, picture_t *);
83 static int Control (vout_display_t *, int, va_list);
84 static void Manage (vout_display_t *);
86 static void SwapBuffers (vout_opengl_t *gl);
88 static vout_window_t *MakeWindow (vout_display_t *vd)
90 vout_window_cfg_t wnd_cfg;
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;
99 vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
101 msg_Err (vd, "parent window not available");
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)
110 vout_display_sys_t *sys = vd->sys;
112 xcb_get_geometry_reply_t *geo =
113 xcb_get_geometry_reply (conn,
114 xcb_get_geometry (conn, sys->embed->handle.xid), NULL);
117 msg_Err (vd, "parent window not valid");
121 xcb_window_t root = geo->root;
122 *pdepth = geo->depth;
123 *pwidth = geo->width;
124 *pheight = geo->height;
127 /* Find the selected screen */
128 const xcb_setup_t *setup = xcb_get_setup (conn);
129 const xcb_screen_t *screen = NULL;
132 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
134 xcb_screen_next (&i))
136 if (i.data->root == root)
146 msg_Err (vd, "parent window screen not found");
149 msg_Dbg (vd, "using screen 0x%"PRIx32 " (number: %u)", root, num);
154 static bool CheckGLX (vout_display_t *vd, Display *dpy, bool *restrict pv13)
159 if (!glXQueryVersion (dpy, &major, &minor))
160 msg_Dbg (vd, "GLX extension not available");
163 msg_Dbg (vd, "GLX extension version %d.%d unknown", major, minor);
166 msg_Dbg (vd, "GLX extension version %d.%d too old", major, minor);
169 msg_Dbg (vd, "using GLX extension version %d.%d", major, minor);
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)
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,
186 xcb_void_cookie_t cc, cm;
188 cc = xcb_create_window_checked (conn, depth, sys->window,
189 sys->embed->handle.xid, 0, 0,
191 XCB_WINDOW_CLASS_INPUT_OUTPUT,
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))
198 msg_Dbg (vd, "using X11 window %08"PRIx32, sys->window);
203 * Probe the X server.
205 static int Open (vlc_object_t *obj)
207 if (!vlc_xlib_init (obj))
210 vout_display_t *vd = (vout_display_t *)obj;
211 vout_display_sys_t *sys = malloc (sizeof (*sys));
221 sys->embed = MakeWindow (vd);
222 if (sys->embed == NULL)
228 /* Connect to X server */
229 Display *dpy = XOpenDisplay (sys->embed->display.x11);
232 vout_display_DeleteWindow (vd, sys->embed);
238 XSetEventQueueOwner (dpy, XCBOwnsEventQueue);
240 if (!CheckGLX (vd, dpy, &sys->v1_3))
243 xcb_connection_t *conn = XGetXCBConnection (dpy);
245 RegisterMouseEvents (obj, conn, sys->embed->handle.xid);
247 /* Find window parameters */
250 uint16_t width, height;
251 const xcb_screen_t *scr = FindWindow (vd, conn, &snum, &depth,
256 sys->window = xcb_generate_id (conn);
258 /* Determine our pixel format */
261 static const int attr[] = {
265 GLX_DOUBLEBUFFER, True,
266 GLX_X_RENDERABLE, True,
267 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
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),
276 xcb_visualid_t visual = wa->visual;
280 GLXFBConfig *confs = glXChooseFBConfig (dpy, snum, attr, &nelem);
283 msg_Err (vd, "no GLX frame bufer configurations");
290 for (int i = 0; i < nelem && !found; i++)
294 XVisualInfo *vi = glXGetVisualFromFBConfig (dpy, conf);
298 if (vi->visualid == visual)
306 msg_Err (vd, "no matching GLX frame buffer configuration");
311 if (!CreateWindow (vd, conn, depth, 0 /* ??? */, width, height))
312 sys->glwin = glXCreateWindow (dpy, conf, sys->window, NULL );
313 if (sys->glwin == None)
315 msg_Err (vd, "cannot create GLX window");
319 /* Create an OpenGL context */
320 sys->ctx = glXCreateNewContext (dpy, conf, GLX_RGBA_TYPE, NULL,
322 if (sys->ctx == NULL)
324 msg_Err (vd, "cannot create GLX context");
328 if (!glXMakeContextCurrent (dpy, sys->glwin, sys->glwin, sys->ctx))
341 XVisualInfo *vi = glXChooseVisual (dpy, snum, attr);
344 msg_Err (vd, "cannot find GLX 1.2 visual" );
347 msg_Dbg (vd, "using GLX visual ID 0x%"PRIx32, (uint32_t)vi->visualid);
349 if (CreateWindow (vd, conn, depth, 0 /* ??? */, width, height) == 0)
350 sys->ctx = glXCreateContext (dpy, vi, 0, True);
352 if (sys->ctx == NULL)
354 msg_Err (vd, "cannot create GLX context");
358 if (glXMakeCurrent (dpy, sys->window, sys->ctx) == False)
360 sys->glwin = sys->window;
363 /* Initialize common OpenGL video display */
365 sys->gl.unlock = NULL;
366 sys->gl.swap = SwapBuffers;
369 if (vout_display_opengl_Init (&sys->vgl, &vd->fmt, &sys->gl))
375 sys->cursor = CreateBlankCursor (conn, scr);
376 sys->visible = false;
379 vout_display_info_t info = vd->info;
380 info.has_pictures_invalid = false;
381 info.has_event_thread = true;
383 /* Setup vout_display_t once everything is fine */
387 vd->prepare = PictureRender;
388 vd->display = PictureDisplay;
389 vd->control = Control;
393 bool is_fullscreen = vd->cfg->is_fullscreen;
394 if (is_fullscreen && vout_window_SetFullScreen (sys->embed, true))
395 is_fullscreen = false;
396 vout_display_SendEventFullscreen (vd, is_fullscreen);
397 vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
408 * Disconnect from the X server.
410 static void Close (vlc_object_t *obj)
412 vout_display_t *vd = (vout_display_t *)obj;
413 vout_display_sys_t *sys = vd->sys;
414 Display *dpy = sys->display;
416 if (sys->gl.sys != NULL)
417 vout_display_opengl_Clean (&sys->vgl);
419 if (sys->ctx != NULL)
422 glXMakeContextCurrent (dpy, None, None, NULL);
424 glXMakeCurrent (dpy, None, NULL);
425 glXDestroyContext (dpy, sys->ctx);
427 glXDestroyWindow (dpy, sys->glwin);
430 /* show the default cursor */
431 xcb_change_window_attributes (XGetXCBConnection (sys->display),
432 sys->embed->handle.xid, XCB_CW_CURSOR,
433 &(uint32_t) { XCB_CURSOR_NONE });
434 xcb_flush (XGetXCBConnection (sys->display));
437 vout_display_DeleteWindow (vd, sys->embed);
441 static void SwapBuffers (vout_opengl_t *gl)
443 vout_display_sys_t *sys = gl->sys;
445 glXSwapBuffers (sys->display, sys->glwin);
449 * Return a direct buffer
451 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
453 vout_display_sys_t *sys = vd->sys;
454 (void)requested_count;
457 sys->pool = vout_display_opengl_GetPool (&sys->vgl);
461 static void PictureRender (vout_display_t *vd, picture_t *pic)
463 vout_display_sys_t *sys = vd->sys;
465 vout_display_opengl_Prepare (&sys->vgl, pic);
468 static void PictureDisplay (vout_display_t *vd, picture_t *pic)
470 vout_display_sys_t *sys = vd->sys;
472 vout_display_opengl_Display (&sys->vgl, &vd->source);
473 picture_Release (pic);
476 static int Control (vout_display_t *vd, int query, va_list ap)
478 vout_display_sys_t *sys = vd->sys;
482 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
484 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
485 return vout_window_SetFullScreen (sys->embed, c->is_fullscreen);
488 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
490 unsigned state = va_arg (ap, unsigned);
491 return vout_window_SetState (sys->embed, state);
494 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
495 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
496 case VOUT_DISPLAY_CHANGE_ZOOM:
497 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
498 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
500 xcb_connection_t *conn = XGetXCBConnection (sys->display);
501 const vout_display_cfg_t *cfg;
502 const video_format_t *source;
503 bool is_forced = false;
505 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
506 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
508 source = (const video_format_t *)va_arg (ap, const video_format_t *);
513 source = &vd->source;
514 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
515 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
516 is_forced = (bool)va_arg (ap, int);
520 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
522 && (cfg->display.width != vd->cfg->display.width
523 ||cfg->display.height != vd->cfg->display.height)
524 && vout_window_SetSize (sys->embed,
525 cfg->display.width, cfg->display.height))
528 vout_display_place_t place;
529 vout_display_PlacePicture (&place, source, cfg, false);
531 /* Move the picture within the window */
532 const uint32_t values[] = { place.x, place.y,
533 place.width, place.height, };
534 xcb_void_cookie_t ck =
535 xcb_configure_window_checked (conn, sys->window,
536 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
537 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
539 if (CheckError (vd, conn, "cannot resize X11 window", ck))
542 glViewport (0, 0, place.width, place.height);
546 /* Hide the mouse. It will be send when
547 * vout_display_t::info.b_hide_mouse is false */
548 case VOUT_DISPLAY_HIDE_MOUSE:
549 xcb_change_window_attributes (XGetXCBConnection (sys->display),
550 sys->embed->handle.xid,
551 XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
554 case VOUT_DISPLAY_GET_OPENGL:
556 vout_opengl_t **gl = va_arg (ap, vout_opengl_t **);
561 case VOUT_DISPLAY_RESET_PICTURES:
564 msg_Err (vd, "Unknown request in XCB vout display");
569 static void Manage (vout_display_t *vd)
571 vout_display_sys_t *sys = vd->sys;
572 xcb_connection_t *conn = XGetXCBConnection (sys->display);
574 ManageEvent (vd, conn, &sys->visible);