diff --git a/tegra/tegra-symbols.txt b/tegra/tegra-symbols.txt index 630e075f..f8811bcd 100644 --- a/tegra/tegra-symbols.txt +++ b/tegra/tegra-symbols.txt @@ -1,5 +1,7 @@ +drm_tegra_bo_export drm_tegra_bo_get_handle drm_tegra_bo_get_name +drm_tegra_bo_import drm_tegra_bo_map drm_tegra_bo_new drm_tegra_bo_open diff --git a/tegra/tegra.c b/tegra/tegra.c index 3d645d87..cf091c1d 100644 --- a/tegra/tegra.c +++ b/tegra/tegra.c @@ -290,3 +290,64 @@ free: free(bo); return err; } + +drm_public int drm_tegra_bo_export(struct drm_tegra_bo *bo, uint32_t flags) +{ + int fd, err; + + flags |= DRM_CLOEXEC; + + err = drmPrimeHandleToFD(bo->drm->fd, bo->handle, flags, &fd); + if (err < 0) + return err; + + return fd; +} + +static ssize_t fd_get_size(int fd) +{ + ssize_t size, offset; + int err; + + offset = lseek(fd, 0, SEEK_CUR); + if (offset < 0) + return -errno; + + size = lseek(fd, 0, SEEK_END); + if (size < 0) + return -errno; + + err = lseek(fd, offset, SEEK_SET); + if (err < 0) + return -errno; + + return size; +} + +drm_public int +drm_tegra_bo_import(struct drm_tegra *drm, int fd, struct drm_tegra_bo **bop) +{ + struct drm_tegra_bo *bo; + ssize_t size; + int err; + + size = fd_get_size(fd); + if (size < 0) + return size; + + bo = drm_tegra_bo_alloc(drm, 0, 0, size); + if (!bo) + return -ENOMEM; + + err = drmPrimeFDToHandle(drm->fd, fd, &bo->handle); + if (err < 0) + goto free; + + *bop = bo; + + return 0; + +free: + free(bo); + return err; +} diff --git a/tegra/tegra.h b/tegra/tegra.h index 333690f2..aaaf455f 100644 --- a/tegra/tegra.h +++ b/tegra/tegra.h @@ -48,4 +48,8 @@ int drm_tegra_bo_get_name(struct drm_tegra_bo *bo, uint32_t *name); int drm_tegra_bo_open(struct drm_tegra *drm, uint32_t name, uint32_t flags, struct drm_tegra_bo **bop); +int drm_tegra_bo_export(struct drm_tegra_bo *bo, uint32_t flags); +int drm_tegra_bo_import(struct drm_tegra *drm, int fd, + struct drm_tegra_bo **bop); + #endif /* __DRM_TEGRA_H__ */