tbm_bufmgr: change tbm_bo_alloc_with_format name
[platform/core/uifw/libtbm.git] / src / tbm_bo.c
index f2870c9..8872848 100644 (file)
@@ -34,15 +34,12 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "tbm_bufmgr_int.h"
 #include "list.h"
 
-static pthread_mutex_t tbm_bo_lock = PTHREAD_MUTEX_INITIALIZER;
-static void _tbm_bo_mutex_unlock(void);
-
 /* check condition */
 #define TBM_BO_RETURN_IF_FAIL(cond) {\
        if (!(cond)) {\
                TBM_ERR("'%s' failed.\n", #cond);\
                _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);\
-               _tbm_bo_mutex_unlock();\
+               _tbm_bufmgr_mutex_unlock();\
                return;\
        } \
 }
@@ -51,47 +48,11 @@ static void _tbm_bo_mutex_unlock(void);
        if (!(cond)) {\
                TBM_ERR("'%s' failed.\n", #cond);\
                _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);\
-               _tbm_bo_mutex_unlock();\
+               _tbm_bufmgr_mutex_unlock();\
                return val;\
        } \
 }
 
-/* LCOV_EXCL_START */
-static bool
-_tbm_bo_mutex_init(void)
-{
-       static bool tbm_bo_mutex_init = false;
-
-       if (tbm_bo_mutex_init)
-               return true;
-
-       if (pthread_mutex_init(&tbm_bo_lock, NULL)) {
-               TBM_ERR("fail: Cannot pthread_mutex_init for tbm_bo_lock.\n");
-               return false;
-       }
-
-       tbm_bo_mutex_init = true;
-
-       return true;
-}
-
-static void
-_tbm_bo_mutex_lock(void)
-{
-       if (!_tbm_bo_mutex_init()) {
-               TBM_ERR("fail: _tbm_bo_mutex_init()\n");
-               return;
-       }
-
-       pthread_mutex_lock(&tbm_bo_lock);
-}
-
-static void
-_tbm_bo_mutex_unlock(void)
-{
-       pthread_mutex_unlock(&tbm_bo_lock);
-}
-
 static char *
 _tbm_flag_to_str(int f)
 {
@@ -192,7 +153,18 @@ _bo_lock(tbm_bo bo, int device, int opt)
        int ret = 1;
        tbm_error_e error;
 
-       if (bo->bufmgr->backend_module_data) {
+       if (bo->bufmgr->use_hal_tbm) {
+               error = (tbm_error_e)hal_tbm_bo_lock((hal_tbm_bo *)bo->bo_data, device, opt);
+               if (error == TBM_ERROR_NOT_SUPPORTED) {
+                       _tbm_set_last_result(TBM_ERROR_NONE);
+               } else {
+                       if (error != TBM_ERROR_NONE) {
+                               TBM_WRN("fail to lock");
+                               _tbm_set_last_result(error);
+                               ret = 0;
+                       }
+               }
+       } else if (bo->bufmgr->backend_module_data) {
                if (bo->bufmgr->bo_func->bo_lock) {
                        error = bo->bufmgr->bo_func->bo_lock(bo->bo_data, device, opt);
                        if (error != TBM_ERROR_NONE) {
@@ -217,7 +189,17 @@ _bo_unlock(tbm_bo bo)
 {
        tbm_error_e error;
 
-       if (bo->bufmgr->backend_module_data) {
+       if (bo->bufmgr->use_hal_tbm) {
+               error = (tbm_error_e)hal_tbm_bo_unlock((hal_tbm_bo *)bo->bo_data);
+               if (error == TBM_ERROR_NOT_SUPPORTED) {
+                       _tbm_set_last_result(TBM_ERROR_NONE);
+               } else {
+                       if (error != TBM_ERROR_NONE) {
+                               TBM_WRN("fail to lock");
+                               _tbm_set_last_result(error);
+                       }
+               }
+       } else if (bo->bufmgr->backend_module_data) {
                if (bo->bufmgr->bo_func->bo_unlock) {
                        error = bo->bufmgr->bo_func->bo_unlock(bo->bo_data);
                        if (error != TBM_ERROR_NONE) {
@@ -254,18 +236,18 @@ _tbm_bo_lock(tbm_bo bo, int device, int opt)
        switch (bo->bufmgr->bo_lock_type) {
        case TBM_BUFMGR_BO_LOCK_TYPE_ONCE:
                if (bo->lock_cnt == 0) {
-                       _tbm_bo_mutex_unlock();
+                       _tbm_bufmgr_mutex_unlock();
                        ret = _bo_lock(bo, device, opt);
-                       _tbm_bo_mutex_lock();
+                       _tbm_bufmgr_mutex_lock();
                        if (ret)
                                bo->lock_cnt++;
                } else
                        ret = 1;
                break;
        case TBM_BUFMGR_BO_LOCK_TYPE_ALWAYS:
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                ret = _bo_lock(bo, device, opt);
-               _tbm_bo_mutex_lock();
+               _tbm_bufmgr_mutex_lock();
                if (ret)
                        bo->lock_cnt++;
                break;
@@ -319,46 +301,61 @@ _tbm_bo_unlock(tbm_bo bo)
 }
 
 static int
-_tbm_bo_is_valid(tbm_bo bo)
+_tbm_bo_magic_check(tbm_bo bo)
 {
-       tbm_bufmgr bufmgr = NULL;
-       tbm_bo old_data = NULL;
+       if (bo->magic != TBM_BO_MAGIC)
+               return 0;
+
+       return 1;
+}
 
-       if (bo == NULL) {
+static int
+_tbm_bo_is_valid(tbm_bo bo)
+{
+       if (!bo) {
                TBM_ERR("error: bo is NULL.\n");
                return 0;
        }
 
-       bufmgr = tbm_bufmgr_get();
-       if (bufmgr == NULL) {
-               TBM_ERR("error: bufmgr is NULL.\n");
+       if (!_tbm_bo_magic_check(bo)) {
+               TBM_ERR("error: No valid bo(%p).\n", bo);
                return 0;
        }
 
-       if (LIST_IS_EMPTY(&bufmgr->bo_list)) {
-               TBM_ERR("error: bo->bo->bufmgr->bo_list is EMPTY.\n");
-               return 0;
-       }
+       return 1;
+}
 
-       LIST_FOR_EACH_ENTRY(old_data, &bufmgr->bo_list, item_link) {
-               if (old_data == 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;
 
-       TBM_ERR("error: No valid bo(%p).\n", bo);
+       LIST_INITHEAD(&bo->user_data_list);
 
-       return 0;
+       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)
 {
        tbm_bo bo;
-       void *bo_priv;
        tbm_backend_bo_data *bo_data;
        tbm_error_e error;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
@@ -367,66 +364,95 @@ tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
        bo = calloc(1, sizeof(struct _tbm_bo));
        if (!bo) {
                /* LCOV_EXCL_START */
-               TBM_ERR("error: fail to create of tbm_bo size(%d) flag(%s)\n",
-                               size, _tbm_flag_to_str(flags));
                _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return NULL;
                /* LCOV_EXCL_STOP */
        }
 
        _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) {
-                       /* LCOV_EXCL_START */
-                       TBM_ERR("error: fail to create of tbm_bo size(%d) flag(%s)\n",
-                                       size, _tbm_flag_to_str(flags));
-                       _tbm_set_last_result(error);
-                       free(bo);
-                       _tbm_bo_mutex_unlock();
-                       return NULL;
-                       /* LCOV_EXCL_STOP */
-               }
-               bo->bo_data = bo_data;
-       } else {
-               bo_priv = bo->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",
-                                       size, _tbm_flag_to_str(flags));
-                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                       free(bo);
-                       _tbm_bo_mutex_unlock();
-                       return NULL;
-                       /* LCOV_EXCL_STOP */
-               }
-               bo->priv = bo_priv;
+       bo_data = tbm_module_bufmgr_bo_alloc(bufmgr->module, bo, size, flags, &error);
+       if (!bo_data || error != TBM_ERROR_NONE) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("tbm_module_bufmgr_bo_alloc failed. error:%d", error);
+               _tbm_set_last_result(error);
+               goto alloc_fail;
+               /* LCOV_EXCL_STOP */
        }
+       bo->bo_data = bo_data;
+       bo->priv = (void *)bo_data; // TODO: this will be DEPRECATED.
 
-       bo->bufmgr->bo_cnt++;
-       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();
 
-       LIST_ADD(&bo->item_link, &bo->bufmgr->bo_list);
+       return bo;
 
-       _tbm_bo_mutex_unlock();
+alloc_fail:
+       TBM_ERR("error: fail to create of tbm_bo size(%d) flag(%s)\n", size, _tbm_flag_to_str(flags));
+       free(bo);
+       _tbm_bufmgr_mutex_unlock();
+       return NULL;
+}
+
+/* LCOV_EXCL_START */
+tbm_bo
+tbm_bo_alloc_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_BO_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
+       TBM_BO_RETURN_VAL_IF_FAIL(bo_data, NULL);
+
+       // return an existed bo if the bo is already created with the same bo_data.
+       if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
+               LIST_FOR_EACH_ENTRY(bo2, &bufmgr->bo_list, item_link) {
+                       if (bo2->bo_data == bo_data) {
+                               TBM_ERR("find bo(%p) ref(%d) flag(%s) in list\n",
+                                               bo2, bo2->ref_cnt, _tbm_flag_to_str(bo2->flags));
+                               bo2->ref_cnt++;
+                               _tbm_bufmgr_mutex_unlock();
+                               return bo2;
+                       }
+               }
+       }
+
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               /* LCOV_EXCL_START */
+               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+               /* LCOV_EXCL_STOP */
+       }
+
+       _tbm_util_check_bo_cnt(bufmgr);
+
+       bo->get_from_hal_surface = 1;
+       bo->bo_data = bo_data;
+
+       _tbm_bo_init(bufmgr, bo, flags);
+
+       TBM_TRACE_BO("bo(%p) refcnt(%d), flag(%s)\n", bo, bo->ref_cnt, _tbm_flag_to_str(bo->flags));
+
+       _tbm_bufmgr_mutex_unlock();
 
        return bo;
 }
 
+/* LCOV_EXCL_STOP */
+
 tbm_bo
 tbm_bo_ref(tbm_bo bo)
 {
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), NULL);
@@ -435,7 +461,7 @@ tbm_bo_ref(tbm_bo bo)
 
        TBM_TRACE_BO("bo(%p) ref_cnt(%d)\n", bo, bo->ref_cnt);
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return bo;
 }
@@ -443,7 +469,7 @@ tbm_bo_ref(tbm_bo bo)
 void
 tbm_bo_unref(tbm_bo bo)
 {
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_IF_FAIL(_tbm_bo_is_valid(bo));
@@ -451,7 +477,7 @@ tbm_bo_unref(tbm_bo bo)
        TBM_TRACE_BO("bo(%p) ref_cnt(%d)\n", bo, bo->ref_cnt - 1);
 
        if (bo->ref_cnt <= 0) {
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return;
        }
 
@@ -459,7 +485,7 @@ tbm_bo_unref(tbm_bo bo)
        if (bo->ref_cnt == 0)
                _tbm_bo_free(bo);
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 }
 
 tbm_bo_handle
@@ -468,26 +494,35 @@ tbm_bo_map(tbm_bo bo, int device, int opt)
        tbm_bo_handle bo_handle;
        tbm_error_e error;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), (tbm_bo_handle) NULL);
 
        if (!_tbm_bo_lock(bo, device, opt)) {
                TBM_ERR("error: fail to lock bo:%p)\n", bo);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return (tbm_bo_handle) NULL;
        }
 
-       if (bo->bufmgr->backend_module_data) {
+       if (bo->bufmgr->use_hal_tbm) {
+               hal_tbm_bo_handle hbo_handle;
+               hbo_handle = hal_tbm_bo_map((hal_tbm_bo *)bo->bo_data, device, opt, (hal_tbm_error *)&error);
+               if (hbo_handle.ptr == NULL) {
+                       /* LCOV_EXCL_START */
+                       _tbm_set_last_result(error);
+                       TBM_ERR("error: bo(%p) bo_handle(%p) error(%d)\n", bo, hbo_handle.ptr, error);
+                       goto bo_map_fail;
+                       /* LCOV_EXCL_STOP */
+               }
+               memcpy(&bo_handle.ptr, &hbo_handle.ptr, sizeof(tbm_bo_handle));
+       } else if (bo->bufmgr->backend_module_data) {
                bo_handle = bo->bufmgr->bo_func->bo_map(bo->bo_data, device, opt, &error);
                if (bo_handle.ptr == NULL) {
                        /* LCOV_EXCL_START */
                        _tbm_set_last_result(error);
                        TBM_ERR("error: fail to map bo:%p error:%d\n", bo, error);
-                       _tbm_bo_unlock(bo);
-                       _tbm_bo_mutex_unlock();
-                       return (tbm_bo_handle) NULL;
+                       goto bo_map_fail;
                        /* LCOV_EXCL_STOP */
                }
        } else {
@@ -496,9 +531,7 @@ tbm_bo_map(tbm_bo bo, int device, int opt)
                        /* LCOV_EXCL_START */
                        _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
                        TBM_ERR("error: fail to map bo:%p\n", bo);
-                       _tbm_bo_unlock(bo);
-                       _tbm_bo_mutex_unlock();
-                       return (tbm_bo_handle) NULL;
+                       goto bo_map_fail;
                        /* LCOV_EXCL_STOP */
                }
        }
@@ -508,9 +541,14 @@ tbm_bo_map(tbm_bo bo, int device, int opt)
 
        TBM_TRACE_BO("bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return bo_handle;
+
+bo_map_fail:
+       _tbm_bo_unlock(bo);
+       _tbm_bufmgr_mutex_unlock();
+       return (tbm_bo_handle) NULL;
 }
 
 int
@@ -519,21 +557,30 @@ tbm_bo_unmap(tbm_bo bo)
        int ret = 1;
        tbm_error_e error;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
        TBM_BO_RETURN_VAL_IF_FAIL(bo->map_cnt > 0, 0);
 
-       if (bo->bufmgr->backend_module_data) {
+       if (bo->bufmgr->use_hal_tbm) {
+               error = (hal_tbm_error)hal_tbm_bo_unmap((hal_tbm_bo *)bo->bo_data);
+               if (error != TBM_ERROR_NONE) {
+                       /* LCOV_EXCL_START */
+                       TBM_ERR("error: bo(%p) map_cnt(%d) error(%d)\n", bo, bo->map_cnt, error);
+                       _tbm_set_last_result(error);
+                       ret = 0;
+                       goto done;
+                       /* LCOV_EXCL_STOP */
+               }
+       } else if (bo->bufmgr->backend_module_data) {
                error = bo->bufmgr->bo_func->bo_unmap(bo->bo_data);
                if (error != TBM_ERROR_NONE) {
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: bo(%p) map_cnt(%d) error(%d)\n", bo, bo->map_cnt, error);
                        _tbm_set_last_result(error);
                        ret = 0;
-                       _tbm_bo_mutex_unlock();
-                       return ret;
+                       goto done;
                        /* LCOV_EXCL_STOP */
                }
        } else {
@@ -542,8 +589,7 @@ tbm_bo_unmap(tbm_bo bo)
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
                        _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                       _tbm_bo_mutex_unlock();
-                       return ret;
+                       goto done;
                        /* LCOV_EXCL_STOP */
                }
        }
@@ -555,7 +601,8 @@ tbm_bo_unmap(tbm_bo bo)
 
        _tbm_bo_unlock(bo);
 
-       _tbm_bo_mutex_unlock();
+done:
+       _tbm_bufmgr_mutex_unlock();
 
        return ret;
 }
@@ -566,19 +613,29 @@ tbm_bo_get_handle(tbm_bo bo, int device)
        tbm_bo_handle bo_handle;
        tbm_error_e error;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), (tbm_bo_handle) NULL);
 
