[v2,1/2] vtest: Add backing store to each resource

Submitted by Tomeu Vizoso on Aug. 3, 2018, 7:15 a.m.

Details

Message ID 20180803071545.35033-2-tomeu.vizoso@collabora.com
State New
Headers show
Series "vtest: Back resources with buffers" ( rev: 2 ) in Virgil 3D

Not browsing as part of any series.

Commit Message

Tomeu Vizoso Aug. 3, 2018, 7:15 a.m.
With the main aim of making vtest's usage of virglrenderer more similar
to that of QEMU, this patch sets a backing store to each resource and
reads from or writes to at each transfer operation.

v2: - Instead of adding a callback on resource destruction, free the
      iovec when we unref a resource as we know that we won't be doing
      anything else with it. (Dave Airlie)
---
 vtest/vtest_protocol.h |  6 +++--
 vtest/vtest_renderer.c | 61 +++++++++++++++++++++++++-----------------
 2 files changed, 41 insertions(+), 26 deletions(-)

Patch hide | download patch | download mbox

diff --git a/vtest/vtest_protocol.h b/vtest/vtest_protocol.h
index f6176437e6e9..1d3e426998d1 100644
--- a/vtest/vtest_protocol.h
+++ b/vtest/vtest_protocol.h
@@ -54,7 +54,7 @@ 
 /* 0 length cmd */
 /* resp VCMD_GET_CAPS + caps */
 
-#define VCMD_RES_CREATE_SIZE 10
+#define VCMD_RES_CREATE_SIZE 11
 #define VCMD_RES_CREATE_RES_HANDLE 0
 #define VCMD_RES_CREATE_TARGET 1
 #define VCMD_RES_CREATE_FORMAT 2
@@ -65,11 +65,12 @@ 
 #define VCMD_RES_CREATE_ARRAY_SIZE 7
 #define VCMD_RES_CREATE_LAST_LEVEL 8
 #define VCMD_RES_CREATE_NR_SAMPLES 9
+#define VCMD_RES_CREATE_DATA_SIZE 10
 
 #define VCMD_RES_UNREF_SIZE 1
 #define VCMD_RES_UNREF_RES_HANDLE 0
 
-#define VCMD_TRANSFER_HDR_SIZE 11
+#define VCMD_TRANSFER_HDR_SIZE 12
 #define VCMD_TRANSFER_RES_HANDLE 0
 #define VCMD_TRANSFER_LEVEL 1
 #define VCMD_TRANSFER_STRIDE 2
@@ -81,6 +82,7 @@ 
 #define VCMD_TRANSFER_HEIGHT 8
 #define VCMD_TRANSFER_DEPTH 9
 #define VCMD_TRANSFER_DATA_SIZE 10
+#define VCMD_TRANSFER_OFFSET 11
 
 #define VCMD_BUSY_WAIT_FLAG_WAIT 1
 
diff --git a/vtest/vtest_renderer.c b/vtest/vtest_renderer.c
index 8f0262a83b20..af2ebf1cef43 100644
--- a/vtest/vtest_renderer.c
+++ b/vtest/vtest_renderer.c
@@ -35,9 +35,11 @@ 
 #include "vtest_protocol.h"
 #include "util.h"
 #include "util/u_debug.h"
+#include "util/u_memory.h"
 
 static int ctx_id = 1;
 static int fence_id = 1;
+static struct iovec iovecs[1024] = {0,};
 
 static int last_fence;
 static void vtest_write_fence(UNUSED void *cookie, uint32_t fence_id_in)
@@ -234,6 +236,7 @@  int vtest_create_resource(void)
 {
     uint32_t res_create_buf[VCMD_RES_CREATE_SIZE];
     struct virgl_renderer_resource_create_args args;
+    struct iovec *iovec;
     int ret;
 
     ret = vtest_block_read(renderer.in_fd, &res_create_buf, sizeof(res_create_buf));
@@ -254,8 +257,24 @@  int vtest_create_resource(void)
     args.flags = 0;
 
     ret = virgl_renderer_resource_create(&args, NULL, 0);
+    if (ret)
+      return ret;
 
     virgl_renderer_ctx_attach_resource(ctx_id, args.handle);
+
+    iovec = CALLOC_STRUCT(iovec);
+    if (!iovec)
+      return -ENOMEM;
+
+    iovec->iov_len = res_create_buf[VCMD_RES_CREATE_DATA_SIZE];
+    iovec->iov_base = calloc(1, iovec->iov_len);
+    if (!iovec->iov_base)
+      return -ENOMEM;
+
+    virgl_renderer_resource_attach_iov(args.handle, iovec, 1);
+    assert(args.handle < ARRAY_SIZE(iovecs));
+    iovecs[args.handle] = *iovec;
+
     return ret;
 }
 
