Path: blob/21.2-virgl/src/gallium/frontends/xvmc/subpicture.c
4565 views
/**************************************************************************1*2* Copyright 2009 Younes Manton.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include <assert.h>2829#include <X11/Xlibint.h>30#include <X11/extensions/XvMClib.h>3132#include "pipe/p_screen.h"33#include "pipe/p_video_codec.h"34#include "pipe/p_state.h"3536#include "util/u_memory.h"37#include "util/u_math.h"38#include "util/format/u_format.h"39#include "util/u_sampler.h"40#include "util/u_surface.h"41#include "util/u_rect.h"42#include "vl/vl_winsys.h"4344#include "xvmc_private.h"4546#define FOURCC_RGB 0x000000347#define FOURCC_AI44 0x3434494148#define FOURCC_IA44 0x343441494950static enum pipe_format XvIDToPipe(struct pipe_screen *screen,51int xvimage_id)52{53enum pipe_format ret;54assert(screen);5556switch (xvimage_id) {57case FOURCC_RGB:58ret = PIPE_FORMAT_B8G8R8X8_UNORM;59break;6061case FOURCC_AI44:62ret = PIPE_FORMAT_R4A4_UNORM;63if (!screen->is_format_supported(64screen, ret, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW))65ret = PIPE_FORMAT_B4G4R4A4_UNORM;66break;6768case FOURCC_IA44:69ret = PIPE_FORMAT_A4R4_UNORM;70if (!screen->is_format_supported(71screen, ret, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW))72ret = PIPE_FORMAT_B4G4R4A4_UNORM;73break;7475default:76XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);77return PIPE_FORMAT_NONE;78}7980if (!screen->is_format_supported(81screen, ret, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW)) {82XVMC_MSG(XVMC_ERR, "[XvMC] Unsupported 2D format %s for Xv image ID 0x%08X.\n", util_format_name(ret), xvimage_id);83ret = PIPE_FORMAT_NONE;84}85return ret;8687}8889static unsigned NumPaletteEntries4XvID(int xvimage_id)90{91switch (xvimage_id) {92case FOURCC_RGB:93return 0;9495case FOURCC_AI44:96case FOURCC_IA44:97return 16;9899default:100XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);101return 0;102}103}104105static int PipeToComponentOrder(struct pipe_screen *screen,106enum pipe_format format,107enum pipe_format *palette_format,108char *component_order)109{110assert(screen);111assert(component_order);112assert(palette_format);113114switch (format) {115case PIPE_FORMAT_B8G8R8X8_UNORM:116return 0;117118case PIPE_FORMAT_A4R4_UNORM:119case PIPE_FORMAT_R4A4_UNORM:120case PIPE_FORMAT_B4G4R4A4_UNORM:121*palette_format = PIPE_FORMAT_R8G8B8X8_UNORM;122component_order[0] = 'Y';123component_order[1] = 'U';124component_order[2] = 'V';125component_order[3] = 'A';126if (!screen->is_format_supported(127screen, *palette_format, PIPE_TEXTURE_1D, 0, 0,128PIPE_BIND_SAMPLER_VIEW)) {129/* One of these formats better be supported... */130*palette_format = PIPE_FORMAT_B8G8R8X8_UNORM;131component_order[0] = 'V';132component_order[2] = 'Y';133}134return 4;135136default:137XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized PIPE_FORMAT 0x%08X.\n", format);138component_order[0] = 0;139component_order[1] = 0;140component_order[2] = 0;141component_order[3] = 0;142return 0;143}144}145146static Status Validate(Display *dpy, XvPortID port, int surface_type_id, int xvimage_id)147{148XvImageFormatValues *subpictures;149int num_subpics;150int i;151152subpictures = XvMCListSubpictureTypes(dpy, port, surface_type_id, &num_subpics);153if (num_subpics < 1) {154free(subpictures);155return BadMatch;156}157if (!subpictures)158return BadAlloc;159160for (i = 0; i < num_subpics; ++i) {161if (subpictures[i].id == xvimage_id) {162XVMC_MSG(XVMC_TRACE, "[XvMC] Found requested subpicture format.\n" \163"[XvMC] port=%u\n" \164"[XvMC] surface id=0x%08X\n" \165"[XvMC] image id=0x%08X\n" \166"[XvMC] type=%08X\n" \167"[XvMC] byte order=%08X\n" \168"[XvMC] bits per pixel=%u\n" \169"[XvMC] format=%08X\n" \170"[XvMC] num planes=%d\n",171port, surface_type_id, xvimage_id, subpictures[i].type, subpictures[i].byte_order,172subpictures[i].bits_per_pixel, subpictures[i].format, subpictures[i].num_planes);173if (subpictures[i].type == XvRGB) {174XVMC_MSG(XVMC_TRACE, "[XvMC] depth=%d\n" \175"[XvMC] red mask=0x%08X\n" \176"[XvMC] green mask=0x%08X\n" \177"[XvMC] blue mask=0x%08X\n",178subpictures[i].depth, subpictures[i].red_mask,179subpictures[i].green_mask, subpictures[i].blue_mask);180}181else if (subpictures[i].type == XvYUV) {182XVMC_MSG(XVMC_TRACE, "[XvMC] y sample bits=0x%08X\n" \183"[XvMC] u sample bits=0x%08X\n" \184"[XvMC] v sample bits=0x%08X\n" \185"[XvMC] horz y period=%u\n" \186"[XvMC] horz u period=%u\n" \187"[XvMC] horz v period=%u\n" \188"[XvMC] vert y period=%u\n" \189"[XvMC] vert u period=%u\n" \190"[XvMC] vert v period=%u\n",191subpictures[i].y_sample_bits, subpictures[i].u_sample_bits, subpictures[i].v_sample_bits,192subpictures[i].horz_y_period, subpictures[i].horz_u_period, subpictures[i].horz_v_period,193subpictures[i].vert_y_period, subpictures[i].vert_u_period, subpictures[i].vert_v_period);194}195break;196}197}198199free(subpictures);200201return i < num_subpics ? Success : BadMatch;202}203204static void205upload_sampler(struct pipe_context *pipe, struct pipe_sampler_view *dst,206const struct pipe_box *dst_box, const void *src, unsigned src_stride,207unsigned src_x, unsigned src_y)208{209struct pipe_transfer *transfer;210void *map;211212map = pipe->texture_map(pipe, dst->texture, 0, PIPE_MAP_WRITE,213dst_box, &transfer);214if (!map)215return;216217util_copy_rect(map, dst->texture->format, transfer->stride, 0, 0,218dst_box->width, dst_box->height,219src, src_stride, src_x, src_y);220221pipe->texture_unmap(pipe, transfer);222}223224static void225upload_sampler_convert(struct pipe_context *pipe, struct pipe_sampler_view *dst,226const struct pipe_box *dst_box, const XvImage *image,227unsigned src_x, unsigned src_y)228{229struct pipe_transfer *transfer;230int i, j;231char *map, *src;232233map = pipe->texture_map(pipe, dst->texture, 0, PIPE_MAP_WRITE,234dst_box, &transfer);235if (!map)236return;237238src = image->data;239src += src_y * image->width + src_x;240if (image->id == FOURCC_AI44) {241/* The format matches what we want, we just have to insert dummy242* bytes. So just copy the same value in twice.243*/244for (i = 0; i < dst_box->height; i++, map += transfer->stride, src += image->width)245for (j = 0; j < dst_box->width; j++)246map[j * 2 + 0] = map[j * 2 + 1] = src[j];247} else {248assert(image->id == FOURCC_IA44);249/* Same idea as above, but we have to swap the low and high nibbles.250*/251for (i = 0; i < dst_box->height; i++, map += transfer->stride, src += image->width)252for (j = 0; j < dst_box->width; j++)253map[j * 2 + 0] = map[j * 2 + 1] = (src[j] >> 4) | (src[j] << 4);254}255256pipe->texture_unmap(pipe, transfer);257}258259PUBLIC260Status XvMCCreateSubpicture(Display *dpy, XvMCContext *context, XvMCSubpicture *subpicture,261unsigned short width, unsigned short height, int xvimage_id)262{263XvMCContextPrivate *context_priv;264XvMCSubpicturePrivate *subpicture_priv;265struct pipe_context *pipe;266struct pipe_resource tex_templ, *tex;267struct pipe_sampler_view sampler_templ;268enum pipe_format palette_format;269Status ret;270271XVMC_MSG(XVMC_TRACE, "[XvMC] Creating subpicture %p.\n", subpicture);272273assert(dpy);274275if (!context)276return XvMCBadContext;277278context_priv = context->privData;279pipe = context_priv->pipe;280281if (!subpicture)282return XvMCBadSubpicture;283284if (width > context_priv->subpicture_max_width ||285height > context_priv->subpicture_max_height)286return BadValue;287288ret = Validate(dpy, context->port, context->surface_type_id, xvimage_id);289if (ret != Success)290return ret;291292subpicture_priv = CALLOC(1, sizeof(XvMCSubpicturePrivate));293if (!subpicture_priv)294return BadAlloc;295296memset(&tex_templ, 0, sizeof(tex_templ));297tex_templ.target = PIPE_TEXTURE_2D;298tex_templ.format = XvIDToPipe(pipe->screen, xvimage_id);299tex_templ.last_level = 0;300if (pipe->screen->get_video_param(pipe->screen,301PIPE_VIDEO_PROFILE_UNKNOWN,302PIPE_VIDEO_ENTRYPOINT_UNKNOWN,303PIPE_VIDEO_CAP_NPOT_TEXTURES)) {304tex_templ.width0 = width;305tex_templ.height0 = height;306}307else {308tex_templ.width0 = util_next_power_of_two(width);309tex_templ.height0 = util_next_power_of_two(height);310}311tex_templ.depth0 = 1;312tex_templ.array_size = 1;313tex_templ.usage = PIPE_USAGE_DYNAMIC;314tex_templ.bind = PIPE_BIND_SAMPLER_VIEW;315tex_templ.flags = 0;316317tex = pipe->screen->resource_create(pipe->screen, &tex_templ);318319memset(&sampler_templ, 0, sizeof(sampler_templ));320u_sampler_view_default_template(&sampler_templ, tex, tex->format);321322subpicture_priv->sampler = pipe->create_sampler_view(pipe, tex, &sampler_templ);323pipe_resource_reference(&tex, NULL);324if (!subpicture_priv->sampler) {325FREE(subpicture_priv);326return BadAlloc;327}328329subpicture_priv->context = context;330subpicture->subpicture_id = XAllocID(dpy);331subpicture->context_id = context->context_id;332subpicture->xvimage_id = xvimage_id;333subpicture->width = width;334subpicture->height = height;335subpicture->num_palette_entries = NumPaletteEntries4XvID(xvimage_id);336subpicture->entry_bytes = PipeToComponentOrder(337pipe->screen, tex_templ.format, &palette_format,338subpicture->component_order);339subpicture->privData = subpicture_priv;340341if (subpicture->num_palette_entries > 0) {342tex_templ.target = PIPE_TEXTURE_1D;343tex_templ.format = palette_format;344tex_templ.width0 = subpicture->num_palette_entries;345tex_templ.height0 = 1;346tex_templ.usage = PIPE_USAGE_DEFAULT;347348tex = pipe->screen->resource_create(pipe->screen, &tex_templ);349350memset(&sampler_templ, 0, sizeof(sampler_templ));351u_sampler_view_default_template(&sampler_templ, tex, tex->format);352sampler_templ.swizzle_a = PIPE_SWIZZLE_1;353subpicture_priv->palette = pipe->create_sampler_view(pipe, tex, &sampler_templ);354pipe_resource_reference(&tex, NULL);355if (!subpicture_priv->sampler) {356FREE(subpicture_priv);357return BadAlloc;358}359}360361SyncHandle();362363XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p created.\n", subpicture);364365return Success;366}367368PUBLIC369Status XvMCClearSubpicture(Display *dpy, XvMCSubpicture *subpicture, short x, short y,370unsigned short width, unsigned short height, unsigned int color)371{372XvMCSubpicturePrivate *subpicture_priv;373XvMCContextPrivate *context_priv;374struct pipe_context *pipe;375struct pipe_sampler_view *dst;376struct pipe_box dst_box = {x, y, 0, width, height, 1};377struct pipe_transfer *transfer;378union util_color uc;379void *map;380381assert(dpy);382383if (!subpicture)384return XvMCBadSubpicture;385386/* Convert color to float */387util_format_unpack_rgba(PIPE_FORMAT_B8G8R8A8_UNORM, uc.f, &color, 1);388389subpicture_priv = subpicture->privData;390context_priv = subpicture_priv->context->privData;391pipe = context_priv->pipe;392dst = subpicture_priv->sampler;393394/* TODO: Assert clear rect is within bounds? Or clip? */395map = pipe->texture_map(pipe, dst->texture, 0, PIPE_MAP_WRITE,396&dst_box, &transfer);397if (!map)398return XvMCBadSubpicture;399400util_fill_rect(map, dst->texture->format, transfer->stride, 0, 0,401dst_box.width, dst_box.height, &uc);402403pipe->texture_unmap(pipe, transfer);404return Success;405}406407PUBLIC408Status XvMCCompositeSubpicture(Display *dpy, XvMCSubpicture *subpicture, XvImage *image,409short srcx, short srcy, unsigned short width, unsigned short height,410short dstx, short dsty)411{412XvMCSubpicturePrivate *subpicture_priv;413XvMCContextPrivate *context_priv;414struct pipe_context *pipe;415struct pipe_box dst_box = {dstx, dsty, 0, width, height, 1};416unsigned src_stride;417418XVMC_MSG(XVMC_TRACE, "[XvMC] Compositing subpicture %p.\n", subpicture);419420assert(dpy);421422if (!subpicture)423return XvMCBadSubpicture;424425assert(image);426427if (subpicture->xvimage_id != image->id)428return BadMatch;429430/* No planar support for now */431if (image->num_planes != 1)432return BadMatch;433434subpicture_priv = subpicture->privData;435context_priv = subpicture_priv->context->privData;436pipe = context_priv->pipe;437438/* clipping should be done by upload_sampler and regardles what the documentation439says image->pitches[0] doesn't seems to be in bytes, so don't use it */440if ((image->id == FOURCC_IA44 || image->id == FOURCC_AI44) &&441subpicture_priv->sampler->texture->format == PIPE_FORMAT_B4G4R4A4_UNORM) {442upload_sampler_convert(pipe, subpicture_priv->sampler, &dst_box, image, srcx, srcy);443} else {444src_stride = image->width * util_format_get_blocksize(subpicture_priv->sampler->texture->format);445upload_sampler(pipe, subpicture_priv->sampler, &dst_box, image->data, src_stride, srcx, srcy);446}447448XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p composited.\n", subpicture);449450return Success;451}452453PUBLIC454Status XvMCDestroySubpicture(Display *dpy, XvMCSubpicture *subpicture)455{456XvMCSubpicturePrivate *subpicture_priv;457458XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying subpicture %p.\n", subpicture);459460assert(dpy);461462if (!subpicture)463return XvMCBadSubpicture;464465subpicture_priv = subpicture->privData;466pipe_sampler_view_reference(&subpicture_priv->sampler, NULL);467pipe_sampler_view_reference(&subpicture_priv->palette, NULL);468FREE(subpicture_priv);469470XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p destroyed.\n", subpicture);471472return Success;473}474475PUBLIC476Status XvMCSetSubpicturePalette(Display *dpy, XvMCSubpicture *subpicture, unsigned char *palette)477{478XvMCSubpicturePrivate *subpicture_priv;479XvMCContextPrivate *context_priv;480struct pipe_context *pipe;481struct pipe_box dst_box = {0, 0, 0, 0, 1, 1};482483assert(dpy);484assert(palette);485486if (!subpicture)487return XvMCBadSubpicture;488489subpicture_priv = subpicture->privData;490context_priv = subpicture_priv->context->privData;491pipe = context_priv->pipe;492493dst_box.width = subpicture->num_palette_entries;494495upload_sampler(pipe, subpicture_priv->palette, &dst_box, palette, 0, 0, 0);496497XVMC_MSG(XVMC_TRACE, "[XvMC] Palette of Subpicture %p set.\n", subpicture);498499return Success;500}501502PUBLIC503Status XvMCBlendSubpicture(Display *dpy, XvMCSurface *target_surface, XvMCSubpicture *subpicture,504short subx, short suby, unsigned short subw, unsigned short subh,505short surfx, short surfy, unsigned short surfw, unsigned short surfh)506{507struct u_rect src_rect = {subx, subx + subw, suby, suby + subh};508struct u_rect dst_rect = {surfx, surfx + surfw, surfy, surfy + surfh};509510XvMCSurfacePrivate *surface_priv;511XvMCSubpicturePrivate *subpicture_priv;512513XVMC_MSG(XVMC_TRACE, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture, target_surface);514515assert(dpy);516517if (!target_surface)518return XvMCBadSurface;519520if (!subpicture)521return XvMCBadSubpicture;522523if (target_surface->context_id != subpicture->context_id)524return BadMatch;525526/* TODO: Verify against subpicture independent scaling */527528surface_priv = target_surface->privData;529subpicture_priv = subpicture->privData;530531/* TODO: Assert rects are within bounds? Or clip? */532subpicture_priv->src_rect = src_rect;533subpicture_priv->dst_rect = dst_rect;534535surface_priv->subpicture = subpicture;536subpicture_priv->surface = target_surface;537538return Success;539}540541PUBLIC542Status XvMCBlendSubpicture2(Display *dpy, XvMCSurface *source_surface, XvMCSurface *target_surface,543XvMCSubpicture *subpicture, short subx, short suby, unsigned short subw, unsigned short subh,544short surfx, short surfy, unsigned short surfw, unsigned short surfh)545{546assert(dpy);547548if (!source_surface || !target_surface)549return XvMCBadSurface;550551if (!subpicture)552return XvMCBadSubpicture;553554if (source_surface->context_id != subpicture->context_id)555return BadMatch;556557if (source_surface->context_id != subpicture->context_id)558return BadMatch;559560/* TODO: Assert rects are within bounds? Or clip? */561562return Success;563}564565PUBLIC566Status XvMCSyncSubpicture(Display *dpy, XvMCSubpicture *subpicture)567{568assert(dpy);569570if (!subpicture)571return XvMCBadSubpicture;572573return Success;574}575576PUBLIC577Status XvMCFlushSubpicture(Display *dpy, XvMCSubpicture *subpicture)578{579assert(dpy);580581if (!subpicture)582return XvMCBadSubpicture;583584return Success;585}586587PUBLIC588Status XvMCGetSubpictureStatus(Display *dpy, XvMCSubpicture *subpicture, int *status)589{590assert(dpy);591592if (!subpicture)593return XvMCBadSubpicture;594595assert(status);596597/* TODO */598*status = 0;599600return Success;601}602603604