st/va: implement Context Surface and Buffer
authorChristian König <christian.koenig@amd.com>
Fri, 4 Jul 2014 16:44:36 +0000 (12:44 -0400)
committerLeo Liu <leo.liu@amd.com>
Wed, 1 Oct 2014 17:21:36 +0000 (13:21 -0400)
This patch implements context managements, relate it HW driver,
functions for video surface managements, and functions for
application data memory buffer managements.

implemented functions:
vlVa(Create|Destroy)Context
vlVa(Create|Destroy|Put)Surfaces
vlVa(Create|Destroy)Buffer

Signed-off-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Leo Liu <leo.liu@amd.com>
src/gallium/state_trackers/va/buffer.c
src/gallium/state_trackers/va/context.c
src/gallium/state_trackers/va/surface.c
src/gallium/state_trackers/va/va_private.h

index 0378f60afe84337c2386bb49c906fadb02c37b59..4023c3276294218b2933315d23efe5b457d7cfb3 100644 (file)
@@ -26,6 +26,9 @@
  *
  **************************************************************************/
 
+#include "util/u_memory.h"
+#include "util/u_handle_table.h"
+
 #include "va_private.h"
 
 VAStatus
@@ -33,55 +36,121 @@ vlVaCreateBuffer(VADriverContextP ctx, VAContextID context, VABufferType type,
                  unsigned int size, unsigned int num_elements, void *data,
                  VABufferID *buf_id)
 {
+   vlVaBuffer *buf;
+
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   buf = CALLOC(1, sizeof(vlVaBuffer));
+   if (!buf)
+      return VA_STATUS_ERROR_ALLOCATION_FAILED;
+
+   buf->type = type;
+   buf->size = size;
+   buf->num_elements = num_elements;
+   buf->data = MALLOC(size * num_elements);
+
+   if (!buf->data) {
+      FREE(buf);
+      return VA_STATUS_ERROR_ALLOCATION_FAILED;
+   }
+
+   if (data)
+      memcpy(buf->data, data, size * num_elements);
+
+   *buf_id = handle_table_add(VL_VA_DRIVER(ctx)->htab, buf);
+
+   return VA_STATUS_SUCCESS;
 }
 
 VAStatus
 vlVaBufferSetNumElements(VADriverContextP ctx, VABufferID buf_id,
                          unsigned int num_elements)
 {
+   vlVaBuffer *buf;
+
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
+   buf->data = REALLOC(buf->data, buf->size * buf->num_elements,
+                       buf->size * num_elements);
+   buf->num_elements = num_elements;
+
+   if (!buf->data)
+      return VA_STATUS_ERROR_ALLOCATION_FAILED;
+
+   return VA_STATUS_SUCCESS;
 }
 
 VAStatus
 vlVaMapBuffer(VADriverContextP ctx, VABufferID buf_id, void **pbuff)
 {
+   vlVaBuffer *buf;
+
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
+   if (!buf)
+      return VA_STATUS_ERROR_INVALID_BUFFER;
+
+   *pbuff = buf->data;
+
+   return VA_STATUS_SUCCESS;
 }
 
 VAStatus
 vlVaUnmapBuffer(VADriverContextP ctx, VABufferID buf_id)
 {
+   vlVaBuffer *buf;
+
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
+   if (!buf)
+      return VA_STATUS_ERROR_INVALID_BUFFER;
+
+   /* Nothing to do here */
+
+   return VA_STATUS_SUCCESS;
 }
 
 VAStatus
-vlVaDestroyBuffer(VADriverContextP ctx, VABufferID buffer_id)
+vlVaDestroyBuffer(VADriverContextP ctx, VABufferID buf_id)
 {
+   vlVaBuffer *buf;
+
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
+   if (!buf)
+      return VA_STATUS_ERROR_INVALID_BUFFER;
+
+   FREE(buf->data);
+   FREE(buf);
+
+   return VA_STATUS_SUCCESS;
 }
 
 VAStatus
 vlVaBufferInfo(VADriverContextP ctx, VABufferID buf_id, VABufferType *type,
                unsigned int *size, unsigned int *num_elements)
 {
+   vlVaBuffer *buf;
+
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
+   if (!buf)
+      return VA_STATUS_ERROR_INVALID_BUFFER;
+
+   *type = buf->type;
+   *size = buf->size;
+   *num_elements = buf->num_elements;
+
+   return VA_STATUS_SUCCESS;
 }
