backend/drm: apply gamma LUT on page-flip

This commit is contained in:
Simon Ser 2020-05-07 17:50:40 +02:00
parent 69b2279092
commit 70883fd10b
6 changed files with 87 additions and 70 deletions

View File

@ -69,6 +69,41 @@ static void atomic_add(struct atomic *atom, uint32_t id, uint32_t prop, uint64_t
} }
} }
static bool create_gamma_lut_blob(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, uint32_t *blob_id) {
if (crtc->gamma_table_size == 0) {
*blob_id = 0;
return true;
}
uint32_t size = crtc->gamma_table_size;
uint16_t *r = crtc->gamma_table;
uint16_t *g = crtc->gamma_table + size;
uint16_t *b = crtc->gamma_table + 2 * size;
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;
}
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) {
wlr_log_errno(WLR_ERROR, "Unable to create property blob");
free(gamma);
return false;
}
free(gamma);
return true;
}
static void plane_disable(struct atomic *atom, struct wlr_drm_plane *plane) { static void plane_disable(struct atomic *atom, struct wlr_drm_plane *plane) {
uint32_t id = plane->id; uint32_t id = plane->id;
const union wlr_drm_plane_props *props = &plane->props; const union wlr_drm_plane_props *props = &plane->props;
@ -126,6 +161,24 @@ static bool atomic_crtc_pageflip(struct wlr_drm_backend *drm,
} }
} }
if (crtc->pending & WLR_DRM_CRTC_GAMMA_LUT) {
// Fallback to legacy gamma interface when gamma properties are not available
// (can happen on older Intel GPUs that support gamma but not degamma).
if (crtc->props.gamma_lut == 0) {
if (!drm_legacy_crtc_set_gamma(drm, crtc)) {
return false;
}
} else {
if (crtc->gamma_lut != 0) {
drmModeDestroyPropertyBlob(drm->fd, crtc->gamma_lut);
}
if (!create_gamma_lut_blob(drm, crtc, &crtc->gamma_lut)) {
return false;
}
}
}
uint32_t flags = DRM_MODE_PAGE_FLIP_EVENT; uint32_t flags = DRM_MODE_PAGE_FLIP_EVENT;
if (modeset) { if (modeset) {
flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
@ -142,6 +195,7 @@ static bool atomic_crtc_pageflip(struct wlr_drm_backend *drm,
} }
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);
atomic_add(&atom, crtc->id, crtc->props.gamma_lut, crtc->gamma_lut);
set_plane_props(&atom, drm, crtc->primary, crtc->id, 0, 0); set_plane_props(&atom, drm, crtc->primary, crtc->id, 0, 0);
if (crtc->cursor) { if (crtc->cursor) {
if (crtc->cursor->cursor_enabled) { if (crtc->cursor->cursor_enabled) {
@ -195,45 +249,6 @@ static bool atomic_crtc_set_cursor(struct wlr_drm_backend *drm,
return true; return true;
} }
static bool atomic_crtc_set_gamma(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, size_t size,
uint16_t *r, uint16_t *g, uint16_t *b) {
// Fallback to legacy gamma interface when gamma properties are not available
// (can happen on older Intel GPUs that support gamma but not degamma).
if (crtc->props.gamma_lut == 0) {
return legacy_iface.crtc_set_gamma(drm, crtc, size, r, g, b);
}
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;
}
for (size_t i = 0; i < size; i++) {
gamma[i].red = r[i];
gamma[i].green = g[i];
gamma[i].blue = b[i];
}
if (crtc->gamma_lut != 0) {
drmModeDestroyPropertyBlob(drm->fd, crtc->gamma_lut);
}
if (drmModeCreatePropertyBlob(drm->fd, gamma,
size * sizeof(struct drm_color_lut), &crtc->gamma_lut)) {
free(gamma);
wlr_log_errno(WLR_ERROR, "Unable to create property blob");
return false;
}
free(gamma);
struct atomic atom;
atomic_begin(crtc, &atom);
atomic_add(&atom, crtc->id, crtc->props.gamma_lut, crtc->gamma_lut);
return atomic_end(drm->fd, 0, &atom);
}
static size_t atomic_crtc_get_gamma_size(struct wlr_drm_backend *drm, static size_t atomic_crtc_get_gamma_size(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc) { struct wlr_drm_crtc *crtc) {
if (crtc->props.gamma_lut_size == 0) { if (crtc->props.gamma_lut_size == 0) {
@ -254,6 +269,5 @@ const struct wlr_drm_interface atomic_iface = {
.conn_enable = atomic_conn_enable, .conn_enable = atomic_conn_enable,
.crtc_pageflip = atomic_crtc_pageflip, .crtc_pageflip = atomic_crtc_pageflip,
.crtc_set_cursor = atomic_crtc_set_cursor, .crtc_set_cursor = atomic_crtc_set_cursor,
.crtc_set_gamma = atomic_crtc_set_gamma,
.crtc_get_gamma_size = atomic_crtc_get_gamma_size, .crtc_get_gamma_size = atomic_crtc_get_gamma_size,
}; };

View File

@ -93,6 +93,7 @@ static void session_signal(struct wl_listener *listener, void *data) {
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){
conn->crtc->pending |= WLR_DRM_CRTC_GAMMA_LUT;
if (conn->output.enabled && conn->output.current_mode != NULL) { if (conn->output.enabled && conn->output.current_mode != NULL) {
drm_connector_set_mode(&conn->output, drm_connector_set_mode(&conn->output,
conn->output.current_mode); conn->output.current_mode);
@ -112,16 +113,6 @@ static void session_signal(struct wl_listener *listener, void *data) {
} }
drm->iface->crtc_set_cursor(drm, conn->crtc, bo); drm->iface->crtc_set_cursor(drm, conn->crtc, bo);
if (conn->crtc->gamma_table != NULL) {
size_t size = conn->crtc->gamma_table_size;
uint16_t *r = conn->crtc->gamma_table;
uint16_t *g = conn->crtc->gamma_table + size;
uint16_t *b = conn->crtc->gamma_table + 2 * size;
drm->iface->crtc_set_gamma(drm, conn->crtc, size, r, g, b);
} else {
set_drm_connector_gamma(&conn->output, 0, NULL, NULL, NULL);
}
} }
} else { } else {
wlr_log(WLR_INFO, "DRM fd paused"); wlr_log(WLR_INFO, "DRM fd paused");

View File

@ -586,7 +586,6 @@ static size_t drm_connector_get_gamma_size(struct wlr_output *output) {
bool set_drm_connector_gamma(struct wlr_output *output, size_t size, bool set_drm_connector_gamma(struct wlr_output *output, size_t size,
const uint16_t *r, const uint16_t *g, const uint16_t *b) { const uint16_t *r, const uint16_t *g, const uint16_t *b) {
struct wlr_drm_connector *conn = get_drm_connector_from_output(output); struct wlr_drm_connector *conn = get_drm_connector_from_output(output);
struct wlr_drm_backend *drm = get_drm_backend_from_backend(output->backend);
if (!conn->crtc) { if (!conn->crtc) {
return false; return false;
@ -618,17 +617,13 @@ bool set_drm_connector_gamma(struct wlr_output *output, size_t size,
memcpy(_b, b, size * sizeof(uint16_t)); memcpy(_b, b, size * sizeof(uint16_t));
} }
bool ok = drm->iface->crtc_set_gamma(drm, conn->crtc, size, _r, _g, _b); conn->crtc->pending |= WLR_DRM_CRTC_GAMMA_LUT;
if (ok) {
wlr_output_update_needs_frame(output);
free(conn->crtc->gamma_table); free(conn->crtc->gamma_table);
conn->crtc->gamma_table = gamma_table; conn->crtc->gamma_table = gamma_table;
conn->crtc->gamma_table_size = size; conn->crtc->gamma_table_size = size;
} else {
free(gamma_table); wlr_output_update_needs_frame(output);
} return true; // will be applied on next page-flip
return ok;
} }
static bool drm_connector_export_dmabuf(struct wlr_output *output, static bool drm_connector_export_dmabuf(struct wlr_output *output,

View File

@ -30,6 +30,12 @@ static bool legacy_crtc_pageflip(struct wlr_drm_backend *drm,
} }
} }
if (crtc->pending & WLR_DRM_CRTC_GAMMA_LUT) {
if (!drm_legacy_crtc_set_gamma(drm, crtc)) {
return false;
}
}
if (cursor != NULL && cursor->cursor_enabled && drmModeMoveCursor(drm->fd, if (cursor != NULL && cursor->cursor_enabled && drmModeMoveCursor(drm->fd,
crtc->id, conn->cursor_x, conn->cursor_y) != 0) { crtc->id, conn->cursor_x, conn->cursor_y) != 0) {
wlr_log_errno(WLR_ERROR, "%s: failed to move cursor", conn->output.name); wlr_log_errno(WLR_ERROR, "%s: failed to move cursor", conn->output.name);
@ -83,10 +89,22 @@ bool legacy_crtc_set_cursor(struct wlr_drm_backend *drm,
return true; return true;
} }
static bool legacy_crtc_set_gamma(struct wlr_drm_backend *drm, bool drm_legacy_crtc_set_gamma(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, size_t size, struct wlr_drm_crtc *crtc) {
uint16_t *r, uint16_t *g, uint16_t *b) { uint32_t size = crtc->gamma_table_size;
return !drmModeCrtcSetGamma(drm->fd, crtc->id, (uint32_t)size, r, g, b); uint16_t *r = NULL, *g = NULL, *b = NULL;
if (size > 0) {
r = crtc->gamma_table;
g = crtc->gamma_table + crtc->gamma_table_size;
b = crtc->gamma_table + 2 * crtc->gamma_table_size;
}
if (drmModeCrtcSetGamma(drm->fd, crtc->id, size, r, g, b) != 0) {
wlr_log_errno(WLR_ERROR, "Failed to set gamma LUT on CRTC %"PRIu32,
crtc->id);
return false;
}
return true;
} }
static size_t legacy_crtc_get_gamma_size(struct wlr_drm_backend *drm, static size_t legacy_crtc_get_gamma_size(struct wlr_drm_backend *drm,
@ -98,6 +116,5 @@ const struct wlr_drm_interface legacy_iface = {
.conn_enable = legacy_conn_enable, .conn_enable = legacy_conn_enable,
.crtc_pageflip = legacy_crtc_pageflip, .crtc_pageflip = legacy_crtc_pageflip,
.crtc_set_cursor = legacy_crtc_set_cursor, .crtc_set_cursor = legacy_crtc_set_cursor,
.crtc_set_gamma = legacy_crtc_set_gamma,
.crtc_get_gamma_size = legacy_crtc_get_gamma_size, .crtc_get_gamma_size = legacy_crtc_get_gamma_size,
}; };

View File

@ -44,6 +44,7 @@ struct wlr_drm_plane {
enum wlr_drm_crtc_field { enum wlr_drm_crtc_field {
WLR_DRM_CRTC_MODE = 1 << 0, WLR_DRM_CRTC_MODE = 1 << 0,
WLR_DRM_CRTC_GAMMA_LUT = 1 << 1,
}; };
struct wlr_drm_crtc { struct wlr_drm_crtc {

View File

@ -22,10 +22,6 @@ struct wlr_drm_interface {
// Enable the cursor buffer on crtc. Set bo to NULL to disable // Enable the cursor buffer on crtc. Set bo to NULL to disable
bool (*crtc_set_cursor)(struct wlr_drm_backend *drm, bool (*crtc_set_cursor)(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, struct gbm_bo *bo); struct wlr_drm_crtc *crtc, struct gbm_bo *bo);
// Set the gamma lut on crtc
bool (*crtc_set_gamma)(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, size_t size,
uint16_t *r, uint16_t *g, uint16_t *b);
// Get the gamma lut size of a crtc // Get the gamma lut size of a crtc
size_t (*crtc_get_gamma_size)(struct wlr_drm_backend *drm, size_t (*crtc_get_gamma_size)(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc); struct wlr_drm_crtc *crtc);
@ -34,4 +30,7 @@ struct wlr_drm_interface {
extern const struct wlr_drm_interface atomic_iface; extern const struct wlr_drm_interface atomic_iface;
extern const struct wlr_drm_interface legacy_iface; extern const struct wlr_drm_interface legacy_iface;
bool drm_legacy_crtc_set_gamma(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc);
#endif #endif