Various cleanups

This commit is contained in:
Scott Anderson 2017-08-05 18:15:39 +12:00
parent d1ca1ec16e
commit dd7a349295
3 changed files with 61 additions and 50 deletions

View File

@ -15,24 +15,26 @@
#include <backend/udev.h> #include <backend/udev.h>
#include "drm.h" #include "drm.h"
static bool wlr_drm_backend_init(struct wlr_backend_state *state) { static bool wlr_drm_backend_init(struct wlr_backend_state *drm) {
wlr_drm_scan_connectors(state); wlr_drm_scan_connectors(drm);
return true; return true;
} }
static void wlr_drm_backend_destroy(struct wlr_backend_state *state) { static void wlr_drm_backend_destroy(struct wlr_backend_state *drm) {
if (!state) { if (!drm) {
return; return;
} }
for (size_t i = 0; state->outputs && i < state->outputs->length; ++i) {
struct wlr_output_state *output = state->outputs->items[i]; for (size_t i = 0; drm->outputs && i < drm->outputs->length; ++i) {
struct wlr_output_state *output = drm->outputs->items[i];
wlr_output_destroy(output->base); wlr_output_destroy(output->base);
} }
wlr_udev_signal_remove(state->udev, &state->drm_invalidated);
wlr_drm_renderer_free(&state->renderer); wlr_udev_signal_remove(drm->udev, &drm->drm_invalidated);
wlr_session_close_file(state->session, state->fd); wlr_drm_renderer_free(&drm->renderer);
wl_event_source_remove(state->drm_event); wlr_session_close_file(drm->session, drm->fd);
free(state); wl_event_source_remove(drm->drm_event);
free(drm);
} }
static struct wlr_backend_impl backend_impl = { static struct wlr_backend_impl backend_impl = {
@ -76,73 +78,68 @@ static void drm_invalidated(struct wl_listener *listener, void *data) {
struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
struct wlr_session *session, struct wlr_udev *udev, int gpu_fd) { struct wlr_session *session, struct wlr_udev *udev, int gpu_fd) {
assert(display && session && gpu_fd > 0); assert(display && session && gpu_fd >= 0);
char *name = drmGetDeviceNameFromFd2(gpu_fd); char *name = drmGetDeviceNameFromFd2(gpu_fd);
drmVersion *version = drmGetVersion(gpu_fd); drmVersion *version = drmGetVersion(gpu_fd);
wlr_log(L_INFO, "Initalizing DRM backend for %s (%s)", name, version->name); wlr_log(L_INFO, "Initalizing DRM backend for %s (%s)", name, version->name);
free(name); free(name);
drmFreeVersion(version); drmFreeVersion(version);
struct wlr_backend_state *state = calloc(1, sizeof(struct wlr_backend_state)); struct wlr_backend_state *drm = calloc(1, sizeof(*drm));
if (!state) { if (!drm) {
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); wlr_log_errno(L_ERROR, "Allocation failed");
return NULL; return NULL;
} }
struct wlr_backend *backend = wlr_backend_create(&backend_impl, state); struct wlr_backend *backend = wlr_backend_create(&backend_impl, drm);
if (!backend) { if (!backend) {
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); wlr_log_errno(L_ERROR, "Allocation failed");
return NULL; return NULL;
} }
state->base = backend; drm->base = backend;
state->session = session; drm->session = session;
state->udev = udev; drm->udev = udev;
state->outputs = list_create(); drm->outputs = list_create();
if (!state->outputs) { if (!drm->outputs) {
wlr_log(L_ERROR, "Failed to allocate list"); wlr_log(L_ERROR, "Failed to allocate list");
goto error_backend; goto error_backend;
} }
state->fd = gpu_fd; drm->fd = gpu_fd;
struct stat st; struct stat st;
if (fstat(state->fd, &st) < 0) { if (fstat(drm->fd, &st) < 0) {
wlr_log(L_ERROR, "Stat failed: %s", strerror(errno)); wlr_log_errno(L_ERROR, "Stat failed");
} }
state->dev = st.st_rdev; drm->dev = st.st_rdev;
state->drm_invalidated.notify = drm_invalidated; drm->drm_invalidated.notify = drm_invalidated;
wlr_udev_signal_add(udev, state->dev, &state->drm_invalidated); wlr_udev_signal_add(udev, drm->dev, &drm->drm_invalidated);
state->display = display; drm->display = display;
struct wl_event_loop *event_loop = wl_display_get_event_loop(display); struct wl_event_loop *event_loop = wl_display_get_event_loop(display);
state->drm_event = wl_event_loop_add_fd(event_loop, state->fd, drm->drm_event = wl_event_loop_add_fd(event_loop, drm->fd,
WL_EVENT_READABLE, wlr_drm_event, NULL); WL_EVENT_READABLE, wlr_drm_event, NULL);
if (!state->drm_event) { if (!drm->drm_event) {
wlr_log(L_ERROR, "Failed to create DRM event source"); wlr_log(L_ERROR, "Failed to create DRM event source");
goto error_fd; goto error_fd;
} }
state->session_signal.notify = session_signal; drm->session_signal.notify = session_signal;
wl_signal_add(&session->session_signal, &state->session_signal); wl_signal_add(&session->session_signal, &drm->session_signal);
if (drmSetClientCap(state->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1)) { if (!wlr_drm_check_features(drm)) {
wlr_log(L_INFO, "DRM universal planes unsupported"); goto error_event;
}
if (drmSetClientCap(state->fd, DRM_CLIENT_CAP_ATOMIC, 1)) {
wlr_log(L_INFO, "Atomic modesetting unsupported");
} }
wlr_drm_init_resources(state); if (!wlr_drm_resources_init(drm)) {
goto error_event;
}
// TODO: what is the difference between the per-output renderer and this if (!wlr_drm_renderer_init(&drm->renderer, drm->fd)) {
// one?
if (!wlr_drm_renderer_init(&state->renderer, state->fd)) {
wlr_log(L_ERROR, "Failed to initialize renderer"); wlr_log(L_ERROR, "Failed to initialize renderer");
goto error_event; goto error_event;
} }
@ -150,12 +147,12 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
return backend; return backend;
error_event: error_event:
wl_event_source_remove(state->drm_event); wl_event_source_remove(drm->drm_event);
error_fd: error_fd:
wlr_session_close_file(state->session, state->fd); wlr_session_close_file(drm->session, drm->fd);
list_free(state->outputs); list_free(drm->outputs);
error_backend: error_backend:
free(state); free(drm);
free(backend); free(backend);
return NULL; return NULL;
} }

