radeonsi: properly handler raster_config setup on CZ
[mesa.git] / src / gallium / state_trackers / vdpau / surface.c
index 206a8397e9f35b0ce3b84e594cbf179df0b8bfb9..55d0d76d16d3f2ab7087ddf974d0534874dc1018 100644 (file)
@@ -19,7 +19,7 @@
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
- * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "util/u_memory.h"
 #include "util/u_debug.h"
 #include "util/u_rect.h"
+#include "util/u_surface.h"
+#include "util/u_video.h"
+#include "vl/vl_defines.h"
 
 #include "vdpau_private.h"
 
+enum getbits_conversion {
+   CONVERSION_NONE,
+   CONVERSION_NV12_TO_YV12,
+   CONVERSION_YV12_TO_NV12,
+   CONVERSION_SWAP_YUYV_UYVY,
+};
+
 /**
  * Create a VdpVideoSurface.
  */
@@ -44,22 +54,15 @@ vlVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type,
                         uint32_t width, uint32_t height,
                         VdpVideoSurface *surface)
 {
-   struct pipe_video_buffer tmpl;
+   struct pipe_context *pipe;
    vlVdpSurface *p_surf;
    VdpStatus ret;
 
-   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Creating a surface\n");
-
    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;
@@ -72,17 +75,34 @@ vlVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type,
       goto inv_device;
    }
 
-   p_surf->device = dev;
-   memset(&tmpl, 0, sizeof(tmpl));
-   tmpl.buffer_format = PIPE_FORMAT_YV12;
-   tmpl.chroma_format = ChromaToPipe(chroma_type);
-   tmpl.width = width;
-   tmpl.height = height;
-   p_surf->video_buffer = dev->context->pipe->create_video_buffer
+   DeviceReference(&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_ENTRYPOINT_BITSTREAM,
+      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
    (
-      dev->context->pipe,
-      &tmpl
+      pipe->screen,
+      PIPE_VIDEO_PROFILE_UNKNOWN,
+      PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
+      PIPE_VIDEO_CAP_PREFERS_INTERLACED
    );
+   if (p_surf->templat.buffer_format != PIPE_FORMAT_NONE)
+      p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
+
+   /* do not mandate early allocation of a video buffer */
+   vlVdpVideoSurfaceClear(p_surf);
+   pipe_mutex_unlock(dev->mutex);
 
    *surface = vlAddDataHTAB(p_surf);
    if (*surface == 0) {
@@ -96,10 +116,10 @@ no_handle:
    p_surf->video_buffer->destroy(p_surf->video_buffer);
 
 inv_device:
+   DeviceReference(&p_surf->device, NULL);
    FREE(p_surf);
 
 no_res:
-no_htab:
 inv_size:
    return ret;
 }
@@ -116,10 +136,15 @@ vlVdpVideoSurfaceDestroy(VdpVideoSurface surface)
    if (!p_surf)
       return VDP_STATUS_INVALID_HANDLE;
 
+   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);
 
+   vlRemoveDataHTAB(surface);
+   DeviceReference(&p_surf->device, NULL);
    FREE(p_surf);
+
    return VDP_STATUS_OK;
 }
 
@@ -138,13 +163,38 @@ vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface,
    if (!p_surf)
       return VDP_STATUS_INVALID_HANDLE;
 
-   *width = p_surf->video_buffer->width;
-   *height = p_surf->video_buffer->height;
-   *chroma_type = PipeToChroma(p_surf->video_buffer->chroma_format);
+   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;
 }
 
