Mesa (main): llvmpipe: do not leak map of display target in fs setup

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Jul 9 16:17:35 UTC 2021


Module: Mesa
Branch: main
Commit: 3e8c6b7ae26d59d5c375801dbfe1fccc27b4c387
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=3e8c6b7ae26d59d5c375801dbfe1fccc27b4c387

Author: Heinrich Fink <hfink at snap.com>
Date:   Mon Jul  5 19:54:20 2021 +0200

llvmpipe: do not leak map of display target in fs setup

For fragment shader textures that are backed by a display target, do not
leak the mapped pointer, but unmap before unref'ing its associated
pipe_resource instances.

Also, make sure that the pointer that's mapped into a jit texture stays
valid while rasterization works on a jit context copy by mapping the
display target again during scene setup, and unmapping when finalizing
rasterization.

v2 (Daniel Stone):
    - remove redundant helper function for [un]mapping DT, use
      llvmpipe_resource_[un]map right away

v3 (Emil Velikov):
    - add comment in lp_setup_set_fragment_sampler_views to explain
    unmapping current texture early in the loop

Signed-off-by: Heinrich Fink <hfink at snap.com>
Reviewed-by: Emil Velikov <emil.velikov at collabora.com>
Reviewed-by: Roland Scheidegger <sroland at vmware.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11741>

---

 src/gallium/drivers/llvmpipe/lp_scene.c |  7 +++++++
 src/gallium/drivers/llvmpipe/lp_setup.c | 19 +++++++++++--------
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/gallium/drivers/llvmpipe/lp_scene.c b/src/gallium/drivers/llvmpipe/lp_scene.c
index 44a34902515..0888341e049 100644
--- a/src/gallium/drivers/llvmpipe/lp_scene.c
+++ b/src/gallium/drivers/llvmpipe/lp_scene.c
@@ -278,6 +278,7 @@ lp_scene_end_rasterization(struct lp_scene *scene )
                             ref->resource[i]->height0,
                             llvmpipe_resource_size(ref->resource[i]));
             j++;
+            llvmpipe_resource_unmap(ref->resource[i], 0, 0);
             pipe_resource_reference(&ref->resource[i], NULL);
          }
       }
@@ -439,6 +440,12 @@ lp_scene_add_resource_reference(struct lp_scene *scene,
       memset(ref, 0, sizeof *ref);
    }
 
+   /* Map resource again to increment the map count. We likely use the
+    * already-mapped pointer in a texture of the jit context, and that pointer
+    * needs to stay mapped during rasterization. This map is unmap'ed when
+    * finalizing scene rasterization. */
+   llvmpipe_resource_map(resource, 0, 0, LP_TEX_USAGE_READ);
+
    /* Append the reference to the reference block.
     */
    pipe_resource_reference(&ref->resource[ref->count++], resource);
diff --git a/src/gallium/drivers/llvmpipe/lp_setup.c b/src/gallium/drivers/llvmpipe/lp_setup.c
index 707ba5df29f..b731dd6d032 100644
--- a/src/gallium/drivers/llvmpipe/lp_setup.c
+++ b/src/gallium/drivers/llvmpipe/lp_setup.c
@@ -898,6 +898,12 @@ lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
    for (i = 0; i < max_tex_num; i++) {
       struct pipe_sampler_view *view = i < num ? views[i] : NULL;
 
+      /* We are going to overwrite/unref the current texture further below. If
+       * set, make sure to unmap its resource to avoid leaking previous
+       * mapping.  */
+      if (setup->fs.current_tex[i])
+         llvmpipe_resource_unmap(setup->fs.current_tex[i], 0, 0);
+
       if (view) {
          struct pipe_resource *res = view->texture;
          struct llvmpipe_resource *lp_tex = llvmpipe_resource(res);
@@ -1002,13 +1008,7 @@ lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,
          }
          else {
             /* display target texture/surface */
-            /*
-             * XXX: Where should this be unmapped?
-             */
-            struct llvmpipe_screen *screen = llvmpipe_screen(res->screen);
-            struct sw_winsys *winsys = screen->winsys;
-            jit_tex->base = winsys->displaytarget_map(winsys, lp_tex->dt,
-                                                         PIPE_MAP_READ);
+            jit_tex->base = llvmpipe_resource_map(res, 0, 0, LP_TEX_USAGE_READ);
             jit_tex->row_stride[0] = lp_tex->row_stride[0];
             jit_tex->img_stride[0] = lp_tex->img_stride[0];
             jit_tex->mip_offsets[0] = 0;
@@ -1419,7 +1419,10 @@ lp_setup_destroy( struct lp_setup_context *setup )
    util_unreference_framebuffer_state(&setup->fb);
 
    for (i = 0; i < ARRAY_SIZE(setup->fs.current_tex); i++) {
-      pipe_resource_reference(&setup->fs.current_tex[i], NULL);
+      struct pipe_resource **res_ptr = &setup->fs.current_tex[i];
+      if (*res_ptr)
+         llvmpipe_resource_unmap(*res_ptr, 0, 0);
+      pipe_resource_reference(res_ptr, NULL);
    }
 
    for (i = 0; i < ARRAY_SIZE(setup->constants); i++) {



More information about the mesa-commit mailing list