[mutter/gbsneto/dmabuf-screencast: 5/13] cogl/onscreen: Add cogl_onscreen_capture_dmabuf() and family



commit b61d2a06472820fdda52c26c0720317a81832f9e
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Mon Dec 9 10:03:07 2019 -0300

    cogl/onscreen: Add cogl_onscreen_capture_dmabuf() and family
    
    This is a winsys-specific API that allows exporting a DMA buffer fd.
    The CoglOnscreenDmabufHandle structure allows passing the ownership
    of the DMA buffer to whoever is using it, so the winsys doesn't need
    to manually track it.
    
    https://gitlab.gnome.org/GNOME/mutter/merge_requests/1086

 cogl/cogl/cogl-onscreen-private.h      |  8 ++++
 cogl/cogl/cogl-onscreen.c              | 69 ++++++++++++++++++++++++++++++++++
 cogl/cogl/cogl-onscreen.h              | 65 ++++++++++++++++++++++++++++++++
 cogl/cogl/winsys/cogl-winsys-private.h |  4 ++
 4 files changed, 146 insertions(+)
---
diff --git a/cogl/cogl/cogl-onscreen-private.h b/cogl/cogl/cogl-onscreen-private.h
index 9718708d0..889c9891a 100644
--- a/cogl/cogl/cogl-onscreen-private.h
+++ b/cogl/cogl/cogl-onscreen-private.h
@@ -75,6 +75,14 @@ struct _CoglOnscreen
   void *winsys;
 };
 
+struct _CoglOnscreenDmabufHandle
+{
+  CoglFramebuffer *framebuffer;
+  int dmabuf_fd;
+  void *private_data;
+  GDestroyNotify destroy_func;
+};
+
 CoglOnscreen *
 _cogl_onscreen_new (void);
 
diff --git a/cogl/cogl/cogl-onscreen.c b/cogl/cogl/cogl-onscreen.c
index 1bd1d5911..763283cf1 100644
--- a/cogl/cogl/cogl-onscreen.c
+++ b/cogl/cogl/cogl-onscreen.c
@@ -42,6 +42,8 @@
 #include "cogl-poll-private.h"
 #include "cogl-gtype-private.h"
 
