From 55a58dfcfbf3bf39c61ffd31401e513648262275 Mon Sep 17 00:00:00 2001 From: Alexandros Frantzis Date: Mon, 24 Jun 2019 16:30:07 +0300 Subject: [PATCH] virgl: Introduce virgl_staging_mgr Add a manager for the staging buffer used in virgl. The staging manager is heavily inspired by u_upload_mgr, but is simpler and is a better fit for virgl's purposes. In particular, the staging manager: * Allows concurrent staging allocations. * Calls the virgl winsys directly to create and map resources, avoiding unnecessarily going through gallium resources and transfers. olv: make virgl_staging_alloc_buffer return a bool Signed-off-by: Alexandros Frantzis Reviewed-by: Chia-I Wu --- src/gallium/drivers/virgl/Makefile.sources | 2 + src/gallium/drivers/virgl/meson.build | 1 + src/gallium/drivers/virgl/virgl_staging_mgr.c | 135 ++++++++++++++++++++++++++ src/gallium/drivers/virgl/virgl_staging_mgr.h | 92 ++++++++++++++++++ 4 files changed, 230 insertions(+) create mode 100644 src/gallium/drivers/virgl/virgl_staging_mgr.c create mode 100644 src/gallium/drivers/virgl/virgl_staging_mgr.h diff --git a/src/gallium/drivers/virgl/Makefile.sources b/src/gallium/drivers/virgl/Makefile.sources index a5d9765..6730568 100644 --- a/src/gallium/drivers/virgl/Makefile.sources +++ b/src/gallium/drivers/virgl/Makefile.sources @@ -12,6 +12,8 @@ C_SOURCES := \ virgl_resource.h \ virgl_screen.c \ virgl_screen.h \ + virgl_staging_mgr.c \ + virgl_staging_mgr.h \ virgl_streamout.c \ virgl_texture.c \ virgl_tgsi.c \ diff --git a/src/gallium/drivers/virgl/meson.build b/src/gallium/drivers/virgl/meson.build index 62e8155..e3c67a2 100644 --- a/src/gallium/drivers/virgl/meson.build +++ b/src/gallium/drivers/virgl/meson.build @@ -25,6 +25,7 @@ files_libvirgl = files( 'virgl_query.c', 'virgl_resource.c', 'virgl_screen.c', + 'virgl_staging_mgr.c', 'virgl_streamout.c', 'virgl_transfer_queue.c', 'virgl_texture.c', diff --git a/src/gallium/drivers/virgl/virgl_staging_mgr.c b/src/gallium/drivers/virgl/virgl_staging_mgr.c new file mode 100644 index 0000000..a5e9d77 --- /dev/null +++ b/src/gallium/drivers/virgl/virgl_staging_mgr.c @@ -0,0 +1,135 @@ +/* + * Copyright 2009 VMware, Inc. + * Copyright 2019 Collabora Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "pipe/p_defines.h" +#include "util/u_inlines.h" +#include "pipe/p_context.h" +#include "util/u_memory.h" +#include "util/u_math.h" + +#include "virgl_staging_mgr.h" +#include "virgl_resource.h" + +static bool +virgl_staging_alloc_buffer(struct virgl_staging_mgr *staging, unsigned min_size) +{ + struct virgl_winsys *vws = staging->vws; + unsigned size; + + /* Release the old buffer, if present: + */ + vws->resource_reference(vws, &staging->hw_res, NULL); + + /* Allocate a new one: + */ + size = align(MAX2(staging->default_size, min_size), 4096); + + staging->hw_res = vws->resource_create(vws, + PIPE_BUFFER, + PIPE_FORMAT_R8_UNORM, + VIRGL_BIND_STAGING, + size, /* width */ + 1, /* height */ + 1, /* depth */ + 1, /* array_size */ + 0, /* last_level */ + 0, /* nr_samples */ + size); /* size */ + if (staging->hw_res == NULL) + return false; + + staging->map = vws->resource_map(vws, staging->hw_res); + if (staging->map == NULL) { + vws->resource_reference(vws, &staging->hw_res, NULL); + return false; + } + + staging->offset = 0; + staging->size = size; + + return true; +} + +void +virgl_staging_init(struct virgl_staging_mgr *staging, struct pipe_context *pipe, + unsigned default_size) +{ + memset(staging, 0, sizeof(*staging)); + + staging->vws = virgl_screen(pipe->screen)->vws; + staging->default_size = default_size; +} + +void +virgl_staging_destroy(struct virgl_staging_mgr *staging) +{ + struct virgl_winsys *vws = staging->vws; + vws->resource_reference(vws, &staging->hw_res, NULL); +} + +bool +virgl_staging_alloc(struct virgl_staging_mgr *staging, + unsigned size, + unsigned alignment, + unsigned *out_offset, + struct virgl_hw_res **outbuf, + void **ptr) +{ + struct virgl_winsys *vws = staging->vws; + unsigned offset = align(staging->offset, alignment); + + assert(out_offset); + assert(outbuf); + assert(ptr); + assert(size); + + /* Make sure we have enough space in the staging buffer + * for the sub-allocation. + */ + if (offset + size > staging->size) { + if (unlikely(!virgl_staging_alloc_buffer(staging, size))) { + *out_offset = ~0; + vws->resource_reference(vws, outbuf, NULL); + *ptr = NULL; + return false; + } + + offset = 0; + } + + assert(staging->size); + assert(staging->hw_res); + assert(staging->map); + assert(offset < staging->size); + assert(offset + size <= staging->size); + + /* Emit the return values: */ + *ptr = staging->map + offset; + vws->resource_reference(vws, outbuf, staging->hw_res); + *out_offset = offset; + + staging->offset = offset + size; + + return true; +} diff --git a/src/gallium/drivers/virgl/virgl_staging_mgr.h b/src/gallium/drivers/virgl/virgl_staging_mgr.h new file mode 100644 index 0000000..7abf187 --- /dev/null +++ b/src/gallium/drivers/virgl/virgl_staging_mgr.h @@ -0,0 +1,92 @@ +/* + * Copyright 2009 VMware, Inc. + * Copyright 2019 Collabora Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* virgl staging buffer manager, heavily inspired by u_upload_mgr. */ + +#ifndef VIRGL_STAGING_MGR_H +#define VIRGL_STAGING_MGR_H + +struct pipe_context; +struct virgl_hw_res; +struct virgl_winsys; + +#ifdef __cplusplus +extern "C" { +#endif + +struct virgl_staging_mgr { + struct virgl_winsys *vws; + unsigned default_size; /* Minimum size of the staging buffer, in bytes. */ + struct virgl_hw_res *hw_res; /* Staging buffer hw_res. */ + unsigned size; /* Current staging buffer size. */ + uint8_t *map; /* Pointer to the mapped staging buffer. */ + unsigned offset; /* Offset pointing at the first unused buffer byte. */ +}; + +/** + * Init the staging manager. + * + * \param staging Pointer to the staging manager to initialize. + * \param pipe Pipe driver. + * \param default_size Minimum size of the staging buffer, in bytes. + */ +void +virgl_staging_init(struct virgl_staging_mgr *staging, struct pipe_context *pipe, + unsigned default_size); + +/** + * Destroy the staging manager. + */ +void +virgl_staging_destroy(struct virgl_staging_mgr *staging); + +/** + * Sub-allocate new memory from the staging buffer. + * + * The returned pointer to the buffer references the internal staging buffer. + * + * The returned pointer to staging buffer memory is guaranteed to be valid + * for as long as the returned buffer is still referenced. + * + * \param staging Staging manager + * \param size Size of the allocation. + * \param alignment Alignment of the suballocation within the buffer. + * \param out_offset Pointer to where the new buffer offset will be returned. + * \param outbuf Pointer to where the staging buffer will be returned. + * \param ptr Pointer to the allocated memory that is returned. + * \return Whether the allocation is successful. + */ +bool +virgl_staging_alloc(struct virgl_staging_mgr *staging, + unsigned size, + unsigned alignment, + unsigned *out_offset, + struct virgl_hw_res **outbuf, + void **ptr); + +#ifdef __cplusplus +} // extern "C" { +#endif + +#endif -- 2.7.4