This is an automated email from the git hooks/post-receive script.

git pushed a commit to reference refs/pull/230/head
in repository enlightenment.

View the commit online.

commit f50ff7cb52dc6a9f6726c121f1b86d29f01c90ee
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 29 12:42:06 2026 -0600

    e_pixmap - hold each buffer once, and let go of destroyed ones
    
    A pixmap takes two holds on a client's buffer: one when it is attached,
    and one on the buffer whose pixels are drawn (wl_shm and single-pixel
    buffers). Both were kept on one busy list, which got them wrong three
    ways:
    
     - An shm buffer was released as soon as it was drawn, while still on
       screen, and a second time when it was replaced. Drawing a new buffer
       emptied the whole list, the new buffer's own attach hold included. A
       client may draw into a released buffer, under E's render.
    
     - A buffer the client destroyed while attached was never let go: the
       destroy listener forgot it, so nothing released it until the window
       was closed. Every such buffer kept its dmabuf file descriptors, or
       its shm pool mapped, for the life of the window.
    
     - A dmabuf replaced after a null attach was never released to the
       client either, for the same reason.
    
    The pixmap now remembers each hold in its own field, and drops it when
    another buffer takes its place, or with the pixmap. A destroyed buffer
    stays held until then, as it may still be on screen; E_Comp_Wl_Buffer
    is only freed once nothing holds it, so the pointers stay valid. A null
    attach keeps the last buffer, which a hidden window is drawn with. A
    buffer's shm pool is referenced once however many holds there are, as
    it is unreferenced once.
    
    When a client gets each release does not change for a buffer that is
    replaced: the attach hold goes with the next attach, the draw hold with
    the next draw, as before.
    
    Shared file, Wayland only paths: e_pixmap_resource_set() is called from
    e_comp_wl.c on commit and on a synchronized subsurface's cached commit,
    e_pixmap_image_refresh() from e_comp.c before a render, and the holds
    are dropped by _e_pixmap_free(). The scanout handler's holds are
    separate and unchanged. The default and x11 suites pass.
    
    wl-buffer-hold checks that a buffer on screen is not released, that each
    replaced one is released once, also across a null attach, and counts the
    pools E keeps mapped after a client destroys 20 buffers on screen.
    Without the fix it gets a release for the buffer on screen, and E keeps
    24 pools mapped where 5 are expected. The dmabuf cases need a GL canvas
    that takes client dmabufs, which no test backend has yet; they are
    covered with the series that adds one.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/bin/e_pixmap.c                   | 112 +++++++----------------
 src/tests/wayland/meson.build        |   1 +
 src/tests/wayland/test_buffer_hold.c | 170 +++++++++++++++++++++++++++++++++++
 3 files changed, 205 insertions(+), 78 deletions(-)

diff --git a/src/bin/e_pixmap.c b/src/bin/e_pixmap.c
index 608d8a613..e94823e6b 100644
--- a/src/bin/e_pixmap.c
+++ b/src/bin/e_pixmap.c
@@ -49,13 +49,16 @@ struct _E_Pixmap
 #endif
 
 #ifdef HAVE_WAYLAND
+   /* The current buffer, NULL when there is none or it was destroyed. */
    E_Comp_Wl_Buffer *buffer;
+   /* The two buffers this pixmap holds, each until another takes its place:
+    * the last one attached, and the one whose pixels are being drawn. Either
+    * may have been destroyed by its client, and may still be on screen. */
+   E_Comp_Wl_Buffer *attached_buffer;
    E_Comp_Wl_Buffer *held_buffer;
    struct wl_listener buffer_destroy_listener;
-   struct wl_listener held_buffer_destroy_listener;
    void *data;
    Eina_Rectangle opaque;
-   Eina_List *busy_list;
 #endif
 
    Eina_Bool usable E_BITFIELD;
