Path: blob/21.2-virgl/src/gallium/frontends/va/subpicture.c
4561 views
/**************************************************************************1*2* Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.3* Copyright 2014 Advanced Micro Devices, Inc.4* All Rights Reserved.5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the8* "Software"), to deal in the Software without restriction, including9* without limitation the rights to use, copy, modify, merge, publish,10* distribute, sub license, and/or sell copies of the Software, and to11* permit persons to whom the Software is furnished to do so, subject to12* the following conditions:13*14* The above copyright notice and this permission notice (including the15* next paragraph) shall be included in all copies or substantial portions16* of the Software.17*18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS19* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF20* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.21* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR22* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,23* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE24* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.25*26**************************************************************************/2728#include "util/u_memory.h"29#include "util/u_handle_table.h"30#include "util/u_sampler.h"3132#include "va_private.h"3334static VAImageFormat subpic_formats[] = {35{36.fourcc = VA_FOURCC_BGRA,37.byte_order = VA_LSB_FIRST,38.bits_per_pixel = 32,39.depth = 32,40.red_mask = 0x00ff0000ul,41.green_mask = 0x0000ff00ul,42.blue_mask = 0x000000fful,43.alpha_mask = 0xff000000ul,44},45};4647VAStatus48vlVaQuerySubpictureFormats(VADriverContextP ctx, VAImageFormat *format_list,49unsigned int *flags, unsigned int *num_formats)50{51if (!ctx)52return VA_STATUS_ERROR_INVALID_CONTEXT;5354if (!(format_list && flags && num_formats))55return VA_STATUS_ERROR_UNKNOWN;5657*num_formats = sizeof(subpic_formats)/sizeof(VAImageFormat);58memcpy(format_list, subpic_formats, sizeof(subpic_formats));5960return VA_STATUS_SUCCESS;61}6263VAStatus64vlVaCreateSubpicture(VADriverContextP ctx, VAImageID image,65VASubpictureID *subpicture)66{67vlVaDriver *drv;68vlVaSubpicture *sub;69VAImage *img;7071if (!ctx)72return VA_STATUS_ERROR_INVALID_CONTEXT;7374drv = VL_VA_DRIVER(ctx);75mtx_lock(&drv->mutex);76img = handle_table_get(drv->htab, image);77if (!img) {78mtx_unlock(&drv->mutex);79return VA_STATUS_ERROR_INVALID_IMAGE;80}8182sub = CALLOC(1, sizeof(*sub));83if (!sub) {84mtx_unlock(&drv->mutex);85return VA_STATUS_ERROR_ALLOCATION_FAILED;86}8788sub->image = img;89*subpicture = handle_table_add(VL_VA_DRIVER(ctx)->htab, sub);90mtx_unlock(&drv->mutex);9192return VA_STATUS_SUCCESS;93}9495VAStatus96vlVaDestroySubpicture(VADriverContextP ctx, VASubpictureID subpicture)97{98vlVaDriver *drv;99vlVaSubpicture *sub;100101if (!ctx)102return VA_STATUS_ERROR_INVALID_CONTEXT;103104drv = VL_VA_DRIVER(ctx);105mtx_lock(&drv->mutex);106107sub = handle_table_get(drv->htab, subpicture);108if (!sub) {109mtx_unlock(&drv->mutex);110return VA_STATUS_ERROR_INVALID_SUBPICTURE;111}112113FREE(sub);114handle_table_remove(drv->htab, subpicture);115mtx_unlock(&drv->mutex);116117return VA_STATUS_SUCCESS;118}119120VAStatus121vlVaSubpictureImage(VADriverContextP ctx, VASubpictureID subpicture, VAImageID image)122{123vlVaDriver *drv;124vlVaSubpicture *sub;125VAImage *img;126127if (!ctx)128return VA_STATUS_ERROR_INVALID_CONTEXT;129130drv = VL_VA_DRIVER(ctx);131mtx_lock(&drv->mutex);132133img = handle_table_get(drv->htab, image);134if (!img) {135mtx_unlock(&drv->mutex);136return VA_STATUS_ERROR_INVALID_IMAGE;137}138139sub = handle_table_get(drv->htab, subpicture);140mtx_unlock(&drv->mutex);141if (!sub)142return VA_STATUS_ERROR_INVALID_SUBPICTURE;143144sub->image = img;145146return VA_STATUS_SUCCESS;147}148149VAStatus150vlVaSetSubpictureChromakey(VADriverContextP ctx, VASubpictureID subpicture,151unsigned int chromakey_min, unsigned int chromakey_max, unsigned int chromakey_mask)152{153if (!ctx)154return VA_STATUS_ERROR_INVALID_CONTEXT;155156return VA_STATUS_ERROR_UNIMPLEMENTED;157}158159VAStatus160vlVaSetSubpictureGlobalAlpha(VADriverContextP ctx, VASubpictureID subpicture, float global_alpha)161{162if (!ctx)163return VA_STATUS_ERROR_INVALID_CONTEXT;164165return VA_STATUS_ERROR_UNIMPLEMENTED;166}167168VAStatus169vlVaAssociateSubpicture(VADriverContextP ctx, VASubpictureID subpicture,170VASurfaceID *target_surfaces, int num_surfaces,171short src_x, short src_y, unsigned short src_width,172unsigned short src_height, short dest_x, short dest_y,173unsigned short dest_width, unsigned short dest_height,174unsigned int flags)175{176vlVaSubpicture *sub;177struct pipe_resource tex_temp, *tex;178struct pipe_sampler_view sampler_templ;179vlVaDriver *drv;180vlVaSurface *surf;181int i;182struct u_rect src_rect = {src_x, src_x + src_width, src_y, src_y + src_height};183struct u_rect dst_rect = {dest_x, dest_x + dest_width, dest_y, dest_y + dest_height};184185if (!ctx)186return VA_STATUS_ERROR_INVALID_CONTEXT;187drv = VL_VA_DRIVER(ctx);188mtx_lock(&drv->mutex);189190sub = handle_table_get(drv->htab, subpicture);191if (!sub) {192mtx_unlock(&drv->mutex);193return VA_STATUS_ERROR_INVALID_SUBPICTURE;194}195196for (i = 0; i < num_surfaces; i++) {197surf = handle_table_get(drv->htab, target_surfaces[i]);198if (!surf) {199mtx_unlock(&drv->mutex);200return VA_STATUS_ERROR_INVALID_SURFACE;201}202}203204sub->src_rect = src_rect;205sub->dst_rect = dst_rect;206207memset(&tex_temp, 0, sizeof(tex_temp));208tex_temp.target = PIPE_TEXTURE_2D;209tex_temp.format = PIPE_FORMAT_B8G8R8A8_UNORM;210tex_temp.last_level = 0;211tex_temp.width0 = src_width;212tex_temp.height0 = src_height;213tex_temp.depth0 = 1;214tex_temp.array_size = 1;215tex_temp.usage = PIPE_USAGE_DYNAMIC;216tex_temp.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;217tex_temp.flags = 0;218if (!drv->pipe->screen->is_format_supported(219drv->pipe->screen, tex_temp.format, tex_temp.target,220tex_temp.nr_samples, tex_temp.nr_storage_samples, tex_temp.bind)) {221mtx_unlock(&drv->mutex);222return VA_STATUS_ERROR_ALLOCATION_FAILED;223}224225tex = drv->pipe->screen->resource_create(drv->pipe->screen, &tex_temp);226227memset(&sampler_templ, 0, sizeof(sampler_templ));228u_sampler_view_default_template(&sampler_templ, tex, tex->format);229sub->sampler = drv->pipe->create_sampler_view(drv->pipe, tex, &sampler_templ);230pipe_resource_reference(&tex, NULL);231if (!sub->sampler) {232mtx_unlock(&drv->mutex);233return VA_STATUS_ERROR_ALLOCATION_FAILED;234}235236for (i = 0; i < num_surfaces; i++) {237surf = handle_table_get(drv->htab, target_surfaces[i]);238util_dynarray_append(&surf->subpics, vlVaSubpicture *, sub);239}240mtx_unlock(&drv->mutex);241242return VA_STATUS_SUCCESS;243}244245VAStatus246vlVaDeassociateSubpicture(VADriverContextP ctx, VASubpictureID subpicture,247VASurfaceID *target_surfaces, int num_surfaces)248{249int i;250int j;251vlVaSurface *surf;252vlVaSubpicture *sub, **array;253vlVaDriver *drv;254255if (!ctx)256return VA_STATUS_ERROR_INVALID_CONTEXT;257drv = VL_VA_DRIVER(ctx);258mtx_lock(&drv->mutex);259260sub = handle_table_get(drv->htab, subpicture);261if (!sub) {262mtx_unlock(&drv->mutex);263return VA_STATUS_ERROR_INVALID_SUBPICTURE;264}265266for (i = 0; i < num_surfaces; i++) {267surf = handle_table_get(drv->htab, target_surfaces[i]);268if (!surf) {269mtx_unlock(&drv->mutex);270return VA_STATUS_ERROR_INVALID_SURFACE;271}272273array = surf->subpics.data;274if (!array)275continue;276277for (j = 0; j < surf->subpics.size/sizeof(vlVaSubpicture *); j++) {278if (array[j] == sub)279array[j] = NULL;280}281282while (surf->subpics.size && util_dynarray_top(&surf->subpics, vlVaSubpicture *) == NULL)283(void)util_dynarray_pop(&surf->subpics, vlVaSubpicture *);284}285pipe_sampler_view_reference(&sub->sampler,NULL);286mtx_unlock(&drv->mutex);287288return VA_STATUS_SUCCESS;289}290291292