surface: move function of allocating for surface to tbm_bo 99/251099/2
authorChangyeon Lee <cyeon.lee@samsung.com>
Thu, 7 Jan 2021 10:36:50 +0000 (19:36 +0900)
committerChangyeon Lee <cyeon.lee@samsung.com>
Mon, 11 Jan 2021 09:36:30 +0000 (18:36 +0900)
Change-Id: Ic71bf090a534da9cb7ffd4c086c34c268b60f67e

src/tbm_bo.c
src/tbm_bufmgr_int.h
src/tbm_surface_internal.c

index 527c30b..b6d9d2e 100644 (file)
@@ -306,6 +306,29 @@ _tbm_bo_is_valid(tbm_bo bo)
        return 1;
 }
 
+static void
+_tbm_bo_init(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);
+}
+
+static void
+_tbm_bo_deinit(tbm_bo bo)
+{
+       bo->magic = 0;
+
+       bo->bufmgr->bo_cnt--;
+       LIST_DEL(&bo->item_link);
+}
+
 tbm_bo
 tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
 {
@@ -333,8 +356,6 @@ tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
 
        _tbm_util_check_bo_cnt(bufmgr);
 
-       bo->bufmgr = bufmgr;
-
        if (bufmgr->backend_module_data) {
                bo_data = bufmgr->bufmgr_func->bufmgr_alloc_bo(bufmgr->bufmgr_data, (unsigned int)size, flags, &error);
                if (!bo_data) {
@@ -349,7 +370,7 @@ tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
                }
                bo->bo_data = bo_data;
        } else {
-               bo_priv = bo->bufmgr->backend->bo_alloc(bo, size, flags);
+               bo_priv = bufmgr->backend->bo_alloc(bo, size, flags);
                if (!bo_priv) {
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: fail to create of tbm_bo size(%d) flag(%s)\n",
@@ -363,17 +384,113 @@ tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
                bo->priv = bo_priv;
        }
 
-       bo->bufmgr->bo_cnt++;
-       bo->magic = TBM_BO_MAGIC;
-       bo->ref_cnt = 1;
-       bo->flags = flags;
+       _tbm_bo_init(bufmgr, bo, flags);
 
        TBM_TRACE_BO("bo(%p) size(%d) refcnt(%d), flag(%s)\n", bo, size, bo->ref_cnt,
                        _tbm_flag_to_str(bo->flags));
 
-       LIST_INITHEAD(&bo->user_data_list);
+       _tbm_bufmgr_mutex_unlock();
+
+       return bo;
+}
+
+/* LCOV_EXCL_START */
+tbm_bo
+tbm_bo_alloc_with_format(tbm_bufmgr bufmgr, int format, int bo_idx, int width,
+                                               int height, tbm_bo_memory_type flags, tbm_error_e *error)
+{
+       tbm_bo bo;
+       tbm_backend_bo_data *bo_data;
+
+       _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       TBM_BO_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
+
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               TBM_ERR("error: fail to tbm_bo_alloc_with_format fmt(%s) idx(%d) w(%d) h(%d) mem_types(%s)\n",
+                               FOURCC_STR(format), bo_idx, width, height, _tbm_flag_to_str(flags));
+               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+       }
+
+       _tbm_util_check_bo_cnt(bufmgr);
+
+       if (!bufmgr->backend_module_data || !bufmgr->bufmgr_func->bufmgr_alloc_bo_with_format) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("error: not supported tbm_bo_alloc_with_format\n");
+               _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
+               free(bo);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+               /* LCOV_EXCL_STOP */
+       }
+
+       bo_data = bufmgr->bufmgr_func->bufmgr_alloc_bo_with_format(bufmgr->bufmgr_data, format, bo_idx,
+                                                                                                                               width, height, flags, error);
+       if (!bo_data) {
+               TBM_ERR("error: fail to tbm_bo_alloc_with_format fmt(%s) idx(%d) w(%d) h(%d) mem_types(%s)\n",
+                               FOURCC_STR(format), bo_idx, width, height, _tbm_flag_to_str(flags));
+               _tbm_set_last_result(*error);
+               free(bo);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+       }
+
+       bo->bo_data = bo_data;
+       _tbm_bo_init(bufmgr, bo, flags);
+
+       _tbm_bufmgr_mutex_unlock();
+
+       return bo;
+}
+
+tbm_bo
+tbm_bo_alloc_with_tiled_format(tbm_bufmgr bufmgr, int width, int height, int bpp, int format,
+                                                       tbm_bo_memory_type flags, int bo_idx, tbm_error_e *error)
+{
+       tbm_bo bo;
+       tbm_backend_bo_data *bo_data;
+
+       _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       TBM_BO_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
+
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               TBM_ERR("error: fail to tbm_bo_alloc_with_tiled_format fmt(%s) idx(%d) w(%d) h(%d) flags(%s)\n",
+                               FOURCC_STR(format), bo_idx, width, height, _tbm_flag_to_str(flags));
+               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+       }
+
+       _tbm_util_check_bo_cnt(bufmgr);
+
+       if (!bufmgr->backend_module_data || !bufmgr->bufmgr_func->bufmgr_alloc_bo_with_tiled_format) {
+               TBM_ERR("error: not supported tbm_bo_alloc_with_tiled_format\n");
+               _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
+               free(bo);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+       }
+
+       bo_data = bufmgr->bufmgr_func->bufmgr_alloc_bo_with_tiled_format(bufmgr->bufmgr_data, width, height,
+                                                                                                                                       bpp, format, flags, bo_idx, error);
+       if (!bo_data) {
+               TBM_ERR("error: fail to tbm_bo_alloc_with_tiled_format fmt(%s) idx(%d) w(%d) h(%d) flags(%s)\n",
+                               FOURCC_STR(format), bo_idx, width, height, _tbm_flag_to_str(flags));
+               _tbm_set_last_result(*error);
+               free(bo);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+       }
 
