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;
}

View File

@ -44,7 +44,6 @@ struct wlr_drm_plane {
enum wlr_drm_crtc_field {
WLR_DRM_CRTC_MODE = 1 << 0,
WLR_DRM_CRTC_GAMMA_LUT = 1 << 1,
};
struct wlr_drm_crtc {
@ -72,9 +71,6 @@ struct wlr_drm_crtc {
uint32_t *overlays;
union wlr_drm_crtc_props props;
uint16_t *gamma_table;
size_t gamma_table_size;
};
struct wlr_drm_backend {
@ -156,16 +152,11 @@ void restore_drm_outputs(struct wlr_drm_backend *drm);
void scan_drm_connectors(struct wlr_drm_backend *state);
int handle_drm_event(int fd, uint32_t mask, void *data);
bool enable_drm_connector(struct wlr_output *output, bool enable);
bool set_drm_connector_gamma(struct wlr_output *output, size_t size,
const uint16_t *r, const uint16_t *g, const uint16_t *b);
bool drm_connector_set_mode(struct wlr_output *output,
struct wlr_output_mode *mode);
size_t drm_crtc_get_gamma_lut_size(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc);
struct wlr_drm_fb *plane_get_next_fb(struct wlr_drm_plane *plane);
bool legacy_crtc_set_cursor(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, struct gbm_bo *bo);
bool legacy_crtc_move_cursor(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, int x, int y);
#endif

View File

@ -22,6 +22,6 @@ extern const struct wlr_drm_interface atomic_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);
struct wlr_drm_crtc *crtc, size_t size, uint16_t *lut);
#endif

View File

@ -24,8 +24,6 @@ struct wlr_output_impl {
bool (*test)(struct wlr_output *output);
bool (*commit)(struct wlr_output *output);
void (*rollback)(struct wlr_output *output);
bool (*set_gamma)(struct wlr_output *output, size_t size,
const uint16_t *r, const uint16_t *g, const uint16_t *b);
size_t (*get_gamma_size)(struct wlr_output *output);
bool (*export_dmabuf)(struct wlr_output *output,
struct wlr_dmabuf_attributes *attribs);

View File

@ -60,6 +60,7 @@ enum wlr_output_state_field {
WLR_OUTPUT_STATE_SCALE = 1 << 4,
WLR_OUTPUT_STATE_TRANSFORM = 1 << 5,
WLR_OUTPUT_STATE_ADAPTIVE_SYNC_ENABLED = 1 << 6,
WLR_OUTPUT_STATE_GAMMA_LUT = 1 << 7,
};
enum wlr_output_state_buffer_type {
@ -94,6 +95,10 @@ struct wlr_output_state {
int32_t width, height;
int32_t refresh; // mHz, may be zero
} custom_mode;
// only valid if WLR_OUTPUT_STATE_GAMMA_LUT
uint16_t *gamma_lut;
size_t gamma_lut_size;
};
struct wlr_output_impl;
@ -375,8 +380,10 @@ size_t wlr_output_get_gamma_size(struct wlr_output *output);
* the value returned by `wlr_output_get_gamma_size`.
*
* Providing zero-sized ramps resets the gamma table.
*
* The gamma table is double-buffered state, see `wlr_output_commit`.
*/
bool wlr_output_set_gamma(struct wlr_output *output, size_t size,
void wlr_output_set_gamma(struct wlr_output *output, size_t size,
const uint16_t *r, const uint16_t *g, const uint16_t *b);
bool wlr_output_export_dmabuf(struct wlr_output *output,
struct wlr_dmabuf_attributes *attribs);

View File

@ -105,13 +105,16 @@ static void gamma_control_handle_set_gamma(struct wl_client *client,
uint16_t *g = table + ramp_size;
uint16_t *b = table + 2 * ramp_size;
bool ok = wlr_output_set_gamma(gamma_control->output, ramp_size, r, g, b);
if (!ok) {
wlr_output_set_gamma(gamma_control->output, ramp_size, r, g, b);
if (!wlr_output_test(gamma_control->output)) {
gamma_control_send_failed(gamma_control);
goto error_table;
}
free(table);
// Gamma LUT will be applied on next output commit
wlr_output_schedule_frame(gamma_control->output);
return;
error_table:
@ -175,7 +178,8 @@ static void gamma_control_manager_get_gamma_control(struct wl_client *client,
wl_list_init(&gamma_control->link);
if (!output->impl->set_gamma) {
size_t gamma_size = wlr_output_get_gamma_size(output);
if (gamma_size == 0) {
zwlr_gamma_control_v1_send_failed(gamma_control->resource);
gamma_control_destroy(gamma_control);
return;
@ -192,8 +196,7 @@ static void gamma_control_manager_get_gamma_control(struct wl_client *client,
wl_list_remove(&gamma_control->link);
wl_list_insert(&manager->controls, &gamma_control->link);
zwlr_gamma_control_v1_send_gamma_size(gamma_control->resource,
wlr_output_get_gamma_size(output));
zwlr_gamma_control_v1_send_gamma_size(gamma_control->resource, gamma_size);
}
static void gamma_control_manager_destroy(struct wl_client *client,

View File

@ -467,8 +467,15 @@ void wlr_output_set_damage(struct wlr_output *output,
output->pending.committed |= WLR_OUTPUT_STATE_DAMAGE;
}
static void output_state_clear_gamma_lut(struct wlr_output_state *state) {
free(state->gamma_lut);
state->gamma_lut = NULL;
state->committed &= ~WLR_OUTPUT_STATE_GAMMA_LUT;
}
static void output_state_clear(struct wlr_output_state *state) {
output_state_clear_buffer(state);
output_state_clear_gamma_lut(state);
pixman_region32_clear(&state->damage);
state->committed = 0;
}
@ -701,12 +708,21 @@ void wlr_output_send_present(struct wlr_output *output,
wlr_signal_emit_safe(&output->events.present, event);
}
bool wlr_output_set_gamma(struct wlr_output *output, size_t size,
void wlr_output_set_gamma(struct wlr_output *output, size_t size,
const uint16_t *r, const uint16_t *g, const uint16_t *b) {
if (!output->impl->set_gamma) {
return false;
output_state_clear_gamma_lut(&output->pending);
output->pending.gamma_lut_size = size;
output->pending.gamma_lut = malloc(3 * size * sizeof(uint16_t));
if (output->pending.gamma_lut == NULL) {
wlr_log_errno(WLR_ERROR, "Allocation failed");
return;
}
return output->impl->set_gamma(output, size, r, g, b);
memcpy(output->pending.gamma_lut, r, size * sizeof(uint16_t));
memcpy(output->pending.gamma_lut + size, g, size * sizeof(uint16_t));
memcpy(output->pending.gamma_lut + 2 * size, b, size * sizeof(uint16_t));
output->pending.committed |= WLR_OUTPUT_STATE_GAMMA_LUT;
}
size_t wlr_output_get_gamma_size(struct wlr_output *output) {