tbm_bufmgr: change tbm_bo_alloc_with_format name
[platform/core/uifw/libtbm.git] / src / tbm_bufmgr.c
index 4f6ece3..b235029 100644 (file)
@@ -33,7 +33,6 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 #include "tbm_bufmgr.h"
 #include "tbm_bufmgr_int.h"
-#include "tbm_bufmgr_backend.h"
 #include "tbm_drm_helper.h"
 #include "list.h"
 
@@ -78,6 +77,33 @@ void _tbm_bufmgr_mutex_unlock(void);
 
 /* LCOV_EXCL_START */
 
+static void
+_tbm_bufmgr_check_bo_cnt(tbm_bufmgr bufmgr)
+{
+       static int last_chk_bo_cnt = 0;
+
+       if ((bufmgr->bo_cnt >= 500) && ((bufmgr->bo_cnt % 20) == 0) &&
+               (bufmgr->bo_cnt > last_chk_bo_cnt)) {
+               TBM_DBG("============TBM BO CNT DEBUG: bo_cnt=%d\n", bufmgr->bo_cnt);
+               tbm_bufmgr_debug_show(bufmgr);
+               last_chk_bo_cnt = bufmgr->bo_cnt;
+       }
+}
+
+static void
+_tbm_bufmgr_initialize_bo(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);
+}
+
 void
 _tbm_bufmgr_mutex_lock(void)
 {
@@ -169,11 +195,48 @@ _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
 _tbm_bufmgr_init(int fd, int server)
 {
+       tbm_error_e error = TBM_ERROR_NONE;
+
 #ifdef TBM_BUFMGR_INIT_TIME
        struct timeval start_tv, end_tv;
 #endif
@@ -234,7 +297,7 @@ _tbm_bufmgr_init(int fd, int server)
        }
 
        /* load bufmgr priv from env */
-       gBufMgr->module = tbm_module_load(gBufMgr, gBufMgr->fd);
+       gBufMgr->module = tbm_module_load(gBufMgr->fd);
        if (!gBufMgr->module) {
                TBM_ERR("error : Fail to load bufmgr backend\n");
                _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
@@ -244,6 +307,34 @@ _tbm_bufmgr_init(int fd, int server)
                return NULL;
 
        }
+
+       // 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);
+       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.");
+               tbm_module_unload(gBufMgr->module);
+               _tbm_set_last_result(error);
+               free(gBufMgr);
+               gBufMgr = NULL;
+               pthread_mutex_unlock(&gLock);
+               return NULL;
+       }
+
+       if (!(gBufMgr->capabilities & TBM_BUFMGR_CAPABILITY_SHARE_FD)) {
+               TBM_ERR("The capabilities of the backend module had no TBM_BUFMGR_CAPABILITY_SHARE_FD.");
+               TBM_ERR("The tbm backend has to get TBM_BUFMGR_CAPABILITY_SHARE_FD. ");
+               tbm_module_unload(gBufMgr->module);
+               _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
+               free(gBufMgr);
+               gBufMgr = NULL;
+               pthread_mutex_unlock(&gLock);
+               return NULL;
+       }
+
        /* LCOV_EXCL_STOP */
 
        gBufMgr->ref_count = 1;
@@ -345,6 +436,9 @@ 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);
 
