Changed backend to accept wl_display.

master
Scott Anderson 8 years ago
parent 0002b8dd08
commit d196a79b6c

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <wayland-server.h>
#include <wlr/session.h> #include <wlr/session.h>
#include <wlr/common/list.h> #include <wlr/common/list.h>
@ -12,9 +13,10 @@
#include "backend/drm/udev.h" #include "backend/drm/udev.h"
#include "common/log.h" #include "common/log.h"
struct wlr_drm_backend *wlr_drm_backend_init(struct wlr_session *session, struct wlr_drm_backend *wlr_drm_backend_init(struct wl_display *display,
struct wl_listener *add, struct wl_listener *rem, struct wl_listener *render) struct wlr_session *session, struct wl_listener *add, struct wl_listener *rem,
{ struct wl_listener *render) {
struct wlr_drm_backend *backend = calloc(1, sizeof *backend); struct wlr_drm_backend *backend = calloc(1, sizeof *backend);
if (!backend) { if (!backend) {
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
@ -29,15 +31,9 @@ struct wlr_drm_backend *wlr_drm_backend_init(struct wlr_session *session,
goto error_backend; goto error_backend;
} }
backend->event_loop = wl_event_loop_create(); if (!wlr_udev_init(display, &backend->udev)) {
if (!backend->event_loop) {
wlr_log(L_ERROR, "Failed to create event loop");
goto error_list;
}
if (!wlr_udev_init(backend)) {
wlr_log(L_ERROR, "Failed to start udev"); wlr_log(L_ERROR, "Failed to start udev");
goto error_loop; goto error_list;
} }
backend->fd = wlr_udev_find_gpu(&backend->udev, backend->session); backend->fd = wlr_udev_find_gpu(&backend->udev, backend->session);
@ -46,32 +42,44 @@ struct wlr_drm_backend *wlr_drm_backend_init(struct wlr_session *session,
goto error_udev; goto error_udev;
} }
if (!wlr_drm_renderer_init(&backend->renderer, backend, backend->fd)) { struct wl_event_loop *event_loop = wl_display_get_event_loop(display);
wlr_log(L_ERROR, "Failed to initialize renderer");
backend->drm_event = wl_event_loop_add_fd(event_loop, backend->fd,
WL_EVENT_READABLE, wlr_drm_event, NULL);
if (!backend->drm_event) {
wlr_log(L_ERROR, "Failed to create DRM event source");
goto error_fd; goto error_fd;
} }
if (!wlr_drm_renderer_init(&backend->renderer, backend->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_add);
wl_signal_init(&backend->signals.output_rem); wl_signal_init(&backend->signals.output_rem);
wl_signal_init(&backend->signals.output_render); wl_signal_init(&backend->signals.output_render);
if (add) if (add) {
wl_signal_add(&backend->signals.output_add, add); wl_signal_add(&backend->signals.output_add, add);
if (rem) }
if (rem) {
wl_signal_add(&backend->signals.output_rem, rem); wl_signal_add(&backend->signals.output_rem, rem);
if (render) }
if (render) {
wl_signal_add(&backend->signals.output_render, render); wl_signal_add(&backend->signals.output_render, render);
}
wlr_drm_scan_connectors(backend); wlr_drm_scan_connectors(backend);
return backend; return backend;
error_event:
wl_event_source_remove(backend->drm_event);
error_fd: error_fd:
wlr_session_close_file(backend->session, backend->fd); wlr_session_close_file(backend->session, backend->fd);
error_udev: error_udev:
wlr_udev_free(&backend->udev); wlr_udev_free(&backend->udev);
error_loop:
wl_event_loop_destroy(backend->event_loop);
error_list: error_list:
list_free(backend->outputs); list_free(backend->outputs);
error_backend: error_backend:
@ -98,15 +106,8 @@ void wlr_drm_backend_free(struct wlr_drm_backend *backend)
wlr_session_close_file(backend->session, backend->fd); wlr_session_close_file(backend->session, backend->fd);
wlr_session_finish(backend->session); wlr_session_finish(backend->session);
wl_event_source_remove(backend->event_src.drm); wl_event_source_remove(backend->drm_event);
wl_event_source_remove(backend->event_src.udev);
wl_event_loop_destroy(backend->event_loop);
list_free(backend->outputs); list_free(backend->outputs);
free(backend); free(backend);
} }
struct wl_event_loop *wlr_drm_backend_get_event_loop(struct wlr_drm_backend *backend)
{
return backend->event_loop;
}

@ -41,7 +41,7 @@ static void page_flip_handler(int fd, unsigned seq, unsigned tv_sec, unsigned tv
void *user) { void *user) {
struct wlr_drm_output *out = user; struct wlr_drm_output *out = user;
struct wlr_drm_backend *backend = out->renderer->backend; struct wlr_drm_backend *backend = wl_container_of(out->renderer, backend, renderer);
out->pageflip_pending = true; out->pageflip_pending = true;
if (!out->cleanup) { if (!out->cleanup) {
@ -49,7 +49,7 @@ static void page_flip_handler(int fd, unsigned seq, unsigned tv_sec, unsigned tv
} }
} }
static int drm_event(int fd, uint32_t mask, void *data) { int wlr_drm_event(int fd, uint32_t mask, void *data) {
drmEventContext event = { drmEventContext event = {
.version = DRM_EVENT_CONTEXT_VERSION, .version = DRM_EVENT_CONTEXT_VERSION,
.page_flip_handler = page_flip_handler, .page_flip_handler = page_flip_handler,
@ -60,8 +60,7 @@ static int drm_event(int fd, uint32_t mask, void *data) {
return 1; return 1;
} }
bool wlr_drm_renderer_init(struct wlr_drm_renderer *renderer, bool wlr_drm_renderer_init(struct wlr_drm_renderer *renderer, int fd) {
struct wlr_drm_backend *backend, int fd) {
renderer->gbm = gbm_create_device(fd); renderer->gbm = gbm_create_device(fd);
if (!renderer->gbm) { if (!renderer->gbm) {
@ -70,28 +69,12 @@ bool wlr_drm_renderer_init(struct wlr_drm_renderer *renderer,
} }
if (!wlr_egl_init(&renderer->egl, EGL_PLATFORM_GBM_MESA, renderer->gbm)) { if (!wlr_egl_init(&renderer->egl, EGL_PLATFORM_GBM_MESA, renderer->gbm)) {
goto error_gbm; gbm_device_destroy(renderer->gbm);
} return false;
backend->event_src.drm = wl_event_loop_add_fd(backend->event_loop,
backend->fd, WL_EVENT_READABLE, drm_event, NULL);
if (!backend->event_src.drm) {
wlr_log(L_ERROR, "Failed to create DRM event source");
goto error_egl;
} }
renderer->fd = fd; renderer->fd = fd;
renderer->backend = backend;
return true; return true;
error_egl:
wlr_egl_free(&renderer->egl);
error_gbm:
gbm_device_destroy(renderer->gbm);
return false;
} }
void wlr_drm_renderer_free(struct wlr_drm_renderer *renderer) { void wlr_drm_renderer_free(struct wlr_drm_renderer *renderer) {
@ -296,7 +279,7 @@ static drmModeModeInfo *select_mode(size_t num_modes, drmModeModeInfo modes[stat
} }
bool wlr_drm_output_modeset(struct wlr_drm_output *out, const char *str) { bool wlr_drm_output_modeset(struct wlr_drm_output *out, const char *str) {
struct wlr_drm_backend *backend = out->renderer->backend; struct wlr_drm_backend *backend = wl_container_of(out->renderer, backend, renderer);
wlr_log(L_INFO, "Modesetting %s with '%s'", out->name, str); wlr_log(L_INFO, "Modesetting %s with '%s'", out->name, str);
drmModeConnector *conn = drmModeGetConnector(backend->fd, out->connector); drmModeConnector *conn = drmModeGetConnector(backend->fd, out->connector);

@ -16,36 +16,39 @@
#include "backend/drm/drm.h" #include "backend/drm/drm.h"
#include "common/log.h" #include "common/log.h"
static bool device_is_kms(struct wlr_udev *udev, /* Tests if 'path' is KMS compatible by trying to open it.
struct wlr_session *session, * It leaves the open device in *fd_out it it succeeds.
struct udev_device *dev, */
int *fd_out) static bool device_is_kms(struct wlr_session *restrict session,
{ const char *restrict path, int *restrict fd_out) {
const char *path = udev_device_get_devnode(dev);
int fd; int fd;
if (!path) if (!path) {
return false; return false;
}
fd = wlr_session_open_file(session, path); fd = wlr_session_open_file(session, path);
if (fd < 0) if (fd < 0) {
return false; return false;
}
drmModeRes *res = drmModeGetResources(fd); drmModeRes *res = drmModeGetResources(fd);
if (!res) if (!res) {
goto out_fd; goto out_fd;
}
if (res->count_crtcs <= 0 || res->count_connectors <= 0 || if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
res->count_encoders <= 0) res->count_encoders <= 0) {
goto out_res; goto out_res;
}
if (*fd_out >= 0) { if (*fd_out >= 0) {
wlr_session_close_file(session, *fd_out); wlr_session_close_file(session, *fd_out);
free(udev->drm_path);
} }
*fd_out = fd; *fd_out = fd;
udev->drm_path = strdup(path);
drmModeFreeResources(res); drmModeFreeResources(res);
return true; return true;
@ -57,10 +60,10 @@ out_fd:
return false; return false;
} }
int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session) /* Tries to find the primary GPU by checking for the "boot_vga" attribute.
{ * If it's not found, it returns the first valid GPU it finds.
int fd = -1; */
int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session) {
struct udev_enumerate *en = udev_enumerate_new(udev->udev); struct udev_enumerate *en = udev_enumerate_new(udev->udev);
if (!en) { if (!en) {
wlr_log(L_ERROR, "Failed to create udev enumeration"); wlr_log(L_ERROR, "Failed to create udev enumeration");
@ -69,16 +72,20 @@ int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session)
udev_enumerate_add_match_subsystem(en, "drm"); udev_enumerate_add_match_subsystem(en, "drm");
udev_enumerate_add_match_sysname(en, "card[0-9]*"); udev_enumerate_add_match_sysname(en, "card[0-9]*");
udev_enumerate_scan_devices(en); udev_enumerate_scan_devices(en);
struct udev_list_entry *entry; struct udev_list_entry *entry;
int fd = -1;
char *drm_path = NULL;
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(en)) { udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(en)) {
bool is_boot_vga = false; bool is_boot_vga = false;
const char *path = udev_list_entry_get_name(entry); const char *path = udev_list_entry_get_name(entry);
struct udev_device *dev = udev_device_new_from_syspath(udev->udev, path); struct udev_device *dev = udev_device_new_from_syspath(udev->udev, path);
if (!dev) if (!dev) {
continue; continue;
}
/* /*
const char *seat = udev_device_get_property_value(dev, "ID_SEAT"); const char *seat = udev_device_get_property_value(dev, "ID_SEAT");
@ -90,14 +97,15 @@ int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session)
} }
*/ */
// This is owned by 'dev', so we don't need to free it
struct udev_device *pci = struct udev_device *pci =
udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL); udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL);
if (pci) { if (pci) {
const char *id = udev_device_get_sysattr_value(pci, "boot_vga"); const char *id = udev_device_get_sysattr_value(pci, "boot_vga");
if (id && strcmp(id, "1") == 0) if (id && strcmp(id, "1") == 0) {
is_boot_vga = true; is_boot_vga = true;
//udev_device_unref(pci); }
} }
// We already have a valid GPU // We already have a valid GPU
@ -106,38 +114,47 @@ int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session)
continue; continue;
} }
if (!device_is_kms(udev, session, dev, &fd)) { path = udev_device_get_devnode(dev);
if (!device_is_kms(session, path, &fd)) {
udev_device_unref(dev); udev_device_unref(dev);
continue; continue;
} }
free(drm_path);
drm_path = strdup(path);
udev_device_unref(dev); udev_device_unref(dev);
if (is_boot_vga) // We've found the primary GPU
if (is_boot_vga) {
break; break;
}
} }
udev_enumerate_unref(en); udev_enumerate_unref(en);
udev->drm_path = drm_path;
return fd; return fd;
} }
static int udev_event(int fd, uint32_t mask, void *data) static int udev_event(int fd, uint32_t mask, void *data) {
{ struct wlr_udev *udev = data;
struct wlr_drm_backend *backend = data; struct wlr_drm_backend *backend = wl_container_of(udev, backend, udev);
struct wlr_udev *udev = &backend->udev;
struct udev_device *dev = udev_monitor_receive_device(udev->mon); struct udev_device *dev = udev_monitor_receive_device(udev->mon);
if (!dev) if (!dev) {
return 1; return 1;
}
const char *path = udev_device_get_devnode(dev); const char *path = udev_device_get_devnode(dev);
if (!path || strcmp(path, udev->drm_path) != 0) if (!path || strcmp(path, udev->drm_path) != 0) {
goto out; goto out;
}
const char *action = udev_device_get_action(dev); const char *action = udev_device_get_action(dev);
if (!action || strcmp(action, "change") != 0) if (!action || strcmp(action, "change") != 0) {
goto out; goto out;
}
wlr_drm_scan_connectors(backend); wlr_drm_scan_connectors(backend);
@ -146,10 +163,7 @@ out:
return 1; return 1;
} }
bool wlr_udev_init(struct wlr_drm_backend *backend) bool wlr_udev_init(struct wl_display *display, struct wlr_udev *udev) {
{
struct wlr_udev *udev = &backend->udev;
udev->udev = udev_new(); udev->udev = udev_new();
if (!udev->udev) { if (!udev->udev) {
wlr_log(L_ERROR, "Failed to create udev context"); wlr_log(L_ERROR, "Failed to create udev context");
@ -165,10 +179,12 @@ bool wlr_udev_init(struct wlr_drm_backend *backend)
udev_monitor_filter_add_match_subsystem_devtype(udev->mon, "drm", NULL); udev_monitor_filter_add_match_subsystem_devtype(udev->mon, "drm", NULL);
udev_monitor_enable_receiving(udev->mon); udev_monitor_enable_receiving(udev->mon);
backend->event_src.udev = wl_event_loop_add_fd(backend->event_loop, struct wl_event_loop *event_loop = wl_display_get_event_loop(display);
udev_monitor_get_fd(udev->mon), WL_EVENT_READABLE, int fd = udev_monitor_get_fd(udev->mon);
udev_event, backend);
if (!backend->event_src.udev) { udev->event = wl_event_loop_add_fd(event_loop, fd, WL_EVENT_READABLE,
udev_event, udev);
if (!udev->event) {
wlr_log(L_ERROR, "Failed to create udev event source"); wlr_log(L_ERROR, "Failed to create udev event source");
goto error_mon; goto error_mon;
} }
@ -184,11 +200,12 @@ error_udev:
return false; return false;
} }
void wlr_udev_free(struct wlr_udev *udev) void wlr_udev_free(struct wlr_udev *udev) {
if (!udev) {
{
if (!udev)
return; return;
}
wl_event_source_remove(udev->event);
udev_monitor_unref(udev->mon); udev_monitor_unref(udev->mon);
udev_unref(udev->udev); udev_unref(udev->udev);

@ -93,16 +93,18 @@ int main()
wl_list_init(&state.render.link); wl_list_init(&state.render.link);
clock_gettime(CLOCK_MONOTONIC, &state.last_frame); clock_gettime(CLOCK_MONOTONIC, &state.last_frame);
struct wl_display *display = wl_display_create();
struct wl_event_loop *event_loop = wl_display_get_event_loop(display);
struct wlr_session *session = wlr_session_start(); struct wlr_session *session = wlr_session_start();
if (!session) { if (!session) {
return 1; return 1;
} }
struct wlr_drm_backend *wlr = wlr_drm_backend_init(session, struct wlr_drm_backend *wlr = wlr_drm_backend_init(display, session,
&state.add, &state.rem, &state.render); &state.add, &state.rem, &state.render);
bool done = false; bool done = false;
struct wl_event_loop *event_loop = wlr_drm_backend_get_event_loop(wlr);
struct wl_event_source *timer = wl_event_loop_add_timer(event_loop, struct wl_event_source *timer = wl_event_loop_add_timer(event_loop,
timer_done, &done); timer_done, &done);

@ -20,12 +20,7 @@ struct wlr_drm_backend {
int fd; int fd;
bool paused; bool paused;
struct wl_event_loop *event_loop; struct wl_event_source *drm_event;
struct {
struct wl_event_source *drm;
struct wl_event_source *udev;
} event_src;
struct { struct {
struct wl_signal output_add; struct wl_signal output_add;
@ -33,17 +28,12 @@ struct wlr_drm_backend {
struct wl_signal output_render; struct wl_signal output_render;
} signals; } signals;
list_t *outputs;
uint32_t taken_crtcs; uint32_t taken_crtcs;
list_t *outputs;
struct wlr_drm_renderer renderer; struct wlr_drm_renderer renderer;
struct wlr_session *session; struct wlr_session *session;
struct wlr_udev udev; struct wlr_udev udev;
}; };
struct wlr_drm_backend *wlr_drm_backend_init(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);
#endif #endif

@ -21,8 +21,7 @@ struct wlr_drm_renderer {
struct wlr_egl egl; struct wlr_egl egl;
}; };
bool wlr_drm_renderer_init(struct wlr_drm_renderer *renderer, bool wlr_drm_renderer_init(struct wlr_drm_renderer *renderer, int fd);
struct wlr_drm_backend *backend, int fd);
void wlr_drm_renderer_free(struct wlr_drm_renderer *renderer); void wlr_drm_renderer_free(struct wlr_drm_renderer *renderer);
enum wlr_drm_output_state { enum wlr_drm_output_state {
@ -62,6 +61,6 @@ void wlr_drm_output_begin(struct wlr_drm_output *out);
void wlr_drm_output_end(struct wlr_drm_output *out); void wlr_drm_output_end(struct wlr_drm_output *out);
void wlr_drm_scan_connectors(struct wlr_drm_backend *backend); void wlr_drm_scan_connectors(struct wlr_drm_backend *backend);
void wlr_drm_event(int fd); int wlr_drm_event(int fd, uint32_t mask, void *data);
#endif #endif

@ -4,15 +4,18 @@
#include <libudev.h> #include <libudev.h>
#include <wlr/session.h> #include <wlr/session.h>
#include <wayland-server.h>
struct wlr_udev { struct wlr_udev {
struct udev *udev; struct udev *udev;
struct udev_monitor *mon; struct udev_monitor *mon;
char *drm_path; char *drm_path;
struct wl_event_source *event;
}; };
struct wlr_drm_backend; struct wlr_drm_backend;
bool wlr_udev_init(struct wlr_drm_backend *backend); bool wlr_udev_init(struct wl_display *display, struct wlr_udev *udev);
void wlr_udev_free(struct wlr_udev *udev); void wlr_udev_free(struct wlr_udev *udev);
int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session); int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session);

@ -7,12 +7,11 @@
struct wlr_drm_backend; struct wlr_drm_backend;
struct wlr_drm_output; struct wlr_drm_output;
struct wlr_drm_backend *wlr_drm_backend_init(struct wlr_session *session, struct wlr_drm_backend *wlr_drm_backend_init(struct wl_display *display,
struct wl_listener *add, struct wl_listener *rem, struct wl_listener *render); 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); void wlr_drm_backend_free(struct wlr_drm_backend *backend);
struct wl_event_loop *wlr_drm_backend_get_event_loop(struct wlr_drm_backend *backend);
bool wlr_drm_output_modeset(struct wlr_drm_output *out, const char *str); bool wlr_drm_output_modeset(struct wlr_drm_output *out, const char *str);
void wlr_drm_output_begin(struct wlr_drm_output *out); void wlr_drm_output_begin(struct wlr_drm_output *out);
void wlr_drm_output_end(struct wlr_drm_output *out); void wlr_drm_output_end(struct wlr_drm_output *out);

Loading…
Cancel
Save