+#include <unistd.h>
+
 static void _cogl_onscreen_free (CoglOnscreen *onscreen);
 
 COGL_OBJECT_DEFINE_WITH_CODE_GTYPE (Onscreen, onscreen,
@@ -583,3 +585,70 @@ cogl_onscreen_get_frame_counter (CoglOnscreen *onscreen)
 {
   return onscreen->frame_counter;
 }
+
+CoglOnscreenDmabufHandle *
+cogl_onscreen_dmabuf_handle_new (CoglFramebuffer *framebuffer,
+                                 int              dmabuf_fd,
+                                 gpointer         user_data,
+                                 GDestroyNotify   destroy_func)
+{
+  CoglOnscreenDmabufHandle *dmabuf_handle;
+
+  g_assert (framebuffer);
+  g_assert (dmabuf_fd != -1);
+
+  dmabuf_handle = g_new0 (CoglOnscreenDmabufHandle, 1);
+  dmabuf_handle->framebuffer = cogl_object_ref (framebuffer);
+  dmabuf_handle->dmabuf_fd = dmabuf_fd;
+  dmabuf_handle->private_data = user_data;
+  dmabuf_handle->destroy_func = destroy_func;
+
+  return dmabuf_handle;
+}
+
+void
+cogl_onscreen_dmabuf_handle_release (CoglOnscreenDmabufHandle *dmabuf_handle)
+{
+  if (!dmabuf_handle)
+    return;
+
+  g_clear_pointer (&dmabuf_handle->framebuffer, cogl_object_unref);
+
+  if (dmabuf_handle->destroy_func)
+    g_clear_pointer (&dmabuf_handle->private_data, dmabuf_handle->destroy_func);
+
+  if (dmabuf_handle->dmabuf_fd != -1)
+    close (dmabuf_handle->dmabuf_fd);
+
+  g_free (dmabuf_handle);
+}
+
+CoglFramebuffer *
+cogl_onscreen_dmabuf_handle_get_framebuffer (CoglOnscreenDmabufHandle *dmabuf_handle)
+{
+  return dmabuf_handle->framebuffer;
+}
+
+int
+cogl_onscreen_dmabuf_handle_get_fd (CoglOnscreenDmabufHandle *dmabuf_handle)
+{
+  return dmabuf_handle->dmabuf_fd;
+}
+
+CoglOnscreenDmabufHandle *
+cogl_onscreen_capture_dmabuf (CoglOnscreen  *onscreen,
+                              GError       **error)
+{
+  CoglFramebuffer *framebuffer;
+  const CoglWinsysVtable *winsys;
+
+  framebuffer = COGL_FRAMEBUFFER (onscreen);
+  winsys = _cogl_framebuffer_get_winsys (COGL_FRAMEBUFFER (onscreen));
+
+  g_assert (framebuffer->allocated);
+
+  if (winsys->onscreen_capture_dmabuf)
+    return winsys->onscreen_capture_dmabuf (onscreen, error);
+
+  return NULL;
+}
diff --git a/cogl/cogl/cogl-onscreen.h b/cogl/cogl/cogl-onscreen.h
index d339025c5..48245b480 100644
--- a/cogl/cogl/cogl-onscreen.h
+++ b/cogl/cogl/cogl-onscreen.h
@@ -761,6 +761,71 @@ cogl_is_onscreen (void *object);
 int64_t
 cogl_onscreen_get_frame_counter (CoglOnscreen *onscreen);
 
+/**
+ * CoglOnscreenDmabufHandle: (skip)
+ *
+ * An opaque type that tracks the lifetime of a DMA buffer fd. Release
+ * with cogl_onscreen_dmabuf_handle_release().
+ */
+typedef struct _CoglOnscreenDmabufHandle CoglOnscreenDmabufHandle;
+
+/**
+ * cogl_onscreen_dmabuf_handle_new: (skip)
+ */
+CoglOnscreenDmabufHandle *
+cogl_onscreen_dmabuf_handle_new (CoglFramebuffer *framebuffer,
+                                 int              dmabuf_fd,
+                                 gpointer         data,
+                                 GDestroyNotify   destroy_func);
+
+/**
+ * cogl_onscreen_dmabuf_handle_release: (skip)
+ *
+ * Releases @dmabuf_handle; it is a programming error to release
+ * an already released handle.
+ */
+void
+cogl_onscreen_dmabuf_handle_release (CoglOnscreenDmabufHandle *dmabuf_handle);
+
+/**
+ * cogl_onscreen_dmabuf_handle_get_framebuffer: (skip)
+ *
+ * Retrieves the #CoglFramebuffer, backed by an exported DMABuf buffer,
+ * of @dmabuf_handle.
+ *
+ * Returns: (transfer none): a #CoglFramebuffer
+ */
+CoglFramebuffer *
+cogl_onscreen_dmabuf_handle_get_framebuffer (CoglOnscreenDmabufHandle *dmabuf_handle);
+
+/**
+ * cogl_onscreen_dmabuf_handle_get_fd: (skip)
+ *
+ * Retrieves the file descriptor of @dmabuf_handle.
+ *
+ * Returns: a valid file descriptor
+ */
+int
+cogl_onscreen_dmabuf_handle_get_fd (CoglOnscreenDmabufHandle *dmabuf_handle);
+
+/**
+ * cogl_onscreen_capture_dmabuf: (skip)
+ * @onscreen: A #CoglOnscreen
+ * @error: (nullable): return location for a #GError
+ *
+ * Creates a new #CoglFramebuffer with the same @onscreen properties
+ * (width, height, format, etc), and exports the new framebuffer's
+ * DMA-Buf handle. @onscreen must be allocated.
+ *
+ * Returns: (nullable)(transfer full): a #CoglOnscreenDmabufHandle. The
+ * return result must be released with cogl_onscreen_dmabuf_handle_release()
+ * after use.
+ */
+CoglOnscreenDmabufHandle *
+cogl_onscreen_capture_dmabuf (CoglOnscreen  *onscreen,
+                              GError       **error);
+
+
 G_END_DECLS
 
 #endif /* __COGL_ONSCREEN_H */
diff --git a/cogl/cogl/winsys/cogl-winsys-private.h b/cogl/cogl/winsys/cogl-winsys-private.h
index 1ed7bf4fb..e58d9f142 100644
--- a/cogl/cogl/winsys/cogl-winsys-private.h
+++ b/cogl/cogl/winsys/cogl-winsys-private.h
@@ -134,6 +134,10 @@ typedef struct _CoglWinsysVtable
   uint32_t
   (*onscreen_x11_get_window_xid) (CoglOnscreen *onscreen);
 
+  CoglOnscreenDmabufHandle *
+  (*onscreen_capture_dmabuf) (CoglOnscreen  *onscreen,
+                              GError       **error);
+
 #ifdef COGL_HAS_XLIB_SUPPORT
   gboolean
   (*texture_pixmap_x11_create) (CoglTexturePixmapX11 *tex_pixmap);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]