vrend: rework context resource management

Do not abuse context object table to manage context resources.  Add
a simpler variant, which replaces the now unused vrend_resource_*,
to manage context resources instead.

This is cleaner and more efficient because we no longer allocate a
vrend_object to manage a vrend_resource.

Signed-off-by: Chia-I Wu <olvaffe@gmail.com>
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
macos/master
Chia-I Wu 5 years ago
parent 80ad9b2067
commit 4e5e469b14
  1. 63
      src/vrend_object.c
  2. 21
      src/vrend_object.h
  3. 17
      src/vrend_renderer.c

@ -33,20 +33,11 @@ struct vrend_object_types {
void (*unref)(void *); void (*unref)(void *);
} obj_types[VIRGL_MAX_OBJECTS]; } obj_types[VIRGL_MAX_OBJECTS];
static void (*resource_unref)(void *);
void vrend_object_set_destroy_callback(int type, void (*cb)(void *)) void vrend_object_set_destroy_callback(int type, void (*cb)(void *))
{ {
obj_types[type].unref = cb; obj_types[type].unref = cb;
} }
void vrend_resource_set_destroy_callback(void (*cb)(void *))
{
resource_unref = cb;
}
static struct util_hash_table *res_hash;
struct vrend_object { struct vrend_object {
enum virgl_object_type type; enum virgl_object_type type;
uint32_t handle; uint32_t handle;
@ -84,26 +75,22 @@ void vrend_object_fini_ctx_table(struct util_hash_table *ctx_hash)
util_hash_table_destroy(ctx_hash); util_hash_table_destroy(ctx_hash);
} }
static void free_res(void *value) static void vrend_ctx_resource_destroy_func(UNUSED void *val)
{ {
struct vrend_object *obj = value; /* we don't own a reference of vrend_resource */
(*resource_unref)(obj->data);
free(obj);
} }
void struct util_hash_table *
vrend_object_init_resource_table(void) vrend_ctx_resource_init_table(void)
{ {
if (!res_hash) return util_hash_table_create(hash_func_u32,
res_hash = util_hash_table_create(hash_func_u32, compare_func, free_res); compare_func,
vrend_ctx_resource_destroy_func);
} }
void vrend_object_fini_resource_table(void) void vrend_ctx_resource_fini_table(struct util_hash_table *res_hash)
{ {
if (res_hash) { util_hash_table_destroy(res_hash);
util_hash_table_destroy(res_hash);
}
res_hash = NULL;
} }
uint32_t uint32_t
@ -153,33 +140,21 @@ void *vrend_object_lookup(struct util_hash_table *handle_hash,
return obj->data; return obj->data;
} }
int vrend_resource_insert(void *data, uint32_t handle) void vrend_ctx_resource_insert(struct util_hash_table *res_hash,
uint32_t res_id,
struct vrend_resource *res)
{ {
struct vrend_object *obj; util_hash_table_set(res_hash, uintptr_to_pointer(res_id), res);
if (!handle)
return 0;
obj = CALLOC_STRUCT(vrend_object);
if (!obj)
return 0;
obj->handle = handle;
obj->data = data;
util_hash_table_set(res_hash, intptr_to_pointer(obj->handle), obj);
return obj->handle;
} }
void vrend_resource_remove(uint32_t handle) void vrend_ctx_resource_remove(struct util_hash_table *res_hash,
uint32_t res_id)
{ {
util_hash_table_remove(res_hash, intptr_to_pointer(handle)); util_hash_table_remove(res_hash, uintptr_to_pointer(res_id));
} }
void *vrend_resource_lookup(uint32_t handle, UNUSED uint32_t ctx_id) struct vrend_resource *vrend_ctx_resource_lookup(struct util_hash_table *res_hash,
uint32_t res_id)
{ {
struct vrend_object *obj; return util_hash_table_get(res_hash, uintptr_to_pointer(res_id));
obj = util_hash_table_get(res_hash, intptr_to_pointer(handle));
if (!obj)
return NULL;
return obj->data;
} }

