X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Ftbm_bufmgr.c;h=a056c03205fd2ff30487953128675a4e5337d198;hb=e565c41f907df4ed8a2b85d304bf11cc3ba0724d;hp=d455e0a9f6688005707335263c0484d3b8850627;hpb=7ef7baaeca30a72e19e8ac20680ddf9336e0c248;p=platform%2Fcore%2Fuifw%2Flibtbm.git diff --git a/src/tbm_bufmgr.c b/src/tbm_bufmgr.c index d455e0a..a056c03 100644 --- a/src/tbm_bufmgr.c +++ b/src/tbm_bufmgr.c @@ -77,6 +77,33 @@ void _tbm_bufmgr_mutex_unlock(void); /* LCOV_EXCL_START */ +static void +_tbm_bufmgr_check_bo_cnt(tbm_bufmgr bufmgr) +{ + static int last_chk_bo_cnt = 0; + + if ((bufmgr->bo_cnt >= 500) && ((bufmgr->bo_cnt % 20) == 0) && + (bufmgr->bo_cnt > last_chk_bo_cnt)) { + TBM_DBG("============TBM BO CNT DEBUG: bo_cnt=%d\n", bufmgr->bo_cnt); + tbm_bufmgr_debug_show(bufmgr); + last_chk_bo_cnt = bufmgr->bo_cnt; + } +} + +static void +_tbm_bufmgr_initialize_bo(tbm_bufmgr bufmgr, tbm_bo bo, int flags) +{ + bo->bufmgr = bufmgr; + bo->flags = flags; + bo->magic = TBM_BO_MAGIC; + bo->ref_cnt = 1; + + LIST_INITHEAD(&bo->user_data_list); + + bufmgr->bo_cnt++; + LIST_ADD(&bo->item_link, &bufmgr->bo_list); +} + void _tbm_bufmgr_mutex_lock(void) { @@ -168,11 +195,48 @@ _tbm_util_get_appname_from_pid(long pid, char *str) snprintf(str, sizeof(cmdline), "%s", cmdline); } +static void +_tbm_bufmgr_copy_module_data(tbm_bufmgr bufmgr, tbm_module *module, int reset) +{ + if (!reset) { + bufmgr->module_data = module->module_data; + bufmgr->backend = module->backend; + + bufmgr->backend_module_data = module->backend_module_data; + bufmgr->bufmgr_data = module->bufmgr_data; + bufmgr->bufmgr_func = module->bufmgr_func; + bufmgr->bo_func = module->bo_func; + + bufmgr->use_hal_tbm = module->use_hal_tbm; + bufmgr->auth_wl_socket_created = module->auth_wl_socket_created; + bufmgr->auth_fd = module->auth_fd; + bufmgr->hal_backend = module->hal_backend; + bufmgr->hal_bufmgr = module->hal_bufmgr; + } else { + bufmgr->module_data = NULL; + bufmgr->backend = NULL; + + bufmgr->backend_module_data = NULL; + bufmgr->bufmgr_data = NULL; + bufmgr->bufmgr_func = NULL; + bufmgr->bo_func = NULL; + + bufmgr->use_hal_tbm = 0; + bufmgr->auth_wl_socket_created = 0; + bufmgr->auth_fd = -1; + bufmgr->hal_backend = NULL; + bufmgr->hal_bufmgr = NULL; + } +} + + /* LCOV_EXCL_STOP */ static tbm_bufmgr _tbm_bufmgr_init(int fd, int server) { + tbm_error_e error = TBM_ERROR_NONE; + #ifdef TBM_BUFMGR_INIT_TIME struct timeval start_tv, end_tv; #endif @@ -233,7 +297,7 @@ _tbm_bufmgr_init(int fd, int server) } /* load bufmgr priv from env */ - gBufMgr->module = tbm_module_load(gBufMgr, gBufMgr->fd); + gBufMgr->module = tbm_module_load(gBufMgr->fd); if (!gBufMgr->module) { TBM_ERR("error : Fail to load bufmgr backend\n"); _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION); @@ -244,20 +308,23 @@ _tbm_bufmgr_init(int fd, int server) } + // TODO: this is temporary. it will be removed after finishing refactoring the tbm_module. + _tbm_bufmgr_copy_module_data(gBufMgr, gBufMgr->module, 0); + /* check the essential capabilities of tbm_module */ - gBufMgr->capabilities = tbm_module_bufmgr_get_capabilities(gBufMgr->module); - if (gBufMgr->capabilities == HAL_TBM_BUFMGR_CAPABILITY_NONE) { + gBufMgr->capabilities = tbm_module_bufmgr_get_capabilities(gBufMgr->module, &error); + if (gBufMgr->capabilities == TBM_BUFMGR_CAPABILITY_NONE) { TBM_ERR("The capabilities of the backend module is TBM_BUFMGR_CAPABILITY_NONE."); TBM_ERR("TBM_BUFMGR_CAPABILITY_SHARE_FD is the essential capability."); tbm_module_unload(gBufMgr->module); - _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION); + _tbm_set_last_result(error); free(gBufMgr); gBufMgr = NULL; pthread_mutex_unlock(&gLock); return NULL; } - if (!(gBufMgr->capabilities & HAL_TBM_BUFMGR_CAPABILITY_SHARE_FD)) { + if (!(gBufMgr->capabilities & TBM_BUFMGR_CAPABILITY_SHARE_FD)) { TBM_ERR("The capabilities of the backend module had no TBM_BUFMGR_CAPABILITY_SHARE_FD."); TBM_ERR("The tbm backend has to get TBM_BUFMGR_CAPABILITY_SHARE_FD. "); tbm_module_unload(gBufMgr->module); @@ -369,6 +436,9 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr) tbm_module_unload(bufmgr->module); + // TODO: this is temporary. it will be removed after finishing refactoring the tbm_module. + _tbm_bufmgr_copy_module_data(bufmgr, bufmgr->module, 1); + if (bufmgr->fd > 0) close(bufmgr->fd); @@ -748,76 +818,24 @@ _tbm_bufmgr_get_bufmgr(void) int tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *native_display) { - int ret; tbm_error_e error; _tbm_bufmgr_mutex_lock(); _tbm_set_last_result(TBM_ERROR_NONE); - TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); - - if (bufmgr->use_hal_tbm) { - if (hal_tbm_backend_has_drm_device(bufmgr->hal_backend, &ret)) { - int fd = tbm_drm_helper_get_fd(); // this must be the auth drm_fd.(master drm_fd); - if (fd < -1) { - TBM_ERR("error: tbm_bufmgr(%p) native_display(%p)\n", bufmgr, native_display); - _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION); - _tbm_bufmgr_mutex_unlock(); - return 0; - } - - // make the wayland server socket for sending the authenticated drm_fd to wayland clients. - if (!tbm_drm_helper_wl_auth_server_init(native_display, fd, NULL, 0)) { - TBM_ERR("error: tbm_drm_helper_wl_auth_server_init failed\n", bufmgr, native_display); - close(fd); - _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION); - _tbm_bufmgr_mutex_unlock(); - return 0; - } - TBM_INFO("tbm creates a wayland socket for authentication of drm_fd."); - - bufmgr->auth_wl_socket_created = 1; - bufmgr->auth_fd = fd; + TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), 0); - // TODO: this duplication will be removed after refactoring tbm_module - bufmgr->module->auth_wl_socket_created = 1; - bufmgr->module->bufmgr->auth_fd = fd; - } - } else if (bufmgr->backend_module_data) { - if (!bufmgr->bufmgr_func->bufmgr_bind_native_display) { - TBM_WRN("skip: tbm_bufmgr(%p) native_display(%p)\n", - bufmgr, native_display); - _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED); - _tbm_bufmgr_mutex_unlock(); - return 1; - } + error = tbm_module_bufmgr_bind_native_display(bufmgr->module, native_display); + if (error != TBM_ERROR_NONE) { + _tbm_set_last_result(error); + _tbm_bufmgr_mutex_unlock(); - error = bufmgr->bufmgr_func->bufmgr_bind_native_display(bufmgr->bufmgr_data, (tbm_native_display *)native_display); - if (error != TBM_ERROR_NONE) { - TBM_ERR("error: tbm_bufmgr(%p) native_display(%p) error(%d)\n", - bufmgr, native_display, error); - _tbm_set_last_result(error); - _tbm_bufmgr_mutex_unlock(); - return 0; - } - ret = 1; - } else { - if (!bufmgr->backend->bufmgr_bind_native_display) { - TBM_WRN("skip: tbm_bufmgr(%p) native_display(%p)\n", - bufmgr, native_display); - _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED); - _tbm_bufmgr_mutex_unlock(); + if (error == TBM_ERROR_NOT_SUPPORTED) { + TBM_WRN("Not supported, so skip: tbm_bufmgr(%p) native_display(%p)", bufmgr, native_display); return 1; } - ret = bufmgr->backend->bufmgr_bind_native_display(bufmgr, native_display); - if (!ret) { - TBM_ERR("error: tbm_bufmgr(%p) native_display(%p)\n", - bufmgr, native_display); - _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION); - _tbm_bufmgr_mutex_unlock(); - return 0; - } + return 0; } TBM_INFO("tbm_bufmgr(%p) native_display(%p)\n", bufmgr, native_display); @@ -872,4 +890,271 @@ tbm_bufmgr tbm_bufmgr_get(void) { return gBufMgr; } + + +tbm_bo +tbm_bufmgr_internal_find_bo(tbm_bufmgr bufmgr, tbm_bo bo) +{ + tbm_bo bo2 = NULL; + + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), NULL); + TBM_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, NULL); + + if (LIST_IS_EMPTY(&bufmgr->bo_list)) + return NULL; + + LIST_FOR_EACH_ENTRY(bo2, &bufmgr->bo_list, item_link) { + if (bo2->bo_data == bo->bo_data) { + return bo2; + } + } + + return NULL; +} + +tbm_bo +tbm_bufmgr_internal_alloc_bo(tbm_bufmgr bufmgr, int size, int flags, tbm_error_e *error) +{ + tbm_bo bo; + tbm_backend_bo_data *bo_data; + + _tbm_bufmgr_check_bo_cnt(bufmgr); + + bo = calloc(1, sizeof(struct _tbm_bo)); + if (!bo) { + /* LCOV_EXCL_START */ + TBM_ERR("memory allocationc failed."); + *error = TBM_ERROR_OUT_OF_MEMORY; + return NULL; + /* LCOV_EXCL_STOP */ + } + + bo_data = tbm_module_bufmgr_bo_alloc(bufmgr->module, bo, size, flags, error); + if (!bo_data) { + /* LCOV_EXCL_START */ + TBM_ERR("tbm_module_bufmgr_bo_alloc failed. size:%d flags:%s error:%d", size, _tbm_flag_to_str(flags), *error); + free(bo); + return NULL; + /* LCOV_EXCL_STOP */ + } + bo->bo_data = bo_data; + bo->priv = (void *)bo_data; // TODO: this will be DEPRECATED. + + _tbm_bufmgr_initialize_bo(bufmgr, bo, flags); + + return bo; +} + + +/* LCOV_EXCL_START */ + +tbm_bo +tbm_bufmgr_internal_alloc_bo_with_format(tbm_bufmgr bufmgr, int format, int bo_idx, int width, + int height, int bpp, tbm_bo_memory_type flags, tbm_error_e *error) +{ + tbm_bo bo = NULL; + + _tbm_bufmgr_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); + + TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL); + + _tbm_bufmgr_check_bo_cnt(bufmgr); + + bo = calloc(1, sizeof(struct _tbm_bo)); + if (!bo) { + /* LCOV_EXCL_START */ + TBM_ERR("memory allocationc failed."); + _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY); + /* LCOV_EXCL_STOP */ + goto fail; + } + + bo->bo_data = tbm_module_bufmgr_bo_alloc_with_format(bufmgr->module, format, bo_idx, width, height, bpp, flags, error); + if (!bo->bo_data) { + /* LCOV_EXCL_START */ + _tbm_set_last_result(*error); + /* LCOV_EXCL_STOP */ + goto fail; + } + + _tbm_bufmgr_initialize_bo(bufmgr, bo, flags); + + _tbm_bufmgr_mutex_unlock(); + + return bo; + +fail: + if (bo) + free(bo); + _tbm_bufmgr_mutex_unlock(); + + return NULL; +} + +tbm_bo +tbm_bufmgr_internal_alloc_bo_with_bo_data(tbm_bufmgr bufmgr, tbm_backend_bo_data *bo_data, int flags) +{ + tbm_bo bo, bo2 = NULL; + + _tbm_bufmgr_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); + + TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL); + TBM_BUFMGR_RETURN_VAL_IF_FAIL(bo_data, NULL); + + _tbm_bufmgr_check_bo_cnt(bufmgr); + + bo = calloc(1, sizeof(struct _tbm_bo)); + if (!bo) { + /* LCOV_EXCL_START */ + TBM_ERR("memory allocationc failed."); + _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY); + _tbm_bufmgr_mutex_unlock(); + return NULL; + /* LCOV_EXCL_STOP */ + } + bo->bo_data = bo_data; + bo->get_from_hal_surface = 1; + + // return an existed bo if the bo is already created with the same bo_data. + bo2 = tbm_bufmgr_internal_find_bo(bufmgr, bo); + if (bo2) { + bo2->ref_cnt++; + free(bo); + _tbm_bufmgr_mutex_unlock(); + return bo2; + } + + _tbm_bufmgr_initialize_bo(bufmgr, bo, flags); + + TBM_TRACE_BO("bo(%p) refcnt(%d), flag(%s)", bo, bo->ref_cnt, _tbm_flag_to_str(bo->flags)); + + _tbm_bufmgr_mutex_unlock(); + + return bo; +} + +tbm_bo +tbm_bufmgr_internal_import_bo_with_key(tbm_bufmgr bufmgr, unsigned int key, tbm_error_e *error) +{ + tbm_bo bo, bo2; + int flags; + + _tbm_bufmgr_check_bo_cnt(bufmgr); + + bo = calloc(1, sizeof(struct _tbm_bo)); + if (!bo) { + /* LCOV_EXCL_START */ + TBM_ERR("memory allocationc failed."); + *error = TBM_ERROR_OUT_OF_MEMORY; + return NULL; + /* LCOV_EXCL_STOP */ + } + + bo->bo_data = tbm_module_bufmgr_bo_import_key(bufmgr->module, bo, key, error); + if (!bo->bo_data) { + /* LCOV_EXCL_START */ + TBM_ERR("tbm_module_bufmgr_bo_import_key failed. tbm_key:%d", key); + free(bo); + return NULL; + /* LCOV_EXCL_STOP */ + } + + // return the existed bo2 if bo->bo_data and bo2->bo_data is the same + bo2 = tbm_bufmgr_internal_find_bo(bufmgr, bo); + if (bo2) { + TBM_TRACE_BO("find bo(%p) ref(%d) key(%d) flag(%s) in list", + bo2, bo2->ref_cnt, key, _tbm_flag_to_str(bo2->flags)); + bo2->ref_cnt++; + free(bo); + return bo2; + } + + // TODO: refactoring tbm_module_bo + if (bufmgr->use_hal_tbm) { + flags = (tbm_bo_memory_type)hal_tbm_bo_get_memory_types((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)error); + if (*error != TBM_ERROR_NONE) { + TBM_ERR("fail to get the bo flags(memory_types)"); + flags = TBM_BO_DEFAULT; + } + } else if (bufmgr->backend_module_data) { + flags = bufmgr->bo_func->bo_get_memory_types(bo->bo_data, error); + if (error != TBM_ERROR_NONE) { + TBM_ERR("fail to get the bo flags(memory_types)"); + flags = TBM_BO_DEFAULT; + } + } else { + if (bufmgr->backend->bo_get_flags) + flags = bufmgr->backend->bo_get_flags(bo); + else + flags = TBM_BO_DEFAULT; + } + + _tbm_bufmgr_initialize_bo(bufmgr, bo, flags); + + return bo; +} + +tbm_bo +tbm_bufmgr_internal_import_bo_with_fd(tbm_bufmgr bufmgr, tbm_fd fd, tbm_error_e *error) +{ + tbm_bo bo, bo2; + int flags; + + _tbm_bufmgr_check_bo_cnt(bufmgr); + + bo = calloc(1, sizeof(struct _tbm_bo)); + if (!bo) { + /* LCOV_EXCL_START */ + TBM_ERR("memory allocationc failed."); + *error = TBM_ERROR_OUT_OF_MEMORY; + return NULL; + /* LCOV_EXCL_STOP */ + } + + bo->bo_data = tbm_module_bufmgr_bo_import_fd(bufmgr->module, bo, fd, error); + if (!bo->bo_data) { + /* LCOV_EXCL_START */ + TBM_ERR("tbm_module_bufmgr_bo_import_fd failed. tbm_fd:%d", fd); + free(bo); + return NULL; + /* LCOV_EXCL_STOP */ + } + + // return the existed bo2 if bo->bo_data and bo2->bo_data is the same + bo2 = tbm_bufmgr_internal_find_bo(bufmgr, bo); + if (bo2) { + TBM_TRACE_BO("find bo(%p) ref(%d) fd(%d) flag(%s) in list", + bo2, bo2->ref_cnt, fd, _tbm_flag_to_str(bo2->flags)); + bo2->ref_cnt++; + free(bo); + return bo2; + } + + // TODO: refactoring tbm_module_bo + if (bufmgr->use_hal_tbm) { + flags = (tbm_bo_memory_type)hal_tbm_bo_get_memory_types((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)error); + if (error != TBM_ERROR_NONE) { + TBM_ERR("fail to get the bo flags(memory_types)"); + flags = TBM_BO_DEFAULT; + } + } else if (bufmgr->backend_module_data) { + flags = bufmgr->bo_func->bo_get_memory_types(bo->bo_data, error); + if (error != TBM_ERROR_NONE) { + TBM_ERR("fail to get the bo flags(memory_types)"); + flags = TBM_BO_DEFAULT; + } + } else { + if (bufmgr->backend->bo_get_flags) + flags = bufmgr->backend->bo_get_flags(bo); + else + flags = TBM_BO_DEFAULT; + } + + _tbm_bufmgr_initialize_bo(bufmgr, bo, flags); + + return bo; +} + /* LCOV_EXCL_STOP */