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 #define SHM_TEXT N_("Use shared memory")
41 #define SHM_LONGTEXT N_( \
42 "Use shared memory to communicate between VLC and the X server.")
44 static int Open (vlc_object_t *);
45 static void Close (vlc_object_t *);
51 set_shortname (N_("X11"))
52 set_description (N_("X11 video output (XCB)"))
53 set_category (CAT_VIDEO)
54 set_subcategory (SUBCAT_VIDEO_VOUT)
55 set_capability ("vout display", 75)
56 set_callbacks (Open, Close)
57 add_shortcut ("xcb-x11")
60 add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
63 /* It must be large enough to absorb the server display jitter but it is
64 * useless to used a too large value, direct rendering cannot be used with
67 #define MAX_PICTURES (3)
69 struct vout_display_sys_t
71 xcb_connection_t *conn;
72 vout_window_t *embed; /* VLC window (when windowed) */
74 xcb_cursor_t cursor; /* blank cursor */
75 xcb_window_t window; /* drawable X window */
76 xcb_gcontext_t gc; /* context to put images */
77 bool shm; /* whether to use MIT-SHM */
78 bool visible; /* whether to draw */
79 uint8_t bpp; /* bits per pixel */
80 uint8_t pad; /* scanline pad */
81 uint8_t depth; /* useful bits per pixel */
82 uint8_t byte_order; /* server byte order */
84 picture_pool_t *pool; /* picture pool */
85 picture_resource_t resource[MAX_PICTURES];
88 static picture_pool_t *Pool (vout_display_t *, unsigned);
89 static void Display (vout_display_t *, picture_t *);
90 static int Control (vout_display_t *, int, va_list);
91 static void Manage (vout_display_t *);
93 static void ResetPictures (vout_display_t *);
98 static int Open (vlc_object_t *obj)
100 vout_display_t *vd = (vout_display_t *)obj;
101 vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
108 /* Get window, connect to X server */
109 const xcb_screen_t *scr;
110 p_sys->embed = GetWindow (vd, &p_sys->conn, &scr, &p_sys->depth);
111 if (p_sys->embed == NULL)
117 const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
118 p_sys->byte_order = setup->image_byte_order;
121 video_format_t fmt_pic = vd->fmt;
123 /* Check that the selected screen supports this depth */
124 xcb_depth_t *d = NULL;
125 for (xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
126 it.rem > 0 && d == NULL;
127 xcb_depth_next (&it))
128 if (it.data->depth == p_sys->depth)
132 msg_Err (obj, "unexpected color depth msimatch");
133 goto error; /* WTH? depth not supported on screen */
136 /* Find a visual type for the selected depth */
137 xcb_visualid_t vid = 0;
139 const xcb_visualtype_t *vt = xcb_depth_visuals (d);
140 for (int i = xcb_depth_visuals_length (d); i > 0; i--)
142 if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
145 fmt_pic.i_rmask = vt->red_mask;
146 fmt_pic.i_gmask = vt->green_mask;
147 fmt_pic.i_bmask = vt->blue_mask;
151 if (p_sys->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY
160 msg_Err (obj, "unexpected visual type mismatch");
163 msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32" (depth: %"PRIu8")", vid,
166 /* Determine our pixel format */
168 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
169 *end = fmt + xcb_setup_pixmap_formats_length (setup);
170 fmt < end && !p_sys->bpp; fmt++)
172 if (fmt->depth != p_sys->depth)
173 continue; /* Wrong depth! */
175 /* Check that the pixmap format is supported by VLC. */
179 if (fmt->bits_per_pixel != 32)
181 fmt_pic.i_chroma = VLC_CODEC_RGBA;
184 if (fmt->bits_per_pixel == 32)
185 fmt_pic.i_chroma = VLC_CODEC_RGB32;
186 else if (fmt->bits_per_pixel == 24)
187 fmt_pic.i_chroma = VLC_CODEC_RGB24;
192 if (fmt->bits_per_pixel != 16)
194 fmt_pic.i_chroma = VLC_CODEC_RGB16;
197 if (fmt->bits_per_pixel != 16)
199 fmt_pic.i_chroma = VLC_CODEC_RGB15;
202 if (fmt->bits_per_pixel != 8)
204 fmt_pic.i_chroma = gray ? VLC_CODEC_GREY : VLC_CODEC_RGB8;
209 if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
210 continue; /* VLC pads lines to 16 pixels internally */
212 /* Byte sex is a non-issue for 8-bits. It can be worked around with
213 * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
214 if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
217 p_sys->bpp = fmt->bits_per_pixel;
218 p_sys->pad = fmt->scanline_pad;
219 msg_Dbg (vd, " %"PRIu8" bits per pixels, %"PRIu8" bits line pad",
220 p_sys->bpp, p_sys->pad);
225 msg_Err (vd, "no supported pixmap formats");
229 /* Create colormap (needed to select non-default visual) */
231 if (vid != scr->root_visual)
233 cmap = xcb_generate_id (p_sys->conn);
234 xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
235 cmap, scr->root, vid);
238 cmap = scr->default_colormap;
241 unsigned width, height;
242 if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
245 p_sys->window = xcb_generate_id (p_sys->conn);
246 p_sys->gc = xcb_generate_id (p_sys->conn);
248 const uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
249 const uint32_t values[] = {
250 /* XCB_CW_EVENT_MASK */
251 XCB_EVENT_MASK_VISIBILITY_CHANGE,
252 /* XCB_CW_COLORMAP */
257 c = xcb_create_window_checked (p_sys->conn, p_sys->depth,
259 p_sys->embed->xid, 0, 0,
261 XCB_WINDOW_CLASS_INPUT_OUTPUT,
263 xcb_map_window (p_sys->conn, p_sys->window);
264 /* Create graphic context (I wonder why the heck do we need this) */
265 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
267 if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
270 msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
271 msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
272 p_sys->cursor = CreateBlankCursor (p_sys->conn, scr);
274 p_sys->visible = false;
276 CheckSHM (obj, p_sys->conn, &p_sys->shm);
279 vout_display_info_t info = vd->info;
280 info.has_pictures_invalid = true;
282 /* Setup vout_display_t once everything is fine */
288 vd->display = Display;
289 vd->control = Control;
293 vout_display_SendEventFullscreen (vd, false);
294 vout_display_SendEventDisplaySize (vd, width, height, false);
305 * Disconnect from the X server.
307 static void Close (vlc_object_t *obj)
309 vout_display_t *vd = (vout_display_t *)obj;
310 vout_display_sys_t *p_sys = vd->sys;
313 /* colormap, window and context are garbage-collected by X */
314 xcb_disconnect (p_sys->conn);
315 vout_display_DeleteWindow (vd, p_sys->embed);
320 * Return a direct buffer
322 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
324 vout_display_sys_t *p_sys = vd->sys;
325 (void)requested_count;
329 vout_display_place_t place;
331 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
334 const uint32_t values[] = { place.x, place.y, place.width, place.height };
335 xcb_configure_window (p_sys->conn, p_sys->window,
336 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
337 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
340 picture_t *pic = picture_NewFromFormat (&vd->fmt);
344 assert (pic->i_planes == 1);
345 memset (p_sys->resource, 0, sizeof(p_sys->resource));
348 picture_t *pic_array[MAX_PICTURES];
349 for (count = 0; count < MAX_PICTURES; count++)
351 picture_resource_t *res = &p_sys->resource[count];
353 res->p->i_lines = pic->p->i_lines;
354 res->p->i_pitch = pic->p->i_pitch;
355 if (PictureResourceAlloc (vd, res, res->p->i_pitch * res->p->i_lines,
356 p_sys->conn, p_sys->shm))
358 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
359 if (!pic_array[count])
361 PictureResourceFree (res, p_sys->conn);
362 memset (res, 0, sizeof(*res));
366 picture_Release (pic);
371 p_sys->pool = picture_pool_New (count, pic_array);
372 /* TODO release picture resources if NULL */
373 xcb_flush (p_sys->conn);
380 * Sends an image to the X server.
382 static void Display (vout_display_t *vd, picture_t *pic)
384 vout_display_sys_t *p_sys = vd->sys;
385 xcb_shm_seg_t segment = pic->p_sys->segment;
386 xcb_void_cookie_t ck;
391 ck = xcb_shm_put_image_checked (p_sys->conn, p_sys->window, p_sys->gc,
392 /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
393 /* real height */ pic->p->i_lines,
394 /* x */ vd->fmt.i_x_offset,
395 /* y */ vd->fmt.i_y_offset,
396 /* width */ vd->fmt.i_visible_width,
397 /* height */ vd->fmt.i_visible_height,
398 0, 0, p_sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
402 const size_t offset = vd->fmt.i_y_offset * pic->p->i_pitch;
403 const unsigned lines = pic->p->i_lines - vd->fmt.i_y_offset;
405 ck = xcb_put_image_checked (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
406 p_sys->window, p_sys->gc,
407 pic->p->i_pitch / pic->p->i_pixel_pitch,
408 lines, -vd->fmt.i_x_offset, 0, 0, p_sys->depth,
409 pic->p->i_pitch * lines, pic->p->p_pixels + offset);
412 /* Wait for reply. This makes sure that the X server gets CPU time to
413 * display the picture. xcb_flush() is *not* sufficient: especially with
414 * shared memory the PUT requests are so short that many of them can fit in
415 * X11 socket output buffer before the kernel preempts VLC. */
416 xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
419 msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
423 /* FIXME might be WAY better to wait in some case (be carefull with
424 * VOUT_DISPLAY_RESET_PICTURES if done) + does not work with
425 * vout_display wrapper. */
427 picture_Release (pic);
430 static int Control (vout_display_t *vd, int query, va_list ap)
432 vout_display_sys_t *p_sys = vd->sys;
436 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
438 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
439 return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
442 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
444 const vout_display_cfg_t *p_cfg =
445 (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
446 const bool is_forced = (bool)va_arg (ap, int);
449 && vout_window_SetSize (p_sys->embed,
450 p_cfg->display.width,
451 p_cfg->display.height))
454 vout_display_place_t place;
455 vout_display_PlacePicture (&place, &vd->source, p_cfg, false);
457 if (place.width != vd->fmt.i_visible_width ||
458 place.height != vd->fmt.i_visible_height)
460 vout_display_SendEventPicturesInvalid (vd);
464 /* Move the picture within the window */
465 const uint32_t values[] = { place.x, place.y };
466 xcb_configure_window (p_sys->conn, p_sys->window,
467 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
471 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
473 unsigned state = va_arg (ap, unsigned);
474 return vout_window_SetState (p_sys->embed, state);
477 case VOUT_DISPLAY_CHANGE_ZOOM:
478 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
479 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
480 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
481 /* I am not sure it is always necessary, but it is way simpler ... */
482 vout_display_SendEventPicturesInvalid (vd);
485 case VOUT_DISPLAY_RESET_PICTURES:
489 vout_display_place_t place;
490 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
492 vd->fmt.i_width = vd->source.i_width * place.width / vd->source.i_visible_width;
493 vd->fmt.i_height = vd->source.i_height * place.height / vd->source.i_visible_height;
495 vd->fmt.i_visible_width = place.width;
496 vd->fmt.i_visible_height = place.height;
497 vd->fmt.i_x_offset = vd->source.i_x_offset * place.width / vd->source.i_visible_width;
498 vd->fmt.i_y_offset = vd->source.i_y_offset * place.height / vd->source.i_visible_height;
502 /* Hide the mouse. It will be send when
503 * vout_display_t::info.b_hide_mouse is false */
504 case VOUT_DISPLAY_HIDE_MOUSE:
505 xcb_change_window_attributes (p_sys->conn, p_sys->embed->xid,
506 XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
510 msg_Err (vd, "Unknown request in XCB vout display");
515 static void Manage (vout_display_t *vd)
517 vout_display_sys_t *p_sys = vd->sys;
519 ManageEvent (vd, p_sys->conn, &p_sys->visible);
522 static void ResetPictures (vout_display_t *vd)
524 vout_display_sys_t *p_sys = vd->sys;
529 for (unsigned i = 0; i < MAX_PICTURES; i++)
531 picture_resource_t *res = &p_sys->resource[i];
533 if (!res->p->p_pixels)
535 PictureResourceFree (res, p_sys->conn);
537 picture_pool_Delete (p_sys->pool);