omap: clarify dmabuf file descriptor ownership
[platform/upstream/libdrm.git] / omap / omap_drm.c
index 96e1871..464dea9 100644 (file)
@@ -34,6 +34,8 @@
 #include <linux/stddef.h>
 #include <errno.h>
 #include <sys/mman.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include <xf86drm.h>
 
@@ -56,6 +58,7 @@ struct omap_bo {
        uint32_t        handle;
        uint32_t        name;           /* flink global handle (DRI2 name) */
        uint64_t        offset;         /* offset to mmap() */
+       int             fd;             /* dmabuf handle */
 };
 
 struct omap_device * omap_device_new(int fd)
@@ -226,6 +229,10 @@ void omap_bo_del(struct omap_bo *bo)
                munmap(bo->map, bo->size);
        }
 
+       if (bo->fd) {
+               close(bo->fd);
+       }
+
        if (bo->handle) {
                struct drm_gem_close req = {
                                .handle = bo->handle,
@@ -264,6 +271,28 @@ uint32_t omap_bo_handle(struct omap_bo *bo)
        return bo->handle;
 }
 
+/* caller owns the dmabuf fd that is returned and is responsible
+ * to close() it when done
+ */
+int omap_bo_dmabuf(struct omap_bo *bo)
+{
+       if (!bo->fd) {
+               struct drm_prime_handle req = {
+                               .handle = bo->handle,
+                               .flags = DRM_CLOEXEC,
+               };
+               int ret;
+
+               ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
+               if (ret) {
+                       return ret;
+               }
+
+               bo->fd = req.fd;
+       }
+       return dup(bo->fd);
+}
+
 uint32_t omap_bo_size(struct omap_bo *bo)
 {
        if (!bo->size) {