• 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 #include <stdio.h>
24 #include "util/macros.h"
25 #include "util/u_surface.h"
26 #include "util/u_memory.h"
27 #include "util/format/u_format.h"
28 #include "util/u_inlines.h"
29 #include "util/os_time.h"
30 #include "frontend/sw_winsys.h"
31 #include "util/os_mman.h"
32 
33 #include "virgl_vtest_winsys.h"
34 #include "virgl_vtest_public.h"
35 #include "virtio-gpu/virgl_protocol.h"
36 
37 /* Gets a pointer to the virgl_hw_res containing the pointed to cache entry. */
38 #define cache_entry_container_res(ptr) \
39     (struct virgl_hw_res*)((char*)ptr - offsetof(struct virgl_hw_res, cache_entry))
40 
41 static void *virgl_vtest_resource_map(struct virgl_winsys *vws,
42                                       struct virgl_hw_res *res);
43 static void virgl_vtest_resource_unmap(struct virgl_winsys *vws,
44                                        struct virgl_hw_res *res);
45 
can_cache_resource_with_bind(uint32_t bind)46 static inline bool can_cache_resource_with_bind(uint32_t bind)
47 {
48    return bind == VIRGL_BIND_CONSTANT_BUFFER ||
49           bind == VIRGL_BIND_INDEX_BUFFER ||
50           bind == VIRGL_BIND_VERTEX_BUFFER ||
51           bind == VIRGL_BIND_CUSTOM ||
52           bind == VIRGL_BIND_STAGING;
53 }
54 
vtest_get_transfer_size(struct virgl_hw_res * res,const struct pipe_box * box,uint32_t stride,uint32_t layer_stride,uint32_t level,uint32_t * valid_stride_p)55 static uint32_t vtest_get_transfer_size(struct virgl_hw_res *res,
56                                         const struct pipe_box *box,
57                                         uint32_t stride, uint32_t layer_stride,
58                                         uint32_t level, uint32_t *valid_stride_p)
59 {
60    uint32_t valid_stride, valid_layer_stride;
61 
62    valid_stride = util_format_get_stride(res->format, box->width);
63    if (stride) {
64       if (box->height > 1)
65          valid_stride = stride;
66    }
67 
68    valid_layer_stride = util_format_get_2d_size(res->format, valid_stride,
69                                                 box->height);
70    if (layer_stride) {
71       if (box->depth > 1)
72          valid_layer_stride = layer_stride;
73    }
74 
75    *valid_stride_p = valid_stride;
76    return valid_layer_stride * box->depth;
77 }
78 
79 static int
virgl_vtest_transfer_put(struct virgl_winsys * vws,struct virgl_hw_res * res,const struct pipe_box * box,uint32_t stride,uint32_t layer_stride,uint32_t buf_offset,uint32_t level)80 virgl_vtest_transfer_put(struct virgl_winsys *vws,
81                          struct virgl_hw_res *res,
82                          const struct pipe_box *box,
83                          uint32_t stride, uint32_t layer_stride,
84                          uint32_t buf_offset, uint32_t level)
85 {
86    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
87    uint32_t size;
88    void *ptr;
89    uint32_t valid_stride;
90 
91    size = vtest_get_transfer_size(res, box, stride, layer_stride, level,
92                                   &valid_stride);
93 
94    virgl_vtest_send_transfer_put(vtws, res->res_handle,
95                                  level, stride, layer_stride,
96                                  box, size, buf_offset);
97 
98    if (vtws->protocol_version >= 2)
99       return 0;
100 
101    ptr = virgl_vtest_resource_map(vws, res);
102    virgl_vtest_send_transfer_put_data(vtws, ptr + buf_offset, size);
103    virgl_vtest_resource_unmap(vws, res);
104    return 0;
105 }
106 
107 static int
virgl_vtest_transfer_get_internal(struct virgl_winsys * vws,struct virgl_hw_res * res,const struct pipe_box * box,uint32_t stride,uint32_t layer_stride,uint32_t buf_offset,uint32_t level,bool flush_front_buffer)108 virgl_vtest_transfer_get_internal(struct virgl_winsys *vws,
109                                   struct virgl_hw_res *res,
110                                   const struct pipe_box *box,
111                                   uint32_t stride, uint32_t layer_stride,
112                                   uint32_t buf_offset, uint32_t level,
113                                   bool flush_front_buffer)
114 {
115    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
116    uint32_t size;
117    void *ptr;
118    uint32_t valid_stride;
119 
120    size = vtest_get_transfer_size(res, box, stride, layer_stride, level,
121                                   &valid_stride);
122    virgl_vtest_send_transfer_get(vtws, res->res_handle,
123                                  level, stride, layer_stride,
124                                  box, size, buf_offset);
125 
126    if (flush_front_buffer || vtws->protocol_version >= 2)
127       virgl_vtest_busy_wait(vtws, res->res_handle, VCMD_BUSY_WAIT_FLAG_WAIT);
128 
129    if (vtws->protocol_version >= 2) {
130       if (flush_front_buffer) {
131          if (box->depth > 1 || box->z > 1) {
132             fprintf(stderr, "Expected a 2D resource, received a 3D resource\n");
133             return -1;
134          }
135 
136          void *dt_map;
137          uint32_t shm_stride;
138 
139          /*
140           * The display target is aligned to 64 bytes, while the shared resource
141           * between the client/server is not.
142           */
143          shm_stride = util_format_get_stride(res->format, res->width);
144          ptr = virgl_vtest_resource_map(vws, res);
145          dt_map = vtws->sws->displaytarget_map(vtws->sws, res->dt, 0);
146 
147          util_copy_rect(dt_map, res->format, res->stride, box->x, box->y,
148                         box->width, box->height, ptr, shm_stride, box->x,
149                         box->y);
150 
151          virgl_vtest_resource_unmap(vws, res);
152          vtws->sws->displaytarget_unmap(vtws->sws, res->dt);
153       }
154    } else {
155       ptr = virgl_vtest_resource_map(vws, res);
156       virgl_vtest_recv_transfer_get_data(vtws, ptr + buf_offset, size,
157                                          valid_stride, box, res->format);
158       virgl_vtest_resource_unmap(vws, res);
159    }
160 
161    return 0;
162 }
163 
164 static int
virgl_vtest_transfer_get(struct virgl_winsys * vws,struct virgl_hw_res * res,const struct pipe_box * box,uint32_t stride,uint32_t layer_stride,uint32_t buf_offset,uint32_t level)165 virgl_vtest_transfer_get(struct virgl_winsys *vws,
166                          struct virgl_hw_res *res,
167                          const struct pipe_box *box,
168                          uint32_t stride, uint32_t layer_stride,
169                          uint32_t buf_offset, uint32_t level)
170 {
171    return virgl_vtest_transfer_get_internal(vws, res, box, stride,
172                                             layer_stride, buf_offset,
173                                             level, false);
174 }
175 
virgl_hw_res_destroy(struct virgl_vtest_winsys * vtws,struct virgl_hw_res * res)176 static void virgl_hw_res_destroy(struct virgl_vtest_winsys *vtws,
177                                  struct virgl_hw_res *res)
178 {
179    virgl_vtest_send_resource_unref(vtws, res->res_handle);
180    if (res->dt)
181       vtws->sws->displaytarget_destroy(vtws->sws, res->dt);
182    if (vtws->protocol_version >= 2) {
183       if (res->ptr)
184          os_munmap(res->ptr, res->size);
185    } else {
186       align_free(res->ptr);
187    }
188 
189    FREE(res);
190 }
191 
virgl_vtest_resource_is_busy(struct virgl_winsys * vws,struct virgl_hw_res * res)192 static bool virgl_vtest_resource_is_busy(struct virgl_winsys *vws,
193                                          struct virgl_hw_res *res)
194 {
195    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
196 
197    /* implement busy check */
198    int ret;
199    ret = virgl_vtest_busy_wait(vtws, res->res_handle, 0);
200 
201    if (ret < 0)
202       return false;
203 
204    return ret == 1 ? true : false;
205 }
206 
virgl_vtest_resource_reference(struct virgl_winsys * vws,struct virgl_hw_res ** dres,struct virgl_hw_res * sres)207 static void virgl_vtest_resource_reference(struct virgl_winsys *vws,
208                                            struct virgl_hw_res **dres,
209                                            struct virgl_hw_res *sres)
210 {
211    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
212    struct virgl_hw_res *old = *dres;
213 
214    if (pipe_reference(&(*dres)->reference, &sres->reference)) {
215       if (!can_cache_resource_with_bind(old->bind)) {
216          virgl_hw_res_destroy(vtws, old);
217       } else {
218          mtx_lock(&vtws->mutex);
219          virgl_resource_cache_add(&vtws->cache, &old->cache_entry);
220          mtx_unlock(&vtws->mutex);
221       }
222    }
223    *dres = sres;
224 }
225 
226 static int
virgl_vtest_winsys_resource_create_blob(struct virgl_winsys * vws,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 flags,uint32_t size,int * fd)227 virgl_vtest_winsys_resource_create_blob(struct virgl_winsys *vws,
228                                         enum pipe_texture_target target,
229                                         uint32_t format,
230                                         uint32_t bind,
231                                         uint32_t width,
232                                         uint32_t height,
233                                         uint32_t depth,
234                                         uint32_t array_size,
235                                         uint32_t last_level,
236                                         uint32_t nr_samples,
237                                         uint32_t flags,
238                                         uint32_t size,
239                                         int *fd)
240 {
241    uint32_t cmd[VIRGL_PIPE_RES_CREATE_SIZE + 1] = { 0 };
242    struct virgl_vtest_winsys *vvws = virgl_vtest_winsys(vws);
243 
244    int32_t blob_id = p_atomic_inc_return(&vvws->blob_id);
245    cmd[0] = VIRGL_CMD0(VIRGL_CCMD_PIPE_RESOURCE_CREATE, 0, VIRGL_PIPE_RES_CREATE_SIZE);
246    cmd[VIRGL_PIPE_RES_CREATE_FORMAT] = format;
247    cmd[VIRGL_PIPE_RES_CREATE_BIND] = bind;
248    cmd[VIRGL_PIPE_RES_CREATE_TARGET] = target;
249    cmd[VIRGL_PIPE_RES_CREATE_WIDTH] = width;
250    cmd[VIRGL_PIPE_RES_CREATE_HEIGHT] = height;
251    cmd[VIRGL_PIPE_RES_CREATE_DEPTH] = depth;
252    cmd[VIRGL_PIPE_RES_CREATE_ARRAY_SIZE] = array_size;
253    cmd[VIRGL_PIPE_RES_CREATE_LAST_LEVEL] = last_level;
254    cmd[VIRGL_PIPE_RES_CREATE_NR_SAMPLES] = nr_samples;
255    cmd[VIRGL_PIPE_RES_CREATE_FLAGS] = flags;
256    cmd[VIRGL_PIPE_RES_CREATE_BLOB_ID] = blob_id;
257 
258    virgl_vtest_submit_cmd(vvws, cmd, VIRGL_PIPE_RES_CREATE_SIZE + 1);
259    return virgl_vtest_send_create_blob(vvws, size, blob_id, fd);
260 }
261 
262 
263 static struct virgl_hw_res *
virgl_vtest_winsys_resource_create(struct virgl_winsys * vws,enum pipe_texture_target target,const void * map_front_private,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 flags,uint32_t size)264 virgl_vtest_winsys_resource_create(struct virgl_winsys *vws,
265                                    enum pipe_texture_target target,
266                                    const void *map_front_private,
267                                    uint32_t format,
268                                    uint32_t bind,
269                                    uint32_t width,
270                                    uint32_t height,
271                                    uint32_t depth,
272                                    uint32_t array_size,
273                                    uint32_t last_level,
274                                    uint32_t nr_samples,
275                                    uint32_t flags,
276                                    uint32_t size)
277 {
278    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
279    struct virgl_hw_res *res;
280    static int handle = 1;
281    int fd = -1;
282    struct virgl_resource_params params = { .size = size,
283                                            .bind = bind,
284                                            .format = format,
285                                            .flags = 0,
286                                            .nr_samples = nr_samples,
287                                            .width = width,
288                                            .height = height,
289                                            .depth = depth,
290                                            .array_size = array_size,
291                                            .last_level = last_level,
292                                            .target = target };
293 
294    res = CALLOC_STRUCT(virgl_hw_res);
295    if (!res)
296       return NULL;
297 
298    if (bind & (VIRGL_BIND_DISPLAY_TARGET | VIRGL_BIND_SCANOUT)) {
299       res->dt = vtws->sws->displaytarget_create(vtws->sws, bind, format,
300                                                 width, height, 64, map_front_private,
301                                                 &res->stride);
302 
303    } else if (vtws->protocol_version < 2) {
304       res->ptr = align_malloc(size, 64);
305       if (!res->ptr) {
306          FREE(res);
307          return NULL;
308       }
309    }
310 
311    if ((flags & (VIRGL_RESOURCE_FLAG_MAP_PERSISTENT |
312                  VIRGL_RESOURCE_FLAG_MAP_COHERENT))) {
313       width = ALIGN(width, getpagesize());
314       size = ALIGN(size, getpagesize());
315       handle = virgl_vtest_winsys_resource_create_blob(vws, target, format, bind,
316                                                        width, height, depth,
317                                                        array_size, last_level, nr_samples,
318                                                        flags, size, &fd);
319 
320       if (handle) {
321          pipe_reference_init(&res->reference, 1);
322          p_atomic_set(&res->num_cs_references, 0);
323       }
324    } else {
325 
326       handle = virgl_vtest_send_resource_create(vtws, handle, target, pipe_to_virgl_format(format), bind,
327                                                 width, height, depth, array_size,
328                                                 last_level, nr_samples, size, &fd);
329    }
330 
331    res->bind = bind;
332    res->format = format;
333    res->height = height;
334    res->width = width;
335    res->size = size;
336 
337    if (vtws->protocol_version >= 2) {
338       if (res->size == 0) {
339          res->ptr = NULL;
340          res->res_handle = handle;
341          goto out;
342       }
343 
344       if (fd < 0) {
345          FREE(res);
346          fprintf(stderr, "Unable to get a valid fd\n");
347          return NULL;
348       }
349 
350       res->ptr = os_mmap(NULL, res->size, PROT_WRITE | PROT_READ, MAP_SHARED,
351                          fd, 0);
352 
353       if (res->ptr == MAP_FAILED) {
354          fprintf(stderr, "Client failed to map shared memory region\n");
355          close(fd);
356          FREE(res);
357          return NULL;
358       }
359 
360       close(fd);
361    }
362 
363    res->res_handle = handle;
364    if (map_front_private && res->ptr && res->dt) {
365       void *dt_map = vtws->sws->displaytarget_map(vtws->sws, res->dt, PIPE_MAP_READ_WRITE);
366       uint32_t shm_stride = util_format_get_stride(res->format, res->width);
367       util_copy_rect(res->ptr, res->format, shm_stride, 0, 0,
368                      res->width, res->height, dt_map, res->stride, 0, 0);
369 
370       struct pipe_box box;
371       u_box_2d(0, 0, res->width, res->height, &box);
372       virgl_vtest_transfer_put(vws, res, &box, res->stride, 0, 0, 0);
373    }
374 
375 out:
376    virgl_resource_cache_entry_init(&res->cache_entry, params);
377    handle++;
378    pipe_reference_init(&res->reference, 1);
379    p_atomic_set(&res->num_cs_references, 0);
380    return res;
381 }
382 
virgl_vtest_resource_map(struct virgl_winsys * vws,struct virgl_hw_res * res)383 static void *virgl_vtest_resource_map(struct virgl_winsys *vws,
384                                       struct virgl_hw_res *res)
385 {
386    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
387 
388    /*
389     * With protocol v0 we can either have a display target or a resource backing
390     * store. With protocol v2 we can have both, so only return the memory mapped
391     * backing store in this function. We can copy to the display target when
392     * appropriate.
393     */
394    if (vtws->protocol_version >= 2 || !res->dt) {
395       res->mapped = res->ptr;
396       return res->mapped;
397    } else {
398       return vtws->sws->displaytarget_map(vtws->sws, res->dt, 0);
399    }
400 }
401 
virgl_vtest_resource_unmap(struct virgl_winsys * vws,struct virgl_hw_res * res)402 static void virgl_vtest_resource_unmap(struct virgl_winsys *vws,
403                                        struct virgl_hw_res *res)
404 {
405    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
406    if (res->mapped)
407       res->mapped = NULL;
408 
409    if (res->dt && vtws->protocol_version < 2)
410       vtws->sws->displaytarget_unmap(vtws->sws, res->dt);
411 }
412 
virgl_vtest_resource_wait(struct virgl_winsys * vws,struct virgl_hw_res * res)413 static void virgl_vtest_resource_wait(struct virgl_winsys *vws,
414                                       struct virgl_hw_res *res)
415 {
416    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
417 
418    virgl_vtest_busy_wait(vtws, res->res_handle, VCMD_BUSY_WAIT_FLAG_WAIT);
419 }
420 
421 static struct virgl_hw_res *
virgl_vtest_winsys_resource_cache_create(struct virgl_winsys * vws,enum pipe_texture_target target,const void * map_front_private,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 flags,uint32_t size)422 virgl_vtest_winsys_resource_cache_create(struct virgl_winsys *vws,
423                                          enum pipe_texture_target target,
424                                          const void *map_front_private,
425                                          uint32_t format,
426                                          uint32_t bind,
427                                          uint32_t width,
428                                          uint32_t height,
429                                          uint32_t depth,
430                                          uint32_t array_size,
431                                          uint32_t last_level,
432                                          uint32_t nr_samples,
433                                          uint32_t flags,
434                                          uint32_t size)
435 {
436    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
437    struct virgl_hw_res *res;
438    struct virgl_resource_cache_entry *entry;
439    struct virgl_resource_params params = { .size = size,
440                                            .bind = bind,
441                                            .format = format,
442                                            .flags = 0,
443                                            .nr_samples = nr_samples,
444                                            .width = width,
445                                            .height = height,
446                                            .depth = depth,
447                                            .array_size = array_size,
448                                            .last_level = last_level,
449                                            .target = target };
450 
451    if (!can_cache_resource_with_bind(bind))
452       goto alloc;
453 
454    mtx_lock(&vtws->mutex);
455 
456    entry = virgl_resource_cache_remove_compatible(&vtws->cache, params);
457    if (entry) {
458       res = cache_entry_container_res(entry);
459       mtx_unlock(&vtws->mutex);
460       pipe_reference_init(&res->reference, 1);
461       return res;
462    }
463 
464    mtx_unlock(&vtws->mutex);
465 
466 alloc:
467 
468    return  virgl_vtest_winsys_resource_create(vws, target, map_front_private,
469                                              format, bind, width, height, depth,
470                                              array_size, last_level, nr_samples, flags,
471                                              size);
472 }
473 
virgl_vtest_res_is_added(struct virgl_vtest_cmd_buf * cbuf,struct virgl_hw_res * res)474 static bool virgl_vtest_res_is_added(struct virgl_vtest_cmd_buf *cbuf,
475                                    struct virgl_hw_res *res)
476 {
477    for (int i = 0; i < cbuf->cres; i++) {
478       if (cbuf->res_bo[i] == res)
479          return true;
480    }
481 
482    return false;
483 }
484 
virgl_vtest_release_all_res(struct virgl_vtest_winsys * vtws,struct virgl_vtest_cmd_buf * cbuf)485 static void virgl_vtest_release_all_res(struct virgl_vtest_winsys *vtws,
486                                         struct virgl_vtest_cmd_buf *cbuf)
487 {
488    int i;
489 
490    for (i = 0; i < cbuf->cres; i++) {
491       p_atomic_dec(&cbuf->res_bo[i]->num_cs_references);
492       virgl_vtest_resource_reference(&vtws->base, &cbuf->res_bo[i], NULL);
493    }
494    cbuf->cres = 0;
495 }
496 
virgl_vtest_add_res(struct virgl_vtest_winsys * vtws,struct virgl_vtest_cmd_buf * cbuf,struct virgl_hw_res * res)497 static void virgl_vtest_add_res(struct virgl_vtest_winsys *vtws,
498                                 struct virgl_vtest_cmd_buf *cbuf,
499                                 struct virgl_hw_res *res)
500 {
501    bool already_in_list = virgl_vtest_res_is_added(cbuf, res);
502    if (unlikely(already_in_list))
503       return;
504 
505    if (cbuf->cres >= cbuf->nres) {
506       unsigned new_nres = cbuf->nres + 256;
507       struct virgl_hw_res **new_re_bo = REALLOC(cbuf->res_bo,
508                                                 cbuf->nres * sizeof(struct virgl_hw_buf*),
509                                                 new_nres * sizeof(struct virgl_hw_buf*));
510       if (!new_re_bo) {
511           fprintf(stderr,"failure to add relocation %d, %d\n", cbuf->cres, cbuf->nres);
512           return;
513       }
514 
515       cbuf->res_bo = new_re_bo;
516       cbuf->nres = new_nres;
517    }
518 
519    cbuf->res_bo[cbuf->cres] = NULL;
520    virgl_vtest_resource_reference(&vtws->base, &cbuf->res_bo[cbuf->cres], res);
521    p_atomic_inc(&res->num_cs_references);
522    cbuf->cres++;
523 }
524 
virgl_vtest_cmd_buf_create(struct virgl_winsys * vws,uint32_t size)525 static struct virgl_cmd_buf *virgl_vtest_cmd_buf_create(struct virgl_winsys *vws,
526                                                         uint32_t size)
527 {
528    struct virgl_vtest_cmd_buf *cbuf;
529 
530    cbuf = CALLOC_STRUCT(virgl_vtest_cmd_buf);
531    if (!cbuf)
532       return NULL;
533 
534    cbuf->nres = 512;
535    cbuf->res_bo = CALLOC(cbuf->nres, sizeof(struct virgl_hw_buf*));
536    if (!cbuf->res_bo) {
537       FREE(cbuf);
538       return NULL;
539    }
540 
541    cbuf->buf = CALLOC(size, sizeof(uint32_t));
542    if (!cbuf->buf) {
543       FREE(cbuf->res_bo);
544       FREE(cbuf);
545       return NULL;
546    }
547 
548    cbuf->ws = vws;
549    cbuf->base.buf = cbuf->buf;
550    return &cbuf->base;
551 }
552 
virgl_vtest_cmd_buf_destroy(struct virgl_cmd_buf * _cbuf)553 static void virgl_vtest_cmd_buf_destroy(struct virgl_cmd_buf *_cbuf)
554 {
555    struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
556 
557    virgl_vtest_release_all_res(virgl_vtest_winsys(cbuf->ws), cbuf);
558    FREE(cbuf->res_bo);
559    FREE(cbuf->buf);
560    FREE(cbuf);
561 }
562 
563 static struct pipe_fence_handle *
virgl_vtest_fence_create(struct virgl_winsys * vws)564 virgl_vtest_fence_create(struct virgl_winsys *vws)
565 {
566    struct virgl_hw_res *res;
567 
568    /* Resources for fences should not be from the cache, since we are basing
569     * the fence status on the resource creation busy status.
570     */
571    res = virgl_vtest_winsys_resource_create(vws,
572                                             PIPE_BUFFER,
573                                             NULL,
574                                             PIPE_FORMAT_R8_UNORM,
575                                             VIRGL_BIND_CUSTOM,
576                                             8, 1, 1, 0, 0, 0, 0, 8);
577 
578    return (struct pipe_fence_handle *)res;
579 }
580 
virgl_vtest_winsys_submit_cmd(struct virgl_winsys * vws,struct virgl_cmd_buf * _cbuf,struct pipe_fence_handle ** fence)581 static int virgl_vtest_winsys_submit_cmd(struct virgl_winsys *vws,
582                                          struct virgl_cmd_buf *_cbuf,
583                                          struct pipe_fence_handle **fence)
584 {
585    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
586    struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
587    int ret;
588 
589    if (cbuf->base.cdw == 0)
590       return 0;
591 
592    ret = virgl_vtest_submit_cmd(vtws, cbuf->base.buf, cbuf->base.cdw);
593    if (fence && ret == 0)
594       *fence = virgl_vtest_fence_create(vws);
595 
596    virgl_vtest_release_all_res(vtws, cbuf);
597    cbuf->base.cdw = 0;
598    return ret;
599 }
600 
virgl_vtest_emit_res(struct virgl_winsys * vws,struct virgl_cmd_buf * _cbuf,struct virgl_hw_res * res,bool write_buf)601 static void virgl_vtest_emit_res(struct virgl_winsys *vws,
602                                  struct virgl_cmd_buf *_cbuf,
603                                  struct virgl_hw_res *res, bool write_buf)
604 {
605    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
606    struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
607 
608    if (write_buf)
609       cbuf->base.buf[cbuf->base.cdw++] = res->res_handle;
610    virgl_vtest_add_res(vtws, cbuf, res);
611 }
612 
virgl_vtest_res_is_ref(struct virgl_winsys * vws,struct virgl_cmd_buf * _cbuf,struct virgl_hw_res * res)613 static bool virgl_vtest_res_is_ref(struct virgl_winsys *vws,
614                                    struct virgl_cmd_buf *_cbuf,
615                                    struct virgl_hw_res *res)
616 {
617    if (!p_atomic_read(&res->num_cs_references))
618       return false;
619 
620    return true;
621 }
622 
virgl_vtest_get_caps(struct virgl_winsys * vws,struct virgl_drm_caps * caps)623 static int virgl_vtest_get_caps(struct virgl_winsys *vws,
624                                 struct virgl_drm_caps *caps)
625 {
626    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
627    int ret;
628 
629    virgl_ws_fill_new_caps_defaults(caps);
630    ret = virgl_vtest_send_get_caps(vtws, caps);
631    // vtest doesn't support that
632    if (caps->caps.v2.capability_bits_v2 & VIRGL_CAP_V2_COPY_TRANSFER_BOTH_DIRECTIONS)
633       caps->caps.v2.capability_bits_v2 &= ~VIRGL_CAP_V2_COPY_TRANSFER_BOTH_DIRECTIONS;
634    return ret;
635 }
636 
637 static struct pipe_fence_handle *
virgl_cs_create_fence(struct virgl_winsys * vws,int fd)638 virgl_cs_create_fence(struct virgl_winsys *vws, int fd)
639 {
640    return virgl_vtest_fence_create(vws);
641 }
642 
virgl_fence_wait(struct virgl_winsys * vws,struct pipe_fence_handle * fence,uint64_t timeout)643 static bool virgl_fence_wait(struct virgl_winsys *vws,
644                              struct pipe_fence_handle *fence,
645                              uint64_t timeout)
646 {
647    struct virgl_hw_res *res = virgl_hw_res(fence);
648 
649    if (timeout == 0)
650       return !virgl_vtest_resource_is_busy(vws, res);
651 
652    if (timeout != OS_TIMEOUT_INFINITE) {
653       int64_t start_time = os_time_get();
654       timeout /= 1000;
655       while (virgl_vtest_resource_is_busy(vws, res)) {
656          if (os_time_get() - start_time >= timeout)
657             return false;
658          os_time_sleep(10);
659       }
660       return true;
661    }
662    virgl_vtest_resource_wait(vws, res);
663    return true;
664 }
665 
virgl_fence_reference(struct virgl_winsys * vws,struct pipe_fence_handle ** dst,struct pipe_fence_handle * src)666 static void virgl_fence_reference(struct virgl_winsys *vws,
667                                   struct pipe_fence_handle **dst,
668                                   struct pipe_fence_handle *src)
669 {
670    struct virgl_vtest_winsys *vdws = virgl_vtest_winsys(vws);
671    virgl_vtest_resource_reference(&vdws->base, (struct virgl_hw_res **)dst,
672                                   virgl_hw_res(src));
673 }
674 
virgl_vtest_flush_frontbuffer(struct virgl_winsys * vws,UNUSED struct virgl_cmd_buf * cmdbuf,struct virgl_hw_res * res,unsigned level,unsigned layer,void * winsys_drawable_handle,struct pipe_box * sub_box)675 static void virgl_vtest_flush_frontbuffer(struct virgl_winsys *vws,
676                                           UNUSED struct virgl_cmd_buf *cmdbuf,
677                                           struct virgl_hw_res *res,
678                                           unsigned level, unsigned layer,
679                                           void *winsys_drawable_handle,
680                                           struct pipe_box *sub_box)
681 {
682    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
683    struct pipe_box box;
684    uint32_t offset = 0;
685    if (!res->dt)
686       return;
687 
688    memset(&box, 0, sizeof(box));
689 
690    if (sub_box) {
691       box = *sub_box;
692       uint32_t shm_stride = util_format_get_stride(res->format, res->width);
693       offset = box.y / util_format_get_blockheight(res->format) * shm_stride +
694                box.x / util_format_get_blockwidth(res->format) * util_format_get_blocksize(res->format);
695    } else {
696       box.z = layer;
697       box.width = res->width;
698       box.height = res->height;
699       box.depth = 1;
700    }
701 
702    virgl_vtest_transfer_get_internal(vws, res, &box, res->stride, 0, offset,
703                                      level, true);
704 
705    vtws->sws->displaytarget_display(vtws->sws, res->dt, winsys_drawable_handle,
706                                     !!sub_box, sub_box);
707 }
708 
709 static void
virgl_vtest_winsys_destroy(struct virgl_winsys * vws)710 virgl_vtest_winsys_destroy(struct virgl_winsys *vws)
711 {
712    struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
713 
714    virgl_resource_cache_flush(&vtws->cache);
715 
716    mtx_destroy(&vtws->mutex);
717    FREE(vtws);
718 }
719 
720 static bool
virgl_vtest_resource_cache_entry_is_busy(struct virgl_resource_cache_entry * entry,void * user_data)721 virgl_vtest_resource_cache_entry_is_busy(struct virgl_resource_cache_entry *entry,
722                                          void *user_data)
723 {
724    struct virgl_vtest_winsys *vtws = user_data;
725    struct virgl_hw_res *res = cache_entry_container_res(entry);
726 
727    return virgl_vtest_resource_is_busy(&vtws->base, res);
728 }
729 
730 static void
virgl_vtest_resource_cache_entry_release(struct virgl_resource_cache_entry * entry,void * user_data)731 virgl_vtest_resource_cache_entry_release(struct virgl_resource_cache_entry *entry,
732                                          void *user_data)
733 {
734    struct virgl_vtest_winsys *vtws = user_data;
735    struct virgl_hw_res *res = cache_entry_container_res(entry);
736 
737    virgl_hw_res_destroy(vtws, res);
738 }
739 
740 struct virgl_winsys *
virgl_vtest_winsys_wrap(struct sw_winsys * sws)741 virgl_vtest_winsys_wrap(struct sw_winsys *sws)
742 {
743    static const unsigned CACHE_TIMEOUT_USEC = 1000000;
744    struct virgl_vtest_winsys *vtws;
745 
746    vtws = CALLOC_STRUCT(virgl_vtest_winsys);
747    if (!vtws)
748       return NULL;
749 
750    virgl_vtest_connect(vtws);
751    vtws->sws = sws;
752 
753    virgl_resource_cache_init(&vtws->cache, CACHE_TIMEOUT_USEC,
754                              virgl_vtest_resource_cache_entry_is_busy,
755                              virgl_vtest_resource_cache_entry_release,
756                              vtws);
757    (void) mtx_init(&vtws->mutex, mtx_plain);
758 
759    vtws->base.destroy = virgl_vtest_winsys_destroy;
760 
761    vtws->base.transfer_put = virgl_vtest_transfer_put;
762    vtws->base.transfer_get = virgl_vtest_transfer_get;
763 
764    vtws->base.resource_create = virgl_vtest_winsys_resource_cache_create;
765    vtws->base.resource_reference = virgl_vtest_resource_reference;
766    vtws->base.resource_map = virgl_vtest_resource_map;
767    vtws->base.resource_wait = virgl_vtest_resource_wait;
768    vtws->base.resource_is_busy = virgl_vtest_resource_is_busy;
769    vtws->base.cmd_buf_create = virgl_vtest_cmd_buf_create;
770    vtws->base.cmd_buf_destroy = virgl_vtest_cmd_buf_destroy;
771    vtws->base.submit_cmd = virgl_vtest_winsys_submit_cmd;
772 
773    vtws->base.emit_res = virgl_vtest_emit_res;
774    vtws->base.res_is_referenced = virgl_vtest_res_is_ref;
775    vtws->base.get_caps = virgl_vtest_get_caps;
776 
777    vtws->base.cs_create_fence = virgl_cs_create_fence;
778    vtws->base.fence_wait = virgl_fence_wait;
779    vtws->base.fence_reference = virgl_fence_reference;
780    vtws->base.supports_fences =  0;
781    vtws->base.supports_encoded_transfers = (vtws->protocol_version >= 2);
782    vtws->base.supports_coherent = 1;
783 
784    vtws->base.flush_frontbuffer = virgl_vtest_flush_frontbuffer;
785 
786    return &vtws->base;
787 }
788