We can remove some duplicated code.
Reviewed-by: Elie Tournier <elie.tournier@collabora.com>
#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,
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;
}
*/
#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"
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,
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)
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);
+}
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)
{
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)
{
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
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;
}