2017-08-09 08:43:01 +00:00
|
|
|
#include <gbm.h>
|
2018-02-12 20:29:23 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wlr/util/log.h>
|
2017-08-09 08:43:01 +00:00
|
|
|
#include <xf86drm.h>
|
|
|
|
#include <xf86drmMode.h>
|
2017-09-30 06:03:34 +00:00
|
|
|
#include "backend/drm/drm.h"
|
2017-09-30 06:11:41 +00:00
|
|
|
#include "backend/drm/iface.h"
|
2017-09-30 06:03:34 +00:00
|
|
|
#include "backend/drm/util.h"
|
2017-08-09 08:43:01 +00:00
|
|
|
|
|
|
|
struct atomic {
|
|
|
|
drmModeAtomicReq *req;
|
|
|
|
bool failed;
|
|
|
|
};
|
|
|
|
|
2020-05-09 16:46:17 +00:00
|
|
|
static void atomic_begin(struct atomic *atom) {
|
|
|
|
memset(atom, 0, sizeof(*atom));
|
2017-08-09 08:43:01 +00:00
|
|
|
|
2020-05-09 16:46:17 +00:00
|
|
|
atom->req = drmModeAtomicAlloc();
|
|
|
|
if (!atom->req) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
atom->failed = true;
|
|
|
|
return;
|
2017-08-09 08:43:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:46:17 +00:00
|
|
|
static bool atomic_commit(struct atomic *atom,
|
|
|
|
struct wlr_drm_connector *conn, uint32_t flags) {
|
2020-12-09 13:31:06 +00:00
|
|
|
struct wlr_drm_backend *drm = conn->backend;
|
2017-08-09 08:43:01 +00:00
|
|
|
if (atom->failed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:46:17 +00:00
|
|
|
int ret = drmModeAtomicCommit(drm->fd, atom->req, flags, drm);
|
2021-04-06 13:44:32 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
wlr_drm_conn_log_errno(conn,
|
|
|
|
(flags & DRM_MODE_ATOMIC_TEST_ONLY) ? WLR_DEBUG : WLR_ERROR,
|
|
|
|
"Atomic %s failed (%s)",
|
2020-05-09 16:46:17 +00:00
|
|
|
(flags & DRM_MODE_ATOMIC_TEST_ONLY) ? "test" : "commit",
|
|
|
|
(flags & DRM_MODE_ATOMIC_ALLOW_MODESET) ? "modeset" : "pageflip");
|
|
|
|
return false;
|
2017-08-09 08:43:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 16:46:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-09 08:43:01 +00:00
|
|
|
|
2020-05-09 16:46:17 +00:00
|
|
|
static void atomic_finish(struct atomic *atom) {
|
|
|
|
drmModeAtomicFree(atom->req);
|
2017-08-09 08:43:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 23:38:43 +00:00
|
|
|
static void atomic_add(struct atomic *atom, uint32_t id, uint32_t prop, uint64_t val) {
|
2017-08-09 08:43:01 +00:00
|
|
|
if (!atom->failed && drmModeAtomicAddProperty(atom->req, id, prop, val) < 0) {
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to add atomic DRM property");
|
2017-08-09 08:43:01 +00:00
|
|
|
atom->failed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 19:11:43 +00:00
|
|
|
static bool create_mode_blob(struct wlr_drm_backend *drm,
|
|
|
|
struct wlr_drm_crtc *crtc, uint32_t *blob_id) {
|
2020-05-18 12:27:42 +00:00
|
|
|
if (!crtc->pending.active) {
|
2020-05-07 19:11:43 +00:00
|
|
|
*blob_id = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-18 12:27:42 +00:00
|
|
|
if (drmModeCreatePropertyBlob(drm->fd, &crtc->pending.mode->drm_mode,
|
2020-05-07 19:11:43 +00:00
|
|
|
sizeof(drmModeModeInfo), blob_id)) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Unable to create mode property blob");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-07 15:50:40 +00:00
|
|
|
static bool create_gamma_lut_blob(struct wlr_drm_backend *drm,
|
2020-05-14 16:27:02 +00:00
|
|
|
size_t size, const uint16_t *lut, uint32_t *blob_id) {
|
|
|
|
if (size == 0) {
|
2020-05-07 15:50:40 +00:00
|
|
|
*blob_id = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct drm_color_lut *gamma = malloc(size * sizeof(struct drm_color_lut));
|
|
|
|
if (gamma == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to allocate gamma table");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:27:02 +00:00
|
|
|
const uint16_t *r = lut;
|
|
|
|
const uint16_t *g = lut + size;
|
|
|
|
const uint16_t *b = lut + 2 * size;
|
2020-05-07 15:50:40 +00:00
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
gamma[i].red = r[i];
|
|
|
|
gamma[i].green = g[i];
|
|
|
|
gamma[i].blue = b[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drmModeCreatePropertyBlob(drm->fd, gamma,
|
|
|
|
size * sizeof(struct drm_color_lut), blob_id) != 0) {
|
2020-05-14 16:27:02 +00:00
|
|
|
wlr_log_errno(WLR_ERROR, "Unable to create gamma LUT property blob");
|
2020-05-07 15:50:40 +00:00
|
|
|
free(gamma);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
free(gamma);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-18 08:34:24 +00:00
|
|
|
static void commit_blob(struct wlr_drm_backend *drm,
|
|
|
|
uint32_t *current, uint32_t next) {
|
|
|
|
if (*current == next) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (*current != 0) {
|
|
|
|
drmModeDestroyPropertyBlob(drm->fd, *current);
|
|
|
|
}
|
|
|
|
*current = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rollback_blob(struct wlr_drm_backend *drm,
|
|
|
|
uint32_t *current, uint32_t next) {
|
|
|
|
if (*current == next) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (next != 0) {
|
|
|
|
drmModeDestroyPropertyBlob(drm->fd, next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 08:25:40 +00:00
|
|
|
static void plane_disable(struct atomic *atom, struct wlr_drm_plane *plane) {
|
2017-08-09 08:43:01 +00:00
|
|
|
uint32_t id = plane->id;
|
|
|
|
const union wlr_drm_plane_props *props = &plane->props;
|
2020-02-12 08:25:40 +00:00
|
|
|
atomic_add(atom, id, props->fb_id, 0);
|
|
|
|
atomic_add(atom, id, props->crtc_id, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_plane_props(struct atomic *atom, struct wlr_drm_backend *drm,
|
|
|
|
struct wlr_drm_plane *plane, uint32_t crtc_id, int32_t x, int32_t y) {
|
|
|
|
uint32_t id = plane->id;
|
|
|
|
const union wlr_drm_plane_props *props = &plane->props;
|
|
|
|
struct wlr_drm_fb *fb = plane_get_next_fb(plane);
|
2020-12-22 16:07:29 +00:00
|
|
|
if (fb == NULL) {
|
2020-12-15 11:21:40 +00:00
|
|
|
wlr_log(WLR_ERROR, "Failed to acquire FB");
|
2020-02-12 08:25:40 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2017-08-09 08:43:01 +00:00
|
|
|
|
2020-12-18 15:24:36 +00:00
|
|
|
uint32_t width = gbm_bo_get_width(fb->bo);
|
|
|
|
uint32_t height = gbm_bo_get_height(fb->bo);
|
|
|
|
|
2017-08-09 08:43:01 +00:00
|
|
|
// The src_* properties are in 16.16 fixed point
|
|
|
|
atomic_add(atom, id, props->src_x, 0);
|
|
|
|
atomic_add(atom, id, props->src_y, 0);
|
2020-12-18 15:24:36 +00:00
|
|
|
atomic_add(atom, id, props->src_w, (uint64_t)width << 16);
|
|
|
|
atomic_add(atom, id, props->src_h, (uint64_t)height << 16);
|
|
|
|
atomic_add(atom, id, props->crtc_w, width);
|
|
|
|
atomic_add(atom, id, props->crtc_h, height);
|
2020-12-15 11:21:40 +00:00
|
|
|
atomic_add(atom, id, props->fb_id, fb->id);
|
2017-08-09 08:43:01 +00:00
|
|
|
atomic_add(atom, id, props->crtc_id, crtc_id);
|
2020-02-12 08:25:40 +00:00
|
|
|
atomic_add(atom, id, props->crtc_x, (uint64_t)x);
|
|
|
|
atomic_add(atom, id, props->crtc_y, (uint64_t)y);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
2020-05-07 10:08:26 +00:00
|
|
|
wlr_log(WLR_ERROR, "Failed to set plane %"PRIu32" properties", plane->id);
|
2020-02-12 08:25:40 +00:00
|
|
|
atom->failed = true;
|
2017-08-09 08:43:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 17:20:56 +00:00
|
|
|
static bool atomic_crtc_commit(struct wlr_drm_backend *drm,
|
2021-04-06 14:32:01 +00:00
|
|
|
struct wlr_drm_connector *conn, const struct wlr_output_state *state,
|
|
|
|
uint32_t flags) {
|
2020-05-14 16:27:02 +00:00
|
|
|
struct wlr_output *output = &conn->output;
|
2020-02-12 08:25:40 +00:00
|
|
|
struct wlr_drm_crtc *crtc = conn->crtc;
|
|
|
|
|
2020-05-18 08:34:24 +00:00
|
|
|
uint32_t mode_id = crtc->mode_id;
|
2020-05-18 12:27:42 +00:00
|
|
|
if (crtc->pending_modeset) {
|
2020-05-18 08:34:24 +00:00
|
|
|
if (!create_mode_blob(drm, crtc, &mode_id)) {
|
2017-08-09 08:43:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 08:34:24 +00:00
|
|
|
uint32_t gamma_lut = crtc->gamma_lut;
|
2021-04-06 14:32:01 +00:00
|
|
|
if (state->committed & WLR_OUTPUT_STATE_GAMMA_LUT) {
|
2020-05-14 16:27:02 +00:00
|
|
|
// Fallback to legacy gamma interface when gamma properties are not
|
|
|
|
// available (can happen on older Intel GPUs that support gamma but not
|
|
|
|
// degamma).
|
2020-05-07 15:50:40 +00:00
|
|
|
if (crtc->props.gamma_lut == 0) {
|
2020-05-14 16:27:02 +00:00
|
|
|
if (!drm_legacy_crtc_set_gamma(drm, crtc,
|
2021-04-06 14:32:01 +00:00
|
|
|
state->gamma_lut_size,
|
|
|
|
state->gamma_lut)) {
|
2020-05-07 15:50:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2021-04-06 14:32:01 +00:00
|
|
|
if (!create_gamma_lut_blob(drm, state->gamma_lut_size,
|
|
|
|
state->gamma_lut, &gamma_lut)) {
|
2020-05-07 15:50:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 14:43:19 +00:00
|
|
|
bool prev_vrr_enabled =
|
|
|
|
output->adaptive_sync_status == WLR_OUTPUT_ADAPTIVE_SYNC_ENABLED;
|
|
|
|
bool vrr_enabled = prev_vrr_enabled;
|
2021-04-06 14:32:01 +00:00
|
|
|
if ((state->committed & WLR_OUTPUT_STATE_ADAPTIVE_SYNC_ENABLED) &&
|
2020-05-27 14:43:19 +00:00
|
|
|
drm_connector_supports_vrr(conn)) {
|
2021-04-06 14:32:01 +00:00
|
|
|
vrr_enabled = state->adaptive_sync_enabled;
|
2020-05-27 14:43:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 12:27:42 +00:00
|
|
|
if (crtc->pending_modeset) {
|
2018-01-15 20:49:37 +00:00
|
|
|
flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
|
2021-04-06 13:42:14 +00:00
|
|
|
} else if (!(flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
|
2018-01-15 20:49:37 +00:00
|
|
|
flags |= DRM_MODE_ATOMIC_NONBLOCK;
|
|
|
|
}
|
2017-08-09 08:43:01 +00:00
|
|
|
|
2018-01-15 20:49:37 +00:00
|
|
|
struct atomic atom;
|
2020-05-09 16:46:17 +00:00
|
|
|
atomic_begin(&atom);
|
2020-05-07 19:11:43 +00:00
|
|
|
atomic_add(&atom, conn->id, conn->props.crtc_id,
|
2020-05-18 12:27:42 +00:00
|
|
|
crtc->pending.active ? crtc->id : 0);
|
|
|
|
if (crtc->pending_modeset && crtc->pending.active &&
|
|
|
|
conn->props.link_status != 0) {
|
2018-10-04 12:11:37 +00:00
|
|
|
atomic_add(&atom, conn->id, conn->props.link_status,
|
|
|
|
DRM_MODE_LINK_STATUS_GOOD);
|
|
|
|
}
|
2020-05-18 08:34:24 +00:00
|
|
|
atomic_add(&atom, crtc->id, crtc->props.mode_id, mode_id);
|
2020-05-18 12:27:42 +00:00
|
|
|
atomic_add(&atom, crtc->id, crtc->props.active, crtc->pending.active);
|
|
|
|
if (crtc->pending.active) {
|
2020-05-14 15:26:30 +00:00
|
|
|
if (crtc->props.gamma_lut != 0) {
|
2020-05-18 08:34:24 +00:00
|
|
|
atomic_add(&atom, crtc->id, crtc->props.gamma_lut, gamma_lut);
|
2020-05-14 15:26:30 +00:00
|
|
|
}
|
2020-05-27 14:43:19 +00:00
|
|
|
if (crtc->props.vrr_enabled != 0) {
|
|
|
|
atomic_add(&atom, crtc->id, crtc->props.vrr_enabled, vrr_enabled);
|
|
|
|
}
|
2020-05-07 19:11:43 +00:00
|
|
|
set_plane_props(&atom, drm, crtc->primary, crtc->id, 0, 0);
|
|
|
|
if (crtc->cursor) {
|
2020-05-27 16:16:03 +00:00
|
|
|
if (drm_connector_is_cursor_visible(conn)) {
|
2020-05-07 19:11:43 +00:00
|
|
|
set_plane_props(&atom, drm, crtc->cursor, crtc->id,
|
|
|
|
conn->cursor_x, conn->cursor_y);
|
|
|
|
} else {
|
|
|
|
plane_disable(&atom, crtc->cursor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
plane_disable(&atom, crtc->primary);
|
|
|
|
if (crtc->cursor) {
|
2020-02-12 08:25:40 +00:00
|
|
|
plane_disable(&atom, crtc->cursor);
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 23:38:43 +00:00
|
|
|
|
2020-05-09 16:46:17 +00:00
|
|
|
bool ok = atomic_commit(&atom, conn, flags);
|
|
|
|
atomic_finish(&atom);
|
2020-05-18 08:34:24 +00:00
|
|
|
|
|
|
|
if (ok && !(flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
|
|
|
|
commit_blob(drm, &crtc->mode_id, mode_id);
|
|
|
|
commit_blob(drm, &crtc->gamma_lut, gamma_lut);
|
2020-05-27 14:43:19 +00:00
|
|
|
|
|
|
|
if (vrr_enabled != prev_vrr_enabled) {
|
|
|
|
output->adaptive_sync_status = vrr_enabled ?
|
|
|
|
WLR_OUTPUT_ADAPTIVE_SYNC_ENABLED :
|
|
|
|
WLR_OUTPUT_ADAPTIVE_SYNC_DISABLED;
|
2020-12-24 16:55:45 +00:00
|
|
|
wlr_drm_conn_log(conn, WLR_DEBUG, "VRR %s",
|
|
|
|
vrr_enabled ? "enabled" : "disabled");
|
2020-05-27 14:43:19 +00:00
|
|
|
}
|
2020-05-18 08:34:24 +00:00
|
|
|
} else {
|
|
|
|
rollback_blob(drm, &crtc->mode_id, mode_id);
|
|
|
|
rollback_blob(drm, &crtc->gamma_lut, gamma_lut);
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:46:17 +00:00
|
|
|
return ok;
|
2017-08-09 08:43:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 08:44:33 +00:00
|
|
|
const struct wlr_drm_interface atomic_iface = {
|
2020-05-07 17:20:56 +00:00
|
|
|
.crtc_commit = atomic_crtc_commit,
|
2017-08-09 08:43:01 +00:00
|
|
|
};
|