-       LIST_ADD(&bo->item_link, &bo->bufmgr->bo_list);
+       bo->bo_data = bo_data;
+       _tbm_bo_init(bufmgr, bo, flags);
 
        _tbm_bufmgr_mutex_unlock();
 
@@ -381,6 +498,55 @@ tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
 }
 
 tbm_bo
+tbm_bo_alloc_with_surface(tbm_bufmgr bufmgr, int width, int height, int format, int flags, int bo_idx)
+{
+       tbm_bo bo;
+       void *bo_priv;
+
+       _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       TBM_BO_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
+
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               TBM_ERR("error: fail to tbm_bo_alloc_with_surface fmt(%s) idx(%d) w(%d) h(%d) flags(%s)\n",
+                               FOURCC_STR(format), bo_idx, width, height, _tbm_flag_to_str(flags));
+               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+       }
+
+       _tbm_util_check_bo_cnt(bufmgr);
+
+       if (!bufmgr->backend->surface_bo_alloc) {
+               TBM_ERR("error: not supported tbm_bo_alloc_with_surface\n");
+               _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
+               free(bo);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+       }
+
+       bo_priv = bufmgr->backend->surface_bo_alloc(bo, width, height, format, flags, bo_idx);
+       if (!bo_priv) {
+               TBM_ERR("error: fail to tbm_bo_alloc_with_surface fmt(%s) idx(%d) w(%d) h(%d) flags(%s)\n",
+                               FOURCC_STR(format), bo_idx, width, height, _tbm_flag_to_str(flags));
+               _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
+               free(bo);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+       }
+
+       bo->priv = bo_priv;
+       _tbm_bo_init(bufmgr, bo, flags);
+
+       _tbm_bufmgr_mutex_unlock();
+
+       return bo;
+}
+/* LCOV_EXCL_STOP */
+
+tbm_bo
 tbm_bo_ref(tbm_bo bo)
 {
        _tbm_bufmgr_mutex_lock();
@@ -677,6 +843,7 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
        void *bo_priv;
        tbm_error_e error;
        tbm_backend_bo_data *bo_data;
+       int flags;
 
        _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
@@ -713,10 +880,8 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
                /* LCOV_EXCL_STOP */
        }
 
