X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Ftbm_bufmgr.c;h=55f9243bb17357433d0d0c61c2f9c78cb382a46f;hb=238437409d604e27c1dd8162578e4c4a9fa5acae;hp=38ec94315d8764a5da659ea06b225351b9636c78;hpb=c06452c54dade3ea445b9a4feffc05a404dbd786;p=platform%2Fcore%2Fuifw%2Flibtbm.git diff --git a/src/tbm_bufmgr.c b/src/tbm_bufmgr.c index 38ec943..55f9243 100644 --- a/src/tbm_bufmgr.c +++ b/src/tbm_bufmgr.c @@ -33,7 +33,6 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include "tbm_bufmgr.h" #include "tbm_bufmgr_int.h" -#include "tbm_bufmgr_backend.h" #include "tbm_drm_helper.h" #include "list.h" @@ -76,8 +75,44 @@ void _tbm_bufmgr_mutex_unlock(void); } \ } +#define TBM_BUFMGR_RETURN_VAL_SET_ERR_IF_FAIL(cond, val, error, error_type) {\ + if (!(cond)) {\ + TBM_ERR("'%s' failed.\n", #cond);\ + error = error_type;\ + _tbm_bufmgr_mutex_unlock();\ + return val;\ + } \ +} + /* 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) { @@ -174,6 +209,8 @@ _tbm_util_get_appname_from_pid(long pid, char *str) 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 @@ -234,7 +271,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); @@ -246,19 +283,19 @@ _tbm_bufmgr_init(int fd, int server) } /* 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_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); @@ -327,22 +364,22 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr) { TBM_RETURN_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr)); - _tbm_bufmgr_mutex_lock(); pthread_mutex_lock(&gLock); + _tbm_bufmgr_mutex_lock(); _tbm_set_last_result(TBM_ERROR_NONE); if (!gBufMgr) { TBM_ERR("gBufmgr already destroy: bufmgr:%p\n", bufmgr); - pthread_mutex_unlock(&gLock); _tbm_bufmgr_mutex_unlock(); + pthread_mutex_unlock(&gLock); return; } bufmgr->ref_count--; if (bufmgr->ref_count > 0) { TBM_DBG("reduce a ref_count(%d) of tbm_bufmgr(%p)\n", bufmgr->ref_count, bufmgr); - pthread_mutex_unlock(&gLock); _tbm_bufmgr_mutex_unlock(); + pthread_mutex_unlock(&gLock); return; } @@ -378,8 +415,8 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr) free(bufmgr); gBufMgr = NULL; - pthread_mutex_unlock(&gLock); _tbm_bufmgr_mutex_unlock(); + pthread_mutex_unlock(&gLock); } unsigned int @@ -504,16 +541,9 @@ tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr) TBM_SNRPRINTF(str, len, c, "%s\n", data); for (i = 0; i < surf->num_bos; i++) { - if (bufmgr->use_hal_tbm) { - size = hal_tbm_bo_get_size((hal_tbm_bo *)surf->bos[i]->bo_data, (hal_tbm_error *)&error); - if (error != TBM_ERROR_NONE) - TBM_WRN("fail to get the size of bo."); - } else if (bufmgr->backend_module_data) { - size = bufmgr->bo_func->bo_get_size(surf->bos[i]->bo_data, &error); - if (error != TBM_ERROR_NONE) - TBM_WRN("fail to get the size of bo."); - } else - size = bufmgr->backend->bo_size(surf->bos[i]); + size = tbm_bo_data_get_size(surf->bos[i]->bo_data, &error); + if (error != TBM_ERROR_NONE) + TBM_WRN("fail to get the size of bo."); TBM_SNRPRINTF(str, len, c, " bo:%-12p %-26d%-10d\n", surf->bos[i], surf->bos[i]->ref_cnt, @@ -534,22 +564,12 @@ tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr) tbm_key key = 0; LIST_FOR_EACH_ENTRY(bo, &bufmgr->bo_list, item_link) { - if (bo->bufmgr->use_hal_tbm) { - size = hal_tbm_bo_get_size((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)&error); - if (error != TBM_ERROR_NONE) - TBM_WRN("fail to get the size of bo."); - key = (tbm_key)hal_tbm_bo_export_key((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)&error); - } else if (bufmgr->backend_module_data) { - size = bufmgr->bo_func->bo_get_size(bo->bo_data, &error); - if (error != TBM_ERROR_NONE) - TBM_WRN("fail to get the size of bo."); - key = bufmgr->bo_func->bo_export_key(bo->bo_data, &error); - if (error != TBM_ERROR_NONE) - TBM_WRN("fail to get the tdm_key of bo."); - } else { - size = bufmgr->backend->bo_size(bo); - key = bufmgr->backend->bo_export(bo); - } + size = tbm_bo_data_get_size(bo->bo_data, &error); + if (error != TBM_ERROR_NONE) + TBM_WRN("fail to get the size of bo."); + key = tbm_bo_data_export_key(bo->bo_data, &error); + if (error != TBM_ERROR_NONE) + TBM_WRN("fail to get the tdm_key of bo."); TBM_SNRPRINTF(str, len, c, "%-3d %-11p %-5d %-7d %-6d %-5u %-7d %-11p %-4d\n", ++bo_cnt, bo, @@ -749,76 +769,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); + TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), 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; - - // 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_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); @@ -873,4 +841,298 @@ 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 (tbm_module_compare_bo_data(bufmgr->module, 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_ERROR_NONE; + 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(size > 0, NULL); + + _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; + goto failed; + /* LCOV_EXCL_STOP */ + } + + bo->bo_data = tbm_module_alloc_bo_data(bufmgr->module, bo, size, flags, &error); + if (!bo->bo_data) { + /* LCOV_EXCL_START */ + TBM_ERR("tbm_module_alloc_bo_data failed. size:%d flags:%s error:%d", size, _tbm_flag_to_str(flags), error); + free(bo); + _tbm_set_last_result(error); + _tbm_bufmgr_mutex_unlock(); + goto failed; + /* LCOV_EXCL_STOP */ + } + + _tbm_bufmgr_initialize_bo(bufmgr, bo, flags); + + TBM_TRACE_BO("bo(%p) size(%d) refcnt(%d), flag(%s)", bo, size, bo->ref_cnt, _tbm_flag_to_str(bo->flags)); + + _tbm_set_last_result(TBM_ERROR_NONE); + _tbm_bufmgr_mutex_unlock(); + + return bo; + +/* LCOV_EXCL_START */ +failed: + _tbm_set_last_result(error); + _tbm_bufmgr_mutex_unlock(); + + return NULL; +/* LCOV_EXCL_STOP */ +} + + +/* 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_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."); + *error = TBM_ERROR_OUT_OF_MEMORY; + goto failed; + /* LCOV_EXCL_STOP */ + } + + bo->bo_data = tbm_module_alloc_bo_data_with_format(bufmgr->module, format, bo_idx, width, height, bpp, flags, error); + if (!bo->bo_data) { + /* LCOV_EXCL_START */ + free(bo); + goto failed; + /* LCOV_EXCL_STOP */ + } + + _tbm_bufmgr_initialize_bo(bufmgr, bo, flags); + + _tbm_bufmgr_mutex_unlock(); + + return bo; + +/* LCOV_EXCL_START */ +failed: + _tbm_bufmgr_mutex_unlock(); + + return NULL; +/* LCOV_EXCL_STOP */ +} + +tbm_bo +tbm_bufmgr_internal_alloc_bo_with_bo_data(tbm_bufmgr bufmgr, tbm_bo_data *bo_data, int flags, tbm_error_e *error) +{ + tbm_bo bo, bo2 = NULL; + + _tbm_bufmgr_mutex_lock(); + + *error = TBM_ERROR_NONE; + + TBM_BUFMGR_RETURN_VAL_SET_ERR_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL, *error, TBM_ERROR_INVALID_PARAMETER); + TBM_BUFMGR_RETURN_VAL_SET_ERR_IF_FAIL(bo_data, NULL, *error, TBM_ERROR_INVALID_PARAMETER); + + _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; + _tbm_bufmgr_mutex_unlock(); + return NULL; + /* LCOV_EXCL_STOP */ + } + bo->bo_data = bo_data; + bo->get_from_surface_data = 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_ERROR_NONE; + tbm_bo bo, bo2; + int flags; + + _tbm_bufmgr_mutex_lock(); + + 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."); + goto failed; + /* LCOV_EXCL_STOP */ + } + + bo->bo_data = tbm_module_import_bo_data_with_key(bufmgr->module, bo, key, &error); + if (!bo->bo_data) { + /* LCOV_EXCL_START */ + TBM_ERR("tbm_module_import_bo_data_with_key failed. tbm_key:%d", key); + free(bo); + goto failed; + /* 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); + _tbm_set_last_result(TBM_ERROR_NONE); + _tbm_bufmgr_mutex_unlock(); + return bo2; + } + + flags = tbm_bo_data_get_memory_types(bo->bo_data, &error); + if (error != TBM_ERROR_NONE) { + TBM_WRN("tbm_bo_data_get_memory_types filed. use the default flags:TBM_BO_DEFAULT."); + flags = TBM_BO_DEFAULT; + error = TBM_ERROR_NONE; + } + + _tbm_bufmgr_initialize_bo(bufmgr, bo, flags); + + TBM_TRACE_BO("import new bo(%p) ref(%d) key(%d) flag(%s) in list", + bo, bo->ref_cnt, key, _tbm_flag_to_str(bo->flags)); + + _tbm_set_last_result(TBM_ERROR_NONE); + _tbm_bufmgr_mutex_unlock(); + + return bo; + +failed: + _tbm_set_last_result(error); + _tbm_bufmgr_mutex_unlock(); + + return NULL; +} + +tbm_bo +tbm_bufmgr_internal_import_bo_with_fd(tbm_bufmgr bufmgr, tbm_fd fd) +{ + tbm_error_e error = TBM_ERROR_NONE; + tbm_bo bo, bo2; + int flags; + + _tbm_bufmgr_mutex_lock(); + + 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."); + goto failed; + /* LCOV_EXCL_STOP */ + } + + bo->bo_data = tbm_module_import_bo_data_with_fd(bufmgr->module, bo, fd, &error); + if (!bo->bo_data) { + /* LCOV_EXCL_START */ + TBM_ERR("tbm_module_import_bo_data_with_fd failed. tbm_fd:%d", fd); + free(bo); + goto failed; + /* 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); + _tbm_set_last_result(TBM_ERROR_NONE); + _tbm_bufmgr_mutex_unlock(); + return bo2; + } + + flags = tbm_bo_data_get_memory_types(bo->bo_data, &error); + if (error != TBM_ERROR_NONE) { + TBM_WRN("tbm_bo_data_get_memory_types filed. use the default flags:TBM_BO_DEFAULT."); + flags = TBM_BO_DEFAULT; + error = TBM_ERROR_NONE; + } + + TBM_TRACE_BO("import bo(%p) ref(%d) fd(%d) flag(%s)", + bo, bo->ref_cnt, fd, _tbm_flag_to_str(bo->flags)); + + _tbm_bufmgr_initialize_bo(bufmgr, bo, flags); + + _tbm_set_last_result(TBM_ERROR_NONE); + _tbm_bufmgr_mutex_unlock(); + + return bo; + +failed: + _tbm_set_last_result(error); + _tbm_bufmgr_mutex_unlock(); + + return NULL; +} + /* LCOV_EXCL_STOP */