[Mesa-dev] [PATCH v4 9/9] egl/wayland: add dri2_wl_free_buffers() helper (v3)

Gwan-gyeong Mun elongbug at gmail.com
Thu Nov 2 22:10:54 UTC 2017


This deduplicates free routines of color_buffers array.

v2:
 - Add clear_all argument to check clearing all of color_buffers or not.
 - Fixes from Eric's review:
   a) polish check routine of check_lock and color_buffers[i].locked
   b) move 'native_buffer = NULL' to avoid leaking locked buffers
 - Fixes from Emil's review:
   a) drop the unneeded cast
   b) apply dri2_wl_free_buffers to update_buffers() and swrast_update_buffers()

v3: Use a dri2_surface_free_image() helper

Signed-off-by: Mun Gwan-gyeong <elongbug at gmail.com>
---
 src/egl/drivers/dri2/platform_wayland.c | 85 +++++++++++++--------------------
 1 file changed, 33 insertions(+), 52 deletions(-)

diff --git a/src/egl/drivers/dri2/platform_wayland.c b/src/egl/drivers/dri2/platform_wayland.c
index d864355eb4..b77f05ca15 100644
--- a/src/egl/drivers/dri2/platform_wayland.c
+++ b/src/egl/drivers/dri2/platform_wayland.c
@@ -252,6 +252,35 @@ dri2_wl_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
    return NULL;
 }
 
+static void
+dri2_wl_free_buffers(struct dri2_egl_surface *dri2_surf, bool check_lock,
+                     bool clear_all)
+{
+   for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
+      bool clear_buffer = false;
+
+      if (dri2_surf->color_buffers[i].native_buffer &&
+          (!check_lock || !dri2_surf->color_buffers[i].locked)) {
+         wl_buffer_destroy(dri2_surf->color_buffers[i].native_buffer);
+         dri2_surf->color_buffers[i].native_buffer = NULL;
+         dri2_surf->color_buffers[i].locked = false;
+         clear_buffer = true;
+      }
+
+      if (clear_all || clear_buffer) {
+         dri2_surface_free_image(&dri2_surf->base,
+                                 &dri2_surf->color_buffers[i].dri_image);
+         dri2_surface_free_image(&dri2_surf->base,
+                                 &dri2_surf->color_buffers[i].linear_copy);
+         if (dri2_surf->color_buffers[i].data)
+            munmap(dri2_surf->color_buffers[i].data,
+                   dri2_surf->color_buffers[i].data_size);
+
+         dri2_surf->color_buffers[i].data = NULL;
+      }
+   }
+}
+
 /**
  * Called via eglDestroySurface(), drv->API.DestroySurface().
  */
@@ -265,17 +294,7 @@ dri2_wl_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
 
    dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
 
-   for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
-      if (dri2_surf->color_buffers[i].native_buffer)
-         wl_buffer_destroy(dri2_surf->color_buffers[i].native_buffer);
-      dri2_surface_free_image(&dri2_surf->base,
-                              &dri2_surf->color_buffers[i].dri_image);
-      dri2_surface_free_image(&dri2_surf->base,
-                              &dri2_surf->color_buffers[i].linear_copy);
-      if (dri2_surf->color_buffers[i].data)
-         munmap(dri2_surf->color_buffers[i].data,
-                dri2_surf->color_buffers[i].data_size);
-   }
+   dri2_wl_free_buffers(dri2_surf, false, true);
 
    if (dri2_dpy->dri2)
       dri2_egl_surface_free_local_buffers(dri2_surf);
@@ -307,22 +326,7 @@ dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf)
    struct dri2_egl_display *dri2_dpy =
       dri2_egl_display(dri2_surf->base.Resource.Display);
 
-   for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
-      if (dri2_surf->color_buffers[i].native_buffer &&
-          !dri2_surf->color_buffers[i].locked)
-         wl_buffer_destroy(dri2_surf->color_buffers[i].native_buffer);
-      dri2_surface_free_image(&dri2_surf->base,
-                              &dri2_surf->color_buffers[i].dri_image);
-      dri2_surface_free_image(&dri2_surf->base,
-                              &dri2_surf->color_buffers[i].linear_copy);
-      if (dri2_surf->color_buffers[i].data)
-         munmap(dri2_surf->color_buffers[i].data,
-                dri2_surf->color_buffers[i].data_size);
-
-      dri2_surf->color_buffers[i].native_buffer = NULL;
-      dri2_surf->color_buffers[i].data = NULL;
-      dri2_surf->color_buffers[i].locked = false;
-   }
+   dri2_wl_free_buffers(dri2_surf, true, true);
 
    if (dri2_dpy->dri2)
       dri2_egl_surface_free_local_buffers(dri2_surf);
@@ -487,9 +491,6 @@ back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
 static int
 update_buffers(struct dri2_egl_surface *dri2_surf)
 {
-   struct dri2_egl_display *dri2_dpy =
-      dri2_egl_display(dri2_surf->base.Resource.Display);
-
    if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
        dri2_surf->base.Height != dri2_surf->wl_win->height) {
 
@@ -509,18 +510,7 @@ update_buffers(struct dri2_egl_surface *dri2_surf)
    /* If we have an extra unlocked buffer at this point, we had to do triple
     * buffering for a while, but now can go back to just double buffering.
     * That means we can free any unlocked buffer now. */
-   for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
-      if (!dri2_surf->color_buffers[i].locked &&
-          dri2_surf->color_buffers[i].native_buffer) {
-         wl_buffer_destroy(dri2_surf->color_buffers[i].native_buffer);
-         dri2_surface_free_image(&dri2_surf->base,
-                                 &dri2_surf->color_buffers[i].dri_image);
-         if (dri2_dpy->is_different_gpu)
-            dri2_surface_free_image(&dri2_surf->base,
-                                    &dri2_surf->color_buffers[i].linear_copy);
-         dri2_surf->color_buffers[i].native_buffer = NULL;
-      }
-   }
+   dri2_wl_free_buffers(dri2_surf, true, false);
 
    return 0;
 }
@@ -1621,16 +1611,7 @@ swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
    /* If we have an extra unlocked buffer at this point, we had to do triple
     * buffering for a while, but now can go back to just double buffering.
     * That means we can free any unlocked buffer now. */
-   for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
-      if (!dri2_surf->color_buffers[i].locked &&
-          dri2_surf->color_buffers[i].native_buffer) {
-         wl_buffer_destroy(dri2_surf->color_buffers[i].native_buffer);
-         munmap(dri2_surf->color_buffers[i].data,
-                dri2_surf->color_buffers[i].data_size);
-         dri2_surf->color_buffers[i].native_buffer = NULL;
-         dri2_surf->color_buffers[i].data = NULL;
-      }
-   }
+   dri2_wl_free_buffers(dri2_surf, true, false);
 
    return 0;
 }
-- 
2.15.0



More information about the mesa-dev mailing list