gallium: rework PIPE_HANDLE_USAGE_* flags
[mesa.git] / src / gallium / state_trackers / va / surface.c
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 "state_tracker/drm_driver.h"
33
34 #include "util/u_memory.h"
35 #include "util/u_handle_table.h"
36 #include "util/u_rect.h"
37 #include "util/u_sampler.h"
38 #include "util/u_surface.h"
39 #include "util/u_video.h"
40
41 #include "vl/vl_compositor.h"
42 #include "vl/vl_video_buffer.h"
43 #include "vl/vl_winsys.h"
44
45 #include "va_private.h"
46
47 #include <va/va_drmcommon.h>
48 #include <drm-uapi/drm_fourcc.h>
49
50 static const enum pipe_format vpp_surface_formats[] = {
51 PIPE_FORMAT_B8G8R8A8_UNORM, PIPE_FORMAT_R8G8B8A8_UNORM,
52 PIPE_FORMAT_B8G8R8X8_UNORM, PIPE_FORMAT_R8G8B8X8_UNORM
53 };
54
55 VAStatus
56 vlVaCreateSurfaces(VADriverContextP ctx, int width, int height, int format,
57 int num_surfaces, VASurfaceID *surfaces)
58 {
59 return vlVaCreateSurfaces2(ctx, format, width, height, surfaces, num_surfaces,
60 NULL, 0);
61 }
62
63 VAStatus
64 vlVaDestroySurfaces(VADriverContextP ctx, VASurfaceID *surface_list, int num_surfaces)
65 {
66 vlVaDriver *drv;
67 int i;
68
69 if (!ctx)
70 return VA_STATUS_ERROR_INVALID_CONTEXT;
71
72 drv = VL_VA_DRIVER(ctx);
73 mtx_lock(&drv->mutex);
74 for (i = 0; i < num_surfaces; ++i) {
75 vlVaSurface *surf = handle_table_get(drv->htab, surface_list[i]);
76 if (!surf) {
77 mtx_unlock(&drv->mutex);
78 return VA_STATUS_ERROR_INVALID_SURFACE;
79 }
80 if (surf->buffer)
81 surf->buffer->destroy(surf->buffer);
82 util_dynarray_fini(&surf->subpics);
83 FREE(surf);
84 handle_table_remove(drv->htab, surface_list[i]);
85 }
86 mtx_unlock(&drv->mutex);
87
88 return VA_STATUS_SUCCESS;
89 }
90
91 VAStatus
92 vlVaSyncSurface(VADriverContextP ctx, VASurfaceID render_target)
93 {
94 vlVaDriver *drv;
95 vlVaContext *context;
96 vlVaSurface *surf;
97
98 if (!ctx)
99 return VA_STATUS_ERROR_INVALID_CONTEXT;
100
101 drv = VL_VA_DRIVER(ctx);
102 if (!drv)
103 return VA_STATUS_ERROR_INVALID_CONTEXT;
104
105 mtx_lock(&drv->mutex);
106 surf = handle_table_get(drv->htab, render_target);
107
108 if (!surf || !surf->buffer) {
109 mtx_unlock(&drv->mutex);
110 return VA_STATUS_ERROR_INVALID_SURFACE;
111 }
112
113 if (!surf->feedback) {
114 // No outstanding operation: nothing to do.
115 mtx_unlock(&drv->mutex);
116 return VA_STATUS_SUCCESS;
117 }
118
119 context = handle_table_get(drv->htab, surf->ctx);
120 if (!context) {
121 mtx_unlock(&drv->mutex);
122 return VA_STATUS_ERROR_INVALID_CONTEXT;
123 }
124
125 if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
126 if (u_reduce_video_profile(context->templat.profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC) {
127 int frame_diff;
128 if (context->desc.h264enc.frame_num_cnt >= surf->frame_num_cnt)
129 frame_diff = context->desc.h264enc.frame_num_cnt - surf->frame_num_cnt;
130 else
131 frame_diff = 0xFFFFFFFF - surf->frame_num_cnt + 1 + context->desc.h264enc.frame_num_cnt;
132 if ((frame_diff == 0) &&
133 (surf->force_flushed == false) &&
134 (context->desc.h264enc.frame_num_cnt % 2 != 0)) {
135 context->decoder->flush(context->decoder);
136 context->first_single_submitted = true;
137 }
138 }
139 context->decoder->get_feedback(context->decoder, surf->feedback, &(surf->coded_buf->coded_size));
140 surf->feedback = NULL;
141 }
142 mtx_unlock(&drv->mutex);
143 return VA_STATUS_SUCCESS;
144 }
145
146 VAStatus
147 vlVaQuerySurfaceStatus(VADriverContextP ctx, VASurfaceID render_target, VASurfaceStatus *status)
148 {
149 if (!ctx)
150 return VA_STATUS_ERROR_INVALID_CONTEXT;
151
152 return VA_STATUS_SUCCESS;
153 }
154
155 VAStatus
156 vlVaQuerySurfaceError(VADriverContextP ctx, VASurfaceID render_target, VAStatus error_status, void **error_info)
157 {
158 if (!ctx)
159 return VA_STATUS_ERROR_INVALID_CONTEXT;
160
161 return VA_STATUS_ERROR_UNIMPLEMENTED;
162 }
163
164 static void
165 upload_sampler(struct pipe_context *pipe, struct pipe_sampler_view *dst,
166 const struct pipe_box *dst_box, const void *src, unsigned src_stride,
167 unsigned src_x, unsigned src_y)
168 {
169 struct pipe_transfer *transfer;
170 void *map;
171
172 map = pipe->transfer_map(pipe, dst->texture, 0, PIPE_TRANSFER_WRITE,
173 dst_box, &transfer);
174 if (!map)
175 return;
176
177 util_copy_rect(map, dst->texture->format, transfer->stride, 0, 0,
178 dst_box->width, dst_box->height,
179 src, src_stride, src_x, src_y);
180
181 pipe->transfer_unmap(pipe, transfer);
182 }
183
184 static VAStatus
185 vlVaPutSubpictures(vlVaSurface *surf, vlVaDriver *drv,
186 struct pipe_surface *surf_draw, struct u_rect *dirty_area,
187 struct u_rect *src_rect, struct u_rect *dst_rect)
188 {
189 vlVaSubpicture *sub;
190 int i;
191
192 if (!(surf->subpics.data || surf->subpics.size))
193 return VA_STATUS_SUCCESS;
194
195 for (i = 0; i < surf->subpics.size/sizeof(vlVaSubpicture *); i++) {
196 struct pipe_blend_state blend;
197 void *blend_state;
198 vlVaBuffer *buf;
199 struct pipe_box box;
200 struct u_rect *s, *d, sr, dr, c;
201 int sw, sh, dw, dh;
202
203 sub = ((vlVaSubpicture **)surf->subpics.data)[i];
204 if (!sub)
205 continue;
206
207 buf = handle_table_get(drv->htab, sub->image->buf);
208 if (!buf)
209 return VA_STATUS_ERROR_INVALID_IMAGE;
210
211 box.x = 0;
212 box.y = 0;
213 box.z = 0;
214 box.width = sub->dst_rect.x1 - sub->dst_rect.x0;
215 box.height = sub->dst_rect.y1 - sub->dst_rect.y0;
216 box.depth = 1;
217
218 s = &sub->src_rect;
219 d = &sub->dst_rect;
220 sw = s->x1 - s->x0;
221 sh = s->y1 - s->y0;
222 dw = d->x1 - d->x0;
223 dh = d->y1 - d->y0;
224 c.x0 = MAX2(d->x0, s->x0);
225 c.y0 = MAX2(d->y0, s->y0);
226 c.x1 = MIN2(d->x0 + dw, src_rect->x1);
227 c.y1 = MIN2(d->y0 + dh, src_rect->y1);
228 sr.x0 = s->x0 + (c.x0 - d->x0)*(sw/(float)dw);
229 sr.y0 = s->y0 + (c.y0 - d->y0)*(sh/(float)dh);
230 sr.x1 = s->x0 + (c.x1 - d->x0)*(sw/(float)dw);
231 sr.y1 = s->y0 + (c.y1 - d->y0)*(sh/(float)dh);
232
233 s = src_rect;
234 d = dst_rect;
235 sw = s->x1 - s->x0;
236 sh = s->y1 - s->y0;
237 dw = d->x1 - d->x0;
238 dh = d->y1 - d->y0;
239 dr.x0 = d->x0 + c.x0*(dw/(float)sw);
240 dr.y0 = d->y0 + c.y0*(dh/(float)sh);
241 dr.x1 = d->x0 + c.x1*(dw/(float)sw);
242 dr.y1 = d->y0 + c.y1*(dh/(float)sh);
243
244 memset(&blend, 0, sizeof(blend));
245 blend.independent_blend_enable = 0;
246 blend.rt[0].blend_enable = 1;
247 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
248 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
249 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ZERO;
250 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
251 blend.rt[0].rgb_func = PIPE_BLEND_ADD;
252 blend.rt[0].alpha_func = PIPE_BLEND_ADD;
253 blend.rt[0].colormask = PIPE_MASK_RGBA;
254 blend.logicop_enable = 0;
255 blend.logicop_func = PIPE_LOGICOP_CLEAR;
256 blend.dither = 0;
257 blend_state = drv->pipe->create_blend_state(drv->pipe, &blend);
258
259 vl_compositor_clear_layers(&drv->cstate);
260 vl_compositor_set_layer_blend(&drv->cstate, 0, blend_state, false);
261 upload_sampler(drv->pipe, sub->sampler, &box, buf->data,
262 sub->image->pitches[0], 0, 0);
263 vl_compositor_set_rgba_layer(&drv->cstate, &drv->compositor, 0, sub->sampler,
264 &sr, NULL, NULL);
265 vl_compositor_set_layer_dst_area(&drv->cstate, 0, &dr);
266 vl_compositor_render(&drv->cstate, &drv->compositor, surf_draw, dirty_area, false);
267 drv->pipe->delete_blend_state(drv->pipe, blend_state);
268 }
269
270 return VA_STATUS_SUCCESS;
271 }
272
273 VAStatus
274 vlVaPutSurface(VADriverContextP ctx, VASurfaceID surface_id, void* draw, short srcx, short srcy,
275 unsigned short srcw, unsigned short srch, short destx, short desty,
276 unsigned short destw, unsigned short desth, VARectangle *cliprects,
277 unsigned int number_cliprects, unsigned int flags)
278 {
279 vlVaDriver *drv;
280 vlVaSurface *surf;
281 struct pipe_screen *screen;
282 struct pipe_resource *tex;
283 struct pipe_surface surf_templ, *surf_draw;
284 struct vl_screen *vscreen;
285 struct u_rect src_rect, *dirty_area;
286 struct u_rect dst_rect = {destx, destx + destw, desty, desty + desth};
287 enum pipe_format format;
288 VAStatus status;
289
290 if (!ctx)
291 return VA_STATUS_ERROR_INVALID_CONTEXT;
292
293 drv = VL_VA_DRIVER(ctx);
294 mtx_lock(&drv->mutex);
295 surf = handle_table_get(drv->htab, surface_id);
296 if (!surf) {
297 mtx_unlock(&drv->mutex);
298 return VA_STATUS_ERROR_INVALID_SURFACE;
299 }
300
301 screen = drv->pipe->screen;
302 vscreen = drv->vscreen;
303
304 tex = vscreen->texture_from_drawable(vscreen, draw);
305 if (!tex) {
306 mtx_unlock(&drv->mutex);
307 return VA_STATUS_ERROR_INVALID_DISPLAY;
308 }
309
310 dirty_area = vscreen->get_dirty_area(vscreen);
311
312 memset(&surf_templ, 0, sizeof(surf_templ));
313 surf_templ.format = tex->format;
314 surf_draw = drv->pipe->create_surface(drv->pipe, tex, &surf_templ);
315 if (!surf_draw) {
316 pipe_resource_reference(&tex, NULL);
317 mtx_unlock(&drv->mutex);
318 return VA_STATUS_ERROR_INVALID_DISPLAY;
319 }
320
321 src_rect.x0 = srcx;
322 src_rect.y0 = srcy;
323 src_rect.x1 = srcw + srcx;
324 src_rect.y1 = srch + srcy;
325
326 format = surf->buffer->buffer_format;
327
328 vl_compositor_clear_layers(&drv->cstate);
329
330 if (format == PIPE_FORMAT_B8G8R8A8_UNORM || format == PIPE_FORMAT_B8G8R8X8_UNORM ||
331 format == PIPE_FORMAT_R8G8B8A8_UNORM || format == PIPE_FORMAT_R8G8B8X8_UNORM) {
332 struct pipe_sampler_view **views;
333
334 views = surf->buffer->get_sampler_view_planes(surf->buffer);
335 vl_compositor_set_rgba_layer(&drv->cstate, &drv->compositor, 0, views[0], &src_rect, NULL, NULL);
336 } else
337 vl_compositor_set_buffer_layer(&drv->cstate, &drv->compositor, 0, surf->buffer, &src_rect, NULL, VL_COMPOSITOR_WEAVE);
338
339 vl_compositor_set_layer_dst_area(&drv->cstate, 0, &dst_rect);
340 vl_compositor_render(&drv->cstate, &drv->compositor, surf_draw, dirty_area, true);
341
342 status = vlVaPutSubpictures(surf, drv, surf_draw, dirty_area, &src_rect, &dst_rect);
343 if (status) {
344 mtx_unlock(&drv->mutex);
345 return status;
346 }
347
348 /* flush before calling flush_frontbuffer so that rendering is flushed
349 * to back buffer so the texture can be copied in flush_frontbuffer
350 */
351 drv->pipe->flush(drv->pipe, NULL, 0);
352
353 screen->flush_frontbuffer(screen, tex, 0, 0,
354 vscreen->get_private(vscreen), NULL);
355
356
357 pipe_resource_reference(&tex, NULL);
358 pipe_surface_reference(&surf_draw, NULL);
359 mtx_unlock(&drv->mutex);
360
361 return VA_STATUS_SUCCESS;
362 }
363
364 VAStatus
365 vlVaLockSurface(VADriverContextP ctx, VASurfaceID surface, unsigned int *fourcc,
366 unsigned int *luma_stride, unsigned int *chroma_u_stride, unsigned int *chroma_v_stride,
367 unsigned int *luma_offset, unsigned int *chroma_u_offset, unsigned int *chroma_v_offset,
368 unsigned int *buffer_name, void **buffer)
369 {
370 if (!ctx)
371 return VA_STATUS_ERROR_INVALID_CONTEXT;
372
373 return VA_STATUS_ERROR_UNIMPLEMENTED;
374 }
375
376 VAStatus
377 vlVaUnlockSurface(VADriverContextP ctx, VASurfaceID surface)
378 {
379 if (!ctx)
380 return VA_STATUS_ERROR_INVALID_CONTEXT;
381
382 return VA_STATUS_ERROR_UNIMPLEMENTED;
383 }
384
385 VAStatus
386 vlVaQuerySurfaceAttributes(VADriverContextP ctx, VAConfigID config_id,
387 VASurfaceAttrib *attrib_list, unsigned int *num_attribs)
388 {
389 vlVaDriver *drv;
390 vlVaConfig *config;
391 VASurfaceAttrib *attribs;
392 struct pipe_screen *pscreen;
393 int i, j;
394
395 STATIC_ASSERT(ARRAY_SIZE(vpp_surface_formats) <= VL_VA_MAX_IMAGE_FORMATS);
396
397 if (config_id == VA_INVALID_ID)
398 return VA_STATUS_ERROR_INVALID_CONFIG;
399
400 if (!attrib_list && !num_attribs)
401 return VA_STATUS_ERROR_INVALID_PARAMETER;
402
403 if (!attrib_list) {
404 *num_attribs = VL_VA_MAX_IMAGE_FORMATS + VASurfaceAttribCount;
405 return VA_STATUS_SUCCESS;
406 }
407
408 if (!ctx)
409 return VA_STATUS_ERROR_INVALID_CONTEXT;
410
411 drv = VL_VA_DRIVER(ctx);
412
413 if (!drv)
414 return VA_STATUS_ERROR_INVALID_CONTEXT;
415
416 mtx_lock(&drv->mutex);
417 config = handle_table_get(drv->htab, config_id);
418 mtx_unlock(&drv->mutex);
419
420 if (!config)
421 return VA_STATUS_ERROR_INVALID_CONFIG;
422
423 pscreen = VL_VA_PSCREEN(ctx);
424
425 if (!pscreen)
426 return VA_STATUS_ERROR_INVALID_CONTEXT;
427
428 attribs = CALLOC(VL_VA_MAX_IMAGE_FORMATS + VASurfaceAttribCount,
429 sizeof(VASurfaceAttrib));
430
431 if (!attribs)
432 return VA_STATUS_ERROR_ALLOCATION_FAILED;
433
434 i = 0;
435
436 /* vlVaCreateConfig returns PIPE_VIDEO_PROFILE_UNKNOWN
437 * only for VAEntrypointVideoProc. */
438 if (config->profile == PIPE_VIDEO_PROFILE_UNKNOWN) {
439 if (config->rt_format & VA_RT_FORMAT_RGB32) {
440 for (j = 0; j < ARRAY_SIZE(vpp_surface_formats); ++j) {
441 attribs[i].type = VASurfaceAttribPixelFormat;
442 attribs[i].value.type = VAGenericValueTypeInteger;
443 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
444 attribs[i].value.value.i = PipeFormatToVaFourcc(vpp_surface_formats[j]);
445 i++;
446 }
447 }
448 }
449 if (config->rt_format & VA_RT_FORMAT_YUV420) {
450 attribs[i].type = VASurfaceAttribPixelFormat;
451 attribs[i].value.type = VAGenericValueTypeInteger;
452 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
453 attribs[i].value.value.i = VA_FOURCC_NV12;
454 i++;
455 }
456 if (config->rt_format & VA_RT_FORMAT_YUV420_10BPP) {
457 attribs[i].type = VASurfaceAttribPixelFormat;
458 attribs[i].value.type = VAGenericValueTypeInteger;
459 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
460 attribs[i].value.value.i = VA_FOURCC_P010;
461 i++;
462 attribs[i].type = VASurfaceAttribPixelFormat;
463 attribs[i].value.type = VAGenericValueTypeInteger;
464 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
465 attribs[i].value.value.i = VA_FOURCC_P016;
466 i++;
467 }
468
469 attribs[i].type = VASurfaceAttribMemoryType;
470 attribs[i].value.type = VAGenericValueTypeInteger;
471 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
472 attribs[i].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA |
473 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
474 i++;
475
476 attribs[i].type = VASurfaceAttribExternalBufferDescriptor;
477 attribs[i].value.type = VAGenericValueTypePointer;
478 attribs[i].flags = VA_SURFACE_ATTRIB_SETTABLE;
479 attribs[i].value.value.p = NULL; /* ignore */
480 i++;
481
482 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_UNKNOWN) {
483 attribs[i].type = VASurfaceAttribMaxWidth;
484 attribs[i].value.type = VAGenericValueTypeInteger;
485 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
486 attribs[i].value.value.i =
487 pscreen->get_video_param(pscreen,
488 config->profile, config->entrypoint,
489 PIPE_VIDEO_CAP_MAX_WIDTH);
490 i++;
491
492 attribs[i].type = VASurfaceAttribMaxHeight;
493 attribs[i].value.type = VAGenericValueTypeInteger;
494 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
495 attribs[i].value.value.i =
496 pscreen->get_video_param(pscreen,
497 config->profile, config->entrypoint,
498 PIPE_VIDEO_CAP_MAX_HEIGHT);
499 i++;
500 } else {
501 attribs[i].type = VASurfaceAttribMaxWidth;
502 attribs[i].value.type = VAGenericValueTypeInteger;
503 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
504 attribs[i].value.value.i = vl_video_buffer_max_size(pscreen);
505 i++;
506
507 attribs[i].type = VASurfaceAttribMaxHeight;
508 attribs[i].value.type = VAGenericValueTypeInteger;
509 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
510 attribs[i].value.value.i = vl_video_buffer_max_size(pscreen);
511 i++;
512 }
513
514 if (i > *num_attribs) {
515 *num_attribs = i;
516 FREE(attribs);
517 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
518 }
519
520 *num_attribs = i;
521 memcpy(attrib_list, attribs, i * sizeof(VASurfaceAttrib));
522 FREE(attribs);
523
524 return VA_STATUS_SUCCESS;
525 }
526
527 static VAStatus
528 surface_from_external_memory(VADriverContextP ctx, vlVaSurface *surface,
529 VASurfaceAttribExternalBuffers *memory_attribute,
530 unsigned index, struct pipe_video_buffer *templat)
531 {
532 vlVaDriver *drv;
533 struct pipe_screen *pscreen;
534 struct pipe_resource res_templ;
535 struct winsys_handle whandle;
536 struct pipe_resource *resources[VL_NUM_COMPONENTS];
537 const enum pipe_format *resource_formats = NULL;
538 VAStatus result;
539 int i;
540
541 pscreen = VL_VA_PSCREEN(ctx);
542 drv = VL_VA_DRIVER(ctx);
543
544 if (!memory_attribute || !memory_attribute->buffers ||
545 index > memory_attribute->num_buffers)
546 return VA_STATUS_ERROR_INVALID_PARAMETER;
547
548 if (surface->templat.width != memory_attribute->width ||
549 surface->templat.height != memory_attribute->height ||
550 memory_attribute->num_planes < 1)
551 return VA_STATUS_ERROR_INVALID_PARAMETER;
552
553 if (memory_attribute->num_planes > VL_NUM_COMPONENTS)
554 return VA_STATUS_ERROR_INVALID_PARAMETER;
555
556 resource_formats = vl_video_buffer_formats(pscreen, templat->buffer_format);
557 if (!resource_formats)
558 return VA_STATUS_ERROR_INVALID_PARAMETER;
559
560 memset(&res_templ, 0, sizeof(res_templ));
561 res_templ.target = PIPE_TEXTURE_2D;
562 res_templ.last_level = 0;
563 res_templ.depth0 = 1;
564 res_templ.array_size = 1;
565 res_templ.width0 = memory_attribute->width;
566 res_templ.height0 = memory_attribute->height;
567 res_templ.bind = PIPE_BIND_SAMPLER_VIEW;
568 res_templ.usage = PIPE_USAGE_DEFAULT;
569
570 memset(&whandle, 0, sizeof(struct winsys_handle));
571 whandle.type = WINSYS_HANDLE_TYPE_FD;
572 whandle.handle = memory_attribute->buffers[index];
573
574 // Create a resource for each plane.
575 memset(resources, 0, sizeof resources);
576 for (i = 0; i < memory_attribute->num_planes; i++) {
577 res_templ.format = resource_formats[i];
578 if (res_templ.format == PIPE_FORMAT_NONE) {
579 result = VA_STATUS_ERROR_INVALID_PARAMETER;
580 goto fail;
581 }
582
583 whandle.stride = memory_attribute->pitches[i];
584 whandle.offset = memory_attribute->offsets[i];
585 resources[i] = pscreen->resource_from_handle(pscreen, &res_templ, &whandle,
586 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
587 if (!resources[i]) {
588 result = VA_STATUS_ERROR_ALLOCATION_FAILED;
589 goto fail;
590 }
591 }
592
593 surface->buffer = vl_video_buffer_create_ex2(drv->pipe, templat, resources);
594 if (!surface->buffer) {
595 result = VA_STATUS_ERROR_ALLOCATION_FAILED;
596 goto fail;
597 }
598 return VA_STATUS_SUCCESS;
599
600 fail:
601 for (i = 0; i < VL_NUM_COMPONENTS; i++) {
602 if (resources[i])
603 pscreen->resource_destroy(pscreen, resources[i]);
604 }
605 return result;
606 }
607
608 VAStatus
609 vlVaHandleSurfaceAllocate(vlVaDriver *drv, vlVaSurface *surface,
610 struct pipe_video_buffer *templat)
611 {
612 struct pipe_surface **surfaces;
613 unsigned i;
614
615 surface->buffer = drv->pipe->create_video_buffer(drv->pipe, templat);
616 if (!surface->buffer)
617 return VA_STATUS_ERROR_ALLOCATION_FAILED;
618
619 surfaces = surface->buffer->get_surfaces(surface->buffer);
620 for (i = 0; i < VL_MAX_SURFACES; ++i) {
621 union pipe_color_union c = {};
622
623 if (!surfaces[i])
624 continue;
625
626 if (i > !!surface->buffer->interlaced)
627 c.f[0] = c.f[1] = c.f[2] = c.f[3] = 0.5f;
628
629 drv->pipe->clear_render_target(drv->pipe, surfaces[i], &c, 0, 0,
630 surfaces[i]->width, surfaces[i]->height,
631 false);
632 }
633 drv->pipe->flush(drv->pipe, NULL, 0);
634
635 return VA_STATUS_SUCCESS;
636 }
637
638 VAStatus
639 vlVaCreateSurfaces2(VADriverContextP ctx, unsigned int format,
640 unsigned int width, unsigned int height,
641 VASurfaceID *surfaces, unsigned int num_surfaces,
642 VASurfaceAttrib *attrib_list, unsigned int num_attribs)
643 {
644 vlVaDriver *drv;
645 VASurfaceAttribExternalBuffers *memory_attribute;
646 struct pipe_video_buffer templat;
647 struct pipe_screen *pscreen;
648 int i;
649 int memory_type;
650 int expected_fourcc;
651 VAStatus vaStatus;
652 vlVaSurface *surf;
653
654 if (!ctx)
655 return VA_STATUS_ERROR_INVALID_CONTEXT;
656
657 if (!(width && height))
658 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
659
660 drv = VL_VA_DRIVER(ctx);
661
662 if (!drv)
663 return VA_STATUS_ERROR_INVALID_CONTEXT;
664
665 pscreen = VL_VA_PSCREEN(ctx);
666
667 if (!pscreen)
668 return VA_STATUS_ERROR_INVALID_CONTEXT;
669
670 /* Default. */
671 memory_attribute = NULL;
672 memory_type = VA_SURFACE_ATTRIB_MEM_TYPE_VA;
673 expected_fourcc = 0;
674
675 for (i = 0; i < num_attribs && attrib_list; i++) {
676 if ((attrib_list[i].type == VASurfaceAttribPixelFormat) &&
677 (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
678 if (attrib_list[i].value.type != VAGenericValueTypeInteger)
679 return VA_STATUS_ERROR_INVALID_PARAMETER;
680 expected_fourcc = attrib_list[i].value.value.i;
681 }
682
683 if ((attrib_list[i].type == VASurfaceAttribMemoryType) &&
684 (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
685
686 if (attrib_list[i].value.type != VAGenericValueTypeInteger)
687 return VA_STATUS_ERROR_INVALID_PARAMETER;
688
689 switch (attrib_list[i].value.value.i) {
690 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
691 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
692 memory_type = attrib_list[i].value.value.i;
693 break;
694 default:
695 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
696 }
697 }
698
699 if ((attrib_list[i].type == VASurfaceAttribExternalBufferDescriptor) &&
700 (attrib_list[i].flags == VA_SURFACE_ATTRIB_SETTABLE)) {
701 if (attrib_list[i].value.type != VAGenericValueTypePointer)
702 return VA_STATUS_ERROR_INVALID_PARAMETER;
703 memory_attribute = (VASurfaceAttribExternalBuffers *)attrib_list[i].value.value.p;
704 }
705 }
706
707 if (VA_RT_FORMAT_YUV420 != format &&
708 VA_RT_FORMAT_YUV422 != format &&
709 VA_RT_FORMAT_YUV444 != format &&
710 VA_RT_FORMAT_YUV420_10BPP != format &&
711 VA_RT_FORMAT_RGB32 != format) {
712 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
713 }
714
715 switch (memory_type) {
716 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
717 break;
718 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
719 if (!memory_attribute)
720 return VA_STATUS_ERROR_INVALID_PARAMETER;
721
722 expected_fourcc = memory_attribute->pixel_format;
723 break;
724 default:
725 assert(0);
726 }
727
728 memset(&templat, 0, sizeof(templat));
729
730 templat.buffer_format = pscreen->get_video_param(
731 pscreen,
732 PIPE_VIDEO_PROFILE_UNKNOWN,
733 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
734 PIPE_VIDEO_CAP_PREFERED_FORMAT
735 );
736 templat.interlaced = pscreen->get_video_param(
737 pscreen,
738 PIPE_VIDEO_PROFILE_UNKNOWN,
739 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
740 PIPE_VIDEO_CAP_PREFERS_INTERLACED
741 );
742
743 if (expected_fourcc) {
744 enum pipe_format expected_format = VaFourccToPipeFormat(expected_fourcc);
745
746 if (expected_format != templat.buffer_format || memory_attribute)
747 templat.interlaced = 0;
748
749 templat.buffer_format = expected_format;
750 }
751
752 templat.chroma_format = ChromaToPipe(format);
753
754 templat.width = width;
755 templat.height = height;
756
757 memset(surfaces, VA_INVALID_ID, num_surfaces * sizeof(VASurfaceID));
758
759 mtx_lock(&drv->mutex);
760 for (i = 0; i < num_surfaces; i++) {
761 surf = CALLOC(1, sizeof(vlVaSurface));
762 if (!surf) {
763 vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
764 goto no_res;
765 }
766
767 surf->templat = templat;
768
769 switch (memory_type) {
770 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
771 /* The application will clear the TILING flag when the surface is
772 * intended to be exported as dmabuf. Adding shared flag because not
773 * null memory_attribute means VASurfaceAttribExternalBuffers is used.
774 */
775 if (memory_attribute &&
776 !(memory_attribute->flags & VA_SURFACE_EXTBUF_DESC_ENABLE_TILING))
777 templat.bind = PIPE_BIND_LINEAR | PIPE_BIND_SHARED;
778
779 vaStatus = vlVaHandleSurfaceAllocate(drv, surf, &templat);
780 if (vaStatus != VA_STATUS_SUCCESS)
781 goto free_surf;
782 break;
783
784 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
785 vaStatus = surface_from_external_memory(ctx, surf, memory_attribute, i, &templat);
786 if (vaStatus != VA_STATUS_SUCCESS)
787 goto free_surf;
788 break;
789
790 default:
791 assert(0);
792 }
793
794 util_dynarray_init(&surf->subpics, NULL);
795 surfaces[i] = handle_table_add(drv->htab, surf);
796 if (!surfaces[i]) {
797 vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
798 goto destroy_surf;
799 }
800 }
801 mtx_unlock(&drv->mutex);
802
803 return VA_STATUS_SUCCESS;
804
805 destroy_surf:
806 surf->buffer->destroy(surf->buffer);
807
808 free_surf:
809 FREE(surf);
810
811 no_res:
812 mtx_unlock(&drv->mutex);
813 if (i)
814 vlVaDestroySurfaces(ctx, surfaces, i);
815
816 return vaStatus;
817 }
818
819 VAStatus
820 vlVaQueryVideoProcFilters(VADriverContextP ctx, VAContextID context,
821 VAProcFilterType *filters, unsigned int *num_filters)
822 {
823 unsigned int num = 0;
824
825 if (!ctx)
826 return VA_STATUS_ERROR_INVALID_CONTEXT;
827
828 if (!num_filters || !filters)
829 return VA_STATUS_ERROR_INVALID_PARAMETER;
830
831 filters[num++] = VAProcFilterDeinterlacing;
832
833 *num_filters = num;
834
835 return VA_STATUS_SUCCESS;
836 }
837
838 VAStatus
839 vlVaQueryVideoProcFilterCaps(VADriverContextP ctx, VAContextID context,
840 VAProcFilterType type, void *filter_caps,
841 unsigned int *num_filter_caps)
842 {
843 unsigned int i;
844
845 if (!ctx)
846 return VA_STATUS_ERROR_INVALID_CONTEXT;
847
848 if (!filter_caps || !num_filter_caps)
849 return VA_STATUS_ERROR_INVALID_PARAMETER;
850
851 i = 0;
852
853 switch (type) {
854 case VAProcFilterNone:
855 break;
856 case VAProcFilterDeinterlacing: {
857 VAProcFilterCapDeinterlacing *deint = filter_caps;
858
859 if (*num_filter_caps < 3) {
860 *num_filter_caps = 3;
861 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
862 }
863
864 deint[i++].type = VAProcDeinterlacingBob;
865 deint[i++].type = VAProcDeinterlacingWeave;
866 deint[i++].type = VAProcDeinterlacingMotionAdaptive;
867 break;
868 }
869
870 case VAProcFilterNoiseReduction:
871 case VAProcFilterSharpening:
872 case VAProcFilterColorBalance:
873 case VAProcFilterSkinToneEnhancement:
874 return VA_STATUS_ERROR_UNIMPLEMENTED;
875 default:
876 assert(0);
877 }
878
879 *num_filter_caps = i;
880
881 return VA_STATUS_SUCCESS;
882 }
883
884 static VAProcColorStandardType vpp_input_color_standards[] = {
885 VAProcColorStandardBT601
886 };
887
888 static VAProcColorStandardType vpp_output_color_standards[] = {
889 VAProcColorStandardBT601
890 };
891
892 VAStatus
893 vlVaQueryVideoProcPipelineCaps(VADriverContextP ctx, VAContextID context,
894 VABufferID *filters, unsigned int num_filters,
895 VAProcPipelineCaps *pipeline_cap)
896 {
897 unsigned int i = 0;
898
899 if (!ctx)
900 return VA_STATUS_ERROR_INVALID_CONTEXT;
901
902 if (!pipeline_cap)
903 return VA_STATUS_ERROR_INVALID_PARAMETER;
904
905 if (num_filters && !filters)
906 return VA_STATUS_ERROR_INVALID_PARAMETER;
907
908 pipeline_cap->pipeline_flags = 0;
909 pipeline_cap->filter_flags = 0;
910 pipeline_cap->num_forward_references = 0;
911 pipeline_cap->num_backward_references = 0;
912 pipeline_cap->num_input_color_standards = ARRAY_SIZE(vpp_input_color_standards);
913 pipeline_cap->input_color_standards = vpp_input_color_standards;
914 pipeline_cap->num_output_color_standards = ARRAY_SIZE(vpp_output_color_standards);
915 pipeline_cap->output_color_standards = vpp_output_color_standards;
916
917 for (i = 0; i < num_filters; i++) {
918 vlVaBuffer *buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, filters[i]);
919 VAProcFilterParameterBufferBase *filter;
920
921 if (!buf || buf->type != VAProcFilterParameterBufferType)
922 return VA_STATUS_ERROR_INVALID_BUFFER;
923
924 filter = buf->data;
925 switch (filter->type) {
926 case VAProcFilterDeinterlacing: {
927 VAProcFilterParameterBufferDeinterlacing *deint = buf->data;
928 if (deint->algorithm == VAProcDeinterlacingMotionAdaptive) {
929 pipeline_cap->num_forward_references = 2;
930 pipeline_cap->num_backward_references = 1;
931 }
932 break;
933 }
934 default:
935 return VA_STATUS_ERROR_UNIMPLEMENTED;
936 }
937 }
938
939 return VA_STATUS_SUCCESS;
940 }
941
942 #if VA_CHECK_VERSION(1, 1, 0)
943 VAStatus
944 vlVaExportSurfaceHandle(VADriverContextP ctx,
945 VASurfaceID surface_id,
946 uint32_t mem_type,
947 uint32_t flags,
948 void *descriptor)
949 {
950 vlVaDriver *drv;
951 vlVaSurface *surf;
952 struct pipe_surface **surfaces;
953 struct pipe_screen *screen;
954 VAStatus ret;
955 unsigned int usage;
956 int i, p;
957
958 VADRMPRIMESurfaceDescriptor *desc = descriptor;
959
960 if (mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2)
961 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
962 if (flags & VA_EXPORT_SURFACE_COMPOSED_LAYERS)
963 return VA_STATUS_ERROR_INVALID_SURFACE;
964
965 drv = VL_VA_DRIVER(ctx);
966 screen = VL_VA_PSCREEN(ctx);
967 mtx_lock(&drv->mutex);
968
969 surf = handle_table_get(drv->htab, surface_id);
970 if (!surf || !surf->buffer) {
971 mtx_unlock(&drv->mutex);
972 return VA_STATUS_ERROR_INVALID_SURFACE;
973 }
974
975 if (surf->buffer->interlaced) {
976 struct pipe_video_buffer *interlaced = surf->buffer;
977 struct u_rect src_rect, dst_rect;
978
979 surf->templat.interlaced = false;
980
981 ret = vlVaHandleSurfaceAllocate(drv, surf, &surf->templat);
982 if (ret != VA_STATUS_SUCCESS) {
983 mtx_unlock(&drv->mutex);
984 return VA_STATUS_ERROR_ALLOCATION_FAILED;
985 }
986
987 src_rect.x0 = dst_rect.x0 = 0;
988 src_rect.y0 = dst_rect.y0 = 0;
989 src_rect.x1 = dst_rect.x1 = surf->templat.width;
990 src_rect.y1 = dst_rect.y1 = surf->templat.height;
991
992 vl_compositor_yuv_deint_full(&drv->cstate, &drv->compositor,
993 interlaced, surf->buffer,
994 &src_rect, &dst_rect,
995 VL_COMPOSITOR_WEAVE);
996
997 interlaced->destroy(interlaced);
998 }
999
1000 surfaces = surf->buffer->get_surfaces(surf->buffer);
1001
1002 usage = 0;
1003 if (flags & VA_EXPORT_SURFACE_WRITE_ONLY)
1004 usage |= PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
1005
1006 desc->fourcc = PipeFormatToVaFourcc(surf->buffer->buffer_format);
1007 desc->width = surf->buffer->width;
1008 desc->height = surf->buffer->height;
1009
1010 for (p = 0; p < VL_MAX_SURFACES; p++) {
1011 struct winsys_handle whandle;
1012 struct pipe_resource *resource;
1013 uint32_t drm_format;
1014
1015 if (!surfaces[p])
1016 break;
1017
1018 resource = surfaces[p]->texture;
1019
1020 switch (resource->format) {
1021 case PIPE_FORMAT_R8_UNORM:
1022 drm_format = DRM_FORMAT_R8;
1023 break;
1024 case PIPE_FORMAT_R8G8_UNORM:
1025 drm_format = DRM_FORMAT_GR88;
1026 break;
1027 case PIPE_FORMAT_R16_UNORM:
1028 drm_format = DRM_FORMAT_R16;
1029 break;
1030 case PIPE_FORMAT_R16G16_UNORM:
1031 drm_format = DRM_FORMAT_GR1616;
1032 break;
1033 case PIPE_FORMAT_B8G8R8A8_UNORM:
1034 drm_format = DRM_FORMAT_ARGB8888;
1035 break;
1036 case PIPE_FORMAT_R8G8B8A8_UNORM:
1037 drm_format = DRM_FORMAT_ABGR8888;
1038 break;
1039 case PIPE_FORMAT_B8G8R8X8_UNORM:
1040 drm_format = DRM_FORMAT_XRGB8888;
1041 break;
1042 case PIPE_FORMAT_R8G8B8X8_UNORM:
1043 drm_format = DRM_FORMAT_XBGR8888;
1044 break;
1045 default:
1046 ret = VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
1047 goto fail;
1048 }
1049
1050 memset(&whandle, 0, sizeof(whandle));
1051 whandle.type = WINSYS_HANDLE_TYPE_FD;
1052
1053 if (!screen->resource_get_handle(screen, drv->pipe, resource,
1054 &whandle, usage)) {
1055 ret = VA_STATUS_ERROR_INVALID_SURFACE;
1056 goto fail;
1057 }
1058
1059 desc->objects[p].fd = (int)whandle.handle;
1060 desc->objects[p].size = 0;
1061 desc->objects[p].drm_format_modifier = whandle.modifier;
1062
1063 desc->layers[p].drm_format = drm_format;
1064 desc->layers[p].num_planes = 1;
1065 desc->layers[p].object_index[0] = p;
1066 desc->layers[p].offset[0] = whandle.offset;
1067 desc->layers[p].pitch[0] = whandle.stride;
1068 }
1069
1070 desc->num_objects = p;
1071 desc->num_layers = p;
1072
1073 mtx_unlock(&drv->mutex);
1074
1075 return VA_STATUS_SUCCESS;
1076
1077 fail:
1078 for (i = 0; i < p; i++)
1079 close(desc->objects[i].fd);
1080
1081 mtx_unlock(&drv->mutex);
1082
1083 return ret;
1084 }
1085 #endif