From 3536b53ed1a45d9a5800a192e1bb0400743b32d0 Mon Sep 17 00:00:00 2001 From: Aaron Boxer Date: Mon, 30 May 2022 13:19:23 -0400 Subject: vo: move allocate_memfd method to wayland_common --- video/out/vo_vaapi_wayland.c | 21 +-------------------- video/out/vo_wlshm.c | 17 +---------------- video/out/wayland_common.c | 23 +++++++++++++++++++++++ video/out/wayland_common.h | 1 + 4 files changed, 26 insertions(+), 36 deletions(-) (limited to 'video/out') diff --git a/video/out/vo_vaapi_wayland.c b/video/out/vo_vaapi_wayland.c index 1167d05da0..30f24332b9 100644 --- a/video/out/vo_vaapi_wayland.c +++ b/video/out/vo_vaapi_wayland.c @@ -245,25 +245,6 @@ static void uninit(struct vo *vo) va_destroy(p->mpvaapi); } -static int allocate_memfd(struct vo *vo, size_t size) -{ - int fd = memfd_create("mpv", MFD_CLOEXEC | MFD_ALLOW_SEALING); - if (fd < 0) { - MP_ERR(vo, "Unable to create memfd file descriptor\n"); - return VO_ERROR; - } - - fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_SEAL); - - if (posix_fallocate(fd, 0, size) == 0) - return fd; - - close(fd); - MP_ERR(vo, "Unable to allocate memfd file descriptor\n"); - - return VO_ERROR; -} - static int preinit(struct vo *vo) { struct priv *p = vo->priv; @@ -313,7 +294,7 @@ static int reconfig(struct vo *vo, struct mp_image_params *params) int width = 1; int height = 1; int stride = MP_ALIGN_UP(width * 4, 16); - int fd = allocate_memfd(vo, stride); + int fd = vo_wayland_allocate_memfd(vo, stride); if (fd < 0) return VO_ERROR; p->solid_buffer_pool = wl_shm_create_pool(wl->shm, fd, height * stride); diff --git a/video/out/vo_wlshm.c b/video/out/vo_wlshm.c index 2bcb340754..2ed20176be 100644 --- a/video/out/vo_wlshm.c +++ b/video/out/vo_wlshm.c @@ -74,21 +74,6 @@ static void buffer_destroy(void *p) munmap(buf->mpi.planes[0], buf->size); } -static int allocate_memfd(size_t size) -{ - int fd = memfd_create("mpv", MFD_CLOEXEC | MFD_ALLOW_SEALING); - if (fd < 0) - return -1; - - fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_SEAL); - - if (posix_fallocate(fd, 0, size) == 0) - return fd; - - close(fd); - return -1; -} - static struct buffer *buffer_create(struct vo *vo, int width, int height) { struct priv *p = vo->priv; @@ -101,7 +86,7 @@ static struct buffer *buffer_create(struct vo *vo, int width, int height) stride = MP_ALIGN_UP(width * 4, 16); size = height * stride; - fd = allocate_memfd(size); + fd = vo_wayland_allocate_memfd(vo, size); if (fd < 0) goto error0; data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); diff --git a/video/out/wayland_common.c b/video/out/wayland_common.c index 0c41c49e50..282fefefef 100644 --- a/video/out/wayland_common.c +++ b/video/out/wayland_common.c @@ -1638,6 +1638,29 @@ static void vo_wayland_dispatch_events(struct vo_wayland_state *wl, int nfds, in } /* Non-static */ +int vo_wayland_allocate_memfd(struct vo *vo, size_t size) +{ +#if !HAVE_MEMFD_CREATE + return VO_ERROR; +#else + int fd = memfd_create("mpv", MFD_CLOEXEC | MFD_ALLOW_SEALING); + if (fd < 0) { + MP_ERR(vo, "Failed to allocate memfd: %s\n", mp_strerror(errno)); + return VO_ERROR; + } + + fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_SEAL); + + if (posix_fallocate(fd, 0, size) == 0) + return fd; + + close(fd); + MP_ERR(vo, "Failed to allocate memfd: %s\n", mp_strerror(errno)); + + return VO_ERROR; +#endif +} + bool vo_wayland_check_visible(struct vo *vo) { struct vo_wayland_state *wl = vo->wl; diff --git a/video/out/wayland_common.h b/video/out/wayland_common.h index c85dc200d5..90bd1ca7e1 100644 --- a/video/out/wayland_common.h +++ b/video/out/wayland_common.h @@ -142,6 +142,7 @@ struct vo_wayland_state { bool vo_wayland_check_visible(struct vo *vo); bool vo_wayland_supported_format(struct vo *vo, uint32_t format); +int vo_wayland_allocate_memfd(struct vo *vo, size_t size); int vo_wayland_control(struct vo *vo, int *events, int request, void *arg); int vo_wayland_init(struct vo *vo); int vo_wayland_reconfig(struct vo *vo); -- cgit v1.2.3