Merge remote-tracking branch 'origin/master' into rootston

This commit is contained in:
Drew DeVault 2017-09-27 20:25:34 -04:00
commit 7cf4ee128e
6 changed files with 130 additions and 43 deletions

View File

@ -26,6 +26,9 @@ static void wlr_drm_backend_destroy(struct wlr_backend *_backend) {
return;
}
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) {
struct wlr_drm_output *output = backend->outputs->items[i];
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,
struct wlr_drm_output *output, uint32_t flag) {
struct wlr_drm_output *output, uint32_t flag, bool modeset) {
if (atom->failed) {
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);
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);
@ -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.active, 1);
set_plane_props(&atom, crtc->primary, crtc->id, fb_id, true);
return atomic_commit(backend->fd, &atom,
output, mode ? DRM_MODE_ATOMIC_ALLOW_MODESET : 0);
return atomic_commit(backend->fd, &atom, output,
mode ? DRM_MODE_ATOMIC_ALLOW_MODESET : DRM_MODE_ATOMIC_NONBLOCK,
mode);
}
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,
uint32_t fb_id, drmModeModeInfo *mode) {
if (mode) {
drmModeSetCrtc(backend->fd, crtc->id, fb_id, 0, 0,
&output->connector, 1, mode);
if (drmModeSetCrtc(backend->fd, crtc->id, fb_id, 0, 0,
&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;
}

View File

@ -4,6 +4,7 @@
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include <time.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm_mode.h>
@ -31,7 +32,10 @@ bool wlr_drm_check_features(struct wlr_drm_backend *backend) {
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");
backend->iface = &legacy_iface;
} else {
@ -297,8 +301,14 @@ static void wlr_drm_output_swap_buffers(struct wlr_output *_output) {
wlr_drm_plane_swap_buffers(renderer, plane);
backend->iface->crtc_pageflip(backend, output, crtc, get_fb_for_bo(plane->back), NULL);
output->pageflip_pending = true;
uint32_t fb_id = get_fb_for_bo(plane->back);
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,
@ -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 *)output->output.current_mode;
drmModeModeInfo *mode = &_mode->mode;
backend->iface->crtc_pageflip(backend, output, crtc, get_fb_for_bo(bo), mode);
output->pageflip_pending = true;
if (backend->iface->crtc_pageflip(backend, output, crtc, get_fb_for_bo(bo), mode)) {
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) {
@ -553,7 +567,7 @@ error_enc:
error_conn:
drmModeFreeConnector(conn);
error_output:
wlr_drm_output_cleanup(output, false);
wlr_drm_output_cleanup(output);
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) {
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);
}
@ -717,6 +732,13 @@ static struct wlr_output_impl output_impl = {
.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) {
const struct wlr_drm_output *output = item;
const uint32_t *id = cmp_to;
@ -748,6 +770,11 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) {
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) {
drmModeConnector *conn = drmModeGetConnector(backend->fd,
res->connectors[i]);
@ -768,6 +795,11 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) {
}
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->state = WLR_DRM_OUTPUT_DISCONNECTED;
output->connector = conn->connector_id;
@ -791,13 +823,14 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) {
size_t edid_len = 0;
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);
free(edid);
if (list_add(backend->outputs, output) == -1) {
wlr_log_errno(L_ERROR, "Allocation failed");
drmModeFreeConnector(conn);
wl_event_source_remove(output->retry_pageflip);
free(output);
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);
} else {
output = backend->outputs->items[index];
seen[index] = true;
}
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) {
struct wlr_drm_output_mode *mode = calloc(1,
sizeof(struct wlr_drm_output_mode));
sizeof(struct wlr_drm_output_mode));
if (!mode) {
wlr_log_errno(L_ERROR, "Allocation failed");
continue;
@ -843,13 +877,30 @@ void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) {
conn->connection != DRM_MODE_CONNECTED) {
wlr_log(L_INFO, "'%s' disconnected", output->output.name);
wlr_drm_output_cleanup(output, false);
wlr_drm_output_cleanup(output);
}
drmModeFreeConnector(conn);
}
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,
@ -884,23 +935,46 @@ int wlr_drm_event(int fd, uint32_t mask, void *data) {
return 1;
}
static void restore_output(struct wlr_drm_output *output, int fd) {
// Wait for any pending pageflips to finish
while (output->pageflip_pending) {
wlr_drm_event(fd, 0, NULL);
void wlr_drm_restore_outputs(struct wlr_drm_backend *drm) {
uint64_t to_close = (1 << drm->outputs->length) - 1;
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;
if (!crtc) {
return;
time_t timeout = time(NULL) + 5;
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,
&output->connector, 1, &crtc->mode);
drmModeFreeCrtc(crtc);
if (to_close) {
wlr_log(L_ERROR, "Timed out stopping output renderers");
}
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) {
return;
}
@ -911,12 +985,7 @@ void wlr_drm_output_cleanup(struct wlr_drm_output *output, bool restore) {
switch (output->state) {
case WLR_DRM_OUTPUT_CONNECTED:
output->state = WLR_DRM_OUTPUT_DISCONNECTED;
if (restore) {
restore_output(output, renderer->fd);
restore = false;
}
case WLR_DRM_OUTPUT_CLEANUP:;
struct wlr_drm_crtc *crtc = output->crtc;
for (int i = 0; i < 3; ++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;
/* Fallthrough */
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'",
output->output.name);
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:
break;
}
output->state = WLR_DRM_OUTPUT_DISCONNECTED;
}

View File

@ -127,6 +127,7 @@ struct wlr_drm_backend {
enum wlr_drm_output_state {
WLR_DRM_OUTPUT_DISCONNECTED,
WLR_DRM_OUTPUT_NEEDS_MODESET,
WLR_DRM_OUTPUT_CLEANUP,
WLR_DRM_OUTPUT_CONNECTED,
};
@ -154,6 +155,7 @@ struct wlr_drm_output {
struct wlr_drm_renderer *renderer;
bool pageflip_pending;
struct wl_event_source *retry_pageflip;
};
// 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_resources_init(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);
int wlr_drm_event(int fd, uint32_t mask, void *data);

View File

@ -175,13 +175,14 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform,
return true;
error:
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglTerminate(egl->display);
eglReleaseThread();
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
return false;
}
void wlr_egl_free(struct wlr_egl *egl) {
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (egl->wl_display && egl->eglUnbindWaylandDisplayWL) {
egl->eglUnbindWaylandDisplayWL(egl->display, egl->wl_display);
}
@ -189,7 +190,6 @@ void wlr_egl_free(struct wlr_egl *egl) {
eglDestroyContext(egl->display, egl->context);
eglTerminate(egl->display);
eglReleaseThread();
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
}
bool wlr_egl_bind_display(struct wlr_egl *egl, struct wl_display *local_display) {