From 00e9d6123d6fc48c7a5b510e95ade215c5b9bd72 Mon Sep 17 00:00:00 2001 From: Russell King Date: Wed, 10 Jun 2015 20:43:53 +0100 Subject: Add prime import Add DRM prime import support to the armada-drm buffer manager. This allows dma_bufs to be imported into armada-drm for display. Signed-off-by: Russell King --- armada_bufmgr.c | 77 +++++++++++++++++++++++++++++++++++++++++--------------- armada_bufmgr.h | 2 ++ debian/changelog | 3 ++- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/armada_bufmgr.c b/armada_bufmgr.c index 0803309..f552a31 100644 --- a/armada_bufmgr.c +++ b/armada_bufmgr.c @@ -393,6 +393,38 @@ struct drm_armada_bo *drm_armada_bo_create_size(struct drm_armada_bufmgr *mgr, return &bo->bo; } +static struct armada_bo *drm_armada_bo_lookup_or_create( + struct drm_armada_bufmgr *mgr, uint32_t handle, size_t size) +{ + struct armada_bo *bo; + + /* + * Lookup this handle in our hash of handles. If it + * already exists, increment the refcount and return it. + */ + if (drmHashLookup(mgr->handle_hash, handle, (void **)&bo) == 0) { + drm_armada_bo_get(&bo->bo); + return bo; + } + + bo = calloc(1, sizeof *bo); + if (!bo) + return NULL; + + bo->bo.ref = 1; + bo->bo.handle = handle; + bo->bo.size = size; + bo->bo.type = DRM_ARMADA_BO_LINEAR; /* assumed */ + bo->alloc_size = size; + bo->ref = 1; + bo->mgr = mgr; + + /* Add it to the handle hash table */ + assert(drmHashInsert(mgr->handle_hash, bo->bo.handle, bo) == 0); + + return bo; +} + struct drm_armada_bo *drm_armada_bo_create_from_name(struct drm_armada_bufmgr *mgr, uint32_t name) { @@ -415,37 +447,42 @@ struct drm_armada_bo *drm_armada_bo_create_from_name(struct drm_armada_bufmgr *m if (ret == -1) return NULL; - /* - * Lookup this handle in our hash of handles. If it - * already exists, increment the refcount and return it. - */ - if (drmHashLookup(mgr->handle_hash, arg.handle, (void **)&bo) == 0) { - drm_armada_bo_get(&bo->bo); - return &bo->bo; - } - - bo = calloc(1, sizeof *bo); + bo = drm_armada_bo_lookup_or_create(mgr, arg.handle, arg.size); if (!bo) { armada_gem_handle_close(fd, arg.handle); return NULL; } - bo->bo.ref = 1; - bo->bo.handle = arg.handle; - bo->bo.size = arg.size; - bo->bo.type = DRM_ARMADA_BO_LINEAR; /* assumed */ - bo->alloc_size = arg.size; - bo->ref = 1; bo->name = name; - bo->mgr = mgr; - - /* Add it to the handle hash table */ - assert(drmHashInsert(mgr->handle_hash, bo->bo.handle, bo) == 0); assert(drmHashInsert(mgr->name_hash, bo->name, bo) == 0); return &bo->bo; } +struct drm_armada_bo *drm_armada_bo_from_fd(struct drm_armada_bufmgr *mgr, + int prime_fd) +{ + int fd = mgr->fd; + struct armada_bo *bo; + uint32_t handle; + off_t size; + + size = lseek(prime_fd, 0, SEEK_END); + if (size == (off_t)-1) + return NULL; + + if (drmPrimeFDToHandle(fd, prime_fd, &handle)) + return NULL; + + bo = drm_armada_bo_lookup_or_create(mgr, handle, size); + if (!bo) { + armada_gem_handle_close(fd, handle); + return NULL; + } + + return &bo->bo; +} + struct drm_armada_bo *drm_armada_bo_dumb_create(struct drm_armada_bufmgr *mgr, unsigned w, unsigned h, unsigned bpp) { diff --git a/armada_bufmgr.h b/armada_bufmgr.h index f4772fd..fb1dcca 100644 --- a/armada_bufmgr.h +++ b/armada_bufmgr.h @@ -40,6 +40,8 @@ int drm_armada_bo_flink(struct drm_armada_bo *bo, uint32_t *name); /* Create a dmabuf fd for the BO */ int drm_armada_bo_to_fd(struct drm_armada_bo *bo, int *fd); +struct drm_armada_bo *drm_armada_bo_from_fd(struct drm_armada_bufmgr *mgr, + int prime_fd); int drm_armada_bo_map(struct drm_armada_bo *bo); void drm_armada_bo_get(struct drm_armada_bo *bo); diff --git a/debian/changelog b/debian/changelog index 3c0c83a..617c069 100644 --- a/debian/changelog +++ b/debian/changelog @@ -3,8 +3,9 @@ libdrm-armada (2.0.3-1) unstable; urgency=low * Add drm_armada_bo_create_size() * Add drm_armada_cache_reap() * Better tracking of re-usable BOs + * Add drm_armada_bo_from_fd() - -- Russell King Sat, 12 Oct 2014 19:50:35 +0100 + -- Russell King Wed, 10 Jun 2015 20:46:00 +0100 libdrm-armada (2.0.2-1) unstable; urgency=low -- cgit