tbm_bufmgr: lock the gLock before locking tbm_bufmgr_mutex
[platform/core/uifw/libtbm.git] / src / tbm_bufmgr.c
index 48dca8e..55f9243 100644 (file)
@@ -75,6 +75,15 @@ void _tbm_bufmgr_mutex_unlock(void);
        } \
 }
 
+#define TBM_BUFMGR_RETURN_VAL_SET_ERR_IF_FAIL(cond, val, error, error_type) {\
+       if (!(cond)) {\
+               TBM_ERR("'%s' failed.\n", #cond);\
+               error = error_type;\
+               _tbm_bufmgr_mutex_unlock();\
+               return val;\
+       } \
+}
+
 /* LCOV_EXCL_START */
 
 static void
@@ -195,41 +204,6 @@ _tbm_util_get_appname_from_pid(long pid, char *str)
        snprintf(str, sizeof(cmdline), "%s", cmdline);
 }
 
-static void
-_tbm_bufmgr_copy_module_data(tbm_bufmgr bufmgr, tbm_module *module, int reset)
-{
-       if (!reset) {
-               bufmgr->module_data = module->module_data;
-               bufmgr->backend = module->backend;
-
-               bufmgr->backend_module_data = module->backend_module_data;
-               bufmgr->bufmgr_data = module->bufmgr_data;
-               bufmgr->bufmgr_func = module->bufmgr_func;
-               bufmgr->bo_func = module->bo_func;
-
-               bufmgr->use_hal_tbm = module->use_hal_tbm;
-               bufmgr->auth_wl_socket_created = module->auth_wl_socket_created;
-               bufmgr->auth_fd = module->auth_fd;
-               bufmgr->hal_backend = module->hal_backend;
-               bufmgr->hal_bufmgr = module->hal_bufmgr;
-       } else {
-               bufmgr->module_data = NULL;
-               bufmgr->backend = NULL;
-
-               bufmgr->backend_module_data = NULL;
-               bufmgr->bufmgr_data = NULL;
-               bufmgr->bufmgr_func = NULL;
-               bufmgr->bo_func = NULL;
-
-               bufmgr->use_hal_tbm = 0;
-               bufmgr->auth_wl_socket_created = 0;
-               bufmgr->auth_fd = -1;
-               bufmgr->hal_backend = NULL;
-               bufmgr->hal_bufmgr = NULL;
-       }
-}
-
-
 /* LCOV_EXCL_STOP */
 
 static tbm_bufmgr
@@ -308,11 +282,8 @@ _tbm_bufmgr_init(int fd, int server)
 
        }
 
-       // TODO: this is temporary. it will be removed after finishing refactoring the tbm_module.
-       _tbm_bufmgr_copy_module_data(gBufMgr, gBufMgr->module, 0);
-
        /* check the essential capabilities of tbm_module */
-       gBufMgr->capabilities = tbm_module_bufmgr_get_capabilities(gBufMgr->module, &error);
+       gBufMgr->capabilities = tbm_module_get_capabilities(gBufMgr->module, &error);
        if (gBufMgr->capabilities == TBM_BUFMGR_CAPABILITY_NONE) {
                TBM_ERR("The capabilities of the backend module is TBM_BUFMGR_CAPABILITY_NONE.");
                TBM_ERR("TBM_BUFMGR_CAPABILITY_SHARE_FD is the essential capability.");
@@ -393,22 +364,22 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 {
        TBM_RETURN_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr));
 
-       _tbm_bufmgr_mutex_lock();
        pthread_mutex_lock(&gLock);
+       _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
        if (!gBufMgr) {
                TBM_ERR("gBufmgr already destroy: bufmgr:%p\n", bufmgr);
-               pthread_mutex_unlock(&gLock);
                _tbm_bufmgr_mutex_unlock();
+               pthread_mutex_unlock(&gLock);
                return;
        }
 
        bufmgr->ref_count--;
        if (bufmgr->ref_count > 0) {
                TBM_DBG("reduce a ref_count(%d) of tbm_bufmgr(%p)\n", bufmgr->ref_count, bufmgr);
-               pthread_mutex_unlock(&gLock);
                _tbm_bufmgr_mutex_unlock();
+               pthread_mutex_unlock(&gLock);
                return;
        }
 