@@ -84,18 +87,6 @@ _e_pixmap_cb_buffer_destroy(struct wl_listener *listener, void *data EINA_UNUSED
    wl_list_init(&cp->buffer_destroy_listener.link);
    cp->buffer_destroy_listener.notify = NULL;
 }
-
-static void
-_e_pixmap_cb_held_buffer_destroy(struct wl_listener *listener, void *data EINA_UNUSED)
-{
-   E_Pixmap *cp;
-
-   cp = container_of(listener, E_Pixmap, held_buffer_destroy_listener);
-   cp->held_buffer = NULL;
-   wl_list_remove(&cp->held_buffer_destroy_listener.link);
-   wl_list_init(&cp->held_buffer_destroy_listener.link);
-   cp->held_buffer_destroy_listener.notify = NULL;
-}
 #endif
 
 static void
@@ -179,41 +170,20 @@ _e_pixmap_wl_resource_release(E_Comp_Wl_Buffer *buffer)
 }
 
 static void
-_e_pixmap_wayland_buffer_release(E_Pixmap *cp, E_Comp_Wl_Buffer *buffer)
+_e_pixmap_wayland_held_release(E_Pixmap *cp)
 {
-   if (!buffer) return;
-
-   if (buffer->busy == 1)
-     cp->busy_list = eina_list_remove(cp->busy_list, buffer);
-   _e_pixmap_wl_resource_release(buffer);
+   if (!cp->held_buffer) return;
+   cp->data = ""
+   _e_pixmap_wl_resource_release(cp->held_buffer);
+   cp->held_buffer = NULL;
 }
 
 static void
-_e_pixmap_wayland_image_clear(E_Pixmap *cp)
+_e_pixmap_wayland_attached_release(E_Pixmap *cp)
 {
-   EINA_SAFETY_ON_NULL_RETURN(cp);
-
-   if (cp->busy_list)
-     {
-        E_Comp_Wl_Buffer *buffer;
-
-        /* The release path sends wl_buffer.release when busy reaches 0. */
-        EINA_LIST_FREE(cp->busy_list, buffer)
-          _e_pixmap_wl_resource_release(buffer);
-     }
-
-   if (!cp->held_buffer) return;
-   if (!cp->held_buffer->pool) return;
-
-   _e_pixmap_wayland_buffer_release(cp, cp->held_buffer);
-   if (cp->held_buffer_destroy_listener.notify)
-     {
-        wl_list_remove(&cp->held_buffer_destroy_listener.link);
-        cp->held_buffer_destroy_listener.notify = NULL;
-     }
-
-   cp->data = ""
-   cp->held_buffer = NULL;
+   if (!cp->attached_buffer) return;
+   _e_pixmap_wl_resource_release(cp->attached_buffer);
+   cp->attached_buffer = NULL;
 }
 #endif
 
@@ -254,18 +224,13 @@ _e_pixmap_free(E_Pixmap *cp)
         break;
       case E_PIXMAP_TYPE_WL:
 #ifdef HAVE_WAYLAND
-        _e_pixmap_wayland_image_clear(cp);
         if (cp->buffer_destroy_listener.notify)
           {
              wl_list_remove(&cp->buffer_destroy_listener.link);
              cp->buffer_destroy_listener.notify = NULL;
           }
-        /* _e_pixmap_wayland_image_clear() can return before unlinking it. */
-        if (cp->held_buffer_destroy_listener.notify)
-          {
-             wl_list_remove(&cp->held_buffer_destroy_listener.link);
-             cp->held_buffer_destroy_listener.notify = NULL;
-          }
+        _e_pixmap_wayland_held_release(cp);
+        _e_pixmap_wayland_attached_release(cp);
 #endif
         break;
       default:
@@ -760,28 +725,25 @@ e_pixmap_resource_set(E_Pixmap *cp, void *resource)
 #ifdef HAVE_WAYLAND
    if (cp->buffer == resource) return;
 
