[gtk/wip/chergert/for-4-6: 16/56] macos: add readonly IOSurfaceLock helper




commit b2ab0b1fcb8f17f992f58157a4c157c58abb613e
Author: Christian Hergert <christian hergert me>
Date:   Mon Feb 28 01:29:22 2022 -0800

    macos: add readonly IOSurfaceLock helper
    
    This can be used to lock a surface for reading to avoid causing the
    surface contents to be invalidated. This is needed when reading back from
    a front-buffer to the back-buffer as is needed when using Cairo surfaces
    to implement something similar to BufferAge.

 gdk/macos/gdkmacosbuffer-private.h |  2 ++
 gdk/macos/gdkmacosbuffer.c         | 39 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
---
diff --git a/gdk/macos/gdkmacosbuffer-private.h b/gdk/macos/gdkmacosbuffer-private.h
index 6be201147c..4b446a7212 100644
--- a/gdk/macos/gdkmacosbuffer-private.h
+++ b/gdk/macos/gdkmacosbuffer-private.h
@@ -41,6 +41,8 @@ GdkMacosBuffer       *_gdk_macos_buffer_new              (int              width
 IOSurfaceRef          _gdk_macos_buffer_get_native       (GdkMacosBuffer  *self);
 void                  _gdk_macos_buffer_lock             (GdkMacosBuffer  *self);
 void                  _gdk_macos_buffer_unlock           (GdkMacosBuffer  *self);
+void                  _gdk_macos_buffer_read_lock        (GdkMacosBuffer  *self);
+void                  _gdk_macos_buffer_read_unlock      (GdkMacosBuffer  *self);
 guint                 _gdk_macos_buffer_get_width        (GdkMacosBuffer  *self);
 guint                 _gdk_macos_buffer_get_height       (GdkMacosBuffer  *self);
 guint                 _gdk_macos_buffer_get_stride       (GdkMacosBuffer  *self);
diff --git a/gdk/macos/gdkmacosbuffer.c b/gdk/macos/gdkmacosbuffer.c
index ac99302ee4..46a0504461 100644
--- a/gdk/macos/gdkmacosbuffer.c
+++ b/gdk/macos/gdkmacosbuffer.c
@@ -192,6 +192,45 @@ _gdk_macos_buffer_unlock (GdkMacosBuffer *self)
   IOSurfaceUnlock (self->surface, 0, NULL);
 }
 
+/**
+ * _gdk_macos_buffer_lock_readonly:
+ *
+ * Like _gdk_macos_buffer_lock() but uses the read-only flag to
+ * indicate we are not interested in retrieving the updates from
+ * the GPU before modifying the CPU-side cache.
+ *
+ * Must be used with _gdk_macos_buffer_unlock_readonly().
+ */
+void
+_gdk_macos_buffer_read_lock (GdkMacosBuffer *self)
+{
+  kern_return_t ret;
+
+  g_return_if_fail (GDK_IS_MACOS_BUFFER (self));
+  g_return_if_fail (self->lock_count == 0);
+
+  self->lock_count++;
+
+  ret = IOSurfaceLock (self->surface, kIOSurfaceLockReadOnly, NULL);
+
+  g_return_if_fail (ret == KERN_SUCCESS);
+}
+
+void
+_gdk_macos_buffer_read_unlock (GdkMacosBuffer *self)
+{
+  kern_return_t ret;
+
+  g_return_if_fail (GDK_IS_MACOS_BUFFER (self));
+  g_return_if_fail (self->lock_count == 1);
+
+  self->lock_count--;
+
+  ret = IOSurfaceUnlock (self->surface, kIOSurfaceLockReadOnly, NULL);
+
+  g_return_if_fail (ret == KERN_SUCCESS);
+}
+
 guint
 _gdk_macos_buffer_get_width (GdkMacosBuffer *self)
 {


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