Merge pull request #506 from acrisci/multi-backend-fixes

[wip] multibackend fixes
This commit is contained in:
Tony Crisci 2017-12-21 18:40:54 -05:00 committed by GitHub
commit 0a370c5298
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 140 additions and 39 deletions

View file

@ -18,6 +18,7 @@ void wlr_backend_init(struct wlr_backend *backend,
const struct wlr_backend_impl *impl) {
assert(backend);
backend->impl = impl;
wl_signal_init(&backend->events.destroy);
wl_signal_init(&backend->events.input_add);
wl_signal_init(&backend->events.input_remove);
wl_signal_init(&backend->events.output_add);
@ -32,6 +33,11 @@ bool wlr_backend_start(struct wlr_backend *backend) {
}
void wlr_backend_destroy(struct wlr_backend *backend) {
if (!backend) {
return;
}
wl_signal_emit(&backend->events.destroy, backend);
if (backend->impl && backend->impl->destroy) {
backend->impl->destroy(backend);
} else {
@ -70,38 +76,45 @@ static struct wlr_backend *attempt_wl_backend(struct wl_display *display) {
}
struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) {
struct wlr_backend *backend;
struct wlr_backend *backend = wlr_multi_backend_create(display);
if (!backend) {
wlr_log(L_ERROR, "could not allocate multibackend");
return NULL;
}
if (getenv("WAYLAND_DISPLAY") || getenv("_WAYLAND_DISPLAY")) {
backend = attempt_wl_backend(display);
if (backend) {
struct wlr_backend *wl_backend = attempt_wl_backend(display);
if (wl_backend) {
wlr_multi_backend_add(backend, wl_backend);
return backend;
}
}
const char *x11_display = getenv("DISPLAY");
if (x11_display) {
return wlr_x11_backend_create(display, x11_display);
struct wlr_backend *x11_backend =
wlr_x11_backend_create(display, x11_display);
wlr_multi_backend_add(backend, x11_backend);
return backend;
}
// Attempt DRM+libinput
struct wlr_session *session = wlr_session_create(display);
if (!session) {
wlr_log(L_ERROR, "Failed to start a DRM session");
wlr_backend_destroy(backend);
return NULL;
}
backend = wlr_multi_backend_create(display, session);
if (!backend) {
goto error_session;
}
struct wlr_backend *libinput = wlr_libinput_backend_create(display, session);
if (!libinput) {
goto error_multi;
}
if (libinput) {
wlr_multi_backend_add(backend, libinput);
} else {
wlr_log(L_ERROR, "Failed to start libinput backend");
wlr_backend_destroy(backend);
wlr_session_destroy(session);
return NULL;
}
int gpus[8];
size_t num_gpus = wlr_session_find_gpus(session, 8, gpus);
@ -125,16 +138,13 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) {
if (!primary_drm) {
wlr_log(L_ERROR, "Failed to open any DRM device");
goto error_multi;
wlr_backend_destroy(libinput);
wlr_session_destroy(session);
wlr_backend_destroy(backend);
return NULL;
}
return backend;
error_multi:
wlr_backend_destroy(backend);
error_session:
wlr_session_destroy(session);
return NULL;
}
uint32_t usec_to_msec(uint64_t usec) {

View file

@ -174,3 +174,8 @@ error_fd:
free(drm);
return NULL;
}
struct wlr_session *wlr_drm_backend_get_session(struct wlr_backend *backend) {
struct wlr_drm_backend *drm = (struct wlr_drm_backend *)backend;
return drm->session;
}

View file

@ -5,6 +5,7 @@
#include <wlr/backend/session.h>
#include <wlr/util/log.h>
#include "backend/multi.h"
#include "backend/drm/drm.h"
struct subbackend_state {
struct wlr_backend *backend;
@ -13,6 +14,7 @@ struct subbackend_state {
struct wl_listener input_remove;
struct wl_listener output_add;
struct wl_listener output_remove;
struct wl_listener backend_destroy;
struct wl_list link;
};
@ -28,13 +30,21 @@ static bool multi_backend_start(struct wlr_backend *wlr_backend) {
return true;
}
static void subbackend_state_destroy(struct subbackend_state *sub) {
wl_list_remove(&sub->input_add.link);
wl_list_remove(&sub->input_remove.link);
wl_list_remove(&sub->output_add.link);
wl_list_remove(&sub->output_remove.link);
wl_list_remove(&sub->backend_destroy.link);
wl_list_remove(&sub->link);
free(sub);
}
static void multi_backend_destroy(struct wlr_backend *wlr_backend) {
struct wlr_multi_backend *backend = (struct wlr_multi_backend *)wlr_backend;
wl_list_remove(&backend->display_destroy.link);
struct subbackend_state *sub, *next;
wl_list_for_each_safe(sub, next, &backend->backends, link) {
wlr_backend_destroy(sub->backend);
free(sub);
}
free(backend);
}
@ -60,11 +70,10 @@ struct wlr_backend_impl backend_impl = {
static void handle_display_destroy(struct wl_listener *listener, void *data) {
struct wlr_multi_backend *backend =
wl_container_of(listener, backend, display_destroy);
multi_backend_destroy(&backend->backend);
multi_backend_destroy((struct wlr_backend*)backend);
}
struct wlr_backend *wlr_multi_backend_create(struct wl_display *display,
struct wlr_session *session) {
struct wlr_backend *wlr_multi_backend_create(struct wl_display *display) {
struct wlr_multi_backend *backend =
calloc(1, sizeof(struct wlr_multi_backend));
if (!backend) {
@ -72,12 +81,14 @@ struct wlr_backend *wlr_multi_backend_create(struct wl_display *display,
return NULL;
}
backend->session = session;
wl_list_init(&backend->backends);
wlr_backend_init(&backend->backend, &backend_impl);
session->display_destroy.notify = handle_display_destroy;
wl_display_add_destroy_listener(display, &session->display_destroy);
wl_signal_init(&backend->events.backend_add);
wl_signal_init(&backend->events.backend_remove);
backend->display_destroy.notify = handle_display_destroy;
wl_display_add_destroy_listener(display, &backend->display_destroy);
return &backend->backend;
}
@ -110,11 +121,34 @@ static void output_remove_reemit(struct wl_listener *listener, void *data) {
wl_signal_emit(&state->container->events.output_remove, data);
}
static void handle_subbackend_destroy(struct wl_listener *listener,
void *data) {
struct subbackend_state *state = wl_container_of(listener,
state, backend_destroy);
subbackend_state_destroy(state);
}
static struct subbackend_state *multi_backend_get_subbackend(struct wlr_multi_backend *multi,
struct wlr_backend *backend) {
struct subbackend_state *sub = NULL;
wl_list_for_each(sub, &multi->backends, link) {
if (sub->backend == backend) {
return sub;
}
}
return NULL;
}
void wlr_multi_backend_add(struct wlr_backend *_multi,
struct wlr_backend *backend) {
assert(wlr_backend_is_multi(_multi));
struct wlr_multi_backend *multi = (struct wlr_multi_backend *)_multi;
if (multi_backend_get_subbackend(multi, backend)) {
// already added
return;
}
struct subbackend_state *sub;
if (!(sub = calloc(1, sizeof(struct subbackend_state)))) {
wlr_log(L_ERROR, "Could not add backend: allocation failed");
@ -125,20 +159,53 @@ void wlr_multi_backend_add(struct wlr_backend *_multi,
sub->backend = backend;
sub->container = &multi->backend;
sub->input_add.notify = input_add_reemit;
sub->input_remove.notify = input_remove_reemit;
sub->output_add.notify = output_add_reemit;
sub->output_remove.notify = output_remove_reemit;
wl_signal_add(&backend->events.destroy, &sub->backend_destroy);
sub->backend_destroy.notify = handle_subbackend_destroy;
wl_signal_add(&backend->events.input_add, &sub->input_add);
sub->input_add.notify = input_add_reemit;
wl_signal_add(&backend->events.input_remove, &sub->input_remove);
sub->input_remove.notify = input_remove_reemit;
wl_signal_add(&backend->events.output_add, &sub->output_add);
sub->output_add.notify = output_add_reemit;
wl_signal_add(&backend->events.output_remove, &sub->output_remove);
sub->output_remove.notify = output_remove_reemit;
wl_signal_emit(&multi->events.backend_add, backend);
}
void wlr_multi_backend_remove(struct wlr_backend *_multi,
struct wlr_backend *backend) {
assert(wlr_backend_is_multi(_multi));
struct wlr_multi_backend *multi = (struct wlr_multi_backend *)_multi;
struct subbackend_state *sub =
multi_backend_get_subbackend(multi, backend);
if (sub) {
wl_signal_emit(&multi->events.backend_remove, backend);
subbackend_state_destroy(sub);
}
}
struct wlr_session *wlr_multi_get_session(struct wlr_backend *_backend) {
assert(wlr_backend_is_multi(_backend));
struct wlr_multi_backend *backend = (struct wlr_multi_backend *)_backend;
return backend->session;
struct subbackend_state *sub;
wl_list_for_each(sub, &backend->backends, link) {
if (wlr_backend_is_drm(sub->backend)) {
return wlr_drm_backend_get_session(sub->backend);
}
}
return NULL;
}
bool wlr_multi_is_empty(struct wlr_backend *_backend) {
assert(wlr_backend_is_multi(_backend));
struct wlr_multi_backend *backend = (struct wlr_multi_backend *)_backend;
return wl_list_length(&backend->backends) < 1;
}

View file

@ -143,4 +143,6 @@ int wlr_drm_event(int fd, uint32_t mask, void *data);
void wlr_drm_connector_start_renderer(struct wlr_drm_connector *conn);
struct wlr_session *wlr_drm_backend_get_session(struct wlr_backend *backend);
#endif

View file

@ -9,10 +9,14 @@
struct wlr_multi_backend {
struct wlr_backend backend;
struct wlr_session *session;
struct wl_list backends;
struct wl_listener display_destroy;
struct {
struct wl_signal backend_add;
struct wl_signal backend_remove;
} events;
};
#endif

View file

@ -11,6 +11,7 @@ struct wlr_backend {
const struct wlr_backend_impl *impl;
struct {
struct wl_signal destroy;
struct wl_signal input_add;
struct wl_signal input_remove;
struct wl_signal output_add;

View file

@ -4,13 +4,18 @@
#include <wlr/backend.h>
#include <wlr/backend/session.h>
struct wlr_backend *wlr_multi_backend_create(struct wl_display *display,
struct wlr_session *session);
struct wlr_backend *wlr_multi_backend_create(struct wl_display *display);
void wlr_multi_backend_add(struct wlr_backend *multi,
struct wlr_backend *backend);
void wlr_multi_backend_remove(struct wlr_backend *multi,
struct wlr_backend *backend);
bool wlr_backend_is_multi(struct wlr_backend *backend);
struct wlr_session *wlr_multi_get_session(struct wlr_backend *base);
bool wlr_multi_is_empty(struct wlr_backend *backend);
#endif

View file

@ -5,6 +5,7 @@
#include <wayland-server.h>
#include <wlr/backend.h>
#include <wlr/backend/headless.h>
#include <wlr/backend/multi.h>
#include <wlr/render.h>
#include <wlr/render/gles2.h>
#include <wlr/util/log.h>
@ -30,7 +31,13 @@ int main(int argc, char **argv) {
assert(server.wl_display = wl_display_create());
assert(server.wl_event_loop = wl_display_get_event_loop(server.wl_display));
assert(server.backend = wlr_backend_autocreate(server.wl_display));
server.backend = wlr_backend_autocreate(server.wl_display);
if (server.backend == NULL) {
wlr_log(L_ERROR, "could not start backend");
wlr_backend_destroy(server.backend);
return 1;
}
assert(server.renderer = wlr_gles2_renderer_create(server.backend));
server.data_device_manager =