aboutsummaryrefslogtreecommitdiff
path: root/shm.c
diff options
context:
space:
mode:
Diffstat (limited to 'shm.c')
-rw-r--r--shm.c610
1 files changed, 417 insertions, 193 deletions
diff --git a/shm.c b/shm.c
index ed20af3a..60e45d4d 100644
--- a/shm.c
+++ b/shm.c
@@ -55,11 +55,43 @@
*/
static off_t max_pool_size = 512 * 1024 * 1024;
-static tll(struct buffer) buffers;
-
static bool can_punch_hole = false;
static bool can_punch_hole_initialized = false;
+struct buffer_pool {
+ int fd; /* memfd */
+ struct wl_shm_pool *wl_pool;
+
+ void *real_mmapped; /* Address returned from mmap */
+ size_t mmap_size; /* Size of mmap (>= size) */
+
+ size_t ref_count;
+};
+
+struct buffer_chain;
+struct buffer_private {
+ struct buffer public;
+ struct buffer_chain *chain;
+
+ size_t ref_count;
+ bool busy; /* Owned by compositor */
+
+ struct buffer_pool *pool;
+ off_t offset; /* Offset into memfd where data begins */
+ size_t size;
+
+ bool scrollable;
+};
+
+struct buffer_chain {
+ tll(struct buffer_private *) bufs;
+ struct wl_shm *shm;
+ size_t pix_instances;
+ bool scrollable;
+};
+
+static tll(struct buffer_private *) deferred;
+
#undef MEASURE_SHM_ALLOCS
#if defined(MEASURE_SHM_ALLOCS)
static size_t max_alloced = 0;
@@ -85,34 +117,70 @@ buffer_destroy_dont_close(struct buffer *buf)
free(buf->pix);
buf->pix = NULL;
buf->wl_buf = NULL;
- buf->mmapped = NULL;
+ buf->data = NULL;
}
static void
-buffer_destroy(struct buffer *buf)
+pool_unref(struct buffer_pool *pool)
{
- buffer_destroy_dont_close(buf);
- if (buf->real_mmapped != MAP_FAILED)
- munmap(buf->real_mmapped, buf->mmap_size);
- if (buf->pool != NULL)
- wl_shm_pool_destroy(buf->pool);
- if (buf->fd >= 0)
- close(buf->fd);
+ if (pool == NULL)
+ return;
+
+ xassert(pool->ref_count > 0);
+ pool->ref_count--;
+
+ if (pool->ref_count > 0)
+ return;
- buf->real_mmapped = MAP_FAILED;
+ if (pool->real_mmapped != MAP_FAILED)
+ munmap(pool->real_mmapped, pool->mmap_size);
+ if (pool->wl_pool != NULL)
+ wl_shm_pool_destroy(pool->wl_pool);
+ if (pool->fd >= 0)
+ close(pool->fd);
+
+ pool->real_mmapped = MAP_FAILED;
+ pool->wl_pool = NULL;
+ pool->fd = -1;
+ free(pool);
+}
+
+static void
+buffer_destroy(struct buffer_private *buf)
+{
+ buffer_destroy_dont_close(&buf->public);
+ pool_unref(buf->pool);
buf->pool = NULL;
- buf->fd = -1;
- free(buf->scroll_damage);
- pixman_region32_fini(&buf->dirty);
+ free(buf->public.scroll_damage);
+ pixman_region32_fini(&buf->public.dirty);
+ free(buf);
+}
+
+static bool
+buffer_unref_no_remove_from_chain(struct buffer_private *buf)
+{
+ xassert(buf->ref_count > 0);
+ buf->ref_count--;
+
+ if (buf->ref_count > 0)
+ return false;
+
+ if (buf->busy)
+ tll_push_back(deferred, buf);
+ else
+ buffer_destroy(buf);
+ return true;
}
void
shm_fini(void)
{
- tll_foreach(buffers, it) {
- buffer_destroy(&it->item);
- tll_remove(buffers, it);
+ LOG_DBG("deferred buffers: %zu", tll_length(deferred));
+
+ tll_foreach(deferred, it) {
+ buffer_destroy(it->item);
+ tll_remove(deferred, it);
}
#if defined(MEASURE_SHM_ALLOCS) && MEASURE_SHM_ALLOCS
@@ -123,11 +191,28 @@ shm_fini(void)
static void
buffer_release(void *data, struct wl_buffer *wl_buffer)
{
- struct buffer *buffer = data;
- LOG_DBG("release: cookie=%lx (buf=%p)", buffer->cookie, (void *)buffer);
- xassert(buffer->wl_buf == wl_buffer);
+ struct buffer_private *buffer = data;
+
+ xassert(buffer->public.wl_buf == wl_buffer);
xassert(buffer->busy);
buffer->busy = false;
+
+ if (buffer->ref_count == 0) {
+ bool found = false;
+ tll_foreach(deferred, it) {
+ if (it->item == buffer) {
+ found = true;
+ tll_remove(deferred, it);
+ break;
+ }
+ }
+
+ buffer_destroy(buffer);
+
+ xassert(found);
+ if (!found)
+ LOG_WARN("deferred delete: buffer not on the 'deferred' list");
+ }
}
static const struct wl_buffer_listener buffer_listener = {
@@ -154,47 +239,53 @@ page_size(void)
#endif
static bool
-instantiate_offset(struct wl_shm *shm, struct buffer *buf, off_t new_offset)
+instantiate_offset(struct buffer_private *buf, off_t new_offset)
{
- xassert(buf->fd >= 0);
- xassert(buf->mmapped == NULL);
- xassert(buf->wl_buf == NULL);
- xassert(buf->pix == NULL);
+ xassert(buf->public.data == NULL);
+ xassert(buf->public.pix == NULL);
+ xassert(buf->public.wl_buf == NULL);
+ xassert(buf->pool != NULL);
+
+ const struct buffer_pool *pool = buf->pool;
void *mmapped = MAP_FAILED;
struct wl_buffer *wl_buf = NULL;
- pixman_image_t **pix = xcalloc(buf->pix_instances, sizeof(*pix));
+ pixman_image_t **pix = xcalloc(buf->public.pix_instances, sizeof(*pix));
- mmapped = (uint8_t *)buf->real_mmapped + new_offset;
+ mmapped = (uint8_t *)pool->real_mmapped + new_offset;
wl_buf = wl_shm_pool_create_buffer(
- buf->pool, new_offset, buf->width, buf->height, buf->stride, WL_SHM_FORMAT_ARGB8888);
+ pool->wl_pool, new_offset,
+ buf->public.width, buf->public.height, buf->public.stride,
+ WL_SHM_FORMAT_ARGB8888);
+
if (wl_buf == NULL) {
LOG_ERR("failed to create SHM buffer");
goto err;
}
/* One pixman image for each worker thread (do we really need multiple?) */
- for (size_t i = 0; i < buf->pix_instances; i++) {
+ for (size_t i = 0; i < buf->public.pix_instances; i++) {
pix[i] = pixman_image_create_bits_no_clear(
- PIXMAN_a8r8g8b8, buf->width, buf->height, (uint32_t *)mmapped, buf->stride);
+ PIXMAN_a8r8g8b8, buf->public.width, buf->public.height,
+ (uint32_t *)mmapped, buf->public.stride);
if (pix[i] == NULL) {
LOG_ERR("failed to create pixman image");
goto err;
}
}
+ buf->public.data = mmapped;
+ buf->public.wl_buf = wl_buf;
+ buf->public.pix = pix;
buf->offset = new_offset;
- buf->mmapped = mmapped;
- buf->wl_buf = wl_buf;
- buf->pix = pix;
wl_buffer_add_listener(wl_buf, &buffer_listener, buf);
return true;
err:
if (pix != NULL) {
- for (size_t i = 0; i < buf->pix_instances; i++)
+ for (size_t i = 0; i < buf->public.pix_instances; i++)
if (pix[i] != NULL)
pixman_image_unref(pix[i]);
}
@@ -206,76 +297,12 @@ err:
return false;
}
-struct buffer *
-shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie, bool scrollable, size_t pix_instances)
+static void NOINLINE
+get_new_buffers(struct buffer_chain *chain, size_t count,
+ int widths[static count], int heights[static count],
+ struct buffer *bufs[static count], bool immediate_purge)
{
- /* Purge buffers marked for purging */
- tll_foreach(buffers, it) {
- if (it->item.cookie != cookie)
- continue;
-
- if (!it->item.purge)
- continue;
-
- xassert(!it->item.busy);
-
- LOG_DBG("cookie=%lx: purging buffer %p (width=%d, height=%d): %zu KB",
- cookie, (void *)&it->item, it->item.width, it->item.height,
- it->item.size / 1024);
-
- buffer_destroy(&it->item);
- tll_remove(buffers, it);
- }
-
- struct buffer *cached = NULL;
-
- tll_foreach(buffers, it) {
- if (it->item.width != width)
- continue;
- if (it->item.height != height)
- continue;
- if (it->item.cookie != cookie)
- continue;
-
- if (it->item.busy)
- it->item.age++;
- else
-#if FORCED_DOUBLE_BUFFERING
- if (it->item.age == 0)
- it->item.age++;
- else
-#endif
- {
- LOG_DBG("cookie=%lx: re-using buffer from cache (buf=%p)",
- cookie, (void *)&it->item);
- it->item.busy = true;
- it->item.purge = false;
- pixman_region32_clear(&it->item.dirty);
- free(it->item.scroll_damage);
- it->item.scroll_damage = NULL;
- xassert(it->item.pix_instances == pix_instances);
- cached = &it->item;
- }
- }
-
- if (cached != NULL)
- return cached;
-
- /* Purge old buffers associated with this cookie */
- tll_foreach(buffers, it) {
- if (it->item.cookie != cookie)
- continue;
-
- if (it->item.busy)
- continue;
-
- if (it->item.width == width && it->item.height == height)
- continue;
-
- LOG_DBG("cookie=%lx: marking buffer %p for purging", cookie, (void *)&it->item);
- it->item.purge = true;
- }
-
+ xassert(count == 1 || !chain->scrollable);
/*
* No existing buffer available. Create a new one by:
*
@@ -286,14 +313,21 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie,
* The pixman image and the wayland buffer are now sharing memory.
*/
+ int stride[count];
+ int sizes[count];
+
+ size_t total_size = 0;
+ for (size_t i = 0; i < count; i++) {
+ stride[i] = stride_for_format_and_width(PIXMAN_a8r8g8b8, widths[i]);
+ sizes[i] = stride[i] * heights[i];
+ total_size += sizes[i];
+ }
+
int pool_fd = -1;
- const int stride = stride_for_format_and_width(PIXMAN_a8r8g8b8, width);
- const size_t size = stride * height;
void *real_mmapped = MAP_FAILED;
- struct wl_shm_pool *pool = NULL;
-
- LOG_DBG("cookie=%lx: allocating new buffer: %zu KB", cookie, size / 1024);
+ struct wl_shm_pool *wl_pool = NULL;
+ struct buffer_pool *pool = NULL;
/* Backing memory for SHM */
#if defined(MEMFD_CREATE)
@@ -312,14 +346,20 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie,
}
#if __SIZEOF_POINTER__ == 8
- off_t initial_offset = scrollable && max_pool_size > 0 ? (max_pool_size / 4) & ~(page_size() - 1) : 0;
- off_t memfd_size = scrollable && max_pool_size > 0 ? max_pool_size : size;
+ off_t offset = chain->scrollable && max_pool_size > 0
+ ? (max_pool_size / 4) & ~(page_size() - 1)
+ : 0;
+ off_t memfd_size = chain->scrollable && max_pool_size > 0
+ ? max_pool_size
+ : total_size;
#else
- off_t initial_offset = 0;
- off_t memfd_size = size;
+ off_t offset = 0;
+ off_t memfd_size = total_size;
#endif
- LOG_DBG("memfd-size: %lu, initial offset: %lu", memfd_size, initial_offset);
+ xassert(chain->scrollable || (offset == 0 && memfd_size == total_size));
+
+ LOG_DBG("memfd-size: %lu, initial offset: %lu", memfd_size, offset);
if (ftruncate(pool_fd, memfd_size) == -1) {
LOG_ERRNO("failed to set size of SHM backing memory file");
@@ -344,10 +384,10 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie,
#endif
}
- if (scrollable && !can_punch_hole) {
- initial_offset = 0;
- memfd_size = size;
- scrollable = false;
+ if (chain->scrollable && !can_punch_hole) {
+ offset = 0;
+ memfd_size = total_size;
+ chain->scrollable = false;
if (ftruncate(pool_fd, memfd_size) < 0) {
LOG_ERRNO("failed to set size of SHM backing memory file");
@@ -375,37 +415,66 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie,
}
#endif
- pool = wl_shm_create_pool(shm, pool_fd, memfd_size);
- if (pool == NULL) {
+ wl_pool = wl_shm_create_pool(chain->shm, pool_fd, memfd_size);
+ if (wl_pool == NULL) {
LOG_ERR("failed to create SHM pool");
goto err;
}
- /* Push to list of available buffers, but marked as 'busy' */
- tll_push_back(
- buffers,
- ((struct buffer){
- .cookie = cookie,
- .width = width,
- .height = height,
- .stride = stride,
+ pool = xmalloc(sizeof(*pool));
+ if (pool == NULL) {
+ LOG_ERRNO("failed to allocate buffer pool");
+ goto err;
+ }
+
+ *pool = (struct buffer_pool){
+ .fd = pool_fd,
+ .wl_pool = wl_pool,
+ .real_mmapped = real_mmapped,
+ .mmap_size = memfd_size,
+ .ref_count = 0,
+ };
+
+ for (size_t i = 0; i < count; i++) {
+ if (sizes[i] == 0) {
+ bufs[i] = NULL;
+ continue;
+ }
+
+ /* Push to list of available buffers, but marked as 'busy' */
+ struct buffer_private *buf = xmalloc(sizeof(*buf));
+ *buf = (struct buffer_private){
+ .public = {
+ .width = widths[i],
+ .height = heights[i],
+ .stride = stride[i],
+ .pix_instances = chain->pix_instances,
+ .age = 1234, /* Force a full repaint */
+ },
+ .chain = chain,
+ .ref_count = immediate_purge ? 0 : 1,
.busy = true,
- .size = size,
- .pix_instances = pix_instances,
- .fd = pool_fd,
.pool = pool,
- .scrollable = scrollable,
- .real_mmapped = real_mmapped,
- .mmap_size = memfd_size,
.offset = 0,
- .age = 1234, /* Force a full repaint */
- }));
+ .size = sizes[i],
+ .scrollable = chain->scrollable,
+ };
- struct buffer *ret = &tll_back(buffers);
- if (!instantiate_offset(shm, ret, initial_offset))
- goto err;
+ if (!instantiate_offset(buf, offset)) {
+ free(buf);
+ goto err;
+ }
- pixman_region32_init(&ret->dirty);
+ if (immediate_purge)
+ tll_push_front(deferred, buf);
+ else
+ tll_push_front(chain->bufs, buf);
+
+ pixman_region32_init(&buf->public.dirty);
+ pool->ref_count++;
+ offset += buf->size;
+ bufs[i] = &buf->public;
+ }
#if defined(MEASURE_SHM_ALLOCS) && MEASURE_SHM_ALLOCS
{
@@ -417,11 +486,19 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie,
}
#endif
- return ret;
+ if (!shm_can_scroll(bufs[0])) {
+ /* We only need to keep the pool FD open if we’re going to SHM
+ * scroll it */
+ close(pool_fd);
+ pool->fd = -1;
+ }
+
+ return;
err:
- if (pool != NULL)
- wl_shm_pool_destroy(pool);
+ pool_unref(pool);
+ if (wl_pool != NULL)
+ wl_shm_pool_destroy(wl_pool);
if (real_mmapped != MAP_FAILED)
munmap(real_mmapped, memfd_size);
if (pool_fd != -1)
@@ -429,13 +506,80 @@ err:
/* We don't handle this */
abort();
- return NULL;
+}
+
+void
+shm_get_many(struct buffer_chain *chain, size_t count,
+ int widths[static count], int heights[static count],
+ struct buffer *bufs[static count])
+{
+ get_new_buffers(chain, count, widths, heights, bufs, true);
+}
+
+struct buffer *
+shm_get_buffer(struct buffer_chain *chain, int width, int height)
+{
+ LOG_DBG(
+ "chain=%p: looking for a re-usable %dx%d buffer "
+ "among %zu potential buffers",
+ (void *)chain, width, height, tll_length(chain->bufs));
+
+ struct buffer_private *cached = NULL;
+ tll_foreach(chain->bufs, it) {
+ struct buffer_private *buf = it->item;
+
+ if (buf->public.width != width || buf->public.height != height) {
+ LOG_DBG("purging mismatching buffer %p", (void *)buf);
+ if (buffer_unref_no_remove_from_chain(buf))
+ tll_remove(chain->bufs, it);
+ continue;
+ }
+
+ if (buf->busy)
+ buf->public.age++;
+ else
+#if FORCED_DOUBLE_BUFFERING
+ if (buf->age == 0)
+ buf->age++;
+ else
+#endif
+ {
+ if (cached == NULL) {
+ LOG_DBG("re-using buffer %p from cache", (void *)buf);
+ buf->busy = true;
+ pixman_region32_clear(&buf->public.dirty);
+ free(buf->public.scroll_damage);
+ buf->public.scroll_damage = NULL;
+ xassert(buf->public.pix_instances == chain->pix_instances);
+ cached = it->item;
+ } else {
+ /* We have multiple buffers eligible for
+ * re-use. Pick the “youngest” one, and mark the
+ * other one for purging */
+ if (buf->public.age < cached->public.age) {
+ shm_unref(&cached->public);
+ cached = buf;
+ } else {
+ if (buffer_unref_no_remove_from_chain(buf))
+ tll_remove(chain->bufs, it);
+ }
+ }
+ }
+ }
+
+ if (cached != NULL)
+ return &cached->public;
+
+ struct buffer *ret;
+ get_new_buffers(chain, 1, &width, &height, &ret, false);
+ return ret;
}
bool
-shm_can_scroll(const struct buffer *buf)
+shm_can_scroll(const struct buffer *_buf)
{
#if __SIZEOF_POINTER__ == 8
+ const struct buffer_private *buf = (const struct buffer_private *)_buf;
return can_punch_hole && max_pool_size > 0 && buf->scrollable;
#else
/* Not enough virtual address space in 32-bit */
@@ -445,14 +589,20 @@ shm_can_scroll(const struct buffer *buf)
#if __SIZEOF_POINTER__ == 8 && defined(FALLOC_FL_PUNCH_HOLE)
static bool
-wrap_buffer(struct wl_shm *shm, struct buffer *buf, off_t new_offset)
+wrap_buffer(struct buffer_private *buf, off_t new_offset)
{
+ struct buffer_pool *pool = buf->pool;
+ xassert(pool->ref_count == 1);
+
/* We don't allow overlapping offsets */
- off_t UNUSED diff =
- new_offset < buf->offset ? buf->offset - new_offset : new_offset - buf->offset;
+ off_t UNUSED diff = new_offset < buf->offset
+ ? buf->offset - new_offset
+ : new_offset - buf->offset;
xassert(diff > buf->size);
- memcpy((uint8_t *)buf->real_mmapped + new_offset, buf->mmapped, buf->size);
+ memcpy((uint8_t *)pool->real_mmapped + new_offset,
+ buf->public.data,
+ buf->size);
off_t trim_ofs, trim_len;
if (new_offset > buf->offset) {
@@ -462,11 +612,11 @@ wrap_buffer(struct wl_shm *shm, struct buffer *buf, off_t new_offset)
} else {
/* Trim everything *after* the new buffer location */
trim_ofs = new_offset + buf->size;
- trim_len = buf->mmap_size - trim_ofs;
+ trim_len = pool->mmap_size - trim_ofs;
}
if (fallocate(
- buf->fd,
+ pool->fd,
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
trim_ofs, trim_len) < 0)
{
@@ -475,30 +625,34 @@ wrap_buffer(struct wl_shm *shm, struct buffer *buf, off_t new_offset)
}
/* Re-instantiate pixman+wl_buffer+raw pointersw */
- buffer_destroy_dont_close(buf);
- return instantiate_offset(shm, buf, new_offset);
+ buffer_destroy_dont_close(&buf->public);
+ return instantiate_offset(buf, new_offset);
}
static bool
-shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows,
+shm_scroll_forward(struct buffer_private *buf, int rows,
int top_margin, int top_keep_rows,
int bottom_margin, int bottom_keep_rows)
{
+ struct buffer_pool *pool = buf->pool;
+
xassert(can_punch_hole);
xassert(buf->busy);
- xassert(buf->pix);
- xassert(buf->wl_buf);
- xassert(buf->fd >= 0);
+ xassert(buf->public.pix != NULL);
+ xassert(buf->public.wl_buf != NULL);
+ xassert(pool != NULL);
+ xassert(pool->ref_count == 1);
+ xassert(pool->fd >= 0);
- LOG_DBG("scrolling %d rows (%d bytes)", rows, rows * buf->stride);
+ LOG_DBG("scrolling %d rows (%d bytes)", rows, rows * buf->public.stride);
- const off_t diff = rows * buf->stride;
+ const off_t diff = rows * buf->public.stride;
xassert(rows > 0);
xassert(diff < buf->size);
if (buf->offset + diff + buf->size > max_pool_size) {
LOG_DBG("memfd offset wrap around");
- if (!wrap_buffer(shm, buf, 0))
+ if (!wrap_buffer(buf, 0))
goto err;
}
@@ -507,6 +661,7 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows,
xassert(new_offset + buf->size <= max_pool_size);
#if TIME_SCROLL
+ struct timeval tot;
struct timeval time1;
gettimeofday(&time1, NULL);
@@ -515,10 +670,13 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows,
if (top_keep_rows > 0) {
/* Copy current 'top' region to its new location */
+ const int stride = buf->public.stride;
+ uint8_t *base = buf->public.data;
+
memmove(
- (uint8_t *)buf->mmapped + (top_margin + rows) * buf->stride,
- (uint8_t *)buf->mmapped + (top_margin + 0) * buf->stride,
- top_keep_rows * buf->stride);
+ base + (top_margin + rows) * stride,
+ base + (top_margin + 0) * stride,
+ top_keep_rows * stride);
#if TIME_SCROLL
gettimeofday(&time2, NULL);
@@ -528,14 +686,14 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows,
}
/* Destroy old objects (they point to the old offset) */
- buffer_destroy_dont_close(buf);
+ buffer_destroy_dont_close(&buf->public);
/* Free unused memory - everything up until the new offset */
const off_t trim_ofs = 0;
const off_t trim_len = new_offset;
if (fallocate(
- buf->fd,
+ pool->fd,
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
trim_ofs, trim_len) < 0)
{
@@ -551,7 +709,7 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows,
#endif
/* Re-instantiate pixman+wl_buffer+raw pointersw */
- bool ret = instantiate_offset(shm, buf, new_offset);
+ bool ret = instantiate_offset(buf, new_offset);
#if TIME_SCROLL
struct timeval time4;
@@ -562,10 +720,14 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows,
if (ret && bottom_keep_rows > 0) {
/* Copy 'bottom' region to its new location */
+ const size_t size = buf->size;
+ const int stride = buf->public.stride;
+ uint8_t *base = buf->public.data;
+
memmove(
- (uint8_t *)buf->mmapped + buf->size - (bottom_margin + bottom_keep_rows) * buf->stride,
- (uint8_t *)buf->mmapped + buf->size - (bottom_margin + rows + bottom_keep_rows) * buf->stride,
- bottom_keep_rows * buf->stride);
+ base + size - (bottom_margin + bottom_keep_rows) * stride,
+ base + size - (bottom_margin + rows + bottom_keep_rows) * stride,
+ bottom_keep_rows * stride);
#if TIME_SCROLL
struct timeval time5;
@@ -584,16 +746,19 @@ err:
}
static bool
-shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows,
+shm_scroll_reverse(struct buffer_private *buf, int rows,
int top_margin, int top_keep_rows,
int bottom_margin, int bottom_keep_rows)
{
xassert(rows > 0);
- const off_t diff = rows * buf->stride;
+ struct buffer_pool *pool = buf->pool;
+ xassert(pool->ref_count == 1);
+
+ const off_t diff = rows * buf->public.stride;
if (diff > buf->offset) {
LOG_DBG("memfd offset reverse wrap-around");
- if (!wrap_buffer(shm, buf, (max_pool_size - buf->size) & ~(page_size() - 1)))
+ if (!wrap_buffer(buf, (max_pool_size - buf->size) & ~(page_size() - 1)))
goto err;
}
@@ -611,10 +776,14 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows,
if (bottom_keep_rows > 0) {
/* Copy 'bottom' region to its new location */
+ const size_t size = buf->size;
+ const int stride = buf->public.stride;
+ uint8_t *base = buf->public.data;
+
memmove(
- (uint8_t *)buf->mmapped + buf->size - (bottom_margin + rows + bottom_keep_rows) * buf->stride,
- (uint8_t *)buf->mmapped + buf->size - (bottom_margin + bottom_keep_rows) * buf->stride,
- bottom_keep_rows * buf->stride);
+ base + size - (bottom_margin + rows + bottom_keep_rows) * stride,
+ base + size - (bottom_margin + bottom_keep_rows) * stride,
+ bottom_keep_rows * stride);
#if TIME_SCROLL
gettimeofday(&time1, NULL);
@@ -624,14 +793,14 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows,
}
/* Destroy old objects (they point to the old offset) */
- buffer_destroy_dont_close(buf);
+ buffer_destroy_dont_close(&buf->public);
/* Free unused memory - everything after the relocated buffer */
const off_t trim_ofs = new_offset + buf->size;
- const off_t trim_len = buf->mmap_size - trim_ofs;
+ const off_t trim_len = pool->mmap_size - trim_ofs;
if (fallocate(
- buf->fd,
+ pool->fd,
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
trim_ofs, trim_len) < 0)
{
@@ -646,7 +815,7 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows,
#endif
/* Re-instantiate pixman+wl_buffer+raw pointers */
- bool ret = instantiate_offset(shm, buf, new_offset);
+ bool ret = instantiate_offset(buf, new_offset);
#if TIME_SCROLL
struct timeval time3;
@@ -657,10 +826,13 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows,
if (ret && top_keep_rows > 0) {
/* Copy current 'top' region to its new location */
+ const int stride = buf->public.stride;
+ uint8_t *base = buf->public.data;
+
memmove(
- (uint8_t *)buf->mmapped + (top_margin + 0) * buf->stride,
- (uint8_t *)buf->mmapped + (top_margin + rows) * buf->stride,
- top_keep_rows * buf->stride);
+ base + (top_margin + 0) * stride,
+ base + (top_margin + rows) * stride,
+ top_keep_rows * stride);
#if TIME_SCROLL
struct timeval time4;
@@ -679,36 +851,88 @@ err:
#endif /* FALLOC_FL_PUNCH_HOLE */
bool
-shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows,
+shm_scroll(struct buffer *_buf, int rows,
int top_margin, int top_keep_rows,
int bottom_margin, int bottom_keep_rows)
{
#if __SIZEOF_POINTER__ == 8 && defined(FALLOC_FL_PUNCH_HOLE)
- if (!shm_can_scroll(buf))
+ if (!shm_can_scroll(_buf))
return false;
+ struct buffer_private *buf = (struct buffer_private *)_buf;
+
xassert(rows != 0);
return rows > 0
- ? shm_scroll_forward(shm, buf, rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows)
- : shm_scroll_reverse(shm, buf, -rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows);
+ ? shm_scroll_forward(buf, rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows)
+ : shm_scroll_reverse(buf, -rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows);
#else
return false;
#endif
}
void
-shm_purge(struct wl_shm *shm, unsigned long cookie)
+shm_purge(struct buffer_chain *chain)
{
- LOG_DBG("cookie=%lx: purging all buffers", cookie);
+ LOG_DBG("chain: %p: purging all buffers", (void *)chain);
/* Purge old buffers associated with this cookie */
- tll_foreach(buffers, it) {
- if (it->item.cookie != cookie)
+ tll_foreach(chain->bufs, it) {
+ if (buffer_unref_no_remove_from_chain(it->item))
+ tll_remove(chain->bufs, it);
+ }
+}
+
+void
+shm_addref(struct buffer *_buf)
+{
+ struct buffer_private *buf = (struct buffer_private *)_buf;
+ buf->ref_count++;
+}
+
+void
+shm_unref(struct buffer *_buf)
+{
+ if (_buf == NULL)
+ return;
+
+ struct buffer_private *buf = (struct buffer_private *)_buf;
+ struct buffer_chain *chain = buf->chain;
+
+ tll_foreach(chain->bufs, it) {
+ if (it->item != buf)
continue;
- xassert(!it->item.busy);
+ if (buffer_unref_no_remove_from_chain(buf))
+ tll_remove(chain->bufs, it);
+ break;
+ }
+}
+
+struct buffer_chain *
+shm_chain_new(struct wl_shm *shm, bool scrollable, size_t pix_instances)
+{
+ struct buffer_chain *chain = xmalloc(sizeof(*chain));
+ *chain = (struct buffer_chain){
+ .bufs = tll_init(),
+ .shm = shm,
+ .pix_instances = pix_instances,
+ .scrollable = scrollable,
+ };
+ return chain;
+}
- buffer_destroy(&it->item);
- tll_remove(buffers, it);
+void
+shm_chain_free(struct buffer_chain *chain)
+{
+ if (chain == NULL)
+ return;
+
+ shm_purge(chain);
+
+ if (tll_length(chain->bufs) > 0) {
+ BUG("chain=%p: there are buffers remaining; "
+ "is there a missing call to shm_unref()?", (void *)chain);
}
+
+ free(chain);
}