From 5b07ccd0a766aeda3fa81a1ec32ab8fa51d05235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Wed, 6 May 2020 21:40:40 +0200 Subject: [PATCH] cogl/dma-buf: Add API to synchronize reading Used before and after accessing DMA buffer content using mmap(). https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1237 --- cogl/cogl/cogl-dma-buf-handle.c | 51 +++++++++++++++++++++++++++++++++ cogl/cogl/cogl-dma-buf-handle.h | 8 ++++++ cogl/meson.build | 1 + 3 files changed, 60 insertions(+) diff --git a/cogl/cogl/cogl-dma-buf-handle.c b/cogl/cogl/cogl-dma-buf-handle.c index d8b4e57c5..7e86e2267 100644 --- a/cogl/cogl/cogl-dma-buf-handle.c +++ b/cogl/cogl/cogl-dma-buf-handle.c @@ -34,6 +34,10 @@ #include "cogl-dma-buf-handle.h" #include "cogl-object.h" +#include +#include +#include +#include #include struct _CoglDmaBufHandle @@ -96,6 +100,53 @@ cogl_dma_buf_handle_free (CoglDmaBufHandle *dmabuf_handle) g_free (dmabuf_handle); } +static gboolean +sync_read (CoglDmaBufHandle *dmabuf_handle, + uint64_t start_or_end, + GError **error) +{ + struct dma_buf_sync sync = { 0 }; + + sync.flags = start_or_end | DMA_BUF_SYNC_READ; + + while (TRUE) + { + int ret; + + ret = ioctl (dmabuf_handle->dmabuf_fd, DMA_BUF_IOCTL_SYNC, &sync); + if (ret == -1 && errno == EINTR) + { + continue; + } + else if (ret == -1) + { + g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), + "ioctl: %s", g_strerror (errno)); + return FALSE; + } + else + { + break; + } + } + + return TRUE; +} + +gboolean +cogl_dma_buf_handle_sync_read_start (CoglDmaBufHandle *dmabuf_handle, + GError **error) +{ + return sync_read (dmabuf_handle, DMA_BUF_SYNC_START, error); +} + +gboolean +cogl_dma_buf_handle_sync_read_end (CoglDmaBufHandle *dmabuf_handle, + GError **error) +{ + return sync_read (dmabuf_handle, DMA_BUF_SYNC_END, error); +} + CoglFramebuffer * cogl_dma_buf_handle_get_framebuffer (CoglDmaBufHandle *dmabuf_handle) { diff --git a/cogl/cogl/cogl-dma-buf-handle.h b/cogl/cogl/cogl-dma-buf-handle.h index b00c5f36b..a329de668 100644 --- a/cogl/cogl/cogl-dma-buf-handle.h +++ b/cogl/cogl/cogl-dma-buf-handle.h @@ -63,6 +63,14 @@ cogl_dma_buf_handle_new (CoglFramebuffer *framebuffer, COGL_EXPORT void cogl_dma_buf_handle_free (CoglDmaBufHandle *dmabuf_handle); +COGL_EXPORT gboolean +cogl_dma_buf_handle_sync_read_start (CoglDmaBufHandle *dmabuf_handle, + GError **error); + +COGL_EXPORT gboolean +cogl_dma_buf_handle_sync_read_end (CoglDmaBufHandle *dmabuf_handle, + GError **error); + /** * cogl_dma_buf_handle_get_framebuffer: (skip) * diff --git a/cogl/meson.build b/cogl/meson.build index e0ea1ed97..a3c61cfb7 100644 --- a/cogl/meson.build +++ b/cogl/meson.build @@ -17,6 +17,7 @@ cogl_config_h = configure_file( cogl_pkg_deps = [ glib_dep, + gio_dep, gobject_dep, graphene_dep, ]