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 struct virgl_resource_params params = { .size = size,
242 .bind = bind,
243 .format = format,
244 .flags = 0,
245 .nr_samples = nr_samples,
246 .width = width,
247 .height = height,
248 .depth = depth,
249 .array_size = array_size,
250 .last_level = last_level,
251 .target = target };
252
253 res = CALLOC_STRUCT(virgl_hw_res);
254 if (!res)
255 return NULL;
256
257 if (bind & (VIRGL_BIND_DISPLAY_TARGET | VIRGL_BIND_SCANOUT)) {
258 res->dt = vtws->sws->displaytarget_create(vtws->sws, bind, format,
259 width, height, 64, NULL,
260 &res->stride);
261
262 } else if (vtws->protocol_version < 2) {
263 res->ptr = align_malloc(size, 64);
264 if (!res->ptr) {
265 FREE(res);
266 return NULL;
267 }
268 }
269
270 res->bind = bind;
271 res->format = format;
272 res->height = height;
273 res->width = width;
274 res->size = size;
275 virgl_vtest_send_resource_create(vtws, handle, target, pipe_to_virgl_format(format), bind,
276 width, height, depth, array_size,
277 last_level, nr_samples, size, &fd);
278
279 if (vtws->protocol_version >= 2) {
280 if (res->size == 0) {
281 res->ptr = NULL;
282 goto out;
283 }
284
285 if (fd < 0) {
286 FREE(res);
287 fprintf(stderr, "Unable to get a valid fd\n");
288 return NULL;
289 }
290
291 res->ptr = os_mmap(NULL, res->size, PROT_WRITE | PROT_READ, MAP_SHARED,
292 fd, 0);
293
294 if (res->ptr == MAP_FAILED) {
295 fprintf(stderr, "Client failed to map shared memory region\n");
296 close(fd);
297 FREE(res);
298 return NULL;
299 }
300
301 close(fd);
302 }
303
304 out:
305 virgl_resource_cache_entry_init(&res->cache_entry, params);
306 res->res_handle = handle++;
307 pipe_reference_init(&res->reference, 1);
308 p_atomic_set(&res->num_cs_references, 0);
309 return res;
310 }
311
virgl_vtest_resource_map(struct virgl_winsys * vws,struct virgl_hw_res * res)312 static void *virgl_vtest_resource_map(struct virgl_winsys *vws,
313 struct virgl_hw_res *res)
314 {
315 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
316
317 /*
318 * With protocol v0 we can either have a display target or a resource backing
319 * store. With protocol v2 we can have both, so only return the memory mapped
320 * backing store in this function. We can copy to the display target when
321 * appropriate.
322 */
323 if (vtws->protocol_version >= 2 || !res->dt) {
324 res->mapped = res->ptr;
325 return res->mapped;
326 } else {
327 return vtws->sws->displaytarget_map(vtws->sws, res->dt, 0);
328 }
329 }
330
virgl_vtest_resource_unmap(struct virgl_winsys * vws,struct virgl_hw_res * res)331 static void virgl_vtest_resource_unmap(struct virgl_winsys *vws,
332 struct virgl_hw_res *res)
333 {
334 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
335 if (res->mapped)
336 res->mapped = NULL;
337
338 if (res->dt && vtws->protocol_version < 2)
339 vtws->sws->displaytarget_unmap(vtws->sws, res->dt);
340 }
341
virgl_vtest_resource_wait(struct virgl_winsys * vws,struct virgl_hw_res * res)342 static void virgl_vtest_resource_wait(struct virgl_winsys *vws,
343 struct virgl_hw_res *res)
344 {
345 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
346
347 virgl_vtest_busy_wait(vtws, res->res_handle, VCMD_BUSY_WAIT_FLAG_WAIT);
348 }
349
350 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)351 virgl_vtest_winsys_resource_cache_create(struct virgl_winsys *vws,
352 enum pipe_texture_target target,
353 uint32_t format,
354 uint32_t bind,
355 uint32_t width,
356 uint32_t height,
357 uint32_t depth,
358 uint32_t array_size,
359 uint32_t last_level,
360 uint32_t nr_samples,
361 uint32_t flags,
362 uint32_t size)
363 {
364 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
365 struct virgl_hw_res *res;
366 struct virgl_resource_cache_entry *entry;
367 struct virgl_resource_params params = { .size = size,
368 .bind = bind,
369 .format = format,
370 .flags = 0,
371 .nr_samples = nr_samples,
372 .width = width,
373 .height = height,
374 .depth = depth,
375 .array_size = array_size,
376 .last_level = last_level,
377 .target = target };
378
379 if (!can_cache_resource_with_bind(bind))
380 goto alloc;
381
382 mtx_lock(&vtws->mutex);
383
384 entry = virgl_resource_cache_remove_compatible(&vtws->cache, params);
385 if (entry) {
386 res = cache_entry_container_res(entry);
387 mtx_unlock(&vtws->mutex);
388 pipe_reference_init(&res->reference, 1);
389 return res;
390 }
391
392 mtx_unlock(&vtws->mutex);
393
394 alloc:
395 res = virgl_vtest_winsys_resource_create(vws, target, format, bind,
396 width, height, depth, array_size,
397 last_level, nr_samples, size);
398 return res;
399 }
400
virgl_vtest_lookup_res(struct virgl_vtest_cmd_buf * cbuf,struct virgl_hw_res * res)401 static boolean virgl_vtest_lookup_res(struct virgl_vtest_cmd_buf *cbuf,
402 struct virgl_hw_res *res)
403 {
404 unsigned hash = res->res_handle & (sizeof(cbuf->is_handle_added)-1);
405 int i;
406
407 if (cbuf->is_handle_added[hash]) {
408 i = cbuf->reloc_indices_hashlist[hash];
409 if (cbuf->res_bo[i] == res)
410 return true;
411
412 for (i = 0; i < cbuf->cres; i++) {
413 if (cbuf->res_bo[i] == res) {
414 cbuf->reloc_indices_hashlist[hash] = i;
415 return true;
416 }
417 }
418 }
419 return false;
420 }
421
virgl_vtest_release_all_res(struct virgl_vtest_winsys * vtws,struct virgl_vtest_cmd_buf * cbuf)422 static void virgl_vtest_release_all_res(struct virgl_vtest_winsys *vtws,
423 struct virgl_vtest_cmd_buf *cbuf)
424 {
425 int i;
426
427 for (i = 0; i < cbuf->cres; i++) {
428 p_atomic_dec(&cbuf->res_bo[i]->num_cs_references);
429 virgl_vtest_resource_reference(&vtws->base, &cbuf->res_bo[i], NULL);
430 }
431 cbuf->cres = 0;
432 }
433
virgl_vtest_add_res(struct virgl_vtest_winsys * vtws,struct virgl_vtest_cmd_buf * cbuf,struct virgl_hw_res * res)434 static void virgl_vtest_add_res(struct virgl_vtest_winsys *vtws,
435 struct virgl_vtest_cmd_buf *cbuf,
436 struct virgl_hw_res *res)
437 {
438 unsigned hash = res->res_handle & (sizeof(cbuf->is_handle_added)-1);
439
440 if (cbuf->cres >= cbuf->nres) {
441 unsigned new_nres = cbuf->nres + 256;
442 struct virgl_hw_res **new_re_bo = REALLOC(cbuf->res_bo,
443 cbuf->nres * sizeof(struct virgl_hw_buf*),
444 new_nres * sizeof(struct virgl_hw_buf*));
445 if (!new_re_bo) {
446 fprintf(stderr,"failure to add relocation %d, %d\n", cbuf->cres, cbuf->nres);
447 return;
448 }
449
450 cbuf->res_bo = new_re_bo;
451 cbuf->nres = new_nres;
452 }
453
454 cbuf->res_bo[cbuf->cres] = NULL;
455 virgl_vtest_resource_reference(&vtws->base, &cbuf->res_bo[cbuf->cres], res);
456 cbuf->is_handle_added[hash] = TRUE;
457
458 cbuf->reloc_indices_hashlist[hash] = cbuf->cres;
459 p_atomic_inc(&res->num_cs_references);
460 cbuf->cres++;
461 }
462
virgl_vtest_cmd_buf_create(struct virgl_winsys * vws,uint32_t size)463 static struct virgl_cmd_buf *virgl_vtest_cmd_buf_create(struct virgl_winsys *vws,
464 uint32_t size)
465 {
466 struct virgl_vtest_cmd_buf *cbuf;
467
468 cbuf = CALLOC_STRUCT(virgl_vtest_cmd_buf);
469 if (!cbuf)
470 return NULL;
471
472 cbuf->nres = 512;
473 cbuf->res_bo = CALLOC(cbuf->nres, sizeof(struct virgl_hw_buf*));
474 if (!cbuf->res_bo) {
475 FREE(cbuf);
476 return NULL;
477 }
478
479 cbuf->buf = CALLOC(size, sizeof(uint32_t));
480 if (!cbuf->buf) {
481 FREE(cbuf->res_bo);
482 FREE(cbuf);
483 return NULL;
484 }
485
486 cbuf->ws = vws;
487 cbuf->base.buf = cbuf->buf;
488 return &cbuf->base;
489 }
490
virgl_vtest_cmd_buf_destroy(struct virgl_cmd_buf * _cbuf)491 static void virgl_vtest_cmd_buf_destroy(struct virgl_cmd_buf *_cbuf)
492 {
493 struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
494
495 virgl_vtest_release_all_res(virgl_vtest_winsys(cbuf->ws), cbuf);
496 FREE(cbuf->res_bo);
497 FREE(cbuf->buf);
498 FREE(cbuf);
499 }
500
501 static struct pipe_fence_handle *
virgl_vtest_fence_create(struct virgl_winsys * vws)502 virgl_vtest_fence_create(struct virgl_winsys *vws)
503 {
504 struct virgl_hw_res *res;
505
506 /* Resources for fences should not be from the cache, since we are basing
507 * the fence status on the resource creation busy status.
508 */
509 res = virgl_vtest_winsys_resource_create(vws,
510 PIPE_BUFFER,
511 PIPE_FORMAT_R8_UNORM,
512 VIRGL_BIND_CUSTOM,
513 8, 1, 1, 0, 0, 0, 8);
514
515 return (struct pipe_fence_handle *)res;
516 }
517
virgl_vtest_winsys_submit_cmd(struct virgl_winsys * vws,struct virgl_cmd_buf * _cbuf,struct pipe_fence_handle ** fence)518 static int virgl_vtest_winsys_submit_cmd(struct virgl_winsys *vws,
519 struct virgl_cmd_buf *_cbuf,
520 struct pipe_fence_handle **fence)
521 {
522 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
523 struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
524 int ret;
525
526 if (cbuf->base.cdw == 0)
527 return 0;
528
529 ret = virgl_vtest_submit_cmd(vtws, cbuf);
530 if (fence && ret == 0)
531 *fence = virgl_vtest_fence_create(vws);
532
533 virgl_vtest_release_all_res(vtws, cbuf);
534 memset(cbuf->is_handle_added, 0, sizeof(cbuf->is_handle_added));
535 cbuf->base.cdw = 0;
536 return ret;
537 }
538
virgl_vtest_emit_res(struct virgl_winsys * vws,struct virgl_cmd_buf * _cbuf,struct virgl_hw_res * res,boolean write_buf)539 static void virgl_vtest_emit_res(struct virgl_winsys *vws,
540 struct virgl_cmd_buf *_cbuf,
541 struct virgl_hw_res *res, boolean write_buf)
542 {
543 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
544 struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
545 boolean already_in_list = virgl_vtest_lookup_res(cbuf, res);
546
547 if (write_buf)
548 cbuf->base.buf[cbuf->base.cdw++] = res->res_handle;
549 if (!already_in_list)
550 virgl_vtest_add_res(vtws, cbuf, res);
551 }
552
virgl_vtest_res_is_ref(struct virgl_winsys * vws,struct virgl_cmd_buf * _cbuf,struct virgl_hw_res * res)553 static boolean virgl_vtest_res_is_ref(struct virgl_winsys *vws,
554 struct virgl_cmd_buf *_cbuf,
555 struct virgl_hw_res *res)
556 {
557 if (!p_atomic_read(&res->num_cs_references))
558 return FALSE;
559
560 return TRUE;
561 }
562
virgl_vtest_get_caps(struct virgl_winsys * vws,struct virgl_drm_caps * caps)563 static int virgl_vtest_get_caps(struct virgl_winsys *vws,
564 struct virgl_drm_caps *caps)
565 {
566 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
567
568 virgl_ws_fill_new_caps_defaults(caps);
569 return virgl_vtest_send_get_caps(vtws, caps);
570 }
571
572 static struct pipe_fence_handle *
virgl_cs_create_fence(struct virgl_winsys * vws,int fd)573 virgl_cs_create_fence(struct virgl_winsys *vws, int fd)
574 {
575 return virgl_vtest_fence_create(vws);
576 }
577
virgl_fence_wait(struct virgl_winsys * vws,struct pipe_fence_handle * fence,uint64_t timeout)578 static bool virgl_fence_wait(struct virgl_winsys *vws,
579 struct pipe_fence_handle *fence,
580 uint64_t timeout)
581 {
582 struct virgl_hw_res *res = virgl_hw_res(fence);
583
584 if (timeout == 0)
585 return !virgl_vtest_resource_is_busy(vws, res);
586
587 if (timeout != PIPE_TIMEOUT_INFINITE) {
588 int64_t start_time = os_time_get();
589 timeout /= 1000;
590 while (virgl_vtest_resource_is_busy(vws, res)) {
591 if (os_time_get() - start_time >= timeout)
592 return FALSE;
593 os_time_sleep(10);
594 }
595 return TRUE;
596 }
597 virgl_vtest_resource_wait(vws, res);
598 return TRUE;
599 }
600
virgl_fence_reference(struct virgl_winsys * vws,struct pipe_fence_handle ** dst,struct pipe_fence_handle * src)601 static void virgl_fence_reference(struct virgl_winsys *vws,
602 struct pipe_fence_handle **dst,
603 struct pipe_fence_handle *src)
604 {
605 struct virgl_vtest_winsys *vdws = virgl_vtest_winsys(vws);
606 virgl_vtest_resource_reference(&vdws->base, (struct virgl_hw_res **)dst,
607 virgl_hw_res(src));
608 }
609
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)610 static void virgl_vtest_flush_frontbuffer(struct virgl_winsys *vws,
611 struct virgl_hw_res *res,
612 unsigned level, unsigned layer,
613 void *winsys_drawable_handle,
614 struct pipe_box *sub_box)
615 {
616 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
617 struct pipe_box box;
618 uint32_t offset = 0;
619 if (!res->dt)
620 return;
621
622 memset(&box, 0, sizeof(box));
623
624 if (sub_box) {
625 box = *sub_box;
626 offset = box.y / util_format_get_blockheight(res->format) * res->stride +
627 box.x / util_format_get_blockwidth(res->format) * util_format_get_blocksize(res->format);
628 } else {
629 box.z = layer;
630 box.width = res->width;
631 box.height = res->height;
632 box.depth = 1;
633 }
634
635 virgl_vtest_transfer_get_internal(vws, res, &box, res->stride, 0, offset,
636 level, true);
637
638 vtws->sws->displaytarget_display(vtws->sws, res->dt, winsys_drawable_handle,
639 sub_box);
640 }
641
642 static void
virgl_vtest_winsys_destroy(struct virgl_winsys * vws)643 virgl_vtest_winsys_destroy(struct virgl_winsys *vws)
644 {
645 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
646
647 virgl_resource_cache_flush(&vtws->cache);
648
649 mtx_destroy(&vtws->mutex);
650 FREE(vtws);
651 }
652
653 static bool
virgl_vtest_resource_cache_entry_is_busy(struct virgl_resource_cache_entry * entry,void * user_data)654 virgl_vtest_resource_cache_entry_is_busy(struct virgl_resource_cache_entry *entry,
655 void *user_data)
656 {
657 struct virgl_vtest_winsys *vtws = user_data;
658 struct virgl_hw_res *res = cache_entry_container_res(entry);
659
660 return virgl_vtest_resource_is_busy(&vtws->base, res);
661 }
662
663 static void
virgl_vtest_resource_cache_entry_release(struct virgl_resource_cache_entry * entry,void * user_data)664 virgl_vtest_resource_cache_entry_release(struct virgl_resource_cache_entry *entry,
665 void *user_data)
666 {
667 struct virgl_vtest_winsys *vtws = user_data;
668 struct virgl_hw_res *res = cache_entry_container_res(entry);
669
670 virgl_hw_res_destroy(vtws, res);
671 }
672
673 struct virgl_winsys *
virgl_vtest_winsys_wrap(struct sw_winsys * sws)674 virgl_vtest_winsys_wrap(struct sw_winsys *sws)
675 {
676 static const unsigned CACHE_TIMEOUT_USEC = 1000000;
677 struct virgl_vtest_winsys *vtws;
678
679 vtws = CALLOC_STRUCT(virgl_vtest_winsys);
680 if (!vtws)
681 return NULL;
682
683 virgl_vtest_connect(vtws);
684 vtws->sws = sws;
685
686 virgl_resource_cache_init(&vtws->cache, CACHE_TIMEOUT_USEC,
687 virgl_vtest_resource_cache_entry_is_busy,
688 virgl_vtest_resource_cache_entry_release,
689 vtws);
690 (void) mtx_init(&vtws->mutex, mtx_plain);
691
692 vtws->base.destroy = virgl_vtest_winsys_destroy;
693
694 vtws->base.transfer_put = virgl_vtest_transfer_put;
695 vtws->base.transfer_get = virgl_vtest_transfer_get;
696
697 vtws->base.resource_create = virgl_vtest_winsys_resource_cache_create;
698 vtws->base.resource_reference = virgl_vtest_resource_reference;
699 vtws->base.resource_map = virgl_vtest_resource_map;
700 vtws->base.resource_wait = virgl_vtest_resource_wait;
701 vtws->base.resource_is_busy = virgl_vtest_resource_is_busy;
702 vtws->base.cmd_buf_create = virgl_vtest_cmd_buf_create;
703 vtws->base.cmd_buf_destroy = virgl_vtest_cmd_buf_destroy;
704 vtws->base.submit_cmd = virgl_vtest_winsys_submit_cmd;
705
706 vtws->base.emit_res = virgl_vtest_emit_res;
707 vtws->base.res_is_referenced = virgl_vtest_res_is_ref;
708 vtws->base.get_caps = virgl_vtest_get_caps;
709
710 vtws->base.cs_create_fence = virgl_cs_create_fence;
711 vtws->base.fence_wait = virgl_fence_wait;
712 vtws->base.fence_reference = virgl_fence_reference;
713 vtws->base.supports_fences = 0;
714 vtws->base.supports_encoded_transfers = (vtws->protocol_version >= 2);
715
716 vtws->base.flush_frontbuffer = virgl_vtest_flush_frontbuffer;
717
718 return &vtws->base;
719 }
720