-   if (cp->buffer && resource)
-     _e_pixmap_wayland_buffer_release(cp, cp->buffer);
-
    if (cp->buffer_destroy_listener.notify)
      {
         wl_list_remove(&cp->buffer_destroy_listener.link);
         cp->buffer_destroy_listener.notify = NULL;
      }
    cp->buffer = resource;
+   /* A null attach keeps the last buffer, which is drawn while the window
+    * is animated away. */
    if (!cp->buffer) return;
    cp->buffer_destroy_listener.notify = _e_pixmap_cb_buffer_destroy;
    wl_signal_add(&cp->buffer->destroy_signal,
                  &cp->buffer_destroy_listener);
 
+   if (cp->attached_buffer == cp->buffer) return;
+   _e_pixmap_wayland_attached_release(cp);
    if (cp->buffer->dmabuf_buffer)
      linux_dmabuf_buffer_ref(cp->buffer->dmabuf_buffer);
    cp->buffer->busy++;
-   if (cp->buffer->busy > 0)
-     {
-        cp->busy_list = eina_list_remove(cp->busy_list, cp->buffer);
-        cp->busy_list = eina_list_append(cp->busy_list, cp->buffer);
-     }
+   cp->attached_buffer = cp->buffer;
 #else
    (void)resource;
 #endif
@@ -827,22 +789,18 @@ _e_pixmap_scanout_handler(void *data, Evas_Native_Surface_Status status)
         if (buffer->dmabuf_buffer)
           linux_dmabuf_buffer_ref(buffer->dmabuf_buffer);
         buffer->busy++;
-        // XXX: fixme - handle cp->busy_list
         break;
       case EVAS_NATIVE_SURFACE_STATUS_SCANOUT_OFF:
         // busy--
-        // XXX: fixme - handle cp->busy_list
         _e_pixmap_wl_resource_release(buffer);
         break;
       case EVAS_NATIVE_SURFACE_STATUS_PLANE_ASSIGN:
         if (buffer->dmabuf_buffer)
           linux_dmabuf_buffer_ref(buffer->dmabuf_buffer);
-        // XXX: fixme - handle cp->busy_list
         buffer->busy++;
         break;
       case EVAS_NATIVE_SURFACE_STATUS_PLANE_RELEASE:
         // busy--
-        // XXX: fixme - handle cp->busy_list
         _e_pixmap_wl_resource_release(buffer);
         break;
      }
@@ -1020,35 +978,33 @@ e_pixmap_image_refresh(E_Pixmap *cp)
         {
            if (cp->held_buffer == cp->buffer) return EINA_TRUE;
 
-           if (cp->held_buffer) _e_pixmap_wayland_image_clear(cp);
-
            /* This catches the case where a client (*cough* xwayland)
-            * deletes a buffer we haven't released
-            */
+            * deletes a buffer we haven't released. What is held stays
+            * drawn until there is something to replace it. */
            if (!cp->buffer) return EINA_FALSE;
 
+           _e_pixmap_wayland_held_release(cp);
+
            if (cp->buffer->single_pixel)
              cp->data = ""
            else if (!cp->buffer->shm_buffer) return EINA_TRUE;
 
            cp->held_buffer = cp->buffer;
-           if (!cp->held_buffer) return EINA_TRUE;
 
            if (cp->held_buffer->shm_buffer)
              {
-                cp->held_buffer->pool =
-                  wl_shm_buffer_ref_pool(cp->held_buffer->shm_buffer);
-                e_comp_wl_shm_guard_add(cp->held_buffer->pool, cp->held_buffer);
+                /* Another hold may have taken the pool already, and the
+                 * buffer gives it back only once. */
+                if (!cp->held_buffer->pool)
+                  {
+                     cp->held_buffer->pool =
+                       wl_shm_buffer_ref_pool(cp->held_buffer->shm_buffer);
+                     e_comp_wl_shm_guard_add(cp->held_buffer->pool,
+                                             cp->held_buffer);
+                  }
                 cp->data = ""
              }
            cp->held_buffer->busy++;
-           cp->held_buffer_destroy_listener.notify =
-             _e_pixmap_cb_held_buffer_destroy;
-
-           if (cp->held_buffer_destroy_listener.link.next)
-             wl_list_remove(&cp->held_buffer_destroy_listener.link);
-           wl_signal_add(&cp->held_buffer->destroy_signal,
-                         &cp->held_buffer_destroy_listener);
            return EINA_TRUE;
         }
 #endif
