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>
37 #include <vlc_vout_display.h>
38 #include <vlc_vout_opengl.h>
39 #include "../opengl.h"
43 static int Open (vlc_object_t *);
44 static void Close (vlc_object_t *);
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)
57 add_shortcut ("xcb-glx")
59 add_shortcut ("opengl")
62 struct vout_display_sys_t
64 Display *display; /* Xlib instance */
65 vout_window_t *embed; /* VLC window (when windowed) */
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 */
75 vout_display_opengl_t vgl;
76 picture_pool_t *pool; /* picture pool */
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 *);
85 static void SwapBuffers (vout_opengl_t *gl);
87 static vout_window_t *MakeWindow (vout_display_t *vd)
89 vout_window_cfg_t wnd_cfg;
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;
96 vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
98 msg_Err (vd, "parent window not available");
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)
107 vout_display_sys_t *sys = vd->sys;
109 xcb_get_geometry_reply_t *geo =
110 xcb_get_geometry_reply (conn,
111 xcb_get_geometry (conn, sys->embed->handle.xid), NULL);
114 msg_Err (vd, "parent window not valid");
118 xcb_window_t root = geo->root;
119 *pdepth = geo->depth;
120 *pwidth = geo->width;
121 *pheight = geo->height;
124 /* Find the selected screen */
125 const xcb_setup_t *setup = xcb_get_setup (conn);
126 const xcb_screen_t *screen = NULL;
129 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
131 xcb_screen_next (&i))
133 if (i.data->root == root)
143 msg_Err (vd, "parent window screen not found");
146 msg_Dbg (vd, "using screen 0x%"PRIx32 " (number: %u)", root, num);
151 static bool CheckGLX (vout_display_t *vd, Display *dpy, bool *restrict pv13)
156 if (!glXQueryVersion (dpy, &major, &minor))
157 msg_Dbg (vd, "GLX extension not available");
160 msg_Dbg (vd, "GLX extension version %d.%d unknown", major, minor);
163 msg_Dbg (vd, "GLX extension version %d.%d too old", major, minor);
166 msg_Dbg (vd, "using GLX extension version %d.%d", major, minor);
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)
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,
183 xcb_void_cookie_t cc, cm;
185 cc = xcb_create_window_checked (conn, depth, sys->window,
186 sys->embed->handle.xid, 0, 0,
188 XCB_WINDOW_CLASS_INPUT_OUTPUT,
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))
195 msg_Dbg (vd, "using X11 window %08"PRIx32, sys->window);
200 * Probe the X server.
202 static int Open (vlc_object_t *obj)
204 vout_display_t *vd = (vout_display_t *)obj;
205 vout_display_sys_t *sys = malloc (sizeof (*sys));
215 sys->embed = MakeWindow (vd);
216 if (sys->embed == NULL)
222 /* Connect to X server */
223 Display *dpy = XOpenDisplay (sys->embed->x11_display);
226 vout_display_DeleteWindow (vd, sys->embed);
232 XSetEventQueueOwner (dpy, XCBOwnsEventQueue);
234 if (!CheckGLX (vd, dpy, &sys->v1_3))
237 xcb_connection_t *conn = XGetXCBConnection (dpy);
239 RegisterMouseEvents (obj, conn, sys->embed->handle.xid);
241 /* Find window parameters */
244 uint16_t width, height;
245 const xcb_screen_t *scr = FindWindow (vd, conn, &snum, &depth,
250 sys->window = xcb_generate_id (conn);
252 /* Determine our pixel format */
255 static const int attr[] = {
259 GLX_DOUBLEBUFFER, True,
260 GLX_X_RENDERABLE, True,
261 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
264 xcb_get_window_attributes_reply_t *wa =
265 xcb_get_window_attributes_reply (conn,
266 xcb_get_window_attributes (conn, sys->embed->handle.xid),
270 xcb_visualid_t visual = wa->visual;
274 GLXFBConfig *confs = glXChooseFBConfig (dpy, snum, attr, &nelem);
277 msg_Err (vd, "no GLX frame bufer configurations");
284 for (int i = 0; i < nelem && !found; i++)
288 XVisualInfo *vi = glXGetVisualFromFBConfig (dpy, conf);
292 if (vi->visualid == visual)
300 msg_Err (vd, "no matching GLX frame buffer configuration");
305 if (!CreateWindow (vd, conn, depth, 0 /* ??? */, width, height))
306 sys->glwin = glXCreateWindow (dpy, conf, sys->window, NULL );
307 if (sys->glwin == None)
309 msg_Err (vd, "cannot create GLX window");
313 /* Create an OpenGL context */
314 sys->ctx = glXCreateNewContext (dpy, conf, GLX_RGBA_TYPE, NULL,
316 if (sys->ctx == NULL)
318 msg_Err (vd, "cannot create GLX context");
322 if (!glXMakeContextCurrent (dpy, sys->glwin, sys->glwin, sys->ctx))
335 XVisualInfo *vi = glXChooseVisual (dpy, snum, attr);
338 msg_Err (vd, "cannot find GLX 1.2 visual" );
341 msg_Dbg (vd, "using GLX visual ID 0x%"PRIx32, (uint32_t)vi->visualid);
343 if (CreateWindow (vd, conn, depth, 0 /* ??? */, width, height) == 0)
344 sys->ctx = glXCreateContext (dpy, vi, 0, True);
346 if (sys->ctx == NULL)
348 msg_Err (vd, "cannot create GLX context");
352 if (glXMakeCurrent (dpy, sys->window, sys->ctx) == False)
354 sys->glwin = sys->window;
357 /* Initialize common OpenGL video display */
359 sys->gl.unlock = NULL;
360 sys->gl.swap = SwapBuffers;
363 if (vout_display_opengl_Init (&sys->vgl, &vd->fmt, &sys->gl))
369 sys->cursor = CreateBlankCursor (conn, scr);
370 sys->visible = false;
373 vout_display_info_t info = vd->info;
374 info.has_pictures_invalid = false;
376 /* Setup vout_display_t once everything is fine */
380 vd->prepare = PictureRender;
381 vd->display = PictureDisplay;
382 vd->control = Control;
386 vout_display_SendEventFullscreen (vd, false);
387 vout_display_SendEventDisplaySize (vd, width, height, false);
398 * Disconnect from the X server.
400 static void Close (vlc_object_t *obj)
402 vout_display_t *vd = (vout_display_t *)obj;
403 vout_display_sys_t *sys = vd->sys;
404 Display *dpy = sys->display;
406 if (sys->gl.sys != NULL)
407 vout_display_opengl_Clean (&sys->vgl);
409 if (sys->ctx != NULL)
413 glXMakeContextCurrent (dpy, None, None, NULL);
414 glXDestroyWindow (dpy, sys->glwin);
417 glXMakeCurrent (dpy, None, NULL);
418 glXDestroyContext (dpy, sys->ctx);
421 vout_display_DeleteWindow (vd, sys->embed);
425 static void SwapBuffers (vout_opengl_t *gl)
427 vout_display_sys_t *sys = gl->sys;
429 glXSwapBuffers (sys->display, sys->glwin);
433 * Return a direct buffer
435 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
437 vout_display_sys_t *sys = vd->sys;
438 (void)requested_count;
441 sys->pool = vout_display_opengl_GetPool (&sys->vgl);
445 static void PictureRender (vout_display_t *vd, picture_t *pic)
447 vout_display_sys_t *sys = vd->sys;
449 vout_display_opengl_Prepare (&sys->vgl, pic);
452 static void PictureDisplay (vout_display_t *vd, picture_t *pic)
454 vout_display_sys_t *sys = vd->sys;
456 vout_display_opengl_Display (&sys->vgl, &vd->source);
457 picture_Release (pic);
460 static int Control (vout_display_t *vd, int query, va_list ap)
462 vout_display_sys_t *sys = vd->sys;
466 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
468 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
469 return vout_window_SetFullScreen (sys->embed, c->is_fullscreen);
472 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
474 unsigned state = va_arg (ap, unsigned);
475 return vout_window_SetState (sys->embed, state);
478 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
479 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
480 case VOUT_DISPLAY_CHANGE_ZOOM:
481 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
482 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
484 xcb_connection_t *conn = XGetXCBConnection (sys->display);
485 const vout_display_cfg_t *cfg;
486 const video_format_t *source;
487 bool is_forced = false;
489 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
490 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
492 source = (const video_format_t *)va_arg (ap, const video_format_t *);
497 source = &vd->source;
498 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
499 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
500 is_forced = (bool)va_arg (ap, int);
504 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
506 && (cfg->display.width != vd->cfg->display.width
507 ||cfg->display.height != vd->cfg->display.height)
508 && vout_window_SetSize (sys->embed,
509 cfg->display.width, cfg->display.height))
512 vout_display_place_t place;
513 vout_display_PlacePicture (&place, source, cfg, false);
515 /* Move the picture within the window */
516 const uint32_t values[] = { place.x, place.y,
517 place.width, place.height, };
518 xcb_void_cookie_t ck =
519 xcb_configure_window_checked (conn, sys->window,
520 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
521 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
523 if (CheckError (vd, conn, "cannot resize X11 window", ck))
526 glViewport (0, 0, place.width, place.height);
530 /* Hide the mouse. It will be send when
531 * vout_display_t::info.b_hide_mouse is false */
532 case VOUT_DISPLAY_HIDE_MOUSE:
533 xcb_change_window_attributes (XGetXCBConnection (sys->display),
534 sys->embed->handle.xid,
535 XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
538 case VOUT_DISPLAY_GET_OPENGL:
540 vout_opengl_t **gl = va_arg (ap, vout_opengl_t **);
545 case VOUT_DISPLAY_RESET_PICTURES:
548 msg_Err (vd, "Unknown request in XCB vout display");
553 static void Manage (vout_display_t *vd)
555 vout_display_sys_t *sys = vd->sys;
556 xcb_connection_t *conn = XGetXCBConnection (sys->display);
558 ManageEvent (vd, conn, &sys->visible);