From bfc42e0f622f242ad057bd36e1946418ed3d842c Mon Sep 17 00:00:00 2001 From: eri Date: Thu, 19 Oct 2023 18:17:33 +0200 Subject: [PATCH] linux_dmabuf_v1: convert to try_from References: wlroots/wlroots#884 --- include/wlr/types/wlr_linux_dmabuf_v1.h | 10 ++-------- types/wlr_linux_dmabuf_v1.c | 25 +++++++++++++------------ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/include/wlr/types/wlr_linux_dmabuf_v1.h b/include/wlr/types/wlr_linux_dmabuf_v1.h index 3f703705..bfc39a85 100644 --- a/include/wlr/types/wlr_linux_dmabuf_v1.h +++ b/include/wlr/types/wlr_linux_dmabuf_v1.h @@ -29,17 +29,11 @@ struct wlr_dmabuf_v1_buffer { struct wl_listener release; }; -/** - * Returns true if the given resource was created via the linux-dmabuf - * buffer protocol, false otherwise - */ -bool wlr_dmabuf_v1_resource_is_buffer(struct wl_resource *buffer_resource); - /** * Returns the struct wlr_dmabuf_buffer if the given resource was created - * via the linux-dmabuf buffer protocol. + * via the linux-dmabuf buffer protocol or NULL otherwise. */ -struct wlr_dmabuf_v1_buffer *wlr_dmabuf_v1_buffer_from_buffer_resource( +struct wlr_dmabuf_v1_buffer *wlr_dmabuf_v1_buffer_try_from_buffer_resource( struct wl_resource *buffer_resource); struct wlr_linux_dmabuf_feedback_v1 { diff --git a/types/wlr_linux_dmabuf_v1.c b/types/wlr_linux_dmabuf_v1.c index 98409e00..f598af18 100644 --- a/types/wlr_linux_dmabuf_v1.c +++ b/types/wlr_linux_dmabuf_v1.c @@ -72,18 +72,17 @@ static const struct wl_buffer_interface wl_buffer_impl = { .destroy = buffer_handle_destroy, }; -bool wlr_dmabuf_v1_resource_is_buffer(struct wl_resource *resource) { - if (!wl_resource_instance_of(resource, &wl_buffer_interface, - &wl_buffer_impl)) { - return false; - } - return wl_resource_get_user_data(resource) != NULL; +static bool buffer_resource_is_instance(struct wl_resource *resource) { + return wl_resource_instance_of(resource, &wl_buffer_interface, + &wl_buffer_impl); } -struct wlr_dmabuf_v1_buffer *wlr_dmabuf_v1_buffer_from_buffer_resource( +struct wlr_dmabuf_v1_buffer *wlr_dmabuf_v1_buffer_try_from_buffer_resource( struct wl_resource *resource) { - assert(wl_resource_instance_of(resource, &wl_buffer_interface, - &wl_buffer_impl)); + if (!buffer_resource_is_instance(resource) || + wl_resource_get_user_data(resource) == NULL) { + return NULL; + } return wl_resource_get_user_data(resource); } @@ -195,7 +194,8 @@ static void params_add(struct wl_client *client, static void buffer_handle_resource_destroy(struct wl_resource *buffer_resource) { struct wlr_dmabuf_v1_buffer *buffer = - wlr_dmabuf_v1_buffer_from_buffer_resource(buffer_resource); + wlr_dmabuf_v1_buffer_try_from_buffer_resource(buffer_resource); + assert(buffer != NULL); buffer->resource = NULL; wlr_buffer_drop(&buffer->base); } @@ -836,13 +836,14 @@ static void linux_dmabuf_bind(struct wl_client *client, void *data, static struct wlr_buffer *buffer_from_resource(struct wl_resource *resource) { struct wlr_dmabuf_v1_buffer *buffer = - wlr_dmabuf_v1_buffer_from_buffer_resource(resource); + wlr_dmabuf_v1_buffer_try_from_buffer_resource(resource); + assert(buffer != NULL); return &buffer->base; } static const struct wlr_buffer_resource_interface buffer_resource_interface = { .name = "wlr_dmabuf_v1_buffer", - .is_instance = wlr_dmabuf_v1_resource_is_buffer, + .is_instance = buffer_resource_is_instance, .from_resource = buffer_from_resource, };