From 0c3653d15946f7413ef26b17ef7a0895202def38 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Tue, 3 Jan 2023 12:14:15 +0000 Subject: [PATCH] color_helpers: Handle naughty clients/EDIDs with more grace Avoid asserting and simply clamping to deal with broken EDIDs like on LG OLED panels for now. --- src/color_helpers.h | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/color_helpers.h b/src/color_helpers.h index b5e05d6..aa6e3a1 100644 --- a/src/color_helpers.h +++ b/src/color_helpers.h @@ -2,27 +2,35 @@ #include #include +#include // Colormetry helper functions for DRM, kindly taken from Weston: // https://gitlab.freedesktop.org/wayland/weston/-/blob/main/libweston/backend-drm/kms-color.c // Licensed under MIT. +// Josh: I changed the asserts to clamps here (going to 0, rather than 1) to deal better with +// bad EDIDs (that have 0'ed out metadata) and naughty clients. + static inline uint16_t color_xy_to_u16(float v) { - assert(v >= 0.0f); - assert(v <= 1.0f); + //assert(v >= 0.0f); + //assert(v <= 1.0f); + v = std::clamp(v, 0.0f, 1.0f); + // CTA-861-G // 6.9.1 Static Metadata Type 1 // chromaticity coordinate encoding - return (uint16_t)round(v * 50000.0); + return (uint16_t)round(v * 50000.0f); } static inline uint16_t nits_to_u16(float nits) { - assert(nits >= 1.0f); - assert(nits <= 65535.0f); + //assert(nits >= 1.0f); + //assert(nits <= 65535.0f); + nits = std::clamp(nits, 0.0f, 65535.0f); + // CTA-861-G // 6.9.1 Static Metadata Type 1 // max display mastering luminance, max content light level, @@ -33,10 +41,12 @@ nits_to_u16(float nits) static inline uint16_t nits_to_u16_dark(float nits) { - assert(nits >= 0.0001f); - assert(nits <= 6.5535f); + //assert(nits >= 0.0001f); + //assert(nits <= 6.5535f); + nits = std::clamp(nits, 0.0f, 6.5535f); + // CTA-861-G // 6.9.1 Static Metadata Type 1 // min display mastering luminance - return (uint16_t)round(nits * 10000.0); + return (uint16_t)round(nits * 10000.0f); }