1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
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 THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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 "pipe/p_screen.h"
30 #include "pipe/p_video_codec.h"
31
32 #include "util/u_memory.h"
33 #include "util/u_handle_table.h"
34 #include "util/u_rect.h"
35 #include "util/u_sampler.h"
36 #include "util/u_surface.h"
37 #include "util/u_video.h"
38 #include "util/set.h"
39
40 #include "vl/vl_compositor.h"
41 #include "vl/vl_video_buffer.h"
42 #include "vl/vl_winsys.h"
43
44 #include "va_private.h"
45
46 #ifdef _WIN32
47 #include "frontend/winsys_handle.h"
48 #include <va/va_win32.h>
49 #else
50 #include "frontend/drm_driver.h"
51 #include <va/va_drmcommon.h>
52 #include "drm-uapi/drm_fourcc.h"
53 #endif
54
55 #ifndef VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3
56 #define VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3 0x08000000
57 #endif
58
59 VAStatus
vlVaCreateSurfaces(VADriverContextP ctx,int width,int height,int format,int num_surfaces,VASurfaceID * surfaces)60 vlVaCreateSurfaces(VADriverContextP ctx, int width, int height, int format,
61 int num_surfaces, VASurfaceID *surfaces)
62 {
63 return vlVaCreateSurfaces2(ctx, format, width, height, surfaces, num_surfaces,
64 NULL, 0);
65 }
66
67 static void
vlVaRemoveDpbSurface(vlVaSurface * surf,VASurfaceID id)68 vlVaRemoveDpbSurface(vlVaSurface *surf, VASurfaceID id)
69 {
70 assert(surf->ctx->templat.entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE);
71
72 switch (u_reduce_video_profile(surf->ctx->templat.profile)) {
73 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
74 for (unsigned i = 0; i < surf->ctx->desc.h264enc.dpb_size; i++) {
75 if (surf->ctx->desc.h264enc.dpb[i].id == id) {
76 memset(&surf->ctx->desc.h264enc.dpb[i], 0, sizeof(surf->ctx->desc.h264enc.dpb[i]));
77 break;
78 }
79 }
80 break;
81 case PIPE_VIDEO_FORMAT_HEVC:
82 for (unsigned i = 0; i < surf->ctx->desc.h265enc.dpb_size; i++) {
83 if (surf->ctx->desc.h265enc.dpb[i].id == id) {
84 memset(&surf->ctx->desc.h265enc.dpb[i], 0, sizeof(surf->ctx->desc.h265enc.dpb[i]));
85 break;
86 }
87 }
88 break;
89 case PIPE_VIDEO_FORMAT_AV1:
90 for (unsigned i = 0; i < surf->ctx->desc.av1enc.dpb_size; i++) {
91 if (surf->ctx->desc.av1enc.dpb[i].id == id) {
92 memset(&surf->ctx->desc.av1enc.dpb[i], 0, sizeof(surf->ctx->desc.av1enc.dpb[i]));
93 break;
94 }
95 }
96 break;
97 default:
98 assert(false);
99 break;
100 }
101 }
102
103 VAStatus
vlVaDestroySurfaces(VADriverContextP ctx,VASurfaceID * surface_list,int num_surfaces)104 vlVaDestroySurfaces(VADriverContextP ctx, VASurfaceID *surface_list, int num_surfaces)
105 {
106 vlVaDriver *drv;
107 int i;
108
109 if (!ctx)
110 return VA_STATUS_ERROR_INVALID_CONTEXT;
111
112 drv = VL_VA_DRIVER(ctx);
113 mtx_lock(&drv->mutex);
114 for (i = 0; i < num_surfaces; ++i) {
115 vlVaSurface *surf = handle_table_get(drv->htab, surface_list[i]);
116 if (!surf) {
117 mtx_unlock(&drv->mutex);
118 return VA_STATUS_ERROR_INVALID_SURFACE;
119 }
120 if (surf->buffer)
121 surf->buffer->destroy(surf->buffer);
122 if (surf->pipe_fence)
123 drv->pipe->screen->fence_reference(drv->pipe->screen, &surf->pipe_fence, NULL);
124 if (surf->ctx) {
125 assert(_mesa_set_search(surf->ctx->surfaces, surf));
126 _mesa_set_remove_key(surf->ctx->surfaces, surf);
127 if (surf->fence && surf->ctx->decoder && surf->ctx->decoder->destroy_fence)
128 surf->ctx->decoder->destroy_fence(surf->ctx->decoder, surf->fence);
129 if (surf->is_dpb)
130 vlVaRemoveDpbSurface(surf, surface_list[i]);
131 }
132 if (drv->last_efc_surface) {
133 vlVaSurface *efc_surf = drv->last_efc_surface;
134 if (efc_surf == surf || efc_surf->efc_surface == surf) {
135 efc_surf->efc_surface = NULL;
136 drv->last_efc_surface = NULL;
137 drv->efc_count = -1;
138 }
139 }
140 if (surf->coded_buf)
141 surf->coded_buf->coded_surf = NULL;
142 util_dynarray_fini(&surf->subpics);
143 FREE(surf);
144 handle_table_remove(drv->htab, surface_list[i]);
145 }
146 mtx_unlock(&drv->mutex);
147
148 return VA_STATUS_SUCCESS;
149 }
150
151 static VAStatus
_vlVaSyncSurface(VADriverContextP ctx,VASurfaceID render_target,uint64_t timeout_ns)152 _vlVaSyncSurface(VADriverContextP ctx, VASurfaceID render_target, uint64_t timeout_ns)
153 {
154 vlVaDriver *drv;
155 vlVaContext *context;
156 vlVaSurface *surf;
157 struct pipe_fence_handle *fence;
158
159 if (!ctx)
160 return VA_STATUS_ERROR_INVALID_CONTEXT;
161
162 drv = VL_VA_DRIVER(ctx);
163 if (!drv)
164 return VA_STATUS_ERROR_INVALID_CONTEXT;
165
166 mtx_lock(&drv->mutex);
167 surf = handle_table_get(drv->htab, render_target);
168 if (!surf) {
169 mtx_unlock(&drv->mutex);
170 return VA_STATUS_ERROR_INVALID_SURFACE;
171 }
172
173 if (surf->coded_buf) {
174 context = surf->coded_buf->ctx;
175 fence = surf->coded_buf->fence;
176 } else {
177 context = surf->ctx;
178 fence = surf->fence;
179 }
180
181 if (surf->pipe_fence) {
182 struct pipe_screen *pscreen = drv->pipe->screen;
183 if (!pscreen->fence_finish(pscreen, NULL, surf->pipe_fence, timeout_ns)) {
184 mtx_unlock(&drv->mutex);
185 return VA_STATUS_ERROR_TIMEDOUT;
186 }
187 pscreen->fence_reference(pscreen, &surf->pipe_fence, NULL);
188 }
189
190 /* No outstanding operation: nothing to do. */
191 if (!surf->fence) {
192 mtx_unlock(&drv->mutex);
193 return VA_STATUS_SUCCESS;
194 }
195
196 if (!context || !context->decoder) {
197 mtx_unlock(&drv->mutex);
198 return VA_STATUS_ERROR_INVALID_CONTEXT;
199 }
200
201 mtx_lock(&context->mutex);
202 mtx_unlock(&drv->mutex);
203 int ret = context->decoder->fence_wait(context->decoder, fence, timeout_ns);
204 mtx_unlock(&context->mutex);
205 return ret ? VA_STATUS_SUCCESS : VA_STATUS_ERROR_TIMEDOUT;
206 }
207
208 VAStatus
vlVaSyncSurface(VADriverContextP ctx,VASurfaceID render_target)209 vlVaSyncSurface(VADriverContextP ctx, VASurfaceID render_target)
210 {
211 return _vlVaSyncSurface(ctx, render_target, VA_TIMEOUT_INFINITE);
212 }
213
214 #if VA_CHECK_VERSION(1, 15, 0)
215 VAStatus
vlVaSyncSurface2(VADriverContextP ctx,VASurfaceID surface,uint64_t timeout_ns)216 vlVaSyncSurface2(VADriverContextP ctx, VASurfaceID surface, uint64_t timeout_ns)
217 {
218 return _vlVaSyncSurface(ctx, surface, timeout_ns);
219 }
220 #endif
221
222 VAStatus
vlVaQuerySurfaceStatus(VADriverContextP ctx,VASurfaceID render_target,VASurfaceStatus * status)223 vlVaQuerySurfaceStatus(VADriverContextP ctx, VASurfaceID render_target, VASurfaceStatus *status)
224 {
225 VAStatus ret = _vlVaSyncSurface(ctx, render_target, 0);
226
227 if (ret == VA_STATUS_SUCCESS)
228 *status = VASurfaceReady;
229 else if (ret == VA_STATUS_ERROR_TIMEDOUT)
230 *status = VASurfaceRendering;
231 else
232 return ret;
233
234 return VA_STATUS_SUCCESS;
235 }
236
237 VAStatus
vlVaQuerySurfaceError(VADriverContextP ctx,VASurfaceID render_target,VAStatus error_status,void ** error_info)238 vlVaQuerySurfaceError(VADriverContextP ctx, VASurfaceID render_target, VAStatus error_status, void **error_info)
239 {
240 if (!ctx)
241 return VA_STATUS_ERROR_INVALID_CONTEXT;
242
243 return VA_STATUS_ERROR_UNIMPLEMENTED;
244 }
245
246 static void
upload_sampler(struct pipe_context * pipe,struct pipe_sampler_view * dst,const struct pipe_box * dst_box,const void * src,unsigned src_stride,unsigned src_x,unsigned src_y)247 upload_sampler(struct pipe_context *pipe, struct pipe_sampler_view *dst,
248 const struct pipe_box *dst_box, const void *src, unsigned src_stride,
249 unsigned src_x, unsigned src_y)
250 {
251 struct pipe_transfer *transfer;
252 void *map;
253
254 map = pipe->texture_map(pipe, dst->texture, 0, PIPE_MAP_WRITE,
255 dst_box, &transfer);
256 if (!map)
257 return;
258
259 util_copy_rect(map, dst->texture->format, transfer->stride, 0, 0,
260 dst_box->width, dst_box->height,
261 src, src_stride, src_x, src_y);
262
263 pipe->texture_unmap(pipe, transfer);
264 }
265
266 static VAStatus
vlVaPutSubpictures(vlVaSurface * surf,vlVaDriver * drv,struct pipe_surface * surf_draw,struct u_rect * dirty_area,struct u_rect * src_rect,struct u_rect * dst_rect)267 vlVaPutSubpictures(vlVaSurface *surf, vlVaDriver *drv,
268 struct pipe_surface *surf_draw, struct u_rect *dirty_area,
269 struct u_rect *src_rect, struct u_rect *dst_rect)
270 {
271 vlVaSubpicture *sub;
272 int i;
273
274 for (i = 0; i < surf->subpics.size/sizeof(vlVaSubpicture *); i++) {
275 struct pipe_blend_state blend;
276 void *blend_state = NULL;
277 vlVaBuffer *buf;
278 struct pipe_box box;
279 struct u_rect *s, *d, sr, dr, c;
280 int sw, sh, dw, dh;
281
282 sub = ((vlVaSubpicture **)surf->subpics.data)[i];
283 if (!sub)
284 continue;
285
286 buf = handle_table_get(drv->htab, sub->image->buf);
287 if (!buf)
288 return VA_STATUS_ERROR_INVALID_IMAGE;
289
290 box.x = 0;
291 box.y = 0;
292 box.z = 0;
293 box.width = sub->dst_rect.x1 - sub->dst_rect.x0;
294 box.height = sub->dst_rect.y1 - sub->dst_rect.y0;
295 box.depth = 1;
296
297 s = &sub->src_rect;
298 d = &sub->dst_rect;
299 sw = s->x1 - s->x0;
300 sh = s->y1 - s->y0;
301 dw = d->x1 - d->x0;
302 dh = d->y1 - d->y0;
303 c.x0 = MAX2(d->x0, s->x0);
304 c.y0 = MAX2(d->y0, s->y0);
305 c.x1 = MIN2(d->x0 + dw, src_rect->x1);
306 c.y1 = MIN2(d->y0 + dh, src_rect->y1);
307 sr.x0 = s->x0 + (c.x0 - d->x0)*(sw/(float)dw);
308 sr.y0 = s->y0 + (c.y0 - d->y0)*(sh/(float)dh);
309 sr.x1 = s->x0 + (c.x1 - d->x0)*(sw/(float)dw);
310 sr.y1 = s->y0 + (c.y1 - d->y0)*(sh/(float)dh);
311
312 s = src_rect;
313 d = dst_rect;
314 sw = s->x1 - s->x0;
315 sh = s->y1 - s->y0;
316 dw = d->x1 - d->x0;
317 dh = d->y1 - d->y0;
318 dr.x0 = d->x0 + c.x0*(dw/(float)sw);
319 dr.y0 = d->y0 + c.y0*(dh/(float)sh);
320 dr.x1 = d->x0 + c.x1*(dw/(float)sw);
321 dr.y1 = d->y0 + c.y1*(dh/(float)sh);
322
323 vl_compositor_clear_layers(&drv->cstate);
324 if (drv->pipe->create_blend_state) {
325 memset(&blend, 0, sizeof(blend));
326 blend.independent_blend_enable = 0;
327 blend.rt[0].blend_enable = 1;
328 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
329 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
330 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ZERO;
331 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
332 blend.rt[0].rgb_func = PIPE_BLEND_ADD;
333 blend.rt[0].alpha_func = PIPE_BLEND_ADD;
334 blend.rt[0].colormask = PIPE_MASK_RGBA;
335 blend.logicop_enable = 0;
336 blend.logicop_func = PIPE_LOGICOP_CLEAR;
337 blend.dither = 0;
338 blend_state = drv->pipe->create_blend_state(drv->pipe, &blend);
339 vl_compositor_set_layer_blend(&drv->cstate, 0, blend_state, false);
340 }
341 upload_sampler(drv->pipe, sub->sampler, &box, buf->data,
342 sub->image->pitches[0], 0, 0);
343 vl_compositor_set_rgba_layer(&drv->cstate, &drv->compositor, 0, sub->sampler,
344 &sr, NULL, NULL);
345 vl_compositor_set_layer_dst_area(&drv->cstate, 0, &dr);
346 vl_compositor_render(&drv->cstate, &drv->compositor, surf_draw, dirty_area, false);
347 if (blend_state)
348 drv->pipe->delete_blend_state(drv->pipe, blend_state);
349 }
350
351 return VA_STATUS_SUCCESS;
352 }
353
354 VAStatus
vlVaPutSurface(VADriverContextP ctx,VASurfaceID surface_id,void * draw,short srcx,short srcy,unsigned short srcw,unsigned short srch,short destx,short desty,unsigned short destw,unsigned short desth,VARectangle * cliprects,unsigned int number_cliprects,unsigned int flags)355 vlVaPutSurface(VADriverContextP ctx, VASurfaceID surface_id, void* draw, short srcx, short srcy,
356 unsigned short srcw, unsigned short srch, short destx, short desty,
357 unsigned short destw, unsigned short desth, VARectangle *cliprects,
358 unsigned int number_cliprects, unsigned int flags)
359 {
360 vlVaDriver *drv;
361 vlVaSurface *surf;
362 struct pipe_screen *screen;
363 struct pipe_resource *tex;
364 struct pipe_surface surf_templ, *surf_draw;
365 struct vl_screen *vscreen;
366 struct u_rect src_rect, *dirty_area;
367 struct u_rect dst_rect = {destx, destx + destw, desty, desty + desth};
368 enum pipe_format format;
369 VAStatus status;
370 enum VL_CSC_COLOR_STANDARD color_standard;
371
372 if (!ctx)
373 return VA_STATUS_ERROR_INVALID_CONTEXT;
374
375 drv = VL_VA_DRIVER(ctx);
376 mtx_lock(&drv->mutex);
377 surf = handle_table_get(drv->htab, surface_id);
378 vlVaGetSurfaceBuffer(drv, surf);
379 if (!surf || !surf->buffer) {
380 mtx_unlock(&drv->mutex);
381 return VA_STATUS_ERROR_INVALID_SURFACE;
382 }
383
384 screen = drv->pipe->screen;
385 vscreen = drv->vscreen;
386
387 tex = vscreen->texture_from_drawable(vscreen, draw);
388 if (!tex) {
389 mtx_unlock(&drv->mutex);
390 return VA_STATUS_ERROR_INVALID_DISPLAY;
391 }
392
393 dirty_area = vscreen->get_dirty_area(vscreen);
394
395 memset(&surf_templ, 0, sizeof(surf_templ));
396 surf_templ.format = tex->format;
397 surf_draw = drv->pipe->create_surface(drv->pipe, tex, &surf_templ);
398 if (!surf_draw) {
399 pipe_resource_reference(&tex, NULL);
400 mtx_unlock(&drv->mutex);
401 return VA_STATUS_ERROR_INVALID_DISPLAY;
402 }
403
404 src_rect.x0 = srcx;
405 src_rect.y0 = srcy;
406 src_rect.x1 = srcw + srcx;
407 src_rect.y1 = srch + srcy;
408
409 format = surf->buffer->buffer_format;
410
411 if (flags & VA_SRC_BT601)
412 color_standard = VL_CSC_COLOR_STANDARD_BT_601;
413 else if (flags & VA_SRC_SMPTE_240)
414 color_standard = VL_CSC_COLOR_STANDARD_SMPTE_240M;
415 else
416 color_standard = VL_CSC_COLOR_STANDARD_BT_709;
417
418 vl_csc_get_matrix(color_standard, NULL, true, &drv->csc);
419 vl_compositor_set_csc_matrix(&drv->cstate, (const vl_csc_matrix *)&drv->csc, 1.0f, 0.0f);
420
421 vl_compositor_clear_layers(&drv->cstate);
422
423 if (!util_format_is_yuv(format)) {
424 struct pipe_sampler_view **views;
425
426 views = surf->buffer->get_sampler_view_planes(surf->buffer);
427 vl_compositor_set_rgba_layer(&drv->cstate, &drv->compositor, 0, views[0], &src_rect, NULL, NULL);
428 } else
429 vl_compositor_set_buffer_layer(&drv->cstate, &drv->compositor, 0, surf->buffer, &src_rect, NULL, VL_COMPOSITOR_WEAVE);
430
431 vl_compositor_set_layer_dst_area(&drv->cstate, 0, &dst_rect);
432 vl_compositor_render(&drv->cstate, &drv->compositor, surf_draw, dirty_area, true);
433
434 status = vlVaPutSubpictures(surf, drv, surf_draw, dirty_area, &src_rect, &dst_rect);
435 if (status) {
436 mtx_unlock(&drv->mutex);
437 return status;
438 }
439
440 if (drv->pipe->flush_resource)
441 drv->pipe->flush_resource(drv->pipe, tex);
442
443 /* flush before calling flush_frontbuffer so that rendering is flushed
444 * to back buffer so the texture can be copied in flush_frontbuffer
445 */
446 vlVaSurfaceFlush(drv, surf);
447
448 screen->flush_frontbuffer(screen, drv->pipe, tex, 0, 0,
449 vscreen->get_private(vscreen), 0, NULL);
450
451
452 pipe_resource_reference(&tex, NULL);
453 pipe_surface_reference(&surf_draw, NULL);
454 mtx_unlock(&drv->mutex);
455
456 return VA_STATUS_SUCCESS;
457 }
458
459 VAStatus
vlVaLockSurface(VADriverContextP ctx,VASurfaceID surface,unsigned int * fourcc,unsigned int * luma_stride,unsigned int * chroma_u_stride,unsigned int * chroma_v_stride,unsigned int * luma_offset,unsigned int * chroma_u_offset,unsigned int * chroma_v_offset,unsigned int * buffer_name,void ** buffer)460 vlVaLockSurface(VADriverContextP ctx, VASurfaceID surface, unsigned int *fourcc,
461 unsigned int *luma_stride, unsigned int *chroma_u_stride, unsigned int *chroma_v_stride,
462 unsigned int *luma_offset, unsigned int *chroma_u_offset, unsigned int *chroma_v_offset,
463 unsigned int *buffer_name, void **buffer)
464 {
465 if (!ctx)
466 return VA_STATUS_ERROR_INVALID_CONTEXT;
467
468 return VA_STATUS_ERROR_UNIMPLEMENTED;
469 }
470
471 VAStatus
vlVaUnlockSurface(VADriverContextP ctx,VASurfaceID surface)472 vlVaUnlockSurface(VADriverContextP ctx, VASurfaceID surface)
473 {
474 if (!ctx)
475 return VA_STATUS_ERROR_INVALID_CONTEXT;
476
477 return VA_STATUS_ERROR_UNIMPLEMENTED;
478 }
479
480 static void
vlVaAddSurfaceFormat(struct pipe_screen * screen,vlVaConfig * config,enum pipe_format format,VASurfaceAttrib * attrib,int * i)481 vlVaAddSurfaceFormat(struct pipe_screen *screen, vlVaConfig *config,
482 enum pipe_format format, VASurfaceAttrib *attrib, int *i)
483 {
484 if (!screen->is_video_format_supported(screen, format, config->profile, config->entrypoint))
485 return;
486
487 attrib[*i].type = VASurfaceAttribPixelFormat;
488 attrib[*i].value.type = VAGenericValueTypeInteger;
489 attrib[*i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
490 attrib[*i].value.value.i = PipeFormatToVaFourcc(format);
491 (*i)++;
492 }
493
494 VAStatus
vlVaQuerySurfaceAttributes(VADriverContextP ctx,VAConfigID config_id,VASurfaceAttrib * attrib_list,unsigned int * num_attribs)495 vlVaQuerySurfaceAttributes(VADriverContextP ctx, VAConfigID config_id,
496 VASurfaceAttrib *attrib_list, unsigned int *num_attribs)
497 {
498 vlVaDriver *drv;
499 vlVaConfig *config;
500 VASurfaceAttrib *attribs;
501 struct pipe_screen *pscreen;
502 int i;
503
504 if (config_id == VA_INVALID_ID)
505 return VA_STATUS_ERROR_INVALID_CONFIG;
506
507 if (!attrib_list && !num_attribs)
508 return VA_STATUS_ERROR_INVALID_PARAMETER;
509
510 if (!attrib_list) {
511 *num_attribs = VL_VA_MAX_IMAGE_FORMATS + VASurfaceAttribCount;
512 return VA_STATUS_SUCCESS;
513 }
514
515 if (!ctx)
516 return VA_STATUS_ERROR_INVALID_CONTEXT;
517
518 drv = VL_VA_DRIVER(ctx);
519
520 if (!drv)
521 return VA_STATUS_ERROR_INVALID_CONTEXT;
522
523 mtx_lock(&drv->mutex);
524 config = handle_table_get(drv->htab, config_id);
525 mtx_unlock(&drv->mutex);
526
527 if (!config)
528 return VA_STATUS_ERROR_INVALID_CONFIG;
529
530 pscreen = VL_VA_PSCREEN(ctx);
531
532 if (!pscreen)
533 return VA_STATUS_ERROR_INVALID_CONTEXT;
534
535 attribs = CALLOC(VL_VA_MAX_IMAGE_FORMATS + VASurfaceAttribCount,
536 sizeof(VASurfaceAttrib));
537
538 if (!attribs)
539 return VA_STATUS_ERROR_ALLOCATION_FAILED;
540
541 i = 0;
542
543 if (config->rt_format & VA_RT_FORMAT_YUV420) {
544 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_NV12, attribs, &i);
545 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_YV12, attribs, &i);
546 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_IYUV, attribs, &i);
547 }
548
549 if (config->rt_format & VA_RT_FORMAT_YUV420_10) {
550 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_P010, attribs, &i);
551 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_P016, attribs, &i);
552 }
553
554 if (config->rt_format & VA_RT_FORMAT_YUV420_12)
555 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_P012, attribs, &i);
556
557 if (config->rt_format & VA_RT_FORMAT_YUV400)
558 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_Y8_400_UNORM, attribs, &i);
559
560 if (config->rt_format & VA_RT_FORMAT_YUV422) {
561 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_UYVY, attribs, &i);
562 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_YUYV, attribs, &i);
563 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_Y8_U8_V8_440_UNORM, attribs, &i);
564 }
565
566 if (config->rt_format & VA_RT_FORMAT_YUV444)
567 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_Y8_U8_V8_444_UNORM, attribs, &i);
568
569 if (config->rt_format & VA_RT_FORMAT_RGBP)
570 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_R8_G8_B8_UNORM, attribs, &i);
571
572 if (config->rt_format & VA_RT_FORMAT_RGB32) {
573 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_R8G8B8A8_UNORM, attribs, &i);
574 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_B8G8R8A8_UNORM, attribs, &i);
575 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_R8G8B8X8_UNORM, attribs, &i);
576 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_B8G8R8X8_UNORM, attribs, &i);
577 }
578
579 if (config->rt_format & VA_RT_FORMAT_RGB32_10) {
580 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_R10G10B10A2_UNORM, attribs, &i);
581 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_B10G10R10A2_UNORM, attribs, &i);
582 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_R10G10B10X2_UNORM, attribs, &i);
583 vlVaAddSurfaceFormat(pscreen, config, PIPE_FORMAT_B10G10R10X2_UNORM, attribs, &i);
584 }
585
586 attribs[i].type = VASurfaceAttribMemoryType;
587 attribs[i].value.type = VAGenericValueTypeInteger;
588 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
589 attribs[i].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA |
590 #ifdef _WIN32
591 VA_SURFACE_ATTRIB_MEM_TYPE_NTHANDLE |
592 VA_SURFACE_ATTRIB_MEM_TYPE_D3D12_RESOURCE;
593 #else
594 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME |
595 #if VA_CHECK_VERSION(1, 21, 0)
596 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3 |
597 #endif
598 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2;
599 #endif
600 i++;
601
602 attribs[i].type = VASurfaceAttribExternalBufferDescriptor;
603 attribs[i].value.type = VAGenericValueTypePointer;
604 attribs[i].flags = VA_SURFACE_ATTRIB_SETTABLE;
605 attribs[i].value.value.p = NULL; /* ignore */
606 i++;
607
608 #ifdef HAVE_VA_SURFACE_ATTRIB_DRM_FORMAT_MODIFIERS
609 if (drv->pipe->create_video_buffer_with_modifiers) {
610 attribs[i].type = VASurfaceAttribDRMFormatModifiers;
611 attribs[i].value.type = VAGenericValueTypePointer;
612 attribs[i].flags = VA_SURFACE_ATTRIB_SETTABLE;
613 attribs[i].value.value.p = NULL; /* ignore */
614 i++;
615 }
616 #endif
617
618 /* If VPP supported entry, use the max dimensions cap values, if not fallback to this below */
619 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_PROCESSING ||
620 pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN,
621 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
622 PIPE_VIDEO_CAP_SUPPORTED))
623 {
624 unsigned min_width, min_height;
625 min_width = pscreen->get_video_param(pscreen,
626 config->profile, config->entrypoint,
627 PIPE_VIDEO_CAP_MIN_WIDTH);
628 min_height = pscreen->get_video_param(pscreen,
629 config->profile, config->entrypoint,
630 PIPE_VIDEO_CAP_MIN_HEIGHT);
631
632 if (min_width > 0 && min_height > 0) {
633 attribs[i].type = VASurfaceAttribMinWidth;
634 attribs[i].value.type = VAGenericValueTypeInteger;
635 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
636 attribs[i].value.value.i = min_width;
637 i++;
638
639 attribs[i].type = VASurfaceAttribMinHeight;
640 attribs[i].value.type = VAGenericValueTypeInteger;
641 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
642 attribs[i].value.value.i = min_height;
643 i++;
644 }
645
646 attribs[i].type = VASurfaceAttribMaxWidth;
647 attribs[i].value.type = VAGenericValueTypeInteger;
648 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
649 attribs[i].value.value.i =
650 pscreen->get_video_param(pscreen,
651 config->profile, config->entrypoint,
652 PIPE_VIDEO_CAP_MAX_WIDTH);
653 i++;
654
655 attribs[i].type = VASurfaceAttribMaxHeight;
656 attribs[i].value.type = VAGenericValueTypeInteger;
657 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
658 attribs[i].value.value.i =
659 pscreen->get_video_param(pscreen,
660 config->profile, config->entrypoint,
661 PIPE_VIDEO_CAP_MAX_HEIGHT);
662 i++;
663 #if VA_CHECK_VERSION(1, 21, 0)
664 int surface_alignment =
665 pscreen->get_video_param(pscreen,
666 config->profile, config->entrypoint,
667 PIPE_VIDEO_CAP_ENC_SURFACE_ALIGNMENT);
668 if (surface_alignment > 0) {
669 attribs[i].type = VASurfaceAttribAlignmentSize;
670 attribs[i].value.type = VAGenericValueTypeInteger;
671 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
672 attribs[i].value.value.i = surface_alignment;
673 i++;
674 }
675 #endif
676 } else {
677 attribs[i].type = VASurfaceAttribMaxWidth;
678 attribs[i].value.type = VAGenericValueTypeInteger;
679 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
680 attribs[i].value.value.i = vl_video_buffer_max_size(pscreen);
681 i++;
682
683 attribs[i].type = VASurfaceAttribMaxHeight;
684 attribs[i].value.type = VAGenericValueTypeInteger;
685 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
686 attribs[i].value.value.i = vl_video_buffer_max_size(pscreen);
687 i++;
688 }
689
690 if (i > *num_attribs) {
691 *num_attribs = i;
692 FREE(attribs);
693 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
694 }
695
696 *num_attribs = i;
697 memcpy(attrib_list, attribs, i * sizeof(VASurfaceAttrib));
698 FREE(attribs);
699
700 return VA_STATUS_SUCCESS;
701 }
702
703 #ifndef _WIN32
704 static VAStatus
surface_from_external_memory(VADriverContextP ctx,vlVaSurface * surface,VASurfaceAttribExternalBuffers * memory_attribute,unsigned index,struct pipe_video_buffer * templat)705 surface_from_external_memory(VADriverContextP ctx, vlVaSurface *surface,
706 VASurfaceAttribExternalBuffers *memory_attribute,
707 unsigned index, struct pipe_video_buffer *templat)
708 {
709 vlVaDriver *drv;
710 struct pipe_screen *pscreen;
711 struct pipe_resource res_templ;
712 struct winsys_handle whandle;
713 struct pipe_resource *resources[VL_NUM_COMPONENTS];
714 enum pipe_format resource_formats[VL_NUM_COMPONENTS];
715 VAStatus result;
716 int i;
717
718 pscreen = VL_VA_PSCREEN(ctx);
719 drv = VL_VA_DRIVER(ctx);
720
721 if (!memory_attribute || !memory_attribute->buffers ||
722 index > memory_attribute->num_buffers)
723 return VA_STATUS_ERROR_INVALID_PARAMETER;
724
725 if (surface->templat.width != memory_attribute->width ||
726 surface->templat.height != memory_attribute->height ||
727 memory_attribute->num_planes < 1)
728 return VA_STATUS_ERROR_INVALID_PARAMETER;
729
730 if (memory_attribute->num_planes > VL_NUM_COMPONENTS)
731 return VA_STATUS_ERROR_INVALID_PARAMETER;
732
733 vl_get_video_buffer_formats(pscreen, templat->buffer_format, resource_formats);
734
735 memset(&res_templ, 0, sizeof(res_templ));
736 res_templ.target = PIPE_TEXTURE_2D;
737 res_templ.last_level = 0;
738 res_templ.depth0 = 1;
739 res_templ.array_size = 1;
740 res_templ.bind = PIPE_BIND_SAMPLER_VIEW;
741 res_templ.usage = PIPE_USAGE_DEFAULT;
742
743 memset(&whandle, 0, sizeof(struct winsys_handle));
744 whandle.type = WINSYS_HANDLE_TYPE_FD;
745 whandle.handle = memory_attribute->buffers[index];
746 whandle.modifier = DRM_FORMAT_MOD_INVALID;
747 whandle.format = templat->buffer_format;
748
749 // Create a resource for each plane.
750 memset(resources, 0, sizeof resources);
751 for (i = 0; i < memory_attribute->num_planes; i++) {
752 unsigned num_planes = util_format_get_num_planes(templat->buffer_format);
753
754 res_templ.format = resource_formats[i];
755 if (res_templ.format == PIPE_FORMAT_NONE) {
756 if (i < num_planes) {
757 result = VA_STATUS_ERROR_INVALID_PARAMETER;
758 goto fail;
759 } else {
760 continue;
761 }
762 }
763
764 res_templ.width0 = util_format_get_plane_width(templat->buffer_format, i,
765 memory_attribute->width);
766 res_templ.height0 = util_format_get_plane_height(templat->buffer_format, i,
767 memory_attribute->height);
768
769 whandle.stride = memory_attribute->pitches[i];
770 whandle.offset = memory_attribute->offsets[i];
771 resources[i] = pscreen->resource_from_handle(pscreen, &res_templ, &whandle,
772 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
773 if (!resources[i]) {
774 result = VA_STATUS_ERROR_ALLOCATION_FAILED;
775 goto fail;
776 }
777 }
778
779 surface->buffer = vl_video_buffer_create_ex2(drv->pipe, templat, resources);
780 if (!surface->buffer) {
781 result = VA_STATUS_ERROR_ALLOCATION_FAILED;
782 goto fail;
783 }
784 return VA_STATUS_SUCCESS;
785
786 fail:
787 for (i = 0; i < VL_NUM_COMPONENTS; i++)
788 pipe_resource_reference(&resources[i], NULL);
789 return result;
790 }
791
792 static VAStatus
surface_from_prime(VADriverContextP ctx,vlVaSurface * surface,VADRMPRIMESurfaceDescriptor * desc,int mem_type,struct pipe_video_buffer * templat)793 surface_from_prime(VADriverContextP ctx, vlVaSurface *surface,
794 VADRMPRIMESurfaceDescriptor *desc, int mem_type,
795 struct pipe_video_buffer *templat)
796 {
797 vlVaDriver *drv;
798 struct pipe_screen *pscreen;
799 struct pipe_resource res_templ;
800 struct winsys_handle whandle;
801 struct pipe_resource *resources[VL_NUM_COMPONENTS];
802 enum pipe_format resource_formats[VL_NUM_COMPONENTS];
803 unsigned num_format_planes, expected_planes, input_planes, plane;
804 VAStatus result;
805
806 num_format_planes = util_format_get_num_planes(templat->buffer_format);
807 pscreen = VL_VA_PSCREEN(ctx);
808 drv = VL_VA_DRIVER(ctx);
809
810 if (!desc || desc->num_layers >= 4 ||desc->num_objects == 0)
811 return VA_STATUS_ERROR_INVALID_PARAMETER;
812
813 if (surface->templat.width != desc->width ||
814 surface->templat.height != desc->height ||
815 desc->num_layers < 1)
816 return VA_STATUS_ERROR_INVALID_PARAMETER;
817
818 if (desc->num_layers > VL_NUM_COMPONENTS)
819 return VA_STATUS_ERROR_INVALID_PARAMETER;
820
821 input_planes = 0;
822 for (unsigned i = 0; i < desc->num_layers; ++i) {
823 if (desc->layers[i].num_planes == 0 || desc->layers[i].num_planes > 4)
824 return VA_STATUS_ERROR_INVALID_PARAMETER;
825
826 for (unsigned j = 0; j < desc->layers[i].num_planes; ++j)
827 if (desc->layers[i].object_index[j] >= desc->num_objects)
828 return VA_STATUS_ERROR_INVALID_PARAMETER;
829
830 input_planes += desc->layers[i].num_planes;
831 }
832
833 expected_planes = num_format_planes;
834 if (desc->objects[0].drm_format_modifier != DRM_FORMAT_MOD_INVALID &&
835 pscreen->is_dmabuf_modifier_supported &&
836 pscreen->is_dmabuf_modifier_supported(pscreen, desc->objects[0].drm_format_modifier,
837 templat->buffer_format, NULL) &&
838 pscreen->get_dmabuf_modifier_planes)
839 expected_planes = pscreen->get_dmabuf_modifier_planes(pscreen, desc->objects[0].drm_format_modifier,
840 templat->buffer_format);
841
842 if (input_planes != expected_planes)
843 return VA_STATUS_ERROR_INVALID_PARAMETER;
844
845 vl_get_video_buffer_formats(pscreen, templat->buffer_format, resource_formats);
846
847 memset(&res_templ, 0, sizeof(res_templ));
848 res_templ.target = PIPE_TEXTURE_2D;
849 res_templ.last_level = 0;
850 res_templ.depth0 = 1;
851 res_templ.array_size = 1;
852 res_templ.bind = PIPE_BIND_SAMPLER_VIEW;
853 res_templ.usage = PIPE_USAGE_DEFAULT;
854 res_templ.format = templat->buffer_format;
855
856 memset(&whandle, 0, sizeof(struct winsys_handle));
857 whandle.type = WINSYS_HANDLE_TYPE_FD;
858 whandle.format = templat->buffer_format;
859 whandle.modifier = desc->objects[0].drm_format_modifier;
860
861 // Create a resource for each plane.
862 memset(resources, 0, sizeof resources);
863
864 /* This does a backwards walk to set the next pointers. It interleaves so
865 * that the main planes always come first and then the first compression metadata
866 * plane of each main plane etc. */
867 plane = input_planes - 1;
868 for (int layer_plane = 3; layer_plane >= 0; --layer_plane) {
869 for (int layer = desc->num_layers - 1; layer >= 0; --layer) {
870 if (layer_plane >= desc->layers[layer].num_planes)
871 continue;
872
873 if (plane < num_format_planes)
874 res_templ.format = resource_formats[plane];
875
876 res_templ.width0 = util_format_get_plane_width(templat->buffer_format, plane,
877 desc->width);
878 res_templ.height0 = util_format_get_plane_height(templat->buffer_format, plane,
879 desc->height);
880 whandle.stride = desc->layers[layer].pitch[layer_plane];
881 whandle.offset = desc->layers[layer].offset[layer_plane];
882 whandle.handle = desc->objects[desc->layers[layer].object_index[layer_plane]].fd;
883 whandle.plane = plane;
884
885 resources[plane] = pscreen->resource_from_handle(pscreen, &res_templ, &whandle,
886 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
887 if (!resources[plane]) {
888 result = VA_STATUS_ERROR_ALLOCATION_FAILED;
889 goto fail;
890 }
891
892 /* After the resource gets created the resource now owns the next reference. */
893 res_templ.next = NULL;
894
895 if (plane)
896 pipe_resource_reference(&res_templ.next, resources[plane]);
897 --plane;
898 }
899 }
900
901 surface->buffer = vl_video_buffer_create_ex2(drv->pipe, templat, resources);
902 if (!surface->buffer) {
903 result = VA_STATUS_ERROR_ALLOCATION_FAILED;
904 goto fail;
905 }
906 return VA_STATUS_SUCCESS;
907
908 fail:
909 pipe_resource_reference(&res_templ.next, NULL);
910 for (int i = 0; i < VL_NUM_COMPONENTS; i++)
911 pipe_resource_reference(&resources[i], NULL);
912 return result;
913 }
914 #else
915 static VAStatus
surface_from_external_win32_memory(VADriverContextP ctx,vlVaSurface * surface,int memory_type,void * res_handle,struct pipe_video_buffer * templat)916 surface_from_external_win32_memory(VADriverContextP ctx, vlVaSurface *surface,
917 int memory_type, void *res_handle,
918 struct pipe_video_buffer *templat)
919 {
920 vlVaDriver *drv;
921 struct pipe_screen *pscreen;
922 struct winsys_handle whandle;
923 VAStatus result;
924
925 pscreen = VL_VA_PSCREEN(ctx);
926 drv = VL_VA_DRIVER(ctx);
927
928 templat->buffer_format = surface->templat.buffer_format;
929 templat->width = surface->templat.width;
930 templat->height = surface->templat.height;
931
932 memset(&whandle, 0, sizeof(whandle));
933 whandle.format = surface->templat.buffer_format;
934 if (memory_type == VA_SURFACE_ATTRIB_MEM_TYPE_NTHANDLE) {
935 whandle.type = WINSYS_HANDLE_TYPE_FD;
936 whandle.handle = res_handle;
937 } else if (memory_type == VA_SURFACE_ATTRIB_MEM_TYPE_D3D12_RESOURCE) {
938 whandle.type = WINSYS_HANDLE_TYPE_D3D12_RES;
939 whandle.com_obj = res_handle;
940 } else {
941 return VA_STATUS_ERROR_INVALID_PARAMETER;
942 }
943
944 surface->buffer = drv->pipe->video_buffer_from_handle(drv->pipe, templat, &whandle, PIPE_USAGE_DEFAULT);
945 if (!surface->buffer) {
946 result = VA_STATUS_ERROR_ALLOCATION_FAILED;
947 goto fail;
948 }
949 return VA_STATUS_SUCCESS;
950
951 fail:
952 return result;
953 }
954
955 #endif
956
957 VAStatus
vlVaHandleSurfaceAllocate(vlVaDriver * drv,vlVaSurface * surface,struct pipe_video_buffer * templat,const uint64_t * modifiers,unsigned int modifiers_count)958 vlVaHandleSurfaceAllocate(vlVaDriver *drv, vlVaSurface *surface,
959 struct pipe_video_buffer *templat,
960 const uint64_t *modifiers,
961 unsigned int modifiers_count)
962 {
963 struct pipe_surface **surfaces;
964 unsigned i;
965
966 if (modifiers_count > 0) {
967 if (!drv->pipe->create_video_buffer_with_modifiers)
968 return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED;
969 surface->buffer =
970 drv->pipe->create_video_buffer_with_modifiers(drv->pipe, templat,
971 modifiers,
972 modifiers_count);
973 } else {
974 surface->buffer = drv->pipe->create_video_buffer(drv->pipe, templat);
975 }
976 if (!surface->buffer)
977 return VA_STATUS_ERROR_ALLOCATION_FAILED;
978
979 if (drv->pipe->screen->get_video_param(drv->pipe->screen,
980 PIPE_VIDEO_PROFILE_UNKNOWN,
981 PIPE_VIDEO_ENTRYPOINT_UNKNOWN,
982 PIPE_VIDEO_CAP_SKIP_CLEAR_SURFACE))
983 return VA_STATUS_SUCCESS;
984
985 surfaces = surface->buffer->get_surfaces(surface->buffer);
986 if (surfaces) {
987 for (i = 0; i < VL_MAX_SURFACES; ++i) {
988 union pipe_color_union c;
989 memset(&c, 0, sizeof(c));
990
991 if (!surfaces[i])
992 continue;
993
994 if (i > !!surface->buffer->interlaced)
995 c.f[0] = c.f[1] = c.f[2] = c.f[3] = 0.5f;
996
997 drv->pipe->clear_render_target(drv->pipe, surfaces[i], &c, 0, 0,
998 surfaces[i]->width, surfaces[i]->height,
999 false);
1000 }
1001 vlVaSurfaceFlush(drv, surface);
1002 }
1003
1004 return VA_STATUS_SUCCESS;
1005 }
1006
1007 struct pipe_video_buffer *
vlVaGetSurfaceBuffer(vlVaDriver * drv,vlVaSurface * surface)1008 vlVaGetSurfaceBuffer(vlVaDriver *drv, vlVaSurface *surface)
1009 {
1010 if (!surface)
1011 return NULL;
1012 if (surface->buffer)
1013 return surface->buffer;
1014 vlVaHandleSurfaceAllocate(drv, surface, &surface->templat, NULL, 0);
1015 return surface->buffer;
1016 }
1017
1018 void
vlVaSurfaceFlush(vlVaDriver * drv,vlVaSurface * surf)1019 vlVaSurfaceFlush(vlVaDriver *drv, vlVaSurface *surf)
1020 {
1021 drv->pipe->flush(drv->pipe, &surf->pipe_fence,
1022 drv->has_external_handles ? 0 : PIPE_FLUSH_ASYNC);
1023 }
1024
1025 static int
rt_format_to_fourcc(uint32_t format)1026 rt_format_to_fourcc(uint32_t format)
1027 {
1028 switch (format) {
1029 case VA_RT_FORMAT_YUV420:
1030 return VA_FOURCC_NV12;
1031 case VA_RT_FORMAT_YUV420_10:
1032 return VA_FOURCC_P010;
1033 case VA_RT_FORMAT_YUV420_12:
1034 return VA_FOURCC_P012;
1035 case VA_RT_FORMAT_YUV422:
1036 return VA_FOURCC_YUY2;
1037 case VA_RT_FORMAT_YUV444:
1038 return VA_FOURCC_444P;
1039 case VA_RT_FORMAT_YUV400:
1040 return VA_FOURCC_Y800;
1041 case VA_RT_FORMAT_RGBP:
1042 return VA_FOURCC_RGBP;
1043 case VA_RT_FORMAT_RGB32:
1044 return VA_FOURCC_BGRA;
1045 case VA_RT_FORMAT_RGB32_10:
1046 return VA_FOURCC_X2R10G10B10;
1047 default:
1048 return 0;
1049 }
1050 }
1051
1052 VAStatus
vlVaCreateSurfaces2(VADriverContextP ctx,unsigned int format,unsigned int width,unsigned int height,VASurfaceID * surfaces,unsigned int num_surfaces,VASurfaceAttrib * attrib_list,unsigned int num_attribs)1053 vlVaCreateSurfaces2(VADriverContextP ctx, unsigned int format,
1054 unsigned int width, unsigned int height,
1055 VASurfaceID *surfaces, unsigned int num_surfaces,
1056 VASurfaceAttrib *attrib_list, unsigned int num_attribs)
1057 {
1058 vlVaDriver *drv;
1059 VASurfaceAttribExternalBuffers *memory_attribute;
1060 #ifdef _WIN32
1061 void **win32_handles;
1062 #else
1063 VADRMPRIMESurfaceDescriptor *prime_desc = NULL;
1064 #ifdef HAVE_VA_SURFACE_ATTRIB_DRM_FORMAT_MODIFIERS
1065 const VADRMFormatModifierList *modifier_list;
1066 #endif
1067 #endif
1068 struct pipe_video_buffer templat = {0};
1069 struct pipe_screen *pscreen;
1070 int i;
1071 int memory_type;
1072 int expected_fourcc;
1073 VAStatus vaStatus;
1074 vlVaSurface *surf;
1075 bool protected;
1076 const uint64_t *modifiers;
1077 unsigned int modifiers_count;
1078
1079 if (!ctx)
1080 return VA_STATUS_ERROR_INVALID_CONTEXT;
1081
1082 if (!(width && height))
1083 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
1084
1085 drv = VL_VA_DRIVER(ctx);
1086
1087 if (!drv)
1088 return VA_STATUS_ERROR_INVALID_CONTEXT;
1089
1090 pscreen = VL_VA_PSCREEN(ctx);
1091
1092 if (!pscreen)
1093 return VA_STATUS_ERROR_INVALID_CONTEXT;
1094
1095 /* Default. */
1096 memory_attribute = NULL;
1097 memory_type = VA_SURFACE_ATTRIB_MEM_TYPE_VA;
1098 expected_fourcc = 0;
1099 modifiers = NULL;
1100 modifiers_count = 0;
1101
1102 protected = format & VA_RT_FORMAT_PROTECTED;
1103 format &= ~VA_RT_FORMAT_PROTECTED;
1104
1105 expected_fourcc = rt_format_to_fourcc(format);
1106 if (!expected_fourcc)
1107 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
1108
1109 for (i = 0; i < num_attribs && attrib_list; i++) {
1110 if (!(attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE))
1111 continue;
1112
1113 switch (attrib_list[i].type) {
1114 case VASurfaceAttribPixelFormat:
1115 if (attrib_list[i].value.type != VAGenericValueTypeInteger)
1116 return VA_STATUS_ERROR_INVALID_PARAMETER;
1117 expected_fourcc = attrib_list[i].value.value.i;
1118 break;
1119 case VASurfaceAttribMemoryType:
1120 if (attrib_list[i].value.type != VAGenericValueTypeInteger)
1121 return VA_STATUS_ERROR_INVALID_PARAMETER;
1122
1123 switch (attrib_list[i].value.value.i) {
1124 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
1125
1126 #ifdef _WIN32
1127 case VA_SURFACE_ATTRIB_MEM_TYPE_NTHANDLE:
1128 case VA_SURFACE_ATTRIB_MEM_TYPE_D3D12_RESOURCE:
1129 #else
1130 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
1131 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2:
1132 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3:
1133 #endif
1134 memory_type = attrib_list[i].value.value.i;
1135 break;
1136 default:
1137 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
1138 }
1139 break;
1140 case VASurfaceAttribExternalBufferDescriptor:
1141 if (attrib_list[i].value.type != VAGenericValueTypePointer)
1142 return VA_STATUS_ERROR_INVALID_PARAMETER;
1143 #ifndef _WIN32
1144 if (memory_type == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2 ||
1145 memory_type == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3)
1146 prime_desc = (VADRMPRIMESurfaceDescriptor *)attrib_list[i].value.value.p;
1147 #else
1148 else if (memory_type == VA_SURFACE_ATTRIB_MEM_TYPE_NTHANDLE ||
1149 memory_type == VA_SURFACE_ATTRIB_MEM_TYPE_D3D12_RESOURCE)
1150 win32_handles = (void**) attrib_list[i].value.value.p;
1151 #endif
1152 else
1153 memory_attribute = (VASurfaceAttribExternalBuffers *)attrib_list[i].value.value.p;
1154 break;
1155 #ifndef _WIN32
1156 #ifdef HAVE_VA_SURFACE_ATTRIB_DRM_FORMAT_MODIFIERS
1157 case VASurfaceAttribDRMFormatModifiers:
1158 if (attrib_list[i].value.type != VAGenericValueTypePointer)
1159 return VA_STATUS_ERROR_INVALID_PARAMETER;
1160 modifier_list = attrib_list[i].value.value.p;
1161 if (modifier_list != NULL) {
1162 modifiers = modifier_list->modifiers;
1163 modifiers_count = modifier_list->num_modifiers;
1164 }
1165 break;
1166 #endif
1167 #endif
1168 case VASurfaceAttribUsageHint:
1169 if (attrib_list[i].value.type != VAGenericValueTypeInteger)
1170 return VA_STATUS_ERROR_INVALID_PARAMETER;
1171 break;
1172 default:
1173 return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED;
1174 }
1175 }
1176
1177 switch (memory_type) {
1178 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
1179 break;
1180 #ifdef _WIN32
1181 case VA_SURFACE_ATTRIB_MEM_TYPE_NTHANDLE:
1182 case VA_SURFACE_ATTRIB_MEM_TYPE_D3D12_RESOURCE:
1183 if (!win32_handles)
1184 return VA_STATUS_ERROR_INVALID_PARAMETER;
1185 break;
1186 #else
1187 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
1188 if (!memory_attribute)
1189 return VA_STATUS_ERROR_INVALID_PARAMETER;
1190 if (modifiers)
1191 return VA_STATUS_ERROR_INVALID_PARAMETER;
1192
1193 expected_fourcc = memory_attribute->pixel_format;
1194 break;
1195 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2:
1196 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3:
1197 /* If we don't have surface descriptor, use it as a hint
1198 * that application will export the surface later. */
1199 if (!prime_desc) {
1200 templat.bind |= PIPE_BIND_SHARED;
1201 memory_type = VA_SURFACE_ATTRIB_MEM_TYPE_VA;
1202 } else {
1203 expected_fourcc = prime_desc->fourcc;
1204 }
1205 break;
1206 #endif
1207 default:
1208 assert(0);
1209 }
1210
1211 if (!modifiers)
1212 templat.interlaced =
1213 !pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN,
1214 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
1215 PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE);
1216
1217 #ifndef _WIN32
1218 if (expected_fourcc != VA_FOURCC_NV12 || memory_attribute || prime_desc)
1219 #else
1220 if (expected_fourcc != VA_FOURCC_NV12 || memory_attribute)
1221 #endif
1222 templat.interlaced = false;
1223
1224 templat.buffer_format = VaFourccToPipeFormat(expected_fourcc);
1225 templat.width = width;
1226 templat.height = height;
1227 if (protected)
1228 templat.bind |= PIPE_BIND_PROTECTED;
1229
1230 memset(surfaces, VA_INVALID_ID, num_surfaces * sizeof(VASurfaceID));
1231
1232 mtx_lock(&drv->mutex);
1233 for (i = 0; i < num_surfaces; i++) {
1234 surf = CALLOC(1, sizeof(vlVaSurface));
1235 if (!surf) {
1236 vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1237 goto no_res;
1238 }
1239
1240 surf->templat = templat;
1241
1242 switch (memory_type) {
1243 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
1244 /* The application will clear the TILING flag when the surface is
1245 * intended to be exported as dmabuf. Adding shared flag because not
1246 * null memory_attribute means VASurfaceAttribExternalBuffers is used.
1247 */
1248 if (memory_attribute &&
1249 !(memory_attribute->flags & VA_SURFACE_EXTBUF_DESC_ENABLE_TILING))
1250 surf->templat.bind = PIPE_BIND_LINEAR | PIPE_BIND_SHARED;
1251
1252 if (modifiers) {
1253 vaStatus = vlVaHandleSurfaceAllocate(drv, surf, &surf->templat, modifiers,
1254 modifiers_count);
1255 if (vaStatus != VA_STATUS_SUCCESS)
1256 goto free_surf;
1257 } /* Delayed allocation from vlVaGetSurfaceBuffer otherwise */
1258 break;
1259
1260 #ifdef _WIN32
1261 case VA_SURFACE_ATTRIB_MEM_TYPE_NTHANDLE:
1262 case VA_SURFACE_ATTRIB_MEM_TYPE_D3D12_RESOURCE:
1263 vaStatus = surface_from_external_win32_memory(ctx, surf, memory_type, win32_handles[i], &templat);
1264 if (vaStatus != VA_STATUS_SUCCESS)
1265 goto free_surf;
1266 break;
1267 #else
1268 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
1269 vaStatus = surface_from_external_memory(ctx, surf, memory_attribute, i, &templat);
1270 if (vaStatus != VA_STATUS_SUCCESS)
1271 goto free_surf;
1272 break;
1273
1274 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2:
1275 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3:
1276 vaStatus = surface_from_prime(ctx, surf, prime_desc, memory_type, &templat);
1277 if (vaStatus != VA_STATUS_SUCCESS)
1278 goto free_surf;
1279 break;
1280 #endif
1281 default:
1282 assert(0);
1283 }
1284
1285 util_dynarray_init(&surf->subpics, NULL);
1286 surfaces[i] = handle_table_add(drv->htab, surf);
1287 if (!surfaces[i]) {
1288 vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1289 goto destroy_surf;
1290 }
1291 }
1292
1293 if (memory_type != VA_SURFACE_ATTRIB_MEM_TYPE_VA)
1294 drv->has_external_handles = true;
1295 mtx_unlock(&drv->mutex);
1296
1297 return VA_STATUS_SUCCESS;
1298
1299 destroy_surf:
1300 if (surf->buffer)
1301 surf->buffer->destroy(surf->buffer);
1302
1303 free_surf:
1304 FREE(surf);
1305
1306 no_res:
1307 mtx_unlock(&drv->mutex);
1308 if (i)
1309 vlVaDestroySurfaces(ctx, surfaces, i);
1310
1311 return vaStatus;
1312 }
1313
1314 VAStatus
vlVaQueryVideoProcFilters(VADriverContextP ctx,VAContextID context,VAProcFilterType * filters,unsigned int * num_filters)1315 vlVaQueryVideoProcFilters(VADriverContextP ctx, VAContextID context,
1316 VAProcFilterType *filters, unsigned int *num_filters)
1317 {
1318 unsigned int num = 0;
1319
1320 if (!ctx)
1321 return VA_STATUS_ERROR_INVALID_CONTEXT;
1322
1323 if (!num_filters || !filters)
1324 return VA_STATUS_ERROR_INVALID_PARAMETER;
1325
1326 filters[num++] = VAProcFilterDeinterlacing;
1327
1328 *num_filters = num;
1329
1330 return VA_STATUS_SUCCESS;
1331 }
1332
1333 VAStatus
vlVaQueryVideoProcFilterCaps(VADriverContextP ctx,VAContextID context,VAProcFilterType type,void * filter_caps,unsigned int * num_filter_caps)1334 vlVaQueryVideoProcFilterCaps(VADriverContextP ctx, VAContextID context,
1335 VAProcFilterType type, void *filter_caps,
1336 unsigned int *num_filter_caps)
1337 {
1338 unsigned int i;
1339
1340 if (!ctx)
1341 return VA_STATUS_ERROR_INVALID_CONTEXT;
1342
1343 if (!filter_caps || !num_filter_caps)
1344 return VA_STATUS_ERROR_INVALID_PARAMETER;
1345
1346 i = 0;
1347
1348 switch (type) {
1349 case VAProcFilterNone:
1350 break;
1351 case VAProcFilterDeinterlacing: {
1352 VAProcFilterCapDeinterlacing *deint = filter_caps;
1353
1354 if (*num_filter_caps < 3) {
1355 *num_filter_caps = 3;
1356 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
1357 }
1358
1359 deint[i++].type = VAProcDeinterlacingBob;
1360 deint[i++].type = VAProcDeinterlacingWeave;
1361 deint[i++].type = VAProcDeinterlacingMotionAdaptive;
1362 break;
1363 }
1364
1365 case VAProcFilterNoiseReduction:
1366 case VAProcFilterSharpening:
1367 case VAProcFilterColorBalance:
1368 case VAProcFilterSkinToneEnhancement:
1369 return VA_STATUS_ERROR_UNIMPLEMENTED;
1370 default:
1371 assert(0);
1372 }
1373
1374 *num_filter_caps = i;
1375
1376 return VA_STATUS_SUCCESS;
1377 }
1378
1379 static VAProcColorStandardType vpp_input_color_standards[] = {
1380 VAProcColorStandardBT601,
1381 VAProcColorStandardBT709
1382 };
1383
1384 static VAProcColorStandardType vpp_output_color_standards[] = {
1385 VAProcColorStandardBT601,
1386 VAProcColorStandardBT709
1387 };
1388
1389 static VAProcColorStandardType vpp_input_color_standards_extends[] = {
1390 VAProcColorStandardBT601,
1391 VAProcColorStandardBT709,
1392 VAProcColorStandardBT2020,
1393 VAProcColorStandardExplicit
1394 };
1395
1396 static VAProcColorStandardType vpp_output_color_standards_extends[] = {
1397 VAProcColorStandardBT601,
1398 VAProcColorStandardBT709,
1399 VAProcColorStandardBT2020,
1400 VAProcColorStandardExplicit
1401 };
1402
1403 VAStatus
vlVaQueryVideoProcPipelineCaps(VADriverContextP ctx,VAContextID context,VABufferID * filters,unsigned int num_filters,VAProcPipelineCaps * pipeline_cap)1404 vlVaQueryVideoProcPipelineCaps(VADriverContextP ctx, VAContextID context,
1405 VABufferID *filters, unsigned int num_filters,
1406 VAProcPipelineCaps *pipeline_cap)
1407 {
1408 unsigned int i = 0;
1409
1410 if (!ctx)
1411 return VA_STATUS_ERROR_INVALID_CONTEXT;
1412
1413 if (!pipeline_cap)
1414 return VA_STATUS_ERROR_INVALID_PARAMETER;
1415
1416 if (num_filters && !filters)
1417 return VA_STATUS_ERROR_INVALID_PARAMETER;
1418
1419 pipeline_cap->pipeline_flags = 0;
1420 pipeline_cap->filter_flags = 0;
1421 pipeline_cap->num_forward_references = 0;
1422 pipeline_cap->num_backward_references = 0;
1423 pipeline_cap->rotation_flags = VA_ROTATION_NONE;
1424 pipeline_cap->mirror_flags = VA_MIRROR_NONE;
1425
1426 struct pipe_screen *pscreen = VL_VA_PSCREEN(ctx);
1427 bool media_only = !pscreen->caps.graphics && !pscreen->caps.compute;
1428 uint32_t pipe_orientation_flags = pscreen->get_video_param(pscreen,
1429 PIPE_VIDEO_PROFILE_UNKNOWN,
1430 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1431 PIPE_VIDEO_CAP_VPP_ORIENTATION_MODES);
1432
1433 if (!media_only || pipe_orientation_flags & PIPE_VIDEO_VPP_ROTATION_90)
1434 pipeline_cap->rotation_flags |= (1 << VA_ROTATION_90);
1435 if (!media_only || pipe_orientation_flags & PIPE_VIDEO_VPP_ROTATION_180)
1436 pipeline_cap->rotation_flags |= (1 << VA_ROTATION_180);
1437 if (!media_only || pipe_orientation_flags & PIPE_VIDEO_VPP_ROTATION_270)
1438 pipeline_cap->rotation_flags |= (1 << VA_ROTATION_270);
1439
1440 if (!media_only || pipe_orientation_flags & PIPE_VIDEO_VPP_FLIP_HORIZONTAL)
1441 pipeline_cap->mirror_flags |= VA_MIRROR_HORIZONTAL;
1442 if (!media_only || pipe_orientation_flags & PIPE_VIDEO_VPP_FLIP_VERTICAL)
1443 pipeline_cap->mirror_flags |= VA_MIRROR_VERTICAL;
1444
1445 if (pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN, PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1446 PIPE_VIDEO_CAP_VPP_SUPPORT_HDR_INPUT)) {
1447 pipeline_cap->num_input_color_standards = ARRAY_SIZE(vpp_input_color_standards_extends);
1448 pipeline_cap->input_color_standards = vpp_input_color_standards_extends;
1449 } else {
1450 pipeline_cap->num_input_color_standards = ARRAY_SIZE(vpp_input_color_standards);
1451 pipeline_cap->input_color_standards = vpp_input_color_standards;
1452 }
1453 if (pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN, PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1454 PIPE_VIDEO_CAP_VPP_SUPPORT_HDR_OUTPUT)) {
1455 pipeline_cap->num_output_color_standards = ARRAY_SIZE(vpp_output_color_standards_extends);
1456 pipeline_cap->output_color_standards = vpp_output_color_standards_extends;
1457 } else {
1458 pipeline_cap->num_output_color_standards = ARRAY_SIZE(vpp_output_color_standards);
1459 pipeline_cap->output_color_standards = vpp_output_color_standards;
1460 }
1461
1462 pipeline_cap->max_input_width = pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN,
1463 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1464 PIPE_VIDEO_CAP_VPP_MAX_INPUT_WIDTH);
1465
1466 pipeline_cap->max_input_height = pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN,
1467 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1468 PIPE_VIDEO_CAP_VPP_MAX_INPUT_HEIGHT);
1469
1470 pipeline_cap->min_input_width = pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN,
1471 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1472 PIPE_VIDEO_CAP_VPP_MIN_INPUT_WIDTH);
1473
1474 pipeline_cap->min_input_height = pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN,
1475 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1476 PIPE_VIDEO_CAP_VPP_MIN_INPUT_HEIGHT);
1477
1478 pipeline_cap->max_output_width = pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN,
1479 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1480 PIPE_VIDEO_CAP_VPP_MAX_OUTPUT_WIDTH);
1481
1482 pipeline_cap->max_output_height = pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN,
1483 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1484 PIPE_VIDEO_CAP_VPP_MAX_OUTPUT_HEIGHT);
1485
1486 pipeline_cap->min_output_width = pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN,
1487 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1488 PIPE_VIDEO_CAP_VPP_MIN_OUTPUT_WIDTH);
1489
1490 pipeline_cap->min_output_height = pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN,
1491 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1492 PIPE_VIDEO_CAP_VPP_MIN_OUTPUT_HEIGHT);
1493
1494 uint32_t pipe_blend_modes = pscreen->get_video_param(pscreen, PIPE_VIDEO_PROFILE_UNKNOWN,
1495 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
1496 PIPE_VIDEO_CAP_VPP_BLEND_MODES);
1497
1498 pipeline_cap->blend_flags = 0;
1499 if (pipe_blend_modes & PIPE_VIDEO_VPP_BLEND_MODE_GLOBAL_ALPHA)
1500 pipeline_cap->blend_flags |= VA_BLEND_GLOBAL_ALPHA;
1501
1502 vlVaDriver *drv = VL_VA_DRIVER(ctx);
1503
1504 mtx_lock(&drv->mutex);
1505 for (i = 0; i < num_filters; i++) {
1506 vlVaBuffer *buf = handle_table_get(drv->htab, filters[i]);
1507 VAProcFilterParameterBufferBase *filter;
1508
1509 if (!buf || buf->type != VAProcFilterParameterBufferType) {
1510 mtx_unlock(&drv->mutex);
1511 return VA_STATUS_ERROR_INVALID_BUFFER;
1512 }
1513
1514 filter = buf->data;
1515 switch (filter->type) {
1516 case VAProcFilterDeinterlacing: {
1517 VAProcFilterParameterBufferDeinterlacing *deint = buf->data;
1518 if (deint->algorithm == VAProcDeinterlacingMotionAdaptive) {
1519 pipeline_cap->num_forward_references = 2;
1520 pipeline_cap->num_backward_references = 1;
1521 }
1522 break;
1523 }
1524 default:
1525 mtx_unlock(&drv->mutex);
1526 return VA_STATUS_ERROR_UNIMPLEMENTED;
1527 }
1528 }
1529 mtx_unlock(&drv->mutex);
1530
1531 return VA_STATUS_SUCCESS;
1532 }
1533
1534 #ifndef _WIN32
pipe_format_to_drm_format(enum pipe_format format)1535 static uint32_t pipe_format_to_drm_format(enum pipe_format format)
1536 {
1537 switch (format) {
1538 case PIPE_FORMAT_R8_UNORM:
1539 return DRM_FORMAT_R8;
1540 case PIPE_FORMAT_R8G8_UNORM:
1541 return DRM_FORMAT_GR88;
1542 case PIPE_FORMAT_R16_UNORM:
1543 return DRM_FORMAT_R16;
1544 case PIPE_FORMAT_R16G16_UNORM:
1545 return DRM_FORMAT_GR1616;
1546 case PIPE_FORMAT_B8G8R8A8_UNORM:
1547 return DRM_FORMAT_ARGB8888;
1548 case PIPE_FORMAT_R8G8B8A8_UNORM:
1549 return DRM_FORMAT_ABGR8888;
1550 case PIPE_FORMAT_B8G8R8X8_UNORM:
1551 return DRM_FORMAT_XRGB8888;
1552 case PIPE_FORMAT_R8G8B8X8_UNORM:
1553 return DRM_FORMAT_XBGR8888;
1554 case PIPE_FORMAT_B10G10R10A2_UNORM:
1555 return DRM_FORMAT_ARGB2101010;
1556 case PIPE_FORMAT_R10G10B10A2_UNORM:
1557 return DRM_FORMAT_ABGR2101010;
1558 case PIPE_FORMAT_B10G10R10X2_UNORM:
1559 return DRM_FORMAT_XRGB2101010;
1560 case PIPE_FORMAT_R10G10B10X2_UNORM:
1561 return DRM_FORMAT_XBGR2101010;
1562 case PIPE_FORMAT_NV12:
1563 return DRM_FORMAT_NV12;
1564 case PIPE_FORMAT_P010:
1565 return DRM_FORMAT_P010;
1566 case PIPE_FORMAT_P012:
1567 return DRM_FORMAT_P012;
1568 case PIPE_FORMAT_YUYV:
1569 case PIPE_FORMAT_R8G8_R8B8_UNORM:
1570 return DRM_FORMAT_YUYV;
1571 default:
1572 return DRM_FORMAT_INVALID;
1573 }
1574 }
1575 #endif
1576
1577 #if VA_CHECK_VERSION(1, 1, 0)
1578 VAStatus
vlVaExportSurfaceHandle(VADriverContextP ctx,VASurfaceID surface_id,uint32_t mem_type,uint32_t flags,void * descriptor)1579 vlVaExportSurfaceHandle(VADriverContextP ctx,
1580 VASurfaceID surface_id,
1581 uint32_t mem_type,
1582 uint32_t flags,
1583 void *descriptor)
1584 {
1585 vlVaDriver *drv;
1586 vlVaSurface *surf;
1587 struct pipe_surface **surfaces;
1588 struct pipe_screen *screen;
1589 VAStatus ret;
1590 unsigned int usage;
1591
1592 #ifdef _WIN32
1593 if ((mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_NTHANDLE)
1594 && (mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_D3D12_RESOURCE))
1595 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
1596
1597 if ((flags & VA_EXPORT_SURFACE_COMPOSED_LAYERS) == 0)
1598 return VA_STATUS_ERROR_INVALID_SURFACE;
1599 #else
1600 int i, p;
1601 if (mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2 &&
1602 mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3)
1603 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
1604 #endif
1605
1606 drv = VL_VA_DRIVER(ctx);
1607 screen = VL_VA_PSCREEN(ctx);
1608 mtx_lock(&drv->mutex);
1609
1610 surf = handle_table_get(drv->htab, surface_id);
1611 vlVaGetSurfaceBuffer(drv, surf);
1612 if (!surf || !surf->buffer) {
1613 mtx_unlock(&drv->mutex);
1614 return VA_STATUS_ERROR_INVALID_SURFACE;
1615 }
1616
1617 if (surf->buffer->interlaced) {
1618 mtx_unlock(&drv->mutex);
1619 return VA_STATUS_ERROR_INVALID_SURFACE;
1620 }
1621
1622 surfaces = surf->buffer->get_surfaces(surf->buffer);
1623
1624 usage = 0;
1625 if (flags & VA_EXPORT_SURFACE_WRITE_ONLY)
1626 usage |= PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
1627
1628 #ifdef _WIN32
1629 struct winsys_handle whandle;
1630 memset(&whandle, 0, sizeof(struct winsys_handle));
1631 struct pipe_resource *resource = surfaces[0]->texture;
1632
1633 if (mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_NTHANDLE)
1634 whandle.type = WINSYS_HANDLE_TYPE_FD;
1635 else if (mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_D3D12_RESOURCE)
1636 whandle.type = WINSYS_HANDLE_TYPE_D3D12_RES;
1637
1638 if (!screen->resource_get_handle(screen, drv->pipe, resource,
1639 &whandle, usage)) {
1640 ret = VA_STATUS_ERROR_INVALID_SURFACE;
1641 goto fail;
1642 }
1643
1644 if (mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_NTHANDLE)
1645 *(HANDLE**)descriptor = whandle.handle;
1646 else if (mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_D3D12_RESOURCE)
1647 *(void**) descriptor = whandle.com_obj;
1648
1649 #else
1650 VADRMPRIMESurfaceDescriptor *desc = descriptor;
1651 desc->fourcc = PipeFormatToVaFourcc(surf->buffer->buffer_format);
1652 desc->width = surf->templat.width;
1653 desc->height = surf->templat.height;
1654 desc->num_objects = 0;
1655
1656 bool supports_contiguous_planes = screen->resource_get_info && surf->buffer->contiguous_planes;
1657
1658 for (p = 0; p < ARRAY_SIZE(desc->objects); p++) {
1659 struct winsys_handle whandle;
1660 struct pipe_resource *resource;
1661 uint32_t drm_format;
1662
1663 if (!surfaces[p])
1664 break;
1665
1666 resource = surfaces[p]->texture;
1667
1668 drm_format = pipe_format_to_drm_format(resource->format);
1669 if (drm_format == DRM_FORMAT_INVALID) {
1670 ret = VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
1671 goto fail;
1672 }
1673
1674 /* If the driver stores all planes contiguously in memory, only one
1675 * handle needs to be exported. resource_get_info is used to obtain
1676 * pitch and offset for each layer. */
1677 if (!desc->num_objects || !supports_contiguous_planes) {
1678 memset(&whandle, 0, sizeof(whandle));
1679 whandle.type = WINSYS_HANDLE_TYPE_FD;
1680
1681 if (!screen->resource_get_handle(screen, drv->pipe, resource,
1682 &whandle, usage)) {
1683 ret = VA_STATUS_ERROR_INVALID_SURFACE;
1684 goto fail;
1685 }
1686
1687 desc->objects[desc->num_objects].fd = (int) whandle.handle;
1688 /* As per VADRMPRIMESurfaceDescriptor documentation, size must be the
1689 * "Total size of this object (may include regions which are not part
1690 * of the surface)."" */
1691 desc->objects[desc->num_objects].size = (uint32_t) whandle.size;
1692 desc->objects[desc->num_objects].drm_format_modifier = whandle.modifier;
1693
1694 desc->num_objects++;
1695 }
1696
1697 if (flags & VA_EXPORT_SURFACE_COMPOSED_LAYERS) {
1698 desc->layers[0].object_index[p] = desc->num_objects - 1;
1699
1700 if (supports_contiguous_planes) {
1701 screen->resource_get_info(screen, resource, &desc->layers[0].pitch[p], &desc->layers[0].offset[p]);
1702 } else {
1703 desc->layers[0].pitch[p] = whandle.stride;
1704 desc->layers[0].offset[p] = whandle.offset;
1705 }
1706 } else {
1707 desc->layers[p].drm_format = drm_format;
1708 desc->layers[p].num_planes = 1;
1709 desc->layers[p].object_index[0] = desc->num_objects - 1;
1710
1711 if (supports_contiguous_planes) {
1712 screen->resource_get_info(screen, resource, &desc->layers[p].pitch[0], &desc->layers[p].offset[0]);
1713 } else {
1714 desc->layers[p].pitch[0] = whandle.stride;
1715 desc->layers[p].offset[0] = whandle.offset;
1716 }
1717 }
1718 }
1719
1720 if (flags & VA_EXPORT_SURFACE_COMPOSED_LAYERS) {
1721 uint32_t drm_format = pipe_format_to_drm_format(surf->buffer->buffer_format);
1722 if (drm_format == DRM_FORMAT_INVALID) {
1723 ret = VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
1724 goto fail;
1725 }
1726
1727 desc->num_layers = 1;
1728 desc->layers[0].drm_format = drm_format;
1729 desc->layers[0].num_planes = p;
1730 } else {
1731 desc->num_layers = p;
1732 }
1733
1734 #if VA_CHECK_VERSION(1, 21, 0)
1735 if (mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3) {
1736 VADRMPRIME3SurfaceDescriptor *desc3 = descriptor;
1737 memset(desc3->reserved, 0, sizeof(desc3->reserved));
1738 desc3->flags = 0;
1739 if (surf->templat.bind & PIPE_BIND_PROTECTED)
1740 desc3->flags |= VA_SURFACE_EXTBUF_DESC_PROTECTED;
1741 }
1742 #endif
1743
1744 #endif
1745
1746 drv->has_external_handles = true;
1747 mtx_unlock(&drv->mutex);
1748
1749 return VA_STATUS_SUCCESS;
1750
1751 fail:
1752 #ifndef _WIN32
1753 for (i = 0; i < desc->num_objects; i++)
1754 close(desc->objects[i].fd);
1755 #else
1756 if(whandle.handle)
1757 CloseHandle(whandle.handle);
1758 #endif
1759
1760 mtx_unlock(&drv->mutex);
1761
1762 return ret;
1763 }
1764 #endif
1765