aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2021-07-19 19:04:38 +0200
committerSimon Zeni <simon@bl4ckb0ne.ca>2021-07-19 13:13:10 -0400
commit8afb4d8bf08c54ddb7f531ff6a253a2a88470ab1 (patch)
treee8f251ca15573eb8206184fd7f6c4f6b5d6f33f9
parentf94eb174c793c09adad8fbadb3333b22f28ee743 (diff)
buffer: re-use wlr_shm_client_buffer
The first time wlr_buffer_from_resource is called with a wl_buffer resource that originates from wl_shm, create a new wlr_shm_client_buffer as usual. If wlr_buffer_from_resource is called multiple times, re-use the existing wlr_shm_client_buffer. This commit changes how the wlr_shm_client_buffer lifetime is managed: previously it was destroyed as soon as the wlr_buffer was released. With this commit it's destroyed when the wl_buffer resource is. Apart from de-duplicating wlr_shm_client_buffer creations, this allows to easily track when a wlr_shm_client_buffer is re-used. This is useful for the renderer and the backends, e.g. the Pixman renderer can keep using the same Pixman image if the buffer is re-used. In the future, this will also allow to re-use resources in the Wayland and X11 backends (remote wl_buffer objects for Wayland, pixmaps for X11).
-rw-r--r--include/types/wlr_buffer.h3
-rw-r--r--types/wlr_buffer.c21
2 files changed, 16 insertions, 8 deletions
diff --git a/include/types/wlr_buffer.h b/include/types/wlr_buffer.h
index 896109fd..4817e23f 100644
--- a/include/types/wlr_buffer.h
+++ b/include/types/wlr_buffer.h
@@ -22,9 +22,6 @@ struct wlr_shm_client_buffer {
struct wl_listener release;
};
-struct wlr_shm_client_buffer *shm_client_buffer_create(
- struct wl_resource *resource);
-
/**
* A read-only buffer that holds a data pointer.
*
diff --git a/types/wlr_buffer.c b/types/wlr_buffer.c
index f32fd928..d44c2b4e 100644
--- a/types/wlr_buffer.c
+++ b/types/wlr_buffer.c
@@ -153,6 +153,8 @@ static void client_buffer_handle_source_destroy(struct wl_listener *listener,
client_buffer->source = NULL;
}
+static struct wlr_shm_client_buffer *shm_client_buffer_get_or_create(
+ struct wl_resource *resource);
static bool buffer_is_shm_client_buffer(struct wlr_buffer *buffer);
static struct wlr_shm_client_buffer *shm_client_buffer_from_buffer(
struct wlr_buffer *buffer);
@@ -164,15 +166,12 @@ struct wlr_buffer *wlr_buffer_from_resource(struct wlr_renderer *renderer,
struct wlr_buffer *buffer;
if (wl_shm_buffer_get(resource) != NULL) {
struct wlr_shm_client_buffer *shm_client_buffer =
- shm_client_buffer_create(resource);
+ shm_client_buffer_get_or_create(resource);
if (shm_client_buffer == NULL) {
wlr_log(WLR_ERROR, "Failed to create shm client buffer");
return NULL;
}
-
- // Ensure the buffer will be released before being destroyed
buffer = wlr_buffer_lock(&shm_client_buffer->base);
- wlr_buffer_drop(&shm_client_buffer->base);
} else if (wlr_dmabuf_v1_resource_is_buffer(resource)) {
struct wlr_dmabuf_v1_buffer *dmabuf =
wlr_dmabuf_v1_buffer_from_buffer_resource(resource);
@@ -347,6 +346,9 @@ static void shm_client_buffer_resource_handle_destroy(
buffer->shm_buffer = NULL;
wl_list_remove(&buffer->resource_destroy.link);
wl_list_init(&buffer->resource_destroy.link);
+
+ // This might destroy the buffer
+ wlr_buffer_drop(&buffer->base);
}
static void shm_client_buffer_handle_release(struct wl_listener *listener,
@@ -358,11 +360,20 @@ static void shm_client_buffer_handle_release(struct wl_listener *listener,
}
}
-struct wlr_shm_client_buffer *shm_client_buffer_create(
+static struct wlr_shm_client_buffer *shm_client_buffer_get_or_create(
struct wl_resource *resource) {
struct wl_shm_buffer *shm_buffer = wl_shm_buffer_get(resource);
assert(shm_buffer != NULL);
+ struct wl_listener *resource_destroy_listener =
+ wl_resource_get_destroy_listener(resource,
+ shm_client_buffer_resource_handle_destroy);
+ if (resource_destroy_listener != NULL) {
+ struct wlr_shm_client_buffer *buffer =
+ wl_container_of(resource_destroy_listener, buffer, resource_destroy);
+ return buffer;
+ }
+
int32_t width = wl_shm_buffer_get_width(shm_buffer);
int32_t height = wl_shm_buffer_get_height(shm_buffer);