• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014, 2015 Red Hat.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <sys/socket.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <netinet/in.h>
28 #include <sys/un.h>
29 #include <unistd.h>
30 
31 #include <os/os_process.h>
32 #include <util/format/u_format.h>
33 
34 #include "virgl_vtest_winsys.h"
35 #include "virgl_vtest_public.h"
36 
37 /* block read/write routines */
virgl_block_write(int fd,void * buf,int size)38 static int virgl_block_write(int fd, void *buf, int size)
39 {
40    void *ptr = buf;
41    int left;
42    int ret;
43    left = size;
44    do {
45       ret = write(fd, ptr, left);
46       if (ret < 0)
47          return -errno;
48       left -= ret;
49       ptr += ret;
50    } while (left);
51    return size;
52 }
53 
virgl_block_read(int fd,void * buf,int size)54 static int virgl_block_read(int fd, void *buf, int size)
55 {
56    void *ptr = buf;
57    int left;
58    int ret;
59    left = size;
60    do {
61       ret = read(fd, ptr, left);
62       if (ret <= 0) {
63          fprintf(stderr,
64                  "lost connection to rendering server on %d read %d %d\n",
65                  size, ret, errno);
66          abort();
67          return ret < 0 ? -errno : 0;
68       }
69       left -= ret;
70       ptr += ret;
71    } while (left);
72    return size;
73 }
74 
virgl_vtest_receive_fd(int socket_fd)75 static int virgl_vtest_receive_fd(int socket_fd)
76 {
77     struct cmsghdr *cmsgh;
78     struct msghdr msgh = { 0 };
79     char buf[CMSG_SPACE(sizeof(int))], c;
80     struct iovec iovec;
81 
82     iovec.iov_base = &c;
83     iovec.iov_len = sizeof(char);
84 
85     msgh.msg_name = NULL;
86     msgh.msg_namelen = 0;
87     msgh.msg_iov = &iovec;
88     msgh.msg_iovlen = 1;
89     msgh.msg_control = buf;
90     msgh.msg_controllen = sizeof(buf);
91     msgh.msg_flags = 0;
92 
93     int size = recvmsg(socket_fd, &msgh, 0);
94     if (size < 0) {
95       fprintf(stderr, "Failed with %s\n", strerror(errno));
96       return -1;
97     }
98 
99     cmsgh = CMSG_FIRSTHDR(&msgh);
100     if (!cmsgh) {
101       fprintf(stderr, "No headers available\n");
102       return -1;
103     }
104 
105     if (cmsgh->cmsg_level != SOL_SOCKET) {
106       fprintf(stderr, "invalid cmsg_level %d\n", cmsgh->cmsg_level);
107       return -1;
108     }
109 
110     if (cmsgh->cmsg_type != SCM_RIGHTS) {
111       fprintf(stderr, "invalid cmsg_type %d\n", cmsgh->cmsg_type);
112       return -1;
113     }
114 
115     return *((int *) CMSG_DATA(cmsgh));
116 }
117 
virgl_vtest_send_init(struct virgl_vtest_winsys * vws)118 static int virgl_vtest_send_init(struct virgl_vtest_winsys *vws)
119 {
120    uint32_t buf[VTEST_HDR_SIZE];
121    const char *nstr = "virtest";
122    char cmdline[64];
123    int ret;
124 
125    ret = os_get_process_name(cmdline, 63);
126    if (ret == FALSE)
127       strcpy(cmdline, nstr);
128 #if defined(HAVE_PROGRAM_INVOCATION_NAME)
129    if (!strcmp(cmdline, "shader_runner")) {
130       const char *name;
131       /* hack to get better testname */
132       name = program_invocation_short_name;
133       name += strlen(name) + 1;
134       strncpy(cmdline, name, 63);
135    }
136 #endif
137    buf[VTEST_CMD_LEN] = strlen(cmdline) + 1;
138    buf[VTEST_CMD_ID] = VCMD_CREATE_RENDERER;
139 
140    virgl_block_write(vws->sock_fd, &buf, sizeof(buf));
141    virgl_block_write(vws->sock_fd, (void *)cmdline, strlen(cmdline) + 1);
142    return 0;
143 }
144 
virgl_vtest_negotiate_version(struct virgl_vtest_winsys * vws)145 static int virgl_vtest_negotiate_version(struct virgl_vtest_winsys *vws)
146 {
147    uint32_t vtest_hdr[VTEST_HDR_SIZE];
148    uint32_t version_buf[VCMD_PROTOCOL_VERSION_SIZE];
149    uint32_t busy_wait_buf[VCMD_BUSY_WAIT_SIZE];
150    uint32_t busy_wait_result[1];
151    ASSERTED int ret;
152 
153    vtest_hdr[VTEST_CMD_LEN] = VCMD_PING_PROTOCOL_VERSION_SIZE;
154    vtest_hdr[VTEST_CMD_ID] = VCMD_PING_PROTOCOL_VERSION;
155    virgl_block_write(vws->sock_fd, &vtest_hdr, sizeof(vtest_hdr));
156 
157    vtest_hdr[VTEST_CMD_LEN] = VCMD_BUSY_WAIT_SIZE;
158    vtest_hdr[VTEST_CMD_ID] = VCMD_RESOURCE_BUSY_WAIT;
159    busy_wait_buf[VCMD_BUSY_WAIT_HANDLE] = 0;
160    busy_wait_buf[VCMD_BUSY_WAIT_FLAGS] = 0;
161    virgl_block_write(vws->sock_fd, &vtest_hdr, sizeof(vtest_hdr));
162    virgl_block_write(vws->sock_fd, &busy_wait_buf, sizeof(busy_wait_buf));
163 
164    ret = virgl_block_read(vws->sock_fd, vtest_hdr, sizeof(vtest_hdr));
165    assert(ret);
166 
167    if (vtest_hdr[VTEST_CMD_ID] == VCMD_PING_PROTOCOL_VERSION) {
168      /* Read dummy busy_wait response */
169      ret = virgl_block_read(vws->sock_fd, vtest_hdr, sizeof(vtest_hdr));
170      assert(ret);
171      ret = virgl_block_read(vws->sock_fd, busy_wait_result, sizeof(busy_wait_result));
172      assert(ret);
173 
174      vtest_hdr[VTEST_CMD_LEN] = VCMD_PROTOCOL_VERSION_SIZE;
175      vtest_hdr[VTEST_CMD_ID] = VCMD_PROTOCOL_VERSION;
176      version_buf[VCMD_PROTOCOL_VERSION_VERSION] = VTEST_PROTOCOL_VERSION;
177      virgl_block_write(vws->sock_fd, &vtest_hdr, sizeof(vtest_hdr));
178      virgl_block_write(vws->sock_fd, &version_buf, sizeof(version_buf));
179 
180      ret = virgl_block_read(vws->sock_fd, vtest_hdr, sizeof(vtest_hdr));
181      assert(ret);
182      ret = virgl_block_read(vws->sock_fd, version_buf, sizeof(version_buf));
183      assert(ret);
184      return version_buf[VCMD_PROTOCOL_VERSION_VERSION];
185    }
186 
187    /* Read dummy busy_wait response */
188    assert(vtest_hdr[VTEST_CMD_ID] == VCMD_RESOURCE_BUSY_WAIT);
189    ret = virgl_block_read(vws->sock_fd, busy_wait_result, sizeof(busy_wait_result));
190    assert(ret);
191 
192    /* Old server, return version 0 */
193    return 0;
194 }
195 
virgl_vtest_connect(struct virgl_vtest_winsys * vws)196 int virgl_vtest_connect(struct virgl_vtest_winsys *vws)
197 {
198    struct sockaddr_un un;
199    int sock, ret;
200 
201    sock = socket(PF_UNIX, SOCK_STREAM, 0);
202    if (sock < 0)
203       return -1;
204 
205    memset(&un, 0, sizeof(un));
206    un.sun_family = AF_UNIX;
207    snprintf(un.sun_path, sizeof(un.sun_path), "%s", VTEST_DEFAULT_SOCKET_NAME);
208 
209    do {
210       ret = 0;
211       if (connect(sock, (struct sockaddr *)&un, sizeof(un)) < 0) {
212          ret = -errno;
213       }
214    } while (ret == -EINTR);
215 
216    vws->sock_fd = sock;
217    virgl_vtest_send_init(vws);
218    vws->protocol_version = virgl_vtest_negotiate_version(vws);
219 
220    /* Version 1 is deprecated. */
221    if (vws->protocol_version == 1)
222       vws->protocol_version = 0;
223 
224    return 0;
225 }
226 
virgl_vtest_send_get_caps(struct virgl_vtest_winsys * vws,struct virgl_drm_caps * caps)227 int virgl_vtest_send_get_caps(struct virgl_vtest_winsys *vws,
228                               struct virgl_drm_caps *caps)
229 {
230    uint32_t get_caps_buf[VTEST_HDR_SIZE * 2];
231    uint32_t resp_buf[VTEST_HDR_SIZE];
232    uint32_t caps_size = sizeof(struct virgl_caps_v2);
233    int ret;
234    get_caps_buf[VTEST_CMD_LEN] = 0;
235    get_caps_buf[VTEST_CMD_ID] = VCMD_GET_CAPS2;
236    get_caps_buf[VTEST_CMD_LEN + 2] = 0;
237    get_caps_buf[VTEST_CMD_ID + 2] = VCMD_GET_CAPS;
238 
239    virgl_block_write(vws->sock_fd, &get_caps_buf, sizeof(get_caps_buf));
240 
241    ret = virgl_block_read(vws->sock_fd, resp_buf, sizeof(resp_buf));
242    if (ret <= 0)
243       return 0;
244 
245    if (resp_buf[1] == 2) {
246        struct virgl_caps_v1 dummy;
247        uint32_t resp_size = resp_buf[0] - 1;
248        uint32_t dummy_size = 0;
249        if (resp_size > caps_size) {
250 	   dummy_size = resp_size - caps_size;
251 	   resp_size = caps_size;
252        }
253 
254        ret = virgl_block_read(vws->sock_fd, &caps->caps, resp_size);
255 
256        if (dummy_size)
257 	   ret = virgl_block_read(vws->sock_fd, &dummy, dummy_size);
258 
259        /* now read back the pointless caps v1 we requested */
260        ret = virgl_block_read(vws->sock_fd, resp_buf, sizeof(resp_buf));
261        if (ret <= 0)
262 	   return 0;
263        ret = virgl_block_read(vws->sock_fd, &dummy, sizeof(struct virgl_caps_v1));
264    } else
265        ret = virgl_block_read(vws->sock_fd, &caps->caps, sizeof(struct virgl_caps_v1));
266 
267    return 0;
268 }
269 
virgl_vtest_send_resource_create2(struct virgl_vtest_winsys * vws,uint32_t handle,enum pipe_texture_target target,uint32_t format,uint32_t bind,uint32_t width,uint32_t height,uint32_t depth,uint32_t array_size,uint32_t last_level,uint32_t nr_samples,uint32_t size,int * out_fd)270 static int virgl_vtest_send_resource_create2(struct virgl_vtest_winsys *vws,
271                                              uint32_t handle,
272                                              enum pipe_texture_target target,
273                                              uint32_t format,
274                                              uint32_t bind,
275                                              uint32_t width,
276                                              uint32_t height,
277                                              uint32_t depth,
278                                              uint32_t array_size,
279                                              uint32_t last_level,
280                                              uint32_t nr_samples,
281                                              uint32_t size,
282                                              int *out_fd)
283 {
284    uint32_t res_create_buf[VCMD_RES_CREATE2_SIZE], vtest_hdr[VTEST_HDR_SIZE];
285 
286    vtest_hdr[VTEST_CMD_LEN] = VCMD_RES_CREATE2_SIZE;
287    vtest_hdr[VTEST_CMD_ID] = VCMD_RESOURCE_CREATE2;
288 
289    res_create_buf[VCMD_RES_CREATE2_RES_HANDLE] = handle;
290    res_create_buf[VCMD_RES_CREATE2_TARGET] = target;
291    res_create_buf[VCMD_RES_CREATE2_FORMAT] = format;
292    res_create_buf[VCMD_RES_CREATE2_BIND] = bind;
293    res_create_buf[VCMD_RES_CREATE2_WIDTH] = width;
294    res_create_buf[VCMD_RES_CREATE2_HEIGHT] = height;
295    res_create_buf[VCMD_RES_CREATE2_DEPTH] = depth;
296    res_create_buf[VCMD_RES_CREATE2_ARRAY_SIZE] = array_size;
297    res_create_buf[VCMD_RES_CREATE2_LAST_LEVEL] = last_level;
298    res_create_buf[VCMD_RES_CREATE2_NR_SAMPLES] = nr_samples;
299    res_create_buf[VCMD_RES_CREATE2_DATA_SIZE] = size;
300 
301    virgl_block_write(vws->sock_fd, &vtest_hdr, sizeof(vtest_hdr));
302    virgl_block_write(vws->sock_fd, &res_create_buf, sizeof(res_create_buf));
303 
304    /* Multi-sampled textures have no backing store attached. */
305    if (size == 0)
306       return 0;
307 
308    *out_fd = virgl_vtest_receive_fd(vws->sock_fd);
309    if (*out_fd < 0) {
310       fprintf(stderr, "failed to get fd\n");
311       return -1;
312    }
313 
314    return 0;
315 }
316 
virgl_vtest_send_resource_create(struct virgl_vtest_winsys * vws,uint32_t handle,enum pipe_texture_target target,uint32_t format,uint32_t bind,uint32_t width,uint32_t height,uint32_t depth,uint32_t array_size,uint32_t last_level,uint32_t nr_samples,uint32_t size,int * out_fd)317 int virgl_vtest_send_resource_create(struct virgl_vtest_winsys *vws,
318                                      uint32_t handle,
319                                      enum pipe_texture_target target,
320                                      uint32_t format,
321                                      uint32_t bind,
322                                      uint32_t width,
323                                      uint32_t height,
324                                      uint32_t depth,
325                                      uint32_t array_size,
326                                      uint32_t last_level,
327                                      uint32_t nr_samples,
328                                      uint32_t size,
329                                      int *out_fd)
330 {
331    uint32_t res_create_buf[VCMD_RES_CREATE_SIZE], vtest_hdr[VTEST_HDR_SIZE];
332 
333    if (vws->protocol_version >= 2)
334       return virgl_vtest_send_resource_create2(vws, handle, target, format,
335                                                bind, width, height, depth,
336                                                array_size, last_level,
337                                                nr_samples, size, out_fd);
338 
339    vtest_hdr[VTEST_CMD_LEN] = VCMD_RES_CREATE_SIZE;
340    vtest_hdr[VTEST_CMD_ID] = VCMD_RESOURCE_CREATE;
341 
342    res_create_buf[VCMD_RES_CREATE_RES_HANDLE] = handle;
343    res_create_buf[VCMD_RES_CREATE_TARGET] = target;
344    res_create_buf[VCMD_RES_CREATE_FORMAT] = format;
345    res_create_buf[VCMD_RES_CREATE_BIND] = bind;
346    res_create_buf[VCMD_RES_CREATE_WIDTH] = width;
347    res_create_buf[VCMD_RES_CREATE_HEIGHT] = height;
348    res_create_buf[VCMD_RES_CREATE_DEPTH] = depth;
349    res_create_buf[VCMD_RES_CREATE_ARRAY_SIZE] = array_size;
350    res_create_buf[VCMD_RES_CREATE_LAST_LEVEL] = last_level;
351    res_create_buf[VCMD_RES_CREATE_NR_SAMPLES] = nr_samples;
352 
353    virgl_block_write(vws->sock_fd, &vtest_hdr, sizeof(vtest_hdr));
354    virgl_block_write(vws->sock_fd, &res_create_buf, sizeof(res_create_buf));
355 
356    return 0;
357 }
358 
virgl_vtest_submit_cmd(struct virgl_vtest_winsys * vws,struct virgl_vtest_cmd_buf * cbuf)359 int virgl_vtest_submit_cmd(struct virgl_vtest_winsys *vws,
360                            struct virgl_vtest_cmd_buf *cbuf)
361 {
362    uint32_t vtest_hdr[VTEST_HDR_SIZE];
363 
364    vtest_hdr[VTEST_CMD_LEN] = cbuf->base.cdw;
365    vtest_hdr[VTEST_CMD_ID] = VCMD_SUBMIT_CMD;
366 
367    virgl_block_write(vws->sock_fd, &vtest_hdr, sizeof(vtest_hdr));
368    virgl_block_write(vws->sock_fd, cbuf->buf, cbuf->base.cdw * 4);
369    return 0;
370 }
371 
virgl_vtest_send_resource_unref(struct virgl_vtest_winsys * vws,uint32_t handle)372 int virgl_vtest_send_resource_unref(struct virgl_vtest_winsys *vws,
373                                     uint32_t handle)
374 {
375    uint32_t vtest_hdr[VTEST_HDR_SIZE];
376    uint32_t cmd[1];
377    vtest_hdr[VTEST_CMD_LEN] = 1;
378    vtest_hdr[VTEST_CMD_ID] = VCMD_RESOURCE_UNREF;
379 
380    cmd[0] = handle;
381    virgl_block_write(vws->sock_fd, &vtest_hdr, sizeof(vtest_hdr));
382    virgl_block_write(vws->sock_fd, &cmd, sizeof(cmd));
383    return 0;
384 }
385 
virgl_vtest_send_transfer_cmd(struct virgl_vtest_winsys * vws,uint32_t vcmd,uint32_t handle,uint32_t level,uint32_t stride,uint32_t layer_stride,const struct pipe_box * box,uint32_t data_size)386 static int virgl_vtest_send_transfer_cmd(struct virgl_vtest_winsys *vws,
387                                   uint32_t vcmd,
388                                   uint32_t handle,
389                                   uint32_t level, uint32_t stride,
390                                   uint32_t layer_stride,
391                                   const struct pipe_box *box,
392                                   uint32_t data_size)
393 {
394    uint32_t vtest_hdr[VTEST_HDR_SIZE];
395    uint32_t cmd[VCMD_TRANSFER_HDR_SIZE];
396    vtest_hdr[VTEST_CMD_LEN] = VCMD_TRANSFER_HDR_SIZE;
397    vtest_hdr[VTEST_CMD_ID] = vcmd;
398 
399    /* The host expects the size in dwords so calculate the rounded up
400     * value here. */
401    if (vcmd == VCMD_TRANSFER_PUT)
402       vtest_hdr[VTEST_CMD_LEN] += (data_size + 3) / 4;
403 
404    cmd[0] = handle;
405    cmd[1] = level;
406    cmd[2] = stride;
407    cmd[3] = layer_stride;
408    cmd[4] = box->x;
409    cmd[5] = box->y;
410    cmd[6] = box->z;
411    cmd[7] = box->width;
412    cmd[8] = box->height;
413    cmd[9] = box->depth;
414    cmd[10] = data_size;
415    virgl_block_write(vws->sock_fd, &vtest_hdr, sizeof(vtest_hdr));
416    virgl_block_write(vws->sock_fd, &cmd, sizeof(cmd));
417 
418    return 0;
419 }
420 
virgl_vtest_send_transfer_cmd2(struct virgl_vtest_winsys * vws,uint32_t vcmd,uint32_t handle,uint32_t level,const struct pipe_box * box,uint32_t data_size,uint32_t offset)421 static int virgl_vtest_send_transfer_cmd2(struct virgl_vtest_winsys *vws,
422                                   uint32_t vcmd,
423                                   uint32_t handle,
424                                   uint32_t level,
425                                   const struct pipe_box *box,
426                                   uint32_t data_size,
427                                   uint32_t offset)
428 {
429    uint32_t vtest_hdr[VTEST_HDR_SIZE];
430    uint32_t cmd[VCMD_TRANSFER2_HDR_SIZE];
431    vtest_hdr[VTEST_CMD_LEN] = VCMD_TRANSFER2_HDR_SIZE;
432    vtest_hdr[VTEST_CMD_ID] = vcmd;
433 
434    /* The host expects the size in dwords so calculate the rounded up
435     * value here. */
436    if (vcmd == VCMD_TRANSFER_PUT2)
437       vtest_hdr[VTEST_CMD_LEN] += (data_size + 3) / 4;
438 
439    cmd[VCMD_TRANSFER2_RES_HANDLE] = handle;
440    cmd[VCMD_TRANSFER2_LEVEL] = level;
441    cmd[VCMD_TRANSFER2_X] = box->x;
442    cmd[VCMD_TRANSFER2_Y] = box->y;
443    cmd[VCMD_TRANSFER2_Z] = box->z;
444    cmd[VCMD_TRANSFER2_WIDTH] = box->width;
445    cmd[VCMD_TRANSFER2_HEIGHT] = box->height;
446    cmd[VCMD_TRANSFER2_DEPTH] = box->depth;
447    cmd[VCMD_TRANSFER2_DATA_SIZE] = data_size;
448    cmd[VCMD_TRANSFER2_OFFSET] = offset;
449    virgl_block_write(vws->sock_fd, &vtest_hdr, sizeof(vtest_hdr));
450    virgl_block_write(vws->sock_fd, &cmd, sizeof(cmd));
451 
452    return 0;
453 }
454 
virgl_vtest_send_transfer_get(struct virgl_vtest_winsys * vws,uint32_t handle,uint32_t level,uint32_t stride,uint32_t layer_stride,const struct pipe_box * box,uint32_t data_size,uint32_t offset)455 int virgl_vtest_send_transfer_get(struct virgl_vtest_winsys *vws,
456                                   uint32_t handle,
457                                   uint32_t level, uint32_t stride,
458                                   uint32_t layer_stride,
459                                   const struct pipe_box *box,
460                                   uint32_t data_size,
461                                   uint32_t offset)
462 {
463    if (vws->protocol_version < 2)
464       return virgl_vtest_send_transfer_cmd(vws, VCMD_TRANSFER_GET, handle,
465                                            level, stride, layer_stride, box,
466                                            data_size);
467 
468    return virgl_vtest_send_transfer_cmd2(vws, VCMD_TRANSFER_GET2, handle,
469                                         level, box, data_size, offset);
470 }
471 
virgl_vtest_send_transfer_put(struct virgl_vtest_winsys * vws,uint32_t handle,uint32_t level,uint32_t stride,uint32_t layer_stride,const struct pipe_box * box,uint32_t data_size,uint32_t offset)472 int virgl_vtest_send_transfer_put(struct virgl_vtest_winsys *vws,
473                                   uint32_t handle,
474                                   uint32_t level, uint32_t stride,
475                                   uint32_t layer_stride,
476                                   const struct pipe_box *box,
477                                   uint32_t data_size,
478                                   uint32_t offset)
479 {
480    if (vws->protocol_version < 2)
481       return virgl_vtest_send_transfer_cmd(vws, VCMD_TRANSFER_PUT, handle,
482                                            level, stride, layer_stride, box,
483                                            data_size);
484 
485    return virgl_vtest_send_transfer_cmd2(vws, VCMD_TRANSFER_PUT2, handle,
486                                         level, box, data_size, offset);
487 }
488 
virgl_vtest_send_transfer_put_data(struct virgl_vtest_winsys * vws,void * data,uint32_t data_size)489 int virgl_vtest_send_transfer_put_data(struct virgl_vtest_winsys *vws,
490                                        void *data,
491                                        uint32_t data_size)
492 {
493    return virgl_block_write(vws->sock_fd, data, data_size);
494 }
495 
virgl_vtest_recv_transfer_get_data(struct virgl_vtest_winsys * vws,void * data,uint32_t data_size,uint32_t stride,const struct pipe_box * box,uint32_t format)496 int virgl_vtest_recv_transfer_get_data(struct virgl_vtest_winsys *vws,
497                                        void *data,
498                                        uint32_t data_size,
499                                        uint32_t stride,
500                                        const struct pipe_box *box,
501                                        uint32_t format)
502 {
503    void *line;
504    void *ptr = data;
505    int hblocks = util_format_get_nblocksy(format, box->height);
506 
507    line = malloc(stride);
508    while (hblocks) {
509       virgl_block_read(vws->sock_fd, line, stride);
510       memcpy(ptr, line, util_format_get_stride(format, box->width));
511       ptr += stride;
512       hblocks--;
513    }
514    free(line);
515    return 0;
516 }
517 
virgl_vtest_busy_wait(struct virgl_vtest_winsys * vws,int handle,int flags)518 int virgl_vtest_busy_wait(struct virgl_vtest_winsys *vws, int handle,
519                           int flags)
520 {
521    uint32_t vtest_hdr[VTEST_HDR_SIZE];
522    uint32_t cmd[VCMD_BUSY_WAIT_SIZE];
523    uint32_t result[1];
524    ASSERTED int ret;
525    vtest_hdr[VTEST_CMD_LEN] = VCMD_BUSY_WAIT_SIZE;
526    vtest_hdr[VTEST_CMD_ID] = VCMD_RESOURCE_BUSY_WAIT;
527    cmd[VCMD_BUSY_WAIT_HANDLE] = handle;
528    cmd[VCMD_BUSY_WAIT_FLAGS] = flags;
529 
530    virgl_block_write(vws->sock_fd, &vtest_hdr, sizeof(vtest_hdr));
531    virgl_block_write(vws->sock_fd, &cmd, sizeof(cmd));
532 
533    ret = virgl_block_read(vws->sock_fd, vtest_hdr, sizeof(vtest_hdr));
534    assert(ret);
535    ret = virgl_block_read(vws->sock_fd, result, sizeof(result));
536    assert(ret);
537    return result[0];
538 }
539