tbm_surface_internal: make an internal function for creating a surface 38/257738/1
authorSooChan Lim <sc1.lim@samsung.com>
Thu, 29 Apr 2021 07:32:24 +0000 (16:32 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Thu, 29 Apr 2021 07:32:24 +0000 (16:32 +0900)
refoctor the creation of a surface.

Change-Id: I4f798a361791b9f2f354c44cae6e92a21c88568e

src/tbm_surface_internal.c

index 8586502..79f94a6 100644 (file)
@@ -591,6 +591,152 @@ _tbm_surface_internal_get_bpp(tbm_format format)
        return bpp;
 }
 
+static struct _tbm_surface *
+_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, 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");
+               *error = TBM_ERROR_OUT_OF_MEMORY;
+               goto alloc_surf_fail;
+               /* LCOV_EXCL_STOP */
+       }
+
+       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)\n", format, tbm_error_str(*error));
+               *error = tbm_get_last_error();
+               goto bpp_fail;
+       }
+
+       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));
+               *error = tbm_get_last_error();
+               goto num_planes_fail;
+       }
+       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;
+               }
+
+               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->num_bos = 1;
+
+       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;
+       }
+
+       surf->flags = flags;
+
+       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 (bufmgr->use_hal_tbm) {
+                       surf->bos[i] = tbm_bo_alloc_with_format(bufmgr, format, i, width, height, surf->info.bpp/8, flags, error);
+                       if (*error == TBM_ERROR_NOT_SUPPORTED) {
+                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to alloc bo idx:%d\n", i);
+                                       *error = tbm_get_last_error();
+                                       goto alloc_bo_fail;
+                               }
+                       }
+               } else if (bufmgr->backend_module_data) {
+                       if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_format) {
+                               /* LCOV_EXCL_START */
+                               surf->bos[i] = tbm_bo_alloc_with_format(bufmgr, format, i, width, height, surf->info.bpp/8, flags, error);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to tbm_bo_alloc_with_format idx:%d\n", i);
+                                       *error = tbm_get_last_error();
+                                       goto alloc_bo_fail;
+                               }
+                               /* LCOV_EXCL_STOP */
+                       } else if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_tiled_format && (flags & TBM_BO_TILED)) {
+                               /* LCOV_EXCL_START */
+                               surf->bos[i] = tbm_bo_alloc_with_tiled_format(bufmgr, width, height, surf->info.bpp/8, format, flags, i, error);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to tbm_bo_alloc_with_tiled_format idx:%d\n", i);
+                                       *error = tbm_get_last_error();
+                                       goto alloc_bo_fail;
+                               }
+                               /* LCOV_EXCL_STOP */
+                       } else {
+                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to alloc bo idx:%d\n", i);
+                                       *error = tbm_get_last_error();
+                                       goto alloc_bo_fail;
+                               }
+                       }
+               } else {
+                       if (bufmgr->backend->surface_bo_alloc) {
+                               /* LCOV_EXCL_START */
+                               surf->bos[i] = tbm_bo_alloc_with_surface(bufmgr, width, height, format, flags, i);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to tbm_bo_alloc_with_surface idx:%d\n", i);
+                                       *error = tbm_get_last_error();
+                                       goto alloc_bo_fail;
+                               /* LCOV_EXCL_STOP */
+                               }
+                       } else {
+                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to alloc bo idx:%d\n", i);
+                                       *error = tbm_get_last_error();
+                                       goto alloc_bo_fail;
+                               }
+                       }
+               }
+
+               _tbm_bo_set_surface(surf->bos[i], surf);
+       }
+
+       *error = TBM_ERROR_NONE;
+
+       return surf;
+
+alloc_bo_fail:
+       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;
+}
+
 int
 tbm_surface_internal_is_valid(tbm_surface_h surface)
 {
@@ -758,14 +904,8 @@ tbm_surface_internal_create_with_flags(int width, int height,
 {
        struct _tbm_bufmgr *bufmgr;
        struct _tbm_surface *surf = NULL;
-       uint32_t size = 0;
-       uint32_t offset = 0;
-       uint32_t stride = 0;
-       uint32_t bo_size = 0;
-       int bo_idx;
-       int i, j;
+       tbm_error_e error = TBM_ERROR_INVALID_OPERATION;
        bool bufmgr_initialized = false;
-       tbm_error_e error;
 
        _tbm_surface_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
@@ -777,7 +917,7 @@ tbm_surface_internal_create_with_flags(int width, int height,
                _init_surface_bufmgr();
                if (!g_surface_bufmgr) {
                        TBM_ERR("fail bufmgr initialization\n");
-                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
+                       error = TBM_ERROR_INVALID_OPERATION;
                        goto check_valid_fail;
                }
                LIST_INITHEAD(&g_surface_bufmgr->surf_list);
@@ -787,124 +927,19 @@ tbm_surface_internal_create_with_flags(int width, int height,
        bufmgr = g_surface_bufmgr;
        if (!TBM_BUFMGR_IS_VALID(bufmgr)) {
                TBM_ERR("The bufmgr is invalid\n");
-               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
+               error = TBM_ERROR_INVALID_PARAMETER;
                goto check_valid_fail;
        }
 
-       surf = calloc(1, sizeof(struct _tbm_surface));
+       // TODO: use_hal_tbm
+       surf = _tbm_surface_internal_create_surface(bufmgr, width, height, format, flags, &error);
        if (!surf) {
-               /* LCOV_EXCL_START */
-               TBM_ERR("fail to alloc surf\n");
-               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
-               goto alloc_surf_fail;
-               /* LCOV_EXCL_STOP */
-       }
-
-       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. error(%s)\n", tbm_error_str(tbm_get_last_error()));
-               goto bpp_fail;
-       }
-       surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
-       if (!surf->info.num_planes) {
-               TBM_ERR("fail to get num_planes. error(%s)\n", tbm_error_str(tbm_get_last_error()));
-               goto num_planes_fail;
-       }
-       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");
-                       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;
-       }
-
-       surf->num_bos = 1;
-
-       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;
-       }
-
-       surf->flags = flags;
-
-       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 (bufmgr->use_hal_tbm) {
-                       surf->bos[i] = tbm_bo_alloc_with_format(bufmgr, format, i, width, height, surf->info.bpp/8, flags, &error);
-                       if (error == TBM_ERROR_NOT_SUPPORTED) {
-                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
-                               if (!surf->bos[i]) {
-                                       TBM_ERR("fail to alloc bo idx:%d\n", i);
-                                       goto alloc_bo_fail;
-                               }
-                       }
-               } else if (bufmgr->backend_module_data) {
-                       if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_format) {
-                               /* LCOV_EXCL_START */
-                               surf->bos[i] = tbm_bo_alloc_with_format(bufmgr, format, i, width, height, surf->info.bpp/8, flags, &error);
-                               if (!surf->bos[i]) {
-                                       TBM_ERR("fail to tbm_bo_alloc_with_format idx:%d\n", i);
-                                       goto alloc_bo_fail;
-                               }
-                               /* LCOV_EXCL_STOP */
-                       } else if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_tiled_format && (flags & TBM_BO_TILED)) {
-                               /* LCOV_EXCL_START */
-                               surf->bos[i] = tbm_bo_alloc_with_tiled_format(bufmgr, width, height, surf->info.bpp/8, format, flags, i, &error);
-                               if (!surf->bos[i]) {
-                                       TBM_ERR("fail to tbm_bo_alloc_with_tiled_format idx:%d\n", i);
-                                       goto alloc_bo_fail;
-                               }
-                               /* LCOV_EXCL_STOP */
-                       } else {
-                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
-                               if (!surf->bos[i]) {
-                                       TBM_ERR("fail to alloc bo idx:%d\n", i);
-                                       goto alloc_bo_fail;
-                               }
-                       }
-               } else {
-                       if (bufmgr->backend->surface_bo_alloc) {
-                               /* LCOV_EXCL_START */
-                               surf->bos[i] = tbm_bo_alloc_with_surface(bufmgr, width, height, format, flags, i);
-                               if (!surf->bos[i]) {
-                                       TBM_ERR("fail to tbm_bo_alloc_with_surface idx:%d\n", i);
-                                       goto alloc_bo_fail;
-                               /* LCOV_EXCL_STOP */
-                               }
-                       } else {
-                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
-                               if (!surf->bos[i]) {
-                                       TBM_ERR("fail to alloc bo idx:%d\n", i);
-                                       goto alloc_bo_fail;
-                               }
-                       }
-               }
-
-               _tbm_bo_set_surface(surf->bos[i], 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", width, height,
-                       _tbm_surface_internal_format_to_str(format), flags, surf);
+       TBM_TRACE_SURFACE_INTERNAL("width(%d) height(%d) format(%s) flags(%d) tbm_surface(%p)\n",
+                       width, height, _tbm_surface_internal_format_to_str(format), flags, surf);
 
        LIST_INITHEAD(&surf->user_data_list);
        LIST_INITHEAD(&surf->debug_data_list);
@@ -912,31 +947,26 @@ tbm_surface_internal_create_with_flags(int width, int height,
 
        LIST_ADD(&surf->item_link, &bufmgr->surf_list);
 
+       _tbm_set_last_result(error);
        _tbm_surface_mutex_unlock();
 
        return surf;
 
 /* LCOV_EXCL_START */
-alloc_bo_fail:
-       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:
+
+surface_alloc_fail:
 check_valid_fail:
        if (bufmgr_initialized && bufmgr) {
                LIST_DELINIT(&bufmgr->surf_list);
                _deinit_surface_bufmgr();
        }
-       _tbm_surface_mutex_unlock();
 
        TBM_ERR("error: width(%d) height(%d) format(%s) flags(%d)\n",
-                       width, height,
-                       _tbm_surface_internal_format_to_str(format), flags);
+               width, height, _tbm_surface_internal_format_to_str(format), flags);
+
+       _tbm_set_last_result(error);
+       _tbm_surface_mutex_unlock();
+
 /* LCOV_EXCL_STOP */
 
        return NULL;