Path: blob/21.2-virgl/src/gallium/winsys/virgl/vtest/virgl_vtest_winsys.c
4566 views
/*1* Copyright 2014, 2015 Red Hat.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE.21*/22#include <stdio.h>23#include "util/u_surface.h"24#include "util/u_memory.h"25#include "util/format/u_format.h"26#include "util/u_inlines.h"27#include "util/os_time.h"28#include "frontend/sw_winsys.h"29#include "os/os_mman.h"3031#include "virgl_vtest_winsys.h"32#include "virgl_vtest_public.h"3334/* Gets a pointer to the virgl_hw_res containing the pointed to cache entry. */35#define cache_entry_container_res(ptr) \36(struct virgl_hw_res*)((char*)ptr - offsetof(struct virgl_hw_res, cache_entry))3738static void *virgl_vtest_resource_map(struct virgl_winsys *vws,39struct virgl_hw_res *res);40static void virgl_vtest_resource_unmap(struct virgl_winsys *vws,41struct virgl_hw_res *res);4243static inline boolean can_cache_resource_with_bind(uint32_t bind)44{45return bind == VIRGL_BIND_CONSTANT_BUFFER ||46bind == VIRGL_BIND_INDEX_BUFFER ||47bind == VIRGL_BIND_VERTEX_BUFFER ||48bind == VIRGL_BIND_CUSTOM ||49bind == VIRGL_BIND_STAGING;50}5152static uint32_t vtest_get_transfer_size(struct virgl_hw_res *res,53const struct pipe_box *box,54uint32_t stride, uint32_t layer_stride,55uint32_t level, uint32_t *valid_stride_p)56{57uint32_t valid_stride, valid_layer_stride;5859valid_stride = util_format_get_stride(res->format, box->width);60if (stride) {61if (box->height > 1)62valid_stride = stride;63}6465valid_layer_stride = util_format_get_2d_size(res->format, valid_stride,66box->height);67if (layer_stride) {68if (box->depth > 1)69valid_layer_stride = layer_stride;70}7172*valid_stride_p = valid_stride;73return valid_layer_stride * box->depth;74}7576static int77virgl_vtest_transfer_put(struct virgl_winsys *vws,78struct virgl_hw_res *res,79const struct pipe_box *box,80uint32_t stride, uint32_t layer_stride,81uint32_t buf_offset, uint32_t level)82{83struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);84uint32_t size;85void *ptr;86uint32_t valid_stride;8788size = vtest_get_transfer_size(res, box, stride, layer_stride, level,89&valid_stride);9091virgl_vtest_send_transfer_put(vtws, res->res_handle,92level, stride, layer_stride,93box, size, buf_offset);9495if (vtws->protocol_version >= 2)96return 0;9798ptr = virgl_vtest_resource_map(vws, res);99virgl_vtest_send_transfer_put_data(vtws, ptr + buf_offset, size);100virgl_vtest_resource_unmap(vws, res);101return 0;102}103104static int105virgl_vtest_transfer_get_internal(struct virgl_winsys *vws,106struct virgl_hw_res *res,107const struct pipe_box *box,108uint32_t stride, uint32_t layer_stride,109uint32_t buf_offset, uint32_t level,110bool flush_front_buffer)111{112struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);113uint32_t size;114void *ptr;115uint32_t valid_stride;116117size = vtest_get_transfer_size(res, box, stride, layer_stride, level,118&valid_stride);119virgl_vtest_send_transfer_get(vtws, res->res_handle,120level, stride, layer_stride,121box, size, buf_offset);122123if (flush_front_buffer || vtws->protocol_version >= 2)124virgl_vtest_busy_wait(vtws, res->res_handle, VCMD_BUSY_WAIT_FLAG_WAIT);125126if (vtws->protocol_version >= 2) {127if (flush_front_buffer) {128if (box->depth > 1 || box->z > 1) {129fprintf(stderr, "Expected a 2D resource, received a 3D resource\n");130return -1;131}132133void *dt_map;134uint32_t shm_stride;135136/*137* The display target is aligned to 64 bytes, while the shared resource138* between the client/server is not.139*/140shm_stride = util_format_get_stride(res->format, res->width);141ptr = virgl_vtest_resource_map(vws, res);142dt_map = vtws->sws->displaytarget_map(vtws->sws, res->dt, 0);143144util_copy_rect(dt_map, res->format, res->stride, box->x, box->y,145box->width, box->height, ptr, shm_stride, box->x,146box->y);147148virgl_vtest_resource_unmap(vws, res);149vtws->sws->displaytarget_unmap(vtws->sws, res->dt);150}151} else {152ptr = virgl_vtest_resource_map(vws, res);153virgl_vtest_recv_transfer_get_data(vtws, ptr + buf_offset, size,154valid_stride, box, res->format);155virgl_vtest_resource_unmap(vws, res);156}157158return 0;159}160161static int162virgl_vtest_transfer_get(struct virgl_winsys *vws,163struct virgl_hw_res *res,164const struct pipe_box *box,165uint32_t stride, uint32_t layer_stride,166uint32_t buf_offset, uint32_t level)167{168return virgl_vtest_transfer_get_internal(vws, res, box, stride,169layer_stride, buf_offset,170level, false);171}172173static void virgl_hw_res_destroy(struct virgl_vtest_winsys *vtws,174struct virgl_hw_res *res)175{176virgl_vtest_send_resource_unref(vtws, res->res_handle);177if (res->dt)178vtws->sws->displaytarget_destroy(vtws->sws, res->dt);179if (vtws->protocol_version >= 2) {180if (res->ptr)181os_munmap(res->ptr, res->size);182} else {183align_free(res->ptr);184}185186FREE(res);187}188189static boolean virgl_vtest_resource_is_busy(struct virgl_winsys *vws,190struct virgl_hw_res *res)191{192struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);193194/* implement busy check */195int ret;196ret = virgl_vtest_busy_wait(vtws, res->res_handle, 0);197198if (ret < 0)199return FALSE;200201return ret == 1 ? TRUE : FALSE;202}203204static void virgl_vtest_resource_reference(struct virgl_winsys *vws,205struct virgl_hw_res **dres,206struct virgl_hw_res *sres)207{208struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);209struct virgl_hw_res *old = *dres;210211if (pipe_reference(&(*dres)->reference, &sres->reference)) {212if (!can_cache_resource_with_bind(old->bind)) {213virgl_hw_res_destroy(vtws, old);214} else {215mtx_lock(&vtws->mutex);216virgl_resource_cache_add(&vtws->cache, &old->cache_entry);217mtx_unlock(&vtws->mutex);218}219}220*dres = sres;221}222223static struct virgl_hw_res *224virgl_vtest_winsys_resource_create(struct virgl_winsys *vws,225enum pipe_texture_target target,226uint32_t format,227uint32_t bind,228uint32_t width,229uint32_t height,230uint32_t depth,231uint32_t array_size,232uint32_t last_level,233uint32_t nr_samples,234uint32_t size)235{236struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);237struct virgl_hw_res *res;238static int handle = 1;239int fd = -1;240241res = CALLOC_STRUCT(virgl_hw_res);242if (!res)243return NULL;244245if (bind & (VIRGL_BIND_DISPLAY_TARGET | VIRGL_BIND_SCANOUT)) {246res->dt = vtws->sws->displaytarget_create(vtws->sws, bind, format,247width, height, 64, NULL,248&res->stride);249250} else if (vtws->protocol_version < 2) {251res->ptr = align_malloc(size, 64);252if (!res->ptr) {253FREE(res);254return NULL;255}256}257258res->bind = bind;259res->format = format;260res->height = height;261res->width = width;262res->size = size;263virgl_vtest_send_resource_create(vtws, handle, target, pipe_to_virgl_format(format), bind,264width, height, depth, array_size,265last_level, nr_samples, size, &fd);266267if (vtws->protocol_version >= 2) {268if (res->size == 0) {269res->ptr = NULL;270goto out;271}272273if (fd < 0) {274FREE(res);275fprintf(stderr, "Unable to get a valid fd\n");276return NULL;277}278279res->ptr = os_mmap(NULL, res->size, PROT_WRITE | PROT_READ, MAP_SHARED,280fd, 0);281282if (res->ptr == MAP_FAILED) {283fprintf(stderr, "Client failed to map shared memory region\n");284close(fd);285FREE(res);286return NULL;287}288289close(fd);290}291292out:293virgl_resource_cache_entry_init(&res->cache_entry, size, bind, format, 0);294res->res_handle = handle++;295pipe_reference_init(&res->reference, 1);296p_atomic_set(&res->num_cs_references, 0);297return res;298}299300static void *virgl_vtest_resource_map(struct virgl_winsys *vws,301struct virgl_hw_res *res)302{303struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);304305/*306* With protocol v0 we can either have a display target or a resource backing307* store. With protocol v2 we can have both, so only return the memory mapped308* backing store in this function. We can copy to the display target when309* appropriate.310*/311if (vtws->protocol_version >= 2 || !res->dt) {312res->mapped = res->ptr;313return res->mapped;314} else {315return vtws->sws->displaytarget_map(vtws->sws, res->dt, 0);316}317}318319static void virgl_vtest_resource_unmap(struct virgl_winsys *vws,320struct virgl_hw_res *res)321{322struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);323if (res->mapped)324res->mapped = NULL;325326if (res->dt && vtws->protocol_version < 2)327vtws->sws->displaytarget_unmap(vtws->sws, res->dt);328}329330static void virgl_vtest_resource_wait(struct virgl_winsys *vws,331struct virgl_hw_res *res)332{333struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);334335virgl_vtest_busy_wait(vtws, res->res_handle, VCMD_BUSY_WAIT_FLAG_WAIT);336}337338static struct virgl_hw_res *339virgl_vtest_winsys_resource_cache_create(struct virgl_winsys *vws,340enum pipe_texture_target target,341uint32_t format,342uint32_t bind,343uint32_t width,344uint32_t height,345uint32_t depth,346uint32_t array_size,347uint32_t last_level,348uint32_t nr_samples,349uint32_t flags,350uint32_t size)351{352struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);353struct virgl_hw_res *res;354struct virgl_resource_cache_entry *entry;355356if (!can_cache_resource_with_bind(bind))357goto alloc;358359mtx_lock(&vtws->mutex);360361entry = virgl_resource_cache_remove_compatible(&vtws->cache, size,362bind, format, 0);363if (entry) {364res = cache_entry_container_res(entry);365mtx_unlock(&vtws->mutex);366pipe_reference_init(&res->reference, 1);367return res;368}369370mtx_unlock(&vtws->mutex);371372alloc:373res = virgl_vtest_winsys_resource_create(vws, target, format, bind,374width, height, depth, array_size,375last_level, nr_samples, size);376return res;377}378379static boolean virgl_vtest_lookup_res(struct virgl_vtest_cmd_buf *cbuf,380struct virgl_hw_res *res)381{382unsigned hash = res->res_handle & (sizeof(cbuf->is_handle_added)-1);383int i;384385if (cbuf->is_handle_added[hash]) {386i = cbuf->reloc_indices_hashlist[hash];387if (cbuf->res_bo[i] == res)388return true;389390for (i = 0; i < cbuf->cres; i++) {391if (cbuf->res_bo[i] == res) {392cbuf->reloc_indices_hashlist[hash] = i;393return true;394}395}396}397return false;398}399400static void virgl_vtest_release_all_res(struct virgl_vtest_winsys *vtws,401struct virgl_vtest_cmd_buf *cbuf)402{403int i;404405for (i = 0; i < cbuf->cres; i++) {406p_atomic_dec(&cbuf->res_bo[i]->num_cs_references);407virgl_vtest_resource_reference(&vtws->base, &cbuf->res_bo[i], NULL);408}409cbuf->cres = 0;410}411412static void virgl_vtest_add_res(struct virgl_vtest_winsys *vtws,413struct virgl_vtest_cmd_buf *cbuf,414struct virgl_hw_res *res)415{416unsigned hash = res->res_handle & (sizeof(cbuf->is_handle_added)-1);417418if (cbuf->cres >= cbuf->nres) {419unsigned new_nres = cbuf->nres + 256;420struct virgl_hw_res **new_re_bo = REALLOC(cbuf->res_bo,421cbuf->nres * sizeof(struct virgl_hw_buf*),422new_nres * sizeof(struct virgl_hw_buf*));423if (!new_re_bo) {424fprintf(stderr,"failure to add relocation %d, %d\n", cbuf->cres, cbuf->nres);425return;426}427428cbuf->res_bo = new_re_bo;429cbuf->nres = new_nres;430}431432cbuf->res_bo[cbuf->cres] = NULL;433virgl_vtest_resource_reference(&vtws->base, &cbuf->res_bo[cbuf->cres], res);434cbuf->is_handle_added[hash] = TRUE;435436cbuf->reloc_indices_hashlist[hash] = cbuf->cres;437p_atomic_inc(&res->num_cs_references);438cbuf->cres++;439}440441static struct virgl_cmd_buf *virgl_vtest_cmd_buf_create(struct virgl_winsys *vws,442uint32_t size)443{444struct virgl_vtest_cmd_buf *cbuf;445446cbuf = CALLOC_STRUCT(virgl_vtest_cmd_buf);447if (!cbuf)448return NULL;449450cbuf->nres = 512;451cbuf->res_bo = CALLOC(cbuf->nres, sizeof(struct virgl_hw_buf*));452if (!cbuf->res_bo) {453FREE(cbuf);454return NULL;455}456457cbuf->buf = CALLOC(size, sizeof(uint32_t));458if (!cbuf->buf) {459FREE(cbuf->res_bo);460FREE(cbuf);461return NULL;462}463464cbuf->ws = vws;465cbuf->base.buf = cbuf->buf;466return &cbuf->base;467}468469static void virgl_vtest_cmd_buf_destroy(struct virgl_cmd_buf *_cbuf)470{471struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);472473virgl_vtest_release_all_res(virgl_vtest_winsys(cbuf->ws), cbuf);474FREE(cbuf->res_bo);475FREE(cbuf->buf);476FREE(cbuf);477}478479static struct pipe_fence_handle *480virgl_vtest_fence_create(struct virgl_winsys *vws)481{482struct virgl_hw_res *res;483484/* Resources for fences should not be from the cache, since we are basing485* the fence status on the resource creation busy status.486*/487res = virgl_vtest_winsys_resource_create(vws,488PIPE_BUFFER,489PIPE_FORMAT_R8_UNORM,490VIRGL_BIND_CUSTOM,4918, 1, 1, 0, 0, 0, 8);492493return (struct pipe_fence_handle *)res;494}495496static int virgl_vtest_winsys_submit_cmd(struct virgl_winsys *vws,497struct virgl_cmd_buf *_cbuf,498struct pipe_fence_handle **fence)499{500struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);501struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);502int ret;503504if (cbuf->base.cdw == 0)505return 0;506507ret = virgl_vtest_submit_cmd(vtws, cbuf);508if (fence && ret == 0)509*fence = virgl_vtest_fence_create(vws);510511virgl_vtest_release_all_res(vtws, cbuf);512memset(cbuf->is_handle_added, 0, sizeof(cbuf->is_handle_added));513cbuf->base.cdw = 0;514return ret;515}516517static void virgl_vtest_emit_res(struct virgl_winsys *vws,518struct virgl_cmd_buf *_cbuf,519struct virgl_hw_res *res, boolean write_buf)520{521struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);522struct virgl_vtest_cmd_buf *cbuf = virgl_vtest_cmd_buf(_cbuf);523boolean already_in_list = virgl_vtest_lookup_res(cbuf, res);524525if (write_buf)526cbuf->base.buf[cbuf->base.cdw++] = res->res_handle;527if (!already_in_list)528virgl_vtest_add_res(vtws, cbuf, res);529}530531static boolean virgl_vtest_res_is_ref(struct virgl_winsys *vws,532struct virgl_cmd_buf *_cbuf,533struct virgl_hw_res *res)534{535if (!p_atomic_read(&res->num_cs_references))536return FALSE;537538return TRUE;539}540541static int virgl_vtest_get_caps(struct virgl_winsys *vws,542struct virgl_drm_caps *caps)543{544struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);545546virgl_ws_fill_new_caps_defaults(caps);547return virgl_vtest_send_get_caps(vtws, caps);548}549550static struct pipe_fence_handle *551virgl_cs_create_fence(struct virgl_winsys *vws, int fd)552{553return virgl_vtest_fence_create(vws);554}555556static bool virgl_fence_wait(struct virgl_winsys *vws,557struct pipe_fence_handle *fence,558uint64_t timeout)559{560struct virgl_hw_res *res = virgl_hw_res(fence);561562if (timeout == 0)563return !virgl_vtest_resource_is_busy(vws, res);564565if (timeout != PIPE_TIMEOUT_INFINITE) {566int64_t start_time = os_time_get();567timeout /= 1000;568while (virgl_vtest_resource_is_busy(vws, res)) {569if (os_time_get() - start_time >= timeout)570return FALSE;571os_time_sleep(10);572}573return TRUE;574}575virgl_vtest_resource_wait(vws, res);576return TRUE;577}578579static void virgl_fence_reference(struct virgl_winsys *vws,580struct pipe_fence_handle **dst,581struct pipe_fence_handle *src)582{583struct virgl_vtest_winsys *vdws = virgl_vtest_winsys(vws);584virgl_vtest_resource_reference(&vdws->base, (struct virgl_hw_res **)dst,585virgl_hw_res(src));586}587588static void virgl_vtest_flush_frontbuffer(struct virgl_winsys *vws,589struct virgl_hw_res *res,590unsigned level, unsigned layer,591void *winsys_drawable_handle,592struct pipe_box *sub_box)593{594struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);595struct pipe_box box;596uint32_t offset = 0;597if (!res->dt)598return;599600memset(&box, 0, sizeof(box));601602if (sub_box) {603box = *sub_box;604offset = box.y / util_format_get_blockheight(res->format) * res->stride +605box.x / util_format_get_blockwidth(res->format) * util_format_get_blocksize(res->format);606} else {607box.z = layer;608box.width = res->width;609box.height = res->height;610box.depth = 1;611}612613virgl_vtest_transfer_get_internal(vws, res, &box, res->stride, 0, offset,614level, true);615616vtws->sws->displaytarget_display(vtws->sws, res->dt, winsys_drawable_handle,617sub_box);618}619620static void621virgl_vtest_winsys_destroy(struct virgl_winsys *vws)622{623struct virgl_vtest_winsys *vtws = virgl_vtest_winsys(vws);624625virgl_resource_cache_flush(&vtws->cache);626627mtx_destroy(&vtws->mutex);628FREE(vtws);629}630631static bool632virgl_vtest_resource_cache_entry_is_busy(struct virgl_resource_cache_entry *entry,633void *user_data)634{635struct virgl_vtest_winsys *vtws = user_data;636struct virgl_hw_res *res = cache_entry_container_res(entry);637638return virgl_vtest_resource_is_busy(&vtws->base, res);639}640641static void642virgl_vtest_resource_cache_entry_release(struct virgl_resource_cache_entry *entry,643void *user_data)644{645struct virgl_vtest_winsys *vtws = user_data;646struct virgl_hw_res *res = cache_entry_container_res(entry);647648virgl_hw_res_destroy(vtws, res);649}650651struct virgl_winsys *652virgl_vtest_winsys_wrap(struct sw_winsys *sws)653{654static const unsigned CACHE_TIMEOUT_USEC = 1000000;655struct virgl_vtest_winsys *vtws;656657vtws = CALLOC_STRUCT(virgl_vtest_winsys);658if (!vtws)659return NULL;660661virgl_vtest_connect(vtws);662vtws->sws = sws;663664virgl_resource_cache_init(&vtws->cache, CACHE_TIMEOUT_USEC,665virgl_vtest_resource_cache_entry_is_busy,666virgl_vtest_resource_cache_entry_release,667vtws);668(void) mtx_init(&vtws->mutex, mtx_plain);669670vtws->base.destroy = virgl_vtest_winsys_destroy;671672vtws->base.transfer_put = virgl_vtest_transfer_put;673vtws->base.transfer_get = virgl_vtest_transfer_get;674675vtws->base.resource_create = virgl_vtest_winsys_resource_cache_create;676vtws->base.resource_reference = virgl_vtest_resource_reference;677vtws->base.resource_map = virgl_vtest_resource_map;678vtws->base.resource_wait = virgl_vtest_resource_wait;679vtws->base.resource_is_busy = virgl_vtest_resource_is_busy;680vtws->base.cmd_buf_create = virgl_vtest_cmd_buf_create;681vtws->base.cmd_buf_destroy = virgl_vtest_cmd_buf_destroy;682vtws->base.submit_cmd = virgl_vtest_winsys_submit_cmd;683684vtws->base.emit_res = virgl_vtest_emit_res;685vtws->base.res_is_referenced = virgl_vtest_res_is_ref;686vtws->base.get_caps = virgl_vtest_get_caps;687688vtws->base.cs_create_fence = virgl_cs_create_fence;689vtws->base.fence_wait = virgl_fence_wait;690vtws->base.fence_reference = virgl_fence_reference;691vtws->base.supports_fences = 0;692vtws->base.supports_encoded_transfers = (vtws->protocol_version >= 2);693694vtws->base.flush_frontbuffer = virgl_vtest_flush_frontbuffer;695696return &vtws->base;697}698699700