]> git.sesse.net Git - vlc/blobdiff - modules/video_output/xcb/xvideo.c
XCB/XVideo: the supported resolution is not always maximum
[vlc] / modules / video_output / xcb / xvideo.c
index 03e9670bb6a3becbfcd7d3996bd89755fe0fa448..397f3b2d5f5f18d98f520b3b0ae0f7545991a9e5 100644 (file)
@@ -25,6 +25,7 @@
 #endif
 
 #include <stdlib.h>
+#include <limits.h>
 #include <assert.h>
 
 #include <xcb/xcb.h>
     "XVideo hardware adaptor to use. By default, VLC will " \
     "use the first functional adaptor.")
 
-#define SHM_TEXT N_("Use shared memory")
-#define SHM_LONGTEXT N_( \
-    "Use shared memory to communicate between VLC and the X server.")
+#define FORMAT_TEXT N_("XVideo format id")
+#define FORMAT_LONGTEXT N_( \
+    "XVideo image format id to use. By default, VLC will " \
+    "try to use the best match for the video being played.")
 
 static int  Open (vlc_object_t *);
 static void Close (vlc_object_t *);
@@ -59,19 +61,18 @@ vlc_module_begin ()
     set_description (N_("XVideo output (XCB)"))
     set_category (CAT_VIDEO)
     set_subcategory (SUBCAT_VIDEO_VOUT)
-    set_capability ("vout display", 155)
+    set_capability ("vout display", 200)
     set_callbacks (Open, Close)
 
-    add_integer ("xvideo-adaptor", -1, NULL,
+    add_integer ("xvideo-adaptor", -1,
                  ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true)
-    add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
-        add_deprecated_alias ("xvideo-shm")
-    add_shortcut ("xcb-xv")
-    add_shortcut ("xv")
-    add_shortcut ("xvideo")
+    add_integer ("xvideo-format-id", 0,
+                 FORMAT_TEXT, FORMAT_LONGTEXT, true)
+    add_obsolete_bool ("xvideo-shm") /* removed in 1.2.0 */
+    add_shortcut ("xcb-xv", "xv", "xvideo", "xid")
 vlc_module_end ()
 
-#define MAX_PICTURES (VOUT_MAX_PICTURES)
+#define MAX_PICTURES (128)
 
 struct vout_display_sys_t
 {
@@ -86,6 +87,7 @@ struct vout_display_sys_t
     uint16_t width;      /* display width */
     uint16_t height;     /* display height */
     uint32_t data_size;  /* picture byte size (for non-SHM) */
+    bool     swap_uv;    /* U/V pointer must be swapped in a picture */
     bool shm;            /* whether to use MIT-SHM */
     bool visible;        /* whether it makes sense to draw at all */
 
@@ -95,7 +97,7 @@ struct vout_display_sys_t
 };
 
 static picture_pool_t *Pool (vout_display_t *, unsigned);
-static void Display (vout_display_t *, picture_t *);
+static void Display (vout_display_t *, picture_t *, subpicture_t *subpicture);
 static int Control (vout_display_t *, int, va_list);
 static void Manage (vout_display_t *);
 
@@ -130,44 +132,54 @@ static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
     return ok;
 }
 
