3 * @brief X C Bindings video output module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_vout_display.h>
36 #include <vlc_picture_pool.h>
40 static int Open (vlc_object_t *);
41 static void Close (vlc_object_t *);
47 set_shortname (N_("X11"))
48 set_description (N_("X11 video output (XCB)"))
49 set_category (CAT_VIDEO)
50 set_subcategory (SUBCAT_VIDEO_VOUT)
51 set_capability ("vout display", 100)
52 set_callbacks (Open, Close)
53 add_shortcut ("xcb-x11", "x11", "xid")
55 add_obsolete_bool ("x11-shm") /* obsoleted since 2.0.0 */
58 /* This must be large enough to absorb the server display jitter.
59 * But excessively large value is useless as direct rendering cannot be used
61 #define MAX_PICTURES (3)
63 struct vout_display_sys_t
65 xcb_connection_t *conn;
66 vout_window_t *embed; /* VLC window */
68 xcb_cursor_t cursor; /* blank cursor */
69 xcb_window_t window; /* drawable X window */
70 xcb_gcontext_t gc; /* context to put images */
71 bool shm; /* whether to use MIT-SHM */
72 bool visible; /* whether to draw */
73 uint8_t depth; /* useful bits per pixel */
75 picture_pool_t *pool; /* picture pool */
76 picture_resource_t resource[MAX_PICTURES];
79 static picture_pool_t *Pool (vout_display_t *, unsigned);
80 static void Display (vout_display_t *, picture_t *, subpicture_t *subpicture);
81 static int Control (vout_display_t *, int, va_list);
82 static void Manage (vout_display_t *);
84 static void ResetPictures (vout_display_t *);
86 static const xcb_depth_t *FindDepth (const xcb_screen_t *scr,
89 xcb_depth_t *d = NULL;
90 for (xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
91 it.rem > 0 && d == NULL;
94 if (it.data->depth == depth)
103 * Probe the X server.
105 static int Open (vlc_object_t *obj)
107 vout_display_t *vd = (vout_display_t *)obj;
108 vout_display_sys_t *sys = malloc (sizeof (*sys));
109 if (unlikely(sys == NULL))
115 /* Get window, connect to X server */
116 xcb_connection_t *conn;
117 const xcb_screen_t *scr;
118 sys->embed = GetWindow (vd, &conn, &scr, &(uint8_t){ 0 });
119 if (sys->embed == NULL)
126 const xcb_setup_t *setup = xcb_get_setup (conn);
128 /* Determine our pixel format */
129 video_format_t fmt_pic;
133 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
134 *end = fmt + xcb_setup_pixmap_formats_length (setup);
138 if (fmt->depth <= sys->depth)
139 continue; /* no better than earlier format */
143 /* Check that the pixmap format is supported by VLC. */
147 if (fmt->bits_per_pixel != 32)
149 #ifdef FIXED_VLC_RGBA_MASK
150 fmt_pic.i_chroma = VLC_CODEC_RGBA;
153 msg_Dbg (vd, "X11 visual with alpha-channel not supported");
157 if (fmt->bits_per_pixel == 32)
158 fmt_pic.i_chroma = VLC_CODEC_RGB32;
159 else if (fmt->bits_per_pixel == 24)
160 fmt_pic.i_chroma = VLC_CODEC_RGB24;
165 if (fmt->bits_per_pixel != 16)
167 fmt_pic.i_chroma = VLC_CODEC_RGB16;
170 if (fmt->bits_per_pixel != 16)
172 fmt_pic.i_chroma = VLC_CODEC_RGB15;
175 if (fmt->bits_per_pixel != 8)
177 fmt_pic.i_chroma = VLC_CODEC_RGB8;
183 /* Byte sex is a non-issue for 8-bits. It can be worked around with
184 * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
185 if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
188 /* Make sure the X server is sane */
189 assert (fmt->bits_per_pixel > 0);
190 if (unlikely(fmt->scanline_pad % fmt->bits_per_pixel))
193 /* Check that the selected screen supports this depth */
194 const xcb_depth_t *d = FindDepth (scr, fmt->depth);
198 /* Find a visual type for the selected depth */
199 const xcb_visualtype_t *vt = xcb_depth_visuals (d);
201 /* First try True Color class */
202 for (int i = xcb_depth_visuals_length (d); i > 0; i--)
204 if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
206 fmt_pic.i_rmask = vt->red_mask;
207 fmt_pic.i_gmask = vt->green_mask;
208 fmt_pic.i_bmask = vt->blue_mask;
211 msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32, vid);
212 sys->depth = fmt->depth;
213 msg_Dbg (vd, " %"PRIu8" bits depth", sys->depth);
214 msg_Dbg (vd, " %"PRIu8" bits per pixel", fmt->bits_per_pixel);
215 msg_Dbg (vd, " %"PRIu8" bits line pad", fmt->scanline_pad);
221 /* Then try Static Gray class */
224 vt = xcb_depth_visuals (d);
225 for (int i = xcb_depth_visuals_length (d); i > 0 && !vid; i--)
227 if (vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
229 fmt_pic.i_chroma = VLC_CODEC_GREY;
236 msg_Err (obj, "no supported pixel format & visual");
240 /* Create colormap (needed to select non-default visual) */
242 if (vid != scr->root_visual)
244 cmap = xcb_generate_id (conn);
245 xcb_create_colormap (conn, XCB_COLORMAP_ALLOC_NONE,
246 cmap, scr->root, vid);
249 cmap = scr->default_colormap;
252 unsigned width, height;
253 if (GetWindowSize (sys->embed, conn, &width, &height))
256 sys->window = xcb_generate_id (conn);
257 sys->gc = xcb_generate_id (conn);
258 xcb_pixmap_t pixmap = xcb_generate_id (conn);
260 const uint32_t mask =
263 XCB_CW_BORDER_PIXMAP |
264 XCB_CW_BORDER_PIXEL |
267 const uint32_t values[] = {
268 /* XCB_CW_BACK_PIXMAP */
270 /* XCB_CW_BACK_PIXEL */
272 /* XCB_CW_BORDER_PIXMAP */
274 /* XCB_CW_BORDER_PIXEL */
276 /* XCB_CW_EVENT_MASK */
277 XCB_EVENT_MASK_VISIBILITY_CHANGE,
278 /* XCB_CW_COLORMAP */
283 xcb_create_pixmap (conn, sys->depth, pixmap, scr->root, 1, 1);
284 c = xcb_create_window_checked (conn, sys->depth, sys->window,
285 sys->embed->handle.xid, 0, 0,
287 XCB_WINDOW_CLASS_INPUT_OUTPUT,
289 xcb_map_window (conn, sys->window);
290 /* Create graphic context (I wonder why the heck do we need this) */
291 xcb_create_gc (conn, sys->gc, sys->window, 0, NULL);
293 if (CheckError (vd, conn, "cannot create X11 window", c))
296 msg_Dbg (vd, "using X11 window %08"PRIx32, sys->window);
297 msg_Dbg (vd, "using X11 graphic context %08"PRIx32, sys->gc);
299 sys->cursor = CreateBlankCursor (conn, scr);
300 sys->visible = false;
301 sys->shm = CheckSHM (obj, conn);
304 /* Setup vout_display_t once everything is fine */
305 vd->info.has_pictures_invalid = true;
306 vd->info.has_event_thread = true;
311 vd->display = Display;
312 vd->control = Control;
316 bool is_fullscreen = vd->cfg->is_fullscreen;
317 if (is_fullscreen && vout_window_SetFullScreen (sys->embed, true))
318 is_fullscreen = false;
319 vout_display_SendEventFullscreen (vd, is_fullscreen);
320 vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
331 * Disconnect from the X server.
333 static void Close (vlc_object_t *obj)
335 vout_display_t *vd = (vout_display_t *)obj;
336 vout_display_sys_t *sys = vd->sys;
340 /* show the default cursor */
341 xcb_change_window_attributes (sys->conn, sys->embed->handle.xid, XCB_CW_CURSOR,
342 &(uint32_t) { XCB_CURSOR_NONE });
343 xcb_flush (sys->conn);
345 /* colormap, window and context are garbage-collected by X */
346 xcb_disconnect (sys->conn);
347 vout_display_DeleteWindow (vd, sys->embed);
352 * Return a direct buffer
354 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
356 vout_display_sys_t *sys = vd->sys;
357 (void)requested_count;
362 vout_display_place_t place;
364 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
367 const uint32_t values[] = { place.x, place.y, place.width, place.height };
368 xcb_configure_window (sys->conn, sys->window,
369 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
370 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
373 picture_t *pic = picture_NewFromFormat (&vd->fmt);
377 assert (pic->i_planes == 1);
378 memset (sys->resource, 0, sizeof(sys->resource));
381 picture_t *pic_array[MAX_PICTURES];
382 for (count = 0; count < MAX_PICTURES; count++)
384 picture_resource_t *res = &sys->resource[count];
386 res->p->i_lines = pic->p->i_lines;
387 res->p->i_pitch = pic->p->i_pitch;
388 if (PictureResourceAlloc (vd, res, res->p->i_pitch * res->p->i_lines,
389 sys->conn, sys->shm))
391 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
392 if (!pic_array[count])
394 PictureResourceFree (res, sys->conn);
395 memset (res, 0, sizeof(*res));
399 picture_Release (pic);
404 sys->pool = picture_pool_New (count, pic_array);
405 /* TODO release picture resources if NULL */
406 xcb_flush (sys->conn);
411 * Sends an image to the X server.
413 static void Display (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
415 vout_display_sys_t *sys = vd->sys;
416 xcb_shm_seg_t segment = pic->p_sys->segment;
417 xcb_void_cookie_t ck;
422 ck = xcb_shm_put_image_checked (sys->conn, sys->window, sys->gc,
423 /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
424 /* real height */ pic->p->i_lines,
425 /* x */ vd->fmt.i_x_offset,
426 /* y */ vd->fmt.i_y_offset,
427 /* width */ vd->fmt.i_visible_width,
428 /* height */ vd->fmt.i_visible_height,
429 0, 0, sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
433 const size_t offset = vd->fmt.i_y_offset * pic->p->i_pitch;
434 const unsigned lines = pic->p->i_lines - vd->fmt.i_y_offset;
436 ck = xcb_put_image_checked (sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
437 sys->window, sys->gc,
438 pic->p->i_pitch / pic->p->i_pixel_pitch,
439 lines, -vd->fmt.i_x_offset, 0, 0, sys->depth,
440 pic->p->i_pitch * lines, pic->p->p_pixels + offset);
443 /* Wait for reply. This makes sure that the X server gets CPU time to
444 * display the picture. xcb_flush() is *not* sufficient: especially with
445 * shared memory the PUT requests are so short that many of them can fit in
446 * X11 socket output buffer before the kernel preempts VLC. */
447 xcb_generic_error_t *e = xcb_request_check (sys->conn, ck);
450 msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
454 /* FIXME might be WAY better to wait in some case (be carefull with
455 * VOUT_DISPLAY_RESET_PICTURES if done) + does not work with
456 * vout_display wrapper. */
458 picture_Release (pic);
462 static int Control (vout_display_t *vd, int query, va_list ap)
464 vout_display_sys_t *sys = vd->sys;
468 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
470 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
471 return vout_window_SetFullScreen (sys->embed, c->is_fullscreen);
474 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
476 const vout_display_cfg_t *p_cfg =
477 (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
478 const bool is_forced = (bool)va_arg (ap, int);
481 && vout_window_SetSize (sys->embed,
482 p_cfg->display.width,
483 p_cfg->display.height))
486 vout_display_place_t place;
487 vout_display_PlacePicture (&place, &vd->source, p_cfg, false);
489 if (place.width != vd->fmt.i_visible_width ||
490 place.height != vd->fmt.i_visible_height)
492 vout_display_SendEventPicturesInvalid (vd);
496 /* Move the picture within the window */
497 const uint32_t values[] = { place.x, place.y };
498 xcb_configure_window (sys->conn, sys->window,
499 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
503 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
505 unsigned state = va_arg (ap, unsigned);
506 return vout_window_SetState (sys->embed, state);
509 case VOUT_DISPLAY_CHANGE_ZOOM:
510 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
511 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
512 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
513 /* I am not sure it is always necessary, but it is way simpler ... */
514 vout_display_SendEventPicturesInvalid (vd);
517 case VOUT_DISPLAY_RESET_PICTURES:
521 vout_display_place_t place;
522 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
524 vd->fmt.i_width = vd->source.i_width * place.width / vd->source.i_visible_width;
525 vd->fmt.i_height = vd->source.i_height * place.height / vd->source.i_visible_height;
527 vd->fmt.i_visible_width = place.width;
528 vd->fmt.i_visible_height = place.height;
529 vd->fmt.i_x_offset = vd->source.i_x_offset * place.width / vd->source.i_visible_width;
530 vd->fmt.i_y_offset = vd->source.i_y_offset * place.height / vd->source.i_visible_height;
534 /* Hide the mouse. It will be send when
535 * vout_display_t::info.b_hide_mouse is false */
536 case VOUT_DISPLAY_HIDE_MOUSE:
537 xcb_change_window_attributes (sys->conn, sys->embed->handle.xid,
538 XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
539 xcb_flush (sys->conn);
543 msg_Err (vd, "Unknown request in XCB vout display");
548 static void Manage (vout_display_t *vd)
550 vout_display_sys_t *sys = vd->sys;
552 ManageEvent (vd, sys->conn, &sys->visible);
555 static void ResetPictures (vout_display_t *vd)
557 vout_display_sys_t *sys = vd->sys;
562 for (unsigned i = 0; i < MAX_PICTURES; i++)
564 picture_resource_t *res = &sys->resource[i];
566 if (!res->p->p_pixels)
568 PictureResourceFree (res, sys->conn);
570 picture_pool_Delete (sys->pool);