28c4c62931bc4256ed6ace029731c6abbd2ad1c4
[mesa.git] / src / gallium / winsys / virgl / vtest / virgl_vtest_winsys.c
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 "virgl_vtest_winsys.h"
25 #include "virgl_vtest_public.h"
26 #include "util/u_memory.h"
27 #include "util/u_format.h"
28 #include "util/u_inlines.h"
29 #include "state_tracker/drm_driver.h"
30 #include "os/os_time.h"
31
32 static void *virgl_vtest_resource_map(struct virgl_winsys *vws, struct virgl_hw_res *res);
33 static void virgl_vtest_resource_unmap(struct virgl_winsys *vws, struct virgl_hw_res *res);
34 static inline boolean can_cache_resource(struct virgl_hw_res *res)
35 {
36 return res->cacheable == TRUE;
37 }
38
39 static uint32_t vtest_get_transfer_size(struct virgl_hw_res *res,
40 const struct pipe_box *box,
41 uint32_t stride, uint32_t layer_stride,
42 uint32_t level, uint32_t *valid_stride_p)
43 {
44 uint32_t valid_stride, valid_layer_stride;
45
46 valid_stride = util_format_get_stride(res->format, box->width);
47 if (stride) {
48 if (box->height > 1)
49 valid_stride = stride;
50 }
51
52 valid_layer_stride = util_format_get_2d_size(res->format, valid_stride,
53 box->height);
54 if (layer_stride) {
55 if (box->depth > 1)
56 valid_layer_stride = layer_stride;
57 }
58
59 *valid_stride_p = valid_stride;
60 return valid_layer_stride * box->depth;
61 }
62
63 static int
64 virgl_vtest_transfer_put(struct virgl_winsys *vws,
65 struct virgl_hw_res *res,
66 const struct pipe_box *box,
67 uint32_t stride, uint32_t layer_stride,
68 uint32_t buf_offset, uint32_t level)
69 {
70 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
71 uint32_t size;
72 void *ptr;
73 uint32_t valid_stride;
74
75 size = vtest_get_transfer_size(res, box, stride, layer_stride, level, &valid_stride);
76
77 virgl_vtest_send_transfer_cmd(vtws, VCMD_TRANSFER_PUT, res->res_handle,
78 level, stride, layer_stride,
79 box, size);
80 ptr = virgl_vtest_resource_map(vws, res);
81 virgl_vtest_send_transfer_put_data(vtws, ptr + buf_offset, size);
82 virgl_vtest_resource_unmap(vws, res);
83 return 0;
84 }
85
86 static int
87 virgl_vtest_transfer_get(struct virgl_winsys *vws,
88 struct virgl_hw_res *res,
89 const struct pipe_box *box,
90 uint32_t stride, uint32_t layer_stride,
91 uint32_t buf_offset, uint32_t level)
92 {
93 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
94 uint32_t size;
95 void *ptr;
96 uint32_t valid_stride;
97
98 size = vtest_get_transfer_size(res, box, stride, layer_stride, level, &valid_stride);
99
100 virgl_vtest_send_transfer_cmd(vtws, VCMD_TRANSFER_GET, res->res_handle,
101 level, stride, layer_stride,
102 box, size);
103
104
105 ptr = virgl_vtest_resource_map(vws, res);
106 virgl_vtest_recv_transfer_get_data(vtws, ptr + buf_offset, size, valid_stride, box, res->format);
107 virgl_vtest_resource_unmap(vws, res);
108 return 0;
109 }
110
111 static void virgl_hw_res_destroy(struct virgl_vtest_winsys *vtws,
112 struct virgl_hw_res *res)
113 {
114 virgl_vtest_send_resource_unref(vtws, res->res_handle);
115 if (res->dt)
116 vtws->sws->displaytarget_destroy(vtws->sws, res->dt);
117 free(res->ptr);
118 FREE(res);
119 }
120
121 static boolean virgl_vtest_resource_is_busy(struct virgl_vtest_winsys *vtws,
122 struct virgl_hw_res *res)
123 {
124 /* implement busy check */
125 int ret;
126 ret = virgl_vtest_busy_wait(vtws, res->res_handle, 0);
127
128 if (ret < 0)
129 return FALSE;
130
131 return ret == 1 ? TRUE : FALSE;
132 }
133
134 static void
135 virgl_cache_flush(struct virgl_vtest_winsys *vtws)
136 {
137 struct list_head *curr, *next;
138 struct virgl_hw_res *res;
139
140 pipe_mutex_lock(vtws->mutex);
141 curr = vtws->delayed.next;
142 next = curr->next;
143
144 while (curr != &vtws->delayed) {
145 res = LIST_ENTRY(struct virgl_hw_res, curr, head);
146 LIST_DEL(&res->head);
147 virgl_hw_res_destroy(vtws, res);
148 curr = next;
149 next = curr->next;
150 }
151 pipe_mutex_unlock(vtws->mutex);
152 }
153
154 static void
155 virgl_cache_list_check_free(struct virgl_vtest_winsys *vtws)
156 {
157 struct list_head *curr, *next;
158 struct virgl_hw_res *res;
159 int64_t now;
160
161 now = os_time_get();
162 curr = vtws->delayed.next;
163 next = curr->next;
164 while (curr != &vtws->delayed) {
165 res = LIST_ENTRY(struct virgl_hw_res, curr, head);
166 if (!os_time_timeout(res->start, res->end, now))
167 break;
168
169 LIST_DEL(&res->head);
170 virgl_hw_res_destroy(vtws, res);
171 curr = next;
172 next = curr->next;
173 }
174 }
175
176 static void virgl_vtest_resource_reference(struct virgl_vtest_winsys *vtws,
177 struct virgl_hw_res **dres,
178 struct virgl_hw_res *sres)
179 {
180 struct virgl_hw_res *old = *dres;
181 if (pipe_reference(&(*dres)->reference, &sres->reference)) {
182 if (!can_cache_resource(old)) {
183 virgl_hw_res_destroy(vtws, old);
184 } else {
185 pipe_mutex_lock(vtws->mutex);
186 virgl_cache_list_check_free(vtws);
187
188 old->start = os_time_get();
189 old->end = old->start + vtws->usecs;
190 LIST_ADDTAIL(&old->head, &vtws->delayed);
191 vtws->num_delayed++;
192 pipe_mutex_unlock(vtws->mutex);
193 }
194 }
195 *dres = sres;
196 }
197
198 static struct virgl_hw_res *virgl_vtest_winsys_resource_create(
199 struct virgl_winsys *vws,
200 enum pipe_texture_target target,
201 uint32_t format,
202 uint32_t bind,
203 uint32_t width,
204 uint32_t height,
205 uint32_t depth,
206 uint32_t array_size,
207 uint32_t last_level,
208 uint32_t nr_samples,
209 uint32_t size)
210 {
211 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
212 struct virgl_hw_res *res;
213 static int handle = 1;
214
215 res = CALLOC_STRUCT(virgl_hw_res);
216 if (!res)
217 return NULL;
218
219 if (bind & (VIRGL_BIND_DISPLAY_TARGET | VIRGL_BIND_SCANOUT)) {
220 res->dt = vtws->sws->displaytarget_create(vtws->sws,
221 bind,
222 format,
223 width,
224 height,
225 64,
226 &res->stride);
227
228 } else {
229 res->ptr = align_malloc(size, 64);
230 if (!res->ptr) {
231 FREE(res);
232 return NULL;
233 }
234 }
235
236 res->bind = bind;
237 res->format = format;
238 res->height = height;
239 res->width = width;
240 virgl_vtest_send_resource_create(vtws, handle, target, format, bind, width,
241 height, depth, array_size, last_level,
242 nr_samples);
243
244 res->res_handle = handle++;
245 pipe_reference_init(&res->reference, 1);
246 return res;
247 }
248
249 static void virgl_vtest_winsys_resource_unref(struct virgl_winsys *vws,
250 struct virgl_hw_res *hres)
251 {
252 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
253 virgl_vtest_resource_reference(vtws, &hres, NULL);
254 }
255
256 static void *virgl_vtest_resource_map(struct virgl_winsys *vws, struct virgl_hw_res *res)
257 {
258 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
259
260 if (res->dt) {
261 return vtws->sws->displaytarget_map(vtws->sws, res->dt, 0);
262 } else {
263 res->mapped = res->ptr;
264 return res->mapped;
265 }
266 }
267
268 static void virgl_vtest_resource_unmap(struct virgl_winsys *vws, struct virgl_hw_res *res)
269 {
270 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
271 if (res->mapped)
272 res->mapped = NULL;
273
274 if (res->dt)
275 vtws->sws->displaytarget_unmap(vtws->sws, res->dt);
276 }
277
278 static void virgl_vtest_resource_wait(struct virgl_winsys *vws, struct virgl_hw_res *res)
279 {
280 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
281
282 virgl_vtest_busy_wait(vtws, res->res_handle, VCMD_BUSY_WAIT_FLAG_WAIT);
283 }
284
285 static inline int virgl_is_res_compat(struct virgl_vtest_winsys *vtws,
286 struct virgl_hw_res *res,
287 uint32_t size, uint32_t bind, uint32_t format)
288 {
289 if (res->bind != bind)
290 return 0;
291 if (res->format != format)
292 return 0;
293 if (res->size < size)
294 return 0;
295 if (res->size > size * 2)
296 return 0;
297
298 if (virgl_vtest_resource_is_busy(vtws, res)) {
299 return -1;
300 }
301
302 return 1;
303 }
304
305 static struct virgl_hw_res *virgl_vtest_winsys_resource_cache_create(struct virgl_winsys *vws,
306 enum pipe_texture_target target,
307 uint32_t format,
308 uint32_t bind,
309 uint32_t width,
310 uint32_t height,
311 uint32_t depth,
312 uint32_t array_size,
313 uint32_t last_level,
314 uint32_t nr_samples,
315 uint32_t size)
316 {
317 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
318 struct virgl_hw_res *res, *curr_res;
319 struct list_head *curr, *next;
320 int64_t now;
321 int ret;
322
323 /* only store binds for vertex/index/const buffers */
324 if (bind != VIRGL_BIND_CONSTANT_BUFFER && bind != VIRGL_BIND_INDEX_BUFFER &&
325 bind != VIRGL_BIND_VERTEX_BUFFER && bind != VIRGL_BIND_CUSTOM)
326 goto alloc;
327
328 pipe_mutex_lock(vtws->mutex);
329
330 res = NULL;
331 curr = vtws->delayed.next;
332 next = curr->next;
333
334 now = os_time_get();
335 while (curr != &vtws->delayed) {
336 curr_res = LIST_ENTRY(struct virgl_hw_res, curr, head);
337
338 if (!res && (ret = virgl_is_res_compat(vtws, curr_res, size, bind, format) > 0))
339 res = curr_res;
340 else if (os_time_timeout(curr_res->start, curr_res->end, now)) {
341 LIST_DEL(&curr_res->head);
342 virgl_hw_res_destroy(vtws, curr_res);
343 } else
344 break;
345
346 if (ret == -1)
347 break;
348
349 curr = next;
350 next = curr->next;
351 }
352
353 if (!res && ret != -1) {
354 while (curr != &vtws->delayed) {
355 curr_res = LIST_ENTRY(struct virgl_hw_res, curr, head);
356 ret = virgl_is_res_compat(vtws, curr_res, size, bind, format);
357 if (ret > 0) {
358 res = curr_res;
359 break;
360 }
361 if (ret == -1)
362 break;
363 curr = next;
364 next = curr->next;
365 }
366 }
367
368 if (res) {
369 LIST_DEL(&res->head);
370 --vtws->num_delayed;
371 pipe_mutex_unlock(vtws->mutex);
372 pipe_reference_init(&res->reference, 1);
373 return res;
374 }
375
376 pipe_mutex_unlock(vtws->mutex);
377
378 alloc:
379 res = virgl_vtest_winsys_resource_create(vws, target, format, bind,
380 width, height, depth, array_size,
381 last_level, nr_samples, size);
382 if (bind == VIRGL_BIND_CONSTANT_BUFFER || bind == VIRGL_BIND_INDEX_BUFFER ||
383 bind == VIRGL_BIND_VERTEX_BUFFER)
384 res->cacheable = TRUE;
385 return res;
386 }
387
388 static struct virgl_cmd_buf *virgl_vtest_cmd_buf_create(struct virgl_winsys *vws)
389 {
390 struct virgl_vtest_cmd_buf *cbuf;
391
392 cbuf = CALLOC_STRUCT(virgl_vtest_cmd_buf);
393 if (!cbuf)
394 return NULL;
395
396 cbuf->nres = 512;
397 cbuf->res_bo = CALLOC(cbuf->nres, sizeof(struct virgl_hw_buf*));
398 if (!cbuf->res_bo) {
399 FREE(cbuf);
400 return NULL;
401 }
402 cbuf->ws = vws;
403 cbuf->base.buf = cbuf->buf;
404 return &cbuf->base;
405 }
406
407 static void virgl_vtest_cmd_buf_destroy(struct virgl_cmd_buf *_cbuf)
408 {
409 struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
410
411 FREE(cbuf->res_bo);
412 FREE(cbuf);
413 }
414
415 static boolean virgl_vtest_lookup_res(struct virgl_vtest_cmd_buf *cbuf,
416 struct virgl_hw_res *res)
417 {
418 unsigned hash = res->res_handle & (sizeof(cbuf->is_handle_added)-1);
419 int i;
420
421 if (cbuf->is_handle_added[hash]) {
422 i = cbuf->reloc_indices_hashlist[hash];
423 if (cbuf->res_bo[i] == res)
424 return true;
425
426 for (i = 0; i < cbuf->cres; i++) {
427 if (cbuf->res_bo[i] == res) {
428 cbuf->reloc_indices_hashlist[hash] = i;
429 return true;
430 }
431 }
432 }
433 return false;
434 }
435
436 static void virgl_vtest_release_all_res(struct virgl_vtest_winsys *vtws,
437 struct virgl_vtest_cmd_buf *cbuf)
438 {
439 int i;
440
441 for (i = 0; i < cbuf->cres; i++) {
442 p_atomic_dec(&cbuf->res_bo[i]->num_cs_references);
443 virgl_vtest_resource_reference(vtws, &cbuf->res_bo[i], NULL);
444 }
445 cbuf->cres = 0;
446 }
447
448 static void virgl_vtest_add_res(struct virgl_vtest_winsys *vtws,
449 struct virgl_vtest_cmd_buf *cbuf, struct virgl_hw_res *res)
450 {
451 unsigned hash = res->res_handle & (sizeof(cbuf->is_handle_added)-1);
452
453 if (cbuf->cres > cbuf->nres) {
454 fprintf(stderr,"failure to add relocation\n");
455 return;
456 }
457
458 cbuf->res_bo[cbuf->cres] = NULL;
459 virgl_vtest_resource_reference(vtws, &cbuf->res_bo[cbuf->cres], res);
460 cbuf->is_handle_added[hash] = TRUE;
461
462 cbuf->reloc_indices_hashlist[hash] = cbuf->cres;
463 p_atomic_inc(&res->num_cs_references);
464 cbuf->cres++;
465 }
466
467 static int virgl_vtest_winsys_submit_cmd(struct virgl_winsys *vws, struct virgl_cmd_buf *_cbuf)
468 {
469 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
470 struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
471 int ret;
472
473 if (cbuf->base.cdw == 0)
474 return 0;
475
476 ret = virgl_vtest_submit_cmd(vtws, cbuf);
477
478 virgl_vtest_release_all_res(vtws, cbuf);
479 memset(cbuf->is_handle_added, 0, sizeof(cbuf->is_handle_added));
480 cbuf->base.cdw = 0;
481 return ret;
482 }
483
484 static void virgl_vtest_emit_res(struct virgl_winsys *vws, struct virgl_cmd_buf *_cbuf, struct virgl_hw_res *res, boolean write_buf)
485 {
486 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
487 struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
488 boolean already_in_list = virgl_vtest_lookup_res(cbuf, res);
489
490 if (write_buf)
491 cbuf->base.buf[cbuf->base.cdw++] = res->res_handle;
492 if (!already_in_list)
493 virgl_vtest_add_res(vtws, cbuf, res);
494 }
495
496 static boolean virgl_vtest_res_is_ref(struct virgl_winsys *vws,
497 struct virgl_cmd_buf *_cbuf,
498 struct virgl_hw_res *res)
499 {
500 if (!res->num_cs_references)
501 return FALSE;
502
503 return TRUE;
504 }
505
506 static int virgl_vtest_get_caps(struct virgl_winsys *vws, struct virgl_drm_caps *caps)
507 {
508 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
509 return virgl_vtest_send_get_caps(vtws, caps);
510 }
511
512 static struct pipe_fence_handle *
513 virgl_cs_create_fence(struct virgl_winsys *vws)
514 {
515 struct virgl_hw_res *res;
516
517 res = virgl_vtest_winsys_resource_cache_create(vws,
518 PIPE_BUFFER,
519 PIPE_FORMAT_R8_UNORM,
520 PIPE_BIND_CUSTOM,
521 8, 1, 1, 0, 0, 0, 8);
522
523 return (struct pipe_fence_handle *)res;
524 }
525
526 static bool virgl_fence_wait(struct virgl_winsys *vws,
527 struct pipe_fence_handle *fence,
528 uint64_t timeout)
529 {
530 struct virgl_vtest_winsys *vdws = virgl_vtest_winsys(vws);
531 struct virgl_hw_res *res = virgl_hw_res(fence);
532
533 if (timeout == 0)
534 return virgl_vtest_resource_is_busy(vdws, res);
535
536 if (timeout != PIPE_TIMEOUT_INFINITE) {
537 int64_t start_time = os_time_get();
538 timeout /= 1000;
539 while (virgl_vtest_resource_is_busy(vdws, res)) {
540 if (os_time_get() - start_time >= timeout)
541 return FALSE;
542 os_time_sleep(10);
543 }
544 return TRUE;
545 }
546 virgl_vtest_resource_wait(vws, res);
547 return TRUE;
548 }
549
550 static void virgl_fence_reference(struct virgl_winsys *vws,
551 struct pipe_fence_handle **dst,
552 struct pipe_fence_handle *src)
553 {
554 struct virgl_vtest_winsys *vdws = virgl_vtest_winsys(vws);
555 virgl_vtest_resource_reference(vdws, (struct virgl_hw_res **)dst,
556 virgl_hw_res(src));
557 }
558
559 static void virgl_vtest_flush_frontbuffer(struct virgl_winsys *vws,
560 struct virgl_hw_res *res,
561 unsigned level, unsigned layer,
562 void *winsys_drawable_handle,
563 struct pipe_box *sub_box)
564 {
565 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
566 struct pipe_box box;
567 void *map;
568 uint32_t size;
569 uint32_t offset = 0, valid_stride;
570 if (!res->dt)
571 return;
572
573 memset(&box, 0, sizeof(box));
574
575 if (sub_box) {
576 box = *sub_box;
577 offset = (res->stride * (box.y / util_format_get_blockheight(res->format))) + (box.x / util_format_get_blockwidth(res->format)) * util_format_get_blocksize(res->format);
578 } else {
579 box.z = layer;
580 box.width = res->width;
581 box.height = res->height;
582 box.depth = 1;
583 }
584
585 size = vtest_get_transfer_size(res, &box, res->stride, 0, level, &valid_stride);
586
587 virgl_vtest_busy_wait(vtws, res->res_handle, VCMD_BUSY_WAIT_FLAG_WAIT);
588 map = vtws->sws->displaytarget_map(vtws->sws, res->dt, 0);
589
590 /* execute a transfer */
591 virgl_vtest_send_transfer_cmd(vtws, VCMD_TRANSFER_GET, res->res_handle,
592 level, res->stride, 0, &box, size);
593 virgl_vtest_recv_transfer_get_data(vtws, map + offset, size, valid_stride, &box, res->format);
594 vtws->sws->displaytarget_unmap(vtws->sws, res->dt);
595
596 vtws->sws->displaytarget_display(vtws->sws, res->dt, winsys_drawable_handle, sub_box);
597 }
598
599 static void
600 virgl_vtest_winsys_destroy(struct virgl_winsys *vws)
601 {
602 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
603
604 virgl_cache_flush(vtws);
605
606 pipe_mutex_destroy(vtws->mutex);
607 FREE(vtws);
608 }
609
610 struct virgl_winsys *
611 virgl_vtest_winsys_wrap(struct sw_winsys *sws)
612 {
613 struct virgl_vtest_winsys *vtws;
614
615 vtws = CALLOC_STRUCT(virgl_vtest_winsys);
616 if (!vtws)
617 return NULL;
618
619 virgl_vtest_connect(vtws);
620 vtws->sws = sws;
621
622 vtws->usecs = 1000000;
623 LIST_INITHEAD(&vtws->delayed);
624 pipe_mutex_init(vtws->mutex);
625
626 vtws->base.destroy = virgl_vtest_winsys_destroy;
627
628 vtws->base.transfer_put = virgl_vtest_transfer_put;
629 vtws->base.transfer_get = virgl_vtest_transfer_get;
630
631 vtws->base.resource_create = virgl_vtest_winsys_resource_cache_create;
632 vtws->base.resource_unref = virgl_vtest_winsys_resource_unref;
633 vtws->base.resource_map = virgl_vtest_resource_map;
634 vtws->base.resource_wait = virgl_vtest_resource_wait;
635 vtws->base.cmd_buf_create = virgl_vtest_cmd_buf_create;
636 vtws->base.cmd_buf_destroy = virgl_vtest_cmd_buf_destroy;
637 vtws->base.submit_cmd = virgl_vtest_winsys_submit_cmd;
638
639 vtws->base.emit_res = virgl_vtest_emit_res;
640 vtws->base.res_is_referenced = virgl_vtest_res_is_ref;
641 vtws->base.get_caps = virgl_vtest_get_caps;
642
643 vtws->base.cs_create_fence = virgl_cs_create_fence;
644 vtws->base.fence_wait = virgl_fence_wait;
645 vtws->base.fence_reference = virgl_fence_reference;
646
647 vtws->base.flush_frontbuffer = virgl_vtest_flush_frontbuffer;
648
649 return &vtws->base;
650 }