[virglrenderer-devel] [PATCH v2 3/4] vtest: refactorized run_renderer function

Nathan Gauer nathan at gauer.org
Fri Jul 27 09:52:37 UTC 2018


run_renderer now uses an array to get the corresponding function
pointer for a command.
Some vtest_* function headers had to be uniformized.

Signed-off-by: Nathan Gauer <nathan at gauer.org>
---
 vtest/vtest.h          |   8 +--
 vtest/vtest_renderer.c |  18 +++++--
 vtest/vtest_server.c   | 115 ++++++++++++++++++++++-------------------
 3 files changed, 80 insertions(+), 61 deletions(-)

diff --git a/vtest/vtest.h b/vtest/vtest.h
index 327c193..cff4cdb 100644
--- a/vtest/vtest.h
+++ b/vtest/vtest.h
@@ -27,10 +27,10 @@
 #include <errno.h>
 int vtest_create_renderer(int in_fd, int out_fd, uint32_t length);
 
-int vtest_send_caps(void);
+int vtest_send_caps(uint32_t length_dw);
 int vtest_send_caps2(void);
-int vtest_create_resource(void);
-int vtest_resource_unref(void);
+int vtest_create_resource(uint32_t length_dw);
+int vtest_resource_unref(uint32_t length_dw);
 int vtest_submit_cmd(uint32_t length_dw);
 
 int vtest_transfer_get(uint32_t length_dw);
@@ -38,7 +38,7 @@ int vtest_transfer_put(uint32_t length_dw);
 
 int vtest_block_read(int fd, void *buf, int size);
 
-int vtest_resource_busy_wait(void);
+int vtest_resource_busy_wait(uint32_t length_dw);
 int vtest_renderer_create_fence(void);
 int vtest_poll(void);
 
diff --git a/vtest/vtest_renderer.c b/vtest/vtest_renderer.c
index 08b74bf..bcc1a8d 100644
--- a/vtest/vtest_renderer.c
+++ b/vtest/vtest_renderer.c
@@ -35,6 +35,7 @@
 #include "vtest_protocol.h"
 #include "util.h"
 #include "util/u_debug.h"
+#include "util/macros.h"
 
 static int ctx_id = 1;
 static int fence_id = 1;
@@ -185,12 +186,13 @@ end:
     return 0;
 }
 
-int vtest_send_caps(void)
+int vtest_send_caps(uint32_t length_dw)
 {
     uint32_t  max_ver, max_size;
     void *caps_buf;
     uint32_t hdr_buf[2];
     int ret;
+    UNUSED_PARAMETER(length_dw);
 
     virgl_renderer_get_cap_set(1, &max_ver, &max_size);
 
@@ -214,12 +216,14 @@ end:
     return 0;
 }
 
-int vtest_create_resource(void)
+int vtest_create_resource(uint32_t length_dw)
 {
     uint32_t res_create_buf[VCMD_RES_CREATE_SIZE];
     struct virgl_renderer_resource_create_args args;
     int ret;
 
+    UNUSED_PARAMETER(length_dw);
+
     ret = vtest_block_read(renderer.in_fd, &res_create_buf, sizeof(res_create_buf));
     if (ret != sizeof(res_create_buf))
 	return -1;
@@ -243,12 +247,14 @@ int vtest_create_resource(void)
     return ret;
 }
 
-int vtest_resource_unref(void)
+int vtest_resource_unref(uint32_t length_dw)
 {
     uint32_t res_unref_buf[VCMD_RES_UNREF_SIZE];
     int ret;
     uint32_t handle;
 
+    UNUSED_PARAMETER(length_dw);
+
     ret = vtest_block_read(renderer.in_fd, &res_unref_buf, sizeof(res_unref_buf));
     if (ret != sizeof(res_unref_buf))
       return -1;
@@ -379,7 +385,7 @@ int vtest_transfer_put(UNUSED uint32_t length_dw)
     return 0;
 }
 
-int vtest_resource_busy_wait(void)
+int vtest_resource_busy_wait(uint32_t length_dw)
 {
   uint32_t bw_buf[VCMD_BUSY_WAIT_SIZE];
   int ret, fd;
@@ -387,6 +393,10 @@ int vtest_resource_busy_wait(void)
   uint32_t hdr_buf[VTEST_HDR_SIZE];
   uint32_t reply_buf[1];
   bool busy = false;
+
+  UNUSED_PARAMETER(length_dw);
+
+  vtest_renderer_create_fence();
   ret = vtest_block_read(renderer.in_fd, &bw_buf, sizeof(bw_buf));
   if (ret != sizeof(bw_buf))
     return -1;
diff --git a/vtest/vtest_server.c b/vtest/vtest_server.c
index 3868fe3..51db8bd 100644
--- a/vtest/vtest_server.c
+++ b/vtest/vtest_server.c
@@ -33,6 +33,7 @@
 #include <fcntl.h>
 
 #include "util.h"
+#include "util/macros.h"
 #include "vtest.h"
 #include "vtest_protocol.h"
 
@@ -86,70 +87,78 @@ static int wait_for_socket_accept(int sock)
     return -1;
 }
 
