From bcabe34a2edc71fef338cd770e851a43b68484cf Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 28 Apr 2021 23:22:46 +0200 Subject: [PATCH] backend: automatically create renderer If a backend accepts buffers (as indicated by get_buffer_caps) but doesn't implement get_renderer, automatically create a renderer. --- backend/backend.c | 24 ++++++++++++++++++++++++ include/wlr/backend.h | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/backend/backend.c b/backend/backend.c index 503b6b7d..f7c618f3 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include "backend/backend.h" #include "backend/multi.h" @@ -35,6 +36,7 @@ void wlr_backend_init(struct wlr_backend *backend, void wlr_backend_finish(struct wlr_backend *backend) { wlr_signal_emit_safe(&backend->events.destroy, backend); + wlr_renderer_destroy(backend->renderer); } bool wlr_backend_start(struct wlr_backend *backend) { @@ -56,10 +58,32 @@ void wlr_backend_destroy(struct wlr_backend *backend) { } } +static bool backend_create_renderer(struct wlr_backend *backend) { + if (backend->renderer != NULL) { + return true; + } + + backend->renderer = wlr_renderer_autocreate(backend); + if (backend->renderer == NULL) { + return false; + } + + return true; +} + struct wlr_renderer *wlr_backend_get_renderer(struct wlr_backend *backend) { if (backend->impl->get_renderer) { return backend->impl->get_renderer(backend); } + if (backend_get_buffer_caps(backend) != 0) { + // If the backend is capable of presenting buffers, automatically create + // the renderer if necessary. + if (!backend_create_renderer(backend)) { + wlr_log(WLR_ERROR, "Failed to create backend renderer"); + return NULL; + } + return backend->renderer; + } return NULL; } diff --git a/include/wlr/backend.h b/include/wlr/backend.h index b56e789c..020be18d 100644 --- a/include/wlr/backend.h +++ b/include/wlr/backend.h @@ -25,6 +25,10 @@ struct wlr_backend { /** Raised when new outputs are added, passed the wlr_output */ struct wl_signal new_output; } events; + + // Private state + + struct wlr_renderer *renderer; }; /**