-static vlc_fourcc_t ParseFormat (vout_display_t *vd,
+static vlc_fourcc_t ParseFormat (vlc_object_t *obj,
                                  const xcb_xv_image_format_info_t *restrict f)
 {
-    if (f->byte_order != ORDER && f->bpp != 8)
-        return 0; /* Argh! */
-
     switch (f->type)
     {
       case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
         switch (f->num_planes)
         {
           case 1:
-            switch (f->bpp)
+            switch (popcount (f->red_mask | f->green_mask | f->blue_mask))
             {
-              case 32:
-                if (f->depth == 24)
-                    return VLC_CODEC_RGB32;
-                break;
               case 24:
-                if (f->depth == 24)
+                if (f->bpp == 32 && f->depth == 32)
+                    return VLC_CODEC_RGBA;
+                if (f->bpp == 32 && f->depth == 24)
+                    return VLC_CODEC_RGB32;
+                if (f->bpp == 24 && f->depth == 24)
                     return VLC_CODEC_RGB24;
                 break;
               case 16:
-                if (f->depth == 16)
+                if (f->byte_order != ORDER)
+                    return 0; /* Mixed endian! */
+                if (f->bpp == 16 && f->depth == 16)
                     return VLC_CODEC_RGB16;
-                if (f->depth == 15)
+                break;
+              case 15:
+                if (f->byte_order != ORDER)
+                    return 0; /* Mixed endian! */
+                if (f->bpp == 16 && f->depth == 16)
+                    return VLC_CODEC_RGBT;
+                if (f->bpp == 16 && f->depth == 15)
                     return VLC_CODEC_RGB15;
                 break;
+              case 12:
+                if (f->bpp == 16 && f->depth == 16)
+                    return VLC_CODEC_RGBA16;
+                if (f->bpp == 16 && f->depth == 12)
+                    return VLC_CODEC_RGB12;
               case 8:
-                if (f->depth == 8)
+                if (f->bpp == 8 && f->depth == 8)
                     return VLC_CODEC_RGB8;
                 break;
             }
             break;
         }
-        msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
+        msg_Err (obj, "unknown XVideo RGB format %"PRIx32" (%.4s)",
                  f->id, f->guid);
-        msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
+        msg_Dbg (obj, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
                  "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
         break;
 
@@ -213,78 +225,144 @@ static vlc_fourcc_t ParseFormat (vout_display_t *vd,
             break;
         }
     bad:
-        msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
+        msg_Err (obj, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
                  f->guid);
-        msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
+        msg_Dbg (obj, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
                  "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
                  f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
-        msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
+        msg_Dbg (obj, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
                  "%"PRIu32"/%"PRIu32"/%"PRIu32,
                  f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
                  f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
-        msg_Warn (vd, " order: %.32s", f->vcomp_order);
+        msg_Warn (obj, " order: %.32s", f->vcomp_order);
         break;
     }
     return 0;
 }
 
+static bool BetterFormat (vlc_fourcc_t a, const vlc_fourcc_t *tab,
+                          unsigned *rankp)
+{
+    for (unsigned i = 0, max = *rankp; i < max && tab[i] != 0; i++)
+        if (tab[i] == a)
+        {
+            *rankp = i;
+            return true;
+        }
+    return false;
+}
 
-static const xcb_xv_image_format_info_t *
-FindFormat (vout_display_t *vd,
-            vlc_fourcc_t chroma, const video_format_t *fmt,
-            xcb_xv_port_t port,
-            const xcb_xv_list_image_formats_reply_t *list,
-            xcb_xv_query_image_attributes_reply_t **restrict pa)
+static xcb_xv_query_image_attributes_reply_t *
+FindFormat (vlc_object_t *obj, xcb_connection_t *conn, video_format_t *fmt,
+            const xcb_xv_adaptor_info_t *a, uint32_t *idp)
 {
-    xcb_connection_t *conn = vd->sys->conn;
-    const xcb_xv_image_format_info_t *f, *end;
+    /* Order chromas by preference */
+    vlc_fourcc_t tab[7];
+    const vlc_fourcc_t *chromav = tab;
 
-#ifndef XCB_XV_OLD
-    f = xcb_xv_list_image_formats_format (list);
-#else
-    f = (xcb_xv_image_format_info_t *) (list + 1);
-#endif
-    end = f + xcb_xv_list_image_formats_format_length (list);
-    for (; f < end; f++)
+    vlc_fourcc_t chroma = var_InheritInteger (obj, "xvideo-format-id");
+    if (chroma != 0) /* Forced chroma */
+    {
+        tab[0] = chroma;
+        tab[1] = 0;
+    }
+    else if (vlc_fourcc_IsYUV (fmt->i_chroma)) /* YUV chroma */
     {
-        if (chroma != ParseFormat (vd, f))
+        chromav = vlc_fourcc_GetYUVFallback (fmt->i_chroma);
+    }
+    else /* RGB chroma */
+    {
+        tab[0] = fmt->i_chroma;
+        tab[1] = VLC_CODEC_RGB32;
+        tab[2] = VLC_CODEC_RGB24;
+        tab[3] = VLC_CODEC_RGB16;
+        tab[4] = VLC_CODEC_RGB15;
+        tab[5] = VLC_CODEC_YUYV;
+        tab[6] = 0;
+    }
+
+    /* Get available image formats */
+    xcb_xv_list_image_formats_reply_t *list =
+        xcb_xv_list_image_formats_reply (conn,
+            xcb_xv_list_image_formats (conn, a->base_id), NULL);
+    if (list == NULL)
+        return NULL;
+
+    /* Check available XVideo chromas */
+    xcb_xv_query_image_attributes_reply_t *attr = NULL;
+    unsigned rank = UINT_MAX;
+
+    for (const xcb_xv_image_format_info_t *f =
+             xcb_xv_list_image_formats_format (list),
+                                          *f_end =
+             f + xcb_xv_list_image_formats_format_length (list);
+         f < f_end;
+         f++)
+    {
+        chroma = ParseFormat (obj, f);
+        if (chroma == 0)
+            continue;
+
+        /* Oink oink! */
+        if ((chroma == VLC_CODEC_I420 || chroma == VLC_CODEC_YV12)
+         && a->name_size >= 4
+         && !memcmp ("OMAP", xcb_xv_adaptor_info_name (a), 4))
+        {
+            msg_Dbg (obj, "skipping slow I420 format");
+            continue; /* OMAP framebuffer sucks at YUV 4:2:0 */
+        }
+
+        if (!BetterFormat (chroma, chromav, &rank))
             continue;
 
         /* VLC pads scanline to 16 pixels internally */
-        unsigned width = (fmt->i_width + 15) & ~15;
-        unsigned height = (fmt->i_height + 15) & ~15;
+        unsigned width = fmt->i_width;
+        unsigned height = fmt->i_height;
         xcb_xv_query_image_attributes_reply_t *i;
         i = xcb_xv_query_image_attributes_reply (conn,
-            xcb_xv_query_image_attributes (conn, port, f->id,
+            xcb_xv_query_image_attributes (conn, a->base_id, f->id,
                                            width, height), NULL);
         if (i == NULL)
             continue;
 
         if (i->width != width || i->height != height)
         {
-            msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
+            msg_Warn (obj, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
                       fmt->i_width, fmt->i_height,
                       i->width, i->height);
-            var_Create (vd->p_libvlc, "xvideo-resolution-error", VLC_VAR_BOOL);
-            if (!var_GetBool (vd->p_libvlc, "xvideo-resolution-error"))
+            var_Create (obj->p_libvlc, "xvideo-res-error", VLC_VAR_BOOL);
+            if (!var_GetBool (obj->p_libvlc, "xvideo-res-error"))
             {
-                dialog_FatalWait (vd, _("Video acceleration not available"),
+                dialog_FatalWait (obj, _("Video acceleration not available"),
                     _("Your video output acceleration driver does not support "
-                      "the required resolution: %ux%u pixels. The maximum "
-                      "supported resolution is %"PRIu32"x%"PRIu32".\n"
+                      "the required resolution of %ux%u pixels. The supported "
+                      "resolution is %"PRIu32"x%"PRIu32".\n"
                       "Video output acceleration will be disabled. However, "
                       "rendering videos with overly large resolution "
                       "may cause severe performance degration."),
                                   width, height, i->width, i->height);
-                var_SetBool (vd->p_libvlc, "xvideo-resolution-error", true);
+                var_SetBool (obj->p_libvlc, "xvideo-res-error", true);
             }
             free (i);
             continue;
         }
-        *pa = i;
-        return f;
+
+        fmt->i_chroma = chroma;
+        if (f->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
+        {
+            fmt->i_rmask = f->red_mask;
+            fmt->i_gmask = f->green_mask;
+            fmt->i_bmask = f->blue_mask;
+        }
+        *idp = f->id;
+        free (attr);
+        attr = i;
+        if (rank == 0)
+            break; /* shortcut for perfect match */
     }
-    return NULL;
+
+    free (list);
+    return attr;
 }
 
 
@@ -294,7 +372,11 @@ FindFormat (vout_display_t *vd,
 static int Open (vlc_object_t *obj)
 {
     vout_display_t *vd = (vout_display_t *)obj;
-    vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
+    vout_display_sys_t *p_sys;
+
+    if (!var_InheritBool (obj, "overlay"))
+        return VLC_EGENERIC;
+    p_sys = malloc (sizeof (*p_sys));
     if (p_sys == NULL)
         return VLC_ENOMEM;
 
@@ -322,23 +404,24 @@ static int Open (vlc_object_t *obj)
     }
 
     p_sys->window = xcb_generate_id (conn);
+    xcb_pixmap_t pixmap = xcb_generate_id (conn);
 
     /* Cache adaptors infos */
     xcb_xv_query_adaptors_reply_t *adaptors =
         xcb_xv_query_adaptors_reply (conn,
-            xcb_xv_query_adaptors (conn, p_sys->embed->xid), NULL);
+            xcb_xv_query_adaptors (conn, p_sys->embed->handle.xid), NULL);
     if (adaptors == NULL)
         goto error;
 
-    int forced_adaptor = var_CreateGetInteger (obj, "xvideo-adaptor");
+    int forced_adaptor = var_InheritInteger (obj, "xvideo-adaptor");
 
     /* */
-    video_format_t fmt = vd->fmt;
-    bool found_adaptor = false;
+    video_format_t fmt;
+    p_sys->port = 0;
 
     xcb_xv_adaptor_info_iterator_t it;
     for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
-         it.rem > 0 && !found_adaptor;
+         it.rem > 0;
          xcb_xv_adaptor_info_next (&it))
     {
         const xcb_xv_adaptor_info_t *a = it.data;
@@ -350,61 +433,15 @@ static int Open (vlc_object_t *obj)
             continue;
         }
 
-        if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
-            continue;
-
-        xcb_xv_list_image_formats_reply_t *r =
-            xcb_xv_list_image_formats_reply (conn,
-                xcb_xv_list_image_formats (conn, a->base_id), NULL);
-        if (r == NULL)
+        if (!(a->type & XCB_XV_TYPE_INPUT_MASK)
+         || !(a->type & XCB_XV_TYPE_IMAGE_MASK))
             continue;
 
         /* Look for an image format */
-        const xcb_xv_image_format_info_t *xfmt = NULL;
-        const vlc_fourcc_t *chromas, chromas_default[] = {
-            fmt.i_chroma,
-            VLC_CODEC_RGB32,
-            VLC_CODEC_RGB24,
-            VLC_CODEC_RGB16,
-            VLC_CODEC_RGB15,
-            VLC_CODEC_YUYV,
-            0
-        };
-        if (vlc_fourcc_IsYUV (fmt.i_chroma))
-            chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
-        else
-            chromas = chromas_default;
-
-        vlc_fourcc_t chroma;
-        for (size_t i = 0; chromas[i]; i++)
-        {
-            chroma = chromas[i];
-
-            /* Oink oink! */
-            if ((chroma == VLC_CODEC_I420 || chroma == VLC_CODEC_YV12)
-             && a->name_size >= 4
-             && !memcmp ("OMAP", xcb_xv_adaptor_info_name (a), 4))
-            {
-                msg_Dbg (vd, "skipping slow I420 format");
-                continue; /* OMAP framebuffer sucks at YUV 4:2:0 */
-            }
-
-            xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
-            if (xfmt != NULL)
-            {
-                p_sys->id = xfmt->id;
-                fmt.i_chroma = chroma;
-                if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
-                {
-                    fmt.i_rmask = xfmt->red_mask;
-                    fmt.i_gmask = xfmt->green_mask;
-                    fmt.i_bmask = xfmt->blue_mask;
-                }
-                break;
-            }
-        }
-        free (r);
-        if (xfmt == NULL) /* No acceptable image formats */
+        fmt = vd->fmt;
+        free (p_sys->att);
+        p_sys->att = FindFormat (obj, conn, &fmt, a, &p_sys->id);
+        if (p_sys->att == NULL) /* No acceptable image formats */
             continue;
 
         /* Grab a port */
@@ -422,7 +459,8 @@ static int Open (vlc_object_t *obj)
                  p_sys->port = port;
                  goto grabbed_port;
              }
-             msg_Dbg (vd, "cannot grab port %"PRIu32, port);
+             msg_Dbg (vd, "cannot grab port %"PRIu32": Xv error %"PRIu8, port,
+                      result);
         }
         continue; /* No usable port */
 
@@ -441,18 +479,37 @@ static int Open (vlc_object_t *obj)
         xcb_xv_format_t *f = xcb_xv_adaptor_info_formats (a);
         for (uint_fast16_t i = a->num_formats; i > 0; i--, f++)
         {
-            if (f->depth != depth)
+            if (f->depth != screen->root_depth)
                 continue; /* this would fail anyway */
 
-            const uint32_t mask =
+            uint32_t mask =
+                XCB_CW_BACK_PIXMAP |
+                XCB_CW_BACK_PIXEL |
+                XCB_CW_BORDER_PIXMAP |
+                XCB_CW_BORDER_PIXEL |
+                XCB_CW_EVENT_MASK |
+                XCB_CW_COLORMAP;
+            const uint32_t list[] = {
+                /* XCB_CW_BACK_PIXMAP */
+                pixmap,
+                /* XCB_CW_BACK_PIXEL */
+                screen->black_pixel,
+                /* XCB_CW_BORDER_PIXMAP */
+                pixmap,
+                /* XCB_CW_BORDER_PIXEL */
+                screen->black_pixel,
                 /* XCB_CW_EVENT_MASK */
-                XCB_EVENT_MASK_VISIBILITY_CHANGE;
+                XCB_EVENT_MASK_VISIBILITY_CHANGE,
+                /* XCB_CW_COLORMAP */
+                screen->default_colormap,
+            };
+
             xcb_void_cookie_t c;
 
+            xcb_create_pixmap (conn, f->depth, pixmap, screen->root, 1, 1);
             c = xcb_create_window_checked (conn, f->depth, p_sys->window,
-                 p_sys->embed->xid, 0, 0, 1, 1, 0,
-                 XCB_WINDOW_CLASS_INPUT_OUTPUT, f->visual,
-                 XCB_CW_EVENT_MASK, &mask);
+                 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
+                 XCB_WINDOW_CLASS_INPUT_OUTPUT, f->visual, mask, list);
 
             if (!CheckError (vd, conn, "cannot create X11 window", c))
             {
@@ -463,19 +520,20 @@ static int Open (vlc_object_t *obj)
             }
         }
         xcb_xv_ungrab_port (conn, p_sys->port, XCB_CURRENT_TIME);
+        p_sys->port = 0;
+        msg_Dbg (vd, "no usable X11 visual");
         continue; /* No workable XVideo format (visual/depth) */
 
     created_window:
-        found_adaptor = true;
         break;
     }
     free (adaptors);
-    if (!found_adaptor)
+    if (!p_sys->port)
     {
         msg_Err (vd, "no available XVideo adaptor");
         goto error;
     }
-    else
+    /* Compute video (window) placement within the parent window */
     {
         xcb_map_window (conn, p_sys->window);
 
@@ -501,16 +559,30 @@ static int Open (vlc_object_t *obj)
     xcb_create_gc (conn, p_sys->gc, p_sys->window, 0, NULL);
     msg_Dbg (vd, "using X11 graphic context 0x%08"PRIx32, p_sys->gc);
 
+    /* Disable color keying if applicable */
+    {
+        xcb_intern_atom_reply_t *r =
+            xcb_intern_atom_reply (conn,
+                xcb_intern_atom (conn, 1, 21, "XV_AUTOPAINT_COLORKEY"), NULL);
+        if (r != NULL && r->atom != 0)
+            xcb_xv_set_port_attribute(conn, p_sys->port, r->atom, 1);
+    }
+
     /* Create cursor */
     p_sys->cursor = CreateBlankCursor (conn, screen);
 
-    CheckSHM (obj, conn, &p_sys->shm);
+    p_sys->shm = CheckSHM (obj, conn);
 
     /* */
     vout_display_info_t info = vd->info;
     info.has_pictures_invalid = false;
+    info.has_event_thread = true;
 
     /* Setup vout_display_t once everything is fine */
+    p_sys->swap_uv = vlc_fourcc_AreUVPlanesSwapped (fmt.i_chroma,
+                                                    vd->fmt.i_chroma);
+    if (p_sys->swap_uv)
+        fmt.i_chroma = vd->fmt.i_chroma;
     vd->fmt = fmt;
     vd->info = info;
 
@@ -521,10 +593,13 @@ static int Open (vlc_object_t *obj)
     vd->manage = Manage;
 
     /* */
-    vout_display_SendEventFullscreen (vd, false);
+    bool is_fullscreen = vd->cfg->is_fullscreen;
+    if (is_fullscreen && vout_window_SetFullScreen (p_sys->embed, true))
+        is_fullscreen = false;
+    vout_display_SendEventFullscreen (vd, is_fullscreen);
     unsigned width, height;
     if (!GetWindowSize (p_sys->embed, conn, &width, &height))
-        vout_display_SendEventDisplaySize (vd, width, height, false);
+        vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
 
     return VLC_SUCCESS;
 
@@ -555,84 +630,95 @@ static void Close (vlc_object_t *obj)
         picture_pool_Delete (p_sys->pool);
     }
 
+    /* show the default cursor */
+    xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid, XCB_CW_CURSOR,
+                                  &(uint32_t) { XCB_CURSOR_NONE });
+    xcb_flush (p_sys->conn);
+
     free (p_sys->att);
     xcb_disconnect (p_sys->conn);
     vout_display_DeleteWindow (vd, p_sys->embed);
     free (p_sys);
 }
 