@@ -271,6 +290,13 @@  int vtest_resource_unref(void)
 
     handle = res_unref_buf[VCMD_RES_UNREF_RES_HANDLE];
     virgl_renderer_ctx_attach_resource(ctx_id, handle);
+
+    virgl_renderer_resource_detach_iov(handle, NULL, NULL);
+    if (iovecs[handle].iov_base != NULL)
+        free(iovecs[handle].iov_base);
+    iovecs[handle].iov_base = NULL;
+    iovecs[handle].iov_len = 0;
+
     virgl_renderer_resource_unref(handle);
     return 0;
 }
@@ -312,6 +338,7 @@  int vtest_submit_cmd(uint32_t length_dw)
   box.h = thdr_buf[VCMD_TRANSFER_HEIGHT];		\
   box.d = thdr_buf[VCMD_TRANSFER_DEPTH];		\
   data_size = thdr_buf[VCMD_TRANSFER_DATA_SIZE];		\
+  offset = thdr_buf[VCMD_TRANSFER_OFFSET];		\
   } while(0)
 
 
@@ -323,8 +350,7 @@  int vtest_transfer_get(UNUSED uint32_t length_dw)
     uint32_t stride, layer_stride, handle;
     struct virgl_box box;
     uint32_t data_size;
-    void *ptr;
-    struct iovec iovec;
+    uint32_t offset;
 
     ret = vtest_block_read(renderer.in_fd, thdr_buf, VCMD_TRANSFER_HDR_SIZE * 4);
     if (ret != VCMD_TRANSFER_HDR_SIZE * 4)
@@ -332,25 +358,20 @@  int vtest_transfer_get(UNUSED uint32_t length_dw)
 
     DECODE_TRANSFER;
 
-    ptr = malloc(data_size);
-    if (!ptr)
-      return -ENOMEM;
-
-    iovec.iov_len = data_size;
-    iovec.iov_base = ptr;
     ret = virgl_renderer_transfer_read_iov(handle,
 				     ctx_id,
 				     level,
 				     stride,
 				     layer_stride,
 				     &box,
-				     0,
-				     &iovec, 1);
+				     offset,
+				     NULL, 0);
     if (ret)
       fprintf(stderr," transfer read failed %d\n", ret);
-    ret = vtest_block_write(renderer.out_fd, ptr, data_size);
 
-    free(ptr);
+    ret = vtest_block_write(renderer.out_fd,
+                            iovecs[handle].iov_base + offset,
+                            data_size);
     return ret < 0 ? ret : 0;
 }
 
@@ -362,8 +383,7 @@  int vtest_transfer_put(UNUSED uint32_t length_dw)
     uint32_t stride, layer_stride, handle;
     struct virgl_box box;
     uint32_t data_size;
-    void *ptr;
-    struct iovec iovec;
+    uint32_t offset;
 
     ret = vtest_block_read(renderer.in_fd, thdr_buf, VCMD_TRANSFER_HDR_SIZE * 4);
     if (ret != VCMD_TRANSFER_HDR_SIZE * 4)
@@ -371,27 +391,20 @@  int vtest_transfer_put(UNUSED uint32_t length_dw)
 
     DECODE_TRANSFER;
 
-    ptr = malloc(data_size);
-    if (!ptr)
-      return -ENOMEM;
-
-    ret = vtest_block_read(renderer.in_fd, ptr, data_size);
+    ret = vtest_block_read(renderer.in_fd, iovecs[handle].iov_base + offset, data_size);
     if (ret < 0)
       return ret;
 
-    iovec.iov_len = data_size;
-    iovec.iov_base = ptr;
     ret = virgl_renderer_transfer_write_iov(handle,
 					    ctx_id,
 					    level,
 					    stride,
 					    layer_stride,
 					    &box,
-					    0,
-					    &iovec, 1);
+					    offset,
+					    NULL, 0);
     if (ret)
       fprintf(stderr," transfer write failed %d\n", ret);
-    free(ptr);
     return 0;
 }