backend/drm: introduce wlr_drm_lease

Closes: https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/3183
This commit is contained in:
Simon Ser 2021-11-03 14:03:59 +01:00 committed by Simon Zeni
parent a37f538ca0
commit 86f5ecf468
5 changed files with 80 additions and 56 deletions

View File

@ -1554,17 +1554,13 @@ int wlr_drm_backend_get_non_master_fd(struct wlr_backend *backend) {
return fd; return fd;
} }
/* TODO: make the function return a `wlr_drm_lease` to provide a destroy event struct wlr_drm_lease *wlr_drm_create_lease(struct wlr_output **outputs,
* that can be fired when the kernel notifies us through uevent that the lease size_t n_outputs, int *lease_fd_ptr) {
* has been destroyed
*/
int wlr_drm_create_lease(struct wlr_output **outputs, size_t n_outputs,
uint32_t *lessee_id) {
assert(outputs); assert(outputs);
if (n_outputs == 0) { if (n_outputs == 0) {
wlr_log(WLR_ERROR, "Can't lease 0 outputs"); wlr_log(WLR_ERROR, "Can't lease 0 outputs");
return -1; return NULL;
} }
struct wlr_drm_backend *drm = struct wlr_drm_backend *drm =
@ -1575,11 +1571,11 @@ int wlr_drm_create_lease(struct wlr_output **outputs, size_t n_outputs,
for (size_t i = 0; i < n_outputs; ++i) { for (size_t i = 0; i < n_outputs; ++i) {
struct wlr_drm_connector *conn = struct wlr_drm_connector *conn =
get_drm_connector_from_output(outputs[i]); get_drm_connector_from_output(outputs[i]);
assert(conn->lessee_id == 0); assert(conn->lease == NULL);
if (conn->backend != drm) { if (conn->backend != drm) {
wlr_log(WLR_ERROR, "Can't lease output from different backends"); wlr_log(WLR_ERROR, "Can't lease output from different backends");
return -1; return NULL;
} }
objects[n_objects++] = conn->id; objects[n_objects++] = conn->id;
@ -1587,7 +1583,7 @@ int wlr_drm_create_lease(struct wlr_output **outputs, size_t n_outputs,
if (!conn->crtc) { if (!conn->crtc) {
wlr_log(WLR_ERROR, "Connector has no CRTC"); wlr_log(WLR_ERROR, "Connector has no CRTC");
return -1; return NULL;
} }
objects[n_objects++] = conn->crtc->id; objects[n_objects++] = conn->crtc->id;
@ -1604,50 +1600,63 @@ int wlr_drm_create_lease(struct wlr_output **outputs, size_t n_outputs,
assert(n_objects != 0); assert(n_objects != 0);
wlr_log(WLR_DEBUG, "Issuing DRM lease with the %d objects", n_objects); struct wlr_drm_lease *lease = calloc(1, sizeof(*lease));
int lease_fd = drmModeCreateLease(drm->fd, objects, n_objects, 0, if (lease == NULL) {
lessee_id); return NULL;
if (lease_fd < 0) {
return lease_fd;
} }
wlr_log(WLR_DEBUG, "Issued DRM lease %"PRIu32, *lessee_id); lease->backend = drm;
wl_signal_init(&lease->events.destroy);
wlr_log(WLR_DEBUG, "Issuing DRM lease with %d objects", n_objects);
int lease_fd = drmModeCreateLease(drm->fd, objects, n_objects, 0,
&lease->lessee_id);
if (lease_fd < 0) {
free(lease);
return NULL;
}
*lease_fd_ptr = lease_fd;
wlr_log(WLR_DEBUG, "Issued DRM lease %"PRIu32, lease->lessee_id);
for (size_t i = 0; i < n_outputs; ++i) { for (size_t i = 0; i < n_outputs; ++i) {
struct wlr_drm_connector *conn = struct wlr_drm_connector *conn =
get_drm_connector_from_output(outputs[i]); get_drm_connector_from_output(outputs[i]);
conn->lessee_id = *lessee_id; conn->lease = lease;
conn->crtc->lessee_id = *lessee_id; conn->crtc->lease = lease;
} }
return lease_fd; return lease;
} }
bool wlr_drm_backend_terminate_lease(struct wlr_backend *backend, void wlr_drm_lease_terminate(struct wlr_drm_lease *lease) {
uint32_t lessee_id) { struct wlr_drm_backend *drm = lease->backend;
wlr_log(WLR_DEBUG, "Terminating DRM lease %d", lessee_id);
assert(backend); wlr_log(WLR_DEBUG, "Terminating DRM lease %d", lease->lessee_id);
struct wlr_drm_backend *drm = get_drm_backend_from_backend(backend); int ret = drmModeRevokeLease(drm->fd, lease->lessee_id);
if (ret < 0) {
int r = drmModeRevokeLease(drm->fd, lessee_id); wlr_log_errno(WLR_ERROR, "Failed to terminate lease");
if (r < 0) {
wlr_log_errno(WLR_DEBUG, "Failed to terminate lease");
} }
drm_lease_destroy(lease);
}
void drm_lease_destroy(struct wlr_drm_lease *lease) {
struct wlr_drm_backend *drm = lease->backend;
wlr_signal_emit_safe(&lease->events.destroy, NULL);
struct wlr_drm_connector *conn; struct wlr_drm_connector *conn;
wl_list_for_each(conn, &drm->outputs, link) { wl_list_for_each(conn, &drm->outputs, link) {
if (conn->lessee_id == lessee_id) { if (conn->lease == lease) {
conn->lessee_id = 0; conn->lease = NULL;
/* Will be re-initialized in scan_drm_connectors */
} }
} }
for (size_t i = 0; i < drm->num_crtcs; ++i) { for (size_t i = 0; i < drm->num_crtcs; ++i) {
if (drm->crtcs[i].lessee_id == lessee_id) { if (drm->crtcs[i].lease == lease) {
drm->crtcs[i].lessee_id = 0; drm->crtcs[i].lease = NULL;
} }
} }
return r >= 0; free(lease);
} }

View File

@ -36,7 +36,7 @@ struct wlr_drm_plane {
struct wlr_drm_crtc { struct wlr_drm_crtc {
uint32_t id; uint32_t id;
uint32_t lessee_id; struct wlr_drm_lease *lease;
// Atomic modesetting only // Atomic modesetting only
uint32_t mode_id; uint32_t mode_id;
@ -118,7 +118,7 @@ struct wlr_drm_connector {
enum wlr_drm_connector_status status; enum wlr_drm_connector_status status;
bool desired_enabled; bool desired_enabled;
uint32_t id; uint32_t id;
uint32_t lessee_id; struct wlr_drm_lease *lease;
struct wlr_drm_crtc *crtc; struct wlr_drm_crtc *crtc;
uint32_t possible_crtcs; uint32_t possible_crtcs;
@ -157,6 +157,7 @@ bool drm_connector_is_cursor_visible(struct wlr_drm_connector *conn);
bool drm_connector_supports_vrr(struct wlr_drm_connector *conn); bool drm_connector_supports_vrr(struct wlr_drm_connector *conn);
size_t drm_crtc_get_gamma_lut_size(struct wlr_drm_backend *drm, size_t drm_crtc_get_gamma_lut_size(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc); struct wlr_drm_crtc *crtc);
void drm_lease_destroy(struct wlr_drm_lease *lease);
struct wlr_drm_fb *plane_get_next_fb(struct wlr_drm_plane *plane); struct wlr_drm_fb *plane_get_next_fb(struct wlr_drm_plane *plane);

View File

@ -14,6 +14,20 @@
#include <wlr/backend/session.h> #include <wlr/backend/session.h>
#include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output.h>
struct wlr_drm_backend;
struct wlr_drm_lease {
int fd;
uint32_t lessee_id;
struct wlr_drm_backend *backend;
struct {
struct wl_signal destroy;
} events;
void *data;
};
/** /**
* Creates a DRM backend using the specified GPU file descriptor (typically from * Creates a DRM backend using the specified GPU file descriptor (typically from
* a device node in /dev/dri). * a device node in /dev/dri).
@ -41,18 +55,20 @@ uint32_t wlr_drm_connector_get_id(struct wlr_output *output);
int wlr_drm_backend_get_non_master_fd(struct wlr_backend *backend); int wlr_drm_backend_get_non_master_fd(struct wlr_backend *backend);
/** /**
* Leases a given output to the caller. The output must be from the associated * Leases the given outputs to the caller. The outputs must be from the
* DRM backend. * associated DRM backend.
* Returns a valid opened DRM FD or -1 on error. *
* Returns NULL on error.
*/ */
int wlr_drm_create_lease(struct wlr_output **outputs, size_t n_outputs, struct wlr_drm_lease *wlr_drm_create_lease(struct wlr_output **outputs,
uint32_t *lessee_id); size_t n_outputs, int *lease_fd);
/** /**
* Terminates a given lease. The output will be owned again by the backend * Terminates and destroys a given lease.
*
* The outputs will be owned again by the backend.
*/ */
bool wlr_drm_backend_terminate_lease(struct wlr_backend *backend, void wlr_drm_lease_terminate(struct wlr_drm_lease *lease);
uint32_t lessee_id);
/** /**
* Add mode to the list of available modes * Add mode to the list of available modes

View File

@ -81,14 +81,13 @@ struct wlr_drm_lease_request_v1 {
struct wlr_drm_lease_v1 { struct wlr_drm_lease_v1 {
struct wl_resource *resource; struct wl_resource *resource;
struct wlr_drm_lease *drm_lease;
struct wlr_drm_lease_device_v1 *device; struct wlr_drm_lease_device_v1 *device;
struct wlr_drm_lease_connector_v1 **connectors; struct wlr_drm_lease_connector_v1 **connectors;
size_t n_connectors; size_t n_connectors;
uint32_t lessee_id;
struct wl_list link; // wlr_drm_lease_device_v1::leases struct wl_list link; // wlr_drm_lease_device_v1::leases
void *data; void *data;

View File

@ -52,13 +52,11 @@ static void drm_lease_v1_destroy(struct wlr_drm_lease_v1 *lease) {
return; return;
} }
wlr_log(WLR_DEBUG, "Destroying lease %"PRIu32, lease->lessee_id); wlr_log(WLR_DEBUG, "Destroying lease %"PRIu32, lease->drm_lease->lessee_id);
wp_drm_lease_v1_send_finished(lease->resource); wp_drm_lease_v1_send_finished(lease->resource);
struct wlr_drm_lease_device_v1 *device = lease->device; wlr_drm_lease_terminate(lease->drm_lease);
wlr_drm_backend_terminate_lease(device->backend, lease->lessee_id);
lease->lessee_id = 0;
for (size_t i = 0; i < lease->n_connectors; ++i) { for (size_t i = 0; i < lease->n_connectors; ++i) {
lease->connectors[i]->active_lease = NULL; lease->connectors[i]->active_lease = NULL;
@ -171,10 +169,10 @@ struct wlr_drm_lease_v1 *wlr_drm_lease_request_v1_grant(
outputs[i] = request->connectors[i]->output; outputs[i] = request->connectors[i]->output;
} }
int fd = wlr_drm_create_lease(outputs, request->n_connectors, int fd;
&lease->lessee_id); lease->drm_lease = wlr_drm_create_lease(outputs, request->n_connectors, &fd);
if (fd < 0) { if (!lease->drm_lease) {
wlr_log_errno(WLR_ERROR, "drm_create_lease failed"); wlr_log(WLR_ERROR, "wlr_drm_create_lease failed");
wp_drm_lease_v1_send_finished(lease->resource); wp_drm_lease_v1_send_finished(lease->resource);
return NULL; return NULL;
} }
@ -183,6 +181,7 @@ struct wlr_drm_lease_v1 *wlr_drm_lease_request_v1_grant(
sizeof(struct wlr_drm_lease_connector_v1 *)); sizeof(struct wlr_drm_lease_connector_v1 *));
if (!lease->connectors) { if (!lease->connectors) {
wlr_log(WLR_ERROR, "Failed to allocate lease connectors list"); wlr_log(WLR_ERROR, "Failed to allocate lease connectors list");
close(fd);
wp_drm_lease_v1_send_finished(lease->resource); wp_drm_lease_v1_send_finished(lease->resource);
return NULL; return NULL;
} }
@ -212,7 +211,7 @@ void wlr_drm_lease_request_v1_reject(
void wlr_drm_lease_v1_revoke(struct wlr_drm_lease_v1 *lease) { void wlr_drm_lease_v1_revoke(struct wlr_drm_lease_v1 *lease) {
assert(lease); assert(lease);
wlr_log(WLR_DEBUG, "Revoking lease %"PRIu32, lease->lessee_id); wlr_log(WLR_DEBUG, "Revoking lease %"PRIu32, lease->drm_lease->lessee_id);
drm_lease_v1_destroy(lease); drm_lease_v1_destroy(lease);
} }