From af367aeb130d7f2dc68c5e32a1b5ce1557b40ea5 Mon Sep 17 00:00:00 2001 From: SooChan Lim Date: Tue, 16 Feb 2021 19:41:25 +0900 Subject: [PATCH] hal: deal with auth drm_fd tbm backend must have the authenticated drm_fd when it has a drm_device. If master_fd set from libtdm exists, libtbm sets the authencated drm_fd to the backend. If there is no master_fd, libtbm get the authenticated drm_fd from the process(display server) which has a master fd. Change-Id: I0111976a26dc1f26191439517e5d8869a8955a36 Signed-off-by: Junkyeong Kim --- src/tbm_bufmgr.c | 75 +++++++++++++++++++++++++++++++++++++-------- src/tbm_bufmgr_int.h | 2 ++ src/tbm_drm_helper_server.c | 12 ++++++-- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/src/tbm_bufmgr.c b/src/tbm_bufmgr.c index c9872df..8182ccf 100644 --- a/src/tbm_bufmgr.c +++ b/src/tbm_bufmgr.c @@ -34,6 +34,7 @@ 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" #include @@ -191,6 +192,7 @@ _tbm_backend_load_hal_tbm(tbm_bufmgr bufmgr) hal_tbm_bufmgr_capability capability; 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) { @@ -204,6 +206,36 @@ _tbm_backend_load_hal_tbm(tbm_bufmgr bufmgr) 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); + } + capability = hal_tbm_bufmgr_get_capabilities(hal_bufmgr, &ret); if (ret != HAL_TBM_ERROR_NONE) { TBM_ERR("hal_tbm_bufmgr_get_capabilities fail."); @@ -227,11 +259,13 @@ _tbm_backend_load_hal_tbm(tbm_bufmgr bufmgr) bufmgr->use_hal_tbm = 1; - TBM_INFO("use hal tbm"); + 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; } @@ -694,6 +728,13 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr) } if (bufmgr->use_hal_tbm) { + if (bufmgr->auth_wl_socket_created) { + tbm_drm_helper_wl_auth_server_deinit(); + close(bufmgr->auth_fd); + tbm_drm_helper_unset_tbm_master_fd(); + } + tbm_drm_helper_unset_fd(); + hal_tbm_put_backend(bufmgr->hal_backend); bufmgr->hal_backend = NULL; bufmgr->hal_bufmgr = NULL; @@ -1104,17 +1145,27 @@ tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *native_display) TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); if (bufmgr->use_hal_tbm) { - error = (tbm_error_e)hal_tbm_bufmgr_bind_native_display(bufmgr->hal_bufmgr, (hal_tbm_native_display *)native_display); - if (error == TBM_ERROR_NOT_SUPPORTED) { - 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; - } else if (error != TBM_ERROR_NONE) { - 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; + 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."); + + bufmgr->auth_wl_socket_created = 1; + bufmgr->auth_fd = fd; } } else if (bufmgr->backend_module_data) { if (!bufmgr->bufmgr_func->bufmgr_bind_native_display) { diff --git a/src/tbm_bufmgr_int.h b/src/tbm_bufmgr_int.h index 9df1dd7..85a9be1 100644 --- a/src/tbm_bufmgr_int.h +++ b/src/tbm_bufmgr_int.h @@ -213,6 +213,8 @@ struct _tbm_bufmgr { tbm_backend_bo_func *bo_func; /* backend functions for bo */ int use_hal_tbm; /* use hal-api-tbm */ + int auth_wl_socket_created; /* create wayland socket for authenticated drm_fd */ + int auth_fd; hal_tbm_backend *hal_backend; /* hal-api-tbm backend */ hal_tbm_bufmgr *hal_bufmgr; /* hal-api-tbm bufmgr */ }; diff --git a/src/tbm_drm_helper_server.c b/src/tbm_drm_helper_server.c index f0f82f4..4be6616 100644 --- a/src/tbm_drm_helper_server.c +++ b/src/tbm_drm_helper_server.c @@ -248,7 +248,7 @@ _tbm_drm_auth_socket_init(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv) } int -tbm_drm_helper_wl_auth_server_init(void *wl_display, int fd, const char *device_name, uint32_t flags) +tbm_drm_helper_wl_auth_server_init(void *wl_display, int fd, const char *device_name, uint32_t flags) { if (!tbm_drm_auth_srv) { TBM_RETURN_VAL_IF_FAIL(wl_display != NULL, 0); @@ -257,7 +257,15 @@ tbm_drm_helper_wl_auth_server_init(void *wl_display, int fd, const char *devic TBM_RETURN_VAL_IF_FAIL(tbm_drm_auth_srv != NULL, 0); tbm_drm_auth_srv->display = (struct wl_display *)wl_display; - tbm_drm_auth_srv->device_name = strdup(device_name); + if (!device_name) { + tbm_drm_auth_srv->device_name = drmGetDeviceNameFromFd(fd); + if (!tbm_drm_auth_srv->device_name) { + TBM_ERR("fail to get device name!\n"); + return 0; + } + } else { + tbm_drm_auth_srv->device_name = strdup(device_name); + } tbm_drm_auth_srv->fd = fd; tbm_drm_auth_srv->flags = flags; -- 2.7.4