View File

@ -18,6 +18,19 @@
#include "drm.h" #include "drm.h"
#include "drm-util.h" #include "drm-util.h"
bool wlr_drm_check_features(struct wlr_backend_state *drm) {
if (drmSetClientCap(drm->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1)) {
wlr_log(L_INFO, "DRM universal planes unsupported");
return false;
}
if (drmSetClientCap(drm->fd, DRM_CLIENT_CAP_ATOMIC, 1)) {
wlr_log(L_INFO, "Atomic modesetting unsupported");
}
return true;
}
static int cmp_plane(const void *arg1, const void *arg2) static int cmp_plane(const void *arg1, const void *arg2)
{ {
const struct wlr_drm_plane *a = arg1; const struct wlr_drm_plane *a = arg1;
@ -112,7 +125,7 @@ error_res:
return false; return false;
} }
bool wlr_drm_init_resources(struct wlr_backend_state *drm) { bool wlr_drm_resources_init(struct wlr_backend_state *drm) {
drmModeRes *res = drmModeGetResources(drm->fd); drmModeRes *res = drmModeGetResources(drm->fd);
if (!res) { if (!res) {
wlr_log_errno(L_ERROR, "Failed to get DRM resources"); wlr_log_errno(L_ERROR, "Failed to get DRM resources");

View File

@ -151,7 +151,8 @@ struct wlr_output_state {
bool cleanup; bool cleanup;
}; };
bool wlr_drm_init_resources(struct wlr_backend_state *drm); bool wlr_drm_check_features(struct wlr_backend_state *drm);
bool wlr_drm_resources_init(struct wlr_backend_state *drm);
void wlr_drm_output_cleanup(struct wlr_output_state *output, bool restore); void wlr_drm_output_cleanup(struct wlr_output_state *output, bool restore);
void wlr_drm_scan_connectors(struct wlr_backend_state *state); void wlr_drm_scan_connectors(struct wlr_backend_state *state);