Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2009 Younes Manton.
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21.  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  22.  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  **************************************************************************/
  27.  
  28. #include <assert.h>
  29.  
  30. #include <X11/Xlibint.h>
  31. #include <X11/extensions/XvMClib.h>
  32.  
  33. #include "pipe/p_screen.h"
  34. #include "pipe/p_video_decoder.h"
  35. #include "pipe/p_state.h"
  36.  
  37. #include "util/u_memory.h"
  38. #include "util/u_math.h"
  39. #include "util/u_format.h"
  40. #include "util/u_sampler.h"
  41. #include "util/u_rect.h"
  42. #include "vl/vl_winsys.h"
  43.  
  44. #include "xvmc_private.h"
  45.  
  46. #define FOURCC_RGB 0x0000003
  47. #define FOURCC_AI44 0x34344941
  48. #define FOURCC_IA44 0x34344149
  49.  
  50. static enum pipe_format XvIDToPipe(int xvimage_id)
  51. {
  52.    switch (xvimage_id) {
  53.       case FOURCC_RGB:
  54.          return PIPE_FORMAT_B8G8R8X8_UNORM;
  55.  
  56.       case FOURCC_AI44:
  57.          return PIPE_FORMAT_A4R4_UNORM;
  58.  
  59.       case FOURCC_IA44:
  60.          return PIPE_FORMAT_R4A4_UNORM;
  61.  
  62.       default:
  63.          XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);
  64.          return PIPE_FORMAT_NONE;
  65.    }
  66. }
  67.  
  68. static unsigned NumPaletteEntries4XvID(int xvimage_id)
  69. {
  70.    switch (xvimage_id) {
  71.       case FOURCC_RGB:
  72.          return 0;
  73.  
  74.       case FOURCC_AI44:
  75.       case FOURCC_IA44:
  76.          return 16;
  77.  
  78.       default:
  79.          XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);
  80.          return 0;
  81.    }
  82. }
  83.  
  84. static int PipeToComponentOrder(enum pipe_format format, char *component_order)
  85. {
  86.    assert(component_order);
  87.  
  88.    switch (format) {
  89.       case PIPE_FORMAT_B8G8R8X8_UNORM:
  90.          return 0;
  91.  
  92.       case PIPE_FORMAT_R4A4_UNORM:
  93.       case PIPE_FORMAT_A4R4_UNORM:
  94.          component_order[0] = 'Y';
  95.          component_order[1] = 'U';
  96.          component_order[2] = 'V';
  97.          component_order[3] = 'A';
  98.          return 4;
  99.  
  100.       default:
  101.          XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized PIPE_FORMAT 0x%08X.\n", format);
  102.          component_order[0] = 0;
  103.          component_order[1] = 0;
  104.          component_order[2] = 0;
  105.          component_order[3] = 0;
  106.          return 0;
  107.    }
  108. }
  109.  
  110. static Status Validate(Display *dpy, XvPortID port, int surface_type_id, int xvimage_id)
  111. {
  112.    XvImageFormatValues *subpictures;
  113.    int num_subpics;
  114.    unsigned int i;
  115.  
  116.    subpictures = XvMCListSubpictureTypes(dpy, port, surface_type_id, &num_subpics);
  117.    if (num_subpics < 1) {
  118.       free(subpictures);
  119.       return BadMatch;
  120.    }
  121.    if (!subpictures)
  122.       return BadAlloc;
  123.  
  124.    for (i = 0; i < num_subpics; ++i) {
  125.       if (subpictures[i].id == xvimage_id) {
  126.          XVMC_MSG(XVMC_TRACE, "[XvMC] Found requested subpicture format.\n" \
  127.                               "[XvMC]   port=%u\n" \
  128.                               "[XvMC]   surface id=0x%08X\n" \
  129.                               "[XvMC]   image id=0x%08X\n" \
  130.                               "[XvMC]   type=%08X\n" \
  131.                               "[XvMC]   byte order=%08X\n" \
  132.                               "[XvMC]   bits per pixel=%u\n" \
  133.                               "[XvMC]   format=%08X\n" \
  134.                               "[XvMC]   num planes=%d\n",
  135.                               port, surface_type_id, xvimage_id, subpictures[i].type, subpictures[i].byte_order,
  136.                               subpictures[i].bits_per_pixel, subpictures[i].format, subpictures[i].num_planes);
  137.          if (subpictures[i].type == XvRGB) {
  138.             XVMC_MSG(XVMC_TRACE, "[XvMC]   depth=%d\n" \
  139.                                  "[XvMC]   red mask=0x%08X\n" \
  140.                                  "[XvMC]   green mask=0x%08X\n" \
  141.                                  "[XvMC]   blue mask=0x%08X\n",
  142.                                  subpictures[i].depth, subpictures[i].red_mask,
  143.                                  subpictures[i].green_mask, subpictures[i].blue_mask);
  144.          }
  145.          else if (subpictures[i].type == XvYUV) {
  146.             XVMC_MSG(XVMC_TRACE, "[XvMC]   y sample bits=0x%08X\n" \
  147.                                  "[XvMC]   u sample bits=0x%08X\n" \
  148.                                  "[XvMC]   v sample bits=0x%08X\n" \
  149.                                  "[XvMC]   horz y period=%u\n" \
  150.                                  "[XvMC]   horz u period=%u\n" \
  151.                                  "[XvMC]   horz v period=%u\n" \
  152.                                  "[XvMC]   vert y period=%u\n" \
  153.                                  "[XvMC]   vert u period=%u\n" \
  154.                                  "[XvMC]   vert v period=%u\n",
  155.                                  subpictures[i].y_sample_bits, subpictures[i].u_sample_bits, subpictures[i].v_sample_bits,
  156.                                  subpictures[i].horz_y_period, subpictures[i].horz_u_period, subpictures[i].horz_v_period,
  157.                                  subpictures[i].vert_y_period, subpictures[i].vert_u_period, subpictures[i].vert_v_period);
  158.          }
  159.          break;
  160.       }
  161.    }
  162.  
  163.    free(subpictures);
  164.  
  165.    return i < num_subpics ? Success : BadMatch;
  166. }
  167.  
  168. static void
  169. upload_sampler(struct pipe_context *pipe, struct pipe_sampler_view *dst,
  170.                const struct pipe_box *dst_box, const void *src, unsigned src_stride,
  171.                unsigned src_x, unsigned src_y)
  172. {
  173.    struct pipe_transfer *transfer;
  174.    void *map;
  175.  
  176.    map = pipe->transfer_map(pipe, dst->texture, 0, PIPE_TRANSFER_WRITE,
  177.                             dst_box, &transfer);
  178.    if (!map)
  179.       return;
  180.  
  181.    util_copy_rect(map, dst->texture->format, transfer->stride, 0, 0,
  182.                   dst_box->width, dst_box->height,
  183.                   src, src_stride, src_x, src_y);
  184.  
  185.    pipe->transfer_unmap(pipe, transfer);
  186. }
  187.  
  188. PUBLIC
  189. Status XvMCCreateSubpicture(Display *dpy, XvMCContext *context, XvMCSubpicture *subpicture,
  190.                             unsigned short width, unsigned short height, int xvimage_id)
  191. {
  192.    XvMCContextPrivate *context_priv;
  193.    XvMCSubpicturePrivate *subpicture_priv;
  194.    struct pipe_context *pipe;
  195.    struct pipe_resource tex_templ, *tex;
  196.    struct pipe_sampler_view sampler_templ;
  197.    Status ret;
  198.  
  199.    XVMC_MSG(XVMC_TRACE, "[XvMC] Creating subpicture %p.\n", subpicture);
  200.  
  201.    assert(dpy);
  202.  
  203.    if (!context)
  204.       return XvMCBadContext;
  205.  
  206.    context_priv = context->privData;
  207.    pipe = context_priv->pipe;
  208.  
  209.    if (!subpicture)
  210.       return XvMCBadSubpicture;
  211.  
  212.    if (width > context_priv->subpicture_max_width ||
  213.        height > context_priv->subpicture_max_height)
  214.       return BadValue;
  215.  
  216.    ret = Validate(dpy, context->port, context->surface_type_id, xvimage_id);
  217.    if (ret != Success)
  218.       return ret;
  219.  
  220.    subpicture_priv = CALLOC(1, sizeof(XvMCSubpicturePrivate));
  221.    if (!subpicture_priv)
  222.       return BadAlloc;
  223.  
  224.    memset(&tex_templ, 0, sizeof(tex_templ));
  225.    tex_templ.target = PIPE_TEXTURE_2D;
  226.    tex_templ.format = XvIDToPipe(xvimage_id);
  227.    tex_templ.last_level = 0;
  228.    if (pipe->screen->get_video_param(pipe->screen,
  229.                                      PIPE_VIDEO_PROFILE_UNKNOWN,
  230.                                      PIPE_VIDEO_CAP_NPOT_TEXTURES)) {
  231.       tex_templ.width0 = width;
  232.       tex_templ.height0 = height;
  233.    }
  234.    else {
  235.       tex_templ.width0 = util_next_power_of_two(width);
  236.       tex_templ.height0 = util_next_power_of_two(height);
  237.    }
  238.    tex_templ.depth0 = 1;
  239.    tex_templ.array_size = 1;
  240.    tex_templ.usage = PIPE_USAGE_DYNAMIC;
  241.    tex_templ.bind = PIPE_BIND_SAMPLER_VIEW;
  242.    tex_templ.flags = 0;
  243.  
  244.    tex = pipe->screen->resource_create(pipe->screen, &tex_templ);
  245.  
  246.    memset(&sampler_templ, 0, sizeof(sampler_templ));
  247.    u_sampler_view_default_template(&sampler_templ, tex, tex->format);
  248.  
  249.    subpicture_priv->sampler = pipe->create_sampler_view(pipe, tex, &sampler_templ);
  250.    pipe_resource_reference(&tex, NULL);
  251.    if (!subpicture_priv->sampler) {
  252.       FREE(subpicture_priv);
  253.       return BadAlloc;
  254.    }
  255.  
  256.    subpicture_priv->context = context;
  257.    subpicture->subpicture_id = XAllocID(dpy);
  258.    subpicture->context_id = context->context_id;
  259.    subpicture->xvimage_id = xvimage_id;
  260.    subpicture->width = width;
  261.    subpicture->height = height;
  262.    subpicture->num_palette_entries = NumPaletteEntries4XvID(xvimage_id);
  263.    subpicture->entry_bytes = PipeToComponentOrder(tex_templ.format, subpicture->component_order);
  264.    subpicture->privData = subpicture_priv;
  265.  
  266.    if (subpicture->num_palette_entries > 0) {
  267.       tex_templ.target = PIPE_TEXTURE_1D;
  268.       tex_templ.format = PIPE_FORMAT_R8G8B8X8_UNORM;
  269.       tex_templ.width0 = subpicture->num_palette_entries;
  270.       tex_templ.height0 = 1;
  271.       tex_templ.usage = PIPE_USAGE_STATIC;
  272.  
  273.       tex = pipe->screen->resource_create(pipe->screen, &tex_templ);
  274.  
  275.       memset(&sampler_templ, 0, sizeof(sampler_templ));
  276.       u_sampler_view_default_template(&sampler_templ, tex, tex->format);
  277.       sampler_templ.swizzle_a = PIPE_SWIZZLE_ONE;
  278.       subpicture_priv->palette = pipe->create_sampler_view(pipe, tex, &sampler_templ);
  279.       pipe_resource_reference(&tex, NULL);
  280.       if (!subpicture_priv->sampler) {
  281.          FREE(subpicture_priv);
  282.          return BadAlloc;
  283.       }
  284.    }
  285.  
  286.    SyncHandle();
  287.  
  288.    XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p created.\n", subpicture);
  289.  
  290.    return Success;
  291. }
  292.  
  293. PUBLIC
  294. Status XvMCClearSubpicture(Display *dpy, XvMCSubpicture *subpicture, short x, short y,
  295.                            unsigned short width, unsigned short height, unsigned int color)
  296. {
  297.    XvMCSubpicturePrivate *subpicture_priv;
  298.    XvMCContextPrivate *context_priv;
  299.    struct pipe_context *pipe;
  300.    struct pipe_sampler_view *dst;
  301.    struct pipe_box dst_box = {x, y, 0, width, height, 1};
  302.    struct pipe_transfer *transfer;
  303.    union util_color uc;
  304.    void *map;
  305.  
  306.    assert(dpy);
  307.  
  308.    if (!subpicture)
  309.       return XvMCBadSubpicture;
  310.  
  311.    /* Convert color to float */
  312.    util_format_read_4f(PIPE_FORMAT_B8G8R8A8_UNORM,
  313.                        uc.f, 1, &color, 4,
  314.                        0, 0, 1, 1);
  315.  
  316.    subpicture_priv = subpicture->privData;
  317.    context_priv = subpicture_priv->context->privData;
  318.    pipe = context_priv->pipe;
  319.    dst = subpicture_priv->sampler;
  320.  
  321.    /* TODO: Assert clear rect is within bounds? Or clip? */
  322.    map = pipe->transfer_map(pipe, dst->texture, 0, PIPE_TRANSFER_WRITE,
  323.                             &dst_box, &transfer);
  324.    if (!map)
  325.       return XvMCBadSubpicture;
  326.  
  327.    util_fill_rect(map, dst->texture->format, transfer->stride, 0, 0,
  328.                   dst_box.width, dst_box.height, &uc);
  329.  
  330.    pipe->transfer_unmap(pipe, transfer);
  331.    return Success;
  332. }
  333.  
  334. PUBLIC
  335. Status XvMCCompositeSubpicture(Display *dpy, XvMCSubpicture *subpicture, XvImage *image,
  336.                                short srcx, short srcy, unsigned short width, unsigned short height,
  337.                                short dstx, short dsty)
  338. {
  339.    XvMCSubpicturePrivate *subpicture_priv;
  340.    XvMCContextPrivate *context_priv;
  341.    struct pipe_context *pipe;
  342.    struct pipe_box dst_box = {dstx, dsty, 0, width, height, 1};
  343.    unsigned src_stride;
  344.  
  345.    XVMC_MSG(XVMC_TRACE, "[XvMC] Compositing subpicture %p.\n", subpicture);
  346.  
  347.    assert(dpy);
  348.  
  349.    if (!subpicture)
  350.       return XvMCBadSubpicture;
  351.  
  352.    assert(image);
  353.  
  354.    if (subpicture->xvimage_id != image->id)
  355.       return BadMatch;
  356.  
  357.    /* No planar support for now */
  358.    if (image->num_planes != 1)
  359.       return BadMatch;
  360.  
  361.    subpicture_priv = subpicture->privData;
  362.    context_priv = subpicture_priv->context->privData;
  363.    pipe = context_priv->pipe;
  364.  
  365.    /* clipping should be done by upload_sampler and regardles what the documentation
  366.    says image->pitches[0] doesn't seems to be in bytes, so don't use it */
  367.    src_stride = image->width * util_format_get_blocksize(subpicture_priv->sampler->texture->format);
  368.    upload_sampler(pipe, subpicture_priv->sampler, &dst_box, image->data, src_stride, srcx, srcy);
  369.  
  370.    XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p composited.\n", subpicture);
  371.  
  372.    return Success;
  373. }
  374.  
  375. PUBLIC
  376. Status XvMCDestroySubpicture(Display *dpy, XvMCSubpicture *subpicture)
  377. {
  378.    XvMCSubpicturePrivate *subpicture_priv;
  379.  
  380.    XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying subpicture %p.\n", subpicture);
  381.  
  382.    assert(dpy);
  383.  
  384.    if (!subpicture)
  385.       return XvMCBadSubpicture;
  386.  
  387.    subpicture_priv = subpicture->privData;
  388.    pipe_sampler_view_reference(&subpicture_priv->sampler, NULL);
  389.    pipe_sampler_view_reference(&subpicture_priv->palette, NULL);
  390.    FREE(subpicture_priv);
  391.  
  392.    XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p destroyed.\n", subpicture);
  393.  
  394.    return Success;
  395. }
  396.  
  397. PUBLIC
  398. Status XvMCSetSubpicturePalette(Display *dpy, XvMCSubpicture *subpicture, unsigned char *palette)
  399. {
  400.    XvMCSubpicturePrivate *subpicture_priv;
  401.    XvMCContextPrivate *context_priv;
  402.    struct pipe_context *pipe;
  403.    struct pipe_box dst_box = {0, 0, 0, 0, 1, 1};
  404.  
  405.    assert(dpy);
  406.    assert(palette);
  407.  
  408.    if (!subpicture)
  409.       return XvMCBadSubpicture;
  410.  
  411.    subpicture_priv = subpicture->privData;
  412.    context_priv = subpicture_priv->context->privData;
  413.    pipe = context_priv->pipe;
  414.  
  415.    dst_box.width = subpicture->num_palette_entries;
  416.  
  417.    upload_sampler(pipe, subpicture_priv->palette, &dst_box, palette, 0, 0, 0);
  418.  
  419.    XVMC_MSG(XVMC_TRACE, "[XvMC] Palette of Subpicture %p set.\n", subpicture);
  420.  
  421.    return Success;
  422. }
  423.  
  424. PUBLIC
  425. Status XvMCBlendSubpicture(Display *dpy, XvMCSurface *target_surface, XvMCSubpicture *subpicture,
  426.                            short subx, short suby, unsigned short subw, unsigned short subh,
  427.                            short surfx, short surfy, unsigned short surfw, unsigned short surfh)
  428. {
  429.    struct u_rect src_rect = {subx, subx + subw, suby, suby + subh};
  430.    struct u_rect dst_rect = {surfx, surfx + surfw, surfy, surfy + surfh};
  431.  
  432.    XvMCSurfacePrivate *surface_priv;
  433.    XvMCSubpicturePrivate *subpicture_priv;
  434.  
  435.    XVMC_MSG(XVMC_TRACE, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture, target_surface);
  436.  
  437.    assert(dpy);
  438.  
  439.    if (!target_surface)
  440.       return XvMCBadSurface;
  441.  
  442.    if (!subpicture)
  443.       return XvMCBadSubpicture;
  444.  
  445.    if (target_surface->context_id != subpicture->context_id)
  446.       return BadMatch;
  447.  
  448.    /* TODO: Verify against subpicture independent scaling */
  449.  
  450.    surface_priv = target_surface->privData;
  451.    subpicture_priv = subpicture->privData;
  452.  
  453.    /* TODO: Assert rects are within bounds? Or clip? */
  454.    subpicture_priv->src_rect = src_rect;
  455.    subpicture_priv->dst_rect = dst_rect;
  456.  
  457.    surface_priv->subpicture = subpicture;
  458.    subpicture_priv->surface = target_surface;
  459.  
  460.    return Success;
  461. }
  462.  
  463. PUBLIC
  464. Status XvMCBlendSubpicture2(Display *dpy, XvMCSurface *source_surface, XvMCSurface *target_surface,
  465.                             XvMCSubpicture *subpicture, short subx, short suby, unsigned short subw, unsigned short subh,
  466.                             short surfx, short surfy, unsigned short surfw, unsigned short surfh)
  467. {
  468.    assert(dpy);
  469.  
  470.    if (!source_surface || !target_surface)
  471.       return XvMCBadSurface;
  472.  
  473.    if (!subpicture)
  474.       return XvMCBadSubpicture;
  475.  
  476.    if (source_surface->context_id != subpicture->context_id)
  477.       return BadMatch;
  478.  
  479.    if (source_surface->context_id != subpicture->context_id)
  480.       return BadMatch;
  481.  
  482.    /* TODO: Assert rects are within bounds? Or clip? */
  483.  
  484.    return Success;
  485. }
  486.  
  487. PUBLIC
  488. Status XvMCSyncSubpicture(Display *dpy, XvMCSubpicture *subpicture)
  489. {
  490.    assert(dpy);
  491.  
  492.    if (!subpicture)
  493.       return XvMCBadSubpicture;
  494.  
  495.    return Success;
  496. }
  497.  
  498. PUBLIC
  499. Status XvMCFlushSubpicture(Display *dpy, XvMCSubpicture *subpicture)
  500. {
  501.    assert(dpy);
  502.  
  503.    if (!subpicture)
  504.       return XvMCBadSubpicture;
  505.  
  506.    return Success;
  507. }
  508.  
  509. PUBLIC
  510. Status XvMCGetSubpictureStatus(Display *dpy, XvMCSubpicture *subpicture, int *status)
  511. {
  512.    assert(dpy);
  513.  
  514.    if (!subpicture)
  515.       return XvMCBadSubpicture;
  516.  
  517.    assert(status);
  518.  
  519.    /* TODO */
  520.    *status = 0;
  521.  
  522.    return Success;
  523. }
  524.