/**********************************************************
- * Copyright 2009 VMware, Inc. All rights reserved.
+ * Copyright 2009-2015 VMware, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
#include "svga_cmd.h"
#include "util/u_debug.h"
#include "util/u_memory.h"
-
+#include "pipe/p_defines.h"
#include "vmw_surface.h"
#include "vmw_screen.h"
+#include "vmw_buffer.h"
+#include "vmw_context.h"
+#include "pipebuffer/pb_bufmgr.h"
+
+void
+vmw_svga_winsys_surface_init(struct svga_winsys_screen *sws,
+ struct svga_winsys_surface *srf,
+ unsigned surf_size, SVGA3dSurfaceAllFlags flags)
+{
+ struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
+ void *data = NULL;
+ struct pb_buffer *pb_buf;
+ uint32_t pb_flags;
+ struct vmw_winsys_screen *vws = vsrf->screen;
+ pb_flags = PIPE_TRANSFER_READ_WRITE | PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
+
+ struct pb_manager *provider;
+ struct pb_desc desc;
+
+ data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf,
+ PIPE_TRANSFER_DONTBLOCK | pb_flags);
+ if (data)
+ goto out_unlock;
+
+ provider = vws->pools.mob_fenced;
+ memset(&desc, 0, sizeof(desc));
+ desc.alignment = 4096;
+ pb_buf = provider->create_buffer(provider, vsrf->size, &desc);
+ if (pb_buf != NULL) {
+ struct svga_winsys_buffer *vbuf =
+ vmw_svga_winsys_buffer_wrap(pb_buf);
+
+ data = vmw_svga_winsys_buffer_map(&vws->base, vbuf, pb_flags);
+ if (data) {
+ if (vsrf->buf) {
+ vmw_svga_winsys_buffer_destroy(&vws->base, vsrf->buf);
+ vsrf->buf = vbuf;
+ goto out_unlock;
+ } else
+ vmw_svga_winsys_buffer_destroy(&vws->base, vbuf);
+ }
+ }
+
+ data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf, pb_flags);
+ if (data == NULL)
+ goto out_unlock;
+
+out_unlock:
+ mtx_unlock(&vsrf->mutex);
+
+ if (data)
+ {
+ if (flags & SVGA3D_SURFACE_BIND_STREAM_OUTPUT) {
+ memset(data, 0, surf_size + sizeof(SVGA3dDXSOState));
+ }
+ else {
+ memset(data, 0, surf_size);
+ }
+ }
+ mtx_lock(&vsrf->mutex);
+ vmw_svga_winsys_buffer_unmap(&vsrf->screen->base, vsrf->buf);
+ mtx_unlock(&vsrf->mutex);
+}
+
+
+
+void *
+vmw_svga_winsys_surface_map(struct svga_winsys_context *swc,
+ struct svga_winsys_surface *srf,
+ unsigned flags, boolean *retry,
+ boolean *rebind)
+{
+ struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
+ void *data = NULL;
+ struct pb_buffer *pb_buf;
+ uint32_t pb_flags;
+ struct vmw_winsys_screen *vws = vsrf->screen;
+
+ *retry = FALSE;
+ *rebind = FALSE;
+ assert((flags & (PIPE_TRANSFER_READ | PIPE_TRANSFER_WRITE)) != 0);
+ mtx_lock(&vsrf->mutex);
+
+ if (vsrf->mapcount) {
+ /* Other mappers will get confused if we discard. */
+ flags &= ~PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
+ }
+
+ vsrf->rebind = FALSE;
+
+ /*
+ * If we intend to read, there's no point discarding the
+ * data if busy.
+ */
+ if (flags & PIPE_TRANSFER_READ || vsrf->shared)
+ flags &= ~PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
+
+ /*
+ * Discard is a hint to a synchronized map.
+ */
+ if (flags & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE)
+ flags &= ~PIPE_TRANSFER_UNSYNCHRONIZED;
+
+ /*
+ * The surface is allowed to be referenced on the command stream iff
+ * we're mapping unsynchronized or discard. This is an early check.
+ * We need to recheck after a failing discard map.
+ */
+ if (!(flags & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
+ PIPE_TRANSFER_UNSYNCHRONIZED)) &&
+ p_atomic_read(&vsrf->validated)) {
+ *retry = TRUE;
+ goto out_unlock;
+ }
+
+ pb_flags = flags & (PIPE_TRANSFER_READ_WRITE | PIPE_TRANSFER_UNSYNCHRONIZED |
+ PIPE_TRANSFER_PERSISTENT);
+
+ if (flags & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
+ struct pb_manager *provider;
+ struct pb_desc desc;
+
+ /*
+ * First, if possible, try to map existing storage with DONTBLOCK.
+ */
+ if (!p_atomic_read(&vsrf->validated)) {
+ data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf,
+ PIPE_TRANSFER_DONTBLOCK | pb_flags);
+ if (data)
+ goto out_mapped;
+ }
+
+ /*
+ * Attempt to get a new buffer.
+ */
+ provider = vws->pools.mob_fenced;
+ memset(&desc, 0, sizeof(desc));
+ desc.alignment = 4096;
+ pb_buf = provider->create_buffer(provider, vsrf->size, &desc);
+ if (pb_buf != NULL) {
+ struct svga_winsys_buffer *vbuf =
+ vmw_svga_winsys_buffer_wrap(pb_buf);
+
+ data = vmw_svga_winsys_buffer_map(&vws->base, vbuf, pb_flags);
+ if (data) {
+ vsrf->rebind = TRUE;
+ /*
+ * We've discarded data on this surface and thus
+ * it's data is no longer consider referenced.
+ */
+ vmw_swc_surface_clear_reference(swc, vsrf);
+ if (vsrf->buf)
+ vmw_svga_winsys_buffer_destroy(&vws->base, vsrf->buf);
+ vsrf->buf = vbuf;
+
+ /* Rebind persistent maps immediately */
+ if (flags & PIPE_TRANSFER_PERSISTENT) {
+ *rebind = TRUE;
+ vsrf->rebind = FALSE;
+ }
+ goto out_mapped;
+ } else
+ vmw_svga_winsys_buffer_destroy(&vws->base, vbuf);
+ }
+ /*
+ * We couldn't get and map a new buffer for some reason.
+ * Fall through to an ordinary map.
+ * But tell pipe driver to flush now if already on validate list,
+ * Otherwise we'll overwrite previous contents.
+ */
+ if (!(flags & PIPE_TRANSFER_UNSYNCHRONIZED) &&
+ p_atomic_read(&vsrf->validated)) {
+ *retry = TRUE;
+ goto out_unlock;
+ }
+ }
+
+ pb_flags |= (flags & PIPE_TRANSFER_DONTBLOCK);
+ data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf, pb_flags);
+ if (data == NULL)
+ goto out_unlock;
+
+out_mapped:
+ ++vsrf->mapcount;
+ vsrf->data = data;
+ vsrf->map_mode = flags & (PIPE_TRANSFER_READ | PIPE_TRANSFER_WRITE);
+out_unlock:
+ mtx_unlock(&vsrf->mutex);
+ return data;
+}
+
+
+void
+vmw_svga_winsys_surface_unmap(struct svga_winsys_context *swc,
+ struct svga_winsys_surface *srf,
+ boolean *rebind)
+{
+ struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
+ mtx_lock(&vsrf->mutex);
+ if (--vsrf->mapcount == 0) {
+ *rebind = vsrf->rebind;
+ vsrf->rebind = FALSE;
+ } else {
+ *rebind = FALSE;
+ }
+ vmw_svga_winsys_buffer_unmap(&vsrf->screen->base, vsrf->buf);
+ mtx_unlock(&vsrf->mutex);
+}
void
vmw_svga_winsys_surface_reference(struct vmw_svga_winsys_surface **pdst,
dst_ref = dst ? &dst->refcnt : NULL;
if (pipe_reference(dst_ref, src_ref)) {
+ if (dst->buf)
+ vmw_svga_winsys_buffer_destroy(&dst->screen->base, dst->buf);
vmw_ioctl_surface_destroy(dst->screen, dst->sid);
#ifdef DEBUG
/* to detect dangling pointers */
assert(p_atomic_read(&dst->validated) == 0);
dst->sid = SVGA3D_INVALID_ID;
#endif
+ mtx_destroy(&dst->mutex);
FREE(dst);
}