-       if (bo->bufmgr->backend_module_data) {
+       if (bo->bufmgr->use_hal_tbm) {
+               hal_tbm_bo_handle hbo_handle;
+               hbo_handle = hal_tbm_bo_get_handle((hal_tbm_bo *)bo->bo_data, device, (hal_tbm_error *)&error);
+               if (hbo_handle.ptr == NULL) {
+                       /* LCOV_EXCL_START */
+                       TBM_ERR("error: bo(%p) bo_handle(%p) error(%d)\n", bo, hbo_handle.ptr, error);
+                       _tbm_set_last_result(error);
+                       goto bo_handle_fail;
+                       /* LCOV_EXCL_STOP */
+               }
+               memcpy(&bo_handle.ptr, &hbo_handle.ptr, sizeof(tbm_bo_handle));
+       } else if (bo->bufmgr->backend_module_data) {
                bo_handle = bo->bufmgr->bo_func->bo_get_handle(bo->bo_data, device, &error);
                if (bo_handle.ptr == NULL) {
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: bo(%p) bo_handle(%p) error(%d)\n", bo, bo_handle.ptr, error);
                        _tbm_set_last_result(error);
-                       _tbm_bo_mutex_unlock();
-                       return (tbm_bo_handle) NULL;
+                       goto bo_handle_fail;
                        /* LCOV_EXCL_STOP */
                }
        } else {
@@ -587,17 +644,20 @@ tbm_bo_get_handle(tbm_bo bo, int device)
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: bo(%p) bo_handle(%p)\n", bo, bo_handle.ptr);
                        _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                       _tbm_bo_mutex_unlock();
-                       return (tbm_bo_handle) NULL;
+                       goto bo_handle_fail;
                        /* LCOV_EXCL_STOP */
                }
        }
 
        TBM_TRACE_BO("bo(%p) bo_handle(%p)\n", bo, bo_handle.ptr);
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return bo_handle;
+
+bo_handle_fail:
+       _tbm_bufmgr_mutex_unlock();
+       return (tbm_bo_handle) NULL;
 }
 
 tbm_key
