3 * @brief GLX video output module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2004 VLC authors and VideoLAN
7 * Copyright © 2009 Rémi Denis-Courmont
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
33 #include <GL/glxext.h>
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_vout_display.h>
39 #include <vlc_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_("OpenGL GLX video output (XCB)"))
53 set_category (CAT_VIDEO)
54 set_subcategory (SUBCAT_VIDEO_VOUT)
55 set_capability ("vout display", 150)
56 set_callbacks (Open, Close)
58 add_shortcut ("xcb-glx", "glx", "opengl", "xid")
61 struct vout_display_sys_t
63 Display *display; /* Xlib instance */
64 xcb_connection_t *conn; /**< XCB connection */
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 */
74 vout_display_opengl_t *vgl;
75 picture_pool_t *pool; /* picture pool */
78 static picture_pool_t *Pool (vout_display_t *, unsigned);
79 static void PictureRender (vout_display_t *, picture_t *, subpicture_t *);
80 static void PictureDisplay (vout_display_t *, picture_t *, subpicture_t *);
81 static int Control (vout_display_t *, int, va_list);
82 static void Manage (vout_display_t *);
84 static void SwapBuffers (vlc_gl_t *gl);
85 static void *GetProcAddress (vlc_gl_t *gl, const char *);
87 static unsigned GetScreenNumber (xcb_connection_t *conn,
88 const xcb_screen_t *screen)
90 const xcb_setup_t *setup = xcb_get_setup (conn);
93 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);;
96 if (i.data->root == screen->root)
102 static bool CheckGLX (vout_display_t *vd, Display *dpy)
107 if (!glXQueryVersion (dpy, &major, &minor))
108 msg_Dbg (vd, "GLX extension not available");
111 msg_Dbg (vd, "GLX extension version %d.%d unknown", major, minor);
114 msg_Dbg (vd, "GLX extension version %d.%d too old", major, minor);
117 msg_Dbg (vd, "using GLX extension version %d.%d", major, minor);
123 static int CreateWindow (vout_display_t *vd, xcb_connection_t *conn,
124 const xcb_screen_t *screen,
125 uint_fast16_t width, uint_fast16_t height)
127 vout_display_sys_t *sys = vd->sys;
128 xcb_pixmap_t pixmap = xcb_generate_id (conn);
129 const uint32_t mask =
132 XCB_CW_BORDER_PIXMAP |
133 XCB_CW_BORDER_PIXEL |
136 const uint32_t values[] = {
141 XCB_EVENT_MASK_VISIBILITY_CHANGE,
142 screen->default_colormap,
144 xcb_void_cookie_t cc, cm;
146 xcb_create_pixmap (conn, screen->root_depth, pixmap, screen->root, 1, 1);
147 cc = xcb_create_window_checked (conn, screen->root_depth, sys->window,
148 sys->embed->handle.xid, 0, 0,
150 XCB_WINDOW_CLASS_INPUT_OUTPUT,
151 screen->root_visual, mask, values);
152 cm = xcb_map_window_checked (conn, sys->window);
153 if (XCB_error_Check (vd, conn, "cannot create X11 window", cc)
154 || XCB_error_Check (vd, conn, "cannot map X11 window", cm))
157 msg_Dbg (vd, "using X11 window %08"PRIx32, sys->window);
162 * Probe the X server.
164 static int Open (vlc_object_t *obj)
166 if (!vlc_xlib_init (obj))
169 vout_display_t *vd = (vout_display_t *)obj;
170 vout_display_sys_t *sys = malloc (sizeof (*sys));
179 /* Get window, connect to X server (via XCB) */
180 xcb_connection_t *conn;
181 const xcb_screen_t *scr;
182 uint16_t width, height;
183 sys->embed = XCB_parent_Create (vd, &conn, &scr, &width, &height);
184 if (sys->embed == NULL)
189 const unsigned snum = GetScreenNumber (conn, scr);
193 Display *dpy = XOpenDisplay (sys->embed->display.x11);
196 xcb_disconnect (conn);
197 vout_display_DeleteWindow (vd, sys->embed);
204 if (!CheckGLX (vd, dpy))
207 sys->window = xcb_generate_id (conn);
209 /* Determine our pixel format */
210 static const int attr[] = {
214 GLX_DOUBLEBUFFER, True,
215 GLX_X_RENDERABLE, True,
216 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
220 xcb_get_window_attributes_reply_t *wa =
221 xcb_get_window_attributes_reply (conn,
222 xcb_get_window_attributes (conn, sys->embed->handle.xid), NULL);
225 xcb_visualid_t visual = wa->visual;
229 GLXFBConfig *confs = glXChooseFBConfig (dpy, snum, attr, &nelem);
232 msg_Err (vd, "no GLX frame buffer configurations");
239 for (int i = 0; i < nelem && !found; i++)
243 XVisualInfo *vi = glXGetVisualFromFBConfig (dpy, conf);
247 if (vi->visualid == visual)
255 msg_Err (vd, "no matching GLX frame buffer configuration");
260 if (!CreateWindow (vd, conn, scr, width, height))
261 sys->glwin = glXCreateWindow (dpy, conf, sys->window, NULL );
262 if (sys->glwin == None)
264 msg_Err (vd, "cannot create GLX window");
268 /* Create an OpenGL context */
269 sys->ctx = glXCreateNewContext (dpy, conf, GLX_RGBA_TYPE, NULL, True);
270 if (sys->ctx == NULL)
272 msg_Err (vd, "cannot create GLX context");
276 if (!glXMakeContextCurrent (dpy, sys->glwin, sys->glwin, sys->ctx))
279 const char *glx_extensions = glXQueryExtensionsString (dpy, snum);
281 bool is_swap_interval_set = false;
282 #ifdef GLX_SGI_swap_control
283 if (HasExtension (glx_extensions, "GLX_SGI_swap_control")) {
284 PFNGLXSWAPINTERVALSGIPROC SwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)GetProcAddress (NULL, "glXSwapIntervalSGI");
285 if (!is_swap_interval_set && SwapIntervalSGI)
286 is_swap_interval_set = !SwapIntervalSGI (1);
289 #ifdef GLX_EXT_swap_control
290 if (HasExtension (glx_extensions, "GLX_EXT_swap_control")) {
291 PFNGLXSWAPINTERVALEXTPROC SwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)GetProcAddress (NULL, "glXSwapIntervalEXT");
292 if (!is_swap_interval_set && SwapIntervalEXT)
294 SwapIntervalEXT (dpy, sys->glwin, 1);
295 is_swap_interval_set = true;
300 /* Initialize common OpenGL video display */
302 sys->gl.unlock = NULL;
303 sys->gl.swap = SwapBuffers;
304 sys->gl.getProcAddress = GetProcAddress;
307 vout_display_info_t info = vd->info;
308 info.has_pictures_invalid = false;
309 info.has_event_thread = true;
311 sys->vgl = vout_display_opengl_New (&vd->fmt, &info.subpicture_chromas,
319 sys->cursor = XCB_cursor_Create (conn, scr);
320 sys->visible = false;
322 /* Setup vout_display_t once everything is fine */
325 vd->prepare = PictureRender;
326 vd->display = PictureDisplay;
327 vd->control = Control;
331 bool is_fullscreen = vd->cfg->is_fullscreen;
332 if (is_fullscreen && vout_window_SetFullScreen (sys->embed, true))
333 is_fullscreen = false;
334 vout_display_SendEventFullscreen (vd, is_fullscreen);
335 vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
346 * Disconnect from the X server.
348 static void Close (vlc_object_t *obj)
350 vout_display_t *vd = (vout_display_t *)obj;
351 vout_display_sys_t *sys = vd->sys;
352 Display *dpy = sys->display;
354 if (sys->gl.sys != NULL)
355 vout_display_opengl_Delete (sys->vgl);
357 if (sys->ctx != NULL)
359 glXMakeContextCurrent (dpy, None, None, NULL);
360 glXDestroyContext (dpy, sys->ctx);
361 glXDestroyWindow (dpy, sys->glwin);
365 /* show the default cursor */
366 xcb_change_window_attributes (sys->conn, sys->embed->handle.xid,
367 XCB_CW_CURSOR, &(uint32_t) { XCB_CURSOR_NONE });
368 xcb_flush (sys->conn);
369 xcb_disconnect (sys->conn);
371 vout_display_DeleteWindow (vd, sys->embed);
375 static void SwapBuffers (vlc_gl_t *gl)
377 vout_display_sys_t *sys = gl->sys;
379 glXSwapBuffers (sys->display, sys->glwin);
382 static void *GetProcAddress (vlc_gl_t *gl, const char *name)
385 #ifdef GLX_ARB_get_proc_address
386 return glXGetProcAddressARB ((const GLubyte *)name);
393 * Return a direct buffer
395 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
397 vout_display_sys_t *sys = vd->sys;
400 sys->pool = vout_display_opengl_GetPool (sys->vgl, requested_count);
404 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
406 vout_display_sys_t *sys = vd->sys;
408 vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
411 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
413 vout_display_sys_t *sys = vd->sys;
415 vout_display_opengl_Display (sys->vgl, &vd->source);
416 picture_Release (pic);
418 subpicture_Delete(subpicture);
421 static int Control (vout_display_t *vd, int query, va_list ap)
423 vout_display_sys_t *sys = vd->sys;
427 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
429 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
430 return vout_window_SetFullScreen (sys->embed, c->is_fullscreen);
433 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
435 unsigned state = va_arg (ap, unsigned);
436 return vout_window_SetState (sys->embed, state);
439 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
440 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
441 case VOUT_DISPLAY_CHANGE_ZOOM:
442 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
443 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
445 const vout_display_cfg_t *cfg;
446 const video_format_t *source;
448 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
449 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
451 source = (const video_format_t *)va_arg (ap, const video_format_t *);
456 source = &vd->source;
457 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
461 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && va_arg (ap, int))
463 vout_window_SetSize (sys->embed,
464 cfg->display.width, cfg->display.height);
465 return VLC_EGENERIC; /* Always fail. See x11.c for rationale. */
468 vout_display_place_t place;
469 vout_display_PlacePicture (&place, source, cfg, false);
471 /* Move the picture within the window */
472 const uint32_t values[] = { place.x, place.y,
473 place.width, place.height, };
474 xcb_void_cookie_t ck =
475 xcb_configure_window_checked (sys->conn, sys->window,
476 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
477 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
479 if (XCB_error_Check (vd, sys->conn, "cannot resize X11 window", ck))
482 glViewport (0, 0, place.width, place.height);
486 /* Hide the mouse. It will be send when
487 * vout_display_t::info.b_hide_mouse is false */
488 case VOUT_DISPLAY_HIDE_MOUSE:
489 xcb_change_window_attributes (sys->conn, sys->embed->handle.xid,
490 XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
491 xcb_flush (sys->conn);
494 case VOUT_DISPLAY_GET_OPENGL:
496 vlc_gl_t **gl = va_arg (ap, vlc_gl_t **);
501 case VOUT_DISPLAY_RESET_PICTURES:
504 msg_Err (vd, "Unknown request in XCB vout display");
509 static void Manage (vout_display_t *vd)
511 vout_display_sys_t *sys = vd->sys;
513 XCB_Manage (vd, sys->conn, &sys->visible);