Merge gitlab.freedesktop.org:wlroots/wlroots

This commit is contained in:
Christian Kröner 2022-08-30 21:25:19 +02:00
commit 26f6fab4eb
275 changed files with 7472 additions and 5463 deletions

View file

@ -1,4 +1,6 @@
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <stdio.h>
#include <wlr/util/log.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
@ -6,6 +8,41 @@
#include "backend/drm/iface.h"
#include "backend/drm/util.h"
static char *atomic_commit_flags_str(uint32_t flags) {
const char *const l[] = {
(flags & DRM_MODE_PAGE_FLIP_EVENT) ? "PAGE_FLIP_EVENT" : NULL,
(flags & DRM_MODE_PAGE_FLIP_ASYNC) ? "PAGE_FLIP_ASYNC" : NULL,
(flags & DRM_MODE_ATOMIC_TEST_ONLY) ? "ATOMIC_TEST_ONLY" : NULL,
(flags & DRM_MODE_ATOMIC_NONBLOCK) ? "ATOMIC_NONBLOCK" : NULL,
(flags & DRM_MODE_ATOMIC_ALLOW_MODESET) ? "ATOMIC_ALLOW_MODESET" : NULL,
};
char *buf = NULL;
size_t size = 0;
FILE *f = open_memstream(&buf, &size);
if (f == NULL) {
return NULL;
}
for (size_t i = 0; i < sizeof(l) / sizeof(l[0]); i++) {
if (l[i] == NULL) {
continue;
}
if (ftell(f) > 0) {
fprintf(f, " | ");
}
fprintf(f, "%s", l[i]);
}
if (ftell(f) == 0) {
fprintf(f, "none");
}
fclose(f);
return buf;
}
struct atomic {
drmModeAtomicReq *req;
bool failed;
@ -33,9 +70,11 @@ static bool atomic_commit(struct atomic *atom,
if (ret != 0) {
wlr_drm_conn_log_errno(conn,
(flags & DRM_MODE_ATOMIC_TEST_ONLY) ? WLR_DEBUG : WLR_ERROR,
"Atomic %s failed (%s)",
(flags & DRM_MODE_ATOMIC_TEST_ONLY) ? "test" : "commit",
(flags & DRM_MODE_ATOMIC_ALLOW_MODESET) ? "modeset" : "pageflip");
"Atomic commit failed");
char *flags_str = atomic_commit_flags_str(flags);
wlr_log(WLR_DEBUG, "(Atomic commit flags: %s)",
flags_str ? flags_str : "<error>");
free(flags_str);
return false;
}
@ -217,8 +256,10 @@ static bool atomic_crtc_commit(struct wlr_drm_connector *conn,
bool prev_vrr_enabled =
output->adaptive_sync_status == WLR_OUTPUT_ADAPTIVE_SYNC_ENABLED;
bool vrr_enabled = prev_vrr_enabled;
if ((state->base->committed & WLR_OUTPUT_STATE_ADAPTIVE_SYNC_ENABLED) &&
drm_connector_supports_vrr(conn)) {
if ((state->base->committed & WLR_OUTPUT_STATE_ADAPTIVE_SYNC_ENABLED)) {
if (!drm_connector_supports_vrr(conn)) {
return false;
}
vrr_enabled = state->base->adaptive_sync_enabled;
}
@ -227,7 +268,12 @@ static bool atomic_crtc_commit(struct wlr_drm_connector *conn,
}
if (modeset) {
flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
} else if (!test_only) {
} else if (!test_only && (state->base->committed & WLR_OUTPUT_STATE_BUFFER)) {
// The wlr_output API requires non-modeset commits with a new buffer to
// wait for the frame event. However compositors often perform
// non-modesets commits without a new buffer without waiting for the
// frame event. In that case we need to make the KMS commit blocking,
// otherwise the kernel will error out with EBUSY.
flags |= DRM_MODE_ATOMIC_NONBLOCK;
}
@ -238,6 +284,13 @@ static bool atomic_crtc_commit(struct wlr_drm_connector *conn,
atomic_add(&atom, conn->id, conn->props.link_status,
DRM_MODE_LINK_STATUS_GOOD);
}
if (active && conn->props.content_type != 0) {
atomic_add(&atom, conn->id, conn->props.content_type,
DRM_MODE_CONTENT_TYPE_GRAPHICS);
}
if (active && conn->props.max_bpc != 0 && conn->max_bpc > 0) {
atomic_add(&atom, conn->id, conn->props.max_bpc, conn->max_bpc);
}
atomic_add(&atom, crtc->id, crtc->props.mode_id, mode_id);
atomic_add(&atom, crtc->id, crtc->props.active, active);
if (active) {

View file

@ -1,6 +1,7 @@
#include <assert.h>
#include <errno.h>
#include <drm_fourcc.h>
#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -12,7 +13,6 @@
#include <wlr/util/log.h>
#include <xf86drm.h>
#include "backend/drm/drm.h"
#include "util/signal.h"
struct wlr_drm_backend *get_drm_backend_from_backend(
struct wlr_backend *wlr_backend) {
@ -58,6 +58,7 @@ static void backend_destroy(struct wlr_backend *backend) {
finish_drm_resources(drm);
udev_hwdb_unref(drm->hwdb);
free(drm->name);
wlr_session_close_file(drm->session, drm->dev);
wl_event_source_remove(drm->drm_event);
@ -172,6 +173,23 @@ static void handle_parent_destroy(struct wl_listener *listener, void *data) {
backend_destroy(&drm->backend);
}
static struct udev_hwdb *create_udev_hwdb(void) {
struct udev *udev = udev_new();
if (!udev) {
wlr_log(WLR_ERROR, "udev_new failed");
return NULL;
}
struct udev_hwdb *hwdb = udev_hwdb_new(udev);
udev_unref(udev);
if (!hwdb) {
wlr_log(WLR_ERROR, "udev_hwdb_new failed");
return NULL;
}
return hwdb;
}
struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
struct wlr_session *session, struct wlr_device *dev,
struct wlr_backend *parent) {
@ -226,6 +244,12 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
drm->session_active.notify = handle_session_active;
wl_signal_add(&session->events.active, &drm->session_active);
drm->hwdb = create_udev_hwdb();
if (!drm->hwdb) {
wlr_log(WLR_INFO, "Failed to load udev_hwdb, "
"falling back to PnP IDs instead of manufacturer names");
}
if (!check_drm_features(drm)) {
goto error_event;
}
@ -250,15 +274,17 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
goto error_mgpu_renderer;
}
// Force a linear layout. In case explicit modifiers aren't supported,
// the meaning of implicit modifiers changes from one GPU to the other.
// In case explicit modifiers are supported, we still have no guarantee
// that the buffer producer will support these, so they might fallback
// to implicit modifiers.
// Forbid implicit modifiers, because their meaning changes from one
// GPU to another.
for (size_t i = 0; i < texture_formats->len; i++) {
const struct wlr_drm_format *fmt = texture_formats->formats[i];
wlr_drm_format_set_add(&drm->mgpu_formats, fmt->format,
DRM_FORMAT_MOD_LINEAR);
for (size_t j = 0; j < fmt->len; j++) {
uint64_t mod = fmt->modifiers[j];
if (mod == DRM_FORMAT_MOD_INVALID) {
continue;
}
wlr_drm_format_set_add(&drm->mgpu_formats, fmt->format, mod);
}
}
}
@ -275,6 +301,7 @@ error_mgpu_renderer:
error_resources:
finish_drm_resources(drm);
error_event:
udev_hwdb_unref(drm->hwdb);
wl_list_remove(&drm->session_active.link);
wl_event_source_remove(drm->drm_event);
error_fd:

View file

@ -29,14 +29,18 @@
#include "render/drm_format_set.h"
#include "render/swapchain.h"
#include "render/wlr_renderer.h"
#include "util/signal.h"
#include "util/env.h"
static const uint32_t SUPPORTED_OUTPUT_STATE =
WLR_OUTPUT_STATE_BACKEND_OPTIONAL |
// Output state which needs a KMS commit to be applied
static const uint32_t COMMIT_OUTPUT_STATE =
WLR_OUTPUT_STATE_BUFFER |
WLR_OUTPUT_STATE_MODE |
WLR_OUTPUT_STATE_ENABLED |
WLR_OUTPUT_STATE_GAMMA_LUT;
WLR_OUTPUT_STATE_GAMMA_LUT |
WLR_OUTPUT_STATE_ADAPTIVE_SYNC_ENABLED;
static const uint32_t SUPPORTED_OUTPUT_STATE =
WLR_OUTPUT_STATE_BACKEND_OPTIONAL | COMMIT_OUTPUT_STATE;
bool check_drm_features(struct wlr_drm_backend *drm) {
if (drmGetCap(drm->fd, DRM_CAP_CURSOR_WIDTH, &drm->cursor_width)) {
@ -72,8 +76,7 @@ bool check_drm_features(struct wlr_drm_backend *drm) {
return false;
}
const char *no_atomic = getenv("WLR_DRM_NO_ATOMIC");
if (no_atomic && strcmp(no_atomic, "1") == 0) {
if (env_parse_bool("WLR_DRM_NO_ATOMIC")) {
wlr_log(WLR_DEBUG,
"WLR_DRM_NO_ATOMIC set, forcing legacy DRM interface");
drm->iface = &legacy_iface;
@ -89,8 +92,7 @@ bool check_drm_features(struct wlr_drm_backend *drm) {
int ret = drmGetCap(drm->fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
drm->clock = (ret == 0 && cap == 1) ? CLOCK_MONOTONIC : CLOCK_REALTIME;
const char *no_modifiers = getenv("WLR_DRM_NO_MODIFIERS");
if (no_modifiers != NULL && strcmp(no_modifiers, "1") == 0) {
if (env_parse_bool("WLR_DRM_NO_MODIFIERS")) {
wlr_log(WLR_DEBUG, "WLR_DRM_NO_MODIFIERS set, disabling modifiers");
} else {
ret = drmGetCap(drm->fd, DRM_CAP_ADDFB2_MODIFIERS, &cap);
@ -380,6 +382,7 @@ static bool drm_crtc_page_flip(struct wlr_drm_connector *conn,
static void drm_connector_state_init(struct wlr_drm_connector_state *state,
struct wlr_drm_connector *conn,
const struct wlr_output_state *base) {
memset(state, 0, sizeof(*state));
state->base = base;
state->modeset = base->committed &
(WLR_OUTPUT_STATE_ENABLED | WLR_OUTPUT_STATE_MODE);
@ -458,37 +461,48 @@ static bool drm_connector_set_pending_fb(struct wlr_drm_connector *conn,
static bool drm_connector_alloc_crtc(struct wlr_drm_connector *conn);
static bool drm_connector_test(struct wlr_output *output) {
static bool drm_connector_test(struct wlr_output *output,
const struct wlr_output_state *state) {
struct wlr_drm_connector *conn = get_drm_connector_from_output(output);
if (!conn->backend->session->active) {
return false;
}
uint32_t unsupported = output->pending.committed & ~SUPPORTED_OUTPUT_STATE;
uint32_t unsupported = state->committed & ~SUPPORTED_OUTPUT_STATE;
if (unsupported != 0) {
wlr_log(WLR_DEBUG, "Unsupported output state fields: 0x%"PRIx32,
unsupported);
return false;
}
if ((output->pending.committed & WLR_OUTPUT_STATE_ENABLED) &&
output->pending.enabled) {
if ((state->committed & COMMIT_OUTPUT_STATE) == 0) {
// This commit doesn't change the KMS state
return true;
}
if ((state->committed & WLR_OUTPUT_STATE_ENABLED) && state->enabled) {
if (output->current_mode == NULL &&
!(output->pending.committed & WLR_OUTPUT_STATE_MODE)) {
!(state->committed & WLR_OUTPUT_STATE_MODE)) {
wlr_drm_conn_log(conn, WLR_DEBUG,
"Can't enable an output without a mode");
return false;
}
}
if ((state->committed & WLR_OUTPUT_ADAPTIVE_SYNC_ENABLED) &&
state->adaptive_sync_enabled &&
!drm_connector_supports_vrr(conn)) {
return false;
}
struct wlr_drm_connector_state pending = {0};
drm_connector_state_init(&pending, conn, &output->pending);
drm_connector_state_init(&pending, conn, state);
if (pending.active) {
if ((output->pending.committed &
if ((state->committed &
(WLR_OUTPUT_STATE_ENABLED | WLR_OUTPUT_STATE_MODE)) &&
!(output->pending.committed & WLR_OUTPUT_STATE_BUFFER)) {
!(state->committed & WLR_OUTPUT_STATE_BUFFER)) {
wlr_drm_conn_log(conn, WLR_DEBUG,
"Can't enable an output without a buffer");
return false;
@ -513,7 +527,7 @@ static bool drm_connector_test(struct wlr_output *output) {
return true;
}
if (output->pending.committed & WLR_OUTPUT_STATE_BUFFER) {
if (state->committed & WLR_OUTPUT_STATE_BUFFER) {
if (!drm_connector_set_pending_fb(conn, pending.base)) {
return false;
}
@ -559,6 +573,11 @@ bool drm_connector_commit_state(struct wlr_drm_connector *conn,
return false;
}
if ((base->committed & COMMIT_OUTPUT_STATE) == 0) {
// This commit doesn't change the KMS state
return true;
}
struct wlr_drm_connector_state pending = {0};
drm_connector_state_init(&pending, conn, base);
@ -596,14 +615,15 @@ bool drm_connector_commit_state(struct wlr_drm_connector *conn,
return true;
}
static bool drm_connector_commit(struct wlr_output *output) {
static bool drm_connector_commit(struct wlr_output *output,
const struct wlr_output_state *state) {
struct wlr_drm_connector *conn = get_drm_connector_from_output(output);
if (!drm_connector_test(output)) {
if (!drm_connector_test(output, state)) {
return false;
}
return drm_connector_commit_state(conn, &output->pending);
return drm_connector_commit_state(conn, state);
}
size_t drm_crtc_get_gamma_lut_size(struct wlr_drm_backend *drm,
@ -692,8 +712,7 @@ static bool drm_connector_set_mode(struct wlr_drm_connector *conn,
return true;
}
if (conn->status != WLR_DRM_CONN_CONNECTED
&& conn->status != WLR_DRM_CONN_NEEDS_MODESET) {
if (conn->status != DRM_MODE_CONNECTED) {
wlr_drm_conn_log(conn, WLR_ERROR,
"Cannot modeset a disconnected output");
return false;
@ -720,7 +739,6 @@ static bool drm_connector_set_mode(struct wlr_drm_connector *conn,
return false;
}
conn->status = WLR_DRM_CONN_CONNECTED;
wlr_output_update_mode(&conn->output, wlr_mode);
wlr_output_update_enabled(&conn->output, true);
conn->desired_enabled = true;
@ -892,7 +910,7 @@ static void drm_connector_destroy_output(struct wlr_output *output) {
dealloc_crtc(conn);
conn->status = WLR_DRM_CONN_DISCONNECTED;
conn->status = DRM_MODE_DISCONNECTED;
conn->desired_enabled = false;
conn->possible_crtcs = 0;
conn->pending_page_flip_crtc = 0;
@ -1071,9 +1089,7 @@ static void realloc_crtcs(struct wlr_drm_backend *drm) {
// Only search CRTCs for user-enabled outputs (that are already
// connected or in need of a modeset)
if ((conn->status == WLR_DRM_CONN_CONNECTED ||
conn->status == WLR_DRM_CONN_NEEDS_MODESET) &&
conn->desired_enabled) {
if (conn->status == DRM_MODE_CONNECTED && conn->desired_enabled) {
connector_constraints[i] = conn->possible_crtcs;
} else {
// Will always fail to match anything
@ -1104,8 +1120,7 @@ static void realloc_crtcs(struct wlr_drm_backend *drm) {
*/
for (size_t i = 0; i < num_outputs; ++i) {
struct wlr_drm_connector *conn = connectors[i];
if (conn->status == WLR_DRM_CONN_CONNECTED &&
conn->desired_enabled &&
if (conn->status == DRM_MODE_CONNECTED && conn->output.enabled &&
connector_match[i] == -1) {
wlr_log(WLR_DEBUG, "Could not match a CRTC for previously connected output; "
"keeping old configuration");
@ -1132,7 +1147,6 @@ static void realloc_crtcs(struct wlr_drm_backend *drm) {
if (connector_match[i] == -1) {
if (prev_enabled) {
wlr_drm_conn_log(conn, WLR_DEBUG, "Output has lost its CRTC");
conn->status = WLR_DRM_CONN_NEEDS_MODESET;
wlr_output_update_enabled(&conn->output, false);
wlr_output_update_mode(&conn->output, NULL);
}
@ -1142,42 +1156,17 @@ static void realloc_crtcs(struct wlr_drm_backend *drm) {
conn->crtc = &drm->crtcs[connector_match[i]];
// Only realloc buffers if we have actually been modeset
if (conn->status != WLR_DRM_CONN_CONNECTED) {
if (conn->status != DRM_MODE_CONNECTED || !conn->output.enabled) {
continue;
}
wlr_output_damage_whole(&conn->output);
}
}
static uint32_t get_possible_crtcs(int fd, const drmModeConnector *conn) {
uint32_t possible_crtcs = 0;
for (int i = 0; i < conn->count_encoders; ++i) {
drmModeEncoder *enc = drmModeGetEncoder(fd, conn->encoders[i]);
if (!enc) {
continue;
}
possible_crtcs |= enc->possible_crtcs;
drmModeFreeEncoder(enc);
}
return possible_crtcs;
}
static void disconnect_drm_connector(struct wlr_drm_connector *conn);
void scan_drm_connectors(struct wlr_drm_backend *drm,
struct wlr_device_hotplug_event *event) {
/*
* This GPU is not really a modesetting device.
* It's just being used as a renderer.
*/
if (drm->num_crtcs == 0) {
return;
}
if (event != NULL && event->connector_id != 0) {
wlr_log(WLR_INFO, "Scanning DRM connector %"PRIu32" on %s",
event->connector_id, drm->name);
@ -1240,12 +1229,17 @@ void scan_drm_connectors(struct wlr_drm_backend *drm,
}
wlr_conn->backend = drm;
wlr_conn->status = WLR_DRM_CONN_DISCONNECTED;
wlr_conn->status = DRM_MODE_DISCONNECTED;
wlr_conn->id = drm_conn->connector_id;
const char *conn_name =
drmModeGetConnectorTypeName(drm_conn->connector_type);
if (conn_name == NULL) {
conn_name = "Unknown";
}
snprintf(wlr_conn->name, sizeof(wlr_conn->name),
"%s-%"PRIu32, conn_get_name(drm_conn->connector_type),
drm_conn->connector_type_id);
"%s-%"PRIu32, conn_name, drm_conn->connector_type_id);
wl_list_insert(drm->outputs.prev, &wlr_conn->link);
wlr_log(WLR_INFO, "Found connector '%s'", wlr_conn->name);
@ -1282,7 +1276,7 @@ void scan_drm_connectors(struct wlr_drm_backend *drm,
}
}
if (wlr_conn->status == WLR_DRM_CONN_DISCONNECTED &&
if (wlr_conn->status == DRM_MODE_DISCONNECTED &&
drm_conn->connection == DRM_MODE_CONNECTED) {
wlr_log(WLR_INFO, "'%s' connected", wlr_conn->name);
wlr_log(WLR_DEBUG, "Current CRTC: %d",
@ -1310,16 +1304,24 @@ void scan_drm_connectors(struct wlr_drm_backend *drm,
wlr_conn->output.non_desktop = non_desktop;
}
wlr_conn->max_bpc = 0;
if (wlr_conn->props.max_bpc != 0) {
if (!introspect_drm_prop_range(drm->fd, wlr_conn->props.max_bpc,
NULL, &wlr_conn->max_bpc)) {
wlr_log(WLR_ERROR, "Failed to introspect 'max bpc' property");
}
}
size_t edid_len = 0;
uint8_t *edid = get_drm_prop_blob(drm->fd,
wlr_conn->id, wlr_conn->props.edid, &edid_len);
parse_edid(&wlr_conn->output, edid_len, edid);
parse_edid(wlr_conn, edid_len, edid);
free(edid);
size_t tile_len = 0;
uint8_t *tile = get_drm_prop_blob(drm->fd,
wlr_conn->id, wlr_conn->props.tile, &tile_len);
parse_tile(&wlr_conn->output, tile_len, tile);
parse_tile(wlr_conn, tile_len, tile);
free(tile);
char *subconnector = NULL;
@ -1334,9 +1336,13 @@ void scan_drm_connectors(struct wlr_drm_backend *drm,
struct wlr_output *output = &wlr_conn->output;
char description[128];
snprintf(description, sizeof(description), "%s %s %s (%s%s%s)",
output->make, output->model, output->serial, output->name,
subconnector ? " via " : "", subconnector ? subconnector : "");
snprintf(description, sizeof(description), "%s %s%s%s (%s%s%s)",
output->make, output->model,
output->serial ? " " : "",
output->serial ? output->serial : "",
output->name,
subconnector ? " via " : "",
subconnector ? subconnector : "");
wlr_output_set_description(output, description);
free(subconnector);
@ -1371,7 +1377,8 @@ void scan_drm_connectors(struct wlr_drm_backend *drm,
wl_list_insert(wlr_conn->output.modes.prev, &mode->wlr_mode.link);
}
wlr_conn->possible_crtcs = get_possible_crtcs(drm->fd, drm_conn);
wlr_conn->possible_crtcs =
drmModeConnectorGetPossibleCrtcs(drm->fd, drm_conn);
if (wlr_conn->possible_crtcs == 0) {
wlr_drm_conn_log(wlr_conn, WLR_ERROR, "No CRTC possible");
}
@ -1381,10 +1388,9 @@ void scan_drm_connectors(struct wlr_drm_backend *drm,
wlr_output_update_enabled(&wlr_conn->output, wlr_conn->crtc != NULL);
wlr_conn->desired_enabled = true;
wlr_conn->status = WLR_DRM_CONN_NEEDS_MODESET;
wlr_conn->status = DRM_MODE_CONNECTED;
new_outputs[new_outputs_len++] = wlr_conn;
} else if ((wlr_conn->status == WLR_DRM_CONN_CONNECTED ||
wlr_conn->status == WLR_DRM_CONN_NEEDS_MODESET) &&
} else if (wlr_conn->status == DRM_MODE_CONNECTED &&
drm_conn->connection != DRM_MODE_CONNECTED) {
wlr_log(WLR_INFO, "'%s' disconnected", wlr_conn->name);
disconnect_drm_connector(wlr_conn);
@ -1416,7 +1422,7 @@ void scan_drm_connectors(struct wlr_drm_backend *drm,
struct wlr_drm_connector *conn = new_outputs[i];
wlr_drm_conn_log(conn, WLR_INFO, "Requesting modeset");
wlr_signal_emit_safe(&drm->backend.events.new_output,
wl_signal_emit_mutable(&drm->backend.events.new_output,
&conn->output);
}
}
@ -1474,7 +1480,7 @@ static void handle_page_flip(int fd, unsigned seq,
conn->pending_page_flip_crtc = 0;
if (conn->status != WLR_DRM_CONN_CONNECTED || conn->crtc == NULL) {
if (conn->status != DRM_MODE_CONNECTED || conn->crtc == NULL) {
wlr_drm_conn_log(conn, WLR_DEBUG,
"Ignoring page-flip event for disabled connector");
return;
@ -1537,7 +1543,7 @@ int handle_drm_event(int fd, uint32_t mask, void *data) {
}
static void disconnect_drm_connector(struct wlr_drm_connector *conn) {
if (conn->status == WLR_DRM_CONN_DISCONNECTED) {
if (conn->status == DRM_MODE_DISCONNECTED) {
return;
}
@ -1545,7 +1551,7 @@ static void disconnect_drm_connector(struct wlr_drm_connector *conn) {
// our wlr_drm_connector.
wlr_output_destroy(&conn->output);
assert(conn->status == WLR_DRM_CONN_DISCONNECTED);
assert(conn->status == DRM_MODE_DISCONNECTED);
}
void destroy_drm_connector(struct wlr_drm_connector *conn) {
@ -1635,7 +1641,7 @@ struct wlr_drm_lease *wlr_drm_create_lease(struct wlr_output **outputs,
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,
int lease_fd = drmModeCreateLease(drm->fd, objects, n_objects, O_CLOEXEC,
&lease->lessee_id);
if (lease_fd < 0) {
free(lease);
@ -1669,7 +1675,7 @@ void wlr_drm_lease_terminate(struct wlr_drm_lease *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);
wl_signal_emit_mutable(&lease->events.destroy, NULL);
struct wlr_drm_connector *conn;
wl_list_for_each(conn, &drm->outputs, link) {

View file

@ -115,8 +115,10 @@ static bool legacy_crtc_commit(struct wlr_drm_connector *conn,
}
}
if ((state->base->committed & WLR_OUTPUT_STATE_ADAPTIVE_SYNC_ENABLED) &&
drm_connector_supports_vrr(conn)) {
if (state->base->committed & WLR_OUTPUT_STATE_ADAPTIVE_SYNC_ENABLED) {
if (!drm_connector_supports_vrr(conn)) {
return false;
}
if (drmModeObjectSetProperty(drm->fd, crtc->id, DRM_MODE_OBJECT_CRTC,
crtc->props.vrr_enabled,
state->base->adaptive_sync_enabled) != 0) {

View file

@ -1,4 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
@ -25,7 +26,9 @@ static const struct prop_info connector_info[] = {
{ "EDID", INDEX(edid) },
{ "PATH", INDEX(path) },
{ "TILE", INDEX(tile) },
{ "content type", INDEX(content_type) },
{ "link-status", INDEX(link_status) },
{ "max bpc", INDEX(max_bpc) },
{ "non-desktop", INDEX(non_desktop) },
{ "panel orientation", INDEX(panel_orientation) },
{ "subconnector", INDEX(subconnector) },
@ -181,3 +184,28 @@ char *get_drm_prop_enum(int fd, uint32_t obj, uint32_t prop_id) {
return str;
}
bool introspect_drm_prop_range(int fd, uint32_t prop_id,
uint64_t *min, uint64_t *max) {
drmModePropertyRes *prop = drmModeGetProperty(fd, prop_id);
if (!prop) {
return false;
}
if (drmModeGetPropertyType(prop) != DRM_MODE_PROP_RANGE) {
drmModeFreeProperty(prop);
return false;
}
assert(prop->count_values == 2);
if (min != NULL) {
*min = prop->values[0];
}
if (max != NULL) {
*max = prop->values[1];
}
drmModeFreeProperty(prop);
return true;
}

View file

@ -48,31 +48,6 @@ void finish_drm_renderer(struct wlr_drm_renderer *renderer) {
wlr_renderer_destroy(renderer->wlr_rend);
}
bool init_drm_surface(struct wlr_drm_surface *surf,
struct wlr_drm_renderer *renderer, uint32_t width, uint32_t height,
const struct wlr_drm_format *drm_format) {
if (surf->width == width && surf->height == height) {
return true;
}
surf->renderer = renderer;
surf->width = width;
surf->height = height;
wlr_swapchain_destroy(surf->swapchain);
surf->swapchain = NULL;
surf->swapchain = wlr_swapchain_create(renderer->allocator, width, height,
drm_format);
if (surf->swapchain == NULL) {
wlr_log(WLR_ERROR, "Failed to create swapchain");
memset(surf, 0, sizeof(*surf));
return false;
}
return true;
}
static void finish_drm_surface(struct wlr_drm_surface *surf) {
if (!surf || !surf->renderer) {
return;
@ -83,12 +58,34 @@ static void finish_drm_surface(struct wlr_drm_surface *surf) {
memset(surf, 0, sizeof(*surf));
}
bool init_drm_surface(struct wlr_drm_surface *surf,
struct wlr_drm_renderer *renderer, int width, int height,
const struct wlr_drm_format *drm_format) {
if (surf->swapchain != NULL && surf->swapchain->width == width &&
surf->swapchain->height == height) {
return true;
}
finish_drm_surface(surf);
surf->swapchain = wlr_swapchain_create(renderer->allocator, width, height,
drm_format);
if (surf->swapchain == NULL) {
wlr_log(WLR_ERROR, "Failed to create swapchain");
return false;
}
surf->renderer = renderer;
return true;
}
struct wlr_buffer *drm_surface_blit(struct wlr_drm_surface *surf,
struct wlr_buffer *buffer) {
struct wlr_renderer *renderer = surf->renderer->wlr_rend;
if (surf->width != (uint32_t)buffer->width ||
surf->height != (uint32_t)buffer->height) {
if (surf->swapchain->width != buffer->width ||
surf->swapchain->height != buffer->height) {
wlr_log(WLR_ERROR, "Surface size doesn't match buffer size");
return NULL;
}
@ -106,7 +103,7 @@ struct wlr_buffer *drm_surface_blit(struct wlr_drm_surface *surf,
float mat[9];
wlr_matrix_identity(mat);
wlr_matrix_scale(mat, surf->width, surf->height);
wlr_matrix_scale(mat, surf->swapchain->width, surf->swapchain->height);
if (!wlr_renderer_begin_with_buffer(renderer, dst)) {
wlr_buffer_unlock(dst);

View file

@ -1,10 +1,14 @@
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <drm_fourcc.h>
#include <drm_mode.h>
#include <drm.h>
#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wlr/util/log.h>
#include "backend/drm/drm.h"
#include "backend/drm/util.h"
int32_t calculate_refresh_rate(const drmModeModeInfo *mode) {
@ -26,125 +30,93 @@ int32_t calculate_refresh_rate(const drmModeModeInfo *mode) {
return refresh;
}
// Constructed from http://edid.tv/manufacturer
static const char *get_manufacturer(uint16_t id) {
#define ID(a, b, c) ((a & 0x1f) << 10) | ((b & 0x1f) << 5) | (c & 0x1f)
switch (id) {
case ID('A', 'A', 'A'): return "Avolites Ltd";
case ID('A', 'C', 'I'): return "Ancor Communications Inc";
case ID('A', 'C', 'R'): return "Acer Technologies";
case ID('A', 'D', 'A'): return "Addi-Data GmbH";
case ID('A', 'P', 'P'): return "Apple Computer Inc";
case ID('A', 'S', 'K'): return "Ask A/S";
case ID('A', 'V', 'T'): return "Avtek (Electronics) Pty Ltd";
case ID('B', 'N', 'O'): return "Bang & Olufsen";
case ID('B', 'N', 'Q'): return "BenQ Corporation";
case ID('C', 'M', 'N'): return "Chimei Innolux Corporation";
case ID('C', 'M', 'O'): return "Chi Mei Optoelectronics corp.";
case ID('C', 'R', 'O'): return "Extraordinary Technologies PTY Limited";
case ID('D', 'E', 'L'): return "Dell Inc.";
case ID('D', 'G', 'C'): return "Data General Corporation";
case ID('D', 'O', 'N'): return "DENON, Ltd.";
case ID('E', 'N', 'C'): return "Eizo Nanao Corporation";
case ID('E', 'P', 'H'): return "Epiphan Systems Inc.";
case ID('E', 'X', 'P'): return "Data Export Corporation";
case ID('F', 'N', 'I'): return "Funai Electric Co., Ltd.";
case ID('F', 'U', 'S'): return "Fujitsu Siemens Computers GmbH";
case ID('G', 'S', 'M'): return "Goldstar Company Ltd";
case ID('H', 'I', 'Q'): return "Kaohsiung Opto Electronics Americas, Inc.";
case ID('H', 'S', 'D'): return "HannStar Display Corp";
case ID('H', 'T', 'C'): return "Hitachi Ltd";
case ID('H', 'W', 'P'): return "Hewlett Packard";
case ID('I', 'N', 'T'): return "Interphase Corporation";
case ID('I', 'N', 'X'): return "Communications Supply Corporation (A division of WESCO)";
case ID('I', 'T', 'E'): return "Integrated Tech Express Inc";
case ID('I', 'V', 'M'): return "Iiyama North America";
case ID('L', 'E', 'N'): return "Lenovo Group Limited";
case ID('M', 'A', 'X'): return "Rogen Tech Distribution Inc";
case ID('M', 'E', 'G'): return "Abeam Tech Ltd";
case ID('M', 'E', 'I'): return "Panasonic Industry Company";
case ID('M', 'T', 'C'): return "Mars-Tech Corporation";
case ID('M', 'T', 'X'): return "Matrox";
case ID('N', 'E', 'C'): return "NEC Corporation";
case ID('N', 'E', 'X'): return "Nexgen Mediatech Inc.";
case ID('O', 'N', 'K'): return "ONKYO Corporation";
case ID('O', 'R', 'N'): return "ORION ELECTRIC CO., LTD.";
case ID('O', 'T', 'M'): return "Optoma Corporation";
case ID('O', 'V', 'R'): return "Oculus VR, Inc.";
case ID('P', 'H', 'L'): return "Philips Consumer Electronics Company";
case ID('P', 'I', 'O'): return "Pioneer Electronic Corporation";
case ID('P', 'N', 'R'): return "Planar Systems, Inc.";
case ID('Q', 'D', 'S'): return "Quanta Display Inc.";
case ID('R', 'A', 'T'): return "Rent-A-Tech";
case ID('R', 'E', 'N'): return "Renesas Technology Corp.";
case ID('S', 'A', 'M'): return "Samsung Electric Company";
case ID('S', 'A', 'N'): return "Sanyo Electric Co., Ltd.";
case ID('S', 'E', 'C'): return "Seiko Epson Corporation";
case ID('S', 'H', 'P'): return "Sharp Corporation";
case ID('S', 'I', 'I'): return "Silicon Image, Inc.";
case ID('S', 'N', 'Y'): return "Sony";
case ID('S', 'T', 'D'): return "STD Computer Inc";
case ID('S', 'V', 'S'): return "SVSI";
case ID('S', 'Y', 'N'): return "Synaptics Inc";
case ID('T', 'C', 'L'): return "Technical Concepts Ltd";
case ID('T', 'O', 'P'): return "Orion Communications Co., Ltd.";
case ID('T', 'S', 'B'): return "Toshiba America Info Systems Inc";
case ID('T', 'S', 'T'): return "Transtream Inc";
case ID('U', 'N', 'K'): return "Unknown";
case ID('V', 'E', 'S'): return "Vestel Elektronik Sanayi ve Ticaret A. S.";
case ID('V', 'I', 'T'): return "Visitech AS";
case ID('V', 'I', 'Z'): return "VIZIO, Inc";
case ID('V', 'L', 'V'): return "Valve";
case ID('V', 'S', 'C'): return "ViewSonic Corporation";
case ID('Y', 'M', 'H'): return "Yamaha Corporation";
default: return "Unknown";
static const char *get_manufacturer(struct udev_hwdb *hwdb, uint16_t code) {
static char pnp_id[4];
// The ASCII 3-letter manufacturer PnP ID is encoded in 5-bit codes
pnp_id[0] = ((code >> 10) & 0x1F) + '@';
pnp_id[1] = ((code >> 5) & 0x1F) + '@';
pnp_id[2] = ((code >> 0) & 0x1F) + '@';
pnp_id[3] = '\0';
if (hwdb == NULL) {
return pnp_id;
}
#undef ID
char query[32];
snprintf(query, sizeof(query), "acpi:%s:", pnp_id);
struct udev_list_entry *acpi_entry =
udev_hwdb_get_properties_list_entry(hwdb, query, 0);
if (acpi_entry == NULL) {
return pnp_id;
}
struct udev_list_entry *vendor_entry =
udev_list_entry_get_by_name(acpi_entry, "ID_VENDOR_FROM_DATABASE");
if (vendor_entry == NULL) {
return pnp_id;
}
return udev_list_entry_get_value(vendor_entry);
}
/* See https://en.wikipedia.org/wiki/Extended_Display_Identification_Data for layout of EDID data.
* We don't parse the EDID properly. We just expect to receive valid data.
*/
void parse_edid(struct wlr_output *restrict output, size_t len, const uint8_t *data) {
void parse_edid(struct wlr_drm_connector *conn, size_t len, const uint8_t *data) {
struct wlr_output *output = &conn->output;
free(output->make);
free(output->model);
free(output->serial);
output->make = NULL;
output->model = NULL;
output->serial = NULL;
if (!data || len < 128) {
snprintf(output->make, sizeof(output->make), "<Unknown>");
snprintf(output->model, sizeof(output->model), "<Unknown>");
return;
}
uint16_t id = (data[8] << 8) | data[9];
snprintf(output->make, sizeof(output->make), "%s", get_manufacturer(id));
output->make = strdup(get_manufacturer(conn->backend->hwdb, id));
uint16_t model = data[10] | (data[11] << 8);
snprintf(output->model, sizeof(output->model), "0x%04X", model);
char model_str[32];
snprintf(model_str, sizeof(model_str), "0x%04" PRIX16, model);
uint32_t serial = data[12] | (data[13] << 8) | (data[14] << 8) | (data[15] << 8);
snprintf(output->serial, sizeof(output->serial), "0x%08X", serial);
char serial_str[32];
if (serial != 0) {
snprintf(serial_str, sizeof(serial_str), "0x%08" PRIX32, serial);
} else {
serial_str[0] = '\0';
}
for (size_t i = 72; i <= 108; i += 18) {
uint16_t flag = (data[i] << 8) | data[i + 1];
if (flag == 0 && data[i + 3] == 0xFC) {
sprintf(output->model, "%.13s", &data[i + 5]);
snprintf(model_str, sizeof(model_str), "%.13s", &data[i + 5]);
// Monitor names are terminated by newline if they're too short
char *nl = strchr(output->model, '\n');
char *nl = strchr(model_str, '\n');
if (nl) {
*nl = '\0';
}
} else if (flag == 0 && data[i + 3] == 0xFF) {
sprintf(output->serial, "%.13s", &data[i + 5]);
snprintf(serial_str, sizeof(serial_str), "%.13s", &data[i + 5]);
// Monitor serial numbers are terminated by newline if they're too
// short
char *nl = strchr(output->serial, '\n');
char* nl = strchr(serial_str, '\n');
if (nl) {
*nl = '\0';
}
}
}
}
void parse_tile(struct wlr_output *restrict output, size_t len, const uint8_t *data) {
void parse_tile(struct wlr_drm_connector *conn, size_t len, const uint8_t *data) {
struct wlr_output *output = &conn->output;
if (len > 0) {
int ret;
ret = sscanf((char*)data, "%d:%d:%d:%d:%d:%d:%d:%d",
@ -207,6 +179,9 @@ const char *conn_get_name(uint32_t type_id) {
case DRM_MODE_CONNECTOR_USB: return "USB";
#endif
default: return "Unknown";
output->model = strdup(model_str);
if (serial_str[0] != '\0') {
output->serial = strdup(serial_str);
}
}