ilo: correctly program SO states for GEN7
[mesa.git] / src / gallium / state_trackers / vdpau / surface.c
index c829c1ff7dec4172dd8d36eb26102ff7bd3be880..5c06f8511aaf3a79ad1e2e96acd227c9f8ee4433 100644 (file)
@@ -33,6 +33,7 @@
 #include "util/u_memory.h"
 #include "util/u_debug.h"
 #include "util/u_rect.h"
+#include "vl/vl_defines.h"
 
 #include "vdpau_private.h"
 
@@ -91,6 +92,7 @@ vlVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type,
       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);
@@ -162,6 +164,25 @@ vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface,
    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.
@@ -201,26 +222,23 @@ vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface,
    }
 
    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,
-            sv->texture->width0, sv->texture->height0, 1
+            width, height, 1
          };
          struct pipe_transfer *transfer;
          uint8_t *map;
 
-         transfer = pipe->get_transfer(pipe, sv->texture, 0, PIPE_TRANSFER_READ, &box);
-         if (transfer == NULL) {
-            pipe_mutex_unlock(vlsurface->device->mutex);
-            return VDP_STATUS_RESOURCES;
-         }
-
-         map = pipe_transfer_map(pipe, transfer);
-         if (map == NULL) {
-            pipe_transfer_destroy(pipe, transfer);
+         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;
          }
@@ -230,7 +248,6 @@ vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface,
                         box.width, box.height, map, transfer->stride, 0, 0);
 
          pipe_transfer_unmap(pipe, transfer);
-         pipe_transfer_destroy(pipe, transfer);
       }
    }
    pipe_mutex_unlock(vlsurface->device->mutex);
@@ -282,6 +299,7 @@ vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
          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);
@@ -291,13 +309,16 @@ vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
    }
 
    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,
-            sv->texture->width0, sv->texture->height0, 1
+            width, height, 1
          };
 
          pipe->transfer_inline_write(pipe, sv->texture, 0,
@@ -311,3 +332,32 @@ vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
 
    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;
+
+   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 = {};
+
+      if (!surfaces[i])
+         continue;
+
+      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);
+}