@@ -606,15 +666,24 @@ tbm_bo_export(tbm_bo bo)
        tbm_key ret;
        tbm_error_e error;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
 
-       if (bo->bufmgr->backend_module_data) {
+       if (bo->bufmgr->use_hal_tbm) {
+               ret = (hal_tbm_key)hal_tbm_bo_export_key((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)&error);
+               if (!ret) {
+                       /* LCOV_EXCL_START */
+                       TBM_ERR("error: bo(%p) tbm_key(%d) error(%d)\n", bo, ret, error);
+                       _tbm_set_last_result(error);
+                       goto done;
+                       /* LCOV_EXCL_STOP */
+               }
+       } else if (bo->bufmgr->backend_module_data) {
                if (!bo->bufmgr->bo_func->bo_export_key) {
                        /* LCOV_EXCL_START */
-                       _tbm_bo_mutex_unlock();
+                       _tbm_bufmgr_mutex_unlock();
                        _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
                        return 0;
                        /* LCOV_EXCL_STOP */
@@ -625,14 +694,13 @@ tbm_bo_export(tbm_bo bo)
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: bo(%p) tbm_key(%d) error(%d)\n", bo, ret, error);
                        _tbm_set_last_result(error);
-                       _tbm_bo_mutex_unlock();
-                       return ret;
+                       goto done;
                        /* LCOV_EXCL_STOP */
                }
        } else {
                if (!bo->bufmgr->backend->bo_export) {
                        /* LCOV_EXCL_START */
-                       _tbm_bo_mutex_unlock();
+                       _tbm_bufmgr_mutex_unlock();
                        _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
                        return 0;
                        /* LCOV_EXCL_STOP */
@@ -643,15 +711,15 @@ tbm_bo_export(tbm_bo bo)
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: bo(%p) tbm_key(%d)\n", bo, ret);
                        _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                       _tbm_bo_mutex_unlock();
-                       return ret;
+                       goto done;
                        /* LCOV_EXCL_STOP */
                }
        }
 
        TBM_TRACE_BO("bo(%p) tbm_key(%u)\n", bo, ret);
 
-       _tbm_bo_mutex_unlock();
+done:
+       _tbm_bufmgr_mutex_unlock();
 
        return ret;
 }
