Merge pull request #144 from ascent12/drm-dp-mst

DisplayPort MST
This commit is contained in:
Drew DeVault 2017-09-27 07:58:01 -04:00 committed by GitHub
commit d7d73cd298
5 changed files with 128 additions and 41 deletions

View File

@ -26,6 +26,9 @@ static void wlr_drm_backend_destroy(struct wlr_backend *_backend) {
return; return;
} }
struct wlr_drm_backend *backend = (struct wlr_drm_backend *)_backend; struct wlr_drm_backend *backend = (struct wlr_drm_backend *)_backend;
wlr_drm_restore_outputs(backend);
for (size_t i = 0; backend->outputs && i < backend->outputs->length; ++i) { for (size_t i = 0; backend->outputs && i < backend->outputs->length; ++i) {
struct wlr_drm_output *output = backend->outputs->items[i]; struct wlr_drm_output *output = backend->outputs->items[i];
wlr_output_destroy(&output->output); wlr_output_destroy(&output->output);

View File

@ -43,16 +43,24 @@ static bool atomic_end(int drm_fd, struct atomic *atom) {
} }
static bool atomic_commit(int drm_fd, struct atomic *atom, static bool atomic_commit(int drm_fd, struct atomic *atom,
struct wlr_drm_output *output, uint32_t flag) { struct wlr_drm_output *output, uint32_t flag, bool modeset) {
if (atom->failed) { if (atom->failed) {
return false; return false;
} }
uint32_t flags = DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK | flag; uint32_t flags = DRM_MODE_PAGE_FLIP_EVENT | flag;
int ret = drmModeAtomicCommit(drm_fd, atom->req, flags, output); int ret = drmModeAtomicCommit(drm_fd, atom->req, flags, output);
if (ret) { if (ret) {
wlr_log_errno(L_ERROR, "Atomic commit failed"); wlr_log_errno(L_ERROR, "%s: Atomic commit failed (%s)",
output->output.name, modeset ? "modeset" : "pageflip");
// Try to commit without new changes
drmModeAtomicSetCursor(atom->req, atom->cursor);
if (drmModeAtomicCommit(drm_fd, atom->req, flags, output)) {
wlr_log_errno(L_ERROR, "%s: Atomic commit failed (%s)",
output->output.name, modeset ? "modeset" : "pageflip");
}
} }
drmModeAtomicSetCursor(atom->req, 0); drmModeAtomicSetCursor(atom->req, 0);
@ -109,8 +117,9 @@ static bool atomic_crtc_pageflip(struct wlr_drm_backend *backend,
atomic_add(&atom, crtc->id, crtc->props.mode_id, crtc->mode_id); atomic_add(&atom, crtc->id, crtc->props.mode_id, crtc->mode_id);
atomic_add(&atom, crtc->id, crtc->props.active, 1); atomic_add(&atom, crtc->id, crtc->props.active, 1);
set_plane_props(&atom, crtc->primary, crtc->id, fb_id, true); set_plane_props(&atom, crtc->primary, crtc->id, fb_id, true);
return atomic_commit(backend->fd, &atom, return atomic_commit(backend->fd, &atom, output,
output, mode ? DRM_MODE_ATOMIC_ALLOW_MODESET : 0); mode ? DRM_MODE_ATOMIC_ALLOW_MODESET : DRM_MODE_ATOMIC_NONBLOCK,
mode);
} }
static void atomic_conn_enable(struct wlr_drm_backend *backend, static void atomic_conn_enable(struct wlr_drm_backend *backend,

View File

@ -9,11 +9,17 @@ static bool legacy_crtc_pageflip(struct wlr_drm_backend *backend,
struct wlr_drm_output *output, struct wlr_drm_crtc *crtc, struct wlr_drm_output *output, struct wlr_drm_crtc *crtc,
uint32_t fb_id, drmModeModeInfo *mode) { uint32_t fb_id, drmModeModeInfo *mode) {
if (mode) { if (mode) {
drmModeSetCrtc(backend->fd, crtc->id, fb_id, 0, 0, if (drmModeSetCrtc(backend->fd, crtc->id, fb_id, 0, 0,
&output->connector, 1, mode); &output->connector, 1, mode)) {
wlr_log_errno(L_ERROR, "%s: Failed to set CRTC", output->output.name);
return false;
}
} }
drmModePageFlip(backend->fd, crtc->id, fb_id, DRM_MODE_PAGE_FLIP_EVENT, output); if (drmModePageFlip(backend->fd, crtc->id, fb_id, DRM_MODE_PAGE_FLIP_EVENT, output)) {
wlr_log_errno(L_ERROR, "%s: Failed to page flip", output->output.name);
return false;
}
return true; return true;
} }

View File

@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include <inttypes.h> #include <inttypes.h>
#include <errno.h> #include <errno.h>
#include <time.h>
#include <xf86drm.h> #include <xf86drm.h>
#include <xf86drmMode.h> #include <xf86drmMode.h>
#include <drm_mode.h> #include <drm_mode.h>
@ -31,7 +32,10 @@ bool wlr_drm_check_features(struct wlr_drm_backend *backend) {
return false; return false;
} }
if (drmSetClientCap(backend->fd, DRM_CLIENT_CAP_ATOMIC, 1)) { if (getenv("WLR_DRM_NO_ATOMIC")) {
wlr_log(L_DEBUG, "WLR_DRM_NO_ATOMIC set, forcing legacy DRM interface");
backend->iface = &legacy_iface;
} else if (drmSetClientCap(backend->fd, DRM_CLIENT_CAP_ATOMIC, 1)) {
wlr_log(L_DEBUG, "Atomic modesetting unsupported, using legacy DRM interface"); wlr_log(L_DEBUG, "Atomic modesetting unsupported, using legacy DRM interface");
backend->iface = &legacy_iface; backend->iface = &legacy_iface;
} else { } else {
@ -297,8 +301,14 @@ static void wlr_drm_output_swap_buffers(struct wlr_output *_output) {
wlr_drm_plane_swap_buffers(renderer, plane); wlr_drm_plane_swap_buffers(renderer, plane);
backend->iface->crtc_pageflip(backend, output, crtc, get_fb_for_bo(plane->back), NULL); uint32_t fb_id = get_fb_for_bo(plane->back);
output->pageflip_pending = true;
if (backend->iface->crtc_pageflip(backend, output, crtc, fb_id, NULL)) {
output->pageflip_pending = true;
} else {
wl_event_source_timer_update(output->retry_pageflip,
1000.0f / output->output.current_mode->refresh);
}
} }
static void wlr_drm_output_set_gamma(struct wlr_output *_output, static void wlr_drm_output_set_gamma(struct wlr_output *_output,
@ -344,8 +354,12 @@ void wlr_drm_output_start_renderer(struct wlr_drm_output *output) {
struct wlr_drm_output_mode *_mode = struct wlr_drm_output_mode *_mode =
(struct wlr_drm_output_mode *)output->output.current_mode; (struct wlr_drm_output_mode *)output->output.current_mode;
drmModeModeInfo *mode = &_mode->mode; drmModeModeInfo *mode = &_mode->mode;
backend->iface->crtc_pageflip(backend, output, crtc, get_fb_for_bo(bo), mode); if (backend->iface->crtc_pageflip(backend, output, crtc, get_fb_for_bo(bo), mode)) {
output->pageflip_pending = true; output->pageflip_pending = true;
} else {
wl_event_source_timer_update(output->retry_pageflip,
1000.0f / output->output.current_mode->refresh);
}
} }
static void wlr_drm_output_enable(struct wlr_output *_output, bool enable) { static void wlr_drm_output_enable(struct wlr_output *_output, bool enable) {
@ -553,7 +567,7 @@ error_enc:
error_conn: error_conn:
drmModeFreeConnector(conn); drmModeFreeConnector(conn);
error_output: error_output:
wlr_drm_output_cleanup(output, false); wlr_drm_output_cleanup(output);
return false; return false;
} }
@ -700,7 +714,8 @@ static bool wlr_drm_output_move_cursor(struct wlr_output *_output,
static void wlr_drm_output_destroy(struct wlr_output *_output) { static void wlr_drm_output_destroy(struct wlr_output *_output) {
struct wlr_drm_output *output = (struct wlr_drm_output *)_output; struct wlr_drm_output *output = (struct wlr_drm_output *)_output;
wlr_drm_output_cleanup(output, true); wlr_drm_output_cleanup(output);
wl_event_source_remove(output->retry_pageflip);
free(output); free(output);
} }
@ -717,6 +732,13 @@ static struct wlr_output_impl output_impl = {
.get_gamma_size = wlr_drm_output_get_gamma_size, .get_gamma_size = wlr_drm_output_get_gamma_size,
}; };
static int retry_pageflip(void *data) {
struct wlr_drm_output *output = data;
wlr_log(L_INFO, "%s: Retrying pageflip", output->output.name);
wlr_drm_output_start_renderer(output);
return 0;
}
static int find_id(const void *item, const void *cmp_to) { static int find_id(const void *item, const void *cmp_to) {
const struct wlr_drm_output *output = item; const struct wlr_drm_output *output = item;
const uint32_t *id = cmp_to; const uint32_t *id = cmp_to;
@ -748,6 +770,11 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) {
return; return;
} }
size_t seen_len = backend->outputs->length;
// +1 so it can never be 0
bool seen[seen_len + 1];
memset(seen, 0, sizeof(seen));
for (int i = 0; i < res->count_connectors; ++i) { for (int i = 0; i < res->count_connectors; ++i) {
drmModeConnector *conn = drmModeGetConnector(backend->fd, drmModeConnector *conn = drmModeGetConnector(backend->fd,
res->connectors[i]); res->connectors[i]);
@ -768,6 +795,11 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) {
} }
wlr_output_init(&output->output, &output_impl); wlr_output_init(&output->output, &output_impl);
struct wl_event_loop *ev = wl_display_get_event_loop(backend->display);
output->retry_pageflip = wl_event_loop_add_timer(ev, retry_pageflip,
output);
output->renderer = &backend->renderer; output->renderer = &backend->renderer;
output->state = WLR_DRM_OUTPUT_DISCONNECTED; output->state = WLR_DRM_OUTPUT_DISCONNECTED;
output->connector = conn->connector_id; output->connector = conn->connector_id;
@ -791,13 +823,14 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) {
size_t edid_len = 0; size_t edid_len = 0;
uint8_t *edid = wlr_drm_get_prop_blob(backend->fd, uint8_t *edid = wlr_drm_get_prop_blob(backend->fd,
output->connector, output->props.edid, &edid_len); output->connector, output->props.edid, &edid_len);
parse_edid(&output->output, edid_len, edid); parse_edid(&output->output, edid_len, edid);
free(edid); free(edid);
if (list_add(backend->outputs, output) == -1) { if (list_add(backend->outputs, output) == -1) {
wlr_log_errno(L_ERROR, "Allocation failed"); wlr_log_errno(L_ERROR, "Allocation failed");
drmModeFreeConnector(conn); drmModeFreeConnector(conn);
wl_event_source_remove(output->retry_pageflip);
free(output); free(output);
continue; continue;
} }
@ -805,6 +838,7 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) {
wlr_log(L_INFO, "Found display '%s'", output->output.name); wlr_log(L_INFO, "Found display '%s'", output->output.name);
} else { } else {
output = backend->outputs->items[index]; output = backend->outputs->items[index];
seen[index] = true;
} }
if (output->state == WLR_DRM_OUTPUT_DISCONNECTED && if (output->state == WLR_DRM_OUTPUT_DISCONNECTED &&
@ -815,7 +849,7 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) {
for (int i = 0; i < conn->count_modes; ++i) { for (int i = 0; i < conn->count_modes; ++i) {
struct wlr_drm_output_mode *mode = calloc(1, struct wlr_drm_output_mode *mode = calloc(1,
sizeof(struct wlr_drm_output_mode)); sizeof(struct wlr_drm_output_mode));
if (!mode) { if (!mode) {
wlr_log_errno(L_ERROR, "Allocation failed"); wlr_log_errno(L_ERROR, "Allocation failed");
continue; continue;
@ -843,13 +877,30 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) {
conn->connection != DRM_MODE_CONNECTED) { conn->connection != DRM_MODE_CONNECTED) {
wlr_log(L_INFO, "'%s' disconnected", output->output.name); wlr_log(L_INFO, "'%s' disconnected", output->output.name);
wlr_drm_output_cleanup(output, false); wlr_drm_output_cleanup(output);
} }
drmModeFreeConnector(conn); drmModeFreeConnector(conn);
} }
drmModeFreeResources(res); drmModeFreeResources(res);
for (size_t i = seen_len; i-- > 0;) {
if (seen[i]) {
continue;
}
struct wlr_drm_output *output = backend->outputs->items[i];
wlr_log(L_INFO, "'%s' disappeared", output->output.name);
wlr_drm_output_cleanup(output);
drmModeFreeCrtc(output->old_crtc);
wl_event_source_remove(output->retry_pageflip);
free(output);
list_del(backend->outputs, i);
}
} }
static void page_flip_handler(int fd, unsigned seq, static void page_flip_handler(int fd, unsigned seq,
@ -884,23 +935,46 @@ int wlr_drm_event(int fd, uint32_t mask, void *data) {
return 1; return 1;
} }
static void restore_output(struct wlr_drm_output *output, int fd) { void wlr_drm_restore_outputs(struct wlr_drm_backend *drm) {
// Wait for any pending pageflips to finish uint64_t to_close = (1 << drm->outputs->length) - 1;
while (output->pageflip_pending) {
wlr_drm_event(fd, 0, NULL); for (size_t i = 0; i < drm->outputs->length; ++i) {
struct wlr_drm_output *output = drm->outputs->items[i];
if (output->state == WLR_DRM_OUTPUT_CONNECTED) {
output->state = WLR_DRM_OUTPUT_CLEANUP;
}
} }
drmModeCrtc *crtc = output->old_crtc; time_t timeout = time(NULL) + 5;
if (!crtc) {
return; while (to_close && time(NULL) < timeout) {
wlr_drm_event(drm->fd, 0, NULL);
for (size_t i = 0; i < drm->outputs->length; ++i) {
struct wlr_drm_output *output = drm->outputs->items[i];
if (output->state != WLR_DRM_OUTPUT_CLEANUP || !output->pageflip_pending) {
to_close &= ~(1 << i);
}
}
} }
drmModeSetCrtc(fd, crtc->crtc_id, crtc->buffer_id, crtc->x, crtc->y, if (to_close) {
&output->connector, 1, &crtc->mode); wlr_log(L_ERROR, "Timed out stopping output renderers");
drmModeFreeCrtc(crtc); }
for (size_t i = 0; i < drm->outputs->length; ++i) {
struct wlr_drm_output *output = drm->outputs->items[i];
drmModeCrtc *crtc = output->old_crtc;
if (!crtc) {
continue;
}
drmModeSetCrtc(drm->fd, crtc->crtc_id, crtc->buffer_id, crtc->x, crtc->y,
&output->connector, 1, &crtc->mode);
drmModeFreeCrtc(crtc);
}
} }
void wlr_drm_output_cleanup(struct wlr_drm_output *output, bool restore) { void wlr_drm_output_cleanup(struct wlr_drm_output *output) {
if (!output) { if (!output) {
return; return;
} }
@ -911,12 +985,7 @@ void wlr_drm_output_cleanup(struct wlr_drm_output *output, bool restore) {
switch (output->state) { switch (output->state) {
case WLR_DRM_OUTPUT_CONNECTED: case WLR_DRM_OUTPUT_CONNECTED:
output->state = WLR_DRM_OUTPUT_DISCONNECTED; case WLR_DRM_OUTPUT_CLEANUP:;
if (restore) {
restore_output(output, renderer->fd);
restore = false;
}
struct wlr_drm_crtc *crtc = output->crtc; struct wlr_drm_crtc *crtc = output->crtc;
for (int i = 0; i < 3; ++i) { for (int i = 0; i < 3; ++i) {
wlr_drm_plane_renderer_free(renderer, crtc->planes[i]); wlr_drm_plane_renderer_free(renderer, crtc->planes[i]);
@ -930,10 +999,6 @@ void wlr_drm_output_cleanup(struct wlr_drm_output *output, bool restore) {
output->possible_crtc = 0; output->possible_crtc = 0;
/* Fallthrough */ /* Fallthrough */
case WLR_DRM_OUTPUT_NEEDS_MODESET: case WLR_DRM_OUTPUT_NEEDS_MODESET:
output->state = WLR_DRM_OUTPUT_DISCONNECTED;
if (restore) {
restore_output(output, renderer->fd);
}
wlr_log(L_INFO, "Emmiting destruction signal for '%s'", wlr_log(L_INFO, "Emmiting destruction signal for '%s'",
output->output.name); output->output.name);
wl_signal_emit(&backend->backend.events.output_remove, &output->output); wl_signal_emit(&backend->backend.events.output_remove, &output->output);
@ -941,4 +1006,6 @@ void wlr_drm_output_cleanup(struct wlr_drm_output *output, bool restore) {
case WLR_DRM_OUTPUT_DISCONNECTED: case WLR_DRM_OUTPUT_DISCONNECTED:
break; break;
} }
output->state = WLR_DRM_OUTPUT_DISCONNECTED;
} }

View File

@ -127,6 +127,7 @@ struct wlr_drm_backend {
enum wlr_drm_output_state { enum wlr_drm_output_state {
WLR_DRM_OUTPUT_DISCONNECTED, WLR_DRM_OUTPUT_DISCONNECTED,
WLR_DRM_OUTPUT_NEEDS_MODESET, WLR_DRM_OUTPUT_NEEDS_MODESET,
WLR_DRM_OUTPUT_CLEANUP,
WLR_DRM_OUTPUT_CONNECTED, WLR_DRM_OUTPUT_CONNECTED,
}; };
@ -154,6 +155,7 @@ struct wlr_drm_output {
struct wlr_drm_renderer *renderer; struct wlr_drm_renderer *renderer;
bool pageflip_pending; bool pageflip_pending;
struct wl_event_source *retry_pageflip;
}; };
// Used to provide atomic or legacy DRM functions // Used to provide atomic or legacy DRM functions
@ -176,8 +178,8 @@ struct wlr_drm_interface {
bool wlr_drm_check_features(struct wlr_drm_backend *drm); bool wlr_drm_check_features(struct wlr_drm_backend *drm);
bool wlr_drm_resources_init(struct wlr_drm_backend *drm); bool wlr_drm_resources_init(struct wlr_drm_backend *drm);
void wlr_drm_resources_free(struct wlr_drm_backend *drm); void wlr_drm_resources_free(struct wlr_drm_backend *drm);
void wlr_drm_output_cleanup(struct wlr_drm_output *output, bool restore); void wlr_drm_restore_outputs(struct wlr_drm_backend *drm);
void wlr_drm_output_cleanup(struct wlr_drm_output *output);
void wlr_drm_scan_connectors(struct wlr_drm_backend *state); void wlr_drm_scan_connectors(struct wlr_drm_backend *state);
int wlr_drm_event(int fd, uint32_t mask, void *data); int wlr_drm_event(int fd, uint32_t mask, void *data);