- TBM_TRACE("bo(%p) size(%d)\n", bo, size);
-
- _tbm_bufmgr_mutex_unlock();
-
- return size;
-}
-
-tbm_bo
-tbm_bo_ref(tbm_bo bo)
-{
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), NULL);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), NULL);
-
- bo->ref_cnt++;
-
- TBM_TRACE("bo(%p) ref_cnt(%d)\n", bo, bo->ref_cnt);
-
- _tbm_bufmgr_mutex_unlock();
-
- return bo;
-}
-
-void
-tbm_bo_unref(tbm_bo bo)
-{
- tbm_bufmgr bufmgr = gBufMgr;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_IF_FAIL(gBufMgr);
- TBM_BUFMGR_RETURN_IF_FAIL(_tbm_bo_is_valid(bo));
-
- TBM_TRACE("bo(%p) ref_cnt(%d)\n", bo, bo->ref_cnt - 1);
-
- if (bo->ref_cnt <= 0) {
- _tbm_bufmgr_mutex_unlock();
- return;
- }
-
- bo->ref_cnt--;
- if (bo->ref_cnt == 0)
- _tbm_bo_free(bo);
-
- _tbm_bufmgr_mutex_unlock();
-}
-
-tbm_bo
-tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
-{
- void *bo_priv;
- tbm_bo bo;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, NULL);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(size > 0, NULL);
-
- bo = calloc(1, sizeof(struct _tbm_bo));
- if (!bo) {
- /* LCOV_EXCL_START */
- TBM_LOG_E("error: fail to create of tbm_bo size(%d) flag(%s)\n",
- size, _tbm_flag_to_str(flags));
- _tbm_set_last_result(TBM_BO_ERROR_HEAP_ALLOC_FAILED);
- _tbm_bufmgr_mutex_unlock();
- return NULL;
- /* LCOV_EXCL_STOP */
- }
-
- _tbm_util_check_bo_cnt(bufmgr);
-
- bo->bufmgr = bufmgr;
-
- bo_priv = bufmgr->backend->bo_alloc(bo, size, flags);
- if (!bo_priv) {
- /* LCOV_EXCL_START */
- TBM_LOG_E("error: fail to create of tbm_bo size(%d) flag(%s)\n",
- size, _tbm_flag_to_str(flags));
- _tbm_set_last_result(TBM_BO_ERROR_BO_ALLOC_FAILED);
- free(bo);
- _tbm_bufmgr_mutex_unlock();
- return NULL;
- /* LCOV_EXCL_STOP */
- }
-
- bufmgr->bo_cnt++;
-
- bo->ref_cnt = 1;
- bo->flags = flags;
- bo->priv = bo_priv;
-
- TBM_TRACE("bo(%p) size(%d) refcnt(%d), flag(%s)\n", bo, size, bo->ref_cnt,
- _tbm_flag_to_str(bo->flags));
-
- LIST_INITHEAD(&bo->user_data_list);
-
- LIST_ADD(&bo->item_link, &bufmgr->bo_list);
-
- _tbm_bufmgr_mutex_unlock();
-
- return bo;
-}
-
-tbm_bo
-tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
-{
- void *bo_priv;
- tbm_bo bo;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, NULL);
-
- if (!bufmgr->backend->bo_import) {
- /* LCOV_EXCL_START */
- _tbm_bufmgr_mutex_unlock();
- return NULL;
- /* LCOV_EXCL_STOP */
- }
-
- _tbm_util_check_bo_cnt(bufmgr);
-
- bo = calloc(1, sizeof(struct _tbm_bo));
- if (!bo) {
- /* LCOV_EXCL_START */
- TBM_LOG_E("error: fail to import of tbm_bo by key(%d)\n", key);
- _tbm_bufmgr_mutex_unlock();
- return NULL;
- /* LCOV_EXCL_STOP */
- }
-
- bo->bufmgr = bufmgr;
-
- bo_priv = bufmgr->backend->bo_import(bo, key);
- if (!bo_priv) {
- /* LCOV_EXCL_START */
- TBM_LOG_E("error: fail to import of tbm_bo by key(%d)\n", key);
- _tbm_set_last_result(TBM_BO_ERROR_IMPORT_FAILED);
- free(bo);
- _tbm_bufmgr_mutex_unlock();
- return NULL;
- /* LCOV_EXCL_STOP */
- }
-
- if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
- tbm_bo bo2 = NULL;
-
- LIST_FOR_EACH_ENTRY(bo2, &bufmgr->bo_list, item_link) {
- if (bo2->priv == bo_priv) {
- TBM_TRACE("find bo(%p) ref(%d) key(%d) flag(%s) in list\n",
- bo2, bo2->ref_cnt, key,
- _tbm_flag_to_str(bo2->flags));
- bo2->ref_cnt++;
- free(bo);
- _tbm_bufmgr_mutex_unlock();
- return bo2;
- }
- }
- }
-
- bufmgr->bo_cnt++;
-
- bo->ref_cnt = 1;
- bo->priv = bo_priv;
-
- if (bufmgr->backend->bo_get_flags)
- bo->flags = bufmgr->backend->bo_get_flags(bo);
- else
- bo->flags = TBM_BO_DEFAULT;
-
- TBM_TRACE("import new bo(%p) ref(%d) key(%d) flag(%s) in list\n",
- bo, bo->ref_cnt, key, _tbm_flag_to_str(bo->flags));
-
- LIST_INITHEAD(&bo->user_data_list);
-
- LIST_ADD(&bo->item_link, &bufmgr->bo_list);
-
- _tbm_bufmgr_mutex_unlock();
-
- return bo;
-}
-
-tbm_bo
-tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
-{
- void *bo_priv;
- tbm_bo bo;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, NULL);
-
- if (!bufmgr->backend->bo_import_fd) {
- /* LCOV_EXCL_START */
- _tbm_bufmgr_mutex_unlock();
- return NULL;
- /* LCOV_EXCL_STOP */
- }
-
- _tbm_util_check_bo_cnt(bufmgr);
-
- bo = calloc(1, sizeof(struct _tbm_bo));
- if (!bo) {
- /* LCOV_EXCL_START */
- TBM_LOG_E("error: fail to import tbm_bo by tbm_fd(%d)\n", fd);
- _tbm_bufmgr_mutex_unlock();
- return NULL;
- /* LCOV_EXCL_STOP */
- }
-
- bo->bufmgr = bufmgr;
-
- bo_priv = bufmgr->backend->bo_import_fd(bo, fd);
- if (!bo_priv) {
- /* LCOV_EXCL_START */
- TBM_LOG_E("error: fail to import tbm_bo by tbm_fd(%d)\n", fd);
- _tbm_set_last_result(TBM_BO_ERROR_IMPORT_FD_FAILED);
- free(bo);
- _tbm_bufmgr_mutex_unlock();
- return NULL;
- /* LCOV_EXCL_STOP */
- }
-
- if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
- tbm_bo bo2 = NULL;
-
- LIST_FOR_EACH_ENTRY(bo2, &bufmgr->bo_list, item_link) {
- if (bo2->priv == bo_priv) {
- TBM_TRACE("find bo(%p) ref(%d) fd(%d) flag(%s) in list\n",
- bo2, bo2->ref_cnt, fd,
- _tbm_flag_to_str(bo2->flags));
- bo2->ref_cnt++;
- free(bo);
- _tbm_bufmgr_mutex_unlock();
- return bo2;
- }
- }
- }
-
- bufmgr->bo_cnt++;
-
- bo->ref_cnt = 1;
- bo->priv = bo_priv;
-
- if (bufmgr->backend->bo_get_flags)
- bo->flags = bufmgr->backend->bo_get_flags(bo);
- else
- bo->flags = TBM_BO_DEFAULT;
-
- TBM_TRACE("import bo(%p) ref(%d) fd(%d) flag(%s)\n",
- bo, bo->ref_cnt, fd, _tbm_flag_to_str(bo->flags));
-
- LIST_INITHEAD(&bo->user_data_list);
-
- LIST_ADD(&bo->item_link, &bufmgr->bo_list);
-
- _tbm_bufmgr_mutex_unlock();
-
- return bo;
-}
-
-tbm_key
-tbm_bo_export(tbm_bo bo)
-{
- tbm_bufmgr bufmgr = gBufMgr;
- tbm_key ret;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
- if (!bufmgr->backend->bo_export) {
- /* LCOV_EXCL_START */
- _tbm_bufmgr_mutex_unlock();
- return 0;
- /* LCOV_EXCL_STOP */
- }
-
- ret = bufmgr->backend->bo_export(bo);
- if (!ret) {
- /* LCOV_EXCL_START */
- _tbm_set_last_result(TBM_BO_ERROR_EXPORT_FAILED);
- TBM_LOG_E("error: bo(%p) tbm_key(%d)\n", bo, ret);
- _tbm_bufmgr_mutex_unlock();
- return ret;
- /* LCOV_EXCL_STOP */
- }
-
- TBM_TRACE("bo(%p) tbm_key(%u)\n", bo, ret);
-
- _tbm_bufmgr_mutex_unlock();
-
- return ret;
-}
-
-tbm_fd
-tbm_bo_export_fd(tbm_bo bo)
-{
- tbm_bufmgr bufmgr = gBufMgr;
- tbm_fd ret;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), -1);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), -1);
-
- if (!bufmgr->backend->bo_export_fd) {
- /* LCOV_EXCL_START */
- _tbm_bufmgr_mutex_unlock();
- return -1;
- /* LCOV_EXCL_STOP */
- }
-
- ret = bufmgr->backend->bo_export_fd(bo);
- if (ret < 0) {
- /* LCOV_EXCL_START */
- _tbm_set_last_result(TBM_BO_ERROR_EXPORT_FD_FAILED);
- TBM_LOG_E("error: bo(%p) tbm_fd(%d)\n", bo, ret);
- _tbm_bufmgr_mutex_unlock();
- return ret;
- /* LCOV_EXCL_STOP */
- }
-
- TBM_TRACE("bo(%p) tbm_fd(%d)\n", bo, ret);
-
- _tbm_bufmgr_mutex_unlock();
-
- return ret;
-}
-
-tbm_bo_handle
-tbm_bo_get_handle(tbm_bo bo, int device)
-{
- tbm_bufmgr bufmgr = gBufMgr;
- tbm_bo_handle bo_handle;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), (tbm_bo_handle) NULL);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), (tbm_bo_handle) NULL);
-
- bo_handle = bufmgr->backend->bo_get_handle(bo, device);
- if (bo_handle.ptr == NULL) {
- /* LCOV_EXCL_START */
- _tbm_set_last_result(TBM_BO_ERROR_GET_HANDLE_FAILED);
- TBM_LOG_E("error: bo(%p) bo_handle(%p)\n", bo, bo_handle.ptr);
- _tbm_bufmgr_mutex_unlock();
- return (tbm_bo_handle) NULL;
- /* LCOV_EXCL_STOP */
- }
-
- TBM_TRACE("bo(%p) bo_handle(%p)\n", bo, bo_handle.ptr);
-
- _tbm_bufmgr_mutex_unlock();
-
- return bo_handle;
-}
-
-tbm_bo_handle
-tbm_bo_map(tbm_bo bo, int device, int opt)
-{
- tbm_bufmgr bufmgr = gBufMgr;
- tbm_bo_handle bo_handle;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), (tbm_bo_handle) NULL);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), (tbm_bo_handle) NULL);
-
- if (!_tbm_bo_lock(bo, device, opt)) {
- _tbm_set_last_result(TBM_BO_ERROR_LOCK_FAILED);
- TBM_LOG_E("error: fail to lock bo:%p)\n", bo);
- _tbm_bufmgr_mutex_unlock();
- return (tbm_bo_handle) NULL;
- }
-
- bo_handle = bufmgr->backend->bo_map(bo, device, opt);
- if (bo_handle.ptr == NULL) {
- /* LCOV_EXCL_START */
- _tbm_set_last_result(TBM_BO_ERROR_MAP_FAILED);
- TBM_LOG_E("error: fail to map bo:%p\n", bo);
- _tbm_bo_unlock(bo);
- _tbm_bufmgr_mutex_unlock();
- return (tbm_bo_handle) NULL;
- /* LCOV_EXCL_STOP */
- }
-
- /* increase the map_count */
- bo->map_cnt++;
-
- TBM_TRACE("bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
-
- _tbm_bufmgr_mutex_unlock();
-
- return bo_handle;
-}
-
-int
-tbm_bo_unmap(tbm_bo bo)
-{
- tbm_bufmgr bufmgr = gBufMgr;
- int ret;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
- ret = bufmgr->backend->bo_unmap(bo);
- if (!ret) {
- /* LCOV_EXCL_START */
- TBM_LOG_E("error: bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
- _tbm_set_last_result(TBM_BO_ERROR_UNMAP_FAILED);
- _tbm_bufmgr_mutex_unlock();
- return ret;
- /* LCOV_EXCL_STOP */
- }
-
- /* decrease the map_count */
- bo->map_cnt--;
-
- TBM_TRACE("bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
-
- _tbm_bo_unlock(bo);
-
- _tbm_bufmgr_mutex_unlock();
-
- return ret;
-}
-
-int
-tbm_bo_swap(tbm_bo bo1, tbm_bo bo2)
-{
- tbm_bufmgr bufmgr = gBufMgr;
- void *temp;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo1), 0);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo2), 0);
-
- TBM_TRACE("before: bo1(%p) bo2(%p)\n", bo1, bo2);
-
- if (bufmgr->backend->bo_size(bo1) != bufmgr->backend->bo_size(bo2)) {
- _tbm_set_last_result(TBM_BO_ERROR_SWAP_FAILED);
- TBM_LOG_E("error: bo1(%p) bo2(%p)\n", bo1, bo2);
- _tbm_bufmgr_mutex_unlock();
- return 0;
- }
-
- TBM_TRACE("after: bo1(%p) bo2(%p)\n", bo1, bo2);
-
- temp = bo1->priv;
- bo1->priv = bo2->priv;
- bo2->priv = temp;
-
- _tbm_bufmgr_mutex_unlock();
-
- return 1;
-}
-
-int
-tbm_bo_locked(tbm_bo bo)
-{
- tbm_bufmgr bufmgr = gBufMgr;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
- if (bufmgr->lock_type == LOCK_TRY_NEVER) {
- TBM_LOG_E("bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
- _tbm_bufmgr_mutex_unlock();
- return 0;
- }
-
- if (bo->lock_cnt > 0) {
- TBM_TRACE("error: bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
- _tbm_bufmgr_mutex_unlock();
- return 1;
- }
-
- TBM_TRACE("bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
- _tbm_bufmgr_mutex_unlock();
-
- return 0;
-}
-
-int
-tbm_bo_add_user_data(tbm_bo bo, unsigned long key,
- tbm_data_free data_free_func)
-{
- tbm_user_data *data;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
- /* check if the data according to the key exist if so, return false. */
- data = user_data_lookup(&bo->user_data_list, key);
- if (data) {
- TBM_TRACE("warning: user data already exist key(%ld)\n", key);
- _tbm_bufmgr_mutex_unlock();
- return 0;
- }
-
- data = user_data_create(key, data_free_func);
- if (!data) {
- TBM_LOG_E("error: bo(%p) key(%lu)\n", bo, key);
- _tbm_bufmgr_mutex_unlock();
- return 0;
- }
-
- TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, data->data);
-
- LIST_ADD(&data->item_link, &bo->user_data_list);
-
- _tbm_bufmgr_mutex_unlock();
-
- return 1;
-}
-
-int
-tbm_bo_set_user_data(tbm_bo bo, unsigned long key, void *data)
-{
- tbm_user_data *old_data;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
- if (LIST_IS_EMPTY(&bo->user_data_list)) {
- TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
- _tbm_bufmgr_mutex_unlock();
- return 0;
- }
-
- old_data = user_data_lookup(&bo->user_data_list, key);
- if (!old_data) {
- TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
- _tbm_bufmgr_mutex_unlock();
- return 0;
- }
-
- if (old_data->data && old_data->free_func)
- old_data->free_func(old_data->data);
- old_data->data = data;
-
- TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
-
- _tbm_bufmgr_mutex_unlock();
-
- return 1;
-}
-
-int
-tbm_bo_get_user_data(tbm_bo bo, unsigned long key, void **data)
-{
- tbm_user_data *old_data;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
- if (!data || LIST_IS_EMPTY(&bo->user_data_list)) {
- TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
- _tbm_bufmgr_mutex_unlock();
- return 0;
- }
-
- old_data = user_data_lookup(&bo->user_data_list, key);
- if (!old_data) {
- TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
- *data = NULL;
- _tbm_bufmgr_mutex_unlock();
- return 0;
- }
-
- *data = old_data->data;
-
- TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
-
- _tbm_bufmgr_mutex_unlock();
-
- return 1;
-}
-
-int
-tbm_bo_delete_user_data(tbm_bo bo, unsigned long key)
-{
- tbm_user_data *old_data;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
- if (LIST_IS_EMPTY(&bo->user_data_list)) {
- TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
- _tbm_bufmgr_mutex_unlock();
- return 0;
- }
-
- old_data = user_data_lookup(&bo->user_data_list, key);
- if (!old_data) {
- TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
- _tbm_bufmgr_mutex_unlock();
- return 0;
- }
-
- TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
-
- user_data_delete(old_data);
-
- _tbm_bufmgr_mutex_unlock();
-
- return 1;
-}
-
-unsigned int
-tbm_bufmgr_get_capability(tbm_bufmgr bufmgr)
-{
- unsigned int capabilities = TBM_BUFMGR_CAPABILITY_NONE;
-
- _tbm_bufmgr_mutex_lock();
-
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), TBM_BUFMGR_CAPABILITY_NONE);
- TBM_BUFMGR_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, TBM_BUFMGR_CAPABILITY_NONE);
-
- TBM_TRACE("tbm_bufmgr(%p) capability(%u)\n", bufmgr, bufmgr->capabilities);
-
- capabilities = bufmgr->capabilities;