diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-08-02 18:48:09 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-02 18:48:09 -0400 |
commit | 1654fc80ebdc4c69d86cbb700ceeb49b8f090c62 (patch) | |
tree | 9973c182ff78d3758e9487166ca55dc58d006e86 /backend | |
parent | 0780d7856a1ec2b6f3bcf2837cd5f697556c531e (diff) | |
parent | c3afe4f42e3ddfbd51500d99416b51aee552b689 (diff) |
Merge pull request #1157 from emersion/wlr-gamma-control
Implement wlr-gamma-control-unstable-v1
Diffstat (limited to 'backend')
-rw-r--r-- | backend/drm/atomic.c | 12 | ||||
-rw-r--r-- | backend/drm/drm.c | 46 |
2 files changed, 44 insertions, 14 deletions
diff --git a/backend/drm/atomic.c b/backend/drm/atomic.c index 61d2f6e1..40a272a1 100644 --- a/backend/drm/atomic.c +++ b/backend/drm/atomic.c @@ -199,14 +199,18 @@ static bool atomic_crtc_move_cursor(struct wlr_drm_backend *drm, static bool atomic_crtc_set_gamma(struct wlr_drm_backend *drm, struct wlr_drm_crtc *crtc, uint16_t *r, uint16_t *g, uint16_t *b, uint32_t size) { - struct drm_color_lut gamma[size]; - // Fallback to legacy gamma interface when gamma properties are not available // (can happen on older intel gpu's that support gamma but not degamma) if (crtc->props.gamma_lut == 0) { return legacy_iface.crtc_set_gamma(drm, crtc, r, g, b, 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 (uint32_t i = 0; i < size; i++) { gamma[i].red = r[i]; gamma[i].green = g[i]; @@ -218,10 +222,12 @@ static bool atomic_crtc_set_gamma(struct wlr_drm_backend *drm, } if (drmModeCreatePropertyBlob(drm->fd, gamma, - sizeof(struct drm_color_lut) * size, &crtc->gamma_lut)) { + 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); diff --git a/backend/drm/drm.c b/backend/drm/drm.c index c4674235..9d030dff 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -228,30 +228,54 @@ static bool drm_connector_swap_buffers(struct wlr_output *output, return true; } -static void drm_connector_set_gamma(struct wlr_output *output, - uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b) { +static void fill_empty_gamma_table(uint32_t size, + uint16_t *r, uint16_t *g, uint16_t *b) { + for (uint32_t i = 0; i < size; ++i) { + uint16_t val = (uint32_t)0xffff * i / (size - 1); + r[i] = g[i] = b[i] = val; + } +} + +static uint32_t drm_connector_get_gamma_size(struct wlr_output *output) { struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; struct wlr_drm_backend *drm = (struct wlr_drm_backend *)output->backend; - bool ok; if (conn->crtc) { - ok = drm->iface->crtc_set_gamma(drm, conn->crtc, r, g, b, size); - if (ok) { - wlr_output_update_needs_swap(output); - } + return drm->iface->crtc_get_gamma_size(drm, conn->crtc); } + return 0; } -static uint32_t drm_connector_get_gamma_size(struct wlr_output *output) { +static bool drm_connector_set_gamma(struct wlr_output *output, + uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b) { struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; struct wlr_drm_backend *drm = (struct wlr_drm_backend *)output->backend; - if (conn->crtc) { - return drm->iface->crtc_get_gamma_size(drm, conn->crtc); + if (!conn->crtc) { + return false; } - return 0; + uint16_t *reset_table = NULL; + if (size == 0) { + size = drm_connector_get_gamma_size(output); + reset_table = malloc(3 * size * sizeof(uint16_t)); + if (reset_table == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate gamma table"); + return false; + } + r = reset_table; + g = reset_table + size; + b = reset_table + 2 * size; + fill_empty_gamma_table(size, r, g, b); + } + + bool ok = drm->iface->crtc_set_gamma(drm, conn->crtc, r, g, b, size); + if (ok) { + wlr_output_update_needs_swap(output); + } + free(reset_table); + return ok; } static bool drm_connector_export_dmabuf(struct wlr_output *output, |