From 15b1ce9e6ced22a90ab0df7a6b86c6d35c40ca93 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 7 May 2017 10:00:23 -0400 Subject: [PATCH] Refactor backend state management --- backend/CMakeLists.txt | 1 + backend/backend.c | 37 +++++++++++ backend/drm/backend.c | 113 ++++++++++++++++------------------ backend/drm/drm.c | 51 ++++++++------- backend/drm/udev.c | 5 +- example/example-drm.c | 51 +++++++-------- include/backend.h | 17 +++++ include/backend/drm/backend.h | 10 +-- include/backend/drm/drm.h | 3 +- include/wlr/backend.h | 27 +++++++- include/wlr/backend/drm.h | 7 +-- 11 files changed, 196 insertions(+), 126 deletions(-) create mode 100644 backend/backend.c create mode 100644 include/backend.h diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 938dc1ab..7183617c 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -12,6 +12,7 @@ add_library(wlr-backend drm/backend.c drm/drm.c drm/udev.c + backend.c egl.c ) diff --git a/backend/backend.c b/backend/backend.c new file mode 100644 index 00000000..e40aaefe --- /dev/null +++ b/backend/backend.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include "common/log.h" +#include "backend.h" + +struct wlr_backend *wlr_backend_create(const struct wlr_backend_impl *impl, + struct wlr_backend_state *state) { + struct wlr_backend *backend = calloc(1, sizeof(struct wlr_backend)); + if (!backend) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return NULL; + } + backend->state = state; + backend->impl = impl; + wl_signal_init(&backend->events.output_add); + wl_signal_init(&backend->events.output_remove); + wl_signal_init(&backend->events.output_frame); + wl_signal_init(&backend->events.keyboard_add); + wl_signal_init(&backend->events.keyboard_remove); + wl_signal_init(&backend->events.pointer_add); + wl_signal_init(&backend->events.pointer_remove); + wl_signal_init(&backend->events.touch_add); + wl_signal_init(&backend->events.touch_remove); + return backend; +} + +bool wlr_backend_init(struct wlr_backend *backend) { + return backend->impl->init(backend->state); +} + +void wlr_backend_destroy(struct wlr_backend *backend) { + backend->impl->destroy(backend->state); + // TODO: Free anything else? + free(backend); +} diff --git a/backend/drm/backend.c b/backend/drm/backend.c index 42e7f5fd..8c02669a 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -8,106 +8,101 @@ #include #include +#include "backend.h" #include "backend/drm/backend.h" #include "backend/drm/drm.h" #include "backend/drm/udev.h" #include "common/log.h" -struct wlr_drm_backend *wlr_drm_backend_init(struct wl_display *display, - struct wlr_session *session, struct wl_listener *add, struct wl_listener *rem, - struct wl_listener *render) { +static bool wlr_drm_backend_init(struct wlr_backend_state *state) { + wlr_drm_scan_connectors(state); + return true; +} - struct wlr_drm_backend *backend = calloc(1, sizeof *backend); +static void free_output(void *item) { + struct wlr_drm_output *out = item; + wlr_drm_output_cleanup(out, true); + free(out); +} + +static void wlr_drm_backend_destroy(struct wlr_backend_state *state) { + if (!state) { + return; + } + list_foreach(state->outputs, free_output); + list_free(state->outputs); + wlr_drm_renderer_free(&state->renderer); + wlr_udev_free(&state->udev); + wlr_session_close_file(state->session, state->fd); + wlr_session_finish(state->session); + wl_event_source_remove(state->drm_event); + free(state); +} + +static struct wlr_backend_impl backend_impl = { + .init = wlr_drm_backend_init, + .destroy = wlr_drm_backend_destroy +}; + +struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, + struct wlr_session *session) { + struct wlr_backend_state *state = calloc(1, sizeof(struct wlr_backend_state)); + if (!state) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return NULL; + } + + struct wlr_backend *backend = wlr_backend_create(&backend_impl, state); if (!backend) { wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); return NULL; } - backend->session = session; - - backend->outputs = list_create(); - if (!backend->outputs) { + state->backend = backend; + state->session = session; + state->outputs = list_create(); + if (!state->outputs) { wlr_log(L_ERROR, "Failed to allocate list"); goto error_backend; } - if (!wlr_udev_init(display, &backend->udev)) { + if (!wlr_udev_init(display, &state->udev)) { wlr_log(L_ERROR, "Failed to start udev"); goto error_list; } - backend->fd = wlr_udev_find_gpu(&backend->udev, backend->session); - if (backend->fd == -1) { + state->fd = wlr_udev_find_gpu(&state->udev, state->session); + if (state->fd == -1) { wlr_log(L_ERROR, "Failed to open DRM device"); goto error_udev; } struct wl_event_loop *event_loop = wl_display_get_event_loop(display); - backend->drm_event = wl_event_loop_add_fd(event_loop, backend->fd, + state->drm_event = wl_event_loop_add_fd(event_loop, state->fd, WL_EVENT_READABLE, wlr_drm_event, NULL); - if (!backend->drm_event) { + if (!state->drm_event) { wlr_log(L_ERROR, "Failed to create DRM event source"); goto error_fd; } - if (!wlr_drm_renderer_init(&backend->renderer, backend->fd)) { + if (!wlr_drm_renderer_init(&state->renderer, state->fd)) { wlr_log(L_ERROR, "Failed to initialize renderer"); goto error_event; } - wl_signal_init(&backend->signals.output_add); - wl_signal_init(&backend->signals.output_rem); - wl_signal_init(&backend->signals.output_render); - - if (add) { - wl_signal_add(&backend->signals.output_add, add); - } - if (rem) { - wl_signal_add(&backend->signals.output_rem, rem); - } - if (render) { - wl_signal_add(&backend->signals.output_render, render); - } - - wlr_drm_scan_connectors(backend); - return backend; error_event: - wl_event_source_remove(backend->drm_event); + wl_event_source_remove(state->drm_event); error_fd: - wlr_session_close_file(backend->session, backend->fd); + wlr_session_close_file(state->session, state->fd); error_udev: - wlr_udev_free(&backend->udev); + wlr_udev_free(&state->udev); error_list: - list_free(backend->outputs); + list_free(state->outputs); error_backend: + free(state); free(backend); return NULL; } - -static void free_output(void *item) -{ - struct wlr_drm_output *out = item; - wlr_drm_output_cleanup(out, true); - free(out); -} - -void wlr_drm_backend_free(struct wlr_drm_backend *backend) -{ - if (!backend) - return; - - list_foreach(backend->outputs, free_output); - - wlr_drm_renderer_free(&backend->renderer); - wlr_udev_free(&backend->udev); - wlr_session_close_file(backend->session, backend->fd); - wlr_session_finish(backend->session); - - wl_event_source_remove(backend->drm_event); - - list_free(backend->outputs); - free(backend); -} diff --git a/backend/drm/drm.c b/backend/drm/drm.c index b5e5739a..abb3fb26 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -13,6 +13,7 @@ #include #include +#include "backend.h" #include "backend/drm/backend.h" #include "backend/drm/drm.h" #include "common/log.h" @@ -167,10 +168,10 @@ static int find_id(const void *item, const void *cmp_to) { } } -void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) { +void wlr_drm_scan_connectors(struct wlr_backend_state *state) { wlr_log(L_INFO, "Scanning DRM connectors"); - drmModeRes *res = drmModeGetResources(backend->fd); + drmModeRes *res = drmModeGetResources(state->fd); if (!res) { wlr_log(L_ERROR, "Failed to get DRM resources"); return; @@ -179,40 +180,40 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) { for (int i = 0; i < res->count_connectors; ++i) { uint32_t id = res->connectors[i]; - drmModeConnector *conn = drmModeGetConnector(backend->fd, id); + drmModeConnector *conn = drmModeGetConnector(state->fd, id); if (!conn) { wlr_log(L_ERROR, "Failed to get DRM connector"); continue; } struct wlr_drm_output *out; - int index = list_seq_find(backend->outputs, find_id, &id); + int index = list_seq_find(state->outputs, find_id, &id); if (index == -1) { - out = calloc(1, sizeof *out); + out = calloc(1, sizeof(struct wlr_drm_output)); if (!out) { wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); drmModeFreeConnector(conn); continue; } - out->renderer = &backend->renderer; + out->renderer = &state->renderer; out->state = DRM_OUTPUT_DISCONNECTED; out->connector = id; snprintf(out->name, sizeof out->name, "%s-%"PRIu32, conn_name[conn->connector_type], conn->connector_type_id); - drmModeEncoder *curr_enc = drmModeGetEncoder(backend->fd, conn->encoder_id); + drmModeEncoder *curr_enc = drmModeGetEncoder(state->fd, conn->encoder_id); if (curr_enc) { - out->old_crtc = drmModeGetCrtc(backend->fd, curr_enc->crtc_id); + out->old_crtc = drmModeGetCrtc(state->fd, curr_enc->crtc_id); free(curr_enc); } - list_add(backend->outputs, out); + list_add(state->outputs, out); wlr_log(L_INFO, "Found display '%s'", out->name); } else { - out = backend->outputs->items[index]; + out = state->outputs->items[index]; } if (out->state == DRM_OUTPUT_DISCONNECTED && @@ -244,8 +245,7 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) { out->state = DRM_OUTPUT_NEEDS_MODESET; wlr_log(L_INFO, "Sending modesetting signal for '%s'", out->name); - wl_signal_emit(&backend->signals.output_add, out); - + wl_signal_emit(&state->backend->events.output_add, out); } else if (out->state == DRM_OUTPUT_CONNECTED && conn->connection != DRM_MODE_CONNECTED) { @@ -270,12 +270,12 @@ struct wlr_drm_mode *wlr_drm_output_get_modes(struct wlr_drm_output *out, size_t } bool wlr_drm_output_modeset(struct wlr_drm_output *out, struct wlr_drm_mode *mode) { - struct wlr_drm_backend *backend = wl_container_of(out->renderer, backend, renderer); + struct wlr_backend_state *state = wl_container_of(out->renderer, state, renderer); wlr_log(L_INFO, "Modesetting '%s' with '%ux%u@%u'", out->name, mode->width, mode->height, mode->rate); - drmModeConnector *conn = drmModeGetConnector(backend->fd, out->connector); + drmModeConnector *conn = drmModeGetConnector(state->fd, out->connector); if (!conn) { wlr_log(L_ERROR, "Failed to get DRM connector"); goto error; @@ -286,7 +286,7 @@ bool wlr_drm_output_modeset(struct wlr_drm_output *out, struct wlr_drm_mode *mod goto error; } - drmModeRes *res = drmModeGetResources(backend->fd); + drmModeRes *res = drmModeGetResources(state->fd); if (!res) { wlr_log(L_ERROR, "Failed to get DRM resources"); goto error; @@ -294,7 +294,7 @@ bool wlr_drm_output_modeset(struct wlr_drm_output *out, struct wlr_drm_mode *mod bool success = false; for (int i = 0; !success && i < conn->count_encoders; ++i) { - drmModeEncoder *enc = drmModeGetEncoder(backend->fd, conn->encoders[i]); + drmModeEncoder *enc = drmModeGetEncoder(state->fd, conn->encoders[i]); if (!enc) continue; @@ -303,8 +303,8 @@ bool wlr_drm_output_modeset(struct wlr_drm_output *out, struct wlr_drm_mode *mod continue; } - if ((backend->taken_crtcs & (1 << j)) == 0) { - backend->taken_crtcs |= 1 << j; + if ((state->taken_crtcs & (1 << j)) == 0) { + state->taken_crtcs |= 1 << j; out->crtc = res->crtcs[j]; success = true; @@ -327,7 +327,7 @@ bool wlr_drm_output_modeset(struct wlr_drm_output *out, struct wlr_drm_mode *mod out->width = mode->width; out->height = mode->height; - if (!display_init_renderer(&backend->renderer, out)) { + if (!display_init_renderer(&state->renderer, out)) { wlr_log(L_ERROR, "Failed to initalise renderer for %s", out->name); goto error; } @@ -343,15 +343,14 @@ error: return false; } -static void page_flip_handler(int fd, unsigned seq, unsigned tv_sec, unsigned tv_usec, - void *user) { - +static void page_flip_handler(int fd, unsigned seq, + unsigned tv_sec, unsigned tv_usec, void *user) { struct wlr_drm_output *out = user; - struct wlr_drm_backend *backend = wl_container_of(out->renderer, backend, renderer); + struct wlr_backend_state *state = wl_container_of(out->renderer, state, renderer); out->pageflip_pending = false; if (out->state == DRM_OUTPUT_CONNECTED) { - wl_signal_emit(&backend->signals.output_render, out); + wl_signal_emit(&state->backend->events.output_frame, out); } } @@ -388,7 +387,7 @@ void wlr_drm_output_cleanup(struct wlr_drm_output *out, bool restore) { } struct wlr_drm_renderer *renderer = out->renderer; - struct wlr_drm_backend *backend = wl_container_of(renderer, backend, renderer); + struct wlr_backend_state *state = wl_container_of(renderer, state, renderer); switch (out->state) { case DRM_OUTPUT_CONNECTED: @@ -414,7 +413,7 @@ void wlr_drm_output_cleanup(struct wlr_drm_output *out, bool restore) { } wlr_log(L_INFO, "Emmiting destruction signal for '%s'", out->name); - wl_signal_emit(&backend->signals.output_rem, out); + wl_signal_emit(&state->backend->events.output_remove, out); break; case DRM_OUTPUT_DISCONNECTED: diff --git a/backend/drm/udev.c b/backend/drm/udev.c index 438047d6..e5470157 100644 --- a/backend/drm/udev.c +++ b/backend/drm/udev.c @@ -11,6 +11,7 @@ #include +#include "backend.h" #include "backend/drm/backend.h" #include "backend/drm/udev.h" #include "backend/drm/drm.h" @@ -139,7 +140,7 @@ int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session) { static int udev_event(int fd, uint32_t mask, void *data) { struct wlr_udev *udev = data; - struct wlr_drm_backend *backend = wl_container_of(udev, backend, udev); + struct wlr_backend_state *state = wl_container_of(udev, state, udev); struct udev_device *dev = udev_monitor_receive_device(udev->mon); if (!dev) { @@ -156,7 +157,7 @@ static int udev_event(int fd, uint32_t mask, void *data) { goto out; } - wlr_drm_scan_connectors(backend); + wlr_drm_scan_connectors(state); out: udev_device_unref(dev); diff --git a/example/example-drm.c b/example/example-drm.c index 1b1cce79..2e33e039 100644 --- a/example/example-drm.c +++ b/example/example-drm.c @@ -14,13 +14,12 @@ struct state { struct timespec last_frame; - struct wl_listener add; - struct wl_listener rem; - struct wl_listener render; + struct wl_listener output_add; + struct wl_listener output_remove; + struct wl_listener output_frame; }; -void output_add(struct wl_listener *listener, void *data) -{ +void output_add(struct wl_listener *listener, void *data) { struct wlr_drm_output *out = data; fprintf(stderr, "Output '%s' added\n", wlr_drm_output_get_name(out)); @@ -30,16 +29,14 @@ void output_add(struct wl_listener *listener, void *data) wlr_drm_output_modeset(out, &modes[0]); } -void output_rem(struct wl_listener *listener, void *data) -{ +void output_remove(struct wl_listener *listener, void *data) { struct wlr_drm_output *out = data; fprintf(stderr, "Output '%s' removed\n", wlr_drm_output_get_name(out)); } -void output_render(struct wl_listener *listener, void *data) -{ +void output_frame(struct wl_listener *listener, void *data) { struct wlr_drm_output *out = data; - struct state *s = wl_container_of(listener, s, render); + struct state *s = wl_container_of(listener, s, output_frame); struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); @@ -68,14 +65,12 @@ void output_render(struct wl_listener *listener, void *data) wlr_drm_output_end(out); } -int timer_done(void *data) -{ +int timer_done(void *data) { *(bool *)data = true; return 1; } -int main() -{ +int main() { if (getenv("DISPLAY")) { fprintf(stderr, "Detected that X is running. Run this in its own virtual terminal.\n"); return 1; @@ -87,14 +82,14 @@ int main() struct state state = { .color = { 1.0, 0.0, 0.0 }, .dec = 0, - .add = { .notify = output_add }, - .rem = { .notify = output_rem }, - .render = { .notify = output_render }, + .output_add = { .notify = output_add }, + .output_remove = { .notify = output_remove }, + .output_frame = { .notify = output_frame }, }; - wl_list_init(&state.add.link); - wl_list_init(&state.rem.link); - wl_list_init(&state.render.link); + wl_list_init(&state.output_add.link); + wl_list_init(&state.output_remove.link); + wl_list_init(&state.output_frame.link); clock_gettime(CLOCK_MONOTONIC, &state.last_frame); struct wl_display *display = wl_display_create(); @@ -105,19 +100,25 @@ int main() return 1; } - struct wlr_drm_backend *wlr = wlr_drm_backend_init(display, session, - &state.add, &state.rem, &state.render); + struct wlr_backend *wlr = wlr_drm_backend_create(display, session); + wl_signal_add(&wlr->events.output_add, &state.output_add); + wl_signal_add(&wlr->events.output_remove, &state.output_remove); + wl_signal_add(&wlr->events.output_frame, &state.output_frame); + if (!wlr || !wlr_backend_init(wlr)) { + return 1; + } bool done = false; struct wl_event_source *timer = wl_event_loop_add_timer(event_loop, timer_done, &done); - wl_event_source_timer_update(timer, 10000); + wl_event_source_timer_update(timer, 5000); - while (!done) + while (!done) { wl_event_loop_dispatch(event_loop, 0); + } wl_event_source_remove(timer); - wlr_drm_backend_free(wlr); + wlr_backend_destroy(wlr); wl_display_destroy(display); } diff --git a/include/backend.h b/include/backend.h new file mode 100644 index 00000000..d42c6f17 --- /dev/null +++ b/include/backend.h @@ -0,0 +1,17 @@ +#ifndef _WLR_BACKEND_INTERNAL_H +#define _WLR_BACKEND_INTERNAL_H + +#include +#include + +struct wlr_backend_state; + +struct wlr_backend_impl { + bool (*init)(struct wlr_backend_state *state); + void (*destroy)(struct wlr_backend_state *state); +}; + +struct wlr_backend *wlr_backend_create(const struct wlr_backend_impl *impl, + struct wlr_backend_state *state); + +#endif diff --git a/include/backend/drm/backend.h b/include/backend/drm/backend.h index bdda7ea7..0c725ccb 100644 --- a/include/backend/drm/backend.h +++ b/include/backend/drm/backend.h @@ -12,21 +12,17 @@ #include #include +#include "backend.h" #include "udev.h" #include "event.h" #include "drm.h" -struct wlr_drm_backend { +struct wlr_backend_state { int fd; + struct wlr_backend *backend; struct wl_event_source *drm_event; - struct { - struct wl_signal output_add; - struct wl_signal output_rem; - struct wl_signal output_render; - } signals; - uint32_t taken_crtcs; list_t *outputs; diff --git a/include/backend/drm/drm.h b/include/backend/drm/drm.h index 51b0cdc6..3e83058a 100644 --- a/include/backend/drm/drm.h +++ b/include/backend/drm/drm.h @@ -8,6 +8,7 @@ #include #include "backend/egl.h" +#include "backend.h" struct wlr_drm_renderer { int fd; @@ -50,7 +51,7 @@ struct wlr_drm_output { void wlr_drm_output_cleanup(struct wlr_drm_output *out, bool restore); -void wlr_drm_scan_connectors(struct wlr_drm_backend *backend); +void wlr_drm_scan_connectors(struct wlr_backend_state *state); int wlr_drm_event(int fd, uint32_t mask, void *data); #endif diff --git a/include/wlr/backend.h b/include/wlr/backend.h index 527efa05..88a5c8e9 100644 --- a/include/wlr/backend.h +++ b/include/wlr/backend.h @@ -1,7 +1,30 @@ #ifndef _WLR_BACKEND_H #define _WLR_BACKEND_H -struct wlr_backend *wlr_backend_init(); -void wlr_backend_free(struct wlr_backend *backend); +#include + +struct wlr_backend_impl; +struct wlr_backend_state; + +struct wlr_backend { + const struct wlr_backend_impl *impl; + struct wlr_backend_state *state; + + struct { + struct wl_signal output_add; + struct wl_signal output_remove; + struct wl_signal output_frame; + struct wl_signal keyboard_add; + struct wl_signal keyboard_remove; + struct wl_signal pointer_add; + struct wl_signal pointer_remove; + struct wl_signal touch_add; + struct wl_signal touch_remove; + } events; +}; + +struct wlr_backend *wlr_backend_autocreate(); +bool wlr_backend_init(struct wlr_backend *backend); +void wlr_backend_destroy(struct wlr_backend *backend); #endif diff --git a/include/wlr/backend/drm.h b/include/wlr/backend/drm.h index 4131b4cb..5db9b35c 100644 --- a/include/wlr/backend/drm.h +++ b/include/wlr/backend/drm.h @@ -3,6 +3,7 @@ #include #include +#include #include // drmModeModeInfo struct wlr_drm_backend; @@ -15,10 +16,8 @@ struct wlr_drm_mode { drmModeModeInfo mode; }; -struct wlr_drm_backend *wlr_drm_backend_init(struct wl_display *display, - struct wlr_session *session, struct wl_listener *add, struct wl_listener *rem, - struct wl_listener *render); -void wlr_drm_backend_free(struct wlr_drm_backend *backend); +struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, + struct wlr_session *session); const char *wlr_drm_output_get_name(struct wlr_drm_output *out);