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")
61 struct vout_display_sys_t
63 Display *display; /* Xlib instance */
64 vout_window_t *embed; /* VLC window (when windowed) */
66 xcb_cursor_t cursor; /* blank cursor */
67 xcb_window_t window; /* drawable X window */
68 xcb_window_t glwin; /* GLX window */
69 bool visible; /* whether to draw */
70 bool v1_3; /* whether GLX >= 1.3 is available */
74 vout_display_opengl_t vgl;
75 picture_pool_t *pool; /* picture pool */
78 static picture_t *Get (vout_display_t *);
79 static void PictureRender (vout_display_t *, picture_t *);
80 static void PictureDisplay (vout_display_t *, picture_t *);
81 static int Control (vout_display_t *, int, va_list);
82 static void Manage (vout_display_t *);
84 static void SwapBuffers (vout_opengl_t *gl);
86 static vout_window_t *MakeWindow (vout_display_t *vd)
88 vout_window_cfg_t wnd_cfg;
90 memset (&wnd_cfg, 0, sizeof (wnd_cfg));
91 wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
92 wnd_cfg.width = vd->cfg->display.width;
93 wnd_cfg.height = vd->cfg->display.height;
95 vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
97 msg_Err (vd, "parent window not available");
101 static const xcb_screen_t *
102 FindWindow (vout_display_t *vd, xcb_connection_t *conn,
103 unsigned *restrict pnum, uint8_t *restrict pdepth)
105 vout_display_sys_t *sys = vd->sys;
107 xcb_get_geometry_reply_t *geo =
108 xcb_get_geometry_reply (conn,
109 xcb_get_geometry (conn, sys->embed->xid), NULL);
112 msg_Err (vd, "parent window not valid");
116 xcb_window_t root = geo->root;
117 *pdepth = geo->depth;
120 /* Find the selected screen */
121 const xcb_setup_t *setup = xcb_get_setup (conn);
122 const xcb_screen_t *screen = NULL;
125 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
127 xcb_screen_next (&i))
129 if (i.data->root == root)
139 msg_Err (vd, "parent window screen not found");
142 msg_Dbg (vd, "using screen 0x%"PRIx32 " (number: %u)", root, num);
147 static bool CheckGLX (vout_display_t *vd, Display *dpy, bool *restrict pv13)
152 if (!glXQueryVersion (dpy, &major, &minor))
153 msg_Dbg (vd, "GLX extension not available");
156 msg_Dbg (vd, "GLX extension version %d.%d unknown", major, minor);
159 msg_Dbg (vd, "GLX extension version %d.%d too old", major, minor);
162 msg_Dbg (vd, "using GLX extension version %d.%d", major, minor);
169 static int CreateWindow (vout_display_t *vd, xcb_connection_t *conn,
170 uint_fast8_t depth, xcb_visualid_t vid)
172 vout_display_sys_t *sys = vd->sys;
174 unsigned width, height;
175 if (GetWindowSize (sys->embed, conn, &width, &height))
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->xid, 0, 0, width, height, 0,
187 XCB_WINDOW_CLASS_INPUT_OUTPUT,
189 cm = xcb_map_window_checked (conn, sys->window);
190 if (CheckError (vd, conn, "cannot create X11 window", cc)
191 || CheckError (vd, conn, "cannot map X11 window", cm))
194 msg_Dbg (vd, "using X11 window %08"PRIx32, sys->window);
199 * Probe the X server.
201 static int Open (vlc_object_t *obj)
203 vout_display_t *vd = (vout_display_t *)obj;
204 vout_display_sys_t *sys = malloc (sizeof (*sys));
214 sys->embed = MakeWindow (vd);
215 if (sys->embed == NULL)
221 /* Connect to X server */
222 Display *dpy = XOpenDisplay (sys->embed->x11_display);
225 vout_display_DeleteWindow (vd, sys->embed);
231 XSetEventQueueOwner (dpy, XCBOwnsEventQueue);
233 if (!CheckGLX (vd, dpy, &sys->v1_3))
236 xcb_connection_t *conn = XGetXCBConnection (dpy);
238 RegisterMouseEvents (obj, conn, sys->embed->xid);
240 /* Find window parameters */
243 const xcb_screen_t *scr = FindWindow (vd, conn, &snum, &depth);
247 sys->window = xcb_generate_id (conn);
249 /* Determine our pixel format */
252 static const int attr[] = {
256 GLX_DOUBLEBUFFER, True,
257 GLX_X_RENDERABLE, True,
258 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
262 GLXFBConfig *confs = glXChooseFBConfig (dpy, snum, attr, &nelem);
265 msg_Err (vd, "no GLX frame bufer configurations");
269 /*XVisualInfo *vi = glXGetVisualFromFBConfig (dpy, confs[0]);*/
270 CreateWindow (vd, conn, depth, 0 /* ??? */);
273 sys->glwin = glXCreateWindow (dpy, confs[0], sys->window, NULL );
274 if (sys->glwin == None)
276 msg_Err (vd, "cannot create GLX window");
281 /* Create an OpenGL context */
282 sys->ctx = glXCreateNewContext (dpy, confs[0], GLX_RGBA_TYPE, NULL,
285 if (sys->ctx == NULL)
287 msg_Err (vd, "cannot create GLX context");
291 if (glXMakeContextCurrent (dpy, sys->glwin, sys->glwin, sys->ctx))
304 XVisualInfo *vi = glXChooseVisual (dpy, snum, attr);
307 msg_Err (vd, "cannot find GLX 1.2 visual" );
310 msg_Dbg (vd, "using GLX visual ID 0x%"PRIx32, (uint32_t)vi->visualid);
312 if (CreateWindow (vd, conn, depth, 0 /* ??? */) == 0)
313 sys->ctx = glXCreateContext (dpy, vi, 0, True);
315 if (sys->ctx == NULL)
317 msg_Err (vd, "cannot create GLX context");
321 if (glXMakeCurrent (dpy, sys->window, sys->ctx) == False)
323 sys->glwin = sys->window;
326 /* Initialize common OpenGL video display */
328 sys->gl.unlock = NULL;
329 sys->gl.swap = SwapBuffers;
332 if (vout_display_opengl_Init (&sys->vgl, &vd->fmt, &sys->gl))
338 sys->cursor = CreateBlankCursor (conn, scr);
339 sys->visible = false;
342 vout_display_info_t info = vd->info;
343 info.has_pictures_invalid = false;
345 /* Setup vout_display_t once everything is fine */
349 vd->prepare = PictureRender;
350 vd->display = PictureDisplay;
351 vd->control = Control;
355 vout_display_SendEventFullscreen (vd, false);
356 //vout_display_SendEventDisplaySize (vd, width, height, false);
367 * Disconnect from the X server.
369 static void Close (vlc_object_t *obj)
371 vout_display_t *vd = (vout_display_t *)obj;
372 vout_display_sys_t *sys = vd->sys;
373 Display *dpy = sys->display;
375 if (sys->gl.sys != NULL)
376 vout_display_opengl_Clean (&sys->vgl);
378 if (sys->ctx != NULL)
382 glXMakeContextCurrent (dpy, None, None, NULL);
383 glXDestroyWindow (dpy, sys->glwin);
386 glXMakeCurrent (dpy, None, NULL);
387 glXDestroyContext (dpy, sys->ctx);
390 vout_display_DeleteWindow (vd, sys->embed);
394 static void SwapBuffers (vout_opengl_t *gl)
396 vout_display_sys_t *sys = gl->sys;
398 glXSwapBuffers (sys->display, sys->glwin);
402 * Return a direct buffer
404 static picture_t *Get (vout_display_t *vd)
406 vout_display_sys_t *sys = vd->sys;
410 sys->pool = vout_display_opengl_GetPool (&sys->vgl);
414 return picture_pool_Get (sys->pool);
417 static void PictureRender (vout_display_t *vd, picture_t *pic)
419 vout_display_sys_t *sys = vd->sys;
421 vout_display_opengl_Prepare (&sys->vgl, pic);
424 static void PictureDisplay (vout_display_t *vd, picture_t *pic)
426 vout_display_sys_t *sys = vd->sys;
428 vout_display_opengl_Display (&sys->vgl, &vd->source);
429 picture_Release (pic);
432 static int Control (vout_display_t *vd, int query, va_list ap)
434 vout_display_sys_t *sys = vd->sys;
438 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
440 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
441 return vout_window_SetFullScreen (sys->embed, c->is_fullscreen);
444 case VOUT_DISPLAY_CHANGE_ON_TOP:
446 int b_on_top = (int)va_arg (ap, int);
447 return vout_window_SetOnTop (sys->embed, b_on_top);
450 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
451 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
452 case VOUT_DISPLAY_CHANGE_ZOOM:
453 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
454 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
455 msg_Err (vd, "unimplemented control request");
458 /* Hide the mouse. It will be send when
459 * vout_display_t::info.b_hide_mouse is false */
460 case VOUT_DISPLAY_HIDE_MOUSE:
461 xcb_change_window_attributes (XGetXCBConnection (sys->display),
463 XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
465 case VOUT_DISPLAY_RESET_PICTURES:
468 msg_Err (vd, "Unknown request in XCB vout display");
473 static void Manage (vout_display_t *vd)
475 vout_display_sys_t *sys = vd->sys;
476 xcb_connection_t *conn = XGetXCBConnection (sys->display);
478 ManageEvent (vd, conn, &sys->visible);