]> git.sesse.net Git - vlc/blobdiff - modules/video_output/xcb/xvideo.c
XCB/XVideo: minor simplification
[vlc] / modules / video_output / xcb / xvideo.c
index 10c9ef151d8b54c063c91e3c266cc062ba344385..c132e895dcccfa35ae23ed85613b440babafff16 100644 (file)
 #include <vlc_plugin.h>
 #include <vlc_vout_display.h>
 #include <vlc_picture_pool.h>
+#include <vlc_dialog.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 ADAPTOR_TEXT N_("XVideo adaptor number")
 #define ADAPTOR_LONGTEXT N_( \
     "XVideo hardware adaptor to use. By default, VLC will " \
@@ -66,19 +62,14 @@ vlc_module_begin ()
     set_capability ("vout display", 155)
     set_callbacks (Open, Close)
 
-    add_string ("x11-display", NULL, NULL,
-                DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
-        add_deprecated_alias ("xvideo-display")
     add_integer ("xvideo-adaptor", -1, NULL,
                  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_shortcut ("xcb-xv", "xv", "xvideo", "xid")
 vlc_module_end ()
 
-#define MAX_PICTURES (VOUT_MAX_PICTURES)
+#define MAX_PICTURES (128)
 
 struct vout_display_sys_t
 {
@@ -93,6 +84,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 */
 
@@ -101,7 +93,7 @@ struct vout_display_sys_t
     picture_resource_t resource[MAX_PICTURES];
 };
 
-static picture_t *Get (vout_display_t *);
+static picture_pool_t *Pool (vout_display_t *, unsigned);
 static void Display (vout_display_t *, picture_t *);
 static int Control (vout_display_t *, int, va_list);
 static void Manage (vout_display_t *);
@@ -115,22 +107,25 @@ static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
     xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
     bool ok = false;
 
+    /* We need XVideo 2.2 for PutImage */
     r = xcb_xv_query_extension_reply (conn, ck, NULL);
-    if (r != NULL)
-    {   /* We need XVideo 2.2 for PutImage */
-        if ((r->major > 2) || (r->major == 2 && r->minor >= 2))
-        {
-            msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
-                     r->major, r->minor);
-            ok = true;
-        }
-        else
-            msg_Dbg (vd, "XVideo extension too old (v%"PRIu8".%"PRIu8,
-                     r->major, r->minor);
-        free (r);
-    }
-    else
+    if (r == NULL)
         msg_Dbg (vd, "XVideo extension not available");
+    else
+    if (r->major != 2)
+        msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" unknown",
+                 r->major, r->minor);
+    else
+    if (r->minor < 2)
+        msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" too old",
+                 r->major, r->minor);
+    else
+    {
+        msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
+                 r->major, r->minor);
+        ok = true;
+    }
+    free (r);
     return ok;
 }
 
@@ -151,6 +146,8 @@ static vlc_fourcc_t ParseFormat (vout_display_t *vd,
               case 32:
                 if (f->depth == 24)
                     return VLC_CODEC_RGB32;
+                if (f->depth == 32)
+                    return 0; /* ARGB -> VLC cannot do that currently */
                 break;
               case 24:
                 if (f->depth == 24)
@@ -269,6 +266,19 @@ FindFormat (vout_display_t *vd,
             msg_Warn (vd, "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"))
+            {
+                dialog_FatalWait (vd, _("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"
+                      "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);
+            }
             free (i);
             continue;
         }
@@ -286,40 +296,38 @@ 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));
+
+    if (!var_InheritBool (obj, "overlay"))
+        return VLC_EGENERIC;
     if (p_sys == NULL)
         return VLC_ENOMEM;
 
     vd->sys = p_sys;
 
     /* Connect to X */
-    xcb_connection_t *conn = Connect (obj);
-    if (conn == NULL)
+    xcb_connection_t *conn;
+    const xcb_screen_t *screen;
+    uint8_t depth;
+    p_sys->embed = GetWindow (vd, &conn, &screen, &depth);
+    if (p_sys->embed == NULL)
     {
         free (p_sys);
         return VLC_EGENERIC;
     }
+
     p_sys->conn = conn;
