Path: blob/21.2-virgl/src/gallium/frontends/vdpau/bitmap.c
4565 views
/**************************************************************************1*2* Copyright 2010 Thomas Balling Sørensen.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 <vdpau/vdpau.h>2829#include "util/u_memory.h"30#include "util/u_sampler.h"3132#include "vdpau_private.h"3334/**35* Create a VdpBitmapSurface.36*/37VdpStatus38vlVdpBitmapSurfaceCreate(VdpDevice device,39VdpRGBAFormat rgba_format,40uint32_t width, uint32_t height,41VdpBool frequently_accessed,42VdpBitmapSurface *surface)43{44struct pipe_context *pipe;45struct pipe_resource res_tmpl, *res;46struct pipe_sampler_view sv_templ;47VdpStatus ret;4849vlVdpBitmapSurface *vlsurface = NULL;5051if (!(width && height))52return VDP_STATUS_INVALID_SIZE;5354vlVdpDevice *dev = vlGetDataHTAB(device);55if (!dev)56return VDP_STATUS_INVALID_HANDLE;5758pipe = dev->context;59if (!pipe)60return VDP_STATUS_INVALID_HANDLE;6162if (!surface)63return VDP_STATUS_INVALID_POINTER;6465vlsurface = CALLOC(1, sizeof(vlVdpBitmapSurface));66if (!vlsurface)67return VDP_STATUS_RESOURCES;6869DeviceReference(&vlsurface->device, dev);7071memset(&res_tmpl, 0, sizeof(res_tmpl));72res_tmpl.target = PIPE_TEXTURE_2D;73res_tmpl.format = VdpFormatRGBAToPipe(rgba_format);74res_tmpl.width0 = width;75res_tmpl.height0 = height;76res_tmpl.depth0 = 1;77res_tmpl.array_size = 1;78res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;79res_tmpl.usage = frequently_accessed ? PIPE_USAGE_DYNAMIC : PIPE_USAGE_DEFAULT;8081mtx_lock(&dev->mutex);8283if (!CheckSurfaceParams(pipe->screen, &res_tmpl)) {84ret = VDP_STATUS_RESOURCES;85goto err_unlock;86}8788res = pipe->screen->resource_create(pipe->screen, &res_tmpl);89if (!res) {90ret = VDP_STATUS_RESOURCES;91goto err_unlock;92}9394vlVdpDefaultSamplerViewTemplate(&sv_templ, res);95vlsurface->sampler_view = pipe->create_sampler_view(pipe, res, &sv_templ);9697pipe_resource_reference(&res, NULL);9899if (!vlsurface->sampler_view) {100ret = VDP_STATUS_RESOURCES;101goto err_unlock;102}103104mtx_unlock(&dev->mutex);105106*surface = vlAddDataHTAB(vlsurface);107if (*surface == 0) {108mtx_lock(&dev->mutex);109ret = VDP_STATUS_ERROR;110goto err_sampler;111}112113return VDP_STATUS_OK;114115err_sampler:116pipe_sampler_view_reference(&vlsurface->sampler_view, NULL);117err_unlock:118mtx_unlock(&dev->mutex);119DeviceReference(&vlsurface->device, NULL);120FREE(vlsurface);121return ret;122}123124/**125* Destroy a VdpBitmapSurface.126*/127VdpStatus128vlVdpBitmapSurfaceDestroy(VdpBitmapSurface surface)129{130vlVdpBitmapSurface *vlsurface;131132vlsurface = vlGetDataHTAB(surface);133if (!vlsurface)134return VDP_STATUS_INVALID_HANDLE;135136mtx_lock(&vlsurface->device->mutex);137pipe_sampler_view_reference(&vlsurface->sampler_view, NULL);138mtx_unlock(&vlsurface->device->mutex);139140vlRemoveDataHTAB(surface);141DeviceReference(&vlsurface->device, NULL);142FREE(vlsurface);143144return VDP_STATUS_OK;145}146147/**148* Retrieve the parameters used to create a VdpBitmapSurface.149*/150VdpStatus151vlVdpBitmapSurfaceGetParameters(VdpBitmapSurface surface,152VdpRGBAFormat *rgba_format,153uint32_t *width, uint32_t *height,154VdpBool *frequently_accessed)155{156vlVdpBitmapSurface *vlsurface;157struct pipe_resource *res;158159vlsurface = vlGetDataHTAB(surface);160if (!vlsurface)161return VDP_STATUS_INVALID_HANDLE;162163if (!(rgba_format && width && height && frequently_accessed))164return VDP_STATUS_INVALID_POINTER;165166res = vlsurface->sampler_view->texture;167*rgba_format = PipeToFormatRGBA(res->format);168*width = res->width0;169*height = res->height0;170*frequently_accessed = res->usage == PIPE_USAGE_DYNAMIC;171172return VDP_STATUS_OK;173}174175/**176* Copy image data from application memory in the surface's native format to177* a VdpBitmapSurface.178*/179VdpStatus180vlVdpBitmapSurfacePutBitsNative(VdpBitmapSurface surface,181void const *const *source_data,182uint32_t const *source_pitches,183VdpRect const *destination_rect)184{185vlVdpBitmapSurface *vlsurface;186struct pipe_box dst_box;187struct pipe_context *pipe;188189vlsurface = vlGetDataHTAB(surface);190if (!vlsurface)191return VDP_STATUS_INVALID_HANDLE;192193if (!(source_data && source_pitches))194return VDP_STATUS_INVALID_POINTER;195196pipe = vlsurface->device->context;197198mtx_lock(&vlsurface->device->mutex);199200dst_box = RectToPipeBox(destination_rect, vlsurface->sampler_view->texture);201pipe->texture_subdata(pipe, vlsurface->sampler_view->texture, 0,202PIPE_MAP_WRITE, &dst_box, *source_data,203*source_pitches, 0);204205mtx_unlock(&vlsurface->device->mutex);206207return VDP_STATUS_OK;208}209210211