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