@@ -436,9 +407,6 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 
        tbm_module_unload(bufmgr->module);
 
-       // TODO: this is temporary. it will be removed after finishing refactoring the tbm_module.
-       _tbm_bufmgr_copy_module_data(bufmgr, bufmgr->module, 1);
-
        if (bufmgr->fd > 0)
                close(bufmgr->fd);
 
@@ -447,8 +415,8 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
        free(bufmgr);
        gBufMgr = NULL;
 
-       pthread_mutex_unlock(&gLock);
        _tbm_bufmgr_mutex_unlock();
+       pthread_mutex_unlock(&gLock);
 }
 
 unsigned int
@@ -573,16 +541,9 @@ tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr)
                        TBM_SNRPRINTF(str, len, c, "%s\n", data);
 
                        for (i = 0; i < surf->num_bos; i++) {
-                               if (bufmgr->use_hal_tbm) {
-                                       size = hal_tbm_bo_get_size((hal_tbm_bo *)surf->bos[i]->bo_data, (hal_tbm_error *)&error);
-                                       if (error != TBM_ERROR_NONE)
-                                               TBM_WRN("fail to get the size of bo.");
-                               } else if (bufmgr->backend_module_data) {
-                                       size = bufmgr->bo_func->bo_get_size(surf->bos[i]->bo_data, &error);
-                                       if (error != TBM_ERROR_NONE)
-                                               TBM_WRN("fail to get the size of bo.");
-                               } else
-                                       size = bufmgr->backend->bo_size(surf->bos[i]);
+                               size = tbm_bo_data_get_size(surf->bos[i]->bo_data, &error);
+                               if (error != TBM_ERROR_NONE)
+                                       TBM_WRN("fail to get the size of bo.");
                                TBM_SNRPRINTF(str, len, c, " bo:%-12p  %-26d%-10d\n",
                                          surf->bos[i],
                                          surf->bos[i]->ref_cnt,
@@ -603,22 +564,12 @@ tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr)
                tbm_key key = 0;
 
                LIST_FOR_EACH_ENTRY(bo, &bufmgr->bo_list, item_link) {
-                       if (bo->bufmgr->use_hal_tbm) {
-                               size = hal_tbm_bo_get_size((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)&error);
-                               if (error != TBM_ERROR_NONE)
-                                       TBM_WRN("fail to get the size of bo.");
-                               key = (tbm_key)hal_tbm_bo_export_key((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)&error);
-                       } else if (bufmgr->backend_module_data) {
-                               size = bufmgr->bo_func->bo_get_size(bo->bo_data, &error);
-                               if (error != TBM_ERROR_NONE)
-                                       TBM_WRN("fail to get the size of bo.");
-                               key = bufmgr->bo_func->bo_export_key(bo->bo_data, &error);
-                               if (error != TBM_ERROR_NONE)
-                                       TBM_WRN("fail to get the tdm_key of bo.");
-                       } else {
-                               size = bufmgr->backend->bo_size(bo);
-                               key = bufmgr->backend->bo_export(bo);
-                       }
+                       size = tbm_bo_data_get_size(bo->bo_data, &error);
+                       if (error != TBM_ERROR_NONE)
+                               TBM_WRN("fail to get the size of bo.");
+                       key = tbm_bo_data_export_key(bo->bo_data, &error);
+                       if (error != TBM_ERROR_NONE)
+                               TBM_WRN("fail to get the tdm_key of bo.");
                        TBM_SNRPRINTF(str, len, c, "%-3d %-11p   %-5d %-7d    %-6d    %-5u %-7d %-11p  %-4d\n",
                                  ++bo_cnt,
                                  bo,
@@ -825,7 +776,7 @@ tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *native_display)
 
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), 0);
 
