ilo: correctly program SO states for GEN7
[mesa.git] / src / gallium / state_trackers / vdpau / surface.c
index 9b6dac9c3f41f905fa21a0869aa3af6c92a3c8f8..5c06f8511aaf3a79ad1e2e96acd227c9f8ee4433 100644 (file)
@@ -1,6 +1,7 @@
 /**************************************************************************
  *
  * Copyright 2010 Thomas Balling Sørensen.
+ * Copyright 2011 Christian König.
  * All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  *
  **************************************************************************/
 
-#include "vdpau_private.h"
-#include <pipe/p_screen.h>
-#include <pipe/p_state.h>
-#include <util/u_memory.h>
-#include <util/u_format.h>
-
-VdpStatus
-vlVdpVideoSurfaceCreate(VdpDevice device,
- VdpChromaType chroma_type,
- uint32_t width,
- uint32_t height,
- VdpVideoSurface *surface)
-{
- printf("[VDPAU] Creating a surface\n");
-
- vlVdpSurface *p_surf;
- VdpStatus ret;
-
- if (!(width && height))
- {
- ret = VDP_STATUS_INVALID_SIZE;
- goto inv_size;
- }
-
-
- if (!vlCreateHTAB()) {
- ret = VDP_STATUS_RESOURCES;
- goto no_htab;
- }
+#include <assert.h>
 
- p_surf = CALLOC(1, sizeof(p_surf));
- if (!p_surf) {
- ret = VDP_STATUS_RESOURCES;
- goto no_res;
- }
+#include "pipe/p_state.h"
 
- vlVdpDevice *dev = vlGetDataHTAB(device);
- if (!dev) {
- ret = VDP_STATUS_INVALID_HANDLE;
- goto inv_device;
- }
+#include "util/u_memory.h"
+#include "util/u_debug.h"
+#include "util/u_rect.h"
+#include "vl/vl_defines.h"
 
- p_surf->chroma_format = TypeToPipe(chroma_type);
- p_surf->device = dev;
- p_surf->width = width;
- p_surf->height = height;
-
- *surface = vlAddDataHTAB(p_surf);
- if (*surface == 0) {
- ret = VDP_STATUS_ERROR;
- goto no_handle;
- }
+#include "vdpau_private.h"
 
- return VDP_STATUS_OK;
+/**
+ * Create a VdpVideoSurface.
+ */
+VdpStatus
+vlVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type,
+                        uint32_t width, uint32_t height,
+                        VdpVideoSurface *surface)
+{
+   struct pipe_context *pipe;
+   vlVdpSurface *p_surf;
+   VdpStatus ret;
+
+   if (!(width && height)) {
+      ret = VDP_STATUS_INVALID_SIZE;
+      goto inv_size;
+   }
+
+   if (!vlCreateHTAB()) {
+      ret = VDP_STATUS_RESOURCES;
+      goto no_htab;
+   }
+
+   p_surf = CALLOC(1, sizeof(vlVdpSurface));
+   if (!p_surf) {
+      ret = VDP_STATUS_RESOURCES;
+      goto no_res;
+   }
+
+   vlVdpDevice *dev = vlGetDataHTAB(device);
+   if (!dev) {
+      ret = VDP_STATUS_INVALID_HANDLE;
+      goto inv_device;
+   }
+
+   p_surf->device = dev;
+   pipe = dev->context;
+
+   pipe_mutex_lock(dev->mutex);
+   memset(&p_surf->templat, 0, sizeof(p_surf->templat));
+   p_surf->templat.buffer_format = pipe->screen->get_video_param
+   (
+      pipe->screen,
+      PIPE_VIDEO_PROFILE_UNKNOWN,
+      PIPE_VIDEO_CAP_PREFERED_FORMAT
+   );
+   p_surf->templat.chroma_format = ChromaToPipe(chroma_type);
+   p_surf->templat.width = width;
+   p_surf->templat.height = height;
+   p_surf->templat.interlaced = pipe->screen->get_video_param
+   (
+      pipe->screen,
+      PIPE_VIDEO_PROFILE_UNKNOWN,
+      PIPE_VIDEO_CAP_PREFERS_INTERLACED
+   );
+   p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
+   vlVdpVideoSurfaceClear(p_surf);
+   pipe_mutex_unlock(dev->mutex);
+
+   *surface = vlAddDataHTAB(p_surf);
+   if (*surface == 0) {
+      ret = VDP_STATUS_ERROR;
+      goto no_handle;
+   }
+
+   return VDP_STATUS_OK;
 
 no_handle:
- FREE(p_surf->psurface);
+   p_surf->video_buffer->destroy(p_surf->video_buffer);
+
 inv_device:
-no_surf:
- FREE(p_surf);
+   FREE(p_surf);
+
 no_res:
- // vlDestroyHTAB(); XXX: Do not destroy this tab, I think.
 no_htab:
 inv_size:
- return ret;
-}
-
-VdpStatus
-vlVdpVideoSurfaceDestroy ( VdpVideoSurface surface )
-{
- vlVdpSurface *p_surf;
-
- p_surf = (vlVdpSurface *)vlGetDataHTAB((vlHandle)surface);
- if (!p_surf)
- return VDP_STATUS_INVALID_HANDLE;
-
- if (p_surf->psurface) {
- if (p_surf->psurface->texture) {
- if (p_surf->psurface->texture->screen)
- p_surf->psurface->texture->screen->tex_surface_destroy(p_surf->psurface);
- }
- }
- FREE(p_surf);
- return VDP_STATUS_OK;
+   return ret;
 }
 