index 048c3f2d8cd4383f34a0e3682f17714c4a71650c..b50429484dbac34255ebbc9e723847807ff06b5b 100644 (file)
@@ -30,6 +30,7 @@
 #include "pipe/p_video_codec.h"
 
 #include "util/u_memory.h"
+#include "util/u_handle_table.h"
 #include "vl/vl_winsys.h"
 
 #include "va_private.h"
@@ -95,10 +96,22 @@ VA_DRIVER_INIT_FUNC(VADriverContextP ctx)
       return VA_STATUS_ERROR_ALLOCATION_FAILED;
 
    drv->vscreen = vl_screen_create(ctx->native_dpy, ctx->x11_screen);
-   if (!drv->vscreen) {
-      FREE(drv);
-      return VA_STATUS_ERROR_ALLOCATION_FAILED;
-   }
+   if (!drv->vscreen)
+      goto error_screen;
+
+   drv->pipe = drv->vscreen->pscreen->context_create(drv->vscreen->pscreen, drv->vscreen);
+   if (!drv->pipe)
+      goto error_pipe;
+
+   drv->htab = handle_table_create();
+   if (!drv->htab)
+      goto error_htab;
+
+   vl_compositor_init(&drv->compositor, drv->pipe);
+   vl_compositor_init_state(&drv->cstate, drv->pipe);
+
+   vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601, NULL, true, &drv->csc);
+   vl_compositor_set_csc_matrix(&drv->cstate, (const vl_csc_matrix *)&drv->csc);
 
    ctx->pDriverData = (void *)drv;
    ctx->version_major = 0;
@@ -113,26 +126,73 @@ VA_DRIVER_INIT_FUNC(VADriverContextP ctx)
    ctx->str_vendor = "mesa gallium vaapi";
 
    return VA_STATUS_SUCCESS;
+
+error_htab:
+   drv->pipe->destroy(drv->pipe);
+
+error_pipe:
+   vl_screen_destroy(drv->vscreen);
+
+error_screen:
+   FREE(drv);
+   return VA_STATUS_ERROR_ALLOCATION_FAILED;
 }
 
 VAStatus
 vlVaCreateContext(VADriverContextP ctx, VAConfigID config_id, int picture_width,
                   int picture_height, int flag, VASurfaceID *render_targets,
-                  int num_render_targets, VAContextID *conext)
+                  int num_render_targets, VAContextID *context_id)
 {
+   struct pipe_video_codec templat = {};
+   vlVaDriver *drv;
+   vlVaContext *context;
+
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   if (!(picture_width && picture_height))
+      return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
+
+   drv = VL_VA_DRIVER(ctx);
+   context = CALLOC(1, sizeof(vlVaContext));
+   if (!context)
+      return VA_STATUS_ERROR_ALLOCATION_FAILED;
+
+   templat.profile = config_id;
+   templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
+   templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
+   templat.width = picture_width;
+   templat.height = picture_height;
+   templat.max_references = num_render_targets;
+   templat.expect_chunked_decode = true;
+
+   context->decoder = drv->pipe->create_video_codec(drv->pipe, &templat);
+   if (!context->decoder) {
+      FREE(context);
+      return VA_STATUS_ERROR_ALLOCATION_FAILED;
+   }
+
+   context->desc.base.profile = config_id;
+   *context_id = handle_table_add(drv->htab, context);
+
+   return VA_STATUS_SUCCESS;
 }
 
 VAStatus
-vlVaDestroyContext(VADriverContextP ctx, VAContextID context)
+vlVaDestroyContext(VADriverContextP ctx, VAContextID context_id)
 {
+   vlVaDriver *drv;
+   vlVaContext *context;
+
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   drv = VL_VA_DRIVER(ctx);
+   context = handle_table_get(drv->htab, context_id);
+   context->decoder->destroy(context->decoder);
+   FREE(context);
+
+   return VA_STATUS_SUCCESS;
 }
 
 VAStatus
