surface.c revision 8d0808214a75bbb254185b0293c8dd48aa5c4df7
1/************************************************************************** 2 * 3 * Copyright 2010 Thomas Balling Sørensen. 4 * Copyright 2011 Christian König. 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 29#include <assert.h> 30 31#include "pipe/p_state.h" 32 33#include "util/u_memory.h" 34#include "util/u_debug.h" 35#include "util/u_rect.h" 36 37#include "vdpau_private.h" 38 39/** 40 * Create a VdpVideoSurface. 41 */ 42VdpStatus 43vlVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type, 44 uint32_t width, uint32_t height, 45 VdpVideoSurface *surface) 46{ 47 struct pipe_context *pipe; 48 vlVdpSurface *p_surf; 49 VdpStatus ret; 50 51 if (!(width && height)) { 52 ret = VDP_STATUS_INVALID_SIZE; 53 goto inv_size; 54 } 55 56 if (!vlCreateHTAB()) { 57 ret = VDP_STATUS_RESOURCES; 58 goto no_htab; 59 } 60 61 p_surf = CALLOC(1, sizeof(vlVdpSurface)); 62 if (!p_surf) { 63 ret = VDP_STATUS_RESOURCES; 64 goto no_res; 65 } 66 67 vlVdpDevice *dev = vlGetDataHTAB(device); 68 if (!dev) { 69 ret = VDP_STATUS_INVALID_HANDLE; 70 goto inv_device; 71 } 72 73 p_surf->device = dev; 74 pipe = dev->context; 75 76 memset(&p_surf->templat, 0, sizeof(p_surf->templat)); 77 p_surf->templat.buffer_format = pipe->screen->get_video_param 78 ( 79 pipe->screen, 80 PIPE_VIDEO_PROFILE_UNKNOWN, 81 PIPE_VIDEO_CAP_PREFERED_FORMAT 82 ); 83 p_surf->templat.chroma_format = ChromaToPipe(chroma_type); 84 p_surf->templat.width = width; 85 p_surf->templat.height = height; 86 p_surf->templat.interlaced = pipe->screen->get_video_param 87 ( 88 pipe->screen, 89 PIPE_VIDEO_PROFILE_UNKNOWN, 90 PIPE_VIDEO_CAP_PREFERS_INTERLACED 91 ); 92 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat); 93 94 *surface = vlAddDataHTAB(p_surf); 95 if (*surface == 0) { 96 ret = VDP_STATUS_ERROR; 97 goto no_handle; 98 } 99 100 return VDP_STATUS_OK; 101 102no_handle: 103 p_surf->video_buffer->destroy(p_surf->video_buffer); 104 105inv_device: 106 FREE(p_surf); 107 108no_res: 109no_htab: 110inv_size: 111 return ret; 112} 113 114/** 115 * Destroy a VdpVideoSurface. 116 */ 117VdpStatus 118vlVdpVideoSurfaceDestroy(VdpVideoSurface surface) 119{ 120 vlVdpSurface *p_surf; 121 122 p_surf = (vlVdpSurface *)vlGetDataHTAB((vlHandle)surface); 123 if (!p_surf) 124 return VDP_STATUS_INVALID_HANDLE; 125 126 if (p_surf->video_buffer) 127 p_surf->video_buffer->destroy(p_surf->video_buffer); 128 129 FREE(p_surf); 130 return VDP_STATUS_OK; 131} 132 133/** 134 * Retrieve the parameters used to create a VdpVideoSurface. 135 */ 136VdpStatus 137vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface, 138 VdpChromaType *chroma_type, 139 uint32_t *width, uint32_t *height) 140{ 141 if (!(width && height && chroma_type)) 142 return VDP_STATUS_INVALID_POINTER; 143 144 vlVdpSurface *p_surf = vlGetDataHTAB(surface); 145 if (!p_surf) 146 return VDP_STATUS_INVALID_HANDLE; 147 148 if (p_surf->video_buffer) { 149 *width = p_surf->video_buffer->width; 150 *height = p_surf->video_buffer->height; 151 *chroma_type = PipeToChroma(p_surf->video_buffer->chroma_format); 152 } else { 153 *width = p_surf->templat.width; 154 *height = p_surf->templat.height; 155 *chroma_type = PipeToChroma(p_surf->templat.chroma_format); 156 } 157 158 return VDP_STATUS_OK; 159} 160 161/** 162 * Copy image data from a VdpVideoSurface to application memory in a specified 163 * YCbCr format. 164 */ 165VdpStatus 166vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface, 167 VdpYCbCrFormat destination_ycbcr_format, 168 void *const *destination_data, 169 uint32_t const *destination_pitches) 170{ 171 vlVdpSurface *vlsurface; 172 struct pipe_context *pipe; 173 enum pipe_format format; 174 struct pipe_sampler_view **sampler_views; 175 unsigned i, j; 176 177 vlsurface = vlGetDataHTAB(surface); 178 if (!vlsurface) 179 return VDP_STATUS_INVALID_HANDLE; 180 181 pipe = vlsurface->device->context; 182 if (!pipe) 183 return VDP_STATUS_INVALID_HANDLE; 184 185 format = FormatYCBCRToPipe(destination_ycbcr_format); 186 if (format == PIPE_FORMAT_NONE) 187 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT; 188 189 if (vlsurface->video_buffer == NULL || format != vlsurface->video_buffer->buffer_format) 190 return VDP_STATUS_NO_IMPLEMENTATION; /* TODO We don't support conversion (yet) */ 191 192 sampler_views = vlsurface->video_buffer->get_sampler_view_planes(vlsurface->video_buffer); 193 if (!sampler_views) 194 return VDP_STATUS_RESOURCES; 195 196 for (i = 0; i < 3; ++i) { 197 struct pipe_sampler_view *sv = sampler_views[i]; 198 if (!sv) continue; 199 200 for (j = 0; j < sv->texture->depth0; ++j) { 201 struct pipe_box box = { 202 0, 0, j, 203 sv->texture->width0, sv->texture->height0, 1 204 }; 205 struct pipe_transfer *transfer; 206 uint8_t *map; 207 208 transfer = pipe->get_transfer(pipe, sv->texture, 0, PIPE_TRANSFER_READ, &box); 209 if (transfer == NULL) 210 return VDP_STATUS_RESOURCES; 211 212 map = pipe_transfer_map(pipe, transfer); 213 if (map == NULL) { 214 pipe_transfer_destroy(pipe, transfer); 215 return VDP_STATUS_RESOURCES; 216 } 217 218 util_copy_rect(destination_data[i] + destination_pitches[i] * j, sv->texture->format, 219 destination_pitches[i] * sv->texture->depth0, 0, 0, 220 box.width, box.height, map, transfer->stride, 0, 0); 221 222 pipe_transfer_unmap(pipe, transfer); 223 pipe_transfer_destroy(pipe, transfer); 224 } 225 } 226 227 return VDP_STATUS_OK; 228} 229 230/** 231 * Copy image data from application memory in a specific YCbCr format to 232 * a VdpVideoSurface. 233 */ 234VdpStatus 235vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface, 236 VdpYCbCrFormat source_ycbcr_format, 237 void const *const *source_data, 238 uint32_t const *source_pitches) 239{ 240 enum pipe_format pformat = FormatYCBCRToPipe(source_ycbcr_format); 241 struct pipe_context *pipe; 242 struct pipe_sampler_view **sampler_views; 243 unsigned i, j; 244 245 if (!vlCreateHTAB()) 246 return VDP_STATUS_RESOURCES; 247 248 vlVdpSurface *p_surf = vlGetDataHTAB(surface); 249 if (!p_surf) 250 return VDP_STATUS_INVALID_HANDLE; 251 252 pipe = p_surf->device->context; 253 if (!pipe) 254 return VDP_STATUS_INVALID_HANDLE; 255 256 if (p_surf->video_buffer == NULL || pformat != p_surf->video_buffer->buffer_format) { 257 258 /* destroy the old one */ 259 if (p_surf->video_buffer) 260 p_surf->video_buffer->destroy(p_surf->video_buffer); 261 262 /* adjust the template parameters */ 263 p_surf->templat.buffer_format = pformat; 264 265 /* and try to create the video buffer with the new format */ 266 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat); 267 268 /* stil no luck? ok forget it we don't support it */ 269 if (!p_surf->video_buffer) 270 return VDP_STATUS_NO_IMPLEMENTATION; 271 } 272 273 sampler_views = p_surf->video_buffer->get_sampler_view_planes(p_surf->video_buffer); 274 if (!sampler_views) 275 return VDP_STATUS_RESOURCES; 276 277 for (i = 0; i < 3; ++i) { 278 struct pipe_sampler_view *sv = sampler_views[i]; 279 if (!sv) continue; 280 281 for (j = 0; j < sv->texture->depth0; ++j) { 282 struct pipe_box dst_box = { 283 0, 0, j, 284 sv->texture->width0, sv->texture->height0, 1 285 }; 286 287 pipe->transfer_inline_write(pipe, sv->texture, 0, 288 PIPE_TRANSFER_WRITE, &dst_box, 289 source_data[i] + source_pitches[i] * j, 290 source_pitches[i] * sv->texture->depth0, 291 0); 292 } 293 } 294 295 return VDP_STATUS_OK; 296} 297