+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) {
+         *width /= 2;
+      }
+   }
+   if (p_surf->templat.interlaced)
+      *height /= 2;
+}
+
 /**
  * Copy image data from a VdpVideoSurface to application memory in a specified
  * YCbCr format.
@@ -155,18 +205,94 @@ vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface,
                               void *const *destination_data,
                               uint32_t const *destination_pitches)
 {
-   if (!vlCreateHTAB())
-      return VDP_STATUS_RESOURCES;
+   vlVdpSurface *vlsurface;
+   struct pipe_context *pipe;
+   enum pipe_format format, buffer_format;
+   struct pipe_sampler_view **sampler_views;
+   enum getbits_conversion conversion = CONVERSION_NONE;
+   unsigned i, j;
 
-   vlVdpSurface *p_surf = vlGetDataHTAB(surface);
-   if (!p_surf)
+   vlsurface = vlGetDataHTAB(surface);
+   if (!vlsurface)
       return VDP_STATUS_INVALID_HANDLE;
 
-   //if (!p_surf->psurface)
-   //   return VDP_STATUS_RESOURCES;
+   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)
+      return VDP_STATUS_INVALID_VALUE;
+
+   buffer_format = vlsurface->video_buffer->buffer_format;
+   if (format != buffer_format) {
+      if (format == PIPE_FORMAT_YV12 && buffer_format == PIPE_FORMAT_NV12)
+         conversion = CONVERSION_NV12_TO_YV12;
+      else if (format == PIPE_FORMAT_NV12 && buffer_format == PIPE_FORMAT_YV12)
+         conversion = CONVERSION_YV12_TO_NV12;
+      else if ((format == PIPE_FORMAT_YUYV && buffer_format == PIPE_FORMAT_UYVY) ||
+               (format == PIPE_FORMAT_UYVY && buffer_format == PIPE_FORMAT_YUYV))
+         conversion = CONVERSION_SWAP_YUYV_UYVY;
+      else
+         return VDP_STATUS_NO_IMPLEMENTATION;
+   }
 
-   //return VDP_STATUS_OK;
-   return VDP_STATUS_NO_IMPLEMENTATION;
+   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->array_size; ++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;
+         }
+
+         if (conversion == CONVERSION_NV12_TO_YV12 && i == 1) {
+            u_copy_nv12_to_yv12(destination_data, destination_pitches,
+                                i, j, transfer->stride, sv->texture->array_size,
+                                map, box.width, box.height);
+         } else if (conversion == CONVERSION_YV12_TO_NV12 && i > 0) {
+            u_copy_yv12_to_nv12(destination_data, destination_pitches,
+                                i, j, transfer->stride, sv->texture->array_size,
+                                map, box.width, box.height);
+         } else if (conversion == CONVERSION_SWAP_YUYV_UYVY) {
+            u_copy_swap422_packed(destination_data, destination_pitches,
+                                   i, j, transfer->stride, sv->texture->array_size,
+                                   map, box.width, box.height);
+         } else {
+            util_copy_rect(destination_data[i] + destination_pitches[i] * j, sv->texture->format,
+                           destination_pitches[i] * sv->texture->array_size, 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;
 }
 
 /**
@@ -182,50 +308,114 @@ vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
    enum pipe_format pformat = FormatYCBCRToPipe(source_ycbcr_format);
    struct pipe_context *pipe;
    struct pipe_sampler_view **sampler_views;
-   unsigned i;
-
-   if (!vlCreateHTAB())
-      return VDP_STATUS_RESOURCES;
+   unsigned i, j;
 
    vlVdpSurface *p_surf = vlGetDataHTAB(surface);
    if (!p_surf)
       return VDP_STATUS_INVALID_HANDLE;
 
-   pipe = p_surf->device->context->pipe;
+   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) {
-      assert(0); // TODO Recreate resource
-      return VDP_STATUS_NO_IMPLEMENTATION;
+
+      /* 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)
+   if (!sampler_views) {
+      pipe_mutex_unlock(p_surf->device->mutex);
       return VDP_STATUS_RESOURCES;
+   }
 
-   for (i = 0; i < 3; ++i) { //TODO put nr of planes into util format
-      struct pipe_sampler_view *sv = sampler_views[i ? i ^ 3 : 0];
-      struct pipe_box dst_box = { 0, 0, 0, sv->texture->width0, sv->texture->height0, 1 };
-
-      struct pipe_transfer *transfer;
-      void *map;
+   for (i = 0; i < 3; ++i) {
+      unsigned width, height;
+      struct pipe_sampler_view *sv = sampler_views[i];
+      if (!sv || !source_pitches[i]) continue;
 
-      transfer = pipe->get_transfer(pipe, sv->texture, 0, PIPE_TRANSFER_WRITE, &dst_box);
-      if (!transfer)
-         return VDP_STATUS_RESOURCES;
+      vlVdpVideoSurfaceSize(p_surf, i, &width, &height);
 
-      map = pipe->transfer_map(pipe, transfer);
-      if (map) {
-         util_copy_rect(map, sv->texture->format, transfer->stride, 0, 0,
-                        dst_box.width, dst_box.height,
-                        source_data[i], source_pitches[i], 0, 0);
+      for (j = 0; j < sv->texture->array_size; ++j) {
+         struct pipe_box dst_box = {
+            0, 0, j,
+            width, height, 1
+         };
 
-         pipe->transfer_unmap(pipe, transfer);
+         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->array_size,
+                                     0);
       }
-
-      pipe->transfer_destroy(pipe, transfer);
    }
+   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;
+
+   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);
+}
+
+/**
+ * Interop to mesa state tracker
+ */
+struct pipe_video_buffer *vlVdpVideoSurfaceGallium(VdpVideoSurface surface)
+{
+   vlVdpSurface *p_surf = vlGetDataHTAB(surface);
+   if (!p_surf)
+      return NULL;
+
+   pipe_mutex_lock(p_surf->device->mutex);
+   if (p_surf->video_buffer == NULL) {
+      struct pipe_context *pipe = p_surf->device->context;
+
+      /* try to create a video buffer if we don't already have one */
+      p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
+   }
+   pipe_mutex_unlock(p_surf->device->mutex);
+
+   return p_surf->video_buffer;
+}