bitmap.c revision 2ec6af0c638da5ebd196e8071630f09a5472b9e4
1/************************************************************************** 2 * 3 * Copyright 2010 Thomas Balling Sørensen. 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 <vdpau/vdpau.h> 29 30#include "util/u_memory.h" 31#include "util/u_sampler.h" 32 33#include "vdpau_private.h" 34 35/** 36 * Create a VdpBitmapSurface. 37 */ 38VdpStatus 39vlVdpBitmapSurfaceCreate(VdpDevice device, 40 VdpRGBAFormat rgba_format, 41 uint32_t width, uint32_t height, 42 VdpBool frequently_accessed, 43 VdpBitmapSurface *surface) 44{ 45 struct pipe_context *pipe; 46 struct pipe_resource res_tmpl, *res; 47 struct pipe_sampler_view sv_templ; 48 49 vlVdpBitmapSurface *vlsurface = NULL; 50 51 if (!(width && height)) 52 return VDP_STATUS_INVALID_SIZE; 53 54 vlVdpDevice *dev = vlGetDataHTAB(device); 55 if (!dev) 56 return VDP_STATUS_INVALID_HANDLE; 57 58 pipe = dev->context; 59 if (!pipe) 60 return VDP_STATUS_INVALID_HANDLE; 61 62 if (!surface) 63 return VDP_STATUS_INVALID_POINTER; 64 65 vlsurface = CALLOC(1, sizeof(vlVdpBitmapSurface)); 66 if (!vlsurface) 67 return VDP_STATUS_RESOURCES; 68 69 vlsurface->device = dev; 70 71 memset(&res_tmpl, 0, sizeof(res_tmpl)); 72 73 res_tmpl.target = PIPE_TEXTURE_2D; 74 res_tmpl.format = FormatRGBAToPipe(rgba_format); 75 res_tmpl.width0 = width; 76 res_tmpl.height0 = height; 77 res_tmpl.depth0 = 1; 78 res_tmpl.array_size = 1; 79 res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET; 80 res_tmpl.usage = frequently_accessed ? PIPE_USAGE_DYNAMIC : PIPE_USAGE_STATIC; 81 res = pipe->screen->resource_create(pipe->screen, &res_tmpl); 82 if (!res) { 83 FREE(dev); 84 return VDP_STATUS_RESOURCES; 85 } 86 87 memset(&sv_templ, 0, sizeof(sv_templ)); 88 u_sampler_view_default_template(&sv_templ, res, res->format); 89 vlsurface->sampler_view = pipe->create_sampler_view(pipe, res, &sv_templ); 90 if (!vlsurface->sampler_view) { 91 pipe_resource_reference(&res, NULL); 92 FREE(dev); 93 return VDP_STATUS_RESOURCES; 94 } 95 96 *surface = vlAddDataHTAB(vlsurface); 97 if (*surface == 0) { 98 pipe_resource_reference(&res, NULL); 99 FREE(dev); 100 return VDP_STATUS_ERROR; 101 } 102 103 pipe_resource_reference(&res, NULL); 104 105 return VDP_STATUS_OK; 106} 107 108/** 109 * Destroy a VdpBitmapSurface. 110 */ 111VdpStatus 112vlVdpBitmapSurfaceDestroy(VdpBitmapSurface surface) 113{ 114 vlVdpBitmapSurface *vlsurface; 115 116 vlsurface = vlGetDataHTAB(surface); 117 if (!vlsurface) 118 return VDP_STATUS_INVALID_HANDLE; 119 120 vlVdpResolveDelayedRendering(vlsurface->device, NULL, NULL); 121 122 pipe_sampler_view_reference(&vlsurface->sampler_view, NULL); 123 124 vlRemoveDataHTAB(surface); 125 FREE(vlsurface); 126 127 return VDP_STATUS_OK; 128} 129 130/** 131 * Retrieve the parameters used to create a VdpBitmapSurface. 132 */ 133VdpStatus 134vlVdpBitmapSurfaceGetParameters(VdpBitmapSurface surface, 135 VdpRGBAFormat *rgba_format, 136 uint32_t *width, uint32_t *height, 137 VdpBool *frequently_accessed) 138{ 139 vlVdpBitmapSurface *vlsurface; 140 struct pipe_resource *res; 141 142 vlsurface = vlGetDataHTAB(surface); 143 if (!vlsurface) 144 return VDP_STATUS_INVALID_HANDLE; 145 146 if (!(rgba_format && width && height && frequently_accessed)) 147 return VDP_STATUS_INVALID_POINTER; 148 149 res = vlsurface->sampler_view->texture; 150 *rgba_format = PipeToFormatRGBA(res->format); 151 *width = res->width0; 152 *height = res->height0; 153 *frequently_accessed = res->usage == PIPE_USAGE_DYNAMIC; 154 155 return VDP_STATUS_OK; 156} 157 158/** 159 * Copy image data from application memory in the surface's native format to 160 * a VdpBitmapSurface. 161 */ 162VdpStatus 163vlVdpBitmapSurfacePutBitsNative(VdpBitmapSurface surface, 164 void const *const *source_data, 165 uint32_t const *source_pitches, 166 VdpRect const *destination_rect) 167{ 168 vlVdpBitmapSurface *vlsurface; 169 struct pipe_box dst_box; 170 struct pipe_context *pipe; 171 172 vlsurface = vlGetDataHTAB(surface); 173 if (!vlsurface) 174 return VDP_STATUS_INVALID_HANDLE; 175 176 if (!(source_data && source_pitches)) 177 return VDP_STATUS_INVALID_POINTER; 178 179 pipe = vlsurface->device->context; 180 181 vlVdpResolveDelayedRendering(vlsurface->device, NULL, NULL); 182 183 dst_box.x = 0; 184 dst_box.y = 0; 185 dst_box.z = 0; 186 dst_box.width = vlsurface->sampler_view->texture->width0; 187 dst_box.height = vlsurface->sampler_view->texture->height0; 188 dst_box.depth = 1; 189 190 if (destination_rect) { 191 dst_box.x = MIN2(destination_rect->x0, destination_rect->x1); 192 dst_box.y = MIN2(destination_rect->y0, destination_rect->y1); 193 dst_box.width = abs(destination_rect->x1 - destination_rect->x0); 194 dst_box.height = abs(destination_rect->y1 - destination_rect->y0); 195 } 196 197 pipe->transfer_inline_write(pipe, vlsurface->sampler_view->texture, 0, 198 PIPE_TRANSFER_WRITE, &dst_box, *source_data, 199 *source_pitches, 0); 200 return VDP_STATUS_OK; 201} 202