@@ -144,7 +204,11 @@ vlVaTerminate(VADriverContextP ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
    drv = ctx->pDriverData;
+   vl_compositor_cleanup_state(&drv->cstate);
+   vl_compositor_cleanup(&drv->compositor);
+   drv->pipe->destroy(drv->pipe);
    vl_screen_destroy(drv->vscreen);
+   handle_table_destroy(drv->htab);
    FREE(drv);
 
    return VA_STATUS_SUCCESS;
index 58cd8ad772e4cefd843d4a48cd90ccb17e631025..6f1ddf9a248fd19d16d9711eeba1e800937d1dec 100644 (file)
  *
  **************************************************************************/
 
+#include "pipe/p_screen.h"
+
+#include "util/u_memory.h"
+#include "util/u_handle_table.h"
+#include "util/u_rect.h"
+
+#include "vl/vl_compositor.h"
+#include "vl/vl_winsys.h"
+
 #include "va_private.h"
 
 VAStatus
 vlVaCreateSurfaces(VADriverContextP ctx, int width, int height, int format,
                    int num_surfaces, VASurfaceID *surfaces)
 {
+   struct pipe_video_buffer templat = {};
+   struct pipe_screen *pscreen;
+   vlVaDriver *drv;
+   int i;
+
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
    if (!(width && height))
       return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   drv = VL_VA_DRIVER(ctx);
+   pscreen = VL_VA_PSCREEN(ctx);
+
+   templat.buffer_format = pscreen->get_video_param
+   (
+      pscreen,
+      PIPE_VIDEO_PROFILE_UNKNOWN,
+      PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
+      PIPE_VIDEO_CAP_PREFERED_FORMAT
+   );
+   templat.chroma_format = ChromaToPipe(format);
+   templat.width = width;
+   templat.height = height;
+   templat.interlaced = pscreen->get_video_param
+   (
+      pscreen,
+      PIPE_VIDEO_PROFILE_UNKNOWN,
+      PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
+      PIPE_VIDEO_CAP_PREFERS_INTERLACED
+   );
+
+   for (i = 0; i < num_surfaces; ++i) {
+      vlVaSurface *surf = CALLOC(1, sizeof(vlVaSurface));
+      if (!surf)
+         goto no_res;
+
+      surf->templat = templat;
+      surfaces[i] = handle_table_add(drv->htab, surf);
+   }
+
+   return VA_STATUS_SUCCESS;
+
+no_res:
+   if (i)
+      vlVaDestroySurfaces(ctx, surfaces, i);
+
+   return VA_STATUS_ERROR_ALLOCATION_FAILED;
 }
 
 VAStatus
 vlVaDestroySurfaces(VADriverContextP ctx, VASurfaceID *surface_list, int num_surfaces)
 {
+   vlVaDriver *drv;
+   int i;
+
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   drv = VL_VA_DRIVER(ctx);
+   for (i = 0; i < num_surfaces; ++i) {
+      vlVaSurface *surf = handle_table_get(drv->htab, surface_list[i]);
+      if (surf->buffer)
+         surf->buffer->destroy(surf->buffer);
+      if(surf->fence)
+         drv->pipe->screen->fence_reference(drv->pipe->screen, &surf->fence, NULL);
+      FREE(surf);
+      handle_table_remove(drv->htab, surface_list[i]);
+   }
+
+   return VA_STATUS_SUCCESS;
 }
 
 VAStatus
@@ -56,7 +120,7 @@ vlVaSyncSurface(VADriverContextP ctx, VASurfaceID render_target)
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   return VA_STATUS_SUCCESS;
 }
 
 VAStatus
@@ -65,7 +129,7 @@ vlVaQuerySurfaceStatus(VADriverContextP ctx, VASurfaceID render_target, VASurfac
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   return VA_STATUS_SUCCESS;
 }
 
 VAStatus
@@ -78,15 +142,69 @@ vlVaQuerySurfaceError(VADriverContextP ctx, VASurfaceID render_target, VAStatus
 }
 
 VAStatus
