winsys/virgl: throw in some inline wrappers
[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 = (struct virgl_hw_res **)
398 CALLOC(cbuf->nres, sizeof(struct virgl_hw_buf*));
399 if (!cbuf->res_bo) {
400 FREE(cbuf);
401 return NULL;
402 }
403 cbuf->ws = vws;
404 cbuf->base.buf = cbuf->buf;
405 return &cbuf->base;
406 }
407
408 static void virgl_vtest_cmd_buf_destroy(struct virgl_cmd_buf *_cbuf)
409 {
410 struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
411
412 FREE(cbuf->res_bo);
413 FREE(cbuf);
414 }
415
416 static boolean virgl_vtest_lookup_res(struct virgl_vtest_cmd_buf *cbuf,
417 struct virgl_hw_res *res)
418 {
419 unsigned hash = res->res_handle & (sizeof(cbuf->is_handle_added)-1);
420 int i;
421
422 if (cbuf->is_handle_added[hash]) {
423 i = cbuf->reloc_indices_hashlist[hash];
424 if (cbuf->res_bo[i] == res)
425 return true;
426
427 for (i = 0; i < cbuf->cres; i++) {
428 if (cbuf->res_bo[i] == res) {
429 cbuf->reloc_indices_hashlist[hash] = i;
430 return true;
431 }
432 }
433 }
434 return false;
435 }
436
437 static void virgl_vtest_release_all_res(struct virgl_vtest_winsys *vtws,
438 struct virgl_vtest_cmd_buf *cbuf)
439 {
440 int i;
441
442 for (i = 0; i < cbuf->cres; i++) {
443 p_atomic_dec(&cbuf->res_bo[i]->num_cs_references);
444 virgl_vtest_resource_reference(vtws, &cbuf->res_bo[i], NULL);
445 }
446 cbuf->cres = 0;
447 }
448
449 static void virgl_vtest_add_res(struct virgl_vtest_winsys *vtws,
450 struct virgl_vtest_cmd_buf *cbuf, struct virgl_hw_res *res)
451 {
452 unsigned hash = res->res_handle & (sizeof(cbuf->is_handle_added)-1);
453
454 if (cbuf->cres > cbuf->nres) {
455 fprintf(stderr,"failure to add relocation\n");
456 return;
457 }
458
459 cbuf->res_bo[cbuf->cres] = NULL;
460 virgl_vtest_resource_reference(vtws, &cbuf->res_bo[cbuf->cres], res);
461 cbuf->is_handle_added[hash] = TRUE;
462
463 cbuf->reloc_indices_hashlist[hash] = cbuf->cres;
464 p_atomic_inc(&res->num_cs_references);
465 cbuf->cres++;
466 }
467
468 static int virgl_vtest_winsys_submit_cmd(struct virgl_winsys *vws, struct virgl_cmd_buf *_cbuf)
469 {
470 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
471 struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
472 int ret;
473
474 if (cbuf->base.cdw == 0)
475 return 0;
476
477 ret = virgl_vtest_submit_cmd(vtws, cbuf);
478
479 virgl_vtest_release_all_res(vtws, cbuf);
480 memset(cbuf->is_handle_added, 0, sizeof(cbuf->is_handle_added));
481 cbuf->base.cdw = 0;
482 return ret;
483 }
484
485 static void virgl_vtest_emit_res(struct virgl_winsys *vws, struct virgl_cmd_buf *_cbuf, struct virgl_hw_res *res, boolean write_buf)
486 {
487 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
488 struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);
489 boolean already_in_list = virgl_vtest_lookup_res(cbuf, res);
490
491 if (write_buf)
492 cbuf->base.buf[cbuf->base.cdw++] = res->res_handle;
493 if (!already_in_list)
494 virgl_vtest_add_res(vtws, cbuf, res);
495 }
496
497 static boolean virgl_vtest_res_is_ref(struct virgl_winsys *vws,
498 struct virgl_cmd_buf *_cbuf,
499 struct virgl_hw_res *res)
500 {
501 if (!res->num_cs_references)
502 return FALSE;
503
504 return TRUE;
505 }
506
507 static int virgl_vtest_get_caps(struct virgl_winsys *vws, struct virgl_drm_caps *caps)
508 {
509 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
510 return virgl_vtest_send_get_caps(vtws, caps);
511 }
512
513 static struct pipe_fence_handle *
514 virgl_cs_create_fence(struct virgl_winsys *vws)
515 {
516 struct virgl_hw_res *res;
517
518 res = virgl_vtest_winsys_resource_cache_create(vws,
519 PIPE_BUFFER,
520 PIPE_FORMAT_R8_UNORM,
521 PIPE_BIND_CUSTOM,
522 8, 1, 1, 0, 0, 0, 8);
523
524 return (struct pipe_fence_handle *)res;
525 }
526
527 static bool virgl_fence_wait(struct virgl_winsys *vws,
528 struct pipe_fence_handle *fence,
529 uint64_t timeout)
530 {
531 struct virgl_vtest_winsys *vdws = virgl_vtest_winsys(vws);
532 struct virgl_hw_res *res = virgl_hw_res(fence);
533
534 if (timeout == 0)
535 return virgl_vtest_resource_is_busy(vdws, res);
536
537 if (timeout != PIPE_TIMEOUT_INFINITE) {
538 int64_t start_time = os_time_get();
539 timeout /= 1000;
540 while (virgl_vtest_resource_is_busy(vdws, res)) {
541 if (os_time_get() - start_time >= timeout)
542 return FALSE;
543 os_time_sleep(10);
544 }
545 return TRUE;
546 }
547 virgl_vtest_resource_wait(vws, res);
548 return TRUE;
549 }
550
551 static void virgl_fence_reference(struct virgl_winsys *vws,
552 struct pipe_fence_handle **dst,
553 struct pipe_fence_handle *src)
554 {
555 struct virgl_vtest_winsys *vdws = virgl_vtest_winsys(vws);
556 virgl_vtest_resource_reference(vdws, (struct virgl_hw_res **)dst,
557 virgl_hw_res(src));
558 }
559
560 static void virgl_vtest_flush_frontbuffer(struct virgl_winsys *vws,
561 struct virgl_hw_res *res,
562 unsigned level, unsigned layer,
563 void *winsys_drawable_handle,
564 struct pipe_box *sub_box)
565 {
566 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
567 struct pipe_box box;
568 void *map;
569 uint32_t size;
570 uint32_t offset = 0, valid_stride;
571 if (!res->dt)
572 return;
573
574 memset(&box, 0, sizeof(box));
575
576 if (sub_box) {
577 box = *sub_box;
578 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);
579 } else {
580 box.z = layer;
581 box.width = res->width;
582 box.height = res->height;
583 box.depth = 1;
584 }
585
586 size = vtest_get_transfer_size(res, &box, res->stride, 0, level, &valid_stride);
587
588 virgl_vtest_busy_wait(vtws, res->res_handle, VCMD_BUSY_WAIT_FLAG_WAIT);
589 map = vtws->sws->displaytarget_map(vtws->sws, res->dt, 0);
590
591 /* execute a transfer */
592 virgl_vtest_send_transfer_cmd(vtws, VCMD_TRANSFER_GET, res->res_handle,
593 level, res->stride, 0, &box, size);
594 virgl_vtest_recv_transfer_get_data(vtws, map + offset, size, valid_stride, &box, res->format);
595 vtws->sws->displaytarget_unmap(vtws->sws, res->dt);
596
597 vtws->sws->displaytarget_display(vtws->sws, res->dt, winsys_drawable_handle, sub_box);
598 }
599
600 static void
601 virgl_vtest_winsys_destroy(struct virgl_winsys *vws)
602 {
603 struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);
604
605 virgl_cache_flush(vtws);
606
607 pipe_mutex_destroy(vtws->mutex);
608 FREE(vtws);
609 }
610
611 struct virgl_winsys *
612 virgl_vtest_winsys_wrap(struct sw_winsys *sws)
613 {
614 struct virgl_vtest_winsys *vtws;
615
616 vtws = CALLOC_STRUCT(virgl_vtest_winsys);
617 if (!vtws)
618 return NULL;
619
620 virgl_vtest_connect(vtws);
621 vtws->sws = sws;
622
623 vtws->usecs = 1000000;
624 LIST_INITHEAD(&vtws->delayed);
625 pipe_mutex_init(vtws->mutex);
626
627 vtws->base.destroy = virgl_vtest_winsys_destroy;
628
629 vtws->base.transfer_put = virgl_vtest_transfer_put;
630 vtws->base.transfer_get = virgl_vtest_transfer_get;
631
632 vtws->base.resource_create = virgl_vtest_winsys_resource_cache_create;
633 vtws->base.resource_unref = virgl_vtest_winsys_resource_unref;
634 vtws->base.resource_map = virgl_vtest_resource_map;
635 vtws->base.resource_wait = virgl_vtest_resource_wait;
636 vtws->base.cmd_buf_create = virgl_vtest_cmd_buf_create;
637 vtws->base.cmd_buf_destroy = virgl_vtest_cmd_buf_destroy;
638 vtws->base.submit_cmd = virgl_vtest_winsys_submit_cmd;
639
640 vtws->base.emit_res = virgl_vtest_emit_res;
641 vtws->base.res_is_referenced = virgl_vtest_res_is_ref;
642 vtws->base.get_caps = virgl_vtest_get_caps;
643
644 vtws->base.cs_create_fence = virgl_cs_create_fence;
645 vtws->base.fence_wait = virgl_fence_wait;
646 vtws->base.fence_reference = virgl_fence_reference;
647
648 vtws->base.flush_frontbuffer = virgl_vtest_flush_frontbuffer;
649
650 return &vtws->base;
651 }