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.
This commit is contained in:
Joshua Ashton 2023-01-03 12:14:15 +00:00
parent d2cc74c570
commit 0c3653d159

View file

@ -2,27 +2,35 @@
#include <cstdint> #include <cstdint>
#include <cmath> #include <cmath>
#include <algorithm>
// Colormetry helper functions for DRM, kindly taken from Weston: // Colormetry helper functions for DRM, kindly taken from Weston:
// https://gitlab.freedesktop.org/wayland/weston/-/blob/main/libweston/backend-drm/kms-color.c // https://gitlab.freedesktop.org/wayland/weston/-/blob/main/libweston/backend-drm/kms-color.c
// Licensed under MIT. // 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 static inline uint16_t
color_xy_to_u16(float v) color_xy_to_u16(float v)
{ {
assert(v >= 0.0f); //assert(v >= 0.0f);
assert(v <= 1.0f); //assert(v <= 1.0f);
v = std::clamp(v, 0.0f, 1.0f);
// CTA-861-G // CTA-861-G
// 6.9.1 Static Metadata Type 1 // 6.9.1 Static Metadata Type 1
// chromaticity coordinate encoding // chromaticity coordinate encoding
return (uint16_t)round(v * 50000.0); return (uint16_t)round(v * 50000.0f);
} }
static inline uint16_t static inline uint16_t
nits_to_u16(float nits) nits_to_u16(float nits)
{ {
assert(nits >= 1.0f); //assert(nits >= 1.0f);
assert(nits <= 65535.0f); //assert(nits <= 65535.0f);
nits = std::clamp(nits, 0.0f, 65535.0f);
// CTA-861-G // CTA-861-G
// 6.9.1 Static Metadata Type 1 // 6.9.1 Static Metadata Type 1
// max display mastering luminance, max content light level, // max display mastering luminance, max content light level,
@ -33,10 +41,12 @@ nits_to_u16(float nits)
static inline uint16_t static inline uint16_t
nits_to_u16_dark(float nits) nits_to_u16_dark(float nits)
{ {
assert(nits >= 0.0001f); //assert(nits >= 0.0001f);
assert(nits <= 6.5535f); //assert(nits <= 6.5535f);
nits = std::clamp(nits, 0.0f, 6.5535f);
// CTA-861-G // CTA-861-G
// 6.9.1 Static Metadata Type 1 // 6.9.1 Static Metadata Type 1
// min display mastering luminance // min display mastering luminance
return (uint16_t)round(nits * 10000.0); return (uint16_t)round(nits * 10000.0f);
} }