From 6dbdf5db345c47ac6edf92161cb7b521c8284e39 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 29 Apr 2019 22:32:35 +0300 Subject: [PATCH] render/dmabuf: add wlr_dmabuf_attributes_copy --- include/wlr/render/dmabuf.h | 6 ++++++ render/dmabuf.c | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/wlr/render/dmabuf.h b/include/wlr/render/dmabuf.h index 1d61da4e..93e50eb3 100644 --- a/include/wlr/render/dmabuf.h +++ b/include/wlr/render/dmabuf.h @@ -9,6 +9,7 @@ #ifndef WLR_RENDER_DMABUF_H #define WLR_RENDER_DMABUF_H +#include #include #define WLR_DMABUF_MAX_PLANES 4 @@ -35,5 +36,10 @@ struct wlr_dmabuf_attributes { * Closes all file descriptors in the DMA-BUF attributes. */ void wlr_dmabuf_attributes_finish(struct wlr_dmabuf_attributes *attribs); +/** + * Clones the DMA-BUF attributes. + */ +bool wlr_dmabuf_attributes_copy(struct wlr_dmabuf_attributes *dst, + struct wlr_dmabuf_attributes *src); #endif diff --git a/render/dmabuf.c b/render/dmabuf.c index 6b500748..bed609dd 100644 --- a/render/dmabuf.c +++ b/render/dmabuf.c @@ -1,10 +1,28 @@ +#define _POSIX_C_SOURCE 200809L +#include #include #include +#include -void wlr_dmabuf_attributes_finish( struct wlr_dmabuf_attributes *attribs) { +void wlr_dmabuf_attributes_finish(struct wlr_dmabuf_attributes *attribs) { for (int i = 0; i < attribs->n_planes; ++i) { close(attribs->fd[i]); attribs->fd[i] = -1; } attribs->n_planes = 0; } + +bool wlr_dmabuf_attributes_copy(struct wlr_dmabuf_attributes *dst, + struct wlr_dmabuf_attributes *src) { + memcpy(dst, src, sizeof(struct wlr_dmabuf_attributes)); + + for (int i = 0; i < src->n_planes; ++i) { + dst->fd[i] = fcntl(src->fd[i], F_DUPFD_CLOEXEC, 0); + if (dst->fd[i] < 0) { + wlr_log_errno(WLR_ERROR, "fcntl(F_DUPFD_CLOEXEC) failed"); + return false; + } + } + + return true; +}