Path: blob/21.2-virgl/src/gallium/auxiliary/vl/vl_video_buffer.c
4565 views
/**************************************************************************1*2* Copyright 2011 Christian König.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 "pipe/p_screen.h"30#include "pipe/p_context.h"31#include "pipe/p_state.h"3233#include "util/format/u_format.h"34#include "util/u_inlines.h"35#include "util/u_sampler.h"36#include "util/u_memory.h"3738#include "vl_video_buffer.h"3940const unsigned const_resource_plane_order_YUV[3] = {410,421,43244};4546const unsigned const_resource_plane_order_YVU[3] = {470,482,49150};5152void53vl_get_video_buffer_formats(struct pipe_screen *screen, enum pipe_format format,54enum pipe_format out_format[VL_NUM_COMPONENTS])55{56unsigned num_planes = util_format_get_num_planes(format);57unsigned i;5859for (i = 0; i < num_planes; i++)60out_format[i] = util_format_get_plane_format(format, i);61for (; i < VL_NUM_COMPONENTS; i++)62out_format[i] = PIPE_FORMAT_NONE;6364if (format == PIPE_FORMAT_YUYV)65out_format[0] = PIPE_FORMAT_R8G8_R8B8_UNORM;66else if (format == PIPE_FORMAT_UYVY)67out_format[0] = PIPE_FORMAT_G8R8_B8R8_UNORM;68}6970const unsigned *71vl_video_buffer_plane_order(enum pipe_format format)72{73switch(format) {74case PIPE_FORMAT_YV12:75return const_resource_plane_order_YVU;7677case PIPE_FORMAT_NV12:78case PIPE_FORMAT_R8G8B8A8_UNORM:79case PIPE_FORMAT_B8G8R8A8_UNORM:80case PIPE_FORMAT_YUYV:81case PIPE_FORMAT_UYVY:82case PIPE_FORMAT_P010:83case PIPE_FORMAT_P016:84return const_resource_plane_order_YUV;8586default:87return NULL;88}89}9091static enum pipe_format92vl_video_buffer_surface_format(enum pipe_format format)93{94const struct util_format_description *desc = util_format_description(format);9596/* a subsampled formats can't work as surface use RGBA instead */97if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)98return PIPE_FORMAT_R8G8B8A8_UNORM;99100return format;101}102103bool104vl_video_buffer_is_format_supported(struct pipe_screen *screen,105enum pipe_format format,106enum pipe_video_profile profile,107enum pipe_video_entrypoint entrypoint)108{109enum pipe_format resource_formats[VL_NUM_COMPONENTS];110unsigned i;111112vl_get_video_buffer_formats(screen, format, resource_formats);113114for (i = 0; i < VL_NUM_COMPONENTS; ++i) {115enum pipe_format format = resource_formats[i];116117if (format == PIPE_FORMAT_NONE)118continue;119120/* we at least need to sample from it */121if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW))122return false;123124format = vl_video_buffer_surface_format(format);125if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_RENDER_TARGET))126return false;127}128129return true;130}131132unsigned133vl_video_buffer_max_size(struct pipe_screen *screen)134{135return screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_SIZE);136}137138void139vl_video_buffer_set_associated_data(struct pipe_video_buffer *vbuf,140struct pipe_video_codec *vcodec,141void *associated_data,142void (*destroy_associated_data)(void *))143{144vbuf->codec = vcodec;145146if (vbuf->associated_data == associated_data)147return;148149if (vbuf->associated_data)150vbuf->destroy_associated_data(vbuf->associated_data);151152vbuf->associated_data = associated_data;153vbuf->destroy_associated_data = destroy_associated_data;154}155156void *157vl_video_buffer_get_associated_data(struct pipe_video_buffer *vbuf,158struct pipe_video_codec *vcodec)159{160if (vbuf->codec == vcodec)161return vbuf->associated_data;162else163return NULL;164}165166void167vl_video_buffer_template(struct pipe_resource *templ,168const struct pipe_video_buffer *tmpl,169enum pipe_format resource_format,170unsigned depth, unsigned array_size,171unsigned usage, unsigned plane,172enum pipe_video_chroma_format chroma_format)173{174unsigned height = tmpl->height;175176memset(templ, 0, sizeof(*templ));177if (depth > 1)178templ->target = PIPE_TEXTURE_3D;179else if (array_size > 1)180templ->target = PIPE_TEXTURE_2D_ARRAY;181else182templ->target = PIPE_TEXTURE_2D;183templ->format = resource_format;184templ->width0 = tmpl->width;185templ->depth0 = depth;186templ->array_size = array_size;187templ->bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;188templ->usage = usage;189190vl_video_buffer_adjust_size(&templ->width0, &height, plane,191chroma_format, false);192templ->height0 = height;193}194195static void196vl_video_buffer_destroy(struct pipe_video_buffer *buffer)197{198struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;199unsigned i;200201assert(buf);202203for (i = 0; i < VL_NUM_COMPONENTS; ++i) {204pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);205pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);206pipe_resource_reference(&buf->resources[i], NULL);207}208209for (i = 0; i < VL_MAX_SURFACES; ++i)210pipe_surface_reference(&buf->surfaces[i], NULL);211212vl_video_buffer_set_associated_data(buffer, NULL, NULL, NULL);213214FREE(buffer);215}216217static struct pipe_sampler_view **218vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer)219{220struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;221unsigned num_planes = util_format_get_num_planes(buffer->buffer_format);222struct pipe_sampler_view sv_templ;223struct pipe_context *pipe;224unsigned i;225226assert(buf);227228pipe = buf->base.context;229230for (i = 0; i < num_planes; ++i ) {231if (!buf->sampler_view_planes[i]) {232memset(&sv_templ, 0, sizeof(sv_templ));233u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);234235if (util_format_get_nr_components(buf->resources[i]->format) == 1)236sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_X;237238buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);239if (!buf->sampler_view_planes[i])240goto error;241}242}243244return buf->sampler_view_planes;245246error:247for (i = 0; i < num_planes; ++i )248pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);249250return NULL;251}252253static struct pipe_sampler_view **254vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer)255{256struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;257struct pipe_sampler_view sv_templ;258struct pipe_context *pipe;259enum pipe_format sampler_format[VL_NUM_COMPONENTS];260const unsigned *plane_order;261unsigned i, j, component;262263assert(buf);264265pipe = buf->base.context;266267vl_get_video_buffer_formats(pipe->screen, buf->base.buffer_format, sampler_format);268plane_order = vl_video_buffer_plane_order(buf->base.buffer_format);269270for (component = 0, i = 0; i < buf->num_planes; ++i ) {271struct pipe_resource *res = buf->resources[plane_order[i]];272const struct util_format_description *desc = util_format_description(res->format);273unsigned nr_components = util_format_get_nr_components(res->format);274if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)275nr_components = 3;276277for (j = 0; j < nr_components && component < VL_NUM_COMPONENTS; ++j, ++component) {278if (buf->sampler_view_components[component])279continue;280281memset(&sv_templ, 0, sizeof(sv_templ));282u_sampler_view_default_template(&sv_templ, res, sampler_format[plane_order[i]]);283sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_X + j;284sv_templ.swizzle_a = PIPE_SWIZZLE_1;285buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, res, &sv_templ);286if (!buf->sampler_view_components[component])287goto error;288}289}290assert(component == VL_NUM_COMPONENTS);291292return buf->sampler_view_components;293294error:295for (i = 0; i < VL_NUM_COMPONENTS; ++i )296pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);297298return NULL;299}300301static struct pipe_surface **302vl_video_buffer_surfaces(struct pipe_video_buffer *buffer)303{304struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;305struct pipe_surface surf_templ;306struct pipe_context *pipe;307unsigned i, j, array_size, surf;308309assert(buf);310311pipe = buf->base.context;312313array_size = buffer->interlaced ? 2 : 1;314for (i = 0, surf = 0; i < VL_NUM_COMPONENTS; ++i) {315for (j = 0; j < array_size; ++j, ++surf) {316assert(surf < VL_MAX_SURFACES);317318if (!buf->resources[i]) {319pipe_surface_reference(&buf->surfaces[surf], NULL);320continue;321}322323if (!buf->surfaces[surf]) {324memset(&surf_templ, 0, sizeof(surf_templ));325surf_templ.format = vl_video_buffer_surface_format(buf->resources[i]->format);326surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = j;327buf->surfaces[surf] = pipe->create_surface(pipe, buf->resources[i], &surf_templ);328if (!buf->surfaces[surf])329goto error;330}331}332}333334return buf->surfaces;335336error:337for (i = 0; i < VL_MAX_SURFACES; ++i )338pipe_surface_reference(&buf->surfaces[i], NULL);339340return NULL;341}342343struct pipe_video_buffer *344vl_video_buffer_create(struct pipe_context *pipe,345const struct pipe_video_buffer *tmpl)346{347enum pipe_format resource_formats[VL_NUM_COMPONENTS];348struct pipe_video_buffer templat, *result;349bool pot_buffers;350351assert(pipe);352assert(tmpl->width > 0 && tmpl->height > 0);353354pot_buffers = !pipe->screen->get_video_param355(356pipe->screen,357PIPE_VIDEO_PROFILE_UNKNOWN,358PIPE_VIDEO_ENTRYPOINT_UNKNOWN,359PIPE_VIDEO_CAP_NPOT_TEXTURES360);361362vl_get_video_buffer_formats(pipe->screen, tmpl->buffer_format, resource_formats);363364templat = *tmpl;365templat.width = pot_buffers ? util_next_power_of_two(tmpl->width)366: align(tmpl->width, VL_MACROBLOCK_WIDTH);367templat.height = pot_buffers ? util_next_power_of_two(tmpl->height)368: align(tmpl->height, VL_MACROBLOCK_HEIGHT);369370if (tmpl->interlaced)371templat.height /= 2;372373result = vl_video_buffer_create_ex374(375pipe, &templat, resource_formats,3761, tmpl->interlaced ? 2 : 1, PIPE_USAGE_DEFAULT,377pipe_format_to_chroma_format(templat.buffer_format)378);379380381if (result && tmpl->interlaced)382result->height *= 2;383384return result;385}386387struct pipe_video_buffer *388vl_video_buffer_create_ex(struct pipe_context *pipe,389const struct pipe_video_buffer *tmpl,390const enum pipe_format resource_formats[VL_NUM_COMPONENTS],391unsigned depth, unsigned array_size, unsigned usage,392enum pipe_video_chroma_format chroma_format)393{394struct pipe_resource res_tmpl;395struct pipe_resource *resources[VL_NUM_COMPONENTS];396unsigned i;397398assert(pipe);399400memset(resources, 0, sizeof resources);401402vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[0], depth, array_size,403usage, 0, chroma_format);404resources[0] = pipe->screen->resource_create(pipe->screen, &res_tmpl);405if (!resources[0])406goto error;407408if (resource_formats[1] == PIPE_FORMAT_NONE) {409assert(resource_formats[2] == PIPE_FORMAT_NONE);410return vl_video_buffer_create_ex2(pipe, tmpl, resources);411}412413vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[1], depth, array_size,414usage, 1, chroma_format);415resources[1] = pipe->screen->resource_create(pipe->screen, &res_tmpl);416if (!resources[1])417goto error;418419if (resource_formats[2] == PIPE_FORMAT_NONE)420return vl_video_buffer_create_ex2(pipe, tmpl, resources);421422vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[2], depth, array_size,423usage, 2, chroma_format);424resources[2] = pipe->screen->resource_create(pipe->screen, &res_tmpl);425if (!resources[2])426goto error;427428return vl_video_buffer_create_ex2(pipe, tmpl, resources);429430error:431for (i = 0; i < VL_NUM_COMPONENTS; ++i)432pipe_resource_reference(&resources[i], NULL);433434return NULL;435}436437struct pipe_video_buffer *438vl_video_buffer_create_ex2(struct pipe_context *pipe,439const struct pipe_video_buffer *tmpl,440struct pipe_resource *resources[VL_NUM_COMPONENTS])441{442struct vl_video_buffer *buffer;443unsigned i;444445buffer = CALLOC_STRUCT(vl_video_buffer);446if (!buffer)447return NULL;448449buffer->base = *tmpl;450buffer->base.context = pipe;451buffer->base.destroy = vl_video_buffer_destroy;452buffer->base.get_sampler_view_planes = vl_video_buffer_sampler_view_planes;453buffer->base.get_sampler_view_components = vl_video_buffer_sampler_view_components;454buffer->base.get_surfaces = vl_video_buffer_surfaces;455buffer->num_planes = 0;456457for (i = 0; i < VL_NUM_COMPONENTS; ++i) {458buffer->resources[i] = resources[i];459if (resources[i])460buffer->num_planes++;461}462463return &buffer->base;464}465466/* Create pipe_video_buffer by using resource_create with planar formats. */467struct pipe_video_buffer *468vl_video_buffer_create_as_resource(struct pipe_context *pipe,469const struct pipe_video_buffer *tmpl,470const uint64_t *modifiers,471int modifiers_count)472{473struct pipe_resource templ, *resources[VL_NUM_COMPONENTS] = {0};474unsigned array_size = tmpl->interlaced ? 2 : 1;475476memset(&templ, 0, sizeof(templ));477templ.target = array_size > 1 ? PIPE_TEXTURE_2D_ARRAY : PIPE_TEXTURE_2D;478templ.width0 = align(tmpl->width, VL_MACROBLOCK_WIDTH);479templ.height0 = align(tmpl->height / array_size, VL_MACROBLOCK_HEIGHT);480templ.depth0 = 1;481templ.array_size = array_size;482templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;483templ.usage = PIPE_USAGE_DEFAULT;484485if (tmpl->buffer_format == PIPE_FORMAT_YUYV)486templ.format = PIPE_FORMAT_R8G8_R8B8_UNORM;487else if (tmpl->buffer_format == PIPE_FORMAT_UYVY)488templ.format = PIPE_FORMAT_G8R8_B8R8_UNORM;489else490templ.format = tmpl->buffer_format;491492if (modifiers)493resources[0] = pipe->screen->resource_create_with_modifiers(pipe->screen,494&templ, modifiers,495modifiers_count);496else497resources[0] = pipe->screen->resource_create(pipe->screen, &templ);498if (!resources[0])499return NULL;500501if (resources[0]->next) {502pipe_resource_reference(&resources[1], resources[0]->next);503if (resources[1]->next)504pipe_resource_reference(&resources[2], resources[1]->next);505}506507struct pipe_video_buffer vidtemplate = *tmpl;508vidtemplate.width = templ.width0;509vidtemplate.height = templ.height0 * array_size;510return vl_video_buffer_create_ex2(pipe, &vidtemplate, resources);511}512513514