From a486851bdd6bbf2fee9a5009c30770f22ca102ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 15 Jul 2021 19:19:31 +0200 Subject: [PATCH] shm: auto-purge when we have multiple buffers eligible for re-use MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It may happen that we end up with multiple non-busy, same-sized buffers for the same cookie (context), and thus eligible for re-use. Before this patch, we would keep all those buffers around. This is completely unnecessary. Under normal circumstances, we’ll either be re-using a single buffer, or swap between two. In the second case, the “other” buffer is always busy, and thus not eligible for re-use. So, if we _do_ detect multiple, re-usable buffers, pick the one with the lowest “age” (increasing the chance of applying damage tracking, instead of re-drawing everything), and mark the other one for purging. --- shm.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/shm.c b/shm.c index 17b9c15d..064d52ec 100644 --- a/shm.c +++ b/shm.c @@ -494,6 +494,15 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie, it->item.scroll_damage = NULL; xassert(it->item.pix_instances == pix_instances); cached = &it->item; + } else { + /* We have multiple buffers eligable for + * re-use. Pick the “youngest” one, and mark the + * other one for purging */ + if (it->item.age < cached->age) { + cached->purge = true; + cached = &it->item; + } else + it->item.purge = true; } } }