-/**
- * Return a direct buffer
- */
-static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
+static void PoolAlloc (vout_display_t *vd, unsigned requested_count)
 {
     vout_display_sys_t *p_sys = vd->sys;
-    (void)requested_count;
 
-    if (!p_sys->pool)
-    {
-        picture_t *pic = picture_New (vd->fmt.i_chroma, p_sys->att->width,
-                                      p_sys->att->height, 0);
-        if (!pic)
-            return NULL;
+    memset (p_sys->resource, 0, sizeof(p_sys->resource));
+
+    const uint32_t *pitches= xcb_xv_query_image_attributes_pitches (p_sys->att);
+    const uint32_t *offsets= xcb_xv_query_image_attributes_offsets (p_sys->att);
+    const unsigned num_planes= __MIN(p_sys->att->num_planes, PICTURE_PLANE_MAX);
+    p_sys->data_size = p_sys->att->data_size;
 
-        memset (p_sys->resource, 0, sizeof(p_sys->resource));
+    picture_t *pic_array[MAX_PICTURES];
+    requested_count = __MIN(requested_count, MAX_PICTURES);
 
-        const uint32_t *offsets =
-            xcb_xv_query_image_attributes_offsets (p_sys->att);
-        p_sys->data_size = p_sys->att->data_size;
+    unsigned count;
+    for (count = 0; count < requested_count; count++)
+    {
+        picture_resource_t *res = &p_sys->resource[count];
 
-        unsigned count;
-        picture_t *pic_array[MAX_PICTURES];
-        for (count = 0; count < MAX_PICTURES; count++)
+        for (unsigned i = 0; i < num_planes; i++)
         {
-            picture_resource_t *res = &p_sys->resource[count];
+            uint32_t data_size;
+            data_size = (i < num_planes - 1) ? offsets[i+1] : p_sys->data_size;
 
-            for (int i = 0; i < pic->i_planes; i++)
-            {
-                res->p[i].i_lines = pic->p[i].i_lines; /* FIXME seems wrong*/
-                res->p[i].i_pitch = pic->p[i].i_pitch;
-            }
-            if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
-                                      p_sys->conn, p_sys->shm))
-                break;
+            res->p[i].i_lines = (data_size - offsets[i]) / pitches[i];
+            res->p[i].i_pitch = pitches[i];
+        }
 
-            /* Allocate further planes as specified by XVideo */
-            /* We assume that offsets[0] is zero */
-            for (int i = 1; i < pic->i_planes; i++)
-                res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
-            if (vd->fmt.i_chroma == VLC_CODEC_YV12)
-            {   /* YVU: swap U and V planes */
-                uint8_t *buf = res->p[2].p_pixels;
-                res->p[2].p_pixels = res->p[1].p_pixels;
-                res->p[1].p_pixels = buf;
-            }
+        if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
+                                  p_sys->conn, p_sys->shm))
+            break;
 