+/**
+ * Destroy a VdpVideoSurface.
+ */
 VdpStatus
-vlVdpVideoSurfaceGetParameters ( VdpVideoSurface surface,
- VdpChromaType *chroma_type,
- uint32_t *width,
- uint32_t *height
-)
+vlVdpVideoSurfaceDestroy(VdpVideoSurface surface)
 {
- if (!(width && height && chroma_type))
- return VDP_STATUS_INVALID_POINTER;
-
-
- vlVdpSurface *p_surf = vlGetDataHTAB(surface);
- if (!p_surf)
- return VDP_STATUS_INVALID_HANDLE;
+   vlVdpSurface *p_surf;
 
+   p_surf = (vlVdpSurface *)vlGetDataHTAB((vlHandle)surface);
+   if (!p_surf)
+      return VDP_STATUS_INVALID_HANDLE;
 
- if (!(p_surf->chroma_format > 0 && p_surf->chroma_format < 3))
- return VDP_STATUS_INVALID_CHROMA_TYPE;
+   pipe_mutex_lock(p_surf->device->mutex);
+   if (p_surf->video_buffer)
+      p_surf->video_buffer->destroy(p_surf->video_buffer);
+   pipe_mutex_unlock(p_surf->device->mutex);
 
- *width = p_surf->width;
- *height = p_surf->height;
- *chroma_type = PipeToType(p_surf->chroma_format);
-
- return VDP_STATUS_OK;
+   FREE(p_surf);
+   return VDP_STATUS_OK;
 }
 
+/**
+ * Retrieve the parameters used to create a VdpVideoSurface.
+ */
 VdpStatus
-vlVdpVideoSurfaceGetBitsYCbCr ( VdpVideoSurface surface,
- VdpYCbCrFormat destination_ycbcr_format,
- void *const *destination_data,
- uint32_t const *destination_pitches
-)
+vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface,
+                               VdpChromaType *chroma_type,
+                               uint32_t *width, uint32_t *height)
 {
- if (!vlCreateHTAB())
- return VDP_STATUS_RESOURCES;
-
-
- vlVdpSurface *p_surf = vlGetDataHTAB(surface);
- if (!p_surf)
- return VDP_STATUS_INVALID_HANDLE;
-
- if (!p_surf->psurface)
- return VDP_STATUS_RESOURCES;
-
+   if (!(width && height && chroma_type))
+      return VDP_STATUS_INVALID_POINTER;
+
+   vlVdpSurface *p_surf = vlGetDataHTAB(surface);
+   if (!p_surf)
+      return VDP_STATUS_INVALID_HANDLE;
+
+   if (p_surf->video_buffer) {
+      *width = p_surf->video_buffer->width;
+      *height = p_surf->video_buffer->height;
+      *chroma_type = PipeToChroma(p_surf->video_buffer->chroma_format);
+   } else {
+      *width = p_surf->templat.width;
+      *height = p_surf->templat.height;
+      *chroma_type = PipeToChroma(p_surf->templat.chroma_format);
+   }
+
+   return VDP_STATUS_OK;
+}
 
