[g3dvl] fix a couple of bugs around paletted subpictures
[mesa.git] / src / gallium / state_trackers / xorg / xvmc / subpicture.c
index 78de154bdd7b962fee05026ccf95d90f067b1854..821c87e0dab2706d4be2656fd41c991f6da6de71 100644 (file)
  **************************************************************************/
 
 #include <assert.h>
+
 #include <X11/Xlibint.h>
 #include <X11/extensions/XvMClib.h>
 #include <xorg/fourcc.h>
-#include <vl_winsys.h>
+
 #include <pipe/p_screen.h>
 #include <pipe/p_video_context.h>
 #include <pipe/p_state.h>
+
 #include <util/u_memory.h>
 #include <util/u_math.h>
+#include <util/u_format.h>
+#include <util/u_sampler.h>
+
+#include <vl_winsys.h>
+
 #include "xvmc_private.h"
 
 #define FOURCC_RGB 0x0000003
@@ -44,12 +51,63 @@ static enum pipe_format XvIDToPipe(int xvimage_id)
    switch (xvimage_id) {
       case FOURCC_RGB:
          return PIPE_FORMAT_B8G8R8X8_UNORM;
+
+      case FOURCC_AI44:
+      case FOURCC_IA44:
+         return PIPE_FORMAT_L4A4_UNORM;
+
       default:
          XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);
          return PIPE_FORMAT_NONE;
    }
 }
 
+static unsigned NumPaletteEntries4XvID(int xvimage_id)
+{
+   switch (xvimage_id) {
+      case FOURCC_RGB:
+         return 0;
+
+      case FOURCC_AI44:
+      case FOURCC_IA44:
+         return 16;
+
+      default:
+         XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);
+         return 0;
+   }
+}
+
+static void XvIDToSwizzle(int xvimage_id, struct pipe_sampler_view *tmpl)
+{
+   switch (xvimage_id) {
+      default:
+         XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);
+
+      /* fall through */
+      case FOURCC_RGB:
+         tmpl->swizzle_r = PIPE_SWIZZLE_BLUE;
+         tmpl->swizzle_g = PIPE_SWIZZLE_GREEN;
+         tmpl->swizzle_b = PIPE_SWIZZLE_RED;
+         tmpl->swizzle_a = PIPE_SWIZZLE_ONE;
+         break;
+
+      case FOURCC_IA44:
+         tmpl->swizzle_r = PIPE_SWIZZLE_ALPHA;
+         tmpl->swizzle_g = PIPE_SWIZZLE_ZERO;
+         tmpl->swizzle_b = PIPE_SWIZZLE_ZERO;
+         tmpl->swizzle_a = PIPE_SWIZZLE_RED;
+         break;
+
+      case FOURCC_AI44:
+         tmpl->swizzle_r = PIPE_SWIZZLE_RED;
+         tmpl->swizzle_g = PIPE_SWIZZLE_ZERO;
+         tmpl->swizzle_b = PIPE_SWIZZLE_ZERO;
+         tmpl->swizzle_a = PIPE_SWIZZLE_ALPHA;
+         break;
+   }
+}
+
 static int PipeToComponentOrder(enum pipe_format format, char *component_order)
 {
    assert(component_order);
@@ -57,15 +115,22 @@ static int PipeToComponentOrder(enum pipe_format format, char *component_order)
    switch (format) {
       case PIPE_FORMAT_B8G8R8X8_UNORM:
          return 0;
+
+      case PIPE_FORMAT_L4A4_UNORM:
+         component_order[0] = 'Y';
+         component_order[1] = 'U';
+         component_order[2] = 'V';
+         component_order[3] = 'A';
+         return 4;
+
       default:
          XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized PIPE_FORMAT 0x%08X.\n", format);
          component_order[0] = 0;
          component_order[1] = 0;
          component_order[2] = 0;
          component_order[3] = 0;
+         return 0;
    }
-
-      return 0;
 }
 
 static Status Validate(Display *dpy, XvPortID port, int surface_type_id, int xvimage_id)
