From abdd8c54f375fefbdf093f65ed01ef66e0689374 Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Sat, 18 Feb 2023 18:32:24 +0100 Subject: [PATCH] backends/native: Handle gamma sizes independent of the KMS LUT Allows for creating LUTs at some fixed size which maintains enough precision for concatenating or otherwise manipulating the LUT without having to care about the precision of the hardware. Part-of: --- src/backends/meta-crtc.c | 58 +++++++++++++++++++++++++++ src/backends/meta-crtc.h | 3 ++ src/backends/native/meta-crtc-kms.c | 14 ------- src/backends/native/meta-kms-update.c | 3 +- 4 files changed, 63 insertions(+), 15 deletions(-) diff --git a/src/backends/meta-crtc.c b/src/backends/meta-crtc.c index f3b1549c8..33d03ac39 100644 --- a/src/backends/meta-crtc.c +++ b/src/backends/meta-crtc.c @@ -218,6 +218,64 @@ meta_gamma_lut_copy (const MetaGammaLut *gamma) return meta_gamma_lut_new (gamma->size, gamma->red, gamma->green, gamma->blue); } +MetaGammaLut * +meta_gamma_lut_copy_to_size (const MetaGammaLut *gamma, + int target_size) +{ + MetaGammaLut *out; + + g_return_val_if_fail (gamma != NULL, NULL); + + if (gamma->size == target_size) + return meta_gamma_lut_copy (gamma); + + out = meta_gamma_lut_new (target_size, NULL, NULL, NULL); + + out->red = g_new0 (uint16_t, target_size); + out->green = g_new0 (uint16_t, target_size); + out->blue = g_new0 (uint16_t, target_size); + + if (target_size >= gamma->size) + { + int i, j; + int slots; + + slots = target_size / gamma->size; + for (i = 0; i < gamma->size; i++) + { + for (j = 0; j < slots; j++) + { + out->red[i * slots + j] = gamma->red[i]; + out->green[i * slots + j] = gamma->green[i]; + out->blue[i * slots + j] = gamma->blue[i]; + } + } + + for (j = i * slots; j < target_size; j++) + { + out->red[j] = gamma->red[i - 1]; + out->green[j] = gamma->green[i - 1]; + out->blue[j] = gamma->blue[i - 1]; + } + } + else + { + int i; + int idx; + + for (i = 0; i < target_size; i++) + { + idx = i * (gamma->size - 1) / (target_size - 1); + + out->red[i] = gamma->red[idx]; + out->green[i] = gamma->green[idx]; + out->blue[i] = gamma->blue[idx]; + } + } + + return out; +} + gboolean meta_gamma_lut_equal (const MetaGammaLut *gamma, const MetaGammaLut *other_gamma) diff --git a/src/backends/meta-crtc.h b/src/backends/meta-crtc.h index 06494f9a9..77b4b6bc5 100644 --- a/src/backends/meta-crtc.h +++ b/src/backends/meta-crtc.h @@ -104,6 +104,9 @@ MetaGammaLut * meta_gamma_lut_new_sized (int size); META_EXPORT_TEST MetaGammaLut * meta_gamma_lut_copy (const MetaGammaLut *gamma); +MetaGammaLut * meta_gamma_lut_copy_to_size (const MetaGammaLut *gamma, + int target_size); + META_EXPORT_TEST gboolean meta_gamma_lut_equal (const MetaGammaLut *gamma, const MetaGammaLut *other_gamma); diff --git a/src/backends/native/meta-crtc-kms.c b/src/backends/native/meta-crtc-kms.c index 0791d6c7b..09224b52a 100644 --- a/src/backends/native/meta-crtc-kms.c +++ b/src/backends/native/meta-crtc-kms.c @@ -206,27 +206,13 @@ meta_crtc_kms_set_gamma_lut (MetaCrtc *crtc, const MetaGammaLut *lut) { MetaCrtcKms *crtc_kms = META_CRTC_KMS (crtc); - MetaKmsCrtc *kms_crtc = meta_crtc_kms_get_kms_crtc (crtc_kms); MetaBackend *backend = meta_gpu_get_backend (meta_crtc_get_gpu (crtc)); MetaMonitorManagerNative *monitor_manager_native = monitor_manager_from_crtc (crtc); ClutterActor *stage = meta_backend_get_stage (backend); - const MetaKmsCrtcState *crtc_state; g_autofree char *gamma_ramp_string = NULL; MetaGammaLut *new_gamma; - crtc_state = meta_kms_crtc_get_current_state (kms_crtc); - - if (lut->size != crtc_state->gamma.size) - { - MetaKmsDevice *kms_device = meta_kms_crtc_get_device (kms_crtc); - - g_warning ("Tried to set a different gamma LUT size on %u (%s)", - meta_kms_crtc_get_id (kms_crtc), - meta_kms_device_get_path (kms_device)); - return; - } - gamma_ramp_string = generate_gamma_ramp_string (lut); meta_topic (META_DEBUG_COLOR, "Setting CRTC (%" G_GUINT64_FORMAT ") gamma to %s", diff --git a/src/backends/native/meta-kms-update.c b/src/backends/native/meta-kms-update.c index d636d020d..fe2dee8a3 100644 --- a/src/backends/native/meta-kms-update.c +++ b/src/backends/native/meta-kms-update.c @@ -401,12 +401,13 @@ meta_kms_update_set_crtc_gamma (MetaKmsUpdate *update, { MetaKmsCrtcColorUpdate *color_update; MetaGammaLut *gamma_update = NULL; + const MetaKmsCrtcState *crtc_state = meta_kms_crtc_get_current_state (crtc); g_assert (!meta_kms_update_is_locked (update)); g_assert (meta_kms_crtc_get_device (crtc) == update->device); if (gamma) - gamma_update = meta_gamma_lut_copy (gamma); + gamma_update = meta_gamma_lut_copy_to_size (gamma, crtc_state->gamma.size); color_update = ensure_color_update (update, crtc); color_update->gamma.state = gamma_update;