From 76243497f7a8481903acfcbe0f7204a4d568983e Mon Sep 17 00:00:00 2001 From: SooChan Lim Date: Wed, 23 Jun 2021 11:54:10 +0900 Subject: [PATCH] tbm_surface_internal: refactor the creation of a surface Reduce the duplicated codes and Be easy to handle an error at the time of the surface creation. Change-Id: I0327d332d8c7ec1a4b0919f562826a7ff5476b83 --- src/tbm_surface_internal.c | 416 ++++++++++++++++++--------------------------- 1 file changed, 167 insertions(+), 249 deletions(-) diff --git a/src/tbm_surface_internal.c b/src/tbm_surface_internal.c index 053bf1a..5d39a0e 100644 --- a/src/tbm_surface_internal.c +++ b/src/tbm_surface_internal.c @@ -594,21 +594,29 @@ _tbm_surface_internal_get_bpp(tbm_format format) return bpp; } +static void +_tbm_surface_internal_free(struct _tbm_surface *surf) +{ + if (!surf) + return; + + free(surf); +} + static struct _tbm_surface * -_tbm_surface_internal_create_surface(tbm_bufmgr bufmgr, int width, int height, int format, int flags, tbm_error_e *error) +_tbm_surface_internal_alloc(tbm_bufmgr bufmgr, int width, int height, int format, tbm_error_e *error) { struct _tbm_surface *surf = NULL; - uint32_t size = 0, offset = 0, stride = 0, bo_size = 0; - int i, j, bo_idx; surf = calloc(1, sizeof(struct _tbm_surface)); if (!surf) { /* LCOV_EXCL_START */ - TBM_ERR("fail to alloc surf\n"); + TBM_ERR("memory allocation failed."); *error = TBM_ERROR_OUT_OF_MEMORY; - goto alloc_surf_fail; + return NULL; /* LCOV_EXCL_STOP */ } + surf->refcnt = 1; surf->magic = TBM_SURFACE_MAGIC; surf->bufmgr = bufmgr; @@ -617,301 +625,219 @@ _tbm_surface_internal_create_surface(tbm_bufmgr bufmgr, int width, int height, i surf->info.format = format; surf->info.bpp = _tbm_surface_internal_get_bpp(format); if (!surf->info.bpp) { - TBM_ERR("fail to get bpp from format(%d), error(%s)\n", format, tbm_error_str(*error)); + /* LCOV_EXCL_START */ + TBM_ERR("_tbm_surface_internal_get_bpp failed. format:%d error:%s", format, tbm_error_str(*error)); *error = tbm_get_last_error(); - goto bpp_fail; + free(surf); + return NULL; + /* LCOV_EXCL_STOP */ } + // get number of planes surf->info.num_planes = _tbm_surface_internal_get_num_planes(format); if (!surf->info.num_planes) { - TBM_ERR("fail to get num_planes from format(%d), error(%s)\n", format, tbm_error_str(*error)); + /* LCOV_EXCL_START */ + TBM_ERR("_tbm_surface_internal_get_num_planes failed. format:%d error:%s", format, tbm_error_str(*error)); *error = tbm_get_last_error(); - goto num_planes_fail; + free(surf); + return NULL; + /* LCOV_EXCL_STOP */ } - surf->refcnt = 1; - /* get size, stride and offset bo_idx */ - for (i = 0; i < surf->info.num_planes; i++) { - if (!_tbm_surface_internal_query_plane_data(surf, i, &size, &offset, &stride, &bo_idx)) { - TBM_ERR("fail to query plane data\n"); - *error = tbm_get_last_error(); - goto query_plane_data_fail; + return surf; +} + +static tbm_error_e +_tbm_surface_internal_set_data(struct _tbm_surface *surf, int flags, int is_surface_data) +{ + tbm_error_e error; + uint32_t size = 0, offset = 0, stride = 0, bo_size = 0;; + int i, j, bo_idx; + tbm_bo_data **bo_data_array = NULL; + int num_bos = 0; + int memory_types; + + // set data with surface + if (is_surface_data) { + // set infomation of planes + for (i = 0; i < surf->info.num_planes; i++) { + error = tbm_surface_data_get_plane_data(surf->surface_data, i, &size, &offset, &stride, &bo_idx); + if (error != TBM_ERROR_NONE) { + TBM_ERR("tbm_surface_data_get_plane_data failed. error:%s", tbm_error_str(error)); + return error; + } + + surf->info.planes[i].size = size; + surf->info.planes[i].offset = offset; + surf->info.planes[i].stride = stride; + surf->planes_bo_idx[i] = bo_idx; } - surf->info.planes[i].size = size; - surf->info.planes[i].offset = offset; - surf->info.planes[i].stride = stride; - surf->planes_bo_idx[i] = bo_idx; - } + // get the bo_data_array + bo_data_array = tbm_surface_data_get_bo_data_array(surf->surface_data, &num_bos, &error); + if (!bo_data_array) { + TBM_ERR("tbm_surface_data_get_bo_data_array failed. error:%s", tbm_error_str(error)); + return error; + } + surf->num_bos = num_bos; - surf->num_bos = 1; + // calculate the size of a surface + for (i = 0; i < surf->info.num_planes; i++) + surf->info.size += surf->info.planes[i].size; - for (i = 0; i < surf->info.num_planes; i++) { - surf->info.size += surf->info.planes[i].size; + // get memory_types(bo flags) + memory_types = tbm_bo_data_get_memory_types(bo_data_array[0], &error); + if (error != TBM_ERROR_NONE) { + TBM_ERR("tbm_bo_data_get_memory_types failed. error:%s", tbm_error_str(error)); + return error; + } + // set flags + surf->flags = memory_types; - if (surf->num_bos < surf->planes_bo_idx[i] + 1) - surf->num_bos = surf->planes_bo_idx[i] + 1; - } + // allocate the array of tbm_bos + for (i = 0; i < num_bos; i++) { + surf->bos[i] = tbm_bufmgr_internal_alloc_bo_with_bo_data(surf->bufmgr, bo_data_array[i], memory_types, &error); + if (!surf->bos[i]) { + TBM_ERR("tbm_bufmgr_internal_alloc_bo_with_bo_data failed. error:%s idx:%d", tbm_error_str(error), i); + goto failed; + } - surf->flags = flags; + _tbm_bo_set_surface(surf->bos[i], surf); + } + } else { + // set infomation of planes + for (i = 0; i < surf->info.num_planes; i++) { + if (!_tbm_surface_internal_query_plane_data(surf, i, &size, &offset, &stride, &bo_idx)) { + TBM_ERR("_tbm_surface_internal_query_plane_data failed."); + error = tbm_get_last_error(); + return error; + } + + surf->info.planes[i].size = size; + surf->info.planes[i].offset = offset; + surf->info.planes[i].stride = stride; + surf->planes_bo_idx[i] = bo_idx; + } + + // calculate the number of bos + surf->num_bos = 1; + for (i = 0; i < surf->info.num_planes; i++) { + surf->info.size += surf->info.planes[i].size; - for (i = 0; i < surf->num_bos; i++) { - bo_size = 0; - for (j = 0; j < surf->info.num_planes; j++) { - if (surf->planes_bo_idx[j] == i) - bo_size += surf->info.planes[j].size; + if (surf->num_bos < surf->planes_bo_idx[i] + 1) + surf->num_bos = surf->planes_bo_idx[i] + 1; } - surf->bos[i] = tbm_bufmgr_internal_alloc_bo_with_format(bufmgr, format, i, width, height, surf->info.bpp/8, flags, error); - if (!surf->bos[i]) { - surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags); + // set flags + surf->flags = flags; + + // allocate the array of tbm_bos + for (i = 0; i < surf->num_bos; i++) { + bo_size = 0; + for (j = 0; j < surf->info.num_planes; j++) { + if (surf->planes_bo_idx[j] == i) + bo_size += surf->info.planes[j].size; + } + + surf->bos[i] = tbm_bufmgr_internal_alloc_bo_with_format(surf->bufmgr, + surf->info.format, i, surf->info.width, surf->info.height, + surf->info.bpp/8, flags, &error); if (!surf->bos[i]) { - TBM_ERR("fail to alloc bo idx:%d\n", i); - *error = tbm_get_last_error(); - goto alloc_bo_fail; + surf->bos[i] = tbm_bo_alloc(surf->bufmgr, bo_size, flags); + if (!surf->bos[i]) { + TBM_ERR("tbm_bo_alloc failed. idx:%d", i); + error = tbm_get_last_error(); + goto failed; + } } - } - _tbm_bo_set_surface(surf->bos[i], surf); + _tbm_bo_set_surface(surf->bos[i], surf); + } } - *error = TBM_ERROR_NONE; + return TBM_ERROR_NONE; - return surf; - -alloc_bo_fail: +failed: for (j = 0; j < i; j++) { if (surf->bos[j]) tbm_bo_unref(surf->bos[j]); } -query_plane_data_fail: -bpp_fail: -num_planes_fail: - free(surf); -alloc_surf_fail: - return NULL; + return error; } static struct _tbm_surface * -_tbm_surface_internal_create_surface_data(tbm_bufmgr bufmgr, int width, int height, int format, int flags, tbm_error_e *error) +_tbm_surface_internal_create_surface(tbm_bufmgr bufmgr, int width, int height, int format, int flags, tbm_error_e *error) { - struct _tbm_surface *surf = NULL; - uint32_t size = 0, offset = 0, stride = 0; - int i, j, bo_idx; - tbm_bo_data **bo_data_array = NULL; - int num_bos = 0; + struct _tbm_surface *surf; + int is_surface_data = 0; - surf = calloc(1, sizeof(struct _tbm_surface)); + surf = _tbm_surface_internal_alloc(bufmgr, width, height, format, error); if (!surf) { /* LCOV_EXCL_START */ - TBM_ERR("fail to alloc surf"); - *error = TBM_ERROR_OUT_OF_MEMORY; - goto alloc_surf_fail; + TBM_ERR("_tbm_surface_internal_alloc failed."); + return NULL; /* LCOV_EXCL_STOP */ } - surf->refcnt = 1; - - surf->magic = TBM_SURFACE_MAGIC; - surf->bufmgr = bufmgr; - surf->info.width = width; - surf->info.height = height; - surf->info.format = format; - surf->info.bpp = _tbm_surface_internal_get_bpp(format); - if (!surf->info.bpp) { - TBM_ERR("fail to get bpp from format(%d), error(%s)", format, tbm_error_str(*error)); - *error = tbm_get_last_error(); - goto bpp_fail; - } - surf->flags = flags; - - // get number of planes - surf->info.num_planes = _tbm_surface_internal_get_num_planes(format); - if (!surf->info.num_planes) { - TBM_ERR("fail to get num_planes from format(%d), error(%s)", format, tbm_error_str(*error)); - *error = tbm_get_last_error(); - goto num_planes_fail; - } - surf->surface_data = tbm_module_alloc_surface_data(bufmgr->module, width, height, format, flags, error); - if (!surf->surface_data) { - TBM_ERR("tbm_module_alloc_surface_data failed. error:%s", tbm_error_str(*error)); - goto alloc_surface_data_fail; - } - - // set infomation of planes - for (i = 0; i < surf->info.num_planes; i++) { - *error = tbm_surface_data_get_plane_data(surf->surface_data, i, &size, &offset, &stride, &bo_idx); - if (*error != TBM_ERROR_NONE) { - goto query_plane_data_fail; + is_surface_data = tbm_module_support_surface_data(bufmgr->module); + if (is_surface_data) { + // alloc surface_data + surf->surface_data = tbm_module_alloc_surface_data(bufmgr->module, width, height, format, flags, error); + if (!surf->surface_data) { + TBM_ERR("tbm_module_alloc_surface_data failed. width:%d height:%d format:%d error:%s", + width, height, format, tbm_error_str(*error)); + return NULL; } - surf->info.planes[i].size = size; - surf->info.planes[i].offset = offset; - surf->info.planes[i].stride = stride; - surf->planes_bo_idx[i] = bo_idx; - } - - // set infomation of bos - bo_data_array = tbm_surface_data_get_bo_data_array(surf->surface_data, &num_bos, error); - if (!bo_data_array) { - TBM_ERR("tbm_surface_data_get_bo_data_array failed. error:%s", tbm_error_str(*error)); - goto get_bos_fail; } - surf->num_bos = num_bos; - for (i = 0; i < surf->info.num_planes; i++) { - surf->info.size += surf->info.planes[i].size; - - if (surf->num_bos < surf->planes_bo_idx[i] + 1) - surf->num_bos = surf->planes_bo_idx[i] + 1; - } - - for (i = 0; i < num_bos; i++) { - surf->bos[i] = tbm_bufmgr_internal_alloc_bo_with_bo_data(bufmgr, bo_data_array[i], flags, error); - if (!surf->bos[i]) { - TBM_ERR("fail to alloc bo idx:%d", i); - goto get_bo_fail; - } - - _tbm_bo_set_surface(surf->bos[i], surf); + // set the surface data + *error = _tbm_surface_internal_set_data(surf, flags, is_surface_data); + if (*error != TBM_ERROR_NONE) { + tbm_surface_data_free(surf->surface_data); + surf->surface_data = NULL; + _tbm_surface_internal_free(surf); + return NULL; } - *error = TBM_ERROR_NONE; - return surf; - -get_bo_fail: - for (j = 0; j < i; j++) { - if (surf->bos[j]) - tbm_bo_unref(surf->bos[j]); - } -get_bos_fail: -query_plane_data_fail: - if (surf->surface_data) - tbm_surface_data_free(surf->surface_data); -alloc_surface_data_fail: -num_planes_fail: -bpp_fail: - if (surf) - free(surf); -alloc_surf_fail: - - return NULL; } static struct _tbm_surface * -_tbm_surface_internal_import_surface_data(tbm_bufmgr bufmgr, int width, int height, int format, tbm_surface_buffer_data *buffer_data, tbm_error_e *error) +_tbm_surface_internal_import_surface(tbm_bufmgr bufmgr, int width, int height, int format, tbm_surface_buffer_data *buffer_data, tbm_error_e *error) { struct _tbm_surface *surf = NULL; - uint32_t size = 0, offset = 0, stride = 0; - int i, j, bo_idx; - tbm_bo_data **bo_data_array = NULL; - int num_bos = 0; - int flags; - surf = calloc(1, sizeof(struct _tbm_surface)); + surf = _tbm_surface_internal_alloc(bufmgr, width, height, format, error); if (!surf) { /* LCOV_EXCL_START */ - TBM_ERR("fail to alloc surf"); - *error = TBM_ERROR_OUT_OF_MEMORY; - goto alloc_surf_fail; + TBM_ERR("_tbm_surface_internal_alloc failed."); + return NULL; /* LCOV_EXCL_STOP */ } - surf->refcnt = 1; - - surf->magic = TBM_SURFACE_MAGIC; - surf->bufmgr = bufmgr; - surf->info.width = width; - surf->info.height = height; - surf->info.format = format; - surf->info.bpp = _tbm_surface_internal_get_bpp(format); - if (!surf->info.bpp) { - TBM_ERR("fail to get bpp from format(%d), error(%s)", format, tbm_error_str(*error)); - *error = tbm_get_last_error(); - goto bpp_fail; - } - - // get number of planes - surf->info.num_planes = _tbm_surface_internal_get_num_planes(format); - if (!surf->info.num_planes) { - TBM_ERR("fail to get num_planes from format(%d), error(%s)", format, tbm_error_str(*error)); - *error = tbm_get_last_error(); - goto num_planes_fail; - } - // import surface + // import surface_data surf->surface_data = tbm_module_import_surface_data(bufmgr->module, width, height, format, buffer_data, error); - if (surf->surface_data) { + if (!surf->surface_data) { + /* LCOV_EXCL_START */ TBM_ERR("tbm_module_import_surface_data failed. width:%d height:%d format:%d error:%s", width, height, format, tbm_error_str(*error)); - goto import_surface_fail; - } - - // set infomation of planes - for (i = 0; i < surf->info.num_planes; i++) { - *error = tbm_surface_data_get_plane_data(surf->surface_data, i, &size, &offset, &stride, &bo_idx); - if (*error != TBM_ERROR_NONE) { - goto query_plane_data_fail; - } - surf->info.planes[i].size = size; - surf->info.planes[i].offset = offset; - surf->info.planes[i].stride = stride; - surf->planes_bo_idx[i] = bo_idx; - } - - // set infomation of bos - bo_data_array = tbm_surface_data_get_bo_data_array(surf->surface_data, &num_bos, error); - if (!bo_data_array) { - TBM_ERR("tbm_surface_data_get_bo_data_array failed. error:%s", tbm_error_str(*error)); - goto get_bos_fail; - } - surf->num_bos = num_bos; - - for (i = 0; i < surf->info.num_planes; i++) { - surf->info.size += surf->info.planes[i].size; - - if (surf->num_bos < surf->planes_bo_idx[i] + 1) - surf->num_bos = surf->planes_bo_idx[i] + 1; + return NULL; + /* LCOV_EXCL_STOP */ } - // get memory_types(bo flags) - flags = tbm_bo_data_get_memory_types(bo_data_array[0], error); + // set the surface data + *error = _tbm_surface_internal_set_data(surf, TBM_BO_DEFAULT, 1); if (*error != TBM_ERROR_NONE) { - TBM_ERR("tbm_bo_data_get_memory_types failed.error:%s", tbm_error_str(*error)); - goto get_memory_types_fail; - } - surf->flags = flags; - - for (i = 0; i < num_bos; i++) { - surf->bos[i] = tbm_bufmgr_internal_alloc_bo_with_bo_data(bufmgr, bo_data_array[i], flags, error); - if (!surf->bos[i]) { - TBM_ERR("fail to alloc bo idx:%d", i); - goto get_bo_fail; - } - - _tbm_bo_set_surface(surf->bos[i], surf); + /* LCOV_EXCL_START */ + tbm_surface_data_free(surf->surface_data); + surf->surface_data = NULL; + _tbm_surface_internal_free(surf); + return NULL; + /* LCOV_EXCL_STOP */ } return surf; - -get_bo_fail: - for (j = 0; j < i; j++) { - if (surf->bos[j]) - tbm_bo_unref(surf->bos[j]); - } -get_memory_types_fail: -get_bos_fail: -query_plane_data_fail: - if (surf->surface_data) - tbm_surface_data_free(surf->surface_data); -import_surface_fail: -num_planes_fail: -bpp_fail: - if (surf) - free(surf); -alloc_surf_fail: - - return NULL; } int @@ -1071,18 +997,10 @@ tbm_surface_internal_create_with_flags(int width, int height, goto check_valid_fail; } - if (tbm_module_support_surface_data(bufmgr->module)) { - surf = _tbm_surface_internal_create_surface_data(bufmgr, width, height, format, flags, &error); - if (!surf) { - TBM_ERR("_tbm_surface_internal_create_surface_data failed."); - goto surface_alloc_fail; - } - } else { - surf = _tbm_surface_internal_create_surface(bufmgr, width, height, format, flags, &error); - if (!surf) { - TBM_ERR("_tbm_surface_internal_create_surface failed."); - goto surface_alloc_fail; - } + surf = _tbm_surface_internal_create_surface(bufmgr, width, height, format, flags, &error); + if (!surf) { + TBM_ERR("_tbm_surface_internal_create_surface failed."); + goto surface_alloc_fail; } TBM_TRACE_SURFACE_INTERNAL("width(%d) height(%d) format(%s) flags(%d) tbm_surface(%p)\n", @@ -2986,7 +2904,7 @@ tbm_surface_internal_import(tbm_surface_info_s *surface_info, tbm_surface_buffer TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(buffer_data != NULL, NULL, error, TBM_ERROR_INVALID_PARAMETER); // import a surface - surf = _tbm_surface_internal_import_surface_data(bufmgr, + surf = _tbm_surface_internal_import_surface(bufmgr, (int)surface_info->width, (int)surface_info->height, (int)surface_info->format, -- 2.7.4