diff --git a/src/tests/wayland/meson.build b/src/tests/wayland/meson.build
index b881b4b0c..a51ac08e6 100644
--- a/src/tests/wayland/meson.build
+++ b/src/tests/wayland/meson.build
@@ -73,6 +73,7 @@ wl_protocol_tests = [
   ['seat-capabilities', 'test_seat_capabilities.c'],
   ['dialog-focus', 'test_dialog_focus.c'],
   ['buffer-release', 'test_buffer_release.c'],
+  ['buffer-hold', 'test_buffer_hold.c'],
   ['client-move', 'test_client_move.c'],
   ['pointer-frame', 'test_pointer_frame.c'],
   ['surface-unmap', 'test_surface_unmap.c'],
diff --git a/src/tests/wayland/test_buffer_hold.c b/src/tests/wayland/test_buffer_hold.c
new file mode 100644
index 000000000..a898c3f5c
--- /dev/null
+++ b/src/tests/wayland/test_buffer_hold.c
@@ -0,0 +1,170 @@
+/* Does E hand back a wl_shm buffer when it is done with it, and not before?
+ *
+ * E draws an shm buffer from the client's memory, so a buffer on screen
+ * must not be released: the client would draw into it. Its replacement
+ * releases it, once. A client may also destroy a buffer it has not got
+ * back; E must then still let go of its memory once another replaces it,
+ * which shows as the pools E has mapped. */
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "e_wl_testkit.h"
+
+#define PROG "test-buffer-hold"
+#define APP_ID "e.test.buffer_hold"
+#define POOL "e-test-buffer-hold"
+#define W 200
+#define H 150
+#define ROUNDS 20
+
+typedef struct
+{
+   struct wl_buffer *buffer;
+   int released;
+} Buffer;
+
+static void
+_release(void *data, struct wl_buffer *buffer __attribute__((unused)))
+{
+   Buffer *b = data;
+
+   b->released++;
+}
+
+static const struct wl_buffer_listener _buffer_listener = { _release };
+
+static void
+_buffer_new(Tk *tk, Buffer *b)
+{
+   struct wl_shm *shm = tk_bind(tk, &wl_shm_interface, 1);
+   struct wl_shm_pool *pool;
+   int fd;
+
+   fd = memfd_create(POOL, MFD_CLOEXEC);
+   if (fd < 0) tk_fail(tk, "memfd_create: %m");
+   if (ftruncate(fd, W * H * 4) < 0) tk_fail(tk, "ftruncate: %m");
+   pool = wl_shm_create_pool(shm, fd, W * H * 4);
+   b->buffer = wl_shm_pool_create_buffer(pool, 0, W, H, W * 4,
+                                         WL_SHM_FORMAT_XRGB8888);
+   b->released = 0;
+   wl_buffer_add_listener(b->buffer, &_buffer_listener, b);
+   wl_shm_pool_destroy(pool);
+   wl_shm_destroy(shm);
+   close(fd);
+}
+
+/* Commit b, or nothing for NULL, and give E time to draw it. */
+static void
+_show(Tk *tk, struct wl_surface *surface, Buffer *b)
+{
+   wl_surface_attach(surface, b ? b->buffer : NULL, 0, 0);
+   wl_surface_damage(surface, 0, 0, W, H);
+   wl_surface_commit(surface);
+   tk_sync(tk);
+   tk_settle(tk);
+}
+
+static void
+_released_expect(Tk *tk, const Buffer *b, int want, const char *what)
+{
+   struct timespec step = { 0, 50000000 };
+   int i;
+
+   for (i = 0; (i < TK_WAIT_SECONDS * 20) && (b->released < want); i++)
+     {
+        nanosleep(&step, NULL);
+        tk_sync(tk);
+     }
+   if (b->released != want)
+     tk_fail(tk, "%s: released %d times, wanted %d", what, b->released, want);
+}
+
+/* How many of this test's pools E has mapped. */
+static int
+_pools_mapped(Tk *tk)
+{
+   char path[64], line[512];
+   const char *pid = getenv("E_TEST_PID");
+   FILE *f;
+   int n = 0;
+
+   if (!pid) tk_fail(tk, "no E_TEST_PID");
+   snprintf(path, sizeof(path), "/proc/%s/maps", pid);
+   f = fopen(path, "r");
+   if (!f) tk_fail(tk, "cannot open %s: %m", path);
+   while (fgets(line, sizeof(line), f))
+     if (strstr(line, POOL)) n++;
+   fclose(f);
+   return n;
+}
+
+int
+main(void)
+{
+   struct wl_subcompositor *subcompositor;
+   struct wl_subsurface *subsurface;
+   struct wl_surface *surface;
+   struct timespec step = { 0, 50000000 };
+   Buffer a, b, c, d, x;
+   Tk_Toplevel *top;
+   Tk *tk;
+   int i, mapped;
+
+   tk = tk_connect(PROG);
+   subcompositor = tk_bind(tk, &wl_subcompositor_interface, 1);
+   top = tk_toplevel_new(tk, APP_ID, "buffer hold", W, H);
+   surface = tk_surface_new(tk);
+   subsurface = wl_subcompositor_get_subsurface(subcompositor, surface,
+                                                tk_toplevel_surface(top));
+   wl_subsurface_set_desync(subsurface);
+   wl_surface_commit(tk_toplevel_surface(top));
+
+   _buffer_new(tk, &a);
+   _buffer_new(tk, &b);
+   _buffer_new(tk, &c);
+   _show(tk, surface, &a);
+   _released_expect(tk, &a, 0, "the first buffer, on screen");
+   _show(tk, surface, &b);
+   _released_expect(tk, &a, 1, "the first buffer, replaced");
+   _released_expect(tk, &b, 0, "the second buffer, on screen");
+   _show(tk, surface, &c);
+   _released_expect(tk, &b, 1, "the second buffer, replaced");
+   _released_expect(tk, &c, 0, "the third buffer, on screen");
+   _released_expect(tk, &a, 1, "the first buffer, two replacements later");
+
+   /* A null attach in between must not make E forget the buffer. */
+   _show(tk, surface, NULL);
+   _buffer_new(tk, &d);
+   _show(tk, surface, &d);
+   _released_expect(tk, &c, 1, "the buffer before a null attach, replaced");
+   _released_expect(tk, &d, 0, "the buffer after a null attach, on screen");
+
+   for (i = 0; i < ROUNDS; i++)
+     {
+        _buffer_new(tk, &x);
+        _show(tk, surface, &x);
+        wl_buffer_destroy(x.buffer);
+        tk_sync(tk);
+     }
+   /* a to d still exist, and the last destroyed one may be on screen. The
+    * others go once the render that might be reading them is over. */
+   for (i = 0; i < TK_WAIT_SECONDS * 20; i++)
+     {
+        mapped = _pools_mapped(tk);
+        if (mapped <= 5) break;
+        nanosleep(&step, NULL);
+        tk_sync(tk);
+     }
+   if (mapped > 5)
+     tk_fail(tk, "%d pools mapped after destroying %d buffers on screen",
+             mapped, ROUNDS);
+
+   tk_disconnect(tk);
+   printf(PROG ": ok\n");
+   return 0;
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to