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.0
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 Lesser 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 DISPLAY_TEXT N_("X11 display")
41 #define DISPLAY_LONGTEXT N_( \
42 "X11 hardware display to use. By default VLC will " \
43 "use the value of the DISPLAY environment variable.")
45 #define SHM_TEXT N_("Use shared memory")
46 #define SHM_LONGTEXT N_( \
47 "Use shared memory to communicate between VLC and the X server.")
49 static int Open (vlc_object_t *);
50 static void Close (vlc_object_t *);
56 set_shortname (N_("X11"))
57 set_description (N_("X11 video output (XCB)"))
58 set_category (CAT_VIDEO)
59 set_subcategory (SUBCAT_VIDEO_VOUT)
60 set_capability ("vout display", 75)
61 set_callbacks (Open, Close)
63 add_string ("x11-display", NULL, NULL,
64 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
65 add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
68 /* It must be large enough to absorb the server display jitter but it is
69 * useless to used a too large value, direct rendering cannot be used with
72 #define MAX_PICTURES (3)
74 struct vout_display_sys_t
76 xcb_connection_t *conn;
77 vout_window_t *embed; /* VLC window (when windowed) */
79 xcb_window_t window; /* drawable X window */
80 xcb_gcontext_t gc; /* context to put images */
81 bool shm; /* whether to use MIT-SHM */
82 uint8_t bpp; /* bits per pixel */
83 uint8_t pad; /* scanline pad */
84 uint8_t depth; /* useful bits per pixel */
85 uint8_t byte_order; /* server byte order */
87 picture_pool_t *pool; /* picture pool */
88 picture_resource_t resource[MAX_PICTURES];
91 static picture_t *Get (vout_display_t *);
92 static void Display (vout_display_t *, picture_t *);
93 static int Control (vout_display_t *, int, va_list);
94 static void Manage (vout_display_t *);
96 static void ResetPictures (vout_display_t *);
101 static int Open (vlc_object_t *obj)
103 vout_display_t *vd = (vout_display_t *)obj;
104 vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
112 p_sys->conn = Connect (obj);
113 if (p_sys->conn == NULL)
120 const xcb_screen_t *scr;
121 p_sys->embed = GetWindow (vd, p_sys->conn, &scr, &p_sys->shm);
122 if (p_sys->embed == NULL)
124 xcb_disconnect (p_sys->conn);
129 const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
130 p_sys->byte_order = setup->image_byte_order;
133 video_format_t fmt_pic = vd->fmt;
135 /* Determine our video format. */
136 xcb_visualid_t vid = 0;
139 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
140 *end = fmt + xcb_setup_pixmap_formats_length (setup);
143 vlc_fourcc_t chroma = 0;
145 if (fmt->depth < depth)
146 continue; /* We already found a better format! */
148 /* Check that the pixmap format is supported by VLC. */
152 if (fmt->bits_per_pixel == 32)
153 chroma = VLC_CODEC_RGB32;
154 else if (fmt->bits_per_pixel == 24)
155 chroma = VLC_CODEC_RGB24;
160 if (fmt->bits_per_pixel != 16)
162 chroma = VLC_CODEC_RGB16;
165 if (fmt->bits_per_pixel != 16)
167 chroma = VLC_CODEC_RGB15;
170 if (fmt->bits_per_pixel != 8)
172 chroma = VLC_CODEC_RGB8;
177 if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
178 continue; /* VLC pads lines to 16 pixels internally */
180 /* Byte sex is a non-issue for 8-bits. It can be worked around with
181 * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
182 if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
185 /* Check that the selected screen supports this depth */
186 xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
187 while (it.rem > 0 && it.data->depth != fmt->depth)
188 xcb_depth_next (&it);
190 continue; /* Depth not supported on this screen */
192 /* Find a visual type for the selected depth */
193 const xcb_visualtype_t *vt = xcb_depth_visuals (it.data);
194 for (int i = xcb_depth_visuals_length (it.data); i > 0; i--)
196 if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
201 if (fmt->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
204 continue; /* Prefer color over gray scale */
205 chroma = VLC_CODEC_GREY;
209 continue; /* The screen does not *really* support this depth */
212 fmt_pic.i_chroma = chroma;
216 fmt_pic.i_rmask = vt->red_mask;
217 fmt_pic.i_gmask = vt->green_mask;
218 fmt_pic.i_bmask = vt->blue_mask;
220 p_sys->bpp = fmt->bits_per_pixel;
221 p_sys->pad = fmt->scanline_pad;
222 p_sys->depth = depth = fmt->depth;
227 msg_Err (vd, "no supported pixmap formats or visual types");
231 msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32" (depth: %"PRIu8")", vid,
233 msg_Dbg (vd, " %"PRIu8" bits per pixels, %"PRIu8" bits line pad",
234 p_sys->bpp, p_sys->pad);
236 /* Create colormap (needed to select non-default visual) */
238 if (vid != scr->root_visual)
240 cmap = xcb_generate_id (p_sys->conn);
241 xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
242 cmap, scr->root, vid);
245 cmap = scr->default_colormap;
248 unsigned width, height;
249 if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
252 p_sys->window = xcb_generate_id (p_sys->conn);
253 p_sys->gc = xcb_generate_id (p_sys->conn);
255 const uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
256 const uint32_t values[] = {
257 /* XCB_CW_EVENT_MASK */
258 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
259 XCB_EVENT_MASK_POINTER_MOTION,
260 /* XCB_CW_COLORMAP */
265 c = xcb_create_window_checked (p_sys->conn, depth, p_sys->window,
266 p_sys->embed->handle.xid, 0, 0,
268 XCB_WINDOW_CLASS_INPUT_OUTPUT,
270 xcb_map_window (p_sys->conn, p_sys->window);
271 /* Create graphic context (I wonder why the heck do we need this) */
272 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
274 if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
277 msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
278 msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
281 vout_display_info_t info = vd->info;
282 info.has_pictures_invalid = true;
284 /* Setup vout_display_t once everything is fine */
290 vd->display = Display;
291 vd->control = Control;
295 vout_display_SendEventDisplaySize (vd, width, height);
296 vout_display_SendEventFullscreen (vd, false);
307 * Disconnect from the X server.
309 static void Close (vlc_object_t *obj)
311 vout_display_t *vd = (vout_display_t *)obj;
312 vout_display_sys_t *p_sys = vd->sys;
315 vout_display_DeleteWindow (vd, p_sys->embed);
316 /* colormap, window and context are garbage-collected by X */
317 xcb_disconnect (p_sys->conn);
322 * Return a direct buffer
324 static picture_t *Get (vout_display_t *vd)
326 vout_display_sys_t *p_sys = vd->sys;
330 vout_display_place_t place;
332 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
335 const uint32_t values[] = { place.x, place.y, place.width, place.height };
336 xcb_configure_window (p_sys->conn, p_sys->window,
337 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
338 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
341 picture_t *pic = picture_NewFromFormat (&vd->fmt);
345 assert (pic->i_planes == 1);
346 memset (p_sys->resource, 0, sizeof(p_sys->resource));
349 picture_t *pic_array[MAX_PICTURES];
350 for (count = 0; count < MAX_PICTURES; count++)
352 picture_resource_t *res = &p_sys->resource[count];
354 res->p->i_lines = pic->p->i_lines;
355 res->p->i_pitch = pic->p->i_pitch;
356 if (PictureResourceAlloc (vd, res, res->p->i_pitch * res->p->i_lines,
357 p_sys->conn, p_sys->shm))
359 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
360 if (!pic_array[count])
362 PictureResourceFree (res, p_sys->conn);
363 memset (res, 0, sizeof(*res));
367 picture_Release (pic);
372 p_sys->pool = picture_pool_New (count, pic_array);
375 /* TODO release picture resources */
378 /* FIXME should also do it in case of error ? */
379 xcb_flush (p_sys->conn);
382 return picture_pool_Get (p_sys->pool);
386 * Sends an image to the X server.
388 static void Display (vout_display_t *vd, picture_t *pic)
390 vout_display_sys_t *p_sys = vd->sys;
391 xcb_shm_seg_t segment = pic->p_sys->segment;
392 xcb_void_cookie_t ck;
395 ck = xcb_shm_put_image_checked (p_sys->conn, p_sys->window, p_sys->gc,
396 /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
397 /* real height */ pic->p->i_lines,
398 /* x */ vd->fmt.i_x_offset,
399 /* y */ vd->fmt.i_y_offset,
400 /* width */ vd->fmt.i_visible_width,
401 /* height */ vd->fmt.i_visible_height,
402 0, 0, p_sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
406 const size_t offset = vd->fmt.i_y_offset * pic->p->i_pitch;
407 const unsigned lines = pic->p->i_lines - vd->fmt.i_y_offset;
409 ck = xcb_put_image_checked (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
410 p_sys->window, p_sys->gc,
411 pic->p->i_pitch / pic->p->i_pixel_pitch,
412 lines, -vd->fmt.i_x_offset, 0, 0, p_sys->depth,
413 pic->p->i_pitch * lines, pic->p->p_pixels + offset);
416 /* Wait for reply. This makes sure that the X server gets CPU time to
417 * display the picture. xcb_flush() is *not* sufficient: especially with
418 * shared memory the PUT requests are so short that many of them can fit in
419 * X11 socket output buffer before the kernel preempts VLC. */
420 xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
423 msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
427 /* FIXME might be WAY better to wait in some case (be carefull with
428 * VOUT_DISPLAY_RESET_PICTURES if done) + does not work with
429 * vout_display wrapper. */
430 picture_Release (pic);
433 static int Control (vout_display_t *vd, int query, va_list ap)
435 vout_display_sys_t *p_sys = vd->sys;
439 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
441 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
442 return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
445 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
447 const vout_display_cfg_t *p_cfg =
448 (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
450 if (vout_window_SetSize (p_sys->embed,
451 p_cfg->display.width,
452 p_cfg->display.height))
455 vout_display_place_t place;
456 vout_display_PlacePicture (&place, &vd->source, p_cfg, false);
458 if (place.width != vd->fmt.i_visible_width ||
459 place.height != vd->fmt.i_visible_height)
461 vout_display_SendEventPicturesInvalid (vd);
465 /* Move the picture within the window */
466 const uint32_t values[] = { place.x, place.y };
467 xcb_configure_window (p_sys->conn, p_sys->window,
468 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
472 case VOUT_DISPLAY_CHANGE_ON_TOP:
474 int b_on_top = (int)va_arg (ap, int);
475 return vout_window_SetOnTop (p_sys->embed, b_on_top);
478 case VOUT_DISPLAY_CHANGE_ZOOM:
479 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
480 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
481 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
482 /* I am not sure it is always necessary, but it is way simpler ... */
483 vout_display_SendEventPicturesInvalid (vd);
486 case VOUT_DISPLAY_RESET_PICTURES:
490 vout_display_place_t place;
491 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
493 vd->fmt.i_width = vd->source.i_width * place.width / vd->source.i_visible_width;
494 vd->fmt.i_height = vd->source.i_height * place.height / vd->source.i_visible_height;
496 vd->fmt.i_visible_width = place.width;
497 vd->fmt.i_visible_height = place.height;
498 vd->fmt.i_x_offset = vd->source.i_x_offset * place.width / vd->source.i_visible_width;
499 vd->fmt.i_y_offset = vd->source.i_y_offset * place.height / vd->source.i_visible_height;
505 /* Hide the mouse. It will be send when
506 * vout_display_t::info.b_hide_mouse is false */
507 VOUT_DISPLAY_HIDE_MOUSE,
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->window);
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);