output: make wlr_output_set_gamma atomic

wlr_output_set_gamma is now double-buffered and applies the gamma LUT on
the next output commit.
This commit is contained in:
Simon Ser 2020-05-14 18:27:02 +02:00 committed by Drew DeVault
parent 7693fdb8a7
commit 347bdb6d9a
9 changed files with 100 additions and 105 deletions

View file

@ -71,23 +71,21 @@ static bool create_mode_blob(struct wlr_drm_backend *drm,
}
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) {
size_t size, const uint16_t *lut, uint32_t *blob_id) {
if (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;
}
const uint16_t *r = lut;
const uint16_t *g = lut + size;
const uint16_t *b = lut + 2 * size;
for (size_t i = 0; i < size; i++) {
gamma[i].red = r[i];
gamma[i].green = g[i];
@ -96,7 +94,7 @@ static bool create_gamma_lut_blob(struct wlr_drm_backend *drm,
if (drmModeCreatePropertyBlob(drm->fd, gamma,
size * sizeof(struct drm_color_lut), blob_id) != 0) {
wlr_log_errno(WLR_ERROR, "Unable to create property blob");
wlr_log_errno(WLR_ERROR, "Unable to create gamma LUT property blob");
free(gamma);
return false;
}
@ -148,6 +146,7 @@ error:
static bool atomic_crtc_commit(struct wlr_drm_backend *drm,
struct wlr_drm_connector *conn, uint32_t flags) {
struct wlr_output *output = &conn->output;
struct wlr_drm_crtc *crtc = conn->crtc;
if (crtc->pending & WLR_DRM_CRTC_MODE) {
@ -160,11 +159,14 @@ static bool atomic_crtc_commit(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 (output->pending.committed & WLR_OUTPUT_STATE_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)) {
if (!drm_legacy_crtc_set_gamma(drm, crtc,
output->pending.gamma_lut_size,
output->pending.gamma_lut)) {
return false;
}
} else {
@ -172,7 +174,9 @@ static bool atomic_crtc_commit(struct wlr_drm_backend *drm,
drmModeDestroyPropertyBlob(drm->fd, crtc->gamma_lut);
}
if (!create_gamma_lut_blob(drm, crtc, &crtc->gamma_lut)) {
wlr_log(WLR_ERROR, "setting gamma LUT %zu", output->pending.gamma_lut_size);
if (!create_gamma_lut_blob(drm, output->pending.gamma_lut_size,
output->pending.gamma_lut, &crtc->gamma_lut)) {
return false;
}
}

View file

@ -305,8 +305,6 @@ void finish_drm_resources(struct wlr_drm_backend *drm) {
drmModeDestroyPropertyBlob(drm->fd, crtc->gamma_lut);
}
free(crtc->gamma_table);
if (crtc->primary) {
wlr_drm_format_set_finish(&crtc->primary->formats);
free(crtc->primary);
@ -568,24 +566,8 @@ static void drm_connector_rollback(struct wlr_output *output) {
wlr_egl_make_current(&drm->renderer.egl, EGL_NO_SURFACE, NULL);
}
static void fill_empty_gamma_table(size_t size,
uint16_t *r, uint16_t *g, uint16_t *b) {
assert(0xFFFF < UINT64_MAX / (size - 1));
for (uint32_t i = 0; i < size; ++i) {
uint16_t val = (uint64_t)0xffff * i / (size - 1);
r[i] = g[i] = b[i] = val;
}
}
static size_t drm_connector_get_gamma_size(struct wlr_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);
struct wlr_drm_crtc *crtc = conn->crtc;
if (crtc == NULL) {
return 0;
}
size_t drm_crtc_get_gamma_lut_size(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc) {
if (crtc->props.gamma_lut_size == 0) {
return (size_t)crtc->legacy_crtc->gamma_size;
}
@ -600,47 +582,16 @@ static size_t drm_connector_get_gamma_size(struct wlr_output *output) {
return gamma_lut_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) {
static size_t drm_connector_get_gamma_size(struct wlr_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);
struct wlr_drm_crtc *crtc = conn->crtc;
if (!conn->crtc) {
return false;
if (crtc == NULL) {
return 0;
}
bool reset = false;
if (size == 0) {
reset = true;
size = drm_connector_get_gamma_size(output);
if (size == 0) {
return false;
}
}
uint16_t *gamma_table = malloc(3 * size * sizeof(uint16_t));
if (gamma_table == NULL) {
wlr_log(WLR_ERROR, "Failed to allocate gamma table");
return false;
}
uint16_t *_r = gamma_table;
uint16_t *_g = gamma_table + size;
uint16_t *_b = gamma_table + 2 * size;
if (reset) {
fill_empty_gamma_table(size, _r, _g, _b);
} else {
memcpy(_r, r, size * sizeof(uint16_t));
memcpy(_g, g, size * sizeof(uint16_t));
memcpy(_b, b, size * sizeof(uint16_t));
}
conn->crtc->pending |= WLR_DRM_CRTC_GAMMA_LUT;
free(conn->crtc->gamma_table);
conn->crtc->gamma_table = gamma_table;
conn->crtc->gamma_table_size = size;
wlr_output_update_needs_frame(output);
return true; // will be applied on next page-flip
return drm_crtc_get_gamma_lut_size(drm, crtc);
}
static bool drm_connector_export_dmabuf(struct wlr_output *output,
@ -1082,7 +1033,6 @@ static const struct wlr_output_impl output_impl = {
.test = drm_connector_test,
.commit = drm_connector_commit,
.rollback = drm_connector_rollback,
.set_gamma = set_drm_connector_gamma,
.get_gamma_size = drm_connector_get_gamma_size,
.export_dmabuf = drm_connector_export_dmabuf,
};
@ -1117,7 +1067,6 @@ static void dealloc_crtc(struct wlr_drm_connector *conn) {
wlr_log(WLR_DEBUG, "De-allocating CRTC %zu for output '%s'",
conn->crtc - drm->crtcs, conn->output.name);
set_drm_connector_gamma(&conn->output, 0, NULL, NULL, NULL);
enable_drm_connector(&conn->output, false);
drm_plane_finish_surface(conn->crtc->primary);
drm_plane_finish_surface(conn->crtc->cursor);

View file

@ -1,4 +1,6 @@
#include <assert.h>
#include <gbm.h>
#include <stdlib.h>
#include <wlr/util/log.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
@ -8,6 +10,7 @@
static bool legacy_crtc_commit(struct wlr_drm_backend *drm,
struct wlr_drm_connector *conn, uint32_t flags) {
struct wlr_output *output = &conn->output;
struct wlr_drm_crtc *crtc = conn->crtc;
struct wlr_drm_plane *cursor = crtc->cursor;
@ -50,8 +53,9 @@ static bool legacy_crtc_commit(struct wlr_drm_backend *drm,
}
}
if (crtc->pending & WLR_DRM_CRTC_GAMMA_LUT) {
if (!drm_legacy_crtc_set_gamma(drm, crtc)) {
if (output->pending.committed & WLR_OUTPUT_STATE_GAMMA_LUT) {
if (!drm_legacy_crtc_set_gamma(drm, crtc,
output->pending.gamma_lut_size, output->pending.gamma_lut)) {
return false;
}
}
@ -99,21 +103,44 @@ static bool legacy_crtc_commit(struct wlr_drm_backend *drm,
return true;
}
static void fill_empty_gamma_table(size_t size,
uint16_t *r, uint16_t *g, uint16_t *b) {
assert(0xFFFF < UINT64_MAX / (size - 1));
for (uint32_t i = 0; i < size; ++i) {
uint16_t val = (uint64_t)0xFFFF * i / (size - 1);
r[i] = g[i] = b[i] = val;
}
}
bool drm_legacy_crtc_set_gamma(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc) {
uint32_t size = crtc->gamma_table_size;
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;
struct wlr_drm_crtc *crtc, size_t size, uint16_t *lut) {
uint16_t *linear_lut = NULL;
if (size == 0) {
// The legacy interface doesn't offer a way to reset the gamma LUT
size = drm_crtc_get_gamma_lut_size(drm, crtc);
if (size == 0) {
return false;
}
linear_lut = malloc(3 * size * sizeof(uint16_t));
if (linear_lut == NULL) {
wlr_log_errno(WLR_ERROR, "Allocation failed");
return false;
}
fill_empty_gamma_table(size, linear_lut, linear_lut + size,
linear_lut + 2 * size);
lut = linear_lut;
}
uint16_t *r = lut, *g = lut + size, *b = lut + 2 * 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;
}
free(linear_lut);
return true;
}