diff options
author | Changyeon Lee <cyeon.lee@samsung.com> | 2021-05-11 18:33:47 +0900 |
---|---|---|
committer | Changyeon Lee <cyeon.lee@samsung.com> | 2021-05-13 15:05:01 +0900 |
commit | d24ae03969798a3080a03bd9e30ab45267edf613 (patch) | |
tree | 16826f2ab6e638125f3dd841b34c0978d6cbcc2d | |
parent | 0dcf7c4ac7ac5d3999bb0cb6a7b9e38694370c53 (diff) | |
download | libtbm-msm-d24ae03969798a3080a03bd9e30ab45267edf613.tar.gz libtbm-msm-d24ae03969798a3080a03bd9e30ab45267edf613.tar.bz2 libtbm-msm-d24ae03969798a3080a03bd9e30ab45267edf613.zip |
Use gbm_bo library for allocating buffer
Change-Id: I2cbf8474c7d9715274c2f7c80690ddb30102dfed
-rw-r--r-- | configure.ac | 5 | ||||
-rw-r--r-- | packaging/libtbm-msm.spec | 1 | ||||
-rw-r--r-- | src/tbm_bufmgr_msm.c | 709 |
3 files changed, 367 insertions, 348 deletions
diff --git a/configure.ac b/configure.ac index 169c05c..f535ec3 100644 --- a/configure.ac +++ b/configure.ac @@ -40,9 +40,10 @@ PKG_CHECK_MODULES(LIBDRM, libdrm) PKG_CHECK_MODULES(LIBTBM, libtbm) PKG_CHECK_MODULES(DLOG, dlog) PKG_CHECK_MODULES(LIBUDEV, libudev) +PKG_CHECK_MODULES(GBM, gbm) -LIBTBM_MSM_CFLAGS="$LIBDRM_CFLAGS $LIBTBM_CFLAGS $DLOG_CFLAGS $LIBUDEV_CFLAGS " -LIBTBM_MSM_LIBS="$LIBDRM_LIBS $LIBTBM_LIBS $DLOG_LIBS $LIBUDEV_LIBS " +LIBTBM_MSM_CFLAGS="$LIBDRM_CFLAGS $LIBTBM_CFLAGS $DLOG_CFLAGS $LIBUDEV_CFLAGS $GBM_CFLAGS " +LIBTBM_MSM_LIBS="$LIBDRM_LIBS $LIBTBM_LIBS $DLOG_LIBS $LIBUDEV_LIBS $GBM_LIBS " AC_SUBST(LIBTBM_MSM_CFLAGS) AC_SUBST(LIBTBM_MSM_LIBS) diff --git a/packaging/libtbm-msm.spec b/packaging/libtbm-msm.spec index 29bf156..6fe6515 100644 --- a/packaging/libtbm-msm.spec +++ b/packaging/libtbm-msm.spec @@ -12,6 +12,7 @@ BuildRequires: pkgconfig(libdrm) BuildRequires: pkgconfig(libtbm) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(libudev) +BuildRequires: pkgconfig(gbm) %description descriptionion: Tizen Buffer manager backend module uses drm msm diff --git a/src/tbm_bufmgr_msm.c b/src/tbm_bufmgr_msm.c index 506673b..79f3b38 100644 --- a/src/tbm_bufmgr_msm.c +++ b/src/tbm_bufmgr_msm.c @@ -52,7 +52,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include <tbm_backend.h> #include <tbm_drm_helper.h> #include <tbm_log.h> -#include <msm_drm.h> +#include <gbm.h> #define TBM_COLOR_FORMAT_COUNT 4 @@ -67,15 +67,14 @@ typedef struct _tbm_bo_msm *tbm_bo_msm; /* tbm buffor object for msm */ struct _tbm_bo_msm { - int fd; - - unsigned int name; /* FLINK ID */ - - unsigned int gem; /* GEM Handle */ + int device_fd; - int dmabuf; /* fd for dmabuf */ + struct gbm_bo *gbo; - void *pBase; /* virtual address */ + unsigned int handle; /* GEM Handle */ + unsigned int name; /* FLINK ID */ + int buf_fd; /* fd for dmabuf */ + void *pBase; /* virtual address */ unsigned int size; @@ -92,7 +91,9 @@ struct _tbm_bo_msm { /* tbm bufmgr private for msm */ struct _tbm_bufmgr_msm { int fd; - void* hashBos; + struct gbm_device *gdevice; + + void *hash_bos; char *device_name; void *bind_display; @@ -125,7 +126,6 @@ uint32_t tbm_msm_color_format_list[TBM_COLOR_FORMAT_COUNT] = { TBM_FORMAT_YUV420 }; - static int _tbm_msm_open_drm() { @@ -213,75 +213,41 @@ _get_msm_flag_from_tbm(unsigned int ftbm) { unsigned int flags = 0; - if (ftbm & TBM_BO_SCANOUT) - flags |= MSM_BO_SCANOUT; + flags |= GBM_BO_USE_RENDERING; - if (ftbm & TBM_BO_NONCACHABLE) - flags |= MSM_BO_UNCACHED; - else - flags |= MSM_BO_WC; // freedreno use WC by default + if (ftbm & TBM_BO_SCANOUT) + flags |= GBM_BO_USE_SCANOUT; return flags; } +#if 0 static unsigned int _get_tbm_flag_from_msm(unsigned int fmsm) { unsigned int flags = 0; - if (fmsm & MSM_BO_SCANOUT) + if (fmsm & GBM_BO_USE_SCANOUT) flags |= TBM_BO_SCANOUT; - if (fmsm & MSM_BO_UNCACHED) - flags |= TBM_BO_NONCACHABLE; - return flags; } +#endif static unsigned int -_get_msm_access_op_from_tbm(unsigned int tbm_op) -{ - unsigned int op = 0; - - if (tbm_op & TBM_OPTION_READ) - op |= MSM_PREP_READ; - - if (tbm_op & TBM_OPTION_WRITE) - op |= MSM_PREP_WRITE; - - return op; -} - -static unsigned int -_get_name(int fd, unsigned int gem) +_get_name(int fd, unsigned int handle) { - struct drm_gem_flink arg = {0, }; - - arg.handle = gem; + struct drm_gem_flink arg = {0,}; + arg.handle = handle; if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &arg)) { - TBM_ERR("fail to DRM_IOCTL_GEM_FLINK gem:%d (%m)", gem); + TBM_ERR("fail to DRM_IOCTL_GEM_FLINK handle:%d", handle); return 0; } return (unsigned int)arg.name; } -static int -_get_dmabuf_fd(int fd, unsigned int gem) -{ - struct drm_prime_handle arg = {0, }; - - arg.handle = gem; - - if (drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) { - TBM_ERR("fail to DRM_IOCTL_PRIME_HANDLE_TO_FD gem:%d (%m)", gem); - return -1; - } - - return arg.fd; -} - static tbm_bo_handle _msm_bo_handle(tbm_bo_msm bo_msm, int device) { @@ -292,36 +258,27 @@ _msm_bo_handle(tbm_bo_msm bo_msm, int device) switch (device) { case TBM_DEVICE_DEFAULT: case TBM_DEVICE_2D: - bo_handle.u32 = (uint32_t)bo_msm->gem; + bo_handle.u32 = (uint32_t)bo_msm->handle; break; case TBM_DEVICE_CPU: if (!bo_msm->pBase) { void *map = NULL; - struct drm_msm_gem_info arg = {0, }; - - arg.handle = bo_msm->gem; - - if (drmCommandWriteRead(bo_msm->fd, DRM_MSM_GEM_INFO, &arg, sizeof(arg))) { - TBM_ERR("fail to DRM_MSM_GEM_INFO gem:%d (%m)", bo_msm->gem); + map = mmap(0, bo_msm->size, PROT_READ | PROT_WRITE, MAP_SHARED, bo_msm->buf_fd, 0); + if(map == MAP_FAILED) { + TBM_ERR("Cannot usrptr fd=%d(%m)", bo_msm->buf_fd); return (tbm_bo_handle) NULL; } - map = mmap(NULL, bo_msm->size, PROT_READ|PROT_WRITE, MAP_SHARED, - bo_msm->fd, arg.offset); - if (map == MAP_FAILED) { - TBM_ERR("Cannot usrptr gem:%d", bo_msm->gem); - return (tbm_bo_handle) NULL; - } bo_msm->pBase = map; } bo_handle.ptr = (void *)bo_msm->pBase; break; case TBM_DEVICE_3D: - bo_handle.u32 = (uint32_t)bo_msm->dmabuf; + bo_handle.u32 = (uint32_t)bo_msm->buf_fd; break; case TBM_DEVICE_MM: - bo_handle.u32 = (uint32_t)bo_msm->dmabuf; + bo_handle.u32 = (uint32_t)bo_msm->buf_fd; break; default: TBM_ERR("Not supported device:%d", device); @@ -337,7 +294,7 @@ tbm_msm_bufmgr_get_capabilities(tbm_backend_bufmgr_data *bufmgr_data, tbm_error_ { tbm_bufmgr_capability capabilities = TBM_BUFMGR_CAPABILITY_NONE; - capabilities = TBM_BUFMGR_CAPABILITY_SHARE_KEY|TBM_BUFMGR_CAPABILITY_SHARE_FD; + capabilities = TBM_BUFMGR_CAPABILITY_SHARE_FD | TBM_BUFMGR_CAPABILITY_SHARE_KEY; if (error) *error = TBM_ERROR_NONE; @@ -636,7 +593,9 @@ tbm_msm_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, unsigned int size, { tbm_bufmgr_msm bufmgr_msm = (tbm_bufmgr_msm)bufmgr_data; tbm_bo_msm bo_msm; + struct gbm_bo *gbo; unsigned int msm_flags; + uint32_t width = 2048, height; if (bufmgr_msm == NULL) { TBM_ERR("bufmgr_data is null\n"); @@ -657,49 +616,65 @@ tbm_msm_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, unsigned int size, msm_flags = _get_msm_flag_from_tbm(flags); - struct drm_msm_gem_new arg = {0 ,}; - - arg.size = size; - arg.flags = msm_flags; + height = ((uint32_t) size + (width * 4) - 1) / (width * 4); - if (drmCommandWriteRead(bufmgr_msm->fd, DRM_MSM_GEM_NEW, &arg, sizeof(arg))) { - TBM_ERR("Cannot create bo_msm(flag:%x, size:%d) (%m)", arg.flags, (unsigned int)size); + gbo = gbm_bo_create(bufmgr_msm->gdevice, width, height, GBM_FORMAT_ARGB8888, msm_flags); + if (!gbo) { + TBM_ERR("Cannot create gbm bo(size:%d mem_types:%d) (%m)", size, flags); if (error) *error = TBM_ERROR_INVALID_OPERATION; goto fail; } - bo_msm->fd = bufmgr_msm->fd; - bo_msm->gem = arg.handle; - bo_msm->size = arg.size; + bo_msm->gbo = gbo; + bo_msm->device_fd = bufmgr_msm->fd; bo_msm->flags_tbm = flags; bo_msm->flags_msm = msm_flags; - bo_msm->name = _get_name(bo_msm->fd, bo_msm->gem); + bo_msm->handle = gbm_bo_get_handle(gbo).u32; + if (!bo_msm->handle) { + TBM_ERR("Cannot get handle:(%m)"); + if (error) + *error = TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + bo_msm->name = _get_name(bo_msm->device_fd, bo_msm->handle); if (!bo_msm->name) { - TBM_ERR("Cannot get name bo_msm:%p", bo_msm); + TBM_ERR("Cannot get name from handle:%d(%m)", bo_msm->handle); if (error) *error = TBM_ERROR_INVALID_OPERATION; - goto fail; + return NULL; } - bo_msm->dmabuf = _get_dmabuf_fd(bo_msm->fd, bo_msm->gem); - if (bo_msm->dmabuf < 0) { + bo_msm->buf_fd = gbm_bo_get_fd(gbo); + if (bo_msm->buf_fd < 0) { TBM_ERR("Cannot get fd bo_msm:%p", bo_msm); if (error) *error = TBM_ERROR_INVALID_OPERATION; goto fail; } - pthread_mutex_init(&bo_msm->mutex, NULL); + bo_msm->size = lseek(bo_msm->buf_fd, 0, SEEK_END); + if (!bo_msm->size) { + TBM_ERR("Cannot get size bo_msm:%p (%m)", bo_msm); + if (error) + *error = TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + if (drmHashInsert(bufmgr_msm->hash_bos, bo_msm->name, (void *)bo_msm) < 0) { + TBM_ERR("Cannot insert bo_msm to Hash(%d)", bo_msm->name); + if (error) + *error = TBM_ERROR_OUT_OF_MEMORY; + goto fail; + } - /* add bo to hash */ - if (drmHashInsert(bufmgr_msm->hashBos, bo_msm->name, (void *)bo_msm) < 0) - TBM_ERR("error Cannot insert bo to Hash(%d)", bo_msm->name); + pthread_mutex_init(&bo_msm->mutex, NULL); - TBM_DBG(" bo_msm:%p, gem:%d(%d), flags:%d(%d), size:%d", + TBM_DBG(" bo_msm:%p, handle:%d, flags:%d(%d), size:%d", bo_msm, - bo_msm->gem, bo_msm->name, + bo_msm->handle, bo_msm->flags_tbm, bo_msm->size); @@ -709,14 +684,110 @@ tbm_msm_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, unsigned int size, return (tbm_backend_bo_data *)bo_msm; fail: - if (bo_msm && bo_msm->gem) { - struct drm_gem_close close_arg = {0 ,}; + if (gbo) gbm_bo_destroy(gbo); + if (bo_msm) free(bo_msm); - close_arg.handle = bo_msm->gem; + return NULL; +} - drmIoctl(bufmgr_msm->fd, DRM_IOCTL_GEM_CLOSE, &close_arg); +static tbm_backend_bo_data * +tbm_msm_bufmgr_alloc_bo_with_format(tbm_backend_bufmgr_data *bufmgr_data, + int format, int bo_idx, int width, + int height, tbm_bo_memory_type mem_types, + tbm_error_e *error) +{ + tbm_bufmgr_msm bufmgr_msm = (tbm_bufmgr_msm)bufmgr_data; + tbm_bo_msm bo_msm; + struct gbm_bo *gbo; + unsigned int msm_flags; + + if (bufmgr_msm == NULL) { + TBM_ERR("bufmgr_data is null\n"); + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return NULL; + } + + bo_msm = calloc(1, sizeof(struct _tbm_bo_msm)); + if (!bo_msm) { + TBM_ERR("fail to allocate the bo_msm private\n"); + if (error) + *error = TBM_ERROR_OUT_OF_MEMORY; + return NULL; + } + + bo_msm->bufmgr_msm = bufmgr_msm; + + msm_flags = _get_msm_flag_from_tbm(mem_types); + + gbo = gbm_bo_create(bufmgr_msm->gdevice, width, height, format, msm_flags); + if (!gbo) { + TBM_ERR("Cannot create gbm bo(format:%d bo_idx:%d width:%d height:%d mem_types:%d) (%m)", + format, bo_idx, width, height, mem_types); + if (error) + *error = TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + bo_msm->gbo = gbo; + bo_msm->device_fd = bufmgr_msm->fd; + bo_msm->flags_tbm = mem_types; + bo_msm->flags_msm = msm_flags; + + bo_msm->handle = gbm_bo_get_handle(gbo).u32; + if (!bo_msm->handle) { + TBM_ERR("Cannot get handle:(%m)"); + if (error) + *error = TBM_ERROR_INVALID_OPERATION; + return NULL; } + bo_msm->name = _get_name(bo_msm->device_fd, bo_msm->handle); + if (!bo_msm->name) { + TBM_ERR("Cannot get name from handle:%d(%m)", bo_msm->handle); + if (error) + *error = TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + bo_msm->buf_fd = gbm_bo_get_fd(gbo); + if (bo_msm->buf_fd < 0) { + TBM_ERR("Cannot get fd bo_msm:%p", bo_msm); + if (error) + *error = TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + bo_msm->size = lseek(bo_msm->buf_fd, 0, SEEK_END); + if (!bo_msm->size) { + TBM_ERR("Cannot get size bo_msm:%p (%m)", bo_msm); + if (error) + *error = TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + if (drmHashInsert(bufmgr_msm->hash_bos, bo_msm->name, (void *)bo_msm) < 0) { + TBM_ERR("Cannot insert bo_msm to Hash(%d)", bo_msm->name); + if (error) + *error = TBM_ERROR_OUT_OF_MEMORY; + goto fail; + } + + pthread_mutex_init(&bo_msm->mutex, NULL); + + TBM_DBG(" bo_msm:%p, handle:%d, flags:%d(%d), size:%d", + bo_msm, + bo_msm->handle, + bo_msm->flags_tbm, + bo_msm->size); + + if (error) + *error = TBM_ERROR_NONE; + + return (tbm_backend_bo_data *)bo_msm; + +fail: + if (gbo) gbm_bo_destroy(gbo); if (bo_msm) free(bo_msm); return NULL; @@ -726,11 +797,13 @@ static tbm_backend_bo_data * tbm_msm_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_error_e *error) { tbm_bufmgr_msm bufmgr_msm = (tbm_bufmgr_msm)bufmgr_data; - tbm_bo_msm bo_msm; - unsigned int gem = 0; + tbm_bo_msm bo_msm = NULL; + struct gbm_bo *gbo = NULL; + struct gbm_import_fd_data fd_data; + union gbm_bo_handle handle; unsigned int name; + struct drm_prime_handle prime_arg = {0, }; int ret; - unsigned int real_size = -1; if (bufmgr_msm == NULL) { TBM_ERR("bufmgr_data is null\n"); @@ -739,70 +812,52 @@ tbm_msm_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_e return NULL; } - //getting handle from fd - struct drm_prime_handle arg = {0 ,}; - - arg.fd = key; - arg.flags = 0; - - if (drmIoctl(bufmgr_msm->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &arg)) { - TBM_ERR("Cannot get gem handle from fd:%d (%m)", arg.fd); + prime_arg.fd = key; + prime_arg.flags = 0; + if (drmIoctl(bufmgr_msm->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &prime_arg)) { + TBM_ERR("Cannot get gem handle from fd:%d (%m)", prime_arg.fd); if (error) *error = TBM_ERROR_INVALID_OPERATION; return NULL; } - gem = arg.handle; - name = _get_name(bufmgr_msm->fd, gem); - if (name == 0) { - TBM_ERR("Cannot get name from gem:%d, fd:%d (%m)", gem, key); + name = _get_name(bufmgr_msm->fd, prime_arg.handle); + if (!name) { + TBM_ERR("Cannot get name from handle:%d, fd:%d (%m)", prime_arg.handle, key); if (error) *error = TBM_ERROR_INVALID_OPERATION; return NULL; } - ret = drmHashLookup(bufmgr_msm->hashBos, name, (void **)&bo_msm); + ret = drmHashLookup(bufmgr_msm->hash_bos, name, (void **)&bo_msm); if (ret == 0) { - if (gem == bo_msm->gem) { + if (prime_arg.handle == bo_msm->handle) { if (error) *error = TBM_ERROR_NONE; return bo_msm; } } - /* Determine size of bo. The fd-to-handle ioctl really should - * return the size, but it doesn't. If we have kernel 3.12 or - * later, we can lseek on the prime fd to get the size. Older - * kernels will just fail, in which case we fall back to the - * provided (estimated or guess size). */ - real_size = lseek(key, 0, SEEK_END); - - struct drm_gem_open open_arg = {0, }; - - open_arg.name = name; + /* we don't know format in backend */ + fd_data.format = GBM_FORMAT_ABGR8888; + fd_data.fd = dup(key); - /* Open the same GEM object only for finding out its size */ - if (drmIoctl(bufmgr_msm->fd, DRM_IOCTL_GEM_OPEN, &open_arg)) { - TBM_ERR("Cannot get gem info from gem:%d, fd:%d (%m)", gem, key); + gbo = gbm_bo_import(bufmgr_msm->gdevice, GBM_BO_IMPORT_FD, &fd_data, GBM_BO_USE_RENDERING); + if (!gbo) { + TBM_ERR("Cannot import gbm bo(fd:%d) (%m)", key); if (error) *error = TBM_ERROR_INVALID_OPERATION; - return NULL; + goto fail; } - /* Free gem handle to avoid a memory leak*/ - struct drm_gem_close gem_close = {0, }; - - gem_close.handle = open_arg.handle; - if (drmIoctl(bufmgr_msm->fd, DRM_IOCTL_GEM_CLOSE, &gem_close)) { - TBM_ERR("Cannot close gem_handle. gem:%d (%m)", open_arg.handle); + handle = gbm_bo_get_handle(gbo); + if (!handle.ptr) { + TBM_ERR("Cannot get handle gbm bo(fd:%d) (%m)", key); if (error) *error = TBM_ERROR_INVALID_OPERATION; - return NULL; + goto fail; } - if (real_size == -1) - real_size = open_arg.size; - bo_msm = calloc(1, sizeof(struct _tbm_bo_msm)); if (!bo_msm) { TBM_ERR("bo_msm:%p fail to allocate the bo_msm", bo_msm); @@ -812,29 +867,54 @@ tbm_msm_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_e } bo_msm->bufmgr_msm = bufmgr_msm; - bo_msm->fd = bufmgr_msm->fd; - bo_msm->gem = gem; - bo_msm->size = real_size; - bo_msm->flags_msm = 0; - bo_msm->flags_tbm = _get_tbm_flag_from_msm(bo_msm->flags_msm); - bo_msm->name = name; - bo_msm->dmabuf = _get_dmabuf_fd(bo_msm->fd, bo_msm->gem); - if (bo_msm->dmabuf < 0) { - TBM_ERR("Cannot get fd bo_msm:%p", bo_msm); + bo_msm->gbo = gbo; + bo_msm->device_fd = bufmgr_msm->fd; + + bo_msm->handle = gbm_bo_get_handle(gbo).u32; + if (!bo_msm->handle) { + TBM_ERR("Cannot get handle (%m)"); if (error) *error = TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + bo_msm->name = _get_name(bo_msm->device_fd, bo_msm->handle); + if (!bo_msm->name) { + TBM_ERR("Cannot get name from handle:%d (%m)", bo_msm->handle); + if (error) + *error = TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + bo_msm->buf_fd = gbm_bo_get_fd(gbo); + if (bo_msm->buf_fd < 0) { + TBM_ERR("Cannot get fd bo_msm:%p (%m)", bo_msm); + if (error) + *error = TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + bo_msm->size = lseek(bo_msm->buf_fd, 0, SEEK_END); + if (!bo_msm->size) { + TBM_ERR("Cannot get size bo_msm:%p (%m)", bo_msm); + if (error) + *error = TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + if (drmHashInsert(bufmgr_msm->hash_bos, bo_msm->name, (void *)bo_msm) < 0) { + TBM_ERR("Cannot insert bo_msm to Hash(%d)", bo_msm->name); + if (error) + *error = TBM_ERROR_OUT_OF_MEMORY; goto fail; } - /* add bo_msm to hash */ - if (drmHashInsert(bufmgr_msm->hashBos, bo_msm->name, (void *)bo_msm) < 0) - TBM_ERR("bo_msm:%p Cannot insert bo_msm to Hash(%d) from gem:%d, fd:%d", - bo_msm, bo_msm->name, gem, key); + pthread_mutex_init(&bo_msm->mutex, NULL); - TBM_DBG(" bo_msm:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d", + TBM_DBG(" bo_msm:%p, handle:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d", bo_msm, - bo_msm->gem, bo_msm->name, - bo_msm->dmabuf, + bo_msm->handle, + bo_msm->buf_fd, key, bo_msm->flags_tbm, bo_msm->size); @@ -845,14 +925,9 @@ tbm_msm_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_e return (tbm_backend_bo_data *)bo_msm; fail: - if (bo_msm && bo_msm->gem) { - struct drm_gem_close close_arg = {0, }; - - close_arg.handle = bo_msm->gem; - - drmIoctl(bufmgr_msm->fd, DRM_IOCTL_GEM_CLOSE, &close_arg); - } - + if (fd_data.fd >= 0) + close(fd_data.fd); + if (gbo) gbm_bo_destroy(gbo); if (bo_msm) free(bo_msm); return NULL; @@ -862,7 +937,10 @@ static tbm_backend_bo_data * tbm_msm_bufmgr_import_key(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tbm_error_e *error) { tbm_bufmgr_msm bufmgr_msm = (tbm_bufmgr_msm)bufmgr_data; - tbm_bo_msm bo_msm; + tbm_bo_msm bo_msm = NULL; + struct drm_gem_open open_arg = {0, }; + struct drm_gem_close close_arg = {0, }; + struct drm_prime_handle prime_arg = {0, }; int ret; if (bufmgr_msm == NULL) { @@ -872,73 +950,67 @@ tbm_msm_bufmgr_import_key(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tbm return NULL; } - ret = drmHashLookup(bufmgr_msm->hashBos, key, (void **)&bo_msm); + ret = drmHashLookup(bufmgr_msm->hash_bos, key, (void **)&bo_msm); if (ret == 0) { if (error) *error = TBM_ERROR_NONE; return (tbm_backend_bo_data *)bo_msm; } - struct drm_gem_open arg = {0 ,}; - - arg.name = key; - - if (drmIoctl(bufmgr_msm->fd, DRM_IOCTL_GEM_OPEN, &arg)) { + open_arg.name = key; + if (drmIoctl(bufmgr_msm->fd, DRM_IOCTL_GEM_OPEN, &open_arg)) { TBM_ERR("Cannot open gem name:%d (%m)", key); if (error) *error = TBM_ERROR_INVALID_OPERATION; return NULL; } - bo_msm = calloc(1, sizeof(struct _tbm_bo_msm)); - if (!bo_msm) { - TBM_ERR("fail to allocate the bo_msm private"); + prime_arg.handle = open_arg.handle; + if (drmIoctl(bufmgr_msm->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &prime_arg)) { + TBM_ERR("fail to DRM_IOCTL_PRIME_HANDLE_TO_FD handle:%d (%m)", prime_arg.handle); if (error) - *error = TBM_ERROR_OUT_OF_MEMORY; - return NULL; + *error = TBM_ERROR_INVALID_OPERATION; + + goto fail; } - bo_msm->bufmgr_msm = bufmgr_msm; - bo_msm->fd = bufmgr_msm->fd; - bo_msm->gem = arg.handle; - bo_msm->size = arg.size; - bo_msm->flags_msm = 0; - bo_msm->flags_tbm = _get_tbm_flag_from_msm(bo_msm->flags_msm); - bo_msm->name = key; - bo_msm->dmabuf = _get_dmabuf_fd(bo_msm->fd, bo_msm->gem); - if (bo_msm->dmabuf < 0) { - TBM_ERR("Cannot get fd bo_msm:%p", bo_msm); + close_arg.handle = open_arg.handle; + if (drmIoctl(bufmgr_msm->fd, DRM_IOCTL_GEM_CLOSE, &close_arg)) + TBM_ERR("Cannot close gem_handle:%d (%m)", close_arg.handle); + + open_arg.handle = 0; + + bo_msm = tbm_msm_bufmgr_import_fd(bufmgr_data, prime_arg.fd, error); + if (!bo_msm) { + TBM_ERR("fail to import fd:%d", prime_arg.fd); + if (error) *error = TBM_ERROR_INVALID_OPERATION; + goto fail; } - /* add bo to hash */ - if (drmHashInsert(bufmgr_msm->hashBos, bo_msm->name, (void *)bo_msm) < 0) - TBM_ERR("Cannot insert bo_msm to Hash(%d)", bo_msm->name); + close(prime_arg.fd); - TBM_DBG(" bo_msm:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d", + if (error) + *error = TBM_ERROR_NONE; + + TBM_DBG(" bo_msm:%p, handle:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d", bo_msm, - bo_msm->gem, bo_msm->name, - bo_msm->dmabuf, + bo_msm->handle, + bo_msm->buf_fd, + key, bo_msm->flags_tbm, bo_msm->size); - if (error) - *error = TBM_ERROR_NONE; - return (tbm_backend_bo_data *)bo_msm; fail: - if (bo_msm && bo_msm->gem) { - struct drm_gem_close close_arg = {0, }; - - close_arg.handle = bo_msm->gem; - + if (open_arg.handle) { + close_arg.handle = open_arg.handle; drmIoctl(bufmgr_msm->fd, DRM_IOCTL_GEM_CLOSE, &close_arg); } - - if (bo_msm) free(bo_msm); + if (prime_arg.fd >= 0) close(prime_arg.fd); return NULL; } @@ -947,8 +1019,8 @@ static void tbm_msm_bo_free(tbm_backend_bo_data *bo_data) { tbm_bo_msm bo_msm = (tbm_bo_msm)bo_data; - tbm_bo_msm temp; tbm_bufmgr_msm bufmgr_msm; + tbm_bo_msm temp; int ret; if (!bo_data) @@ -958,41 +1030,27 @@ tbm_msm_bo_free(tbm_backend_bo_data *bo_data) if (!bufmgr_msm) return; - TBM_DBG(" bo_msm:%p, gem:%d(%d), fd:%d, size:%d", + TBM_DBG(" bo_msm:%p, handle:%d(%d), fd:%d, size:%d", bo_msm, - bo_msm->gem, bo_msm->name, - bo_msm->dmabuf, + bo_msm->handle, + bo_msm->buf_fd, bo_msm->size); if (bo_msm->pBase) { - if (munmap(bo_msm->pBase, bo_msm->size) == -1) { - TBM_ERR("bo_msm:%p fail to munmap (%m)", bo_msm); - } + if (munmap(bo_msm->pBase, bo_msm->size) == -1) + TBM_ERR("bo_msm:%p fail to munmap(%m)"); } - /* close dmabuf */ - if (bo_msm->dmabuf) { - close(bo_msm->dmabuf); - bo_msm->dmabuf = -1; - } - - /* delete bo from hash */ - ret = drmHashLookup(bufmgr_msm->hashBos, bo_msm->name, (void**)&temp); + ret = drmHashLookup(bufmgr_msm->hash_bos, bo_msm->name, (void **)&temp); if (ret == 0) - drmHashDelete(bufmgr_msm->hashBos, bo_msm->name); + drmHashDelete(bufmgr_msm->hash_bos, bo_msm->name); else - TBM_ERR("Cannot find bo_msm to Hash(%d), ret=%d", bo_msm->name, ret); + TBM_ERR("Cannot find bo_msm to Hash(%d), ret:%d", bo_msm->name, ret); if (temp != bo_msm) - TBM_ERR("hashBos probably has several BOs with same name!!!"); - - /* Free gem handle */ - struct drm_gem_close arg = {0, }; - - arg.handle = bo_msm->gem; + TBM_ERR("hash_bos probably has several BOs with same name!!!\n"); - if (drmIoctl(bo_msm->fd, DRM_IOCTL_GEM_CLOSE, &arg)) - TBM_ERR("bo_msm:%p fail to gem close (%m)\n", bo_msm); + gbm_bo_destroy(bo_msm->gbo); free(bo_msm); } @@ -1043,31 +1101,31 @@ tbm_msm_bo_get_handle(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, t return (tbm_bo_handle) NULL; } - if (!bo_msm->gem) { - TBM_ERR("Cannot map gem:%d", bo_msm->gem); + if (!bo_msm->handle) { + TBM_ERR("Cannot map handle:%d", bo_msm->handle); if (error) *error = TBM_ERROR_INVALID_PARAMETER; return (tbm_bo_handle) NULL; } - TBM_DBG("bo_msm:%p, gem:%d(%d), fd:%d, flags:%d, size:%d, %s", - bo_msm, - bo_msm->gem, bo_msm->name, - bo_msm->dmabuf, - bo_msm->flags_tbm, - bo_msm->size, - STR_DEVICE[device]); - /*Get mapped bo_handle*/ bo_handle = _msm_bo_handle(bo_msm, device); if (bo_handle.ptr == NULL) { - TBM_ERR("Cannot get handle: gem:%d, device:%d\n", - bo_msm->gem, device); + TBM_ERR("Cannot get handle: handle:%d, device:%d\n", + bo_msm->handle, device); if (error) *error = TBM_ERROR_INVALID_OPERATION; return (tbm_bo_handle) NULL; } + TBM_DBG("bo_msm:%p, handle:%d(%d), fd:%d, flags:%d, size:%d, %s", + bo_msm, + bo_msm->handle, + bo_msm->buf_fd, + bo_msm->flags_tbm, + bo_msm->size, + STR_DEVICE[device]); + if (error) *error = TBM_ERROR_NONE; @@ -1095,30 +1153,30 @@ tbm_msm_bo_map(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, return (tbm_bo_handle) NULL; } - if (!bo_msm->gem) { - TBM_ERR("Cannot map gem:%d", bo_msm->gem); + if (!bo_msm->handle) { + TBM_ERR("Cannot map handle:%d", bo_msm->handle); if (error) *error = TBM_ERROR_INVALID_PARAMETER; return (tbm_bo_handle) NULL; } - TBM_DBG(" bo_msm:%p, gem:%d(%d), fd:%d, %s, %s", - bo_msm, - bo_msm->gem, bo_msm->name, - bo_msm->dmabuf, - STR_DEVICE[device], - STR_OPT[opt]); - /*Get mapped bo_handle*/ bo_handle = _msm_bo_handle(bo_msm, device); if (bo_handle.ptr == NULL) { - TBM_ERR("Cannot get handle: gem:%d, device:%d, opt:%d", - bo_msm->gem, device, opt); + TBM_ERR("Cannot get handle: handle:%d, device:%d, opt:%d", + bo_msm->handle, device, opt); if (error) *error = TBM_ERROR_INVALID_PARAMETER; return (tbm_bo_handle) NULL; } + TBM_DBG(" bo_msm:%p, handle:%d(%d), fd:%d, %s, %s", + bo_msm, + bo_msm->handle, + bo_msm->buf_fd, + STR_DEVICE[device], + STR_OPT[opt]); + if (error) *error = TBM_ERROR_NONE; @@ -1138,81 +1196,13 @@ tbm_msm_bo_unmap(tbm_backend_bo_data *bo_data) if (!bufmgr_msm) return TBM_ERROR_INVALID_PARAMETER; - if (!bo_msm->gem) - return TBM_ERROR_INVALID_PARAMETER; - - TBM_DBG(" bo_msm:%p, gem:%d(%d), fd:%d", - bo_msm, - bo_msm->gem, bo_msm->name, - bo_msm->dmabuf); - - return TBM_ERROR_NONE; -} - -static tbm_error_e -tbm_msm_bo_lock(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, - tbm_bo_access_option opt) -{ - tbm_bo_msm bo_msm = (tbm_bo_msm)bo_data; - - if (!bo_msm) - return TBM_ERROR_INVALID_PARAMETER; - - if (!bo_msm->gem) - return TBM_ERROR_INVALID_PARAMETER; - - if (device != TBM_DEVICE_CPU) - return TBM_ERROR_NONE; - - struct drm_msm_gem_cpu_prep arg = {0, }; - - arg.handle = bo_msm->gem; - arg.op = _get_msm_access_op_from_tbm(opt); - arg.timeout.tv_sec = 1; - arg.timeout.tv_nsec = 0; - - if (drmCommandWrite(bo_msm->fd, DRM_MSM_GEM_CPU_PREP, &arg, sizeof(arg))) { - TBM_ERR("fail to DRM_MSM_GEM_CPU_PREP gem:%d (%m)", bo_msm->gem); - return TBM_ERROR_INVALID_OPERATION; - } - - bo_msm->device = TBM_DEVICE_CPU; - - TBM_DBG(" bo_msm:%p, gem:%d(%d), fd:%d", - bo_msm, - bo_msm->gem, bo_msm->name, - bo_msm->dmabuf); - - return TBM_ERROR_NONE; -} - -static tbm_error_e -tbm_msm_bo_unlock(tbm_backend_bo_data *bo_data) -{ - tbm_bo_msm bo_msm = (tbm_bo_msm)bo_data; - - if (!bo_msm) - return TBM_ERROR_INVALID_PARAMETER; - - if (!bo_msm->gem) + if (!bo_msm->handle) return TBM_ERROR_INVALID_PARAMETER; - if (bo_msm->device != TBM_DEVICE_CPU) - return TBM_ERROR_NONE; - - struct drm_msm_gem_cpu_fini arg = {0, }; - - arg.handle = bo_msm->gem; - - if (drmCommandWrite(bo_msm->fd, DRM_MSM_GEM_CPU_FINI, &arg, sizeof(arg))) { - TBM_ERR("fail to DRM_MSM_GEM_CPU_FINI gem:%d (%m)", bo_msm->gem); - return TBM_ERROR_INVALID_OPERATION; - } - - TBM_DBG(" bo_msm:%p, gem:%d(%d), fd:%d", + TBM_DBG(" bo_msm:%p, handle:%d(%d), fd:%d", bo_msm, - bo_msm->gem, bo_msm->name, - bo_msm->dmabuf); + bo_msm->handle, + bo_msm->buf_fd); return TBM_ERROR_NONE; } @@ -1229,19 +1219,19 @@ tbm_msm_bo_export_fd(tbm_backend_bo_data *bo_data, tbm_error_e *error) return -1; } - fd = _get_dmabuf_fd(bo_msm->fd, bo_msm->gem); + fd = gbm_bo_get_fd(bo_msm->gbo); if (fd < 0) { - TBM_ERR("bo_msm:%p Cannot dmabuf=%d", - bo_msm, bo_msm->gem); + TBM_ERR("bo_msm:%p Cannot handle=%d", + bo_msm, bo_msm->handle); if (error) *error = TBM_ERROR_INVALID_OPERATION; return (tbm_fd)-1; } - TBM_DBG(" bo_msm:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d", + TBM_DBG(" bo_msm:%p, handle:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d", bo_msm, - bo_msm->gem, bo_msm->name, - bo_msm->dmabuf, + bo_msm->handle, + bo_msm->buf_fd, fd, bo_msm->flags_tbm, bo_msm->size); @@ -1249,10 +1239,10 @@ tbm_msm_bo_export_fd(tbm_backend_bo_data *bo_data, tbm_error_e *error) if (error) *error = TBM_ERROR_NONE; - return (tbm_fd)fd; + return (tbm_fd)dup(fd); } -static tbm_key +tbm_key tbm_msm_bo_export_key(tbm_backend_bo_data *bo_data, tbm_error_e *error) { tbm_bo_msm bo_msm = (tbm_bo_msm)bo_data; @@ -1263,12 +1253,22 @@ tbm_msm_bo_export_key(tbm_backend_bo_data *bo_data, tbm_error_e *error) return 0; } - TBM_DBG(" bo_msm:%p, gem:%d(%d), fd:%d, flags:%d, size:%d", - bo_msm, - bo_msm->gem, bo_msm->name, - bo_msm->dmabuf, - bo_msm->flags_tbm, - bo_msm->size); + if (!bo_msm->name) { + bo_msm->name = _get_name(bo_msm->device_fd, bo_msm->handle); + if (!bo_msm->name) { + TBM_ERR("error Cannot get name"); + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return 0; + } + } + + TBM_DBG(" bo_msm:%p, handle:%d(%d), fd:%d, flags:%d, size:%d\n", + bo_msm, + bo_msm->handle, bo_msm->name, + bo_msm->buf_fd, + bo_msm->flags_tbm, + bo_msm->size); if (error) *error = TBM_ERROR_NONE; @@ -1282,8 +1282,8 @@ tbm_msm_deinit(tbm_backend_bufmgr_data *bufmgr_data) tbm_bufmgr_msm bufmgr_msm = (tbm_bufmgr_msm)bufmgr_data; tbm_bufmgr bufmgr; tbm_error_e error; - unsigned long key; void *value; + unsigned long key; TBM_RETURN_IF_FAIL(bufmgr_msm != NULL); @@ -1292,14 +1292,14 @@ tbm_msm_deinit(tbm_backend_bufmgr_data *bufmgr_data) tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_msm->bufmgr_func); tbm_backend_bufmgr_free_bo_func(bufmgr, bufmgr_msm->bo_func); - if (bufmgr_msm->hashBos) { - while (drmHashFirst(bufmgr_msm->hashBos, &key, &value) > 0) { + if (bufmgr_msm->hash_bos) { + while (drmHashFirst(bufmgr_msm->hash_bos, &key, &value) > 0) { free(value); - drmHashDelete(bufmgr_msm->hashBos, key); + drmHashDelete(bufmgr_msm->hash_bos, key); } - drmHashDestroy(bufmgr_msm->hashBos); - bufmgr_msm->hashBos = NULL; + drmHashDestroy(bufmgr_msm->hash_bos); + bufmgr_msm->hash_bos = NULL; } if (bufmgr_msm->bind_display) @@ -1313,6 +1313,9 @@ tbm_msm_deinit(tbm_backend_bufmgr_data *bufmgr_data) else tbm_drm_helper_unset_fd(); + if (bufmgr_msm->gdevice) + gbm_device_destroy(bufmgr_msm->gdevice); + close(bufmgr_msm->fd); free(bufmgr_msm); @@ -1342,6 +1345,14 @@ tbm_msm_init(tbm_bufmgr bufmgr, tbm_error_e *error) return NULL; } + bufmgr_msm->hash_bos = drmHashCreate(); + if (!bufmgr_msm->hash_bos) { + TBM_ERR("fail to create hash bos"); + if (error) + *error = TBM_ERROR_OUT_OF_MEMORY; + goto fail_hash_bos; + } + /* check the master_fd which already had opened */ bufmgr_msm->fd = tbm_drm_helper_get_master_fd(); if (bufmgr_msm->fd < 0) { @@ -1393,8 +1404,13 @@ tbm_msm_init(tbm_bufmgr bufmgr, tbm_error_e *error) } tbm_drm_helper_set_fd(bufmgr_msm->fd); - /*Create Hash Table*/ - bufmgr_msm->hashBos = drmHashCreate(); + bufmgr_msm->gdevice = gbm_create_device(bufmgr_msm->fd); + if (!bufmgr_msm->gdevice) { + TBM_ERR("fail to create gbm_device:%m"); + if (error) + *error = TBM_ERROR_INVALID_OPERATION; + goto fail_create_gbm_device; + } /* alloc and register bufmgr_funcs */ bufmgr_func = tbm_backend_bufmgr_alloc_bufmgr_func(bufmgr, &err); @@ -1410,7 +1426,7 @@ tbm_msm_init(tbm_bufmgr bufmgr, tbm_error_e *error) bufmgr_func->bufmgr_get_supported_formats = tbm_msm_bufmgr_get_supported_formats; bufmgr_func->bufmgr_get_plane_data = tbm_msm_bufmgr_get_plane_data; bufmgr_func->bufmgr_alloc_bo = tbm_msm_bufmgr_alloc_bo; - bufmgr_func->bufmgr_alloc_bo_with_format = NULL; + bufmgr_func->bufmgr_alloc_bo_with_format = tbm_msm_bufmgr_alloc_bo_with_format; bufmgr_func->bufmgr_import_fd = tbm_msm_bufmgr_import_fd; bufmgr_func->bufmgr_import_key = tbm_msm_bufmgr_import_key; @@ -1438,8 +1454,6 @@ tbm_msm_init(tbm_bufmgr bufmgr, tbm_error_e *error) bo_func->bo_get_handle = tbm_msm_bo_get_handle; bo_func->bo_map = tbm_msm_bo_map; bo_func->bo_unmap = tbm_msm_bo_unmap; - bo_func->bo_lock = tbm_msm_bo_lock; - bo_func->bo_unlock = tbm_msm_bo_unlock; bo_func->bo_export_fd = tbm_msm_bo_export_fd; bo_func->bo_export_key = tbm_msm_bo_export_key; @@ -1452,7 +1466,7 @@ tbm_msm_init(tbm_bufmgr bufmgr, tbm_error_e *error) } bufmgr_msm->bo_func = bo_func; - TBM_DBG("drm_fd:%d", bufmgr_msm->fd); + TBM_DBG("fd:%d", bufmgr_msm->fd); if (error) *error = TBM_ERROR_NONE; @@ -1467,8 +1481,9 @@ fail_alloc_bo_func: fail_register_bufmgr_func: tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_func); fail_alloc_bufmgr_func: - if (bufmgr_msm->hashBos) - drmHashDestroy(bufmgr_msm->hashBos); + if (bufmgr_msm->gdevice) + gbm_device_destroy(bufmgr_msm->gdevice); +fail_create_gbm_device: if (bufmgr_msm->device_name) free(bufmgr_msm->device_name); fail_get_device_name: @@ -1479,6 +1494,9 @@ fail_get_device_name: close(bufmgr_msm->fd); fail_get_auth_info: fail_open_drm: + if (bufmgr_msm->hash_bos) + drmHashDestroy(bufmgr_msm->hash_bos); +fail_hash_bos: free(bufmgr_msm); return NULL; } @@ -1490,4 +1508,3 @@ tbm_backend_module tbm_backend_module_data = { tbm_msm_init, tbm_msm_deinit }; - |