#include <stdlib.h>
#include <assert.h>
+#include <sys/types.h>
+#include <sys/shm.h>
+
#include <xcb/xcb.h>
+#include <xcb/shm.h>
+
#include <xcb/xcb_aux.h>
#include <xcb/xcb_image.h>
#include <vlc_vout.h>
#include <vlc_window.h>
+#include "xcb_vlc.h"
+
#define DISPLAY_TEXT N_("X11 display")
#define DISPLAY_LONGTEXT N_( \
"X11 hardware display to use. By default VLC will " \
"use the value of the DISPLAY environment variable.")
+#define SHM_TEXT N_("Use shared memory")
+#define SHM_LONGTEXT N_( \
+ "Use shared memory to communicate between VLC and the X server.")
+
static int Open (vlc_object_t *);
static void Close (vlc_object_t *);
* Module descriptor
*/
vlc_module_begin ()
- set_shortname (_("XCB"))
- set_description (_("(Experimental) XCB video output"))
+ set_shortname (N_("XCB"))
+ set_description (N_("(Experimental) XCB video output"))
set_category (CAT_VIDEO)
set_subcategory (SUBCAT_VIDEO_VOUT)
set_capability ("video output", 0)
add_string ("x11-display", NULL, NULL,
DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
+ add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
vlc_module_end ()
struct vout_sys_t
xcb_screen_t *screen;
vout_window_t *embed; /* VLC window (when windowed) */
- xcb_visualid_t vid;
+ xcb_visualid_t vid; /* selected visual */
xcb_window_t parent; /* parent X window */
+ xcb_colormap_t cmap; /* colormap for selected visual */
xcb_window_t window; /* drawable X window */
xcb_gcontext_t gc; /* context to put images */
+ bool shm; /* whether to use MIT-SHM */
+ uint8_t bpp; /* bits per pixel */
};
static int Init (vout_thread_t *);
static void Deinit (vout_thread_t *);
+static void Render (vout_thread_t *, picture_t *);
static void Display (vout_thread_t *, picture_t *);
static int Manage (vout_thread_t *);
-static int CheckError (vout_thread_t *vout, const char *str,
- xcb_void_cookie_t ck)
+int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
{
xcb_generic_error_t *err;
{
msg_Err (vout, "cannot connect to X server %s",
display ? display : "");
+ free (display);
goto error;
}
+ free (display);
/* Get the preferred screen */
xcb_screen_t *scr = xcb_aux_get_screen (p_sys->conn, snum);
assert (p_sys->screen);
msg_Dbg (vout, "using screen %d (depth %"PRIu8")", snum, scr->root_depth);
- if (strchr ("\x08\x0f\x10\x18\x20", scr->root_depth) == NULL)
- {
- msg_Err (vout, "unsupported %"PRIu8"-bits color depth",
- scr->root_depth);
- goto error;
- }
-
/* Determine the visual (color depth and palette) */
xcb_visualtype_t *vt = NULL;
+ bool gray = false;
+
if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_TRUE_COLOR,
scr->root_depth)) != NULL)
msg_Dbg (vout, "using TrueColor visual ID %d", (int)vt->visual_id);
else
- if ((vt = xcb_aux_find_visual_by_attrs (scr,XCB_VISUAL_CLASS_STATIC_COLOR,
+#if 0
+ if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_STATIC_COLOR,
scr->root_depth)) != NULL)
msg_Dbg (vout, "using static color visual ID %d", (int)vt->visual_id);
else
+#endif
+ if ((scr->root_depth == 8)
+ && (vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_STATIC_GRAY,
+ scr->root_depth)) != NULL)
{
- vt = xcb_aux_get_visualtype (p_sys->conn, snum, scr->root_visual);
+ msg_Dbg (vout, "using static gray visual ID %d", (int)vt->visual_id);
+ gray = true;
+ }
+ else
+ {
+ vt = xcb_aux_find_visual_by_id (scr, scr->root_visual);
assert (vt);
msg_Err (vout, "unsupported visual class %"PRIu8, vt->_class);
goto error;
}
p_sys->vid = vt->visual_id;
+ /* Determine our input format (normally, done in Init() but X11
+ * never changes its format) */
+ p_sys->bpp = scr->root_depth;
+ switch (scr->root_depth)
+ {
+ case 24:
+ p_sys->bpp = 32;
+ case 32: /* FIXME: untested */
+ vout->output.i_chroma = VLC_FOURCC ('R', 'V', '3', '2');
+ break;
+
+ case 16:
+ vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '6');
+ break;
+
+ case 15:
+ p_sys->bpp = 16;
+ vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '5');
+ break;
+
+ case 8: /* FIXME: VLC cannot convert */
+ vout->output.i_chroma = gray ? VLC_FOURCC ('G', 'R', 'E', 'Y')
+ : VLC_FOURCC ('R', 'G', 'B', '2');
+ break;
+
+ default:
+ msg_Err (vout, "unsupported %"PRIu8"-bits screen depth",
+ scr->root_depth);
+ goto error;
+ }
+ vout->fmt_out.i_chroma = vout->output.i_chroma;
+ if (!gray)
+ {
+ vout->output.i_rmask = vt->red_mask;
+ vout->output.i_gmask = vt->green_mask;
+ vout->output.i_bmask = vt->blue_mask;
+ }
+
+ /* Create colormap (needed to select non-default visual) */
+ p_sys->cmap = xcb_generate_id (p_sys->conn);
+ xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
+ p_sys->cmap, scr->root, p_sys->vid);
+
+ /* Check shared memory support */
+ p_sys->shm = var_CreateGetBool (vout, "x11-shm") > 0;
+ if (p_sys->shm)
+ {
+ xcb_shm_query_version_cookie_t ck;
+ xcb_shm_query_version_reply_t *r;
+ xcb_generic_error_t *err;
+
+ ck = xcb_shm_query_version (p_sys->conn);
+ r = xcb_shm_query_version_reply (p_sys->conn, ck, &err);
+ if (!r)
+ {
+ msg_Err (vout, "shared memory (MIT-SHM) not available");
+ msg_Warn (vout, "display will be slow");
+ p_sys->shm = false;
+ }
+ }
+
+ /* Get window */
+ /* FIXME: WTH to put as initial width/height values??? */
+ p_sys->embed = vout_RequestXWindow (vout, &(int){ 0 }, &(int){ 0 },
+ &(unsigned){ 0 }, &(unsigned){ 0 });
+ if (p_sys->embed == NULL)
+ {
+ msg_Err (vout, "parent window not available");
+ goto error;
+ }
+
vout->pf_init = Init;
vout->pf_end = Deinit;
+ vout->pf_render = Render;
vout->pf_display = Display;
vout->pf_manage = Manage;
return VLC_SUCCESS;
vout_thread_t *vout = (vout_thread_t *)obj;
vout_sys_t *p_sys = vout->p_sys;
- assert (p_sys->embed == NULL);
+ if (p_sys->embed)
+ vout_ReleaseWindow (p_sys->embed);
+ /* colormap is garbage-ollected by X (?) */
if (p_sys->conn)
xcb_disconnect (p_sys->conn);
free (p_sys);
}
-static void PictureRelease (picture_t *pic)
+struct picture_sys_t
+{
+ xcb_connection_t *conn; /* Shared connection to X server */
+ xcb_image_t *image; /* Picture buffer */
+ xcb_image_t *native; /* Rendered picture buffer (in X server format) */
+ xcb_shm_seg_t segment; /* Shared memory segment X ID */
+};
+
+#define SHM_ERR ((void *)(intptr_t)(-1))
+
+static int PictureInit (vout_thread_t *vout, picture_t *pic)
+{
+ vout_sys_t *p_sys = vout->p_sys;
+ picture_sys_t *priv = malloc (sizeof (*p_sys));
+
+ if (priv == NULL)
+ return VLC_ENOMEM;
+
+ assert (pic->i_status == FREE_PICTURE);
+ vout_InitPicture (vout, pic, vout->output.i_chroma,
+ vout->output.i_width, vout->output.i_height,
+ vout->output.i_aspect);
+
+ void *shm = SHM_ERR;
+ const size_t size = pic->p->i_pitch * pic->p->i_lines;
+
+ if (p_sys->shm)
+ { /* Allocate shared memory segment */
+ int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
+
+ if (id == -1)
+ {
+ msg_Err (vout, "shared memory allocation error: %m");
+ goto error;
+ }
+
+ /* Attach the segment to VLC */
+ shm = shmat (id, NULL, 0 /* read/write */);
+ if (shm == SHM_ERR)
+ {
+ msg_Err (vout, "shared memory attachment error: %m");
+ shmctl (id, IPC_RMID, 0);
+ goto error;
+ }
+
+ /* Attach the segment to X */
+ xcb_void_cookie_t ck;
+
+ priv->segment = xcb_generate_id (p_sys->conn);
+ ck = xcb_shm_attach_checked (p_sys->conn, priv->segment, id, 1);
+ shmctl (id, IPC_RMID, 0);
+
+ if (CheckError (vout, "shared memory server-side error", ck))
+ {
+ msg_Info (vout, "using buggy X (remote) server? SSH?");
+ shmdt (shm);
+ shm = SHM_ERR;
+ }
+ }
+ else
+ priv->segment = 0;
+
+ const unsigned real_width = pic->p->i_pitch / (p_sys->bpp >> 3);
+ /* FIXME: anyway to getthing more intuitive than that?? */
+
+ /* NOTE: 32-bits scanline_pad assumed, FIXME? (see xdpyinfo) */
+ xcb_image_t *img;
+ img = xcb_image_create (real_width, pic->p->i_lines,
+ XCB_IMAGE_FORMAT_Z_PIXMAP, 32,
+ p_sys->screen->root_depth, p_sys->bpp, p_sys->bpp,
+#ifdef WORDS_BIGENDIAN
+ XCB_IMAGE_ORDER_MSB_FIRST,
+#else
+ XCB_IMAGE_ORDER_LSB_FIRST,
+#endif
+ XCB_IMAGE_ORDER_MSB_FIRST,
+ NULL,
+ (shm != SHM_ERR) ? size : 0,
+ (shm != SHM_ERR) ? shm : NULL);
+
+ if (img == NULL)
+ {
+ if (shm != SHM_ERR)
+ xcb_shm_detach (p_sys->conn, priv->segment);
+ goto error;
+ }
+ if (shm != SHM_ERR && xcb_image_native (p_sys->conn, img, 0) == NULL)
+ msg_Warn (vout, "incompatible X server image format");
+
+ priv->conn = p_sys->conn;
+ priv->image = img;
+ priv->native = NULL;
+ pic->p_sys = priv;
+ pic->p->p_pixels = img->data;
+ pic->i_status = DESTROYED_PICTURE;
+ pic->i_type = DIRECT_PICTURE;
+ return VLC_SUCCESS;
+
+error:
+ if (shm != SHM_ERR)
+ shmdt (shm);
+ free (priv);
+ return VLC_EGENERIC;
+}
+
+
+/**
+ * Release picture private data
+ */
+static void PictureDeinit (picture_t *pic)
{
- xcb_image_t *img = (xcb_image_t *)pic->p_sys;
- xcb_image_destroy (img);
+ struct picture_sys_t *p_sys = pic->p_sys;
+
+ if (p_sys->segment != 0)
+ {
+ xcb_shm_detach (p_sys->conn, p_sys->segment);
+ shmdt (p_sys->image->data);
+ }
+ if ((p_sys->native != NULL) && (p_sys->native != p_sys->image))
+ xcb_image_destroy (p_sys->native);
+ xcb_image_destroy (p_sys->image);
+ free (p_sys);
}
/**
const xcb_screen_t *screen = p_sys->screen;
unsigned x, y, width, height;
- /* Determine parent window */
+ /* Determine parent window and size */
if (vout->b_fullscreen)
{
- p_sys->embed = NULL;
p_sys->parent = screen->root;
width = screen->width_in_pixels;
height = screen->height_in_pixels;
}
else
{
- p_sys->embed = vout_RequestWindow (vout, &(int){ 0 }, &(int){ 0 },
- &width, &height);
- if (p_sys->embed == NULL)
- {
- msg_Err (vout, "cannot get window");
- return VLC_EGENERIC;
- }
- p_sys->parent = (intptr_t)p_sys->embed->handle;
+ p_sys->parent = p_sys->embed->handle.xid;
+
+ /* Subscribe to parent window resize events */
+ const uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
+ xcb_change_window_attributes (p_sys->conn, p_sys->parent,
+ XCB_CW_EVENT_MASK, &value);
+
+ xcb_get_geometry_cookie_t ck;
+ ck = xcb_get_geometry (p_sys->conn, p_sys->parent);
+
+ xcb_get_geometry_reply_t *geo;
+ xcb_generic_error_t *err;
+ geo = xcb_get_geometry_reply (p_sys->conn, ck, &err);
+ width = geo->width;
+ height = geo->height;
+ free (geo);
}
- /* Determine our input format */
- uint8_t depth = screen->root_depth, bpp = depth;
- switch (depth)
- {
- case 24:
- bpp = 32;
- case 32: /* FIXME: untested */
- vout->output.i_chroma = VLC_FOURCC ('R', 'V', '3', '2');
- break;
-
- case 16:
- vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '6');
- break;
-
- case 15:
- bpp = 16;
- vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '5');
- break;
-
- case 8: /* FIXME: VLC cannot convert */
- vout->output.i_chroma = VLC_FOURCC ('R', 'G', 'B', '2');
- break;
+ vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
- default:
- assert (0);
- }
+ /* FIXME: I don't get the subtlety between output and fmt_out here */
+ vout->fmt_out.i_visible_width = width;
+ vout->fmt_out.i_visible_height = height;
+ vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
+
+ vout->output.i_width = vout->fmt_out.i_width =
+ width * vout->fmt_in.i_width / vout->fmt_in.i_visible_width;
+ vout->output.i_height = vout->fmt_out.i_height =
+ height * vout->fmt_in.i_height / vout->fmt_in.i_visible_height;
+ vout->fmt_out.i_x_offset =
+ width * vout->fmt_in.i_x_offset / vout->fmt_in.i_visible_width;
+ p_vout->fmt_out.i_y_offset =
+ height * vout->fmt_in.i_y_offset / vout->fmt_in.i_visible_height;
- vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
- vout->output.i_width = width;
- vout->output.i_height = height;
assert (height > 0);
- vout->output.i_aspect = width * VOUT_ASPECT_FACTOR / height;
- /* FIXME: I don't get the subtlety between output and fmt_out here */
+ vout->output.i_aspect = vout->fmt_out.i_aspect =
+ width * VOUT_ASPECT_FACTOR / height;
/* Create window */
+ const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK
+ | XCB_CW_COLORMAP;
+ const uint32_t values[] = {
+ /* XCB_CW_BACK_PIXEL */
+ screen->black_pixel,
+ /* XCB_CW_EVENT_MASK */
+ XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
+ XCB_EVENT_MASK_POINTER_MOTION,
+ /* XCB_CW_COLORMAP */
+ p_sys->cmap,
+ };
xcb_void_cookie_t c;
xcb_window_t window = xcb_generate_id (p_sys->conn);
- p_sys->window = window;
c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
p_sys->parent, x, y, width, height, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
- screen->root_visual, 0, NULL);
+ p_sys->vid, mask, values);
if (CheckError (vout, "cannot create X11 window", c))
goto error;
+ p_sys->window = window;
msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
xcb_map_window (p_sys->conn, window);
{
picture_t *pic = vout->p_picture + I_OUTPUTPICTURES;
- assert (pic->i_status == FREE_PICTURE);
- vout_InitPicture (vout, pic, vout->output.i_chroma,
- vout->output.i_width, vout->output.i_height,
- vout->output.i_aspect);
-
- xcb_image_t *img;
- const unsigned real_width = pic->p->i_pitch / (bpp >> 3);
- /* FIXME: anyway to getthing more intuitive than that?? */
-
- /* NOTE: 32-bits scanline_pad assumed, FIXME? (see xdpyinfo) */
- img = xcb_image_create (real_width, height, XCB_IMAGE_FORMAT_Z_PIXMAP,
- 32, depth, bpp, bpp,
-#ifdef WORDS_BIGENDIAN
- XCB_IMAGE_ORDER_MSB_FIRST,
-#else
- XCB_IMAGE_ORDER_LSB_FIRST,
-#endif
- XCB_IMAGE_ORDER_MSB_FIRST,
- NULL, 0, NULL);
- if (!img)
- goto error;
-
- pic->p_sys = (picture_sys_t *)img;
- pic->pf_release = PictureRelease;
- pic->p->p_pixels = img->data;
-
- pic->i_status = DESTROYED_PICTURE;
- pic->i_type = DIRECT_PICTURE;
+ if (PictureInit (vout, pic))
+ break;
PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
}
while (I_OUTPUTPICTURES < 2);
vout_sys_t *p_sys = vout->p_sys;
while (I_OUTPUTPICTURES > 0)
- picture_Release (PP_OUTPUTPICTURE[--I_OUTPUTPICTURES]);
+ PictureDeinit (PP_OUTPUTPICTURE[--I_OUTPUTPICTURES]);
xcb_unmap_window (p_sys->conn, p_sys->window);
xcb_destroy_window (p_sys->conn, p_sys->window);
- vout_ReleaseWindow (p_sys->embed);
- p_sys->embed = NULL;
}
+/**
+ * Prepares an image ahead of display.
+ */
+static void Render (vout_thread_t *vout, picture_t *pic)
+{
+ vout_sys_t *p_sys = vout->p_sys;
+ picture_sys_t *priv = pic->p_sys;
+
+ if ((priv->native != NULL) && (priv->native != priv->image))
+ xcb_image_destroy (priv->native);
+ priv->native = xcb_image_native (p_sys->conn, priv->image, 1);
+}
+
+
/**
* Sends an image to the X server.
*/
static void Display (vout_thread_t *vout, picture_t *pic)
{
vout_sys_t *p_sys = vout->p_sys;
- xcb_image_t *img = (xcb_image_t *)pic->p_sys;
- xcb_image_t *native = xcb_image_native (p_sys->conn, img, 1);
+ picture_sys_t *priv = pic->p_sys;
+ xcb_image_t *img = priv->image, *native = priv->native;
- if (native == NULL)
+ if ((native == img) && (img->base == NULL))
{
- msg_Err (vout, "image conversion failure");
- return;
+ xcb_shm_segment_info_t info = {
+ .shmseg = priv->segment,
+ .shmid = -1, /* lost track of it, unimportant */
+ .shmaddr = img->data,
+ };
+
+ xcb_image_shm_put (p_sys->conn, p_sys->window, p_sys->gc, img, info,
+ 0, 0, 0, 0, img->width, img->height, 0);
}
-
- xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, native, 0, 0, 0);
+ else
+ if (native != NULL)
+ xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, native, 0, 0, 0);
xcb_flush (p_sys->conn);
-
- if (native != img)
- {
- /*msg_Warn (vout, "image not in native X11 pixmap format");*/
- xcb_image_destroy (native);
- }
}
/**
xcb_generic_event_t *ev;
while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
- {
- switch (ev->response_type & ~0x80)
- {
- default:
- msg_Dbg (vout, "unhandled event %02x", (unsigned)ev->response_type);
- }
- free (ev);
- }
+ ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
if (xcb_connection_has_error (p_sys->conn))
{