- return VDP_STATUS_OK;
+static void
+vlVdpVideoSurfaceSize(vlVdpSurface *p_surf, int component,
+                      unsigned *width, unsigned *height)
+{
+   *width = p_surf->templat.width;
+   *height = p_surf->templat.height;
+
+   if (component > 0) {
+      if (p_surf->templat.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
+         *width /= 2;
+         *height /= 2;
+      } else if (p_surf->templat.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
+         *height /= 2;
+      }
+   }
+   if (p_surf->templat.interlaced)
+      *height /= 2;
 }
 
+/**
+ * Copy image data from a VdpVideoSurface to application memory in a specified
+ * YCbCr format.
+ */
 VdpStatus
-vlVdpVideoSurfacePutBitsYCbCr ( VdpVideoSurface surface,
- VdpYCbCrFormat source_ycbcr_format,
- void const *const *source_data,
- uint32_t const *source_pitches
-)
+vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface,
+                              VdpYCbCrFormat destination_ycbcr_format,
+                              void *const *destination_data,
+                              uint32_t const *destination_pitches)
 {
- uint32_t size_surface_bytes;
- const struct util_format_description *format_desc;
- enum pipe_format pformat = FormatToPipe(source_ycbcr_format);
+   vlVdpSurface *vlsurface;
+   struct pipe_context *pipe;
+   enum pipe_format format;
+   struct pipe_sampler_view **sampler_views;
+   unsigned i, j;
+
+   vlsurface = vlGetDataHTAB(surface);
+   if (!vlsurface)
+      return VDP_STATUS_INVALID_HANDLE;
+
+   pipe = vlsurface->device->context;
+   if (!pipe)
+      return VDP_STATUS_INVALID_HANDLE;
+
+   format = FormatYCBCRToPipe(destination_ycbcr_format);
+   if (format == PIPE_FORMAT_NONE)
+       return VDP_STATUS_INVALID_Y_CB_CR_FORMAT;
+
+   if (vlsurface->video_buffer == NULL || format != vlsurface->video_buffer->buffer_format)
+      return VDP_STATUS_NO_IMPLEMENTATION; /* TODO We don't support conversion (yet) */
+
+   pipe_mutex_lock(vlsurface->device->mutex);
+   sampler_views = vlsurface->video_buffer->get_sampler_view_planes(vlsurface->video_buffer);
+   if (!sampler_views) {
+      pipe_mutex_unlock(vlsurface->device->mutex);
+      return VDP_STATUS_RESOURCES;
+   }
+
+   for (i = 0; i < 3; ++i) {
+      unsigned width, height;
+      struct pipe_sampler_view *sv = sampler_views[i];
+      if (!sv) continue;
+
+      vlVdpVideoSurfaceSize(vlsurface, i, &width, &height);
+
+      for (j = 0; j < sv->texture->depth0; ++j) {
+         struct pipe_box box = {
+            0, 0, j,
+            width, height, 1
+         };
+         struct pipe_transfer *transfer;
+         uint8_t *map;
+
+         map = pipe->transfer_map(pipe, sv->texture, 0,
+                                       PIPE_TRANSFER_READ, &box, &transfer);
+         if (!map) {
+            pipe_mutex_unlock(vlsurface->device->mutex);
+            return VDP_STATUS_RESOURCES;
+         }
+
+         util_copy_rect(destination_data[i] + destination_pitches[i] * j, sv->texture->format,
+                        destination_pitches[i] * sv->texture->depth0, 0, 0,
+                        box.width, box.height, map, transfer->stride, 0, 0);
+
+         pipe_transfer_unmap(pipe, transfer);
+      }
+   }
+   pipe_mutex_unlock(vlsurface->device->mutex);
+
+   return VDP_STATUS_OK;
+}
 