@@ -662,15 +730,24 @@ tbm_bo_export_fd(tbm_bo bo)
        tbm_fd ret;
        tbm_error_e error;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), -1);
 
-       if (bo->bufmgr->backend_module_data) {
+       if (bo->bufmgr->use_hal_tbm) {
+               ret = (hal_tbm_fd)hal_tbm_bo_export_fd((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)&error);
+               if (ret < 0) {
+                       /* LCOV_EXCL_START */
+                       TBM_ERR("error: bo(%p) tbm_fd(%d) error(%d)\n", bo, ret, error);
+                       _tbm_set_last_result(error);
+                       goto done;
+                       /* LCOV_EXCL_STOP */
+               }
+       } else if (bo->bufmgr->backend_module_data) {
                if (!bo->bufmgr->bo_func->bo_export_fd) {
                        /* LCOV_EXCL_START */
-                       _tbm_bo_mutex_unlock();
+                       _tbm_bufmgr_mutex_unlock();
                        _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
                        return -1;
                        /* LCOV_EXCL_STOP */
@@ -681,14 +758,13 @@ tbm_bo_export_fd(tbm_bo bo)
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: bo(%p) tbm_fd(%d) error(%d)\n", bo, ret, error);
                        _tbm_set_last_result(error);
-                       _tbm_bo_mutex_unlock();
-                       return ret;
+                       goto done;
                        /* LCOV_EXCL_STOP */
                }
        } else {
                if (!bo->bufmgr->backend->bo_export_fd) {
                        /* LCOV_EXCL_START */
-                       _tbm_bo_mutex_unlock();
+                       _tbm_bufmgr_mutex_unlock();
                        _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
                        return -1;
                        /* LCOV_EXCL_STOP */
@@ -699,15 +775,15 @@ tbm_bo_export_fd(tbm_bo bo)
                        /* LCOV_EXCL_START */
                        TBM_ERR("error: bo(%p) tbm_fd(%d)\n", bo, ret);
                        _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                       _tbm_bo_mutex_unlock();
-                       return ret;
+                       goto done;
                        /* LCOV_EXCL_STOP */
                }
        }
 
        TBM_TRACE_BO("bo(%p) tbm_fd(%d)\n", bo, ret);
 
-       _tbm_bo_mutex_unlock();
+done:
+       _tbm_bufmgr_mutex_unlock();
 
        return ret;
 }