+    p_sys->att = NULL;
+    p_sys->pool = NULL;
+    p_sys->swap_uv = false;
 
     if (!CheckXVideo (vd, conn))
     {
         msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
-        xcb_disconnect (conn);
-        free (p_sys);
-        return VLC_EGENERIC;
-    }
-
-    const xcb_screen_t *screen;
-    p_sys->embed = GetWindow (vd, conn, &screen, &p_sys->shm);
-    if (p_sys->embed == NULL)
-    {
-        xcb_disconnect (conn);
-        free (p_sys);
-        return VLC_EGENERIC;
+        goto error;
     }
 
-    /* */
-    p_sys->att = NULL;
-    p_sys->pool = NULL;
+    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 =
@@ -328,15 +336,15 @@ static int Open (vlc_object_t *obj)
     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;
+    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;
@@ -374,7 +382,7 @@ static int Open (vlc_object_t *obj)
             chromas = chromas_default;
 
         vlc_fourcc_t chroma;
-        for (size_t i = 0; chromas[i] && (xfmt == NULL); i++)
+        for (size_t i = 0; chromas[i]; i++)
         {
             chroma = chromas[i];
 
@@ -388,10 +396,25 @@ static int Open (vlc_object_t *obj)
             }
 
             xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
+            if (xfmt != NULL)
+            {
+                p_sys->id = xfmt->id;
+                p_sys->swap_uv = vlc_fourcc_AreUVPlanesSwapped (fmt.i_chroma,
+                                                                chroma);
+                if (!p_sys->swap_uv)
+                    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 */
-            goto skip_adaptor;
+            continue;
 
         /* Grab a port */
         for (unsigned i = 0; i < a->num_ports; i++)
@@ -406,14 +429,14 @@ static int Open (vlc_object_t *obj)
              if (result == 0)
              {
                  p_sys->port = port;
-                 found_adaptor = true;
-                 break;
+                 goto grabbed_port;
              }
-             msg_Dbg (vd, "cannot grab port %"PRIu32, port);
+             msg_Dbg (vd, "cannot grab port %"PRIu32": Xv error %"PRIu8, port,
+                      result);
         }
-        if (!found_adaptor)
-            goto skip_adaptor;
+        continue; /* No usable port */
 
+    grabbed_port:
         /* Found port - initialize selected format */
         name = strndup (xcb_xv_adaptor_info_name (a), a->name_size);
         if (name != NULL)
@@ -422,45 +445,69 @@ static int Open (vlc_object_t *obj)
             free (name);
         }
         msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
-
-        p_sys->id = xfmt->id;
         msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
-        fmt.i_chroma = chroma;
-        if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
+
+        /* Look for an X11 visual, create a window */
+        xcb_xv_format_t *f = xcb_xv_adaptor_info_formats (a);
+        for (uint_fast16_t i = a->num_formats; i > 0; i--, f++)
         {
-            fmt.i_rmask = xfmt->red_mask;
-            fmt.i_gmask = xfmt->green_mask;
-            fmt.i_bmask = xfmt->blue_mask;
+            if (f->depth != screen->root_depth)
+                continue; /* this would fail anyway */
+
+            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_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->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))
+            {
+                msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32
+                         " (depth: %"PRIu8")", f->visual, f->depth);
+                msg_Dbg (vd, "using X11 window 0x%08"PRIx32, p_sys->window);
+                goto created_window;
+            }
         }
+        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) */
 
-    skip_adaptor:
-        free (r);
+    created_window:
+        break;
     }
     free (adaptors);
-    if (!found_adaptor)
+    if (!p_sys->port)
     {
         msg_Err (vd, "no available XVideo adaptor");
         goto error;
     }
