- 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;
-
- _tbm_bufmgr_mutex_unlock();
-
- return capabilities;
-}
-
-int
-tbm_bo_get_flags(tbm_bo bo)
-{
- int flags;
-
- _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);
-
- flags = bo->flags;
-
- TBM_TRACE("bo(%p)\n", bo);
-
- _tbm_bufmgr_mutex_unlock();
-
- return flags;
-}
-
-/* LCOV_EXCL_START */
-tbm_error_e
-tbm_get_last_error(void)
-{
- return tbm_last_error;
-}
-
-void
-tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr, char *str, int *len)
-{
- char app_name[255] = {0,}, title[512] = {0,};
- tbm_surface_debug_data *debug_old_data = NULL;
-
- pthread_mutex_lock(&gLock);
-
- if (!TBM_BUFMGR_IS_VALID(bufmgr) || (bufmgr != gBufMgr)) {
- TBM_LOG_E("invalid bufmgr\n");
- pthread_mutex_unlock(&gLock);
- return;
- }
-
- TBM_SNPRINTF(str, len, "\n");
- _tbm_util_get_appname_from_pid(getpid(), app_name);
- _tbm_util_get_appname_brief(app_name);
- TBM_SNPRINTF(str, len, "============TBM DEBUG: %s(%d)===========================\n",
- app_name, getpid());
-
- snprintf(title, 255, "%s", "no surface refcnt width height bpp size n_b n_p flags format app_name ");
-
- if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) {
- LIST_FOR_EACH_ENTRY(debug_old_data, &bufmgr->debug_key_list, item_link) {
- strncat(title, " ", MAX_SIZE_N(title));
- strncat(title, debug_old_data->key, MAX_SIZE_N(title));
- }
- }
-
- TBM_SNPRINTF(str, len, "[tbm_surface information]\n");
- TBM_SNPRINTF(str, len, "%s\n", title);
-
- /* show the tbm_surface information in surf_list */
- if (!LIST_IS_EMPTY(&bufmgr->surf_list)) {
- tbm_surface_h surf = NULL;
- int surf_cnt = 0;
-
- LIST_FOR_EACH_ENTRY(surf, &bufmgr->surf_list, item_link) {
- char data[512] = {0,};
- unsigned int pid;
- int i;
-
- pid = _tbm_surface_internal_get_debug_pid(surf);
- if (!pid) {
- /* if pid is null, set the self_pid */
- pid = getpid();
- }
-
- memset(app_name, 0x0, 255 * sizeof(char));
- _tbm_util_get_appname_from_pid(pid, app_name);
- _tbm_util_get_appname_brief(app_name);
-
- snprintf(data, 255, "%-2d %-9p %-4d %-5u %-6u %-3u %-6u %-2d %-2d %-3d %-8s %-15s",
- ++surf_cnt,
- surf,
- surf->refcnt,
- surf->info.width,
- surf->info.height,
- surf->info.bpp,
- surf->info.size / 1024,
- surf->num_bos,
- surf->num_planes,
- surf->flags,
- _tbm_surface_internal_format_to_str(surf->info.format) + 11,
- app_name);
-
- if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) {
- LIST_FOR_EACH_ENTRY(debug_old_data, &bufmgr->debug_key_list, item_link) {
- char *value;
-
- strncat(data, " ", MAX_SIZE_N(title));
-
- value = _tbm_surface_internal_get_debug_data(surf, debug_old_data->key);
- if (value)
- strncat(data, value, MAX_SIZE_N(title));
- else
- strncat(data, "none", MAX_SIZE_N(title));
- }
- }
- TBM_SNPRINTF(str, len, "%s\n", data);
-
- for (i = 0; i < surf->num_bos; i++) {
- TBM_SNPRINTF(str, len, " bo:%-12p %-26d%-10d\n",
- surf->bos[i],
- surf->bos[i]->ref_cnt,
- bufmgr->backend->bo_size(surf->bos[i]) / 1024);
- }
- }
- } else
- TBM_SNPRINTF(str, len, " no tbm_surfaces.\n");
- TBM_SNPRINTF(str, len, "\n");
-
- TBM_SNPRINTF(str, len, "[tbm_bo information]\n");
- TBM_SNPRINTF(str, len, "no bo refcnt size lock_cnt map_cnt flags surface\n");