+typedef int (*vtest_cmd_fptr)(uint32_t);
+
+static const vtest_cmd_fptr vtest_commands[] = {
+   NULL /* CMD ids starts at 1 */,
+   vtest_send_caps,
+   vtest_create_resource,
+   vtest_resource_unref,
+   vtest_transfer_get,
+   vtest_transfer_put,
+   vtest_submit_cmd,
+   vtest_resource_busy_wait,
+   NULL /* vtest_create_renderer is a special case */,
+#ifdef WITH_VULKAN
+   /* add Vulkan vtest_cmds here */
+#endif
+};
+
 static int run_renderer(int in_fd, int out_fd)
 {
-    int ret;
+    int err, ret;
     uint32_t header[VTEST_HDR_SIZE];
-    bool inited = false;
-again:
-    ret = vtest_wait_for_fd_read(in_fd);
-    if (ret < 0)
-      goto fail;
+    int initialized = 0;
+
+    do {
+      ret = vtest_wait_for_fd_read(in_fd);
+      if (ret < 0) {
+         err = 1;
+         break;
+      }
 
-    ret = vtest_block_read(in_fd, &header, sizeof(header));
+      ret = vtest_block_read(in_fd, &header, sizeof(header));
+      if (ret < 0 || (size_t)ret < sizeof(header)) {
+         err = 2;
+         break;
+      }
+
+      if (!initialized) {
+         /* The first command MUST be VCMD_CREATE_RENDERER */
+         if (header[1] != VCMD_CREATE_RENDERER) {
+            err = 3;
+            break;
+         }
 
-    if (ret == 8) {
-      if (!inited) {
-	if (header[1] != VCMD_CREATE_RENDERER)
-	  goto fail;
-	ret = vtest_create_renderer(in_fd, out_fd, header[0]);
-	inited = true;
+         ret = vtest_create_renderer(in_fd, out_fd, header[0]);
+         initialized = 1;
+         vtest_poll();
+         continue;
       }
+
       vtest_poll();
-      switch (header[1]) {
-      case VCMD_GET_CAPS:
-	ret = vtest_send_caps();
-	break;
-      case VCMD_RESOURCE_CREATE:
-	ret = vtest_create_resource();
-	break;
-      case VCMD_RESOURCE_UNREF:
-	ret = vtest_resource_unref();
-	break;
-      case VCMD_SUBMIT_CMD:
-	ret = vtest_submit_cmd(header[0]);
-	break;
-      case VCMD_TRANSFER_GET:
-	ret = vtest_transfer_get(header[0]);
-	break;
-      case VCMD_TRANSFER_PUT:
-	ret = vtest_transfer_put(header[0]);
-	break;
-      case VCMD_RESOURCE_BUSY_WAIT:
-        vtest_renderer_create_fence();
-	ret = vtest_resource_busy_wait();
-	break;
-      case VCMD_GET_CAPS2:
-	ret = vtest_send_caps2();
-	break;
-      default:
-	break;
+      if (header[1] <= 0 || header[1] >= ARRAY_SIZE(vtest_commands)
+         || vtest_commands[header[1]] == NULL) {
+
+#if DEBUG
+         fprintf(stderr, "invalid command received. Ignoring\n");
+#endif
+         continue;
       }
 
-      if (ret < 0) {
-	goto fail;
+      ret = vtest_commands[header[1]](header[0]);
+      if (ret != 0) {
+         err = 6;
+         break;
       }
+   } while (1);
 
-      goto again;
-    }
-    if (ret <= 0) {
-      goto fail;
-    }
-fail:
-    fprintf(stderr, "socket failed - closing renderer\n");
-    vtest_destroy_renderer();
-    close(in_fd);
-    return 0;
+   fprintf(stderr, "socket failed (%d) - closing renderer\n", err);
+
+   vtest_destroy_renderer();
+   close(in_fd);
+
+   return 0;
 }
 
 int main(int argc, char **argv)
-- 
2.18.0



More information about the virglrenderer-devel mailing list