@ -27,8 +27,7 @@
#include "virgl_protocol.h" #include "virgl_protocol.h"
void vrend_object_init_resource_table(void); struct vrend_resource;
void vrend_object_fini_resource_table(void);
struct util_hash_table *vrend_object_init_ctx_table(void); struct util_hash_table *vrend_object_init_ctx_table(void);
void vrend_object_fini_ctx_table(struct util_hash_table *ctx_hash); void vrend_object_fini_ctx_table(struct util_hash_table *ctx_hash);
@ -41,12 +40,18 @@ uint32_t vrend_object_insert_nofree(struct util_hash_table *handle_hash,
uint32_t handle, uint32_t handle,
enum virgl_object_type type, enum virgl_object_type type,
bool free_data); bool free_data);
/* resources are global */
int vrend_resource_insert(void *data, uint32_t handle);
void vrend_resource_remove(uint32_t handle);
void *vrend_resource_lookup(uint32_t handle, uint32_t ctx_id);
void vrend_object_set_destroy_callback(int type, void (*cb)(void *)); void vrend_object_set_destroy_callback(int type, void (*cb)(void *));
void vrend_resource_set_destroy_callback(void (*cb)(void *));
struct util_hash_table *vrend_ctx_resource_init_table(void);
void vrend_ctx_resource_fini_table(struct util_hash_table *res_hash);
void vrend_ctx_resource_insert(struct util_hash_table *res_hash,
uint32_t res_id,
struct vrend_resource *res);
void vrend_ctx_resource_remove(struct util_hash_table *res_hash,
uint32_t res_id);
struct vrend_resource *vrend_ctx_resource_lookup(struct util_hash_table *res_hash,
uint32_t res_id);
#endif #endif

@ -6038,7 +6038,7 @@ void vrend_destroy_context(struct vrend_context *ctx)
if(ctx->ctx_id) if(ctx->ctx_id)
vrend_renderer_force_ctx_0(); vrend_renderer_force_ctx_0();
vrend_object_fini_ctx_table(ctx->res_hash); vrend_ctx_resource_fini_table(ctx->res_hash);
list_del(&ctx->ctx_entry); list_del(&ctx->ctx_entry);
@ -6069,7 +6069,7 @@ struct vrend_context *vrend_create_context(int id, uint32_t nlen, const char *de
list_inithead(&grctx->sub_ctxs); list_inithead(&grctx->sub_ctxs);
list_inithead(&grctx->active_nontimer_query_list); list_inithead(&grctx->active_nontimer_query_list);
grctx->res_hash = vrend_object_init_ctx_table(); grctx->res_hash = vrend_ctx_resource_init_table();
grctx->shader_cfg.use_gles = vrend_state.use_gles; grctx->shader_cfg.use_gles = vrend_state.use_gles;
grctx->shader_cfg.use_core_profile = vrend_state.use_core_profile; grctx->shader_cfg.use_core_profile = vrend_state.use_core_profile;
@ -10079,24 +10079,17 @@ void vrend_renderer_attach_res_ctx(struct vrend_context *ctx, int resource_id)
if (!res) if (!res)
return; return;
vrend_object_insert_nofree(ctx->res_hash, res, sizeof(*res), resource_id, 1, false); vrend_ctx_resource_insert(ctx->res_hash, resource_id, res);
} }
void vrend_renderer_detach_res_ctx(struct vrend_context *ctx, int res_handle) void vrend_renderer_detach_res_ctx(struct vrend_context *ctx, int res_handle)
{ {
struct vrend_resource *res; vrend_ctx_resource_remove(ctx->res_hash, res_handle);
res = vrend_object_lookup(ctx->res_hash, res_handle, 1);
if (!res)
return;
vrend_object_remove(ctx->res_hash, res_handle, 1);
} }
static struct vrend_resource *vrend_renderer_ctx_res_lookup(struct vrend_context *ctx, int res_handle) static struct vrend_resource *vrend_renderer_ctx_res_lookup(struct vrend_context *ctx, int res_handle)
{ {
struct vrend_resource *res = vrend_object_lookup(ctx->res_hash, res_handle, 1); return vrend_ctx_resource_lookup(ctx->res_hash, res_handle);
return res;
} }
void vrend_context_set_debug_flags(struct vrend_context *ctx, const char *flagstring) void vrend_context_set_debug_flags(struct vrend_context *ctx, const char *flagstring)

Loading…
Cancel
Save