@@ -101,7 +166,8 @@ static Status Validate(Display *dpy, XvPortID port, int surface_type_id, int xvi
                                  "[XvMC]   red mask=0x%08X\n" \
                                  "[XvMC]   green mask=0x%08X\n" \
                                  "[XvMC]   blue mask=0x%08X\n",
-                                 subpictures[i].depth, subpictures[i].red_mask, subpictures[i].green_mask, subpictures[i].blue_mask);
+                                 subpictures[i].depth, subpictures[i].red_mask,
+                                 subpictures[i].green_mask, subpictures[i].blue_mask);
          }
          else if (subpictures[i].type == XvYUV) {
             XVMC_MSG(XVMC_TRACE, "[XvMC]   y sample bits=0x%08X\n" \
@@ -133,9 +199,8 @@ Status XvMCCreateSubpicture(Display *dpy, XvMCContext *context, XvMCSubpicture *
    XvMCContextPrivate *context_priv;
    XvMCSubpicturePrivate *subpicture_priv;
    struct pipe_video_context *vpipe;
-   struct pipe_resource template;
-   struct pipe_resource *tex;
-   struct pipe_surface surf_template;
+   struct pipe_resource tex_templ, *tex;
+   struct pipe_sampler_view sampler_templ;
    Status ret;
 
    XVMC_MSG(XVMC_TRACE, "[XvMC] Creating subpicture %p.\n", subpicture);
@@ -163,46 +228,67 @@ Status XvMCCreateSubpicture(Display *dpy, XvMCContext *context, XvMCSubpicture *
    if (!subpicture_priv)
       return BadAlloc;
 
-   memset(&template, 0, sizeof(struct pipe_resource));
-   template.target = PIPE_TEXTURE_2D;
-   template.format = XvIDToPipe(xvimage_id);
-   template.last_level = 0;
+   memset(&tex_templ, 0, sizeof(tex_templ));
+   tex_templ.target = PIPE_TEXTURE_2D;
+   tex_templ.format = XvIDToPipe(xvimage_id);
+   tex_templ.last_level = 0;
    if (vpipe->get_param(vpipe, PIPE_CAP_NPOT_TEXTURES)) {
-      template.width0 = width;
-      template.height0 = height;
+      tex_templ.width0 = width;
+      tex_templ.height0 = height;
    }
    else {
-      template.width0 = util_next_power_of_two(width);
-      template.height0 = util_next_power_of_two(height);
+      tex_templ.width0 = util_next_power_of_two(width);
+      tex_templ.height0 = util_next_power_of_two(height);
    }
-   template.depth0 = 1;
-   template.array_size = 1;
-   template.usage = PIPE_USAGE_DYNAMIC;
-   template.bind = PIPE_BIND_SAMPLER_VIEW;
-   template.flags = 0;
+   tex_templ.depth0 = 1;
+   tex_templ.array_size = 1;
+   tex_templ.usage = PIPE_USAGE_DYNAMIC;
+   tex_templ.bind = PIPE_BIND_SAMPLER_VIEW;
+   tex_templ.flags = 0;
 
-   subpicture_priv->context = context;
-   tex = vpipe->screen->resource_create(vpipe->screen, &template);
+   tex = vpipe->screen->resource_create(vpipe->screen, &tex_templ);
+
+   memset(&sampler_templ, 0, sizeof(sampler_templ));
+   u_sampler_view_default_template(&sampler_templ, tex, tex->format);
+   XvIDToSwizzle(xvimage_id, &sampler_templ);
 
-   memset(&surf_template, 0, sizeof(surf_template));
-   surf_template.format = tex->format;
-   surf_template.usage = PIPE_BIND_SAMPLER_VIEW;
-   subpicture_priv->sfc = vpipe->create_surface(vpipe, tex, &surf_template);
+   subpicture_priv->sampler = vpipe->create_sampler_view(vpipe, tex, &sampler_templ);
    pipe_resource_reference(&tex, NULL);
-   if (!subpicture_priv->sfc) {
+   if (!subpicture_priv->sampler) {
       FREE(subpicture_priv);
       return BadAlloc;
    }
 
+   subpicture_priv->context = context;
    subpicture->subpicture_id = XAllocID(dpy);
    subpicture->context_id = context->context_id;
    subpicture->xvimage_id = xvimage_id;
    subpicture->width = width;
    subpicture->height = height;
-   subpicture->num_palette_entries = 0;
-   subpicture->entry_bytes = PipeToComponentOrder(template.format, subpicture->component_order);
+   subpicture->num_palette_entries = NumPaletteEntries4XvID(xvimage_id);
+   subpicture->entry_bytes = PipeToComponentOrder(tex_templ.format, subpicture->component_order);
    subpicture->privData = subpicture_priv;
 
+   if (subpicture->num_palette_entries > 0) {
+      tex_templ.target = PIPE_TEXTURE_1D;
+      tex_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
+      tex_templ.width0 = subpicture->num_palette_entries;
+      tex_templ.height0 = 1;
+      tex_templ.usage = PIPE_USAGE_STATIC;
+
+      tex = vpipe->screen->resource_create(vpipe->screen, &tex_templ);
+
+      memset(&sampler_templ, 0, sizeof(sampler_templ));
+      u_sampler_view_default_template(&sampler_templ, tex, tex->format);
+      sampler_templ.swizzle_a = PIPE_SWIZZLE_ONE;
+      subpicture_priv->palette = vpipe->create_sampler_view(vpipe, tex, &sampler_templ);
+      pipe_resource_reference(&tex, NULL);
+      if (!subpicture_priv->sampler) {
+         FREE(subpicture_priv);
+         return BadAlloc;
+      }
+   }
+
    SyncHandle();
 
    XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p created.\n", subpicture);
@@ -216,7 +302,7 @@ Status XvMCClearSubpicture(Display *dpy, XvMCSubpicture *subpicture, short x, sh
 {
    XvMCSubpicturePrivate *subpicture_priv;
    XvMCContextPrivate *context_priv;
-   unsigned int tmp_color;
+   struct pipe_box dst_box = {x, y, 0, width, height, 1};
    float color_f[4];
 
    assert(dpy);
@@ -226,17 +312,15 @@ Status XvMCClearSubpicture(Display *dpy, XvMCSubpicture *subpicture, short x, sh
 
    /* Convert color to float */
    util_format_read_4f(PIPE_FORMAT_B8G8R8A8_UNORM,
-                    color_f, 1,
-                    &color, 4,
-                    0, 0, 1, 1);
+                       color_f, 1, &color, 4,
+                       0, 0, 1, 1);
 
    subpicture_priv = subpicture->privData;
    context_priv = subpicture_priv->context->privData;
    /* TODO: Assert clear rect is within bounds? Or clip? */
-   context_priv->vctx->vpipe->clear_render_target(context_priv->vctx->vpipe,
-                                           subpicture_priv->sfc, x, y,
-                                                                                  color_f,
-                                           width, height);
+   context_priv->vctx->vpipe->clear_sampler(context_priv->vctx->vpipe,
+                                            subpicture_priv->sampler, &dst_box,
+                                            color_f);
 
    return Success;
 }
@@ -249,10 +333,8 @@ Status XvMCCompositeSubpicture(Display *dpy, XvMCSubpicture *subpicture, XvImage
    XvMCSubpicturePrivate *subpicture_priv;
    XvMCContextPrivate *context_priv;
    struct pipe_video_context *vpipe;
-   struct pipe_transfer *xfer;
-   unsigned char *src, *dst, *dst_line;
-   unsigned x, y;
    struct pipe_box dst_box = {dstx, dsty, 0, width, height, 1};
+   unsigned src_stride;
 
    XVMC_MSG(XVMC_TRACE, "[XvMC] Compositing subpicture %p.\n", subpicture);
 
@@ -274,39 +356,11 @@ Status XvMCCompositeSubpicture(Display *dpy, XvMCSubpicture *subpicture, XvImage
    context_priv = subpicture_priv->context->privData;
    vpipe = context_priv->vctx->vpipe;
 
-   /* TODO: Assert rects are within bounds? Or clip? */
-
-   xfer = vpipe->get_transfer(vpipe, subpicture_priv->sfc->texture,
-                              0, PIPE_TRANSFER_WRITE, &dst_box);
-   if (!xfer)
-      return BadAlloc;
-
-   src = image->data;
-   dst = vpipe->transfer_map(vpipe, xfer);
-   if (!dst) {
-      vpipe->transfer_destroy(vpipe, xfer);
-      return BadAlloc;
-   }
-
-   switch (image->id) {
-      case FOURCC_RGB:
-         assert(subpicture_priv->sfc->format == XvIDToPipe(image->id));
-         for (y = 0; y < height; ++y) {
-            dst_line = dst;
-            for (x = 0; x < width; ++x, src += 3, dst_line += 4) {
-               dst_line[0] = src[2]; /* B */
-               dst_line[1] = src[1]; /* G */
-               dst_line[2] = src[0]; /* R */
-            }
-            dst += xfer->stride;
-         }
-         break;
-      default:
-         XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", image->id);
-   }
-
-   vpipe->transfer_unmap(vpipe, xfer);
-   vpipe->transfer_destroy(vpipe, xfer);
+   /* clipping should be done by upload_sampler and regardles what the documentation
+   says image->pitches[0] doesn't seems to be in bytes, so don't use it */
+   src_stride = image->width * util_format_get_blocksize(subpicture_priv->sampler->texture->format);
+   vpipe->upload_sampler(vpipe, subpicture_priv->sampler, &dst_box,
+                         image->data, src_stride, srcx, srcy);
 
    XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p composited.\n", subpicture);
 
@@ -326,7 +380,8 @@ Status XvMCDestroySubpicture(Display *dpy, XvMCSubpicture *subpicture)
       return XvMCBadSubpicture;
 
    subpicture_priv = subpicture->privData;
-   pipe_surface_reference(&subpicture_priv->sfc, NULL);
+   pipe_sampler_view_reference(&subpicture_priv->sampler, NULL);
+   pipe_sampler_view_reference(&subpicture_priv->palette, NULL);
    FREE(subpicture_priv);
 
    XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p destroyed.\n", subpicture);
@@ -337,15 +392,28 @@ Status XvMCDestroySubpicture(Display *dpy, XvMCSubpicture *subpicture)
 PUBLIC
 Status XvMCSetSubpicturePalette(Display *dpy, XvMCSubpicture *subpicture, unsigned char *palette)
 {
+   XvMCSubpicturePrivate *subpicture_priv;
+   XvMCContextPrivate *context_priv;
+   struct pipe_video_context *vpipe;
+   struct pipe_box dst_box = {0, 0, 0, 0, 1, 1};
+
    assert(dpy);
+   assert(palette);
 
    if (!subpicture)
       return XvMCBadSubpicture;
 
-   assert(palette);
+   subpicture_priv = subpicture->privData;
+   context_priv = subpicture_priv->context->privData;
+   vpipe = context_priv->vctx->vpipe;
+
+   dst_box.width = subpicture->num_palette_entries;
 
-   /* We don't support paletted subpictures */
-   return BadMatch;
+   vpipe->upload_sampler(vpipe, subpicture_priv->palette, &dst_box, palette, 0, 0, 0);
+
+   XVMC_MSG(XVMC_TRACE, "[XvMC] Palette of Subpicture %p set.\n", subpicture);
+
+   return Success;
 }
 
 PUBLIC
@@ -353,6 +421,9 @@ Status XvMCBlendSubpicture(Display *dpy, XvMCSurface *target_surface, XvMCSubpic
                            short subx, short suby, unsigned short subw, unsigned short subh,
                            short surfx, short surfy, unsigned short surfw, unsigned short surfh)
 {
+   struct pipe_video_rect src_rect = {subx, suby, subw, subh};
+   struct pipe_video_rect dst_rect = {surfx, surfy, surfw, surfh};
+
    XvMCSurfacePrivate *surface_priv;
    XvMCSubpicturePrivate *subpicture_priv;
 
@@ -375,16 +446,10 @@ Status XvMCBlendSubpicture(Display *dpy, XvMCSurface *target_surface, XvMCSubpic
    subpicture_priv = subpicture->privData;
 
    /* TODO: Assert rects are within bounds? Or clip? */
+   subpicture_priv->src_rect = src_rect;
+   subpicture_priv->dst_rect = dst_rect;
 
    surface_priv->subpicture = subpicture;
-   surface_priv->subx = subx;
-   surface_priv->suby = suby;
-   surface_priv->subw = subw;
-   surface_priv->subh = subh;
-   surface_priv->surfx = surfx;
-   surface_priv->surfy = surfy;
-   surface_priv->surfw = surfw;
-   surface_priv->surfh = surfh;
    subpicture_priv->surface = target_surface;
 
    return Success;