aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2020-12-11 18:14:58 +0100
committerSimon Ser <contact@emersion.fr>2020-12-21 16:30:47 +0100
commitdefcd9b025c8a6ce844f8b2c772ee763cbc334b5 (patch)
treeee9b919b4fcabbc73695dfa7f8777b22f0b8d2be /backend
parentde896caceb9f6354a99c9435c3a429ff52b6ca4a (diff)
backend/wayland: re-use wl_buffers
Instead of re-importing a buffer each time we submit a new frame, re-use the wl_buffer objects if possible.
Diffstat (limited to 'backend')
-rw-r--r--backend/wayland/backend.c6
-rw-r--r--backend/wayland/output.c38
2 files changed, 40 insertions, 4 deletions
diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c
index 8e77133d..11b769bf 100644
--- a/backend/wayland/backend.c
+++ b/backend/wayland/backend.c
@@ -210,6 +210,11 @@ static void backend_destroy(struct wlr_backend *backend) {
wlr_drm_format_set_finish(&wl->linux_dmabuf_v1_formats);
+ struct wlr_wl_buffer *buffer, *tmp_buffer;
+ wl_list_for_each_safe(buffer, tmp_buffer, &wl->buffers, link) {
+ destroy_wl_buffer(buffer);
+ }
+
destroy_wl_seats(wl);
if (wl->zxdg_decoration_manager_v1) {
zxdg_decoration_manager_v1_destroy(wl->zxdg_decoration_manager_v1);
@@ -270,6 +275,7 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_display *display,
wl_list_init(&wl->devices);
wl_list_init(&wl->outputs);
wl_list_init(&wl->seats);
+ wl_list_init(&wl->buffers);
wl->remote_display = wl_display_connect(remote);
if (!wl->remote_display) {
diff --git a/backend/wayland/output.c b/backend/wayland/output.c
index 188c22b6..db9056d5 100644
--- a/backend/wayland/output.c
+++ b/backend/wayland/output.c
@@ -133,24 +133,33 @@ static bool output_attach_render(struct wlr_output *wlr_output,
return true;
}
-static void destroy_wl_buffer(struct wlr_wl_buffer *buffer) {
+void destroy_wl_buffer(struct wlr_wl_buffer *buffer) {
if (buffer == NULL) {
return;
}
+ wl_list_remove(&buffer->buffer_destroy.link);
+ wl_list_remove(&buffer->link);
wl_buffer_destroy(buffer->wl_buffer);
- wlr_buffer_unlock(buffer->buffer);
free(buffer);
}
static void buffer_handle_release(void *data, struct wl_buffer *wl_buffer) {
struct wlr_wl_buffer *buffer = data;
- destroy_wl_buffer(buffer);
+ buffer->released = true;
+ wlr_buffer_unlock(buffer->buffer); // might free buffer
}
static const struct wl_buffer_listener buffer_listener = {
.release = buffer_handle_release,
};
+static void buffer_handle_buffer_destroy(struct wl_listener *listener,
+ void *data) {
+ struct wlr_wl_buffer *buffer =
+ wl_container_of(listener, buffer, buffer_destroy);
+ destroy_wl_buffer(buffer);
+}
+
static bool test_buffer(struct wlr_wl_backend *wl,
struct wlr_buffer *wlr_buffer) {
struct wlr_dmabuf_attributes attribs;
@@ -207,12 +216,33 @@ static struct wlr_wl_buffer *create_wl_buffer(struct wlr_wl_backend *wl,
}
buffer->wl_buffer = wl_buffer;
buffer->buffer = wlr_buffer_lock(wlr_buffer);
+ wl_list_insert(&wl->buffers, &buffer->link);
wl_buffer_add_listener(wl_buffer, &buffer_listener, buffer);
+ buffer->buffer_destroy.notify = buffer_handle_buffer_destroy;
+ wl_signal_add(&wlr_buffer->events.destroy, &buffer->buffer_destroy);
+
return buffer;
}
+static struct wlr_wl_buffer *get_or_create_wl_buffer(struct wlr_wl_backend *wl,
+ struct wlr_buffer *wlr_buffer) {
+ struct wlr_wl_buffer *buffer;
+ wl_list_for_each(buffer, &wl->buffers, link) {
+ // We can only re-use a wlr_wl_buffer if the parent compositor has
+ // released it, because wl_buffer.release is per-wl_buffer, not per
+ // wl_surface.commit.
+ if (buffer->buffer == wlr_buffer && buffer->released) {
+ buffer->released = false;
+ wlr_buffer_lock(buffer->buffer);
+ return buffer;
+ }
+ }
+
+ return create_wl_buffer(wl, wlr_buffer);
+}
+
static bool output_test(struct wlr_output *wlr_output) {
struct wlr_wl_output *output =
get_wl_output_from_output(wlr_output);
@@ -287,7 +317,7 @@ static bool output_commit(struct wlr_output *wlr_output) {
}
struct wlr_wl_buffer *buffer =
- create_wl_buffer(output->backend, wlr_buffer);
+ get_or_create_wl_buffer(output->backend, wlr_buffer);
if (buffer == NULL) {
return false;
}