@@ -715,35 +791,15 @@ tbm_bo_export_fd(tbm_bo bo)
 tbm_bo
 tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
 {
-       tbm_bo bo;
-       tbm_bo bo2 = NULL;
-       void *bo_priv;
+       tbm_bo bo, bo2 = NULL;
        tbm_error_e error;
-       tbm_backend_bo_data *bo_data;
+       int flags;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
 
-       if (bufmgr->backend_module_data) {
-               if (!bufmgr->bufmgr_func->bufmgr_import_key) {
-                       /* LCOV_EXCL_START */
-                       _tbm_bo_mutex_unlock();
-                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
-                       return NULL;
-                       /* LCOV_EXCL_STOP */
-               }
-       } else {
-               if (!bufmgr->backend->bo_import) {
-                       /* LCOV_EXCL_START */
-                       _tbm_bo_mutex_unlock();
-                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
-                       return NULL;
-                       /* LCOV_EXCL_STOP */
-               }
-       }
-
        _tbm_util_check_bo_cnt(bufmgr);
 
        bo = calloc(1, sizeof(struct _tbm_bo));
@@ -751,92 +807,59 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
                /* LCOV_EXCL_START */
                TBM_ERR("error: fail to import of tbm_bo by key(%d)\n", key);
                _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return NULL;
                /* 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 (!bo_data) {
-                       /* LCOV_EXCL_START */
-                       TBM_ERR("error: fail to import of tbm_bo by key(%d). error(%d)\n", key, error);
-                       _tbm_set_last_result(error);
-                       free(bo);
-                       _tbm_bo_mutex_unlock();
-                       return NULL;
-                       /* LCOV_EXCL_STOP */
-               }
-
-               if (!LIST_IS_EMPTY(&bo->bufmgr->bo_list)) {
-                       LIST_FOR_EACH_ENTRY(bo2, &bo->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,
-                                                       _tbm_flag_to_str(bo2->flags));
-                                       bo2->ref_cnt++;
-                                       free(bo);
-                                       _tbm_bo_mutex_unlock();
-                                       return bo2;
-                               }
-                       }
-               }
-               bo->bo_data = bo_data;
-       } else {
-               bo_priv = bo->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);
-                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                       free(bo);
-                       _tbm_bo_mutex_unlock();
-                       return NULL;
-                       /* LCOV_EXCL_STOP */
-               }
-
-               if (!LIST_IS_EMPTY(&bo->bufmgr->bo_list)) {
-                       LIST_FOR_EACH_ENTRY(bo2, &bo->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,
-                                                       _tbm_flag_to_str(bo2->flags));
-                                       bo2->ref_cnt++;
-                                       free(bo);
-                                       _tbm_bo_mutex_unlock();
-                                       return bo2;
-                               }
-                       }
-               }
-               bo->priv = bo_priv;
+       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);
+               _tbm_set_last_result(error);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
-       bo->bufmgr->bo_cnt++;
-       bo->ref_cnt = 1;
+       // 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_bufmgr_mutex_unlock();
+               return bo2;
+       }
 
