*/
#include <assert.h>
#include <fcntl.h>
+#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
return NULL;
}
+ if (pthread_mutex_init(&drv->table_lock, NULL)) {
+ free(drv);
+ return NULL;
+ }
+
+ drv->buffer_table = drmHashCreate();
+ if (!drv->buffer_table) {
+ free(drv);
+ return NULL;
+ }
+
if (drv->backend->init) {
ret = drv->backend->init(drv);
if (ret) {
if (drv->backend->close)
drv->backend->close(drv);
+ pthread_mutex_destroy(&drv->table_lock);
+ drmHashDestroy(drv->buffer_table);
+
free(drv);
}
drv_format_t format, uint64_t flags)
{
int ret;
+ size_t plane;
struct bo *bo;
bo = drv_bo_new(drv, width, height, format);
return NULL;
}
+ pthread_mutex_lock(&drv->table_lock);
+
+ for (plane = 0; plane < bo->num_planes; plane++)
+ drv_increment_reference_count(drv, bo, plane);
+
+ pthread_mutex_unlock(&drv->table_lock);
+
return bo;
}
void drv_bo_destroy(struct bo *bo)
{
- bo->drv->backend->bo_destroy(bo);
+ size_t plane;
+ uintptr_t total = 0;
+ struct driver *drv = bo->drv;
+
+ pthread_mutex_lock(&drv->table_lock);
+
+ for (plane = 0; plane < bo->num_planes; plane++)
+ drv_decrement_reference_count(drv, bo, plane);
+
+ for (plane = 0; plane < bo->num_planes; plane++)
+ total += drv_get_reference_count(drv, bo, plane);
+
+ pthread_mutex_unlock(&drv->table_lock);
+
+ if (total == 0)
+ bo->drv->backend->bo_destroy(bo);
+
free(bo);
}
bo->sizes[0] = data->height * data->stride;
bo->handles[0].u32 = prime_handle.handle;
+ pthread_mutex_lock(&drv->table_lock);
+ drv_increment_reference_count(drv, bo, 0);
+ pthread_mutex_unlock(&drv->table_lock);
+
return bo;
}
return error;
}
+
+uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
+ size_t plane)
+{
+ void *count;
+ uintptr_t num = 0;
+
+ if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
+ num = (uintptr_t) (count);
+
+ return num;
+}
+
+void drv_increment_reference_count(struct driver *drv, struct bo *bo,
+ size_t plane)
+{
+ uintptr_t num = drv_get_reference_count(drv, bo, plane);
+
+ /* If a value isn't in the table, drmHashDelete is a no-op */
+ drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
+ drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
+ (void *) (num + 1));
+}
+
+void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
+ size_t plane)
+{
+ uintptr_t num = drv_get_reference_count(drv, bo, plane);
+
+ drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
+
+ if (num > 0)
+ drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
+ (void *) (num - 1));
+}
uint32_t format, uint32_t flags);
int drv_dumb_bo_destroy(struct bo *bo);
int drv_gem_bo_destroy(struct bo *bo);
-
+uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
+ size_t plane);
+void drv_increment_reference_count(struct driver *drv, struct bo *bo,
+ size_t plane);
+void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
+ size_t plane);
#endif