@@ -724,72 +818,24 @@ _tbm_bufmgr_get_bufmgr(void)
 int
 tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *native_display)
 {
-       int ret;
        tbm_error_e error;
 
        _tbm_bufmgr_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
 
-       TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
-
-       if (bufmgr->use_hal_tbm) {
-               if (hal_tbm_backend_has_drm_device(bufmgr->hal_backend, &ret)) {
-                       int fd = tbm_drm_helper_get_fd(); // this must be the auth drm_fd.(master drm_fd);
-                       if (fd < -1) {
-                               TBM_ERR("error: tbm_bufmgr(%p) native_display(%p)\n", bufmgr, native_display);
-                               _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                               _tbm_bufmgr_mutex_unlock();
-                               return 0;
-                       }
-
-                       // make the wayland server socket for sending the authenticated drm_fd to wayland clients.
-                       if (!tbm_drm_helper_wl_auth_server_init(native_display, fd, NULL, 0)) {
-                               TBM_ERR("error: tbm_drm_helper_wl_auth_server_init failed\n", bufmgr, native_display);
-                               close(fd);
-                               _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                               _tbm_bufmgr_mutex_unlock();
-                               return 0;
-                       }
-                       TBM_INFO("tbm creates a wayland socket for authentication of drm_fd.");
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), 0);
 
-                       bufmgr->auth_wl_socket_created = 1;
-                       bufmgr->auth_fd = fd;
-               }
-       } else if (bufmgr->backend_module_data) {
-               if (!bufmgr->bufmgr_func->bufmgr_bind_native_display) {
-                       TBM_WRN("skip: tbm_bufmgr(%p) native_display(%p)\n",
-                                       bufmgr, native_display);
-                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
-                       _tbm_bufmgr_mutex_unlock();
-                       return 1;
-               }
+       error = tbm_module_bufmgr_bind_native_display(bufmgr->module, native_display);
+       if (error != TBM_ERROR_NONE) {
+               _tbm_set_last_result(error);
+               _tbm_bufmgr_mutex_unlock();
 
-               error = bufmgr->bufmgr_func->bufmgr_bind_native_display(bufmgr->bufmgr_data, (tbm_native_display *)native_display);
-               if (error != TBM_ERROR_NONE) {
-                       TBM_ERR("error: tbm_bufmgr(%p) native_display(%p) error(%d)\n",
-                                       bufmgr, native_display, error);
-                       _tbm_set_last_result(error);
-                       _tbm_bufmgr_mutex_unlock();
-                       return 0;
-               }
-               ret = 1;
-       } else {
-               if (!bufmgr->backend->bufmgr_bind_native_display) {
-                       TBM_WRN("skip: tbm_bufmgr(%p) native_display(%p)\n",
-                                       bufmgr, native_display);
-                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
-                       _tbm_bufmgr_mutex_unlock();
+               if (error == TBM_ERROR_NOT_SUPPORTED) {
+                       TBM_WRN("Not supported, so skip: tbm_bufmgr(%p) native_display(%p)", bufmgr, native_display);
                        return 1;
                }
 
-               ret = bufmgr->backend->bufmgr_bind_native_display(bufmgr, native_display);
-               if (!ret) {
-                       TBM_ERR("error: tbm_bufmgr(%p) native_display(%p)\n",
-                                       bufmgr, native_display);
-                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                       _tbm_bufmgr_mutex_unlock();
-                       return 0;
-               }
+               return 0;
        }
 
        TBM_INFO("tbm_bufmgr(%p) native_display(%p)\n", bufmgr, native_display);
@@ -844,4 +890,72 @@ tbm_bufmgr tbm_bufmgr_get(void)
 {
        return gBufMgr;
 }
+
+
+tbm_bo
+tbm_bufmgr_internal_find_bo(tbm_bufmgr bufmgr, tbm_bo bo)
+{
+       tbm_bo bo2 = NULL;
+
+       TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), NULL);
+       TBM_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, NULL);
+
+       if (LIST_IS_EMPTY(&bufmgr->bo_list))
+               return NULL;
+
+       LIST_FOR_EACH_ENTRY(bo2, &bufmgr->bo_list, item_link) {
+               if (bo2->bo_data == bo->bo_data) {
+                       return bo2;
+               }
+       }
+
+       return NULL;
+}
+
+/* LCOV_EXCL_START */
+
+tbm_bo
+tbm_bufmgr_internal_alloc_bo_with_format(tbm_bufmgr bufmgr, int format, int bo_idx, int width,
+                                               int height, int bpp, tbm_bo_memory_type flags, tbm_error_e *error)
+{
+       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);
+
+       _tbm_bufmgr_check_bo_cnt(bufmgr);
+
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("memory allocationc failed.");
+               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+               /* 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);
+       if (!bo->bo_data) {
+               /* LCOV_EXCL_START */
+               _tbm_set_last_result(*error);
+               /* LCOV_EXCL_STOP */
+               goto fail;
+       }
+
+       _tbm_bufmgr_initialize_bo(bufmgr, bo, flags);
+
+       _tbm_bufmgr_mutex_unlock();
+
+       return bo;
+
+fail:
+       if (bo)
+               free(bo);
+       _tbm_bufmgr_mutex_unlock();
+
+       return NULL;
+}
+
 /* LCOV_EXCL_STOP */