-       error = tbm_module_bufmgr_bind_native_display(bufmgr->module, native_display);
+       error = tbm_module_bind_native_display(bufmgr->module, native_display);
        if (error != TBM_ERROR_NONE) {
                _tbm_set_last_result(error);
                _tbm_bufmgr_mutex_unlock();
@@ -904,19 +855,23 @@ tbm_bufmgr_internal_find_bo(tbm_bufmgr bufmgr, tbm_bo bo)
                return NULL;
 
        LIST_FOR_EACH_ENTRY(bo2, &bufmgr->bo_list, item_link) {
-               if (bo2->bo_data == bo->bo_data) {
+               if (tbm_module_compare_bo_data(bufmgr->module, bo2->bo_data, bo->bo_data))
                        return bo2;
-               }
        }
 
        return NULL;
 }
 
 tbm_bo
-tbm_bufmgr_internal_alloc_bo(tbm_bufmgr bufmgr, int size, int flags, tbm_error_e *error)
+tbm_bufmgr_internal_alloc_bo(tbm_bufmgr bufmgr, int size, int flags)
 {
+       tbm_error_e error = TBM_ERROR_NONE;
        tbm_bo bo;
-       tbm_backend_bo_data *bo_data;
+
+       _tbm_bufmgr_mutex_lock();
+
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(size > 0, NULL);
 
        _tbm_bufmgr_check_bo_cnt(bufmgr);
 
@@ -924,25 +879,38 @@ tbm_bufmgr_internal_alloc_bo(tbm_bufmgr bufmgr, int size, int flags, tbm_error_e
        if (!bo) {
                /* LCOV_EXCL_START */
                TBM_ERR("memory allocationc failed.");
-               *error = TBM_ERROR_OUT_OF_MEMORY;
-               return NULL;
+               error = TBM_ERROR_OUT_OF_MEMORY;
+               goto failed;
                /* LCOV_EXCL_STOP */
        }
 
-       bo_data = tbm_module_bufmgr_bo_alloc(bufmgr->module, bo, size, flags, error);
-       if (!bo_data) {
+       bo->bo_data = tbm_module_alloc_bo_data(bufmgr->module, bo, size, flags, &error);
+       if (!bo->bo_data) {
                /* LCOV_EXCL_START */
-               TBM_ERR("tbm_module_bufmgr_bo_alloc failed. size:%d flags:%s error:%d", size, _tbm_flag_to_str(flags), *error);
+               TBM_ERR("tbm_module_alloc_bo_data failed. size:%d flags:%s error:%d", size, _tbm_flag_to_str(flags), error);
                free(bo);
-               return NULL;
+               _tbm_set_last_result(error);
+               _tbm_bufmgr_mutex_unlock();
+               goto failed;
                /* LCOV_EXCL_STOP */
        }
-       bo->bo_data = bo_data;
-       bo->priv = (void *)bo_data; // TODO: this will be DEPRECATED.
 
        _tbm_bufmgr_initialize_bo(bufmgr, bo, flags);
 
+       TBM_TRACE_BO("bo(%p) size(%d) refcnt(%d), flag(%s)", bo, size, bo->ref_cnt, _tbm_flag_to_str(bo->flags));
+
+       _tbm_set_last_result(TBM_ERROR_NONE);
+       _tbm_bufmgr_mutex_unlock();
+
        return bo;
+
+/* LCOV_EXCL_START */
+failed:
+       _tbm_set_last_result(error);
+       _tbm_bufmgr_mutex_unlock();
+
+       return NULL;
+/* LCOV_EXCL_STOP */
 }
 
 
@@ -955,7 +923,6 @@ tbm_bufmgr_internal_alloc_bo_with_format(tbm_bufmgr bufmgr, int format, int bo_i
        tbm_bo bo = NULL;
 
        _tbm_bufmgr_mutex_lock();
-       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
 
@@ -965,17 +932,17 @@ tbm_bufmgr_internal_alloc_bo_with_format(tbm_bufmgr bufmgr, int format, int bo_i
        if (!bo) {
                /* LCOV_EXCL_START */
                TBM_ERR("memory allocationc failed.");
-               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+               *error = TBM_ERROR_OUT_OF_MEMORY;
+               goto failed;
                /* LCOV_EXCL_STOP */
-               goto fail;
        }
 
-       bo->bo_data = tbm_module_bufmgr_bo_alloc_with_format(bufmgr->module, format, bo_idx, width, height, bpp, flags, error);
+       bo->bo_data = tbm_module_alloc_bo_data_with_format(bufmgr->module, format, bo_idx, width, height, bpp, flags, error);
        if (!bo->bo_data) {
                /* LCOV_EXCL_START */
-               _tbm_set_last_result(*error);
+               free(bo);
+               goto failed;
                /* LCOV_EXCL_STOP */
-               goto fail;
        }
 
        _tbm_bufmgr_initialize_bo(bufmgr, bo, flags);
@@ -984,24 +951,25 @@ tbm_bufmgr_internal_alloc_bo_with_format(tbm_bufmgr bufmgr, int format, int bo_i
 
        return bo;
 
-fail:
-       if (bo)
-               free(bo);
+/* LCOV_EXCL_START */
+failed:
        _tbm_bufmgr_mutex_unlock();
 
        return NULL;
+/* LCOV_EXCL_STOP */
 }
 
 tbm_bo
-tbm_bufmgr_internal_alloc_bo_with_bo_data(tbm_bufmgr bufmgr, tbm_backend_bo_data *bo_data, int flags)
+tbm_bufmgr_internal_alloc_bo_with_bo_data(tbm_bufmgr bufmgr, tbm_bo_data *bo_data, int flags, tbm_error_e *error)
 {
        tbm_bo bo, bo2 = NULL;
 
        _tbm_bufmgr_mutex_lock();
-       _tbm_set_last_result(TBM_ERROR_NONE);
 
-       TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
-       TBM_BUFMGR_RETURN_VAL_IF_FAIL(bo_data, NULL);
+       *error = TBM_ERROR_NONE;
+
+       TBM_BUFMGR_RETURN_VAL_SET_ERR_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+       TBM_BUFMGR_RETURN_VAL_SET_ERR_IF_FAIL(bo_data, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
 
        _tbm_bufmgr_check_bo_cnt(bufmgr);
 
@@ -1009,13 +977,13 @@ tbm_bufmgr_internal_alloc_bo_with_bo_data(tbm_bufmgr bufmgr, tbm_backend_bo_data
        if (!bo) {
                /* LCOV_EXCL_START */
                TBM_ERR("memory allocationc failed.");
-               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+               *error = TBM_ERROR_OUT_OF_MEMORY;
                _tbm_bufmgr_mutex_unlock();
                return NULL;
                /* LCOV_EXCL_STOP */
        }
        bo->bo_data = bo_data;
-       bo->get_from_hal_surface = 1;
+       bo->get_from_surface_data = 1;
 
        // return an existed bo if the bo is already created with the same bo_data.
        bo2 = tbm_bufmgr_internal_find_bo(bufmgr, bo);
@@ -1035,4 +1003,136 @@ tbm_bufmgr_internal_alloc_bo_with_bo_data(tbm_bufmgr bufmgr, tbm_backend_bo_data
        return bo;
 }
 
+tbm_bo
+tbm_bufmgr_internal_import_bo_with_key(tbm_bufmgr bufmgr, unsigned int key)
+{
+       tbm_error_e error = TBM_ERROR_NONE;
+       tbm_bo bo, bo2;
+       int flags;
+
+       _tbm_bufmgr_mutex_lock();
+
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
+
+       _tbm_bufmgr_check_bo_cnt(bufmgr);
+
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("memory allocationc failed.");
+               goto failed;
+               /* LCOV_EXCL_STOP */
+       }
+
+       bo->bo_data = tbm_module_import_bo_data_with_key(bufmgr->module, bo, key, &error);
+       if (!bo->bo_data) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("tbm_module_import_bo_data_with_key failed. tbm_key:%d", key);
+               free(bo);
+               goto failed;
+               /* LCOV_EXCL_STOP */
+       }
+
+       // return the existed bo2 if bo->bo_data and bo2->bo_data is the same
+       bo2 = tbm_bufmgr_internal_find_bo(bufmgr, bo);
+       if (bo2) {
+               TBM_TRACE_BO("find bo(%p) ref(%d) key(%d) flag(%s) in list",
+                                       bo2, bo2->ref_cnt, key, _tbm_flag_to_str(bo2->flags));
+               bo2->ref_cnt++;
+               free(bo);
+               _tbm_set_last_result(TBM_ERROR_NONE);
+               _tbm_bufmgr_mutex_unlock();
+               return bo2;
+       }
+
+       flags = tbm_bo_data_get_memory_types(bo->bo_data, &error);
+       if (error != TBM_ERROR_NONE) {
+               TBM_WRN("tbm_bo_data_get_memory_types filed. use the default flags:TBM_BO_DEFAULT.");
+               flags = TBM_BO_DEFAULT;
+               error = TBM_ERROR_NONE;
+       }
+
+       _tbm_bufmgr_initialize_bo(bufmgr, bo, flags);
+
+       TBM_TRACE_BO("import new bo(%p) ref(%d) key(%d) flag(%s) in list",
+                               bo, bo->ref_cnt, key, _tbm_flag_to_str(bo->flags));
+
+       _tbm_set_last_result(TBM_ERROR_NONE);
+       _tbm_bufmgr_mutex_unlock();
+
+       return bo;
+
+failed:
+       _tbm_set_last_result(error);
+       _tbm_bufmgr_mutex_unlock();
+
+       return NULL;
+}
+
+tbm_bo
+tbm_bufmgr_internal_import_bo_with_fd(tbm_bufmgr bufmgr, tbm_fd fd)
+{
+       tbm_error_e error = TBM_ERROR_NONE;
+       tbm_bo bo, bo2;
+       int flags;
+
+       _tbm_bufmgr_mutex_lock();
+
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
+
+       _tbm_bufmgr_check_bo_cnt(bufmgr);
+
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("memory allocationc failed.");
+               goto failed;
+               /* LCOV_EXCL_STOP */
+       }
+
+       bo->bo_data = tbm_module_import_bo_data_with_fd(bufmgr->module, bo, fd, &error);
+       if (!bo->bo_data) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("tbm_module_import_bo_data_with_fd failed. tbm_fd:%d", fd);
+               free(bo);
+               goto failed;
+               /* LCOV_EXCL_STOP */
+       }
+
+       // return the existed bo2 if bo->bo_data and bo2->bo_data is the same
+       bo2 = tbm_bufmgr_internal_find_bo(bufmgr, bo);
+       if (bo2) {
+               TBM_TRACE_BO("find bo(%p) ref(%d) fd(%d) flag(%s) in list",
+                                       bo2, bo2->ref_cnt, fd, _tbm_flag_to_str(bo2->flags));
+               bo2->ref_cnt++;
+               free(bo);
+               _tbm_set_last_result(TBM_ERROR_NONE);
+               _tbm_bufmgr_mutex_unlock();
+               return bo2;
+       }
+
+       flags = tbm_bo_data_get_memory_types(bo->bo_data, &error);
+       if (error != TBM_ERROR_NONE) {
+               TBM_WRN("tbm_bo_data_get_memory_types filed. use the default flags:TBM_BO_DEFAULT.");
+               flags = TBM_BO_DEFAULT;
+               error = TBM_ERROR_NONE;
+       }
+
+       TBM_TRACE_BO("import bo(%p) ref(%d) fd(%d) flag(%s)",
+                               bo, bo->ref_cnt, fd, _tbm_flag_to_str(bo->flags));
+
+       _tbm_bufmgr_initialize_bo(bufmgr, bo, flags);
+
+       _tbm_set_last_result(TBM_ERROR_NONE);
+       _tbm_bufmgr_mutex_unlock();
+
+       return bo;
+
+failed:
+       _tbm_set_last_result(error);
+       _tbm_bufmgr_mutex_unlock();
+
+       return NULL;
+}
+
 /* LCOV_EXCL_STOP */