-vlVaPutSurface(VADriverContextP ctx, VASurfaceID surface, void* draw, short srcx, short srcy,
+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)
 {
+   vlVaDriver *drv;
+   vlVaSurface *surf;
+   struct pipe_screen *screen;
+   struct pipe_resource *tex;
+   struct pipe_surface surf_templ, *surf_draw;
+   struct u_rect src_rect, *dirty_area;
+
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-   return VA_STATUS_ERROR_UNIMPLEMENTED;
+   drv = VL_VA_DRIVER(ctx);
+   surf = handle_table_get(drv->htab, surface_id);
+   if (!surf)
+      return VA_STATUS_ERROR_INVALID_SURFACE;
+
+   screen = drv->pipe->screen;
+
+   if(surf->fence) {
+      screen->fence_finish(screen, surf->fence, PIPE_TIMEOUT_INFINITE);
+      screen->fence_reference(screen, &surf->fence, NULL);
+   }
+
+   tex = vl_screen_texture_from_drawable(drv->vscreen, (Drawable)draw);
+   if (!tex)
+      return VA_STATUS_ERROR_INVALID_DISPLAY;
+
+   dirty_area = vl_screen_get_dirty_area(drv->vscreen);
+
+   memset(&surf_templ, 0, sizeof(surf_templ));
+   surf_templ.format = tex->format;
+   surf_draw = drv->pipe->create_surface(drv->pipe, tex, &surf_templ);
+   if (!surf_draw) {
+      pipe_resource_reference(&tex, NULL);
+      return VA_STATUS_ERROR_INVALID_DISPLAY;
+   }
+
+   src_rect.x0 = srcx;
+   src_rect.y0 = srcy;
+   src_rect.x1 = srcw + srcx;
+   src_rect.y1 = srch + srcy;
+
+   vl_compositor_clear_layers(&drv->cstate);
+   vl_compositor_set_buffer_layer(&drv->cstate, &drv->compositor, 0, surf->buffer, &src_rect, NULL, VL_COMPOSITOR_WEAVE);
+   vl_compositor_render(&drv->cstate, &drv->compositor, surf_draw, dirty_area, true);
+
+   screen->flush_frontbuffer
+   (
+      screen, tex, 0, 0,
+      vl_screen_get_private(drv->vscreen), NULL
+   );
+
+   screen->fence_reference(screen, &surf->fence, NULL);
+   drv->pipe->flush(drv->pipe, &surf->fence, 0);
+
+   pipe_resource_reference(&tex, NULL);
+   pipe_surface_reference(&surf_draw, NULL);
+
+   return VA_STATUS_SUCCESS;
 }
 
 VAStatus
index 130d2dff1a02ecd08f9ca4dd8d7c4fa1387a2492..7ba5bf4996d20807fd098df9a5f9bb2c65cf0a78 100644 (file)
 #include <va/va_backend.h>
 
 #include "pipe/p_video_enums.h"
+#include "pipe/p_video_codec.h"
+
+#include "vl/vl_compositor.h"
+#include "vl/vl_csc.h"
 
 #define VL_VA_DRIVER(ctx) ((vlVaDriver *)ctx->pDriverData)
 #define VL_VA_PSCREEN(ctx) (VL_VA_DRIVER(ctx)->vscreen->pscreen)
 
+static inline enum pipe_video_chroma_format
+ChromaToPipe(int format)
+{
+   switch (format) {
+   case VA_RT_FORMAT_YUV420:
+      return PIPE_VIDEO_CHROMA_FORMAT_420;
+   case VA_RT_FORMAT_YUV422:
+      return PIPE_VIDEO_CHROMA_FORMAT_422;
+   case VA_RT_FORMAT_YUV444:
+      return PIPE_VIDEO_CHROMA_FORMAT_444;
+   default:
+      assert(0);
+      return PIPE_VIDEO_CHROMA_FORMAT_420;
+   }
+}
+
 static inline VAProfile
 PipeToProfile(enum pipe_video_profile profile)
 {
@@ -102,8 +122,36 @@ ProfileToPipe(VAProfile profile)
 
 typedef struct {
    struct vl_screen *vscreen;
+   struct pipe_context *pipe;
+   struct handle_table *htab;
+   struct vl_compositor compositor;
+   struct vl_compositor_state cstate;
+   vl_csc_matrix csc;
 } vlVaDriver;
 
+typedef struct {
+   struct pipe_video_codec *decoder;
+   union {
+      struct pipe_picture_desc base;
+      struct pipe_mpeg12_picture_desc mpeg12;
+      struct pipe_mpeg4_picture_desc mpeg4;
+      struct pipe_vc1_picture_desc vc1;
+      struct pipe_h264_picture_desc h264;
+   } desc;
+} vlVaContext;
+
+typedef struct {
+   VABufferType type;
+   unsigned int size;
+   unsigned int num_elements;
+   void *data;
+} vlVaBuffer;
+
+typedef struct {
+   struct pipe_video_buffer templat, *buffer;
+   struct pipe_fence_handle *fence;
+} vlVaSurface;
+
 // Public functions:
 VAStatus VA_DRIVER_INIT_FUNC(VADriverContextP ctx);