-
-    /* Create window */
+    else
     {
-        const uint32_t mask =
-            /* XCB_CW_EVENT_MASK */
-            XCB_EVENT_MASK_VISIBILITY_CHANGE;
-        xcb_void_cookie_t c;
-        xcb_window_t window = xcb_generate_id (conn);
-
-        c = xcb_create_window_checked (conn, screen->root_depth, window,
-                                       p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
-                                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
-                                       screen->root_visual,
-                                       XCB_CW_EVENT_MASK, &mask);
-        if (CheckError (vd, conn, "cannot create X11 window", c))
-            goto error;
-        p_sys->window = window;
-        msg_Dbg (vd, "using X11 window 0x%08"PRIx32, window);
-        xcb_map_window (conn, window);
+        xcb_map_window (conn, p_sys->window);
 
         vout_display_place_t place;
 
@@ -469,10 +516,12 @@ static int Open (vlc_object_t *obj)
         p_sys->height = place.height;
 
         /* */
-        const uint32_t values[] = { place.x, place.y, place.width, place.height };
-        xcb_configure_window (conn, window,
+        const uint32_t values[] = {
+            place.x, place.y, place.width, place.height };
+        xcb_configure_window (conn, p_sys->window,
                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
-                              XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
+                              XCB_CONFIG_WINDOW_WIDTH |
+                              XCB_CONFIG_WINDOW_HEIGHT,
                               values);
     }
     p_sys->visible = false;
@@ -485,28 +534,31 @@ static int Open (vlc_object_t *obj)
     /* Create cursor */
     p_sys->cursor = CreateBlankCursor (conn, screen);
 
-    /* */
-    p_sys->pool = NULL;
+    CheckSHM (obj, conn, &p_sys->shm);
 
     /* */
     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 */
     vd->fmt = fmt;
     vd->info = info;
 
-    vd->get = Get;
+    vd->pool = Pool;
     vd->prepare = NULL;
     vd->display = Display;
     vd->control = Control;
     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;
 
@@ -537,42 +589,48 @@ 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);
-    vout_display_DeleteWindow (vd, p_sys->embed);
     xcb_disconnect (p_sys->conn);
+    vout_display_DeleteWindow (vd, p_sys->embed);
     free (p_sys);
 }
 
 /**
  * Return a direct buffer
  */
-static picture_t *Get (vout_display_t *vd)
+static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
 {
     vout_display_sys_t *p_sys = vd->sys;
 
     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);
         p_sys->data_size = p_sys->att->data_size;
 
         unsigned count;
         picture_t *pic_array[MAX_PICTURES];
-        for (count = 0; count < MAX_PICTURES; count++)
+        for (count = 0; count < requested_count; count++)
         {
+            if (count >= MAX_PICTURES)
+                break;
             picture_resource_t *res = &p_sys->resource[count];
 
-            for (int i = 0; i < pic->i_planes; i++)
+            for (int i = 0; i < __MIN (p_sys->att->num_planes, PICTURE_PLANE_MAX); i++)
             {
-                res->p[i].i_lines = pic->p[i].i_lines; /* FIXME seems wrong*/
-                res->p[i].i_pitch = pic->p[i].i_pitch;
+                res->p[i].i_lines =
+                    ((i + 1 < p_sys->att->num_planes ? offsets[i+1] :
+                                                       p_sys->data_size) - offsets[i]) / pitches[i];
+                res->p[i].i_pitch = pitches[i];
             }
             if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
                                       p_sys->conn, p_sys->shm))
@@ -580,9 +638,9 @@ static picture_t *Get (vout_display_t *vd)
 
             /* Allocate further planes as specified by XVideo */
             /* We assume that offsets[0] is zero */
-            for (int i = 1; i < pic->i_planes; i++)
+            for (int i = 1; i < __MIN (p_sys->att->num_planes, PICTURE_PLANE_MAX); i++)
                 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
-            if (vd->fmt.i_chroma == VLC_CODEC_YV12)
+            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;
@@ -597,22 +655,16 @@ static picture_t *Get (vout_display_t *vd)
                 break;
             }
         }
-        picture_Release (pic);
 
         if (count == 0)
             return NULL;
 
         p_sys->pool = picture_pool_New (count, pic_array);
-        if (!p_sys->pool)
-        {
-            /* TODO release picture resources */
-            return NULL;
-        }
-        /* FIXME should also do it in case of error ? */
+        /* TODO release picture resources if NULL */
         xcb_flush (p_sys->conn);
     }
 
-    return picture_pool_Get (p_sys->pool);
+    return p_sys->pool;
 }
 
 /**
@@ -679,7 +731,7 @@ static int Control (vout_display_t *vd, int query, va_list ap)
     {
         const vout_display_cfg_t *cfg;
         const video_format_t *source;
-        bool is_forced;
+        bool is_forced = false;
 
         if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
          || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
@@ -720,10 +772,10 @@ 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