#include "tbm_bufmgr_backend.h"
#include "tbm_drm_helper.h"
+#define PREFIX_LIB "libtbm_"
+#define SUFFIX_LIB ".so"
+#define DEFAULT_LIB PREFIX_LIB"default"SUFFIX_LIB
+
+/* values to indicate unspecified fields in XF86ModReqInfo. */
+#define MAJOR_UNSPEC 0xFF
+#define MINOR_UNSPEC 0xFF
+#define PATCH_UNSPEC 0xFFFF
+#define ABI_VERS_UNSPEC 0xFFFFFFFF
+
+#define MODULE_VERSION_NUMERIC(maj, min, patch) \
+ ((((maj) & 0xFF) << 24) | (((min) & 0xFF) << 16) | (patch & 0xFFFF))
+#define GET_MODULE_MAJOR_VERSION(vers) (((vers) >> 24) & 0xFF)
+#define GET_MODULE_MINOR_VERSION(vers) (((vers) >> 16) & 0xFF)
+#define GET_MODULE_PATCHLEVEL(vers) ((vers) & 0xFFFF)
+
+/* LCOV_EXCL_START */
+
+static int
+_tbm_backend_load_hal_tbm(tbm_module *module)
+{
+ hal_tbm_backend *hal_backend = NULL;
+ hal_tbm_bufmgr *hal_bufmgr;
+ hal_tbm_error ret = HAL_TBM_ERROR_NONE;
+ hal_tbm_fd auth_drm_fd = -1;
+
+ hal_backend = hal_tbm_get_backend(&ret);
+ if (hal_backend == NULL || ret != HAL_TBM_ERROR_NONE) {
+ TBM_ERR("get backend fail");
+ return 0;
+ }
+
+ hal_bufmgr = hal_tbm_backend_get_bufmgr(hal_backend, &ret);
+ if (hal_bufmgr == NULL || ret != HAL_TBM_ERROR_NONE) {
+ TBM_ERR("get hal_bufmgr fail");
+ goto get_backend_fail;
+ }
+
+ if (hal_tbm_backend_has_drm_device(hal_backend, &ret)) {
+ auth_drm_fd = hal_tbm_backend_get_master_drm_fd(hal_backend, &ret);
+ if (auth_drm_fd < 0) {
+ TBM_INFO("tbm_backend has no master drm_fd.");
+
+ auth_drm_fd = tbm_drm_helper_get_master_fd();
+ if (auth_drm_fd < 0) {
+ TBM_INFO("libtbm requests an authenticated drm_fd to a process(display server).");
+ if (!tbm_drm_helper_get_auth_info(&auth_drm_fd, NULL, NULL)) {
+ TBM_ERR("get auth drm_fd fail");
+ goto get_backend_fail;
+ }
+ } else {
+ TBM_INFO("libtbm gets a master drm_fd from libtdm via tbm_drm_helper.");
+ }
+
+ TBM_INFO("libtbm sends a master drm_fd as an authentiated drm_fd to tbm_backend.");
+ ret = hal_tbm_backend_set_authenticated_drm_fd(hal_backend, auth_drm_fd);
+ if (ret != HAL_TBM_ERROR_NONE) {
+ TBM_ERR("hal_tbm_backend_set_authenticated_drm_fd failed.");
+ goto get_backend_fail;
+ }
+ } else {
+ TBM_INFO("tbm_backend has a master drm_fd.");
+
+ tbm_drm_helper_set_tbm_master_fd(auth_drm_fd);
+ }
+ tbm_drm_helper_set_fd(auth_drm_fd);
+ }
+
+ module->hal_backend = hal_backend;
+ module->hal_bufmgr = hal_bufmgr;
+
+ module->use_hal_tbm = 1;
+
+ TBM_INFO("use HAL-TBM_API");
+
+ return 1;
+
+get_backend_fail:
+ if (auth_drm_fd >= 0)
+ close(auth_drm_fd);
+ hal_tbm_put_backend(hal_backend);
+ return 0;
+}
+
+static int
+_check_version(TBMModuleVersionInfo *data)
+{
+ int backend_module_major, backend_module_minor;
+ int tbm_backend_major, tbm_backend_minor;
+
+ backend_module_major = GET_ABI_MAJOR(data->abiversion);
+ backend_module_minor = GET_ABI_MINOR(data->abiversion);
+
+ TBM_DBG("TBM module %s: vendor=\"%s\" ABI=%d,%d\n",
+ data->modname ? data->modname : "UNKNOWN!",
+ data->vendor ? data->vendor : "UNKNOWN!", backend_module_major, backend_module_minor);
+
+ tbm_backend_major = GET_ABI_MAJOR(TBM_ABI_VERSION);
+ tbm_backend_minor = GET_ABI_MINOR(TBM_ABI_VERSION);
+
+ TBM_DBG("TBM ABI version %d.%d\n",
+ tbm_backend_major, tbm_backend_minor);
+
+ if (backend_module_major != tbm_backend_major) {
+ TBM_ERR("TBM module ABI major ver(%d) doesn't match the TBM's ver(%d)\n",
+ backend_module_major, tbm_backend_major);
+ return 0;
+ } else if (backend_module_minor > tbm_backend_minor) {
+ TBM_ERR("TBM module ABI minor ver(%d) is newer than the TBM's ver(%d)\n",
+ backend_module_minor, tbm_backend_minor);
+ return 0;
+ }
+
+ return 1;
+}
+
+static int
+_tbm_backend_check_bufmgr_func(tbm_backend_bufmgr_func *bufmgr_func)
+{
+ TBM_RETURN_VAL_IF_FAIL(bufmgr_func, 0); /* mandatory symbol */
+ TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_get_capabilities, 0); /* mandatory symbol */
+ TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_bind_native_display, 0); /* mandatory symbol */
+ TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_get_supported_formats, 0); /* mandatory symbol */
+ TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_get_plane_data, 0); /* mandatory symbol */
+ TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_alloc_bo, 0); /* mandatory symbol */
+ if (!bufmgr_func->bufmgr_alloc_bo_with_format)
+ TBM_DBG("No bufmgr_func->bufmgr_alloc_bo_with_format.");
+ TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_import_fd, 0); /* mandatory symbol */
+ if (!bufmgr_func->bufmgr_import_key)
+ TBM_DBG("No bufmgr_func->bo_export_key.");
+
+ return 1;
+}
+
+static int
+_tbm_backend_check_bufmgr_bo(tbm_backend_bo_func *bo_func)
+{
+ TBM_RETURN_VAL_IF_FAIL(bo_func, 0); /* mandatory symbol */
+ TBM_RETURN_VAL_IF_FAIL(bo_func->bo_free, 0); /* mandatory symbol */
+ TBM_RETURN_VAL_IF_FAIL(bo_func->bo_get_size, 0); /* mandatory symbol */
+ TBM_RETURN_VAL_IF_FAIL(bo_func->bo_get_memory_types, 0); /* mandatory symbol */
+ TBM_RETURN_VAL_IF_FAIL(bo_func->bo_get_handle, 0); /* mandatory symbol */
+ TBM_RETURN_VAL_IF_FAIL(bo_func->bo_map, 0); /* mandatory symbol */
+ TBM_RETURN_VAL_IF_FAIL(bo_func->bo_unmap, 0); /* mandatory symbol */
+ if (!bo_func->bo_lock)
+ TBM_DBG("No bo_func->bo_lock.");
+ if (!bo_func->bo_unlock)
+ TBM_DBG("No bo_func->bo_unlock.");
+ TBM_RETURN_VAL_IF_FAIL(bo_func->bo_export_fd, 0); /* mandatory symbol */
+ if (!bo_func->bo_export_key)
+ TBM_INFO("No bo_func->bo_export_key.");
+
+ return 1;
+}
+
+static int
+_tbm_backend_load_module(tbm_module *module, const char *file)
+{
+ char path[PATH_MAX] = {0, };
+ void *module_data = NULL;
+ tbm_backend_module *backend_module_data = NULL;
+ tbm_backend_bufmgr_data *bufmgr_data = NULL;
+ int backend_module_major, backend_module_minor;
+ int tbm_backend_major, tbm_backend_minor;
+ tbm_error_e error;
+
+ snprintf(path, sizeof(path), BUFMGR_MODULE_DIR "/%s", file);
+
+ module_data = dlopen(path, RTLD_LAZY);
+ if (!module_data) {
+ TBM_ERR("failed to load module: %s(%s)\n", dlerror(), file);
+ return 0;
+ }
+
+ backend_module_data = dlsym(module_data, "tbm_backend_module_data");
+ if (!backend_module_data) {
+ TBM_ERR("Error: module does not have data object.\n");
+ goto err;
+ }
+
+ tbm_backend_major = GET_ABI_MAJOR(TBM_BACKEND_ABI_LATEST_VERSION);
+ tbm_backend_minor = GET_ABI_MINOR(TBM_BACKEND_ABI_LATEST_VERSION);
+ TBM_INFO("TBM Backend ABI version %d.%d\n", tbm_backend_major, tbm_backend_minor);
+
+ backend_module_major = GET_ABI_MAJOR(backend_module_data->abi_version);
+ backend_module_minor = GET_ABI_MINOR(backend_module_data->abi_version);
+
+ TBM_INFO("TBM module %s: vendor=\"%s\" Backend ABI version=%d.%d\n",
+ backend_module_data->name ? backend_module_data->name : "UNKNOWN!",
+ backend_module_data->vendor ? backend_module_data->vendor : "UNKNOWN!",
+ backend_module_major, backend_module_minor);
+
+ if (backend_module_major > tbm_backend_major) {
+ TBM_ERR("TBM module ABI major ver(%d) is newer than the TBM's ver(%d)\n",
+ backend_module_major, tbm_backend_major);
+ goto err;
+ } else if (backend_module_minor > tbm_backend_minor) {
+ TBM_ERR("TBM module ABI minor ver(%d) is newer than the TBM's ver(%d)\n",
+ backend_module_minor, tbm_backend_minor);
+ goto err;
+ }
+
+ if (!backend_module_data->init) {
+ TBM_ERR("Error: module does not supply init symbol.\n");
+ goto err;
+ }
+
+ if (!backend_module_data->deinit) {
+ TBM_ERR("Error: module does not supply deinit symbol.\n");
+ goto err;
+ }
+
+ bufmgr_data = backend_module_data->init((tbm_bufmgr)module, &error);
+ if (!bufmgr_data) {
+ TBM_ERR("Fail to init module(%s)\n", file);
+ goto err;
+ }
+
+ /* check the mandatory symbols of the backend module */
+ if (!_tbm_backend_check_bufmgr_func(module->bufmgr_func)) {
+ TBM_ERR("Fail to check the bufmgr_func symboles.");
+ goto err;
+ }
+
+ if (!_tbm_backend_check_bufmgr_bo(module->bo_func)) {
+ TBM_ERR("Fail to check the bufmgr_bo symboles.");
+ goto err;
+ }
+
+ module->module_data = module_data;
+ module->backend_module_data = backend_module_data;
+ module->bufmgr_data = bufmgr_data;
+
+ TBM_INFO("Success to load module(%s)\n", file);
+
+ return 1;
+
+err:
+ if (bufmgr_data)
+ module->backend_module_data->deinit(bufmgr_data);
+ if (module_data)
+ dlclose(module_data);
+
+ return 0;
+}
+
+static int
+_tbm_backend_load_bufmgr_module(tbm_module *module, int fd, const char *file)
+{
+ char path[PATH_MAX] = {0, };
+ TBMModuleVersionInfo *vers;
+ TBMModuleData *initdata;
+ ModuleInitProc init;
+ void *module_data;
+
+ snprintf(path, sizeof(path), BUFMGR_MODULE_DIR "/%s", file);
+
+ module_data = dlopen(path, RTLD_LAZY);
+ if (!module_data) {
+ TBM_ERR("failed to load module: %s(%s)\n", dlerror(), file);
+ return 0;
+ }
+
+ initdata = dlsym(module_data, "tbmModuleData");
+ if (!initdata) {
+ TBM_ERR("Error: module does not have data object.\n");
+ goto err;
+ }
+
+ vers = initdata->vers;
+ if (!vers) {
+ TBM_ERR("Error: module does not supply version information.\n");
+ goto err;
+ }
+
+ init = initdata->init;
+ if (!init) {
+ TBM_ERR("Error: module does not supply init symbol.\n");
+ goto err;
+ }
+
+ if (!_check_version(vers)) {
+ TBM_ERR("Fail to check version.\n");
+ goto err;
+ }
+
+ if (!init((tbm_bufmgr)module, fd)) {
+ TBM_ERR("Fail to init module(%s)\n", file);
+ goto err;
+ }
+
+ if (!module->backend || !module->backend->priv) {
+ TBM_ERR("Error: module(%s) wrong operation. Check backend or backend's priv.\n", file);
+ goto err;
+ }
+
+ module->module_data = module_data;
+
+ TBM_DBG("Success to load module(%s)\n", file);
+
+ return 1;
+
+err:
+ dlclose(module_data);
+ return 0;
+}
+
+/* LCOV_EXCL_STOP */
+
tbm_module *
-tbm_module_load(int fd)
+tbm_module_load(tbm_bufmgr bufmgr, int fd)
{
- // TODO:
- return NULL;
+ tbm_module *module;
+ struct dirent **namelist;
+ int ret = 0, n;
+
+ module = calloc(1, sizeof(struct _tbm_module));
+ if (!module) {
+ TBM_ERR("fail to allocate the memory");
+ return NULL;
+ }
+ module->bufmgr = bufmgr;
+
+ /* try to load the hal-tbm backend module */
+ ret = _tbm_backend_load_hal_tbm(module);
+ if (ret) {
+ module->type = TBM_MODULE_TYPE_HAL_TBM;
+ goto done;
+ }
+
+ /* try to load the new backend module */
+ ret = _tbm_backend_load_module(module, DEFAULT_LIB);
+ if (ret) {
+ module->type = TBM_MODULE_TYPE_TBM_BACKEND;
+ goto done;
+ }
+
+ /* try to load the old(deprecated) backend mdoule */
+ ret = _tbm_backend_load_bufmgr_module(module, fd, DEFAULT_LIB);
+ if (ret) {
+ module->type = TBM_MODULE_TYPE_BUFMGR_BACKEND;
+ return module;
+ }
+
+ /* load bufmgr priv from configured path */
+ n = scandir(BUFMGR_MODULE_DIR, &namelist, 0, alphasort);
+ if (n < 0) {
+ TBM_ERR("no files : %s\n", BUFMGR_MODULE_DIR);
+ tbm_module_unload(module);
+ return NULL;
+ }
+
+ while (n--) {
+ if (!ret && strstr(namelist[n]->d_name, PREFIX_LIB)) {
+ const char *p = strstr(namelist[n]->d_name, SUFFIX_LIB);
+
+ if (p && !strcmp(p, SUFFIX_LIB)) {
+ ret = _tbm_backend_load_module(module, namelist[n]->d_name);
+ if (ret)
+ module->type = TBM_MODULE_TYPE_TBM_BACKEND;
+ else {
+ ret = _tbm_backend_load_bufmgr_module(module, fd, namelist[n]->d_name);
+ module->type = TBM_MODULE_TYPE_BUFMGR_BACKEND;
+ }
+ }
+ }
+
+ free(namelist[n]);
+ }
+
+ free(namelist);
+
+ if (!ret) {
+ free(module);
+ module = NULL;
+ }
+
+done:
+ return module;
}
void
tbm_module_unload(tbm_module *module)
{
- // TODO:
+ switch (module->type) {
+ case TBM_MODULE_TYPE_HAL_TBM:
+ if (module->auth_wl_socket_created) {
+ tbm_drm_helper_wl_auth_server_deinit();
+ close(module->auth_fd);
+ }
+ tbm_drm_helper_unset_tbm_master_fd();
+ tbm_drm_helper_unset_fd();
+
+ hal_tbm_put_backend(module->hal_backend);
+ module->hal_backend = NULL;
+ module->hal_bufmgr = NULL;
+ module->use_hal_tbm = 0;
+ break;
+ case TBM_MODULE_TYPE_TBM_BACKEND:
+ module->backend_module_data->deinit(module->bufmgr_data);
+ module->bo_func = NULL;
+ module->bufmgr_func = NULL;
+ module->bufmgr_data = NULL;
+ module->backend_module_data = NULL;
+
+ dlclose(module->module_data);
+ break;
+ case TBM_MODULE_TYPE_BUFMGR_BACKEND:
+ module->backend->bufmgr_deinit(module->backend->priv);
+ module->backend->priv = NULL;
+ tbm_backend_free(module->backend);
+ module->backend = NULL;
+
+ dlclose(module->module_data);
+ break;
+ default:
+ TBM_ERR("Wrong module type:%d", module->type);
+ break;
+ }
+
+ module->bufmgr = NULL;
+
+ free(module);
}
+int
+tbm_module_bufmgr_get_capabilities(tbm_module *module)
+{
+ tbm_error_e error = TBM_ERROR_NOT_SUPPORTED;
+ int capabilities = 0;
+
+ switch (module->type) {
+ case TBM_MODULE_TYPE_HAL_TBM:
+ capabilities = hal_tbm_bufmgr_get_capabilities(module->hal_bufmgr, (hal_tbm_error *)&error);
+ break;
+ case TBM_MODULE_TYPE_TBM_BACKEND:
+ capabilities = module->bufmgr_func->bufmgr_get_capabilities(module->bufmgr_data, &error);
+ break;
+ case TBM_MODULE_TYPE_BUFMGR_BACKEND:
+ TBM_ERR("Do not support at tbm_bufmgr_backend.");
+ break;
+ default:
+ TBM_ERR("Wrong module type:%d", module->type);
+ break;
+ }
+
+ if (error != TBM_ERROR_NONE) {
+ TBM_ERR("fail to get capabilities of bufmgr");
+ return 0;
+ }
+
+ return capabilities;
+}