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 1.2.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 */
132 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
133 *end = fmt + xcb_setup_pixmap_formats_length (setup);
137 if (fmt->depth <= sys->depth)
138 continue; /* no better than earlier format */
140 video_format_t fmt_pic = vd->fmt;
142 /* Check that the pixmap format is supported by VLC. */
146 if (fmt->bits_per_pixel != 32)
148 #ifdef FIXED_VLC_RGBA_MASK
149 fmt_pic.i_chroma = VLC_CODEC_RGBA;
152 msg_Dbg (vd, "X11 visual with alpha-channel not supported");
156 if (fmt->bits_per_pixel == 32)
157 fmt_pic.i_chroma = VLC_CODEC_RGB32;
158 else if (fmt->bits_per_pixel == 24)
159 fmt_pic.i_chroma = VLC_CODEC_RGB24;
164 if (fmt->bits_per_pixel != 16)
166 fmt_pic.i_chroma = VLC_CODEC_RGB16;
169 if (fmt->bits_per_pixel != 16)
171 fmt_pic.i_chroma = VLC_CODEC_RGB15;
174 if (fmt->bits_per_pixel != 8)
176 fmt_pic.i_chroma = VLC_CODEC_RGB8;
182 /* Byte sex is a non-issue for 8-bits. It can be worked around with
183 * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
184 if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
187 /* Make sure the X server is sane */
188 assert (fmt->bits_per_pixel > 0);
189 if (unlikely(fmt->scanline_pad % fmt->bits_per_pixel))
192 /* Check that the selected screen supports this depth */
193 const xcb_depth_t *d = FindDepth (scr, fmt->depth);
197 /* Find a visual type for the selected depth */
198 const xcb_visualtype_t *vt = xcb_depth_visuals (d);
200 /* First try True Color class */
201 for (int i = xcb_depth_visuals_length (d); i > 0; i--)
203 if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
205 fmt_pic.i_rmask = vt->red_mask;
206 fmt_pic.i_gmask = vt->green_mask;
207 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;
310 vd->display = Display;
311 vd->control = Control;
315 bool is_fullscreen = vd->cfg->is_fullscreen;
316 if (is_fullscreen && vout_window_SetFullScreen (sys->embed, true))
317 is_fullscreen = false;
318 vout_display_SendEventFullscreen (vd, is_fullscreen);
319 vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
330 * Disconnect from the X server.
332 static void Close (vlc_object_t *obj)
334 vout_display_t *vd = (vout_display_t *)obj;
335 vout_display_sys_t *sys = vd->sys;
339 /* show the default cursor */
340 xcb_change_window_attributes (sys->conn, sys->embed->handle.xid, XCB_CW_CURSOR,
341 &(uint32_t) { XCB_CURSOR_NONE });
342 xcb_flush (sys->conn);
344 /* colormap, window and context are garbage-collected by X */
345 xcb_disconnect (sys->conn);
346 vout_display_DeleteWindow (vd, sys->embed);
351 * Return a direct buffer
353 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
355 vout_display_sys_t *sys = vd->sys;
356 (void)requested_count;
361 vout_display_place_t place;
363 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
366 const uint32_t values[] = { place.x, place.y, place.width, place.height };
367 xcb_configure_window (sys->conn, sys->window,
368 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
369 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
372 picture_t *pic = picture_NewFromFormat (&vd->fmt);
376 assert (pic->i_planes == 1);
377 memset (sys->resource, 0, sizeof(sys->resource));
380 picture_t *pic_array[MAX_PICTURES];
381 for (count = 0; count < MAX_PICTURES; count++)
383 picture_resource_t *res = &sys->resource[count];
385 res->p->i_lines = pic->p->i_lines;
386 res->p->i_pitch = pic->p->i_pitch;
387 if (PictureResourceAlloc (vd, res, res->p->i_pitch * res->p->i_lines,
388 sys->conn, sys->shm))
390 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
391 if (!pic_array[count])
393 PictureResourceFree (res, sys->conn);
394 memset (res, 0, sizeof(*res));
398 picture_Release (pic);
403 sys->pool = picture_pool_New (count, pic_array);
404 /* TODO release picture resources if NULL */
405 xcb_flush (sys->conn);
410 * Sends an image to the X server.
412 static void Display (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
414 vout_display_sys_t *sys = vd->sys;
415 xcb_shm_seg_t segment = pic->p_sys->segment;
416 xcb_void_cookie_t ck;
421 ck = xcb_shm_put_image_checked (sys->conn, sys->window, sys->gc,
422 /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
423 /* real height */ pic->p->i_lines,
424 /* x */ vd->fmt.i_x_offset,
425 /* y */ vd->fmt.i_y_offset,
426 /* width */ vd->fmt.i_visible_width,
427 /* height */ vd->fmt.i_visible_height,
428 0, 0, sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
432 const size_t offset = vd->fmt.i_y_offset * pic->p->i_pitch;
433 const unsigned lines = pic->p->i_lines - vd->fmt.i_y_offset;
435 ck = xcb_put_image_checked (sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
436 sys->window, sys->gc,
437 pic->p->i_pitch / pic->p->i_pixel_pitch,
438 lines, -vd->fmt.i_x_offset, 0, 0, sys->depth,
439 pic->p->i_pitch * lines, pic->p->p_pixels + offset);
442 /* Wait for reply. This makes sure that the X server gets CPU time to
443 * display the picture. xcb_flush() is *not* sufficient: especially with
444 * shared memory the PUT requests are so short that many of them can fit in
445 * X11 socket output buffer before the kernel preempts VLC. */
446 xcb_generic_error_t *e = xcb_request_check (sys->conn, ck);
449 msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
453 /* FIXME might be WAY better to wait in some case (be carefull with
454 * VOUT_DISPLAY_RESET_PICTURES if done) + does not work with
455 * vout_display wrapper. */
457 picture_Release (pic);
461 static int Control (vout_display_t *vd, int query, va_list ap)
463 vout_display_sys_t *sys = vd->sys;
467 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
469 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
470 return vout_window_SetFullScreen (sys->embed, c->is_fullscreen);
473 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
475 const vout_display_cfg_t *p_cfg =
476 (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
477 const bool is_forced = (bool)va_arg (ap, int);
480 && vout_window_SetSize (sys->embed,
481 p_cfg->display.width,
482 p_cfg->display.height))
485 vout_display_place_t place;
486 vout_display_PlacePicture (&place, &vd->source, p_cfg, false);
488 if (place.width != vd->fmt.i_visible_width ||
489 place.height != vd->fmt.i_visible_height)
491 vout_display_SendEventPicturesInvalid (vd);
495 /* Move the picture within the window */
496 const uint32_t values[] = { place.x, place.y };
497 xcb_configure_window (sys->conn, sys->window,
498 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
502 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
504 unsigned state = va_arg (ap, unsigned);
505 return vout_window_SetState (sys->embed, state);
508 case VOUT_DISPLAY_CHANGE_ZOOM:
509 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
510 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
511 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
512 /* I am not sure it is always necessary, but it is way simpler ... */
513 vout_display_SendEventPicturesInvalid (vd);
516 case VOUT_DISPLAY_RESET_PICTURES:
520 vout_display_place_t place;
521 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
523 vd->fmt.i_width = vd->source.i_width * place.width / vd->source.i_visible_width;
524 vd->fmt.i_height = vd->source.i_height * place.height / vd->source.i_visible_height;
526 vd->fmt.i_visible_width = place.width;
527 vd->fmt.i_visible_height = place.height;
528 vd->fmt.i_x_offset = vd->source.i_x_offset * place.width / vd->source.i_visible_width;
529 vd->fmt.i_y_offset = vd->source.i_y_offset * place.height / vd->source.i_visible_height;
533 /* Hide the mouse. It will be send when
534 * vout_display_t::info.b_hide_mouse is false */
535 case VOUT_DISPLAY_HIDE_MOUSE:
536 xcb_change_window_attributes (sys->conn, sys->embed->handle.xid,
537 XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
538 xcb_flush (sys->conn);
542 msg_Err (vd, "Unknown request in XCB vout display");
547 static void Manage (vout_display_t *vd)
549 vout_display_sys_t *sys = vd->sys;
551 ManageEvent (vd, sys->conn, &sys->visible);
554 static void ResetPictures (vout_display_t *vd)
556 vout_display_sys_t *sys = vd->sys;
561 for (unsigned i = 0; i < MAX_PICTURES; i++)
563 picture_resource_t *res = &sys->resource[i];
565 if (!res->p->p_pixels)
567 PictureResourceFree (res, sys->conn);
569 picture_pool_Delete (sys->pool);