- if (!vlCreateHTAB())
- return VDP_STATUS_RESOURCES;
+/**
+ * Copy image data from application memory in a specific YCbCr format to
+ * a VdpVideoSurface.
+ */
+VdpStatus
+vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
+                              VdpYCbCrFormat source_ycbcr_format,
+                              void const *const *source_data,
+                              uint32_t const *source_pitches)
+{
+   enum pipe_format pformat = FormatYCBCRToPipe(source_ycbcr_format);
+   struct pipe_context *pipe;
+   struct pipe_sampler_view **sampler_views;
+   unsigned i, j;
+
+   if (!vlCreateHTAB())
+      return VDP_STATUS_RESOURCES;
+
+   vlVdpSurface *p_surf = vlGetDataHTAB(surface);
+   if (!p_surf)
+      return VDP_STATUS_INVALID_HANDLE;
+
+   pipe = p_surf->device->context;
+   if (!pipe)
+      return VDP_STATUS_INVALID_HANDLE;
+
+   pipe_mutex_lock(p_surf->device->mutex);
+   if (p_surf->video_buffer == NULL || pformat != p_surf->video_buffer->buffer_format) {
+
+      /* destroy the old one */
+      if (p_surf->video_buffer)
+         p_surf->video_buffer->destroy(p_surf->video_buffer);
+
+      /* adjust the template parameters */
+      p_surf->templat.buffer_format = pformat;
+
+      /* and try to create the video buffer with the new format */
+      p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
+
+      /* stil no luck? ok forget it we don't support it */
+      if (!p_surf->video_buffer) {
+         pipe_mutex_unlock(p_surf->device->mutex);
+         return VDP_STATUS_NO_IMPLEMENTATION;
+      }
+      vlVdpVideoSurfaceClear(p_surf);
+   }
+
+   sampler_views = p_surf->video_buffer->get_sampler_view_planes(p_surf->video_buffer);
+   if (!sampler_views) {
+      pipe_mutex_unlock(p_surf->device->mutex);
+      return VDP_STATUS_RESOURCES;
+   }
+
+   for (i = 0; i < 3; ++i) {
+      unsigned width, height;
+      struct pipe_sampler_view *sv = sampler_views[i];
+      if (!sv || !source_pitches[i]) continue;
+
+      vlVdpVideoSurfaceSize(p_surf, i, &width, &height);
+
+      for (j = 0; j < sv->texture->depth0; ++j) {
+         struct pipe_box dst_box = {
+            0, 0, j,
+            width, height, 1
+         };
+
+         pipe->transfer_inline_write(pipe, sv->texture, 0,
+                                     PIPE_TRANSFER_WRITE, &dst_box,
+                                     source_data[i] + source_pitches[i] * j,
+                                     source_pitches[i] * sv->texture->depth0,
+                                     0);
+      }
+   }
+   pipe_mutex_unlock(p_surf->device->mutex);
+
+   return VDP_STATUS_OK;
+}
 
+/**
+ * Helper function to initially clear the VideoSurface after (re-)creation
+ */
+void
+vlVdpVideoSurfaceClear(vlVdpSurface *vlsurf)
+{
+   struct pipe_context *pipe = vlsurf->device->context;
+   struct pipe_surface **surfaces;
+   unsigned i;
 
- vlVdpSurface *p_surf = vlGetDataHTAB(surface);
- if (!p_surf)
- return VDP_STATUS_INVALID_HANDLE;
+   if (!vlsurf->video_buffer)
+      return;
 
+   surfaces = vlsurf->video_buffer->get_surfaces(vlsurf->video_buffer);
+   for (i = 0; i < VL_MAX_SURFACES; ++i) {
+      union pipe_color_union c = {};
 
- //size_surface_bytes = ( source_pitches[0] * p_surf->height util_format_get_blockheight(pformat) );
- /*util_format_translate(enum pipe_format dst_format,
- void *dst, unsigned dst_stride,
- unsigned dst_x, unsigned dst_y,
- enum pipe_format src_format,
- const void *src, unsigned src_stride,
- unsigned src_x, unsigned src_y,
- unsigned width, unsigned height);*/
+      if (!surfaces[i])
+         continue;
 
- return VDP_STATUS_NO_IMPLEMENTATION;
+      if (i > !!vlsurf->templat.interlaced)
+         c.f[0] = c.f[1] = c.f[2] = c.f[3] = 0.5f;
 
+      pipe->clear_render_target(pipe, surfaces[i], &c, 0, 0,
+                                surfaces[i]->width, surfaces[i]->height);
+   }
+   pipe->flush(pipe, NULL, 0);
 }