From c82f37542d3a63c3b5dd573dcdb8559ea1f888f7 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Thu, 29 Apr 2021 09:18:19 +0200 Subject: [PATCH] backend/headless: use common renderer and allocator Instead of managing our own renderer and allocator, let the common code do it. Because wlr_headless_backend_create_with_renderer needs to re-use the parent renderer, we have to hand-roll some of the renderer initialization. --- backend/headless/backend.c | 39 +++++++++++++++++++------------------- backend/headless/output.c | 17 +++++++++++------ include/backend/headless.h | 6 ++---- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/backend/headless/backend.c b/backend/headless/backend.c index 875eb768..b4166eef 100644 --- a/backend/headless/backend.c +++ b/backend/headless/backend.c @@ -9,6 +9,7 @@ #include #include #include +#include "backend/backend.h" #include "backend/headless.h" #include "render/drm_format_set.h" #include "render/gbm_allocator.h" @@ -54,7 +55,7 @@ static void backend_destroy(struct wlr_backend *wlr_backend) { } wl_list_remove(&backend->display_destroy.link); - wl_list_remove(&backend->renderer_destroy.link); + wl_list_remove(&backend->parent_renderer_destroy.link); struct wlr_headless_output *output, *output_tmp; wl_list_for_each_safe(output, output_tmp, &backend->outputs, link) { @@ -71,11 +72,6 @@ static void backend_destroy(struct wlr_backend *wlr_backend) { free(backend->format); - if (!backend->has_parent_renderer) { - wlr_renderer_destroy(backend->renderer); - } - - wlr_allocator_destroy(backend->allocator); close(backend->drm_fd); free(backend); } @@ -84,7 +80,11 @@ static struct wlr_renderer *backend_get_renderer( struct wlr_backend *wlr_backend) { struct wlr_headless_backend *backend = headless_backend_from_backend(wlr_backend); - return backend->renderer; + if (backend->parent_renderer != NULL) { + return backend->parent_renderer; + } else { + return wlr_backend->renderer; + } } static int backend_get_drm_fd(struct wlr_backend *wlr_backend) { @@ -93,7 +93,7 @@ static int backend_get_drm_fd(struct wlr_backend *wlr_backend) { return backend->drm_fd; } -static uint32_t backend_get_buffer_caps(struct wlr_backend *wlr_backend) { +static uint32_t get_buffer_caps(struct wlr_backend *wlr_backend) { return WLR_BUFFER_CAP_DATA_PTR | WLR_BUFFER_CAP_DMABUF | WLR_BUFFER_CAP_SHM; @@ -104,7 +104,7 @@ static const struct wlr_backend_impl backend_impl = { .destroy = backend_destroy, .get_renderer = backend_get_renderer, .get_drm_fd = backend_get_drm_fd, - .get_buffer_caps = backend_get_buffer_caps, + .get_buffer_caps = get_buffer_caps, }; static void handle_display_destroy(struct wl_listener *listener, void *data) { @@ -115,16 +115,18 @@ static void handle_display_destroy(struct wl_listener *listener, void *data) { static void handle_renderer_destroy(struct wl_listener *listener, void *data) { struct wlr_headless_backend *backend = - wl_container_of(listener, backend, renderer_destroy); + wl_container_of(listener, backend, parent_renderer_destroy); backend_destroy(&backend->backend); } static bool backend_init(struct wlr_headless_backend *backend, struct wl_display *display, struct wlr_renderer *renderer) { wlr_backend_init(&backend->backend, &backend_impl); + backend->display = display; wl_list_init(&backend->outputs); wl_list_init(&backend->input_devices); + wl_list_init(&backend->parent_renderer_destroy.link); if (renderer == NULL) { renderer = wlr_renderer_autocreate(&backend->backend); @@ -132,17 +134,20 @@ static bool backend_init(struct wlr_headless_backend *backend, wlr_log(WLR_ERROR, "Failed to create renderer"); return false; } + backend->backend.renderer = renderer; + } else { + backend->parent_renderer = renderer; + backend->parent_renderer_destroy.notify = handle_renderer_destroy; + wl_signal_add(&renderer->events.destroy, &backend->parent_renderer_destroy); } - backend->renderer = renderer; - backend->allocator = wlr_allocator_autocreate(&backend->backend, renderer); - if (!backend->allocator) { + if (backend_get_allocator(&backend->backend) == NULL) { wlr_log(WLR_ERROR, "Failed to create allocator"); return false; } const struct wlr_drm_format_set *formats = - wlr_renderer_get_render_formats(backend->renderer); + wlr_renderer_get_render_formats(renderer); if (formats == NULL) { wlr_log(WLR_ERROR, "Failed to get available DMA-BUF formats from renderer"); return false; @@ -158,8 +163,6 @@ static bool backend_init(struct wlr_headless_backend *backend, backend->display_destroy.notify = handle_display_destroy; wl_display_add_destroy_listener(display, &backend->display_destroy); - wl_list_init(&backend->renderer_destroy.link); - return true; } @@ -246,7 +249,6 @@ struct wlr_backend *wlr_headless_backend_create_with_renderer( wlr_log(WLR_ERROR, "Failed to allocate wlr_headless_backend"); return NULL; } - backend->has_parent_renderer = true; int drm_fd = wlr_renderer_get_drm_fd(renderer); if (drm_fd < 0) { @@ -263,9 +265,6 @@ struct wlr_backend *wlr_headless_backend_create_with_renderer( goto error_init; } - backend->renderer_destroy.notify = handle_renderer_destroy; - wl_signal_add(&renderer->events.destroy, &backend->renderer_destroy); - return &backend->backend; error_init: diff --git a/backend/headless/output.c b/backend/headless/output.c index 578876ea..7f3b4445 100644 --- a/backend/headless/output.c +++ b/backend/headless/output.c @@ -4,6 +4,7 @@ #include #include #include +#include "backend/backend.h" #include "backend/headless.h" #include "render/swapchain.h" #include "render/wlr_renderer.h" @@ -19,13 +20,14 @@ static bool output_set_custom_mode(struct wlr_output *wlr_output, int32_t width, int32_t height, int32_t refresh) { struct wlr_headless_output *output = headless_output_from_output(wlr_output); + struct wlr_allocator *allocator = backend_get_allocator(wlr_output->backend); if (refresh <= 0) { refresh = HEADLESS_DEFAULT_REFRESH; } wlr_swapchain_destroy(output->swapchain); - output->swapchain = wlr_swapchain_create(output->backend->allocator, + output->swapchain = wlr_swapchain_create(allocator, width, height, output->backend->format); if (!output->swapchain) { wlr_output_destroy(wlr_output); @@ -42,6 +44,7 @@ static bool output_attach_render(struct wlr_output *wlr_output, int *buffer_age) { struct wlr_headless_output *output = headless_output_from_output(wlr_output); + struct wlr_renderer *renderer = wlr_backend_get_renderer(wlr_output->backend); wlr_buffer_unlock(output->back_buffer); output->back_buffer = wlr_swapchain_acquire(output->swapchain, buffer_age); @@ -50,8 +53,7 @@ static bool output_attach_render(struct wlr_output *wlr_output, return false; } - if (!wlr_renderer_bind_buffer(output->backend->renderer, - output->back_buffer)) { + if (!wlr_renderer_bind_buffer(renderer, output->back_buffer)) { wlr_log(WLR_ERROR, "Failed to bind buffer to renderer"); return false; } @@ -75,6 +77,7 @@ static bool output_test(struct wlr_output *wlr_output) { static bool output_commit(struct wlr_output *wlr_output) { struct wlr_headless_output *output = headless_output_from_output(wlr_output); + struct wlr_renderer *renderer = wlr_backend_get_renderer(wlr_output->backend); if (!output_test(wlr_output)) { return false; @@ -95,7 +98,7 @@ static bool output_commit(struct wlr_output *wlr_output) { case WLR_OUTPUT_STATE_BUFFER_RENDER: assert(output->back_buffer != NULL); - wlr_renderer_bind_buffer(output->backend->renderer, NULL); + wlr_renderer_bind_buffer(renderer, NULL); buffer = output->back_buffer; output->back_buffer = NULL; @@ -120,8 +123,9 @@ static bool output_commit(struct wlr_output *wlr_output) { static void output_rollback_render(struct wlr_output *wlr_output) { struct wlr_headless_output *output = headless_output_from_output(wlr_output); + struct wlr_renderer *renderer = wlr_backend_get_renderer(wlr_output->backend); - wlr_renderer_bind_buffer(output->backend->renderer, NULL); + wlr_renderer_bind_buffer(renderer, NULL); wlr_buffer_unlock(output->back_buffer); output->back_buffer = NULL; @@ -178,6 +182,7 @@ struct wlr_output *wlr_headless_add_output(struct wlr_backend *wlr_backend, unsigned int width, unsigned int height) { struct wlr_headless_backend *backend = headless_backend_from_backend(wlr_backend); + struct wlr_allocator *allocator = backend_get_allocator(wlr_backend); struct wlr_headless_output *output = calloc(1, sizeof(struct wlr_headless_output)); @@ -190,7 +195,7 @@ struct wlr_output *wlr_headless_add_output(struct wlr_backend *wlr_backend, backend->display); struct wlr_output *wlr_output = &output->wlr_output; - output->swapchain = wlr_swapchain_create(backend->allocator, + output->swapchain = wlr_swapchain_create(allocator, width, height, backend->format); if (!output->swapchain) { goto error; diff --git a/include/backend/headless.h b/include/backend/headless.h index a6720eb4..1ad82fc2 100644 --- a/include/backend/headless.h +++ b/include/backend/headless.h @@ -9,16 +9,14 @@ struct wlr_headless_backend { struct wlr_backend backend; int drm_fd; - struct wlr_renderer *renderer; - struct wlr_allocator *allocator; struct wlr_drm_format *format; struct wl_display *display; struct wl_list outputs; size_t last_output_num; struct wl_list input_devices; struct wl_listener display_destroy; - struct wl_listener renderer_destroy; - bool has_parent_renderer; + struct wlr_renderer *parent_renderer; + struct wl_listener parent_renderer_destroy; bool started; };