tbm_module: change tbm_backend_surface_data into tbm_surface_data
[platform/core/uifw/libtbm.git] / src / tbm_module.c
index 6e99845..276098c 100644 (file)
@@ -460,7 +460,7 @@ tbm_module_unload(tbm_module *module)
 }
 
 int
-tbm_module_bufmgr_get_capabilities(tbm_module *module, tbm_error_e *error)
+tbm_module_get_capabilities(tbm_module *module, tbm_error_e *error)
 {
        int capabilities = 0;
        tbm_backend_bufmgr_func *bufmgr_func = NULL;
@@ -496,7 +496,7 @@ tbm_module_bufmgr_get_capabilities(tbm_module *module, tbm_error_e *error)
 }
 
 tbm_error_e
-tbm_module_bufmgr_bind_native_display(tbm_module *module, void *native_display)
+tbm_module_bind_native_display(tbm_module *module, void *native_display)
 {
        tbm_error_e error = TBM_ERROR_NONE;
        tbm_backend_bufmgr_func *bufmgr_func = NULL;
@@ -559,7 +559,7 @@ tbm_module_bufmgr_bind_native_display(tbm_module *module, void *native_display)
 }
 
 tbm_error_e
-tbm_module_bufmgr_get_supported_formats(tbm_module *module, uint32_t **formats, uint32_t *num)
+tbm_module_get_supported_formats(tbm_module *module, uint32_t **formats, uint32_t *num)
 {
        tbm_error_e error = TBM_ERROR_NONE;
        tbm_backend_bufmgr_func *bufmgr_func = NULL;
@@ -602,7 +602,7 @@ tbm_module_bufmgr_get_supported_formats(tbm_module *module, uint32_t **formats,
 }
 
 tbm_error_e
-tbm_module_bufmgr_get_plane_data(tbm_module *module, int format, int plane_idx, uint32_t w, uint32_t h,
+tbm_module_get_plane_data(tbm_module *module, int format, int plane_idx, uint32_t w, uint32_t h,
                                        uint32_t *size, uint32_t *offset, uint32_t *pitch, int *bo_idx)
 {
        tbm_error_e error = TBM_ERROR_NONE;
@@ -644,8 +644,111 @@ tbm_module_bufmgr_get_plane_data(tbm_module *module, int format, int plane_idx,
        return error;
 }
 
+int
+tbm_module_support_surface_data(tbm_module *module)
+{
+       tbm_error_e error = TBM_ERROR_NONE;
+       tbm_surface_data *surface_data = NULL;
+
+       static int tbm_module_check_support_surface_data = 0;
+
+       TBM_RETURN_VAL_IF_FAIL(module, 0);
+
+       // check once support_surface_data or not.
+       if (tbm_module_check_support_surface_data) {
+               // return the value which already set.
+               return module->support_surface_data;
+       }
+
+       // check this only once
+       tbm_module_check_support_surface_data = 1;
+
+       if (module->type != TBM_MODULE_TYPE_HAL_TBM)
+               goto done;
+
+       // Assume that the hal-tbm supports the hal surface apis if tbm_module_alloc_surface_data succeed.
+       surface_data = tbm_module_alloc_surface_data(module, 10, 10, TBM_FORMAT_ARGB8888, TBM_BO_DEFAULT, &error);
+       if (!surface_data)
+               goto done;
+       free(surface_data);
+
+       module->support_surface_data = 1;
+
+done:
+       return module->support_surface_data;
+}
+
+
+tbm_surface_data *
+tbm_module_alloc_surface_data(tbm_module *module, int width, int height, int format, int flags, tbm_error_e *error)
+{
+       tbm_surface_data *surface_data = NULL;
+
+       TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+       TBM_RETURN_VAL_SET_ERR_IF_FAIL(module->type == TBM_MODULE_TYPE_HAL_TBM, NULL, *error, TBM_ERROR_INVALID_OPERATION);
+
+       surface_data = calloc(1, sizeof(struct _tbm_surface_data));
+       if (!surface_data) {
+               TBM_ERR("memory allocation failed.");
+               *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+       }
+
+       surface_data->hal_surface = hal_tbm_bufmgr_alloc_surface(module->hal_bufmgr,
+                                                                                          (uint32_t)width,
+                                                                                          (uint32_t)height,
+                                                                                          (hal_tbm_format)format,
+                                                                                          (hal_tbm_bo_memory_type)flags,
+                                                                                          NULL,
+                                                                                          0,
+                                                                                          (hal_tbm_error *)error);
+       if (!surface_data->hal_surface) {
+               TBM_ERR("hal_tbm_bufmgr_alloc_surface failed.");
+               *error = TBM_ERROR_INVALID_OPERATION;
+               free(surface_data);
+               return NULL;
+       }
+
+       surface_data->module = module;
+
+       return surface_data;
+}
+
+tbm_surface_data *
+tbm_module_import_surface_data(tbm_module *module, int width, int height, int format, tbm_surface_buffer_data *buffer_data, tbm_error_e *error)
+{
+       tbm_surface_data *surface_data = NULL;
+
+       TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+       TBM_RETURN_VAL_SET_ERR_IF_FAIL(module->type == TBM_MODULE_TYPE_HAL_TBM, NULL, *error, TBM_ERROR_INVALID_OPERATION);
+
+       surface_data = calloc(1, sizeof(struct _tbm_surface_data));
+       if (!surface_data) {
+               TBM_ERR("memory allocation failed.");
+               *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+       }
+
+       surface_data->hal_surface = hal_tbm_bufmgr_import_surface(module->hal_bufmgr,
+                                                                                       (uint32_t)width,
+                                                                                       (uint32_t)height,
+                                                                                       (hal_tbm_format)format,
+                                                                                       (hal_tbm_surface_buffer_data *)buffer_data,
+                                                                                       (hal_tbm_error *)error);
+       if (!surface_data->hal_surface) {
+               TBM_ERR("hal_tbm_bufmgr_import_surface failed. width:%d height:%d format:%d error:%s",
+                               width, height, format, tbm_error_str(*error));
+               free(surface_data);
+               return NULL;
+       }
+
+       surface_data->module = module;
+
+       return surface_data;
+}
+
 tbm_backend_bo_data *
-tbm_module_bufmgr_bo_alloc(tbm_module *module, tbm_bo bo, int size, int flags, tbm_error_e *error)
+tbm_module_alloc_bo_data(tbm_module *module, tbm_bo bo, int size, int flags, tbm_error_e *error)
 {
        tbm_backend_bo_data *bo_data = NULL;
        tbm_backend_bufmgr_func *bufmgr_func = NULL;
@@ -685,7 +788,7 @@ tbm_module_bufmgr_bo_alloc(tbm_module *module, tbm_bo bo, int size, int flags, t
 }
 
 tbm_backend_bo_data *
-tbm_module_bufmgr_bo_alloc_with_format(tbm_module *module, int format, int bo_idx, int width,
+tbm_module_alloc_bo_data_with_format(tbm_module *module, int format, int bo_idx, int width,
                                                int height, int bpp, tbm_bo_memory_type flags, tbm_error_e *error)
 {
        tbm_backend_bo_data *bo_data = NULL;
@@ -703,7 +806,10 @@ tbm_module_bufmgr_bo_alloc_with_format(tbm_module *module, int format, int bo_id
        case TBM_MODULE_TYPE_TBM_BACKEND:
                bufmgr_func = module->bufmgr_func;
                TBM_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr_func, NULL, *error, TBM_ERROR_INVALID_OPERATION);
-               TBM_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr_func->bufmgr_alloc_bo_with_format, NULL, *error, TBM_ERROR_NOT_SUPPORTED);
+               if (!bufmgr_func->bufmgr_alloc_bo_with_format) {
+                       *error = TBM_ERROR_NOT_SUPPORTED;
+                       return NULL;
+               }
 
                bo_data = bufmgr_func->bufmgr_alloc_bo_with_format(module->bufmgr_data, format, bo_idx, width, height, flags, error);
                break;
@@ -725,7 +831,7 @@ tbm_module_bufmgr_bo_alloc_with_format(tbm_module *module, int format, int bo_id
 
 
 tbm_backend_bo_data *
-tbm_module_bufmgr_bo_import_fd(tbm_module *module, tbm_bo bo, tbm_fd fd, tbm_error_e *error)
+tbm_module_import_bo_data_with_fd(tbm_module *module, tbm_bo bo, tbm_fd fd, tbm_error_e *error)
 {
        tbm_backend_bo_data *bo_data = NULL;
        tbm_backend_bufmgr_func *bufmgr_func = NULL;
@@ -768,7 +874,7 @@ tbm_module_bufmgr_bo_import_fd(tbm_module *module, tbm_bo bo, tbm_fd fd, tbm_err
 }
 
 tbm_backend_bo_data *
-tbm_module_bufmgr_bo_import_key(tbm_module *module, tbm_bo bo, tbm_key key, tbm_error_e *error)
+tbm_module_import_bo_data_with_key(tbm_module *module, tbm_bo bo, tbm_key key, tbm_error_e *error)
 {
        tbm_backend_bo_data *bo_data = NULL;
        tbm_backend_bufmgr_func *bufmgr_func = NULL;
@@ -811,7 +917,65 @@ tbm_module_bufmgr_bo_import_key(tbm_module *module, tbm_bo bo, tbm_key key, tbm_
 }
 
 void
-tbm_module_bo_free(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, int get_from_hal_surface)
+tbm_surface_data_free(tbm_surface_data *surface_data)
+{
+       TBM_RETURN_IF_FAIL(surface_data);
+       TBM_RETURN_IF_FAIL(surface_data->module);
+       TBM_RETURN_IF_FAIL(surface_data->module->type == TBM_MODULE_TYPE_HAL_TBM);
+
+       surface_data->module = NULL;
+
+       hal_tbm_surface_free(surface_data->hal_surface);
+       surface_data->hal_surface = NULL;
+
+       free(surface_data);
+}
+
+tbm_error_e
+tbm_surface_data_get_plane_data(tbm_surface_data *surface_data, int plane_idx, uint32_t *size, uint32_t *offset, uint32_t *pitch, int *bo_idx)
+{
+       tbm_error_e error;
+
+       TBM_RETURN_VAL_IF_FAIL(surface_data, TBM_ERROR_INVALID_PARAMETER);
+       TBM_RETURN_VAL_IF_FAIL(surface_data->hal_surface, TBM_ERROR_NOT_SUPPORTED);
+
+       error = (tbm_error_e)hal_tbm_surface_get_plane_data(surface_data->hal_surface, plane_idx, size, offset, pitch, bo_idx);
+       TBM_RETURN_VAL_IF_FAIL(error == TBM_ERROR_NONE, error);
+
+       return TBM_ERROR_NONE;
+}
+
+tbm_backend_bo_data **
+tbm_surface_data_get_bo_data_array(tbm_surface_data *surface_data, int *num_bos, tbm_error_e *error)
+{
+       hal_tbm_bo **hal_bos = NULL;
+
+       TBM_RETURN_VAL_SET_ERR_IF_FAIL(surface_data, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+       TBM_RETURN_VAL_SET_ERR_IF_FAIL(surface_data->hal_surface, NULL, *error, TBM_ERROR_NOT_SUPPORTED);
+
+       hal_bos = hal_tbm_surface_get_bos(surface_data->hal_surface, num_bos, (hal_tbm_error *)error);
+       TBM_RETURN_VAL_IF_FAIL(hal_bos, NULL);
+
+       return (tbm_backend_bo_data **)hal_bos;
+}
+
+tbm_surface_buffer_data *
+tbm_surface_data_export(tbm_surface_data *surface_data, tbm_error_e *error)
+{
+       tbm_surface_buffer_data *buffer_data;
+
+       TBM_RETURN_VAL_SET_ERR_IF_FAIL(surface_data, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+       TBM_RETURN_VAL_SET_ERR_IF_FAIL(surface_data->hal_surface, NULL, *error, TBM_ERROR_NOT_SUPPORTED);
+
+       buffer_data = (tbm_surface_buffer_data *)hal_tbm_surface_export((hal_tbm_surface *)surface_data->hal_surface,
+                                                                                                                                       (hal_tbm_error *)error);
+       TBM_RETURN_VAL_IF_FAIL(buffer_data, NULL);
+
+       return buffer_data;
+}
+
+void
+tbm_module_bo_free(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, int get_from_surface_data)
 {
        tbm_backend_bo_func *bo_func = NULL;
        tbm_bufmgr_backend backend = NULL;
@@ -823,7 +987,7 @@ tbm_module_bo_free(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data,
        switch (module->type) {
        case TBM_MODULE_TYPE_HAL_TBM:
                // call hal_tbm_bo_free when bo is created by tbm_bo_alloc api.
-               if (get_from_hal_surface)
+               if (!get_from_surface_data)
                        hal_tbm_bo_free(bo_data);
                break;
 /* LCOV_EXCL_START */
@@ -937,7 +1101,7 @@ tbm_module_bo_get_handle(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_
 {
        tbm_backend_bo_func *bo_func = NULL;
        tbm_bufmgr_backend backend = NULL;
-       tbm_bo_handle bo_handle;
+       tbm_bo_handle bo_handle = (tbm_bo_handle)NULL;
        hal_tbm_bo_handle hbo_handle;
 
        TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, (tbm_bo_handle)NULL, *error, TBM_ERROR_INVALID_PARAMETER);
@@ -984,7 +1148,7 @@ tbm_module_bo_map(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, i
 {
        tbm_backend_bo_func *bo_func = NULL;
        tbm_bufmgr_backend backend = NULL;
-       tbm_bo_handle bo_handle;
+       tbm_bo_handle bo_handle = (tbm_bo_handle)NULL;
        hal_tbm_bo_handle hbo_handle;
 
        TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, (tbm_bo_handle)NULL, *error, TBM_ERROR_INVALID_PARAMETER);
@@ -1054,7 +1218,7 @@ tbm_module_bo_unmap(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data)
                TBM_RETURN_VAL_IF_FAIL(backend, TBM_ERROR_INVALID_OPERATION);
                TBM_RETURN_VAL_IF_FAIL(backend->bo_unmap, TBM_ERROR_NOT_SUPPORTED);
 
-               ret = bo->bufmgr->backend->bo_unmap(bo);
+               ret = backend->bo_unmap(bo);
                if (!ret)
                        error = TBM_ERROR_INVALID_OPERATION;
                else
@@ -1201,3 +1365,47 @@ tbm_module_bo_export_fd(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_d
 
        return fd;
 }
+
+tbm_key
+tbm_module_bo_export_key(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, tbm_error_e *error)
+{
+       tbm_backend_bo_func *bo_func = NULL;
+       tbm_bufmgr_backend backend = NULL;
+       tbm_key ret;
+
+       TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, 0, *error, TBM_ERROR_INVALID_PARAMETER);
+
+       switch (module->type) {
+       case TBM_MODULE_TYPE_HAL_TBM:
+               ret = (hal_tbm_fd)hal_tbm_bo_export_key((hal_tbm_bo *)bo_data, (hal_tbm_error *)error);
+               break;
+/* LCOV_EXCL_START */
+       case TBM_MODULE_TYPE_TBM_BACKEND:
+               bo_func = module->bo_func;
+               TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func, 0, *error, TBM_ERROR_INVALID_OPERATION);
+               TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func->bo_export_key, 0, *error, TBM_ERROR_NOT_SUPPORTED);
+
+               ret = bo_func->bo_export_key(bo_data, error);
+               break;
+       case TBM_MODULE_TYPE_BUFMGR_BACKEND:
+               TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
+               backend = module->backend;
+               TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend, 0, *error, TBM_ERROR_INVALID_OPERATION);
+               TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend->bo_export, 0, *error, TBM_ERROR_NOT_SUPPORTED);
+
+               ret = backend->bo_export(bo);
+               if (!ret)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               else
+                       *error = TBM_ERROR_NONE;
+               break;
+       default:
+               TBM_ERR("Wrong module type:%d", module->type);
+               ret = -1;
+               *error = TBM_ERROR_INVALID_OPERATION;
+               break;
+/* LCOV_EXCL_STOP */
+       }
+
+       return ret;
+}