-       bo->bufmgr = bufmgr;
-
-       if (bo->bufmgr->backend_module_data) {
-               bo_data = bo->bufmgr->bufmgr_func->bufmgr_import_key(bufmgr->bufmgr_data, key, &error);
+       if (bufmgr->backend_module_data) {
+               bo_data = bufmgr->bufmgr_func->bufmgr_import_key(bufmgr->bufmgr_data, key, &error);
                if (!bo_data) {
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: fail to import of tbm_bo by key(%d). error(%d)\n", key, error);
@@ -727,8 +892,8 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
                        /* LCOV_EXCL_STOP */
                }
 
-               if (!LIST_IS_EMPTY(&bo->bufmgr->bo_list)) {
-                       LIST_FOR_EACH_ENTRY(bo2, &bo->bufmgr->bo_list, item_link) {
+               if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
+                       LIST_FOR_EACH_ENTRY(bo2, &bufmgr->bo_list, item_link) {
                                if (bo2->bo_data == bo_data) {
                                        TBM_TRACE_BO("find bo(%p) ref(%d) key(%d) flag(%s) in list\n",
                                                        bo2, bo2->ref_cnt, key,
@@ -742,7 +907,7 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
                }
                bo->bo_data = bo_data;
        } else {
-               bo_priv = bo->bufmgr->backend->bo_import(bo, key);
+               bo_priv = bufmgr->backend->bo_import(bo, key);
                if (!bo_priv) {
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: fail to import of tbm_bo by key(%d)\n", key);
@@ -753,8 +918,8 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
                        /* LCOV_EXCL_STOP */
                }
 
-               if (!LIST_IS_EMPTY(&bo->bufmgr->bo_list)) {
-                       LIST_FOR_EACH_ENTRY(bo2, &bo->bufmgr->bo_list, item_link) {
+               if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
+                       LIST_FOR_EACH_ENTRY(bo2, &bufmgr->bo_list, item_link) {
                                if (bo2->priv == bo_priv) {
                                        TBM_TRACE_BO("find bo(%p) ref(%d) key(%d) flag(%s) in list\n",
                                                        bo2, bo2->ref_cnt, key,
@@ -769,31 +934,25 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
                bo->priv = bo_priv;
        }
 
-       bo->bufmgr->bo_cnt++;
-       bo->magic = TBM_BO_MAGIC;
-       bo->ref_cnt = 1;
-
-       if (bo->bufmgr->backend_module_data) {
-               bo->flags = bo->bufmgr->bo_func->bo_get_memory_types(bo->bo_data, &error);
+       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)");
                        _tbm_set_last_result(error);
-                       bo->flags = TBM_BO_DEFAULT;
+                       flags = TBM_BO_DEFAULT;
                }
        } else {
-               if (bo->bufmgr->backend->bo_get_flags)
-                       bo->flags = bo->bufmgr->backend->bo_get_flags(bo);
+               if (bufmgr->backend->bo_get_flags)
+                       flags = bufmgr->backend->bo_get_flags(bo);
                else
-                       bo->flags = TBM_BO_DEFAULT;
+                       flags = TBM_BO_DEFAULT;
        }
 
+       _tbm_bo_init(bufmgr, bo, flags);
+
        TBM_TRACE_BO("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, &bo->bufmgr->bo_list);
-
        _tbm_bufmgr_mutex_unlock();
 
        return bo;
@@ -807,6 +966,7 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
        void *bo_priv;
        tbm_backend_bo_data *bo_data;
        tbm_error_e error;
+       int flags;
 
        _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
@@ -843,10 +1003,8 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
                /* LCOV_EXCL_STOP */
        }
 
-       bo->bufmgr = bufmgr;
-
-       if (bo->bufmgr->backend_module_data) {
-               bo_data = bo->bufmgr->bufmgr_func->bufmgr_import_fd(bufmgr->bufmgr_data, fd, &error);
+       if (bufmgr->backend_module_data) {
+               bo_data = bufmgr->bufmgr_func->bufmgr_import_fd(bufmgr->bufmgr_data, fd, &error);
                if (!bo_data) {
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: fail to import tbm_bo by tbm_fd(%d). error(%d)\n", fd, error);
@@ -857,8 +1015,8 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
                        /* LCOV_EXCL_STOP */
                }
 
-               if (!LIST_IS_EMPTY(&bo->bufmgr->bo_list)) {
-                       LIST_FOR_EACH_ENTRY(bo2, &bo->bufmgr->bo_list, item_link) {
+               if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
+                       LIST_FOR_EACH_ENTRY(bo2, &bufmgr->bo_list, item_link) {
                                if (bo2->bo_data == bo_data) {
                                        TBM_TRACE_BO("find bo(%p) ref(%d) fd(%d) flag(%s) in list\n",
                                                        bo2, bo2->ref_cnt, fd,
@@ -872,7 +1030,7 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
                }
                bo->bo_data = bo_data;
        } else {
-               bo_priv = bo->bufmgr->backend->bo_import_fd(bo, fd);
+               bo_priv = bufmgr->backend->bo_import_fd(bo, fd);
                if (!bo_priv) {
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: fail to import tbm_bo by tbm_fd(%d)\n", fd);
@@ -883,8 +1041,8 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
                        /* LCOV_EXCL_STOP */
                }
 
-               if (!LIST_IS_EMPTY(&bo->bufmgr->bo_list)) {
-                       LIST_FOR_EACH_ENTRY(bo2, &bo->bufmgr->bo_list, item_link) {
+               if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
+                       LIST_FOR_EACH_ENTRY(bo2, &bufmgr->bo_list, item_link) {
                                if (bo2->priv == bo_priv) {
                                        TBM_TRACE_BO("find bo(%p) ref(%d) fd(%d) flag(%s) in list\n",
                                                        bo2, bo2->ref_cnt, fd,
@@ -899,31 +1057,25 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
                bo->priv = bo_priv;
        }
 
-       bo->bufmgr->bo_cnt++;
-       bo->magic = TBM_BO_MAGIC;
-       bo->ref_cnt = 1;
-
-       if (bo->bufmgr->backend_module_data) {
-               bo->flags = bo->bufmgr->bo_func->bo_get_memory_types(bo->bo_data, &error);
+       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)");
                        _tbm_set_last_result(error);
-                       bo->flags = TBM_BO_DEFAULT;
+                       flags = TBM_BO_DEFAULT;
                }
        } else {
-               if (bo->bufmgr->backend->bo_get_flags)
-                       bo->flags = bo->bufmgr->backend->bo_get_flags(bo);
+               if (bufmgr->backend->bo_get_flags)
+                       flags = bufmgr->backend->bo_get_flags(bo);
                else
-                       bo->flags = TBM_BO_DEFAULT;
+                       flags = TBM_BO_DEFAULT;
        }
 
+       _tbm_bo_init(bufmgr, bo, flags);
+
        TBM_TRACE_BO("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, &bo->bufmgr->bo_list);
-
        _tbm_bufmgr_mutex_unlock();
 
        return bo;
@@ -1244,10 +1396,8 @@ _tbm_bo_free(tbm_bo bo)
                bo->priv = NULL;
        }
 
-       bo->magic = 0;
-       bo->bufmgr->bo_cnt--;
+       _tbm_bo_deinit(bo);
 
-       LIST_DEL(&bo->item_link);
        free(bo);
 }
 /* LCOV_EXCL_STOP */
index f4e6e28..7972b97 100644 (file)
@@ -63,6 +63,12 @@ extern tbm_bufmgr gBufMgr;
 extern int b_dump_queue;
 extern int trace_mask;
 
+#define C(b, m)                (((b) >> (m)) & 0xFF)
+#define B(c, s)                ((((unsigned int)(c)) & 0xff) << (s))
+#define FOURCC(a, b, c, d)     (B(d, 24) | B(c, 16) | B(b, 8) | B(a, 0))
+#define FOURCC_STR(id)         C(id, 0), C(id, 8), C(id, 16), C(id, 24)
+#define FOURCC_ID(str)         FOURCC(((char*)str)[0], ((char*)str)[1], ((char*)str)[2], ((char*)str)[3])
+
 /* check flags */
 #define RETURN_CHECK_FLAG(cond) {\
        if ((cond)) {\
@@ -267,7 +273,6 @@ int _tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface);
 int _tbm_surface_is_valid(tbm_surface_h surface);
 void _tbm_bo_free(tbm_bo bo);
 
-/* functions for mutex */
 int tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
                                  tbm_surface_info_s *info, int map);
 void tbm_surface_internal_unmap(tbm_surface_h surface);
@@ -289,7 +294,14 @@ tbm_bufmgr tbm_bufmgr_get(void);
 
 void _tbm_set_last_result(tbm_error_e err);
 
+/* functions for mutex */
 void _tbm_bufmgr_mutex_lock(void);
 void _tbm_bufmgr_mutex_unlock(void);
 
+tbm_bo tbm_bo_alloc_with_format(tbm_bufmgr bufmgr, int format, int bo_idx, int width,
+                                               int height, tbm_bo_memory_type flags, tbm_error_e *error);
+tbm_bo tbm_bo_alloc_with_tiled_format(tbm_bufmgr bufmgr, int width, int height, int bpp, int format,
+                                                       tbm_bo_memory_type flags, int bo_idx, tbm_error_e *error);
+tbm_bo tbm_bo_alloc_with_surface(tbm_bufmgr bufmgr, int width, int height, int format, int flags, int bo_idx);
+
 #endif                                                 /* _TBM_BUFMGR_INT_H_ */
index a682fd1..10ebe79 100644 (file)
@@ -47,12 +47,6 @@ static tbm_bufmgr g_surface_bufmgr;
 static pthread_mutex_t tbm_surface_lock = PTHREAD_MUTEX_INITIALIZER;
 void _tbm_surface_mutex_unlock(void);
 
-#define C(b, m)              (((b) >> (m)) & 0xFF)
-#define B(c, s)              ((((unsigned int)(c)) & 0xff) << (s))
-#define FOURCC(a, b, c, d)     (B(d, 24) | B(c, 16) | B(b, 8) | B(a, 0))
-#define FOURCC_STR(id)      C(id, 0), C(id, 8), C(id, 16), C(id, 24)
-#define FOURCC_ID(str)      FOURCC(((char*)str)[0], ((char*)str)[1], ((char*)str)[2], ((char*)str)[3])
-
 /* check condition */
 #define TBM_SURFACE_RETURN_IF_FAIL(cond) {\
        if (!(cond)) {\
@@ -728,9 +722,6 @@ tbm_surface_internal_create_with_flags(int width, int height,
        int bo_idx;
        int i, j;
        bool bufmgr_initialized = false;
-       tbm_bo bo = NULL;
-       void *bo_priv = NULL;
-       tbm_backend_bo_data *bo_data = NULL;
        tbm_error_e error;
 
        _tbm_surface_mutex_lock();
@@ -812,70 +803,20 @@ tbm_surface_internal_create_with_flags(int width, int height,
                if (bufmgr->backend_module_data) {
                        if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_format) {
                                /* LCOV_EXCL_START */
-                               bo = calloc(1, sizeof(struct _tbm_bo));
-                               if (!bo) {
-                                       TBM_ERR("fail to alloc bo struct\n");
-                                       _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
-                                       goto alloc_bo_fail;
-                               }
-
-                               bo->bufmgr = surf->bufmgr;
-
-                               _tbm_bufmgr_mutex_lock();
-
-                               bo_data = bufmgr->bufmgr_func->bufmgr_alloc_bo_with_format(bufmgr->bufmgr_data, format, i,
-                                                                                                                               width, height, flags, &error);
-                               if (!bo_data) {
-                                       TBM_ERR("fail to alloc bo priv. error(%d)\n", error);
-                                       _tbm_set_last_result(error);
-                                       free(bo);
-                                       _tbm_bufmgr_mutex_unlock();
+                               surf->bos[i] = tbm_bo_alloc_with_format(bufmgr, format, i, width, height, flags, &error);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to tbm_bo_alloc_with_format idx:%d\n", i);
                                        goto alloc_bo_fail;
                                }
-                               bo->bo_data = bo_data;
-
-                               bo->ref_cnt = 1;
-                               bo->flags = flags;
-                               LIST_INITHEAD(&bo->user_data_list);
-
-                               LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list);
-
-                               _tbm_bufmgr_mutex_unlock();
-
-                               surf->bos[i] = bo;
                                /* LCOV_EXCL_STOP */
                        } else if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_tiled_format && (flags & TBM_BO_TILED)) {
-                               bo = calloc(1, sizeof(struct _tbm_bo));
-                               if (!bo) {
-                                       TBM_ERR("fail to alloc bo struct\n");
-                                       _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
-                                       goto alloc_bo_fail;
-                               }
-
-                               bo->bufmgr = surf->bufmgr;
-
-                               _tbm_bufmgr_mutex_lock();
-
-                               bo_data = bufmgr->bufmgr_func->bufmgr_alloc_bo_with_tiled_format(bufmgr->bufmgr_data, width, height, surf->info.bpp/8, format, flags, i, &error);
-                               if (!bo_data) {
-                                       TBM_ERR("fail to alloc bo priv. error(%d)\n", error);
-                                       _tbm_set_last_result(error);
-                                       free(bo);
-                                       _tbm_bufmgr_mutex_unlock();
+                               /* 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;
                                }
-                               bo->bo_data = bo_data;
-
-                               bo->ref_cnt = 1;
-                               bo->flags = flags;
-                               LIST_INITHEAD(&bo->user_data_list);
-
-                               LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list);
-
-                               _tbm_bufmgr_mutex_unlock();
-
-                               surf->bos[i] = bo;
-
+                               /* LCOV_EXCL_STOP */
                        } else {
                                surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
                                if (!surf->bos[i]) {
@@ -886,37 +827,12 @@ tbm_surface_internal_create_with_flags(int width, int height,
                } else {
                        if (bufmgr->backend->surface_bo_alloc) {
                                /* LCOV_EXCL_START */
-                               bo = calloc(1, sizeof(struct _tbm_bo));
-                               if (!bo) {
-                                       TBM_ERR("fail to alloc bo struct\n");
-                                       _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
-                                       goto alloc_bo_fail;
-                               }
-
-                               bo->bufmgr = surf->bufmgr;
-
-                               _tbm_bufmgr_mutex_lock();
-
-                               bo_priv = bufmgr->backend->surface_bo_alloc(bo, width, height, format, flags, i);
-                               if (!bo_priv) {
-                                       TBM_ERR("fail to alloc bo priv\n");
-                                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                                       free(bo);
-                                       _tbm_bufmgr_mutex_unlock();
+                               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;
-                               }
-                               bo->priv = bo_priv;
-
-                               bo->ref_cnt = 1;
-                               bo->flags = flags;
-                               LIST_INITHEAD(&bo->user_data_list);
-
-                               LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list);
-
-                               _tbm_bufmgr_mutex_unlock();
-
-                               surf->bos[i] = bo;
                                /* LCOV_EXCL_STOP */
+                               }
                        } else {
                                surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
                                if (!surf->bos[i]) {