tu: Implement fallback linear staging blit for CopyImage
[mesa.git] / src / gallium / state_trackers / va / postproc.c
index 0cec0c88124eb6b7476359831ee3f1cf43297636..1845883284468747cfe6132805e1df6d17b7c037 100644 (file)
  **************************************************************************/
 
 #include "util/u_handle_table.h"
+#include "util/u_memory.h"
+#include "util/u_compute.h"
 
 #include "vl/vl_defines.h"
 #include "vl/vl_video_buffer.h"
+#include "vl/vl_deint_filter.h"
 
 #include "va_private.h"
 
 static const VARectangle *
-vlVaRegionDefault(const VARectangle *region, struct pipe_video_buffer *buf,
+vlVaRegionDefault(const VARectangle *region, vlVaSurface *surf,
                  VARectangle *def)
 {
    if (region)
@@ -41,8 +44,8 @@ vlVaRegionDefault(const VARectangle *region, struct pipe_video_buffer *buf,
 
    def->x = 0;
    def->y = 0;
-   def->width = buf->width;
-   def->height = buf->height;
+   def->width = surf->templat.width;
+   def->height = surf->templat.height;
 
    return def;
 }
@@ -79,6 +82,7 @@ vlVaPostProcCompositor(vlVaDriver *drv, vlVaContext *context,
    vl_compositor_set_layer_dst_area(&drv->cstate, 0, &dst_rect);
    vl_compositor_render(&drv->cstate, &drv->compositor, surfaces[0], NULL, false);
 
+   drv->pipe->flush(drv->pipe, NULL, 0);
    return VA_STATUS_SUCCESS;
 }
 
@@ -93,9 +97,11 @@ static void vlVaGetBox(struct pipe_video_buffer *buf, unsigned idx,
    width = region->width;
    height = region->height;
 
-   vl_video_buffer_adjust_size(&x, &y, plane, buf->chroma_format,
+   vl_video_buffer_adjust_size(&x, &y, plane,
+                               pipe_format_to_chroma_format(buf->buffer_format),
                                buf->interlaced);
-   vl_video_buffer_adjust_size(&width, &height, plane, buf->chroma_format,
+   vl_video_buffer_adjust_size(&width, &height, plane,
+                               pipe_format_to_chroma_format(buf->buffer_format),
                                buf->interlaced);
 
    box->x = region->x < 0 ? -x : x;
@@ -113,19 +119,68 @@ static VAStatus vlVaPostProcBlit(vlVaDriver *drv, vlVaContext *context,
 {
    struct pipe_surface **src_surfaces;
    struct pipe_surface **dst_surfaces;
+   struct u_rect src_rect;
+   struct u_rect dst_rect;
+   bool scale = false;
+   bool grab = false;
    unsigned i;
 
-   if (src->interlaced != dst->interlaced)
-      return VA_STATUS_ERROR_INVALID_SURFACE;
+   if ((src->buffer_format == PIPE_FORMAT_B8G8R8A8_UNORM ||
+        src->buffer_format == PIPE_FORMAT_B8G8R8X8_UNORM) &&
+       !src->interlaced)
+      grab = true;
+
+   if ((src->width != dst->width || src->height != dst->height) &&
+       (src->interlaced && dst->interlaced))
+      scale = true;
 
    src_surfaces = src->get_surfaces(src);
    if (!src_surfaces || !src_surfaces[0])
       return VA_STATUS_ERROR_INVALID_SURFACE;
 
+   if (scale || (src->interlaced != dst->interlaced && dst->interlaced)) {
+      vlVaSurface *surf;
+
+      surf = handle_table_get(drv->htab, context->target_id);
+      surf->templat.interlaced = false;
+      dst->destroy(dst);
+
+      if (vlVaHandleSurfaceAllocate(drv, surf, &surf->templat) != VA_STATUS_SUCCESS)
+         return VA_STATUS_ERROR_ALLOCATION_FAILED;
+
+      dst = context->target = surf->buffer;
+   }
+
    dst_surfaces = dst->get_surfaces(dst);
    if (!dst_surfaces || !dst_surfaces[0])
       return VA_STATUS_ERROR_INVALID_SURFACE;
 
+   src_rect.x0 = src_region->x;
+   src_rect.y0 = src_region->y;
+   src_rect.x1 = src_region->x + src_region->width;
+   src_rect.y1 = src_region->y + src_region->height;
+
+   dst_rect.x0 = dst_region->x;
+   dst_rect.y0 = dst_region->y;
+   dst_rect.x1 = dst_region->x + dst_region->width;
+   dst_rect.y1 = dst_region->y + dst_region->height;
+
+   if (grab) {
+      vl_compositor_convert_rgb_to_yuv(&drv->cstate, &drv->compositor, 0,
+                                       ((struct vl_video_buffer *)src)->resources[0],
+                                       dst, &src_rect, &dst_rect);
+
+      return VA_STATUS_SUCCESS;
+   }
+
+   if (src->interlaced != dst->interlaced) {
+      vl_compositor_yuv_deint_full(&drv->cstate, &drv->compositor,
+                                   src, dst, &src_rect, &dst_rect,
+                                   deinterlace);
+
+      return VA_STATUS_SUCCESS;
+   }
+
    for (i = 0; i < VL_MAX_SURFACES; ++i) {
       struct pipe_surface *from = src_surfaces[i];
       struct pipe_blit_info blit;
@@ -165,7 +220,11 @@ static VAStatus vlVaPostProcBlit(vlVaDriver *drv, vlVaContext *context,
       blit.mask = PIPE_MASK_RGBA;
       blit.filter = PIPE_TEX_MIPFILTER_LINEAR;
 
-      drv->pipe->blit(drv->pipe, &blit);
+      if (drv->pipe->screen->get_param(drv->pipe->screen,
+                                       PIPE_CAP_PREFER_COMPUTE_FOR_MULTIMEDIA))
+         util_compute_blit(drv->pipe, &blit, &context->blit_cs);
+      else
+         drv->pipe->blit(drv->pipe, &blit);
    }
 
    // TODO: figure out why this is necessary for DMA-buf sharing
@@ -174,6 +233,51 @@ static VAStatus vlVaPostProcBlit(vlVaDriver *drv, vlVaContext *context,
    return VA_STATUS_SUCCESS;
 }
 
+static struct pipe_video_buffer *
+vlVaApplyDeint(vlVaDriver *drv, vlVaContext *context,
+               VAProcPipelineParameterBuffer *param,
+               struct pipe_video_buffer *current,
+               unsigned field)
+{
+   vlVaSurface *prevprev, *prev, *next;
+
+   if (param->num_forward_references < 2 ||
+       param->num_backward_references < 1)
+      return current;
+
+   prevprev = handle_table_get(drv->htab, param->forward_references[1]);
+   prev = handle_table_get(drv->htab, param->forward_references[0]);
+   next = handle_table_get(drv->htab, param->backward_references[0]);
+
+   if (!prevprev || !prev || !next)
+      return current;
+
+   if (context->deint && (context->deint->video_width != current->width ||
+       context->deint->video_height != current->height)) {
+      vl_deint_filter_cleanup(context->deint);
+      FREE(context->deint);
+      context->deint = NULL;
+   }
+
+   if (!context->deint) {
+      context->deint = MALLOC(sizeof(struct vl_deint_filter));
+      if (!vl_deint_filter_init(context->deint, drv->pipe, current->width,
+                                current->height, false, false)) {
+         FREE(context->deint);
+         context->deint = NULL;
+         return current;
+      }
+   }
+
+   if (!vl_deint_filter_check_buffers(context->deint, prevprev->buffer,
+                                      prev->buffer, current, next->buffer))
+      return current;
+
+   vl_deint_filter_render(context->deint, prevprev->buffer, prev->buffer,
+                          current, next->buffer, field);
+   return context->deint->video_buffer;
+}
+
 VAStatus
 vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
 {
@@ -181,7 +285,8 @@ vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver *drv, vlVaContext *contex
    VARectangle def_src_region, def_dst_region;
    const VARectangle *src_region, *dst_region;
    VAProcPipelineParameterBuffer *param;
-   vlVaSurface *src_surface;
+   struct pipe_video_buffer *src;
+   vlVaSurface *src_surface, *dst_surface;
    unsigned i;
 
    if (!drv || !context)
@@ -196,9 +301,13 @@ vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver *drv, vlVaContext *contex
    param = buf->data;
 
    src_surface = handle_table_get(drv->htab, param->surface);
+   dst_surface = handle_table_get(drv->htab, context->target_id);
+
    if (!src_surface || !src_surface->buffer)
       return VA_STATUS_ERROR_INVALID_SURFACE;
 
+   src = src_surface->buffer;
+
    for (i = 0; i < param->num_filters; i++) {
       vlVaBuffer *buf = handle_table_get(drv->htab, param->filters[i]);
       VAProcFilterParameterBufferBase *filter;
@@ -222,6 +331,11 @@ vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver *drv, vlVaContext *contex
             deinterlace = VL_COMPOSITOR_WEAVE;
             break;
 
+         case VAProcDeinterlacingMotionAdaptive:
+            src = vlVaApplyDeint(drv, context, param, src,
+                                !!(deint->flags & VA_DEINTERLACING_BOTTOM_FIELD));
+            break;
+
          default:
             return VA_STATUS_ERROR_UNIMPLEMENTED;
          }
@@ -234,15 +348,15 @@ vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver *drv, vlVaContext *contex
       }
    }
 
-   src_region = vlVaRegionDefault(param->surface_region, src_surface->buffer, &def_src_region);
-   dst_region = vlVaRegionDefault(param->output_region, context->target, &def_dst_region);
+   src_region = vlVaRegionDefault(param->surface_region, src_surface, &def_src_region);
+   dst_region = vlVaRegionDefault(param->output_region, dst_surface, &def_dst_region);
 
-   if (context->target->buffer_format != PIPE_FORMAT_NV12)
+   if (context->target->buffer_format != PIPE_FORMAT_NV12 &&
+       context->target->buffer_format != PIPE_FORMAT_P010 &&
+       context->target->buffer_format != PIPE_FORMAT_P016)
       return vlVaPostProcCompositor(drv, context, src_region, dst_region,
-                                    src_surface->buffer, context->target,
-                                    deinterlace);
+                                    src, context->target, deinterlace);
    else
       return vlVaPostProcBlit(drv, context, src_region, dst_region,
-                              src_surface->buffer, context->target,
-                              deinterlace);
+                              src, context->target, deinterlace);
 }