-            pic_array[count] = picture_NewFromResource (&vd->fmt, res);
-            if (!pic_array[count])
-            {
-                PictureResourceFree (res, p_sys->conn);
-                memset (res, 0, sizeof(*res));
-                break;
-            }
-        }
-        picture_Release (pic);
+        /* Allocate further planes as specified by XVideo */
+        /* We assume that offsets[0] is zero */
+        for (unsigned i = 1; i < num_planes; i++)
+            res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
 
-        if (count == 0)
-            return NULL;
+        if (p_sys->swap_uv)
+        {   /* YVU: swap U and V planes */
+            uint8_t *buf = res->p[2].p_pixels;
+            res->p[2].p_pixels = res->p[1].p_pixels;
+            res->p[1].p_pixels = buf;
+        }
 
-        p_sys->pool = picture_pool_New (count, pic_array);
-        /* TODO release picture resources if NULL */
-        xcb_flush (p_sys->conn);
+        pic_array[count] = picture_NewFromResource (&vd->fmt, res);
+        if (!pic_array[count])
+        {
+            PictureResourceFree (res, p_sys->conn);
+            memset (res, 0, sizeof(*res));
+            break;
+        }
     }
 
+    if (count == 0)
+        return;
+
+    p_sys->pool = picture_pool_New (count, pic_array);
+    /* TODO release picture resources if NULL */
+    xcb_flush (p_sys->conn);
+}
+
+/**
+ * Return a direct buffer
+ */
+static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
+{
+    vout_display_sys_t *p_sys = vd->sys;
+
+    if (!p_sys->pool)
+        PoolAlloc (vd, requested_count);
+
     return p_sys->pool;
 }
 
 /**
  * Sends an image to the X server.
  */