-       if (bo->bufmgr->backend_module_data) {
-               bo->flags = bo->bufmgr->bo_func->bo_get_memory_types(bo->bo_data, &error);
+       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)");
                        _tbm_set_last_result(error);
-                       bo->flags = TBM_BO_DEFAULT;
+                       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)");
+                       _tbm_set_last_result(error);
+                       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_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return bo;
 }
@@ -844,35 +867,15 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
 tbm_bo
 tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
 {
-       tbm_bo bo;
-       tbm_bo bo2 = NULL;
-       void *bo_priv;
-       tbm_backend_bo_data *bo_data;
+       tbm_bo bo, bo2 = NULL;
        tbm_error_e error;
+       int flags;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
 
-       if (bufmgr->backend_module_data) {
-               if (!bufmgr->bufmgr_func->bufmgr_import_fd) {
-                       /* LCOV_EXCL_START */
-                       _tbm_bo_mutex_unlock();
-                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
-                       return NULL;
-                       /* LCOV_EXCL_STOP */
-               }
-       } else {
-               if (!bufmgr->backend->bo_import_fd) {
-                       /* LCOV_EXCL_START */
-                       _tbm_bo_mutex_unlock();
-                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
-                       return NULL;
-                       /* LCOV_EXCL_STOP */
-               }
-       }
-
        _tbm_util_check_bo_cnt(bufmgr);
 
        bo = calloc(1, sizeof(struct _tbm_bo));
@@ -880,92 +883,59 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
                /* LCOV_EXCL_START */
                TBM_ERR("error: fail to import tbm_bo by tbm_fd(%d)\n", fd);
                _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return NULL;
                /* 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 (!bo_data) {
-                       /* LCOV_EXCL_START */
-                       TBM_ERR("error: fail to import tbm_bo by tbm_fd(%d). error(%d)\n", fd, error);
-                       _tbm_set_last_result(error);
-                       free(bo);
-                       _tbm_bo_mutex_unlock();
-                       return NULL;
-                       /* LCOV_EXCL_STOP */
-               }
-
-               if (!LIST_IS_EMPTY(&bo->bufmgr->bo_list)) {
-                       LIST_FOR_EACH_ENTRY(bo2, &bo->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,
-                                                       _tbm_flag_to_str(bo2->flags));
-                                       bo2->ref_cnt++;
-                                       free(bo);
-                                       _tbm_bo_mutex_unlock();
-                                       return bo2;
-                               }
-                       }
-               }
-               bo->bo_data = bo_data;
-       } else {
-               bo_priv = bo->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);
-                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                       free(bo);
-                       _tbm_bo_mutex_unlock();
-                       return NULL;
-                       /* LCOV_EXCL_STOP */
-               }
-
-               if (!LIST_IS_EMPTY(&bo->bufmgr->bo_list)) {
-                       LIST_FOR_EACH_ENTRY(bo2, &bo->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,
-                                                       _tbm_flag_to_str(bo2->flags));
-                                       bo2->ref_cnt++;
-                                       free(bo);
-                                       _tbm_bo_mutex_unlock();
-                                       return bo2;
-                               }
-                       }
-               }
-               bo->priv = bo_priv;
+       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);
+               _tbm_set_last_result(error);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
-       bo->bufmgr->bo_cnt++;
-       bo->ref_cnt = 1;
+       // 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_bufmgr_mutex_unlock();
+               return bo2;
+       }
 
