#include #include "util/u_pointer.h" #include "util/u_memory.h" #include "util/u_hash_table.h" #include "vrend_object.h" struct vrend_object_types { void (*unref)(void *); } obj_types[VIRGL_MAX_OBJECTS]; void vrend_object_set_destroy_callback(int type, void (*cb)(void *)) { obj_types[type].unref = cb; } static unsigned hash_func(void *key) { intptr_t ip = pointer_to_intptr(key); return (unsigned)(ip & 0xffffffff); } static int compare(void *key1, void *key2) { if (key1 < key2) return -1; if (key1 > key2) return 1; else return 0; } static struct util_hash_table *res_hash; struct vrend_object { enum virgl_object_type type; uint32_t handle; void *data; }; struct util_hash_table *vrend_object_init_ctx_table(void) { struct util_hash_table *ctx_hash; ctx_hash = util_hash_table_create(hash_func, compare); return ctx_hash; } static void vrend_object_free(struct vrend_object *obj) { if (obj_types[obj->type].unref) obj_types[obj->type].unref(obj->data); else { /* for objects with no callback just free them */ free(obj->data); } free(obj); } static enum pipe_error free_cb(void *key, void *value, void *data) { struct vrend_object *obj = value; vrend_object_free(obj); return PIPE_OK; } void vrend_object_fini_ctx_table(struct util_hash_table *ctx_hash) { if (!ctx_hash) return; util_hash_table_foreach(ctx_hash, free_cb, NULL); util_hash_table_destroy(ctx_hash); } void vrend_object_init_resource_table(void) { if (!res_hash) res_hash = util_hash_table_create(hash_func, compare); } void vrend_object_fini_resource_table(void) { if (res_hash) util_hash_table_destroy(res_hash); res_hash = NULL; } uint32_t vrend_object_insert(struct util_hash_table *handle_hash, void *data, uint32_t length, uint32_t handle, enum virgl_object_type type) { struct vrend_object *obj = CALLOC_STRUCT(vrend_object); if (!obj) return 0; obj->handle = handle; obj->data = data; obj->type = type; util_hash_table_set(handle_hash, intptr_to_pointer(obj->handle), obj); return obj->handle; } void vrend_object_remove(struct util_hash_table *handle_hash, uint32_t handle, enum virgl_object_type type) { struct vrend_object *obj; obj = util_hash_table_get(handle_hash, intptr_to_pointer(handle)); if (!obj) return; util_hash_table_remove(handle_hash, intptr_to_pointer(handle)); vrend_object_free(obj); } void *vrend_object_lookup(struct util_hash_table *handle_hash, uint32_t handle, enum virgl_object_type type) { struct vrend_object *obj; obj = util_hash_table_get(handle_hash, intptr_to_pointer(handle)); if (!obj) { return NULL; } if (obj->type != type) return NULL; return obj->data; } void *vrend_resource_insert(void *data, uint32_t length, uint32_t handle) { struct vrend_object *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) { struct vrend_object *obj; obj = util_hash_table_get(res_hash, intptr_to_pointer(handle)); if (!obj) return; util_hash_table_remove(res_hash, intptr_to_pointer(handle)); free(obj); } void *vrend_resource_lookup(uint32_t handle, uint32_t ctx_id) { struct vrend_object *obj; obj = util_hash_table_get(res_hash, intptr_to_pointer(handle)); if (!obj) return NULL; return obj->data; } static enum pipe_error dump_cb(void *key, void *value, void *data) { struct vrend_object *obj = value; fprintf(stderr, "%p: %d %d\n", key, obj->type, obj->handle); graw_renderer_dump_resource(obj->data); return PIPE_OK; } void vrend_object_dumb_ctx_table(struct util_hash_table *ctx_hash) { if (!ctx_hash) return; util_hash_table_foreach(ctx_hash, dump_cb, NULL); }