s/Tungsten Graphics/VMware/
[mesa.git] / src / gallium / state_trackers / vdpau / surface.c
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 VMWARE 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 #include "util/u_surface.h"
37 #include "vl/vl_defines.h"
38
39 #include "vdpau_private.h"
40
41 enum getbits_conversion {
42 CONVERSION_NONE,
43 CONVERSION_NV12_TO_YV12,
44 CONVERSION_YV12_TO_NV12,
45 CONVERSION_SWAP_YUYV_UYVY,
46 };
47
48 /**
49 * Create a VdpVideoSurface.
50 */
51 VdpStatus
52 vlVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type,
53 uint32_t width, uint32_t height,
54 VdpVideoSurface *surface)
55 {
56 struct pipe_context *pipe;
57 vlVdpSurface *p_surf;
58 VdpStatus ret;
59
60 if (!(width && height)) {
61 ret = VDP_STATUS_INVALID_SIZE;
62 goto inv_size;
63 }
64
65 p_surf = CALLOC(1, sizeof(vlVdpSurface));
66 if (!p_surf) {
67 ret = VDP_STATUS_RESOURCES;
68 goto no_res;
69 }
70
71 vlVdpDevice *dev = vlGetDataHTAB(device);
72 if (!dev) {
73 ret = VDP_STATUS_INVALID_HANDLE;
74 goto inv_device;
75 }
76
77 p_surf->device = dev;
78 pipe = dev->context;
79
80 pipe_mutex_lock(dev->mutex);
81 memset(&p_surf->templat, 0, sizeof(p_surf->templat));
82 p_surf->templat.buffer_format = pipe->screen->get_video_param
83 (
84 pipe->screen,
85 PIPE_VIDEO_PROFILE_UNKNOWN,
86 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
87 PIPE_VIDEO_CAP_PREFERED_FORMAT
88 );
89 p_surf->templat.chroma_format = ChromaToPipe(chroma_type);
90 p_surf->templat.width = width;
91 p_surf->templat.height = height;
92 p_surf->templat.interlaced = pipe->screen->get_video_param
93 (
94 pipe->screen,
95 PIPE_VIDEO_PROFILE_UNKNOWN,
96 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
97 PIPE_VIDEO_CAP_PREFERS_INTERLACED
98 );
99 if (p_surf->templat.buffer_format != PIPE_FORMAT_NONE)
100 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
101
102 /* do not mandate early allocation of a video buffer */
103 vlVdpVideoSurfaceClear(p_surf);
104 pipe_mutex_unlock(dev->mutex);
105
106 *surface = vlAddDataHTAB(p_surf);
107 if (*surface == 0) {
108 ret = VDP_STATUS_ERROR;
109 goto no_handle;
110 }
111
112 return VDP_STATUS_OK;
113
114 no_handle:
115 p_surf->video_buffer->destroy(p_surf->video_buffer);
116
117 inv_device:
118 FREE(p_surf);
119
120 no_res:
121 inv_size:
122 return ret;
123 }
124
125 /**
126 * Destroy a VdpVideoSurface.
127 */
128 VdpStatus
129 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface)
130 {
131 vlVdpSurface *p_surf;
132
133 p_surf = (vlVdpSurface *)vlGetDataHTAB((vlHandle)surface);
134 if (!p_surf)
135 return VDP_STATUS_INVALID_HANDLE;
136
137 pipe_mutex_lock(p_surf->device->mutex);
138 if (p_surf->video_buffer)
139 p_surf->video_buffer->destroy(p_surf->video_buffer);
140 pipe_mutex_unlock(p_surf->device->mutex);
141
142 vlRemoveDataHTAB(surface);
143 FREE(p_surf);
144
145 return VDP_STATUS_OK;
146 }
147
148 /**
149 * Retrieve the parameters used to create a VdpVideoSurface.
150 */
151 VdpStatus
152 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface,
153 VdpChromaType *chroma_type,
154 uint32_t *width, uint32_t *height)
155 {
156 if (!(width && height && chroma_type))
157 return VDP_STATUS_INVALID_POINTER;
158
159 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
160 if (!p_surf)
161 return VDP_STATUS_INVALID_HANDLE;
162
163 if (p_surf->video_buffer) {
164 *width = p_surf->video_buffer->width;
165 *height = p_surf->video_buffer->height;
166 *chroma_type = PipeToChroma(p_surf->video_buffer->chroma_format);
167 } else {
168 *width = p_surf->templat.width;
169 *height = p_surf->templat.height;
170 *chroma_type = PipeToChroma(p_surf->templat.chroma_format);
171 }
172
173 return VDP_STATUS_OK;
174 }
175
176 static void
177 vlVdpVideoSurfaceSize(vlVdpSurface *p_surf, int component,
178 unsigned *width, unsigned *height)
179 {
180 *width = p_surf->templat.width;
181 *height = p_surf->templat.height;
182
183 if (component > 0) {
184 if (p_surf->templat.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
185 *width /= 2;
186 *height /= 2;
187 } else if (p_surf->templat.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
188 *width /= 2;
189 }
190 }
191 if (p_surf->templat.interlaced)
192 *height /= 2;
193 }
194
195 static void
196 vlVdpCopyNV12ToYV12(void *const *destination_data,
197 uint32_t const *destination_pitches,
198 int src_plane, int src_field,
199 int src_stride, int num_fields,
200 uint8_t const *src,
201 int width, int height)
202 {
203 int x, y;
204 unsigned u_stride = destination_pitches[2] * num_fields;
205 unsigned v_stride = destination_pitches[1] * num_fields;
206 uint8_t *u_dst = (uint8_t *)destination_data[2] + destination_pitches[2] * src_field;
207 uint8_t *v_dst = (uint8_t *)destination_data[1] + destination_pitches[1] * src_field;
208
209 /* TODO: SIMD */
210 for (y = 0; y < height; y++) {
211 for (x = 0; x < width; x++) {
212 u_dst[x] = src[2*x];
213 v_dst[x] = src[2*x+1];
214 }
215 u_dst += u_stride;
216 v_dst += v_stride;
217 src += src_stride;
218 }
219 }
220
221 static void
222 vlVdpCopyYV12ToNV12(void *const *destination_data,
223 uint32_t const *destination_pitches,
224 int src_plane, int src_field,
225 int src_stride, int num_fields,
226 uint8_t const *src,
227 int width, int height)
228 {
229 int x, y;
230 unsigned offset = 2 - src_plane;
231 unsigned stride = destination_pitches[1] * num_fields;
232 uint8_t *dst = (uint8_t *)destination_data[1] + destination_pitches[1] * src_field;
233
234 /* TODO: SIMD */
235 for (y = 0; y < height; y++) {
236 for (x = 0; x < 2 * width; x += 2) {
237 dst[x+offset] = src[x>>1];
238 }
239 dst += stride;
240 src += src_stride;
241 }
242 }
243
244 static void
245 vlVdpCopySwap422Packed(void *const *destination_data,
246 uint32_t const *destination_pitches,
247 int src_plane, int src_field,
248 int src_stride, int num_fields,
249 uint8_t const *src,
250 int width, int height)
251 {
252 int x, y;
253 unsigned stride = destination_pitches[0] * num_fields;
254 uint8_t *dst = (uint8_t *)destination_data[0] + destination_pitches[0] * src_field;
255
256 /* TODO: SIMD */
257 for (y = 0; y < height; y++) {
258 for (x = 0; x < 4 * width; x += 4) {
259 dst[x+0] = src[x+1];
260 dst[x+1] = src[x+0];
261 dst[x+2] = src[x+3];
262 dst[x+3] = src[x+2];
263 }
264 dst += stride;
265 src += src_stride;
266 }
267 }
268
269 /**
270 * Copy image data from a VdpVideoSurface to application memory in a specified
271 * YCbCr format.
272 */
273 VdpStatus
274 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface,
275 VdpYCbCrFormat destination_ycbcr_format,
276 void *const *destination_data,
277 uint32_t const *destination_pitches)
278 {
279 vlVdpSurface *vlsurface;
280 struct pipe_context *pipe;
281 enum pipe_format format, buffer_format;
282 struct pipe_sampler_view **sampler_views;
283 enum getbits_conversion conversion = CONVERSION_NONE;
284 unsigned i, j;
285
286 vlsurface = vlGetDataHTAB(surface);
287 if (!vlsurface)
288 return VDP_STATUS_INVALID_HANDLE;
289
290 pipe = vlsurface->device->context;
291 if (!pipe)
292 return VDP_STATUS_INVALID_HANDLE;
293
294 format = FormatYCBCRToPipe(destination_ycbcr_format);
295 if (format == PIPE_FORMAT_NONE)
296 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT;
297
298 if (vlsurface->video_buffer == NULL)
299 return VDP_STATUS_INVALID_VALUE;
300
301 buffer_format = vlsurface->video_buffer->buffer_format;
302 if (format != buffer_format) {
303 if (format == PIPE_FORMAT_YV12 && buffer_format == PIPE_FORMAT_NV12)
304 conversion = CONVERSION_NV12_TO_YV12;
305 else if (format == PIPE_FORMAT_NV12 && buffer_format == PIPE_FORMAT_YV12)
306 conversion = CONVERSION_YV12_TO_NV12;
307 else if ((format == PIPE_FORMAT_YUYV && buffer_format == PIPE_FORMAT_UYVY) ||
308 (format == PIPE_FORMAT_UYVY && buffer_format == PIPE_FORMAT_YUYV))
309 conversion = CONVERSION_SWAP_YUYV_UYVY;
310 else
311 return VDP_STATUS_NO_IMPLEMENTATION;
312 }
313
314 pipe_mutex_lock(vlsurface->device->mutex);
315 sampler_views = vlsurface->video_buffer->get_sampler_view_planes(vlsurface->video_buffer);
316 if (!sampler_views) {
317 pipe_mutex_unlock(vlsurface->device->mutex);
318 return VDP_STATUS_RESOURCES;
319 }
320
321 for (i = 0; i < 3; ++i) {
322 unsigned width, height;
323 struct pipe_sampler_view *sv = sampler_views[i];
324 if (!sv) continue;
325
326 vlVdpVideoSurfaceSize(vlsurface, i, &width, &height);
327
328 for (j = 0; j < sv->texture->array_size; ++j) {
329 struct pipe_box box = {
330 0, 0, j,
331 width, height, 1
332 };
333 struct pipe_transfer *transfer;
334 uint8_t *map;
335
336 map = pipe->transfer_map(pipe, sv->texture, 0,
337 PIPE_TRANSFER_READ, &box, &transfer);
338 if (!map) {
339 pipe_mutex_unlock(vlsurface->device->mutex);
340 return VDP_STATUS_RESOURCES;
341 }
342
343 if (conversion == CONVERSION_NV12_TO_YV12 && i == 1) {
344 vlVdpCopyNV12ToYV12(destination_data, destination_pitches,
345 i, j, transfer->stride, sv->texture->array_size,
346 map, box.width, box.height);
347 } else if (conversion == CONVERSION_YV12_TO_NV12 && i > 0) {
348 vlVdpCopyYV12ToNV12(destination_data, destination_pitches,
349 i, j, transfer->stride, sv->texture->array_size,
350 map, box.width, box.height);
351 } else if (conversion == CONVERSION_SWAP_YUYV_UYVY) {
352 vlVdpCopySwap422Packed(destination_data, destination_pitches,
353 i, j, transfer->stride, sv->texture->array_size,
354 map, box.width, box.height);
355 } else {
356 util_copy_rect(destination_data[i] + destination_pitches[i] * j, sv->texture->format,
357 destination_pitches[i] * sv->texture->array_size, 0, 0,
358 box.width, box.height, map, transfer->stride, 0, 0);
359 }
360
361 pipe_transfer_unmap(pipe, transfer);
362 }
363 }
364 pipe_mutex_unlock(vlsurface->device->mutex);
365
366 return VDP_STATUS_OK;
367 }
368
369 /**
370 * Copy image data from application memory in a specific YCbCr format to
371 * a VdpVideoSurface.
372 */
373 VdpStatus
374 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
375 VdpYCbCrFormat source_ycbcr_format,
376 void const *const *source_data,
377 uint32_t const *source_pitches)
378 {
379 enum pipe_format pformat = FormatYCBCRToPipe(source_ycbcr_format);
380 struct pipe_context *pipe;
381 struct pipe_sampler_view **sampler_views;
382 unsigned i, j;
383
384 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
385 if (!p_surf)
386 return VDP_STATUS_INVALID_HANDLE;
387
388 pipe = p_surf->device->context;
389 if (!pipe)
390 return VDP_STATUS_INVALID_HANDLE;
391
392 pipe_mutex_lock(p_surf->device->mutex);
393 if (p_surf->video_buffer == NULL || pformat != p_surf->video_buffer->buffer_format) {
394
395 /* destroy the old one */
396 if (p_surf->video_buffer)
397 p_surf->video_buffer->destroy(p_surf->video_buffer);
398
399 /* adjust the template parameters */
400 p_surf->templat.buffer_format = pformat;
401
402 /* and try to create the video buffer with the new format */
403 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
404
405 /* stil no luck? ok forget it we don't support it */
406 if (!p_surf->video_buffer) {
407 pipe_mutex_unlock(p_surf->device->mutex);
408 return VDP_STATUS_NO_IMPLEMENTATION;
409 }
410 vlVdpVideoSurfaceClear(p_surf);
411 }
412
413 sampler_views = p_surf->video_buffer->get_sampler_view_planes(p_surf->video_buffer);
414 if (!sampler_views) {
415 pipe_mutex_unlock(p_surf->device->mutex);
416 return VDP_STATUS_RESOURCES;
417 }
418
419 for (i = 0; i < 3; ++i) {
420 unsigned width, height;
421 struct pipe_sampler_view *sv = sampler_views[i];
422 if (!sv || !source_pitches[i]) continue;
423
424 vlVdpVideoSurfaceSize(p_surf, i, &width, &height);
425
426 for (j = 0; j < sv->texture->array_size; ++j) {
427 struct pipe_box dst_box = {
428 0, 0, j,
429 width, height, 1
430 };
431
432 pipe->transfer_inline_write(pipe, sv->texture, 0,
433 PIPE_TRANSFER_WRITE, &dst_box,
434 source_data[i] + source_pitches[i] * j,
435 source_pitches[i] * sv->texture->array_size,
436 0);
437 }
438 }
439 pipe_mutex_unlock(p_surf->device->mutex);
440
441 return VDP_STATUS_OK;
442 }
443
444 /**
445 * Helper function to initially clear the VideoSurface after (re-)creation
446 */
447 void
448 vlVdpVideoSurfaceClear(vlVdpSurface *vlsurf)
449 {
450 struct pipe_context *pipe = vlsurf->device->context;
451 struct pipe_surface **surfaces;
452 unsigned i;
453
454 if (!vlsurf->video_buffer)
455 return;
456
457 surfaces = vlsurf->video_buffer->get_surfaces(vlsurf->video_buffer);
458 for (i = 0; i < VL_MAX_SURFACES; ++i) {
459 union pipe_color_union c = {};
460
461 if (!surfaces[i])
462 continue;
463
464 if (i > !!vlsurf->templat.interlaced)
465 c.f[0] = c.f[1] = c.f[2] = c.f[3] = 0.5f;
466
467 pipe->clear_render_target(pipe, surfaces[i], &c, 0, 0,
468 surfaces[i]->width, surfaces[i]->height);
469 }
470 pipe->flush(pipe, NULL, 0);
471 }
472
473 /**
474 * Interop to mesa state tracker
475 */
476 struct pipe_video_buffer *vlVdpVideoSurfaceGallium(VdpVideoSurface surface)
477 {
478 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
479 if (!p_surf)
480 return NULL;
481
482 pipe_mutex_lock(p_surf->device->mutex);
483 if (p_surf->video_buffer == NULL) {
484 struct pipe_context *pipe = p_surf->device->context;
485
486 /* try to create a video buffer if we don't already have one */
487 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
488 }
489 pipe_mutex_unlock(p_surf->device->mutex);
490
491 return p_surf->video_buffer;
492 }