-       if (bo->bufmgr->backend_module_data) {
-               bo->flags = bo->bufmgr->bo_func->bo_get_memory_types(bo->bo_data, &error);
+       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)");
                        _tbm_set_last_result(error);
-                       bo->flags = TBM_BO_DEFAULT;
+                       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)");
+                       _tbm_set_last_result(error);
+                       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_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return bo;
 }
@@ -976,12 +946,18 @@ tbm_bo_size(tbm_bo bo)
        int size;
        tbm_error_e error;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
 
-       if (bo->bufmgr->backend_module_data) {
+       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_ERR("fail to get the size of the bo_data(%d).", error);
+                       _tbm_set_last_result(TBM_ERROR_NONE);
+               }
+       } else if (bo->bufmgr->backend_module_data) {
                size = bo->bufmgr->bo_func->bo_get_size(bo->bo_data, &error);
                if (error != TBM_ERROR_NONE) {
                        TBM_ERR("fail to get the size of the bo_data(%d).", error);
@@ -992,7 +968,7 @@ tbm_bo_size(tbm_bo bo)
 
        TBM_TRACE_BO("bo(%p) size(%d)\n", bo, size);
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return size;
 }
@@ -1000,7 +976,7 @@ tbm_bo_size(tbm_bo bo)
 int
 tbm_bo_locked(tbm_bo bo)
 {
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
@@ -1008,18 +984,18 @@ tbm_bo_locked(tbm_bo bo)
        if (bo->bufmgr->bo_lock_type == TBM_BUFMGR_BO_LOCK_TYPE_NEVER) {
                TBM_ERR("bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
                _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return 0;
        }
 
        if (bo->lock_cnt > 0) {
                TBM_TRACE_BO("error: bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return 1;
        }
 
        TBM_TRACE_BO("bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return 0;
 }
@@ -1031,7 +1007,7 @@ tbm_bo_swap(tbm_bo bo1, tbm_bo bo2)
        int size1 = -1, size2 = -2;
        void *temp;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo1), 0);
@@ -1039,7 +1015,20 @@ tbm_bo_swap(tbm_bo bo1, tbm_bo bo2)
 
        TBM_TRACE_BO("before: bo1(%p) bo2(%p)\n", bo1, bo2);
 
-       if (bo1->bufmgr->backend_module_data) {
+       if (bo1->bufmgr->use_hal_tbm) {
+               size1 = hal_tbm_bo_get_size((hal_tbm_bo *)bo1->bo_data, (hal_tbm_error *)&error1);
+               if (error1 != TBM_ERROR_NONE) {
+                       TBM_ERR("fail to get the size of bo1.(%d)", error1);
+                       _tbm_set_last_result(error1);
+                       goto fail;
+               }
+               size2 = hal_tbm_bo_get_size((hal_tbm_bo *)bo2->bo_data, (hal_tbm_error *)&error2);
+               if (error2 != TBM_ERROR_NONE) {
+                       TBM_ERR("fail to get the size of bo1.(%d)", error2);
+                       _tbm_set_last_result(error2);
+                       goto fail;
+               }
+       } else if (bo1->bufmgr->backend_module_data) {
                size1 = bo1->bufmgr->bo_func->bo_get_size(bo1->bo_data, &error1);
                if (error1 != TBM_ERROR_NONE) {
                        TBM_ERR("fail to get the size of bo1.(%d)", error1);
@@ -1069,13 +1058,13 @@ tbm_bo_swap(tbm_bo bo1, tbm_bo bo2)
        bo1->priv = bo2->priv;
        bo2->priv = temp;
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return 1;
 
 fail:
        TBM_ERR("error: bo1(%p) bo2(%p)\n", bo1, bo2);
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return 0;
 }
@@ -1086,7 +1075,7 @@ tbm_bo_add_user_data(tbm_bo bo, unsigned long key,
 {
        tbm_user_data *data;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
@@ -1096,7 +1085,7 @@ tbm_bo_add_user_data(tbm_bo bo, unsigned long key,
        if (data) {
                TBM_TRACE_BO("warning: user data already exist key(%ld)\n", key);
                _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return 0;
        }
 
@@ -1104,7 +1093,7 @@ tbm_bo_add_user_data(tbm_bo bo, unsigned long key,
        if (!data) {
                TBM_ERR("error: bo(%p) key(%lu)\n", bo, key);
                _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return 0;
        }
 
@@ -1112,7 +1101,7 @@ tbm_bo_add_user_data(tbm_bo bo, unsigned long key,
 
        LIST_ADD(&data->item_link, &bo->user_data_list);
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return 1;
 }
@@ -1122,7 +1111,7 @@ tbm_bo_delete_user_data(tbm_bo bo, unsigned long key)
 {
        tbm_user_data *old_data;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
@@ -1130,7 +1119,7 @@ tbm_bo_delete_user_data(tbm_bo bo, unsigned long key)
        if (LIST_IS_EMPTY(&bo->user_data_list)) {
                TBM_TRACE_BO("bo(%p) key(%lu)\n", bo, key);
                _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return 0;
        }
 
@@ -1138,7 +1127,7 @@ tbm_bo_delete_user_data(tbm_bo bo, unsigned long key)
        if (!old_data) {
                TBM_TRACE_BO("bo(%p) key(%lu)\n", bo, key);
                _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return 0;
        }
 
@@ -1146,7 +1135,7 @@ tbm_bo_delete_user_data(tbm_bo bo, unsigned long key)
 
        user_data_delete(old_data);
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return 1;
 }
@@ -1156,7 +1145,7 @@ tbm_bo_set_user_data(tbm_bo bo, unsigned long key, void *data)
 {
        tbm_user_data *old_data;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
@@ -1164,7 +1153,7 @@ tbm_bo_set_user_data(tbm_bo bo, unsigned long key, void *data)
        if (LIST_IS_EMPTY(&bo->user_data_list)) {
                TBM_TRACE_BO("error: bo(%p) key(%lu)\n", bo, key);
                _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return 0;
        }
 
@@ -1172,7 +1161,7 @@ tbm_bo_set_user_data(tbm_bo bo, unsigned long key, void *data)
        if (!old_data) {
                TBM_TRACE_BO("error: bo(%p) key(%lu)\n", bo, key);
                _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return 0;
        }
 
@@ -1182,7 +1171,7 @@ tbm_bo_set_user_data(tbm_bo bo, unsigned long key, void *data)
 
        TBM_TRACE_BO("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return 1;
 }
@@ -1192,7 +1181,7 @@ tbm_bo_get_user_data(tbm_bo bo, unsigned long key, void **data)
 {
        tbm_user_data *old_data;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
@@ -1200,7 +1189,7 @@ tbm_bo_get_user_data(tbm_bo bo, unsigned long key, void **data)
        if (!data || LIST_IS_EMPTY(&bo->user_data_list)) {
                TBM_TRACE_BO("error: bo(%p) key(%lu)\n", bo, key);
                _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return 0;
        }
 
@@ -1209,7 +1198,7 @@ tbm_bo_get_user_data(tbm_bo bo, unsigned long key, void **data)
                *data = NULL;
                TBM_TRACE_BO("error: bo(%p) key(%lu)\n", bo, key);
                _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
-               _tbm_bo_mutex_unlock();
+               _tbm_bufmgr_mutex_unlock();
                return 0;
        }
 
@@ -1217,7 +1206,7 @@ tbm_bo_get_user_data(tbm_bo bo, unsigned long key, void **data)
 
        TBM_TRACE_BO("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return 1;
 }
@@ -1227,7 +1216,7 @@ tbm_bo_get_flags(tbm_bo bo)
 {
        int flags;
 
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
 
@@ -1235,7 +1224,7 @@ tbm_bo_get_flags(tbm_bo bo)
 
        TBM_TRACE_BO("bo(%p)\n", bo);
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return flags;
 }
@@ -1245,13 +1234,13 @@ tbm_bo_get_flags(tbm_bo bo)
 int
 _tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface)
 {
-       _tbm_bo_mutex_lock();
+       _tbm_bufmgr_mutex_lock();
 
        TBM_BO_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
 
        bo->surface = surface;
 
-       _tbm_bo_mutex_unlock();
+       _tbm_bufmgr_mutex_unlock();
 
        return 1;
 }
@@ -1277,7 +1266,15 @@ _tbm_bo_free(tbm_bo bo)
        }
 
        /* call the bo_free */
-       if (bo->bufmgr->backend_module_data) {
+       if (bo->bufmgr->use_hal_tbm) {
+               // call hal_tbm_bo_free when bo is created by tbm_bo_alloc api.
+               if (!bo->get_from_hal_surface) {
+                       bo->get_from_hal_surface = 0;
+
+                       hal_tbm_bo_free(bo->bo_data);
+                       bo->bo_data = NULL;
+               }
+       } else if (bo->bufmgr->backend_module_data) {
                bo->bufmgr->bo_func->bo_free(bo->bo_data);
                bo->bo_data = NULL;
        } else {
@@ -1285,9 +1282,8 @@ _tbm_bo_free(tbm_bo bo)
                bo->priv = NULL;
        }
 
-       bo->bufmgr->bo_cnt--;
+       _tbm_bo_deinit(bo);
 
-       LIST_DEL(&bo->item_link);
        free(bo);
 }
 /* LCOV_EXCL_STOP */