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