-static void Display (vout_display_t *vd, picture_t *pic)
+static void Display (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
 {
     vout_display_sys_t *p_sys = vd->sys;
     xcb_shm_seg_t segment = pic->p_sys->segment;
@@ -671,6 +757,7 @@ static void Display (vout_display_t *vd, picture_t *pic)
     }
 out:
     picture_Release (pic);
+    (void)subpicture;
 }
 
 static int Control (vout_display_t *vd, int query, va_list ap)
@@ -734,17 +821,18 @@ static int Control (vout_display_t *vd, int query, va_list ap)
         xcb_flush (p_sys->conn);
         return VLC_SUCCESS;
     }
-    case VOUT_DISPLAY_CHANGE_ON_TOP:
+    case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
     {
-        int on_top = (int)va_arg (ap, int);
-        return vout_window_SetOnTop (p_sys->embed, on_top);
+        unsigned state = va_arg (ap, unsigned);
+        return vout_window_SetState (p_sys->embed, state);
     }
 
     /* Hide the mouse. It will be send when
      * vout_display_t::info.b_hide_mouse is false */
     case VOUT_DISPLAY_HIDE_MOUSE:
-        xcb_change_window_attributes (p_sys->conn, p_sys->embed->xid,
+        xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
                                   XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
+        xcb_flush (p_sys->conn);
         return VLC_SUCCESS;
     case VOUT_DISPLAY_RESET_PICTURES:
         assert(0);