From cd9f25711cd2ce46a9af577b6426bab73c270fda Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 24 Apr 2018 22:45:44 +0100 Subject: [PATCH 1/4] backend: destroy renderer when destroying backend --- backend/headless/backend.c | 1 + backend/wayland/backend.c | 1 + backend/x11/backend.c | 1 + 3 files changed, 3 insertions(+) diff --git a/backend/headless/backend.c b/backend/headless/backend.c index 81b01376..1d29e12a 100644 --- a/backend/headless/backend.c +++ b/backend/headless/backend.c @@ -54,6 +54,7 @@ static void backend_destroy(struct wlr_backend *wlr_backend) { wlr_signal_emit_safe(&wlr_backend->events.destroy, backend); + wlr_renderer_destroy(backend->renderer); wlr_egl_finish(&backend->egl); free(backend); } diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index 4bcb9360..85a98b72 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -88,6 +88,7 @@ static void wlr_wl_backend_destroy(struct wlr_backend *wlr_backend) { free(backend->seat_name); wl_event_source_remove(backend->remote_display_src); + wlr_renderer_destroy(backend->renderer); wlr_egl_finish(&backend->egl); if (backend->seat) { wl_seat_destroy(backend->seat); diff --git a/backend/x11/backend.c b/backend/x11/backend.c index b4b0f154..3a409155 100644 --- a/backend/x11/backend.c +++ b/backend/x11/backend.c @@ -239,6 +239,7 @@ static void wlr_x11_backend_destroy(struct wlr_backend *backend) { } wl_list_remove(&x11->display_destroy.link); + wlr_renderer_destroy(x11->renderer); wlr_egl_finish(&x11->egl); if (x11->cursor) { From 89a9c96fab054f5535038811b598d34430b24cf4 Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 24 Apr 2018 23:05:15 +0100 Subject: [PATCH 2/4] compositor: make wlr_compositor_destroy destroy resources --- include/wlr/types/wlr_compositor.h | 3 +- include/wlr/types/wlr_region.h | 3 +- types/wlr_compositor.c | 47 ++++++++++++++++++++---------- types/wlr_region.c | 10 +++---- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/include/wlr/types/wlr_compositor.h b/include/wlr/types/wlr_compositor.h index 816ff713..5dd2ce98 100644 --- a/include/wlr/types/wlr_compositor.h +++ b/include/wlr/types/wlr_compositor.h @@ -15,7 +15,8 @@ struct wlr_compositor { struct wl_global *wl_global; struct wl_list wl_resources; struct wlr_renderer *renderer; - struct wl_list surfaces; + struct wl_list surface_resources; + struct wl_list region_resources; struct wlr_subcompositor subcompositor; diff --git a/include/wlr/types/wlr_region.h b/include/wlr/types/wlr_region.h index ffacea72..a79ab61d 100644 --- a/include/wlr/types/wlr_region.h +++ b/include/wlr/types/wlr_region.h @@ -8,8 +8,7 @@ struct wl_resource; /* * Implements the given resource as region. */ -void wlr_region_create(struct wl_client *client, struct wl_resource *res, - uint32_t id); +struct wl_resource *wlr_region_create(struct wl_client *client, uint32_t id); pixman_region32_t *wlr_region_from_resource(struct wl_resource *resource); diff --git a/types/wlr_compositor.c b/types/wlr_compositor.c index 2d9eec0f..f9bd43fd 100644 --- a/types/wlr_compositor.c +++ b/types/wlr_compositor.c @@ -99,6 +99,10 @@ static void subcompositor_init(struct wlr_subcompositor *subcompositor, static void subcompositor_finish(struct wlr_subcompositor *subcompositor) { wl_global_destroy(subcompositor->wl_global); + struct wl_resource *resource, *tmp; + wl_resource_for_each_safe(resource, tmp, &subcompositor->wl_resources) { + wl_resource_destroy(resource); + } } @@ -111,7 +115,8 @@ static struct wlr_compositor *compositor_from_resource( return wl_resource_get_user_data(resource); } -static void destroy_surface_listener(struct wl_listener *listener, void *data) { +static void compositor_handle_surface_destroy(struct wl_listener *listener, + void *data) { wl_list_remove(wl_resource_get_link(data)); } @@ -134,35 +139,36 @@ static void wl_compositor_create_surface(struct wl_client *client, return; } surface->compositor_data = compositor; - surface->compositor_listener.notify = &destroy_surface_listener; + surface->compositor_listener.notify = compositor_handle_surface_destroy; wl_resource_add_destroy_listener(surface_resource, &surface->compositor_listener); - wl_list_insert(&compositor->surfaces, + wl_list_insert(&compositor->surface_resources, wl_resource_get_link(surface_resource)); wlr_signal_emit_safe(&compositor->events.new_surface, surface); } static void wl_compositor_create_region(struct wl_client *client, struct wl_resource *resource, uint32_t id) { - wlr_region_create(client, resource, id); + struct wlr_compositor *compositor = compositor_from_resource(resource); + + struct wl_resource *region_resource = wlr_region_create(client, id); + if (region_resource == NULL) { + wl_resource_post_no_memory(resource); + return; + } + + wl_list_insert(&compositor->region_resources, + wl_resource_get_link(region_resource)); } static const struct wl_compositor_interface wl_compositor_impl = { .create_surface = wl_compositor_create_surface, - .create_region = wl_compositor_create_region + .create_region = wl_compositor_create_region, }; static void wl_compositor_destroy(struct wl_resource *resource) { - struct wlr_compositor *compositor = compositor_from_resource(resource); - struct wl_resource *_resource = NULL; - wl_resource_for_each(_resource, &compositor->wl_resources) { - if (_resource == resource) { - struct wl_list *link = wl_resource_get_link(_resource); - wl_list_remove(link); - break; - } - } + wl_list_remove(wl_resource_get_link(resource)); } static void wl_compositor_bind(struct wl_client *wl_client, void *data, @@ -189,6 +195,16 @@ void wlr_compositor_destroy(struct wlr_compositor *compositor) { subcompositor_finish(&compositor->subcompositor); wl_list_remove(&compositor->display_destroy.link); wl_global_destroy(compositor->wl_global); + struct wl_resource *resource, *tmp; + wl_resource_for_each_safe(resource, tmp, &compositor->surface_resources) { + wl_resource_destroy(resource); + } + wl_resource_for_each_safe(resource, tmp, &compositor->region_resources) { + wl_resource_destroy(resource); + } + wl_resource_for_each_safe(resource, tmp, &compositor->wl_resources) { + wl_resource_destroy(resource); + } free(compositor); } @@ -218,7 +234,8 @@ struct wlr_compositor *wlr_compositor_create(struct wl_display *display, compositor->renderer = renderer; wl_list_init(&compositor->wl_resources); - wl_list_init(&compositor->surfaces); + wl_list_init(&compositor->surface_resources); + wl_list_init(&compositor->region_resources); wl_signal_init(&compositor->events.new_surface); wl_signal_init(&compositor->events.destroy); diff --git a/types/wlr_region.c b/types/wlr_region.c index 6309c1a9..f452bbdf 100644 --- a/types/wlr_region.c +++ b/types/wlr_region.c @@ -38,12 +38,10 @@ static void destroy_region(struct wl_resource *resource) { free(reg); } -void wlr_region_create(struct wl_client *client, struct wl_resource *res, - uint32_t id) { +struct wl_resource *wlr_region_create(struct wl_client *client, uint32_t id) { pixman_region32_t *region = calloc(1, sizeof(pixman_region32_t)); if (region == NULL) { - wl_resource_post_no_memory(res); - return; + return NULL; } pixman_region32_init(region); @@ -52,11 +50,11 @@ void wlr_region_create(struct wl_client *client, struct wl_resource *res, &wl_region_interface, 1, id); if (region_resource == NULL) { free(region); - wl_resource_post_no_memory(res); - return; + return NULL; } wl_resource_set_implementation(region_resource, ®ion_impl, region, destroy_region); + return region_resource; } pixman_region32_t *wlr_region_from_resource(struct wl_resource *resource) { From d47713ac0f28e9dcc85f8aa40a0372381730d43b Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 24 Apr 2018 23:31:58 +0100 Subject: [PATCH 3/4] compositor: destroy subsurface resources with wlr_subcompositor --- include/wlr/types/wlr_compositor.h | 1 + include/wlr/types/wlr_surface.h | 2 +- types/wlr_compositor.c | 47 +++++++++++++++++++++++------- types/wlr_surface.c | 10 ++++--- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/include/wlr/types/wlr_compositor.h b/include/wlr/types/wlr_compositor.h index 5dd2ce98..f6be0a74 100644 --- a/include/wlr/types/wlr_compositor.h +++ b/include/wlr/types/wlr_compositor.h @@ -9,6 +9,7 @@ struct wlr_surface; struct wlr_subcompositor { struct wl_global *wl_global; struct wl_list wl_resources; + struct wl_list subsurface_resources; }; struct wlr_compositor { diff --git a/include/wlr/types/wlr_surface.h b/include/wlr/types/wlr_surface.h index 35d47926..f799b28a 100644 --- a/include/wlr/types/wlr_surface.h +++ b/include/wlr/types/wlr_surface.h @@ -121,7 +121,7 @@ bool wlr_surface_has_buffer(struct wlr_surface *surface); /** * Create the subsurface implementation for this surface. */ -void wlr_surface_make_subsurface(struct wlr_surface *surface, +struct wlr_subsurface *wlr_surface_make_subsurface(struct wlr_surface *surface, struct wlr_surface *parent, uint32_t id); /** diff --git a/types/wlr_compositor.c b/types/wlr_compositor.c index f9bd43fd..103b1b61 100644 --- a/types/wlr_compositor.c +++ b/types/wlr_compositor.c @@ -20,6 +20,15 @@ struct wlr_subsurface *wlr_subsurface_from_surface( return (struct wlr_subsurface *)surface->role_data; } +static const struct wl_subcompositor_interface subcompositor_impl; + +static struct wlr_subcompositor *subcompositor_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, &wl_subcompositor_interface, + &subcompositor_impl)); + return wl_resource_get_user_data(resource); +} + static void subcompositor_handle_destroy(struct wl_client *client, struct wl_resource *resource) { wl_resource_destroy(resource); @@ -29,6 +38,8 @@ static void subcompositor_handle_get_subsurface(struct wl_client *client, struct wl_resource *resource, uint32_t id, struct wl_resource *surface_resource, struct wl_resource *parent_resource) { + struct wlr_subcompositor *subcompositor = + subcompositor_from_resource(resource); struct wlr_surface *surface = wlr_surface_from_resource(surface_resource); struct wlr_surface *parent = wlr_surface_from_resource(parent_resource); @@ -64,14 +75,25 @@ static void subcompositor_handle_get_subsurface(struct wl_client *client, return; } - wlr_surface_make_subsurface(surface, parent, id); + struct wlr_subsurface *subsurface = + wlr_surface_make_subsurface(surface, parent, id); + if (subsurface == NULL) { + return; + } + + wl_list_insert(&subcompositor->subsurface_resources, + wl_resource_get_link(subsurface->resource)); } -static const struct wl_subcompositor_interface subcompositor_interface = { +static const struct wl_subcompositor_interface subcompositor_impl = { .destroy = subcompositor_handle_destroy, .get_subsurface = subcompositor_handle_get_subsurface, }; +static void subcompositor_resource_destroy(struct wl_resource *resource) { + wl_list_remove(wl_resource_get_link(resource)); +} + static void subcompositor_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id) { struct wlr_subcompositor *subcompositor = data; @@ -81,8 +103,8 @@ static void subcompositor_bind(struct wl_client *client, void *data, wl_client_post_no_memory(client); return; } - wl_resource_set_implementation(resource, &subcompositor_interface, - subcompositor, NULL); + wl_resource_set_implementation(resource, &subcompositor_impl, + subcompositor, subcompositor_resource_destroy); wl_list_insert(&subcompositor->wl_resources, wl_resource_get_link(resource)); } @@ -95,23 +117,28 @@ static void subcompositor_init(struct wlr_subcompositor *subcompositor, return; } wl_list_init(&subcompositor->wl_resources); + wl_list_init(&subcompositor->subsurface_resources); } static void subcompositor_finish(struct wlr_subcompositor *subcompositor) { wl_global_destroy(subcompositor->wl_global); struct wl_resource *resource, *tmp; + wl_resource_for_each_safe(resource, tmp, + &subcompositor->subsurface_resources) { + wl_resource_destroy(resource); + } wl_resource_for_each_safe(resource, tmp, &subcompositor->wl_resources) { wl_resource_destroy(resource); } } -static const struct wl_compositor_interface wl_compositor_impl; +static const struct wl_compositor_interface compositor_impl; static struct wlr_compositor *compositor_from_resource( struct wl_resource *resource) { assert(wl_resource_instance_of(resource, &wl_compositor_interface, - &wl_compositor_impl)); + &compositor_impl)); return wl_resource_get_user_data(resource); } @@ -162,12 +189,12 @@ static void wl_compositor_create_region(struct wl_client *client, wl_resource_get_link(region_resource)); } -static const struct wl_compositor_interface wl_compositor_impl = { +static const struct wl_compositor_interface compositor_impl = { .create_surface = wl_compositor_create_surface, .create_region = wl_compositor_create_region, }; -static void wl_compositor_destroy(struct wl_resource *resource) { +static void compositor_resource_destroy(struct wl_resource *resource) { wl_list_remove(wl_resource_get_link(resource)); } @@ -182,8 +209,8 @@ static void wl_compositor_bind(struct wl_client *wl_client, void *data, wl_client_post_no_memory(wl_client); return; } - wl_resource_set_implementation(resource, &wl_compositor_impl, - compositor, wl_compositor_destroy); + wl_resource_set_implementation(resource, &compositor_impl, + compositor, compositor_resource_destroy); wl_list_insert(&compositor->wl_resources, wl_resource_get_link(resource)); } diff --git a/types/wlr_surface.c b/types/wlr_surface.c index 9c0324a0..578c43d3 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -827,7 +827,7 @@ static void subsurface_handle_surface_destroy(struct wl_listener *listener, wlr_subsurface_destroy(subsurface); } -void wlr_surface_make_subsurface(struct wlr_surface *surface, +struct wlr_subsurface *wlr_surface_make_subsurface(struct wlr_surface *surface, struct wlr_surface *parent, uint32_t id) { struct wl_client *client = wl_resource_get_client(surface->resource); @@ -835,13 +835,13 @@ void wlr_surface_make_subsurface(struct wlr_surface *surface, calloc(1, sizeof(struct wlr_subsurface)); if (!subsurface) { wl_client_post_no_memory(client); - return; + return NULL; } subsurface->cached = wlr_surface_state_create(); if (subsurface->cached == NULL) { free(subsurface); wl_client_post_no_memory(client); - return; + return NULL; } subsurface->synchronized = true; subsurface->surface = surface; @@ -863,7 +863,7 @@ void wlr_surface_make_subsurface(struct wlr_surface *surface, wlr_surface_state_destroy(subsurface->cached); free(subsurface); wl_client_post_no_memory(client); - return; + return NULL; } wl_resource_set_implementation(subsurface->resource, @@ -873,6 +873,8 @@ void wlr_surface_make_subsurface(struct wlr_surface *surface, surface->role_data = subsurface; wlr_signal_emit_safe(&parent->events.new_subsurface, subsurface); + + return subsurface; } From 449f06556aa9550540c37af0cae0d6902b29f204 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 26 Apr 2018 00:11:36 +0100 Subject: [PATCH 4/4] Destroy wlr_surface with wlr_renderer --- include/wlr/render/interface.h | 6 ------ include/wlr/render/wlr_renderer.h | 10 ++++++++-- include/wlr/types/wlr_surface.h | 2 ++ render/wlr_renderer.c | 5 +++++ types/wlr_region.c | 7 +++++-- types/wlr_surface.c | 15 +++++++++++++-- 6 files changed, 33 insertions(+), 12 deletions(-) diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index 9dbe7247..e7cdce0a 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -11,12 +11,6 @@ #include #include -struct wlr_renderer_impl; - -struct wlr_renderer { - const struct wlr_renderer_impl *impl; -}; - struct wlr_renderer_impl { void (*begin)(struct wlr_renderer *renderer, uint32_t width, uint32_t height); diff --git a/include/wlr/render/wlr_renderer.h b/include/wlr/render/wlr_renderer.h index eb365762..21f9c16c 100644 --- a/include/wlr/render/wlr_renderer.h +++ b/include/wlr/render/wlr_renderer.h @@ -6,9 +6,15 @@ #include #include -struct wlr_output; +struct wlr_renderer_impl; -struct wlr_renderer; +struct wlr_renderer { + const struct wlr_renderer_impl *impl; + + struct { + struct wl_signal destroy; + } events; +}; void wlr_renderer_begin(struct wlr_renderer *r, int width, int height); void wlr_renderer_end(struct wlr_renderer *r); diff --git a/include/wlr/types/wlr_surface.h b/include/wlr/types/wlr_surface.h index f799b28a..5ff9996d 100644 --- a/include/wlr/types/wlr_surface.h +++ b/include/wlr/types/wlr_surface.h @@ -93,6 +93,8 @@ struct wlr_surface { // wlr_subsurface::parent_pending_link struct wl_list subsurface_pending_list; + struct wl_listener renderer_destroy; + void *data; }; diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c index 700da05b..8d1bd9ce 100644 --- a/render/wlr_renderer.c +++ b/render/wlr_renderer.c @@ -5,6 +5,7 @@ #include #include #include +#include "util/signal.h" void wlr_renderer_init(struct wlr_renderer *renderer, const struct wlr_renderer_impl *impl) { @@ -18,9 +19,13 @@ void wlr_renderer_init(struct wlr_renderer *renderer, assert(impl->format_supported); assert(impl->texture_from_pixels); renderer->impl = impl; + + wl_signal_init(&renderer->events.destroy); } void wlr_renderer_destroy(struct wlr_renderer *r) { + wlr_signal_emit_safe(&r->events.destroy, r); + if (r && r->impl && r->impl->destroy) { r->impl->destroy(r); } else { diff --git a/types/wlr_region.c b/types/wlr_region.c index f452bbdf..860864c1 100644 --- a/types/wlr_region.c +++ b/types/wlr_region.c @@ -32,10 +32,13 @@ static const struct wl_region_interface region_impl = { .subtract = region_subtract, }; -static void destroy_region(struct wl_resource *resource) { +static void region_handle_resource_destroy(struct wl_resource *resource) { pixman_region32_t *reg = wlr_region_from_resource(resource); pixman_region32_fini(reg); free(reg); + + // Set by wlr_compositor + wl_list_remove(wl_resource_get_link(resource)); } struct wl_resource *wlr_region_create(struct wl_client *client, uint32_t id) { @@ -53,7 +56,7 @@ struct wl_resource *wlr_region_create(struct wl_client *client, uint32_t id) { return NULL; } wl_resource_set_implementation(region_resource, ®ion_impl, region, - destroy_region); + region_handle_resource_destroy); return region_resource; } diff --git a/types/wlr_surface.c b/types/wlr_surface.c index 578c43d3..9a2421db 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -622,7 +622,7 @@ void wlr_subsurface_destroy(struct wlr_subsurface *subsurface) { free(subsurface); } -static void destroy_surface(struct wl_resource *resource) { +static void surface_handle_resource_destroy(struct wl_resource *resource) { struct wlr_surface *surface = wlr_surface_from_resource(resource); wlr_signal_emit_safe(&surface->events.destroy, surface); @@ -634,6 +634,13 @@ static void destroy_surface(struct wl_resource *resource) { free(surface); } +static void surface_handle_renderer_destroy(struct wl_listener *listener, + void *data) { + struct wlr_surface *surface = + wl_container_of(listener, surface, renderer_destroy); + wl_resource_destroy(surface->resource); +} + struct wlr_surface *wlr_surface_create(struct wl_resource *res, struct wlr_renderer *renderer) { struct wlr_surface *surface = calloc(1, sizeof(struct wlr_surface)); @@ -654,7 +661,11 @@ struct wlr_surface *wlr_surface_create(struct wl_resource *res, wl_list_init(&surface->subsurfaces); wl_list_init(&surface->subsurface_pending_list); wl_resource_set_implementation(res, &surface_interface, - surface, destroy_surface); + surface, surface_handle_resource_destroy); + + wl_signal_add(&renderer->events.destroy, &surface->renderer_destroy); + surface->renderer_destroy.notify = surface_handle_renderer_destroy; + return surface; }