virgl: move resource creation / import / destruction to common code
authorGurchetan Singh <gurchetansingh@chromium.org>
Mon, 3 Dec 2018 23:16:43 +0000 (15:16 -0800)
committerBas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Wed, 19 Dec 2018 12:29:16 +0000 (13:29 +0100)
We can remove some duplicated code.

Reviewed-by: Elie Tournier <elie.tournier@collabora.com>
src/gallium/drivers/virgl/virgl_buffer.c
src/gallium/drivers/virgl/virgl_resource.c
src/gallium/drivers/virgl/virgl_resource.h
src/gallium/drivers/virgl/virgl_texture.c

index c0f2afe3fb0a6a304f0129595ea78e6510b171d2..cee26da41b77343e1a294d0c318e76b4b58bb19a 100644 (file)
 #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;
 }
index 6d930dc1a5af2920d4ce3b61093b7d266d108663..26d14d6bd766036eeff1896a908eda9400aea0ae 100644 (file)
@@ -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);
+}
index 28b12c317f62e605dd3f601a66c478373e850827..e211fb881b19168df209288ab01c478e421ce19c 100644 (file)
@@ -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
index d79ae4d3d098cf72af6636c39aae618101b0d6ec..df419db111a375d539481c69810e766ad5d76c17 100644 (file)
@@ -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;
 }