From b45aa6290bfcecf6a33dd5eb20f333907fb09251 Mon Sep 17 00:00:00 2001 From: Gurchetan Singh Date: Mon, 3 Dec 2018 15:16:43 -0800 Subject: [PATCH] virgl: move resource creation / import / destruction to common code We can remove some duplicated code. Reviewed-by: Elie Tournier --- src/gallium/drivers/virgl/virgl_buffer.c | 33 +-------- src/gallium/drivers/virgl/virgl_resource.c | 84 +++++++++++++++++++--- src/gallium/drivers/virgl/virgl_resource.h | 16 ++--- src/gallium/drivers/virgl/virgl_texture.c | 70 ++---------------- 4 files changed, 89 insertions(+), 114 deletions(-) diff --git a/src/gallium/drivers/virgl/virgl_buffer.c b/src/gallium/drivers/virgl/virgl_buffer.c index c0f2afe3fb0..cee26da41b7 100644 --- a/src/gallium/drivers/virgl/virgl_buffer.c +++ b/src/gallium/drivers/virgl/virgl_buffer.c @@ -27,16 +27,6 @@ #include "virgl_resource.h" #include "virgl_screen.h" -static void virgl_buffer_destroy(struct pipe_screen *screen, - struct pipe_resource *buf) -{ - struct virgl_screen *vs = virgl_screen(screen); - struct virgl_resource *vbuf = virgl_resource(buf); - - vs->vws->resource_unref(vs->vws, vbuf->hw_res); - FREE(vbuf); -} - static void *virgl_buffer_transfer_map(struct pipe_context *ctx, struct pipe_resource *resource, unsigned level, @@ -132,30 +122,13 @@ static void virgl_buffer_transfer_flush_region(struct pipe_context *ctx, static const struct u_resource_vtbl virgl_buffer_vtbl = { u_default_resource_get_handle, /* get_handle */ - virgl_buffer_destroy, /* resource_destroy */ + virgl_resource_destroy, /* resource_destroy */ virgl_buffer_transfer_map, /* transfer_map */ virgl_buffer_transfer_flush_region, /* transfer_flush_region */ virgl_buffer_transfer_unmap, /* transfer_unmap */ }; -struct pipe_resource *virgl_buffer_create(struct virgl_screen *vs, - const struct pipe_resource *template) +void virgl_buffer_init(struct virgl_resource *res) { - struct virgl_resource *buf; - uint32_t vbind; - buf = CALLOC_STRUCT(virgl_resource); - buf->clean = TRUE; - buf->u.b = *template; - buf->u.b.screen = &vs->base; - buf->u.vtbl = &virgl_buffer_vtbl; - pipe_reference_init(&buf->u.b.reference, 1); - virgl_resource_layout(&buf->u.b, &buf->metadata); - - vbind = pipe_to_virgl_bind(template->bind); - - buf->hw_res = vs->vws->resource_create(vs->vws, template->target, - template->format, vbind, - template->width0, 1, 1, 1, 0, 0, - buf->metadata.total_size); - return &buf->u.b; + res->u.vtbl = &virgl_buffer_vtbl; } diff --git a/src/gallium/drivers/virgl/virgl_resource.c b/src/gallium/drivers/virgl/virgl_resource.c index 6d930dc1a5a..26d14d6bd76 100644 --- a/src/gallium/drivers/virgl/virgl_resource.c +++ b/src/gallium/drivers/virgl/virgl_resource.c @@ -22,6 +22,7 @@ */ #include "util/u_format.h" #include "util/u_inlines.h" +#include "util/u_memory.h" #include "virgl_context.h" #include "virgl_resource.h" #include "virgl_screen.h" @@ -56,11 +57,37 @@ bool virgl_res_needs_readback(struct virgl_context *vctx, static struct pipe_resource *virgl_resource_create(struct pipe_screen *screen, const struct pipe_resource *templ) { - struct virgl_screen *vs = virgl_screen(screen); - if (templ->target == PIPE_BUFFER) - return virgl_buffer_create(vs, templ); - else - return virgl_texture_create(vs, templ); + unsigned vbind; + struct virgl_screen *vs = virgl_screen(screen); + struct virgl_resource *res = CALLOC_STRUCT(virgl_resource); + + res->clean = TRUE; + res->u.b = *templ; + res->u.b.screen = &vs->base; + pipe_reference_init(&res->u.b.reference, 1); + vbind = pipe_to_virgl_bind(templ->bind); + virgl_resource_layout(&res->u.b, &res->metadata); + res->hw_res = vs->vws->resource_create(vs->vws, templ->target, + templ->format, vbind, + templ->width0, + templ->height0, + templ->depth0, + templ->array_size, + templ->last_level, + templ->nr_samples, + res->metadata.total_size); + if (!res->hw_res) { + FREE(res); + return NULL; + } + + if (templ->target == PIPE_BUFFER) + virgl_buffer_init(res); + else + virgl_texture_init(res); + + return &res->u.b; + } static struct pipe_resource *virgl_resource_from_handle(struct pipe_screen *screen, @@ -68,11 +95,24 @@ static struct pipe_resource *virgl_resource_from_handle(struct pipe_screen *scre struct winsys_handle *whandle, unsigned usage) { - struct virgl_screen *vs = virgl_screen(screen); - if (templ->target == PIPE_BUFFER) - return NULL; - else - return virgl_texture_from_handle(vs, templ, whandle); + struct virgl_screen *vs = virgl_screen(screen); + if (templ->target == PIPE_BUFFER) + return NULL; + + struct virgl_resource *res = CALLOC_STRUCT(virgl_resource); + res->u.b = *templ; + res->u.b.screen = &vs->base; + pipe_reference_init(&res->u.b.reference, 1); + + res->hw_res = vs->vws->resource_create_from_handle(vs->vws, whandle); + if (!res->hw_res) { + FREE(res); + return NULL; + } + + virgl_texture_init(res); + + return &res->u.b; } void virgl_init_screen_resource_functions(struct pipe_screen *screen) @@ -212,3 +252,27 @@ void virgl_resource_destroy_transfer(struct virgl_context *vctx, util_range_destroy(&trans->range); slab_free(&vctx->transfer_pool, trans); } + +void virgl_resource_destroy(struct pipe_screen *screen, + struct pipe_resource *resource) +{ + struct virgl_screen *vs = virgl_screen(screen); + struct virgl_resource *res = virgl_resource(resource); + vs->vws->resource_unref(vs->vws, res->hw_res); + FREE(res); +} + +boolean virgl_resource_get_handle(struct pipe_screen *screen, + struct pipe_resource *resource, + struct winsys_handle *whandle) +{ + struct virgl_screen *vs = virgl_screen(screen); + struct virgl_resource *res = virgl_resource(resource); + + if (res->u.b.target == PIPE_BUFFER) + return FALSE; + + return vs->vws->resource_get_handle(vs->vws, res->hw_res, + res->metadata.stride[0], + whandle); +} diff --git a/src/gallium/drivers/virgl/virgl_resource.h b/src/gallium/drivers/virgl/virgl_resource.h index 28b12c317f6..e211fb881b1 100644 --- a/src/gallium/drivers/virgl/virgl_resource.h +++ b/src/gallium/drivers/virgl/virgl_resource.h @@ -65,12 +65,7 @@ void virgl_init_screen_resource_functions(struct pipe_screen *screen); void virgl_init_context_resource_functions(struct pipe_context *ctx); -struct pipe_resource *virgl_texture_create(struct virgl_screen *vs, - const struct pipe_resource *templ); - -struct pipe_resource *virgl_texture_from_handle(struct virgl_screen *vs, - const struct pipe_resource *templ, - struct winsys_handle *whandle); +void virgl_texture_init(struct virgl_resource *res); static inline struct virgl_resource *virgl_resource(struct pipe_resource *r) { @@ -82,8 +77,7 @@ static inline struct virgl_transfer *virgl_transfer(struct pipe_transfer *trans) return (struct virgl_transfer *)trans; } -struct pipe_resource *virgl_buffer_create(struct virgl_screen *vs, - const struct pipe_resource *templ); +void virgl_buffer_init(struct virgl_resource *res); static inline unsigned pipe_to_virgl_bind(unsigned pbind) { @@ -135,4 +129,10 @@ virgl_resource_create_transfer(struct pipe_context *ctx, void virgl_resource_destroy_transfer(struct virgl_context *vctx, struct virgl_transfer *trans); +void virgl_resource_destroy(struct pipe_screen *screen, + struct pipe_resource *resource); + +boolean virgl_resource_get_handle(struct pipe_screen *screen, + struct pipe_resource *resource, + struct winsys_handle *whandle); #endif diff --git a/src/gallium/drivers/virgl/virgl_texture.c b/src/gallium/drivers/virgl/virgl_texture.c index d79ae4d3d09..df419db111a 100644 --- a/src/gallium/drivers/virgl/virgl_texture.c +++ b/src/gallium/drivers/virgl/virgl_texture.c @@ -181,78 +181,16 @@ static void virgl_texture_transfer_unmap(struct pipe_context *ctx, virgl_resource_destroy_transfer(vctx, trans); } -static boolean virgl_texture_get_handle(struct pipe_screen *screen, - struct pipe_resource *ptex, - struct winsys_handle *whandle) -{ - struct virgl_screen *vs = virgl_screen(screen); - struct virgl_resource *vtex = virgl_resource(ptex); - - return vs->vws->resource_get_handle(vs->vws, vtex->hw_res, - vtex->metadata.stride[0], - whandle); -} - -static void virgl_texture_destroy(struct pipe_screen *screen, - struct pipe_resource *res) -{ - struct virgl_screen *vs = virgl_screen(screen); - struct virgl_resource *vtex = virgl_resource(res); - vs->vws->resource_unref(vs->vws, vtex->hw_res); - FREE(vtex); -} - static const struct u_resource_vtbl virgl_texture_vtbl = { - virgl_texture_get_handle, /* get_handle */ - virgl_texture_destroy, /* resource_destroy */ + virgl_resource_get_handle, /* get_handle */ + virgl_resource_destroy, /* resource_destroy */ virgl_texture_transfer_map, /* transfer_map */ NULL, /* transfer_flush_region */ virgl_texture_transfer_unmap, /* transfer_unmap */ }; -struct pipe_resource * -virgl_texture_from_handle(struct virgl_screen *vs, - const struct pipe_resource *template, - struct winsys_handle *whandle) +void virgl_texture_init(struct virgl_resource *res) { - struct virgl_resource *tex = CALLOC_STRUCT(virgl_resource); - tex->u.b = *template; - tex->u.b.screen = &vs->base; - pipe_reference_init(&tex->u.b.reference, 1); - tex->u.vtbl = &virgl_texture_vtbl; - - tex->hw_res = vs->vws->resource_create_from_handle(vs->vws, whandle); - return &tex->u.b; -} - -struct pipe_resource *virgl_texture_create(struct virgl_screen *vs, - const struct pipe_resource *template) -{ - struct virgl_resource *tex; - unsigned vbind; - - tex = CALLOC_STRUCT(virgl_resource); - tex->clean = TRUE; - tex->u.b = *template; - tex->u.b.screen = &vs->base; - pipe_reference_init(&tex->u.b.reference, 1); - tex->u.vtbl = &virgl_texture_vtbl; - virgl_resource_layout(&tex->u.b, &tex->metadata); - - vbind = pipe_to_virgl_bind(template->bind); - tex->hw_res = vs->vws->resource_create(vs->vws, template->target, - template->format, vbind, - template->width0, - template->height0, - template->depth0, - template->array_size, - template->last_level, - template->nr_samples, - tex->metadata.total_size); - if (!tex->hw_res) { - FREE(tex); - return NULL; - } - return &tex->u.b; + res->u.vtbl = &virgl_texture_vtbl; } -- 2.30.2