From 25c613f48d30e29ded3bd3c8c9d239048c8c8696 Mon Sep 17 00:00:00 2001 From: SooChan Lim Date: Wed, 14 Mar 2018 18:49:19 +0900 Subject: [PATCH] make the new backend inteface with tbm_backend.h Change-Id: Ie574c5c28eadcb742579819dc5138784be922bdf --- src/tbm_bufmgr_sprd.c | 833 +++++++++++++++++++++++++++++--------------------- 1 file changed, 489 insertions(+), 344 deletions(-) diff --git a/src/tbm_bufmgr_sprd.c b/src/tbm_bufmgr_sprd.c index 63b7844..997e4d2 100644 --- a/src/tbm_bufmgr_sprd.c +++ b/src/tbm_bufmgr_sprd.c @@ -48,11 +48,9 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include #include -#include -#include #include #include -#include +#include #include #define DEBUG @@ -89,6 +87,8 @@ _target_name(void) if (!f) return NULL; + memset(app_name, 0x00, sizeof(app_name)); + if (fgets(app_name, 100, f) == NULL) { fclose(f); return NULL; @@ -112,6 +112,8 @@ _target_name(void) #define TBM_SPRD_DEBUG(...) #endif +#define STRERR_BUFSIZE 128 + #define SIZE_ALIGN(value, base) (((value) + ((base) - 1)) & ~((base) - 1)) #define TBM_SURFACE_ALIGNMENT_PLANE (64) @@ -134,29 +136,29 @@ _target_name(void) } struct dma_buf_info { - unsigned long size; - unsigned int fence_supported; - unsigned int padding; + unsigned long size; + unsigned int fence_supported; + unsigned int padding; }; -#define DMA_BUF_ACCESS_READ 0x1 -#define DMA_BUF_ACCESS_WRITE 0x2 -#define DMA_BUF_ACCESS_DMA 0x4 -#define DMA_BUF_ACCESS_MAX 0x8 +#define DMA_BUF_ACCESS_READ 0x1 +#define DMA_BUF_ACCESS_WRITE 0x2 +#define DMA_BUF_ACCESS_DMA 0x4 +#define DMA_BUF_ACCESS_MAX 0x8 -#define DMA_FENCE_LIST_MAX 5 +#define DMA_FENCE_LIST_MAX 5 struct dma_buf_fence { - unsigned long ctx; - unsigned int type; + unsigned long ctx; + unsigned int type; }; -#define DMABUF_IOCTL_BASE 'F' -#define DMABUF_IOWR(nr, type) _IOWR(DMABUF_IOCTL_BASE, nr, type) +#define DMABUF_IOCTL_BASE 'F' +#define DMABUF_IOWR(nr, type) _IOWR(DMABUF_IOCTL_BASE, nr, type) -#define DMABUF_IOCTL_GET_INFO DMABUF_IOWR(0x00, struct dma_buf_info) -#define DMABUF_IOCTL_GET_FENCE DMABUF_IOWR(0x01, struct dma_buf_fence) -#define DMABUF_IOCTL_PUT_FENCE DMABUF_IOWR(0x02, struct dma_buf_fence) +#define DMABUF_IOCTL_GET_INFO DMABUF_IOWR(0x00, struct dma_buf_info) +#define DMABUF_IOCTL_GET_FENCE DMABUF_IOWR(0x01, struct dma_buf_fence) +#define DMABUF_IOCTL_PUT_FENCE DMABUF_IOWR(0x02, struct dma_buf_fence) /* tgl key values */ #define GLOBAL_KEY ((unsigned int)(-1)) @@ -187,11 +189,6 @@ union _tbm_bo_cache_state { typedef struct _tbm_bufmgr_sprd *tbm_bufmgr_sprd; typedef struct _tbm_bo_sprd *tbm_bo_sprd; -typedef struct _sprd_private { - int ref_count; - struct _tbm_bo_sprd *bo_priv; -} PrivGem; - /* tbm buffor object for sprd */ struct _tbm_bo_sprd { int fd; @@ -209,8 +206,6 @@ struct _tbm_bo_sprd { unsigned int flags_sprd; unsigned int flags_tbm; - PrivGem *private; - pthread_mutex_t mutex; struct dma_buf_fence dma_fence[DMA_FENCE_LIST_MAX]; int device; @@ -218,6 +213,8 @@ struct _tbm_bo_sprd { tbm_bo_cache_state cache_state; unsigned int map_cnt; + + tbm_bufmgr_sprd bufmgr_sprd; }; /* tbm bufmgr private for sprd */ @@ -229,8 +226,13 @@ struct _tbm_bufmgr_sprd { int tgl_fd; - void *bind_display; char *device_name; + void *bind_display; + + tbm_backend_bufmgr_func *bufmgr_func; + tbm_backend_bo_func *bo_func; + + tbm_bufmgr bufmgr; }; char *STR_DEVICE[] = { @@ -262,10 +264,12 @@ _tgl_get_version(int fd) { struct tgl_ver_data data; int err; + char buf[STRERR_BUFSIZE]; err = ioctl(fd, TGL_IOCTL_GET_VERSION, &data); if (err) { - TBM_SPRD_ERROR("error(%s) %s:%d\n", strerror(errno)); + TBM_SPRD_ERROR("error(%s) %s:%d\n", + strerror_r(errno, buf, STRERR_BUFSIZE)); return 0; } @@ -279,13 +283,15 @@ _tgl_init(int fd, unsigned int key) { struct tgl_reg_data data; int err; + char buf[STRERR_BUFSIZE]; data.key = key; data.timeout_ms = 1000; err = ioctl(fd, TGL_IOCTL_REGISTER, &data); if (err) { - TBM_SPRD_ERROR("error(%s) key:%d\n", strerror(errno), key); + TBM_SPRD_ERROR("error(%s) key:%d\n", + strerror_r(errno, buf, STRERR_BUFSIZE), key); return 0; } @@ -297,11 +303,13 @@ _tgl_destroy(int fd, unsigned int key) { struct tgl_reg_data data; int err; + char buf[STRERR_BUFSIZE]; data.key = key; err = ioctl(fd, TGL_IOCTL_UNREGISTER, &data); if (err) { - TBM_SPRD_ERROR("error(%s) key:%d\n", strerror(errno), key); + TBM_SPRD_ERROR("error(%s) key:%d\n", + strerror_r(errno, buf, STRERR_BUFSIZE), key); return 0; } @@ -312,8 +320,9 @@ static inline int _tgl_lock(int fd, unsigned int key, int opt) { struct tgl_lock_data data; - enum tgl_type_data tgl_type; int err; + char buf[STRERR_BUFSIZE]; + enum tgl_type_data tgl_type; switch (opt) { case TBM_OPTION_READ: @@ -333,7 +342,7 @@ _tgl_lock(int fd, unsigned int key, int opt) err = ioctl(fd, TGL_IOCTL_LOCK, &data); if (err) { TBM_SPRD_ERROR("error(%s) key:%d opt:%d\n", - strerror(errno), key, opt); + strerror_r(errno, buf, STRERR_BUFSIZE), key, opt); return 0; } @@ -345,6 +354,7 @@ _tgl_unlock(int fd, unsigned int key) { struct tgl_lock_data data; int err; + char buf[STRERR_BUFSIZE]; data.key = key; data.type = TGL_TYPE_NONE; @@ -352,7 +362,7 @@ _tgl_unlock(int fd, unsigned int key) err = ioctl(fd, TGL_IOCTL_UNLOCK, &data); if (err) { TBM_SPRD_ERROR("error(%s) key:%d\n", - strerror(errno), key); + strerror_r(errno, buf, STRERR_BUFSIZE), key); return 0; } @@ -364,6 +374,7 @@ _tgl_set_data(int fd, unsigned int key, unsigned int val) { struct tgl_usr_data data; int err; + char buf[STRERR_BUFSIZE]; data.key = key; data.data1 = val; @@ -371,7 +382,7 @@ _tgl_set_data(int fd, unsigned int key, unsigned int val) err = ioctl(fd, TGL_IOCTL_SET_DATA, &data); if (err) { TBM_SPRD_ERROR("error(%s) key:%d\n", - strerror(errno), key); + strerror_r(errno, buf, STRERR_BUFSIZE), key); return 0; } @@ -383,13 +394,14 @@ _tgl_get_data(int fd, unsigned int key, unsigned int *locked) { struct tgl_usr_data data = { 0, }; int err; + char buf[STRERR_BUFSIZE]; data.key = key; err = ioctl(fd, TGL_IOCTL_GET_DATA, &data); if (err) { TBM_SPRD_ERROR("error(%s) key:%d\n", - strerror(errno), key); + strerror_r(errno, buf, STRERR_BUFSIZE), key); return 0; } @@ -581,7 +593,7 @@ _bo_set_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int device if (bo_sprd->flags_sprd & SPRD_BO_NONCACHABLE) return 1; - /* get cache state of a bo */ + /* get cache state of a bo_sprd */ bo_sprd->cache_state.val = _tgl_get_data(bufmgr_sprd->tgl_fd, bo_sprd->name, NULL); /* get global cache flush count */ @@ -773,7 +785,7 @@ _get_name(int fd, unsigned int gem) arg.handle = gem; if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &arg)) { - TBM_SPRD_ERROR("error fail to get flink gem=%d\n", gem); + TBM_SPRD_ERROR("fail to DRM_IOCTL_GEM_FLINK gem:%d", gem); return 0; } @@ -784,6 +796,7 @@ static tbm_bo_handle _sprd_bo_handle(tbm_bo_sprd bo_sprd, int device) { tbm_bo_handle bo_handle; + memset(&bo_handle, 0x0, sizeof(uint64_t)); switch (device) { @@ -843,6 +856,7 @@ _sprd_bo_handle(tbm_bo_sprd bo_sprd, int device) #endif break; default: + TBM_SPRD_ERROR("Not supported device:%d\n", device); bo_handle.ptr = (void *) NULL; break; } @@ -851,35 +865,44 @@ _sprd_bo_handle(tbm_bo_sprd bo_sprd, int device) } static int -tbm_sprd_bo_size(tbm_bo bo) +tbm_sprd_bo_get_size(tbm_backend_bo_data *bo_data, tbm_error_e *error) { - SPRD_RETURN_VAL_IF_FAIL(bo != NULL, 0); + tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data; - tbm_bo_sprd bo_sprd; + if (!bo_sprd) { + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return 0; + } - bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0); + if (error) + *error = TBM_ERROR_NONE; return bo_sprd->size; } -static void * -tbm_sprd_bo_alloc(tbm_bo bo, int size, int flags) +static tbm_backend_bo_data * +tbm_sprd_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, int size, tbm_bo_memory_type flags, tbm_error_e *error) { - SPRD_RETURN_VAL_IF_FAIL(bo != NULL, 0); - - tbm_bufmgr_sprd bufmgr_sprd; - unsigned int sprd_flags; + tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data; tbm_bo_sprd bo_sprd; + unsigned int sprd_flags; - bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0); + if (bufmgr_sprd == NULL) { + TBM_SPRD_ERROR("bufmgr_data is null\n"); + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return NULL; + } bo_sprd = calloc(1, sizeof(struct _tbm_bo_sprd)); if (!bo_sprd) { - TBM_SPRD_ERROR("error fail to allocate the bo private\n"); - return 0; + TBM_SPRD_ERROR("error fail to allocate the bo_sprd\n"); + if (error) + *error = TBM_ERROR_OUT_OF_MEMORY; + return NULL; } + bo_sprd->bufmgr_sprd = bufmgr_sprd; #ifdef USE_CONTIG_ONLY flags = TBM_BO_SCANOUT; @@ -891,14 +914,17 @@ tbm_sprd_bo_alloc(tbm_bo bo, int size, int flags) #endif // USE_CONTIG_ONLY struct drm_sprd_gem_create arg = {0, }; + arg.size = (uint64_t)size; arg.flags = sprd_flags; if (drmCommandWriteRead(bufmgr_sprd->fd, DRM_SPRD_GEM_CREATE, &arg, sizeof(arg))) { - TBM_SPRD_ERROR("error Cannot create bo(flag:%x, size:%d)\n", + TBM_SPRD_ERROR("error Cannot create bo_sprd(flag:%x, size:%d)\n", arg.flags, (unsigned int)arg.size); free(bo_sprd); - return 0; + if (error) + *error = TBM_ERROR_OPERATION_FAILED; + return NULL; } bo_sprd->fd = bufmgr_sprd->fd; @@ -911,126 +937,130 @@ tbm_sprd_bo_alloc(tbm_bo bo, int size, int flags) if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 0)) { TBM_SPRD_ERROR("error fail init cache state(%d)\n", bo_sprd->name); free(bo_sprd); - return 0; + if (error) + *error = TBM_ERROR_OPERATION_FAILED; + return NULL; } pthread_mutex_init(&bo_sprd->mutex, NULL); - if (bufmgr_sprd->use_dma_fence - && !bo_sprd->dmabuf) { + if (bufmgr_sprd->use_dma_fence && !bo_sprd->dmabuf) { struct drm_prime_handle arg = {0, }; arg.handle = bo_sprd->gem; if (drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) { TBM_SPRD_ERROR("error Cannot dmabuf=%d\n", bo_sprd->gem); free(bo_sprd); - return 0; + if (error) + *error = TBM_ERROR_OPERATION_FAILED; + return NULL; } bo_sprd->dmabuf = arg.fd; } - /* add bo to hash */ - PrivGem *privGem = calloc(1, sizeof(PrivGem)); - if (!privGem) { - TBM_SPRD_ERROR("error Fail to calloc PrivGem\n"); - free(bo_sprd); - return 0; - } - - privGem->ref_count = 1; - privGem->bo_priv = bo_sprd; - if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)privGem) < 0) - TBM_SPRD_ERROR("error Cannot insert bo to Hash(%d)\n", bo_sprd->name); + /* add bo_sprd to hash */ + if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)bo_sprd) < 0) + TBM_SPRD_ERROR("Cannot insert bo_sprd to Hash(%d)\n", bo_sprd->name); TBM_SPRD_DEBUG("%s size:%d, gem:%d(%d), flags:%d(%d)\n", __FUNCTION__, bo_sprd->size, bo_sprd->gem, bo_sprd->name, flags, sprd_flags); - return (void *)bo_sprd; + if (error) + *error = TBM_ERROR_NONE; + + return (tbm_backend_bo_data *)bo_sprd; } static void -tbm_sprd_bo_free(tbm_bo bo) +tbm_sprd_bo_free(tbm_backend_bo_data *bo_data) { - tbm_bo_sprd bo_sprd; + tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data; + tbm_bo_sprd temp; tbm_bufmgr_sprd bufmgr_sprd; + char buf[STRERR_BUFSIZE]; + int ret; - if (!bo) + if (!bo_data) return; - bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo); - SPRD_RETURN_IF_FAIL(bufmgr_sprd != NULL); - - bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo); - SPRD_RETURN_IF_FAIL(bo_sprd != NULL); + bufmgr_sprd = bo_sprd->bufmgr_sprd; + if (!bufmgr_sprd) + return; - TBM_SPRD_DEBUG("size:%d, gem:%d(%d)\n", - bo_sprd->size, bo_sprd->gem, bo_sprd->name); + TBM_SPRD_DEBUG(" bo_sprd:%p, gem:%d(%d), fd:%d, size:%d\n", + bo_sprd, + bo_sprd->gem, bo_sprd->name, + bo_sprd->dmabuf, + bo_sprd->size); if (bo_sprd->pBase) { - if (munmap(bo_sprd->pBase, bo_sprd->size) == -1) - TBM_SPRD_ERROR("error fail to munmap.\n"); + if (munmap(bo_sprd->pBase, bo_sprd->size) == -1) { + TBM_SPRD_ERROR("bo_sprd:%p fail to munmap(%s)\n", + bo_sprd, strerror_r(errno, buf, STRERR_BUFSIZE)); + } } - /* closedmabuf */ + /* close dmabuf */ if (bo_sprd->dmabuf) { close(bo_sprd->dmabuf); bo_sprd->dmabuf = 0; } /* delete bo from hash */ - PrivGem *privGem = NULL; - int ret; + ret = drmHashLookup(bufmgr_sprd->hashBos, bo_sprd->name, + (void **)&temp); + if (ret == 0) + drmHashDelete(bufmgr_sprd->hashBos, bo_sprd->name); + else + TBM_SPRD_ERROR("Cannot find bo_sprd to Hash(%d), ret=%d\n", bo_sprd->name, ret); - ret = drmHashLookup(bufmgr_sprd->hashBos, bo_sprd->name, (void **)&privGem); - if (ret == 0) { - privGem->ref_count--; - if (privGem->ref_count == 0) { - drmHashDelete(bufmgr_sprd->hashBos, bo_sprd->name); - free(privGem); - privGem = NULL; - } - } else { - TBM_SPRD_DEBUG("warning Cannot find bo to Hash(%d), ret=%d\n", bo_sprd->name, ret); - } + if (temp != bo_sprd) + TBM_SPRD_ERROR("hashBos probably has several BOs with same name!!!\n"); _bo_destroy_cache_state(bufmgr_sprd, bo_sprd); /* Free gem handle */ struct drm_gem_close arg = {0, }; + memset(&arg, 0, sizeof(arg)); arg.handle = bo_sprd->gem; if (drmIoctl(bo_sprd->fd, DRM_IOCTL_GEM_CLOSE, &arg)) - TBM_SPRD_ERROR("error fail to DRM_IOCTL_GEM_CLOSE\n"); - + TBM_SPRD_ERROR("bo_sprd:%p fail to gem close.(%s)\n", + bo_sprd, strerror_r(errno, buf, STRERR_BUFSIZE)); free(bo_sprd); } - -static void * -tbm_sprd_bo_import(tbm_bo bo, unsigned int key) +static tbm_backend_bo_data * +tbm_sprd_bufmgr_import(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tbm_error_e *error) { - SPRD_RETURN_VAL_IF_FAIL(bo != NULL, NULL); - - tbm_bufmgr_sprd bufmgr_sprd; + tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data; tbm_bo_sprd bo_sprd; - PrivGem *privGem = NULL; int ret; - bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, NULL); + if (bufmgr_sprd == NULL) { + TBM_SPRD_ERROR("bufmgr_data is null\n"); + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return NULL; + } - ret = drmHashLookup(bufmgr_sprd->hashBos, key, (void **)&privGem); - if (ret == 0) - return privGem->bo_priv; + ret = drmHashLookup(bufmgr_sprd->hashBos, key, (void **)&bo_sprd); + if (ret == 0) { + if (error) + *error = TBM_ERROR_NONE; + return (tbm_backend_bo_data *)bo_sprd; + } - struct drm_sprd_gem_info info = {0, }; struct drm_gem_open arg = {0, }; + struct drm_sprd_gem_info info = {0, }; arg.name = key; if (drmIoctl(bufmgr_sprd->fd, DRM_IOCTL_GEM_OPEN, &arg)) { TBM_SPRD_ERROR("error Cannot open gem name=%d\n", key); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; return NULL; } @@ -1040,14 +1070,19 @@ tbm_sprd_bo_import(tbm_bo bo, unsigned int key) &info, sizeof(struct drm_sprd_gem_info))) { TBM_SPRD_ERROR("error Cannot get gem info=%d\n", key); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; goto fail_get_gem; } bo_sprd = calloc(1, sizeof(struct _tbm_bo_sprd)); if (!bo_sprd) { - TBM_SPRD_ERROR("error fail to allocate the bo private\n"); + TBM_SPRD_ERROR("error fail to allocate the bo_sprd\n"); + if (error) + *error = TBM_ERROR_OUT_OF_MEMORY; goto fail_alloc_bo; } + bo_sprd->bufmgr_sprd = bufmgr_sprd; bo_sprd->fd = bufmgr_sprd->fd; bo_sprd->gem = arg.handle; @@ -1063,6 +1098,8 @@ tbm_sprd_bo_import(tbm_bo bo, unsigned int key) if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 1)) { TBM_SPRD_ERROR("error fail init cache state(%d)\n", bo_sprd->name); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; goto fail_init_cache; } @@ -1072,32 +1109,29 @@ tbm_sprd_bo_import(tbm_bo bo, unsigned int key) arg.handle = bo_sprd->gem; if (drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) { TBM_SPRD_ERROR("error Cannot dmabuf=%d\n", bo_sprd->gem); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; goto fail_prime_handle_to_fd; } bo_sprd->dmabuf = arg.fd; } - /* add bo to hash */ - privGem = calloc(1, sizeof(PrivGem)); - if (!privGem) { - TBM_SPRD_ERROR("error Fail to alloc\n"); - goto fail_alloc_gem_priv; - } + /* add bo_sprd to hash */ + if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)bo_sprd) < 0) + TBM_SPRD_ERROR("Cannot insert bo_sprd to Hash(%d)\n", bo_sprd->name); - privGem->ref_count = 1; - privGem->bo_priv = bo_sprd; - if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)privGem) < 0) - TBM_SPRD_ERROR("error Cannot insert bo to Hash(%d)\n", bo_sprd->name); + TBM_SPRD_DEBUG(" bo_sprd:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d\n", + bo_sprd, + bo_sprd->gem, bo_sprd->name, + bo_sprd->dmabuf, + bo_sprd->flags_tbm, bo_sprd->flags_sprd, + bo_sprd->size); - TBM_SPRD_DEBUG("size:%d, gem:%d(%d), flags:%d(%d)\n", - bo_sprd->size, bo_sprd->gem, bo_sprd->name, - bo_sprd->flags_tbm, bo_sprd->flags_sprd); + if (error) + *error = TBM_ERROR_NONE; - return (void *)bo_sprd; + return (tbm_backend_bo_data *)bo_sprd; -fail_alloc_gem_priv: - if (bo_sprd->dmabuf) - close(bo_sprd->dmabuf); fail_prime_handle_to_fd: _bo_destroy_cache_state(bufmgr_sprd, bo_sprd); fail_init_cache: @@ -1111,51 +1145,64 @@ fail_get_gem: return NULL; } -static void * -tbm_sprd_bo_import_fd(tbm_bo bo, tbm_fd key) +static tbm_backend_bo_data * +tbm_sprd_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_error_e *error) { - SPRD_RETURN_VAL_IF_FAIL(bo != NULL, NULL); - - tbm_bufmgr_sprd bufmgr_sprd; + tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data; tbm_bo_sprd bo_sprd; + unsigned int gem; unsigned int name; - PrivGem *privGem; + int ret; + char buf[STRERR_BUFSIZE]; - bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, NULL); + if (bufmgr_sprd == NULL) { + TBM_SPRD_ERROR("bufmgr_data is null\n"); + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return NULL; + } - //getting handle from fd + /*getting handle from fd*/ struct drm_prime_handle arg = {0, }; - unsigned int gem; arg.fd = key; if (drmIoctl(bufmgr_sprd->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &arg)) { - TBM_SPRD_ERROR("error bo:%p Cannot get gem handle from fd:%d (%s)\n", - bo, arg.fd, strerror(errno)); + TBM_SPRD_ERROR("Cannot get gem handle from fd:%d (%s)\n", + arg.fd, strerror_r(errno, buf, STRERR_BUFSIZE)); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; return NULL; } gem = arg.handle; name = _get_name(bufmgr_sprd->fd, gem); if (!name) { - TBM_SPRD_ERROR("error bo:%p Cannot get name from gem:%d, fd:%d (%s)\n", - bo, gem, key, strerror(errno)); + TBM_SPRD_ERROR("Cannot get name from gem:%d, fd:%d (%s)\n", + gem, key, strerror_r(errno, buf, STRERR_BUFSIZE)); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; return NULL; } - if (!drmHashLookup(bufmgr_sprd->hashBos, name, (void **)&privGem)) { - if (gem == privGem->bo_priv->gem) - return privGem->bo_priv; + ret = drmHashLookup(bufmgr_sprd->hashBos, name, (void **)&bo_sprd); + if (ret == 0) { + if (gem == bo_sprd->gem) { + if (error) + *error = TBM_ERROR_NONE; + return bo_sprd; + } } - unsigned int real_size; - struct drm_sprd_gem_info info = {0, }; - - /* Determine size of bo. The fd-to-handle ioctl really should + /* Determine size of bo_sprd. 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). */ + * provided (estimated or guess size). + * */ + + unsigned int real_size; + struct drm_sprd_gem_info info = {0, }; + real_size = lseek(key, 0, SEEK_END); info.handle = gem; @@ -1163,8 +1210,10 @@ tbm_sprd_bo_import_fd(tbm_bo bo, tbm_fd key) DRM_SPRD_GEM_GET, &info, sizeof(struct drm_sprd_gem_info))) { - TBM_SPRD_ERROR("error bo:%p Cannot get gem info from gem:%d, fd:%d (%s)\n", - bo, gem, key, strerror(errno)); + TBM_SPRD_ERROR("Cannot get gem info from gem:%d, fd:%d (%s)\n", + gem, key, strerror_r(errno, buf, STRERR_BUFSIZE)); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; return NULL; } @@ -1173,176 +1222,207 @@ tbm_sprd_bo_import_fd(tbm_bo bo, tbm_fd key) bo_sprd = calloc(1, sizeof(struct _tbm_bo_sprd)); if (!bo_sprd) { - TBM_SPRD_ERROR("error bo:%p fail to allocate the bo private\n", bo); + TBM_SPRD_ERROR("error bo_sprd:%p fail to allocate the bo_sprd\n", bo_sprd); + if (error) + *error = TBM_ERROR_OUT_OF_MEMORY; return NULL; } + bo_sprd->bufmgr_sprd = bufmgr_sprd; bo_sprd->fd = bufmgr_sprd->fd; bo_sprd->gem = gem; bo_sprd->size = real_size; bo_sprd->flags_sprd = info.flags; bo_sprd->flags_tbm = _get_tbm_flag_from_sprd(bo_sprd->flags_sprd); - bo_sprd->name = name; - if (!bo_sprd->name) { - TBM_SPRD_ERROR("error bo:%p Cannot get name from gem:%d, fd:%d (%s)\n", - bo, gem, key, strerror(errno)); - goto fail_check_name; - } if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 1)) { TBM_SPRD_ERROR("error fail init cache state(%d)\n", bo_sprd->name); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; goto fail_init_cache; } - /* add bo to hash */ - privGem = calloc(1, sizeof(PrivGem)); - if (!privGem) { - TBM_SPRD_ERROR("error Fail to callocprivGem\n"); - goto fail_alloc_gem_priv; - } - - privGem->ref_count = 1; - privGem->bo_priv = bo_sprd; - if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)privGem) < 0) { - TBM_SPRD_ERROR("error bo:%p Cannot insert bo to Hash(%d) from gem:%d, fd:%d\n", - bo, bo_sprd->name, gem, key); - } + /* add bo_sprd to hash */ + if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)bo_sprd) < 0) + TBM_SPRD_ERROR("bo_sprd:%p Cannot insert bo_sprd to Hash(%d) from gem:%d, fd:%d\n", + bo_sprd, bo_sprd->name, gem, key); - TBM_SPRD_DEBUG("bo:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n", - bo, + TBM_SPRD_DEBUG("bo_sprd:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n", + bo_sprd, bo_sprd->gem, bo_sprd->name, bo_sprd->dmabuf, key, bo_sprd->flags_tbm, bo_sprd->flags_sprd, bo_sprd->size); - return (void *)bo_sprd; + if (error) + *error = TBM_ERROR_NONE; + + return (tbm_backend_bo_data *)bo_sprd; -fail_alloc_gem_priv: - _bo_destroy_cache_state(bufmgr_sprd, bo_sprd); fail_init_cache: -fail_check_name: free(bo_sprd); return NULL; } -static unsigned int -tbm_sprd_bo_export(tbm_bo bo) +static tbm_key +tbm_sprd_bo_export_key(tbm_backend_bo_data *bo_data, tbm_error_e *error) { - SPRD_RETURN_VAL_IF_FAIL(bo != NULL, 0); - - tbm_bo_sprd bo_sprd; + tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data; - bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0); + if (!bo_sprd) { + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return 0; + } if (!bo_sprd->name) { bo_sprd->name = _get_name(bo_sprd->fd, bo_sprd->gem); if (!bo_sprd->name) { TBM_SPRD_ERROR("error Cannot get name\n"); + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; return 0; } } - TBM_SPRD_DEBUG("size:%d, gem:%d(%d), flags:%d(%d)\n", - bo_sprd->size, bo_sprd->gem, bo_sprd->name, - bo_sprd->flags_tbm, bo_sprd->flags_sprd); + TBM_SPRD_DEBUG(" bo_sprd:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d\n", + bo_sprd, + bo_sprd->gem, bo_sprd->name, + bo_sprd->dmabuf, + bo_sprd->flags_tbm, bo_sprd->flags_sprd, + bo_sprd->size); + + if (error) + *error = TBM_ERROR_NONE; - return (unsigned int)bo_sprd->name; + return (tbm_key)bo_sprd->name; } static tbm_fd -tbm_sprd_bo_export_fd(tbm_bo bo) +tbm_sprd_bo_export_fd(tbm_backend_bo_data *bo_data, tbm_error_e *error) { - SPRD_RETURN_VAL_IF_FAIL(bo != NULL, -1); - - tbm_bo_sprd bo_sprd; + tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data; + struct drm_prime_handle arg = {0, }; int ret; + char buf[STRERR_BUFSIZE]; - bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, -1); - - struct drm_prime_handle arg = {0, }; + if (!bo_sprd) { + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return -1; + } arg.handle = bo_sprd->gem; ret = drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg); if (ret) { - TBM_SPRD_ERROR("error bo:%p Cannot dmabuf=%d (%s)\n", - bo, bo_sprd->gem, strerror(errno)); + TBM_SPRD_ERROR("bo_sprd:%p Cannot dmabuf=%d (%s)\n", + bo_sprd, bo_sprd->gem, strerror_r(errno, buf, STRERR_BUFSIZE)); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; return (tbm_fd) ret; } - TBM_SPRD_DEBUG("bo:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n", - bo, + TBM_SPRD_DEBUG("bo_sprd:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n", + bo_sprd, bo_sprd->gem, bo_sprd->name, bo_sprd->dmabuf, arg.fd, bo_sprd->flags_tbm, bo_sprd->flags_sprd, bo_sprd->size); + if (error) + *error = TBM_ERROR_NONE; + return (tbm_fd)arg.fd; } - static tbm_bo_handle -tbm_sprd_bo_get_handle(tbm_bo bo, int device) +tbm_sprd_bo_get_handle(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, tbm_error_e *error) { - SPRD_RETURN_VAL_IF_FAIL(bo != NULL, (tbm_bo_handle) NULL); - + tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data; tbm_bo_handle bo_handle; - tbm_bo_sprd bo_sprd; - bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, (tbm_bo_handle) NULL); + if (!bo_sprd) { + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return (tbm_bo_handle) NULL; + } if (!bo_sprd->gem) { - TBM_SPRD_ERROR("error Cannot map gem=%d\n", bo_sprd->gem); + TBM_SPRD_ERROR("Cannot map gem=%d\n", bo_sprd->gem); + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; return (tbm_bo_handle) NULL; } - TBM_SPRD_DEBUG("gem:%d(%d), %s\n", - bo_sprd->gem, bo_sprd->name, STR_DEVICE[device]); + TBM_SPRD_DEBUG("bo_sprd:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d, %s\n", + bo_sprd, + bo_sprd->gem, bo_sprd->name, + bo_sprd->dmabuf, + bo_sprd->flags_tbm, bo_sprd->flags_sprd, + bo_sprd->size, + STR_DEVICE[device]); /*Get mapped bo_handle*/ bo_handle = _sprd_bo_handle(bo_sprd, device); if (bo_handle.ptr == NULL) { - TBM_SPRD_ERROR("error Cannot get handle: gem:%d, device:%d\n", + TBM_SPRD_ERROR("Cannot get handle: gem:%d, device:%d\n", bo_sprd->gem, device); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; return (tbm_bo_handle) NULL; } + if (error) + *error = TBM_ERROR_NONE; + return bo_handle; } static tbm_bo_handle -tbm_sprd_bo_map(tbm_bo bo, int device, int opt) +tbm_sprd_bo_map(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, + tbm_bo_access_option opt, tbm_error_e *error) { - SPRD_RETURN_VAL_IF_FAIL(bo != NULL, (tbm_bo_handle) NULL); - + tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data; tbm_bo_handle bo_handle; - tbm_bo_sprd bo_sprd; tbm_bufmgr_sprd bufmgr_sprd; - bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, (tbm_bo_handle) NULL); + if (!bo_sprd) { + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return (tbm_bo_handle) NULL; + } - bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, (tbm_bo_handle) NULL); + bufmgr_sprd = bo_sprd->bufmgr_sprd; + if (!bufmgr_sprd) { + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return (tbm_bo_handle) NULL; + } if (!bo_sprd->gem) { - TBM_SPRD_ERROR("error Cannot map gem=%d\n", bo_sprd->gem); + TBM_SPRD_ERROR("Cannot map gem=%d\n", bo_sprd->gem); + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; return (tbm_bo_handle) NULL; } - TBM_SPRD_DEBUG("%s gem:%d(%d), %s, %s\n", - __FUNCTION__, bo_sprd->gem, bo_sprd->name, STR_DEVICE[device], STR_OPT[opt]); + TBM_SPRD_DEBUG(" bo_sprd:%p, gem:%d(%d), fd:%d, %s, %s\n", + bo_sprd, + bo_sprd->gem, bo_sprd->name, + bo_sprd->dmabuf, + STR_DEVICE[device], + STR_OPT[opt]); /*Get mapped bo_handle*/ bo_handle = _sprd_bo_handle(bo_sprd, device); if (bo_handle.ptr == NULL) { - TBM_SPRD_ERROR("error Cannot get handle: gem:%d, device:%d, opt:%d\n", + TBM_SPRD_ERROR("Cannot get handle: gem:%d, device:%d, opt:%d\n", bo_sprd->gem, device, opt); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; return (tbm_bo_handle) NULL; } @@ -1351,49 +1431,58 @@ tbm_sprd_bo_map(tbm_bo bo, int device, int opt) bo_sprd->map_cnt++; + if (error) + *error = TBM_ERROR_NONE; + return bo_handle; } -static int -tbm_sprd_bo_unmap(tbm_bo bo) +static tbm_error_e +tbm_sprd_bo_unmap(tbm_backend_bo_data *bo_data) { - SPRD_RETURN_VAL_IF_FAIL(bo != NULL, 0); - + tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data; tbm_bufmgr_sprd bufmgr_sprd; - tbm_bo_sprd bo_sprd; - bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0); + if (!bo_sprd) + return TBM_ERROR_INVALID_PARAMETER; - bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0); + bufmgr_sprd = bo_sprd->bufmgr_sprd; + if (!bufmgr_sprd) + return TBM_ERROR_INVALID_PARAMETER; if (!bo_sprd->gem) - return 0; + return TBM_ERROR_INVALID_PARAMETER; bo_sprd->map_cnt--; if (bo_sprd->map_cnt == 0) _bo_save_cache_state(bufmgr_sprd, bo_sprd); - TBM_SPRD_DEBUG("gem:%d(%d) \n", bo_sprd->gem, bo_sprd->name); + TBM_SPRD_DEBUG(" bo_sprd:%p, gem:%d(%d), fd:%d\n", + bo_sprd, + bo_sprd->gem, bo_sprd->name, + bo_sprd->dmabuf); - return 1; + return TBM_ERROR_NONE; } static void -tbm_sprd_bufmgr_deinit(void *priv) +tbm_sprd_deinit(tbm_backend_bufmgr_data *bufmgr_data) { - SPRD_RETURN_IF_FAIL(priv != NULL); + tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data; + tbm_bufmgr bufmgr; + tbm_error_e error; + unsigned long key; + void *value; - tbm_bufmgr_sprd bufmgr_sprd; + SPRD_RETURN_IF_FAIL(bufmgr_sprd != NULL); - bufmgr_sprd = (tbm_bufmgr_sprd)priv; + bufmgr = bufmgr_sprd->bufmgr; - if (bufmgr_sprd->hashBos) { - unsigned long key; - void *value; + tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_sprd->bufmgr_func); + tbm_backend_bufmgr_free_bo_func(bufmgr, bufmgr_sprd->bo_func); + if (bufmgr_sprd->hashBos) { while (drmHashFirst(bufmgr_sprd->hashBos, &key, &value) > 0) { free(value); drmHashDelete(bufmgr_sprd->hashBos, key); @@ -1406,7 +1495,7 @@ tbm_sprd_bufmgr_deinit(void *priv) if (bufmgr_sprd->bind_display) tbm_drm_helper_wl_auth_server_deinit(); - if (tbm_backend_is_display_server()) + if (tbm_backend_bufmgr_query_display_server(bufmgr, &error)) tbm_drm_helper_unset_tbm_master_fd(); tbm_drm_helper_unset_fd(); @@ -1421,44 +1510,36 @@ tbm_sprd_bufmgr_deinit(void *priv) free(bufmgr_sprd); } -static int -tbm_sprd_surface_supported_format(uint32_t **formats, uint32_t *num) +static tbm_error_e +tbm_sprd_bufmgr_get_supported_formats(tbm_backend_bufmgr_data *bufmgr_data, + uint32_t **formats, uint32_t *num) { + tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data; uint32_t *color_formats; - color_formats = (uint32_t *)calloc(1, - sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT); + SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, TBM_ERROR_INVALID_PARAMETER); + + color_formats = (uint32_t *)calloc(1, sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT); if (color_formats == NULL) - return 0; + return TBM_ERROR_OUT_OF_MEMORY; - memcpy(color_formats, tbm_sprd_color_format_list, - sizeof(uint32_t)*TBM_COLOR_FORMAT_COUNT); + memcpy(color_formats, tbm_sprd_color_format_list, sizeof(uint32_t)*TBM_COLOR_FORMAT_COUNT); *formats = color_formats; *num = TBM_COLOR_FORMAT_COUNT; - return 1; -} + TBM_SPRD_DEBUG("supported format count = %d\n", *num); + return TBM_ERROR_NONE; +} -/** - * @brief get the plane data of the surface. - * @param[in] width : the width of the surface - * @param[in] height : the height of the surface - * @param[in] format : the format of the surface - * @param[in] plane_idx : the format of the surface - * @param[out] size : the size of the plane - * @param[out] offset : the offset of the plane - * @param[out] pitch : the pitch of the plane - * @param[out] padding : the padding of the plane - * @return 1 if this function succeeds, otherwise 0. - */ -static int -tbm_sprd_surface_get_plane_data(int width, int height, - tbm_format format, int plane_idx, uint32_t *size, uint32_t *offset, - uint32_t *pitch, int *bo_idx) +static tbm_error_e +tbm_sprd_bufmgr_get_plane_data(tbm_backend_bufmgr_data *bufmgr_data, + tbm_format format, int plane_idx, int width, + int height, uint32_t *size, uint32_t *offset, + uint32_t *pitch, int *bo_idx) { - int ret = 1; + tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data; int bpp; int _offset = 0; int _pitch = 0; @@ -1466,6 +1547,8 @@ tbm_sprd_surface_get_plane_data(int width, int height, int _bo_idx = 0; int _align_height = 0; + SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, TBM_ERROR_INVALID_PARAMETER); + switch (format) { /* 16 bpp RGB */ case TBM_FORMAT_XRGB4444: @@ -1699,73 +1782,88 @@ tbm_sprd_surface_get_plane_data(int width, int height, *pitch = _pitch; *bo_idx = _bo_idx; - return ret; + return TBM_ERROR_NONE; } -static int -tbm_sprd_bo_get_flags(tbm_bo bo) +static tbm_bo_memory_type +tbm_sprd_bo_get_memory_type(tbm_backend_bo_data *bo_data, tbm_error_e *error) { - SPRD_RETURN_VAL_IF_FAIL(bo != NULL, 0); + tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data; - tbm_bo_sprd bo_sprd; + if (!bo_sprd) { + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return TBM_BO_DEFAULT; + } - bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo); - SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0); + if (error) + *error = TBM_ERROR_NONE; return bo_sprd->flags_tbm; } -static int -tbm_sprd_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *NativeDisplay) +static tbm_bufmgr_capability +tbm_sprd_bufmgr_get_capabilities(tbm_backend_bufmgr_data *bufmgr_data, tbm_error_e *error) { - tbm_bufmgr_sprd bufmgr_sprd; + tbm_bufmgr_capability capabilities = TBM_BUFMGR_CAPABILITY_NONE; - bufmgr_sprd = tbm_backend_get_priv_from_bufmgr(bufmgr); - SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0); + capabilities = TBM_BUFMGR_CAPABILITY_SHARE_KEY|TBM_BUFMGR_CAPABILITY_SHARE_FD; + + if (error) + *error = TBM_ERROR_NONE; + + return capabilities; +} + +static tbm_error_e +tbm_sprd_bufmgr_bind_native_display(tbm_backend_bufmgr_data *bufmgr_data, tbm_native_display *native_display) +{ + tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data; + SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, TBM_ERROR_INVALID_PARAMETER); - if (!tbm_drm_helper_wl_auth_server_init(NativeDisplay, bufmgr_sprd->fd, + if (!tbm_drm_helper_wl_auth_server_init(native_display, bufmgr_sprd->fd, bufmgr_sprd->device_name, 0)) { TBM_SPRD_ERROR("fail to tbm_drm_helper_wl_server_init\n"); - return 0; + return TBM_ERROR_OPERATION_FAILED; } - bufmgr_sprd->bind_display = NativeDisplay; + bufmgr_sprd->bind_display = native_display; - return 1; + return TBM_ERROR_NONE; } -MODULEINITPPROTO(init_tbm_bufmgr_priv); - -static TBMModuleVersionInfo SprdVersRec = { - "sprd", - "Samsung", - TBM_ABI_VERSION, -}; - -TBMModuleData tbmModuleData = { &SprdVersRec, init_tbm_bufmgr_priv}; - -int -init_tbm_bufmgr_priv(tbm_bufmgr bufmgr, int fd) +static tbm_backend_bufmgr_data * +tbm_sprd_init(tbm_bufmgr bufmgr, tbm_error_e *error) { - tbm_bufmgr_backend bufmgr_backend; - tbm_bufmgr_sprd bufmgr_sprd; + tbm_bufmgr_sprd bufmgr_sprd = NULL; + tbm_backend_bufmgr_func *bufmgr_func = NULL; + tbm_backend_bo_func *bo_func = NULL; int fp; + tbm_error_e err; - if (!bufmgr) - return 0; + if (!bufmgr) { + TBM_SPRD_ERROR("bufmgr is null.\n"); + if (error) + *error = TBM_ERROR_INVALID_PARAMETER; + return NULL; + } bufmgr_sprd = calloc(1, sizeof(struct _tbm_bufmgr_sprd)); if (!bufmgr_sprd) { TBM_SPRD_ERROR("fail to alloc bufmgr_sprd!\n"); - return 0; + if (error) + *error = TBM_ERROR_OUT_OF_MEMORY; + return NULL; } - if (tbm_backend_is_display_server()) { + if (tbm_backend_bufmgr_query_display_server(bufmgr, &err)) { bufmgr_sprd->fd = tbm_drm_helper_get_master_fd(); if (bufmgr_sprd->fd < 0) { bufmgr_sprd->fd = _tbm_sprd_open_drm(); if (bufmgr_sprd->fd < 0) { TBM_SPRD_ERROR("fail to open drm!\n"); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; goto fail_open_drm; } } @@ -1776,20 +1874,21 @@ init_tbm_bufmgr_priv(tbm_bufmgr bufmgr, int fd) if (!bufmgr_sprd->device_name) { TBM_SPRD_ERROR("fail to get device name!\n"); tbm_drm_helper_unset_tbm_master_fd(); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; goto fail_get_device_name; } } else { if (!tbm_drm_helper_get_auth_info(&(bufmgr_sprd->fd), &(bufmgr_sprd->device_name), NULL)) { TBM_SPRD_ERROR("fail to get auth drm info!\n"); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; goto fail_get_auth_info; } } tbm_drm_helper_set_fd(bufmgr_sprd->fd); - //Create Hash Table - bufmgr_sprd->hashBos = drmHashCreate(); - //Check if the tbm manager supports dma fence or not. fp = open("/sys/module/dmabuf_sync/parameters/enabled", O_RDONLY); if (fp != -1) { @@ -1804,38 +1903,69 @@ init_tbm_bufmgr_priv(tbm_bufmgr bufmgr, int fd) if (!_bufmgr_init_cache_state(bufmgr_sprd)) { TBM_SPRD_ERROR("fail to init bufmgr cache state\n"); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; goto fail_init_cache_state; } - bufmgr_backend = tbm_backend_alloc(); - if (!bufmgr_backend) { - TBM_SPRD_ERROR("fail to alloc backend!\n"); - goto fail_alloc_backend; - } - - bufmgr_backend->priv = (void *)bufmgr_sprd; - bufmgr_backend->bufmgr_deinit = tbm_sprd_bufmgr_deinit; - bufmgr_backend->bo_size = tbm_sprd_bo_size; - bufmgr_backend->bo_alloc = tbm_sprd_bo_alloc; - bufmgr_backend->bo_free = tbm_sprd_bo_free; - bufmgr_backend->bo_import = tbm_sprd_bo_import; - bufmgr_backend->bo_import_fd = tbm_sprd_bo_import_fd; - bufmgr_backend->bo_export = tbm_sprd_bo_export; - bufmgr_backend->bo_export_fd = tbm_sprd_bo_export_fd; - bufmgr_backend->bo_get_handle = tbm_sprd_bo_get_handle; - bufmgr_backend->bo_map = tbm_sprd_bo_map; - bufmgr_backend->bo_unmap = tbm_sprd_bo_unmap; - bufmgr_backend->surface_get_plane_data = tbm_sprd_surface_get_plane_data; - bufmgr_backend->surface_supported_format = tbm_sprd_surface_supported_format; - bufmgr_backend->bo_get_flags = tbm_sprd_bo_get_flags; - bufmgr_backend->bo_lock = NULL; - bufmgr_backend->bo_unlock = NULL; - bufmgr_backend->bufmgr_bind_native_display = tbm_sprd_bufmgr_bind_native_display; - - if (!tbm_backend_init(bufmgr, bufmgr_backend)) { - TBM_SPRD_ERROR("fail to init backend!\n"); - goto fail_init_backend; - } + /*Create Hash Table*/ + bufmgr_sprd->hashBos = drmHashCreate(); + + /* alloc and register bufmgr_funcs */ + bufmgr_func = tbm_backend_bufmgr_alloc_bufmgr_func(bufmgr, &err); + if (!bufmgr_func) { + TBM_SPRD_ERROR("fail to alloc bufmgr_func! err(%d)\n", err); + if (error) + *error = TBM_ERROR_OUT_OF_MEMORY; + goto fail_alloc_bufmgr_func; + } + + bufmgr_func->bufmgr_get_capabilities = tbm_sprd_bufmgr_get_capabilities; + bufmgr_func->bufmgr_bind_native_display = tbm_sprd_bufmgr_bind_native_display; + bufmgr_func->bufmgr_get_supported_formats = tbm_sprd_bufmgr_get_supported_formats; + bufmgr_func->bufmgr_get_plane_data = tbm_sprd_bufmgr_get_plane_data; + bufmgr_func->bufmgr_alloc_bo = tbm_sprd_bufmgr_alloc_bo; + bufmgr_func->bufmgr_alloc_bo_with_format = NULL; + bufmgr_func->bufmgr_import_fd = tbm_sprd_bufmgr_import_fd; + bufmgr_func->bufmgr_import_key = tbm_sprd_bufmgr_import; + + err = tbm_backend_bufmgr_register_bufmgr_func(bufmgr, bufmgr_func); + if (err != TBM_ERROR_NONE) { + TBM_SPRD_ERROR("fail to register bufmgr_func! err(%d)\n", err); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; + goto fail_register_bufmgr_func; + } + bufmgr_sprd->bufmgr_func = bufmgr_func; + + /* alloc and register bo_funcs */ + bo_func = tbm_backend_bufmgr_alloc_bo_func(bufmgr, &err); + if (!bo_func) { + TBM_SPRD_ERROR("fail to alloc bo_func! err(%d)\n", err); + if (error) + *error = TBM_ERROR_OUT_OF_MEMORY; + goto fail_alloc_bo_func; + } + + bo_func->bo_free = tbm_sprd_bo_free; + bo_func->bo_get_size = tbm_sprd_bo_get_size; + bo_func->bo_get_memory_types = tbm_sprd_bo_get_memory_type; + bo_func->bo_get_handle = tbm_sprd_bo_get_handle; + bo_func->bo_map = tbm_sprd_bo_map; + bo_func->bo_unmap = tbm_sprd_bo_unmap; + bo_func->bo_lock = NULL; + bo_func->bo_unlock = NULL; + bo_func->bo_export_fd = tbm_sprd_bo_export_fd; + bo_func->bo_export_key = tbm_sprd_bo_export_key; + + err = tbm_backend_bufmgr_register_bo_func(bufmgr, bo_func); + if (err != TBM_ERROR_NONE) { + TBM_SPRD_ERROR("fail to register bo_func! err(%d)\n", err); + if (error) + *error = TBM_ERROR_OPERATION_FAILED; + goto fail_register_bo_func; + } + bufmgr_sprd->bo_func = bo_func; #ifdef DEBUG { @@ -1854,16 +1984,24 @@ init_tbm_bufmgr_priv(tbm_bufmgr bufmgr, int fd) bufmgr_sprd->use_dma_fence ? "supported!" : "NOT supported!"); TBM_SPRD_DEBUG("fd:%d\n", bufmgr_sprd->fd); - return 1; + if (error) + *error = TBM_ERROR_NONE; -fail_init_backend: - tbm_backend_free(bufmgr_backend); -fail_alloc_backend: + bufmgr_sprd->bufmgr = bufmgr; + + return (tbm_backend_bufmgr_data *)bufmgr_sprd; + +fail_register_bo_func: + tbm_backend_bufmgr_free_bo_func(bufmgr, bo_func); +fail_alloc_bo_func: +fail_register_bufmgr_func: + tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_func); +fail_alloc_bufmgr_func: _bufmgr_deinit_cache_state(bufmgr_sprd); -fail_init_cache_state: if (bufmgr_sprd->hashBos) drmHashDestroy(bufmgr_sprd->hashBos); - if (tbm_backend_is_display_server()) +fail_init_cache_state: + if (tbm_backend_bufmgr_query_display_server(bufmgr, &err)) tbm_drm_helper_unset_tbm_master_fd(); tbm_drm_helper_unset_fd(); if (bufmgr_sprd->device_name) @@ -1873,6 +2011,13 @@ fail_get_device_name: fail_get_auth_info: fail_open_drm: free(bufmgr_sprd); - return 0; + return NULL; } +tbm_backend_module tbm_backend_module_data = { + "sprd", + "Samsung", + TBM_BACKEND_ABI_VERSION_2_0, + tbm_sprd_init, + tbm_sprd_deinit +}; -- 2.7.4