Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 52 additions & 24 deletions drivers/gpu/drm/apple/dcp-color.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,10 @@ static inline bool dcp_color_space_matches(u8 colorimetry,
if (colorimetry == DCP_COLORIMETRY_BT2020_RGB)
return true;
/*
* Compositors (e.g. Mutter) request BT2020_RGB with AUTO format
* for HDR even on timings where only YCbCr BT.2020 is safe.
* Compositors (e.g. Mutter, Hyprland) request BT2020_RGB with
* AUTO format even on timings where only YCbCr BT.2020 is safe.
*/
return hdr &&
req->color_format == DRM_CONNECTOR_COLOR_FORMAT_AUTO &&
return req->color_format == DRM_CONNECTOR_COLOR_FORMAT_AUTO &&
colorimetry == DCP_COLORIMETRY_BT2020_YCC;
case DRM_MODE_COLORIMETRY_BT2020_YCC:
return colorimetry == DCP_COLORIMETRY_BT2020_YCC;
Expand Down Expand Up @@ -283,33 +282,16 @@ static inline int dcp_default_sdr_color(const struct dcp_display_mode *mode,
return -EINVAL;
}

static inline int dcp_select_color_mode(const struct dcp_display_mode *mode,
const struct dcp_color_request *req,
struct dcp_color_mode *out)
static inline const struct dcp_color_entry *
dcp_scan_color_modes(const struct dcp_display_mode *mode,
const struct dcp_color_request *req)
{
u8 max_bpc = req->max_bpc ? req->max_bpc : 12;
bool hdr = req->eotf != HDMI_EOTF_TRADITIONAL_GAMMA_SDR;
Comment on lines +285 to 290
unsigned int pass, max_pass, i;
const struct dcp_color_entry *best = NULL;
u32 best_rank = 0;

if (dcp_color_request_is_default(req)) {
if (req->eotf == HDMI_EOTF_TRADITIONAL_GAMMA_SDR &&
req->colorspace == DRM_MODE_COLORIMETRY_DEFAULT)
return dcp_default_sdr_color(mode, out);
if (req->eotf == HDMI_EOTF_SMPTE_ST2084 &&
(req->colorspace == DRM_MODE_COLORIMETRY_DEFAULT ||
req->colorspace == DRM_MODE_COLORIMETRY_BT2020_RGB)) {
if (!req->allow_hdr || mode->pq.score < 0)
return -EINVAL;
*out = mode->pq;
return 0;
}
}

if (hdr && !req->allow_hdr)
return -EINVAL;

max_pass = (req->allow_unsafe_sdr && !mode->sdr_usable && mode->sdr_unsafe) ? 2 : 1;
for (pass = 0; pass < max_pass && !best; pass++) {
bool allow_unsafe = pass > 0;
Expand Down Expand Up @@ -346,6 +328,52 @@ static inline int dcp_select_color_mode(const struct dcp_display_mode *mode,
}
}

return best;
}

static inline int dcp_select_color_mode(const struct dcp_display_mode *mode,
const struct dcp_color_request *req,
struct dcp_color_mode *out)
{
bool hdr = req->eotf != HDMI_EOTF_TRADITIONAL_GAMMA_SDR;
const struct dcp_color_entry *best;

if (dcp_color_request_is_default(req)) {
if (req->eotf == HDMI_EOTF_TRADITIONAL_GAMMA_SDR &&
req->colorspace == DRM_MODE_COLORIMETRY_DEFAULT)
return dcp_default_sdr_color(mode, out);
if (req->eotf == HDMI_EOTF_SMPTE_ST2084 &&
(req->colorspace == DRM_MODE_COLORIMETRY_DEFAULT ||
req->colorspace == DRM_MODE_COLORIMETRY_BT2020_RGB)) {
if (!req->allow_hdr || mode->pq.score < 0)
return -EINVAL;
*out = mode->pq;
return 0;
}
}

if (hdr && !req->allow_hdr)
return -EINVAL;

best = dcp_scan_color_modes(mode, req);
if (!best && !hdr &&
req->colorspace == DRM_MODE_COLORIMETRY_BT2020_RGB &&
req->color_format == DRM_CONNECTOR_COLOR_FORMAT_AUTO &&
req->broadcast_rgb == DRM_HDMI_BROADCAST_RGB_AUTO) {
/*
* Compositors (e.g. Hyprland's default cm=edid) set
* Colorspace=BT2020_RGB in SDR when the sink EDID advertises
* BT.2020, even on bandwidth-limited timings (such as 4K60
* HDMI 2.0) where only 8-bit sRGB/BT.601 YCbCr 4:2:0 is safe.
* Rejecting that commit with -EINVAL traps compositors in an
* unpaced modeset retry loop; fall back through the filtered
* catalogue so unsafe, EOTF, and max_bpc rules still apply.
Comment on lines +364 to +370
*/
struct dcp_color_request fallback = *req;

fallback.colorspace = DRM_MODE_COLORIMETRY_DEFAULT;
best = dcp_scan_color_modes(mode, &fallback);
}
if (!best)
return -EINVAL;
dcp_color_mode_from_entry(out, best);
Expand Down
5 changes: 3 additions & 2 deletions drivers/gpu/drm/apple/iomfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3778,9 +3778,10 @@ int dcp_crtc_check_hdr(struct apple_dcp *dcp, struct drm_crtc *crtc,
req.colorspace == DRM_MODE_COLORIMETRY_DEFAULT)
return 0;
if (!READ_ONCE(dcp->iomfb_template_pq) || dcp_is_ext26(dcp)) {
/* Fixed 8-bit SDR pipes satisfy any default SDR request with max_bpc >= 8. */
/* Fixed 8-bit SDR pipes satisfy default or auto BT.2020 SDR with max_bpc >= 8. */
if (req.eotf == HDMI_EOTF_TRADITIONAL_GAMMA_SDR &&
req.colorspace == DRM_MODE_COLORIMETRY_DEFAULT &&
(req.colorspace == DRM_MODE_COLORIMETRY_DEFAULT ||
req.colorspace == DRM_MODE_COLORIMETRY_BT2020_RGB) &&
req.color_format == DRM_CONNECTOR_COLOR_FORMAT_AUTO &&
req.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_AUTO &&
(!req.max_bpc || req.max_bpc >= 8))
Comment on lines +3781 to 3787
Expand Down
94 changes: 94 additions & 0 deletions tools/testing/selftests/drivers/gpu/apple-dcp/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,100 @@ def color_selection(root):
assert(!dcp_select_color_mode(&k4, &req, &out) && out.id == 119 && out.format == DCP_COLOR_FORMAT_YCBCR420);
assert(k4.colors[1].format == DCP_COLOR_FORMAT_YCBCR444);

/* SDR BT2020_RGB + AUTO matches safe SDR BT2020_YCC when no BT2020_RGB exists */
struct dcp_display_mode sdr_ycc = {
.color_mode_id = 123, .sdr_usable = true,
.sdr = { .score = 100, .id = 123, .eotf = DCP_EOTF_SDR_GAMMA,
.format = DCP_COLOR_FORMAT_YCBCR444, .colorimetry = DCP_COLORIMETRY_SRGB,
.range = DCP_COLOR_YCBCR_RANGE_LIMITED, .depth = 8 },
};
add_entry(&sdr_ycc, 123, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_SRGB, DCP_COLOR_YCBCR_RANGE_LIMITED, 8, DCP_COLOR_FLAG_DOWNSTREAM_420);
add_entry(&sdr_ycc, 137, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_BT2020_YCC, DCP_COLOR_YCBCR_RANGE_LIMITED, 10, DCP_COLOR_FLAG_DOWNSTREAM_420);
req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, .max_bpc = 10 };
assert(!dcp_select_color_mode(&sdr_ycc, &req, &out) && out.id == 137 && out.format == DCP_COLOR_FORMAT_YCBCR420);
/* Explicit wire format (even matching YCBCR420) disables the BT2020_RGB -> BT2020_YCC alias */
req.color_format = DRM_CONNECTOR_COLOR_FORMAT_YCBCR420;
assert(dcp_select_color_mode(&sdr_ycc, &req, &out) == -EINVAL);
req.color_format = DRM_CONNECTOR_COLOR_FORMAT_RGB444;
assert(dcp_select_color_mode(&sdr_ycc, &req, &out) == -EINVAL);

/* 4K60 HDMI 2.0 timing (LG UHD shape): only 8-bit 4:2:0 sRGB/BT.601 is safe;
* BT.2020 entries are unsafe. Auto SDR BT2020_RGB (Hyprland cm=edid) falls back
* through the filtered catalogue while preserving safety, EOTF, max_bpc, and
* strict rejection of explicit overrides and HDR.
*/
struct dcp_display_mode lg4k60 = {
.color_mode_id = 80, .sdr_usable = true, .sdr_unsafe = true,
/* Cached default points at an unsafe RGB mode (rate.py 4:2:0 parser shape):
* fallback MUST NOT bypass filtering via dcp_default_sdr_color(). */
.sdr_rgb = { .score = 300000, .id = 80, .eotf = DCP_EOTF_SDR_GAMMA,
.format = DCP_COLOR_FORMAT_RGB, .colorimetry = DCP_COLORIMETRY_SRGB,
.range = DCP_COLOR_YCBCR_RANGE_FULL, .depth = 8 },
.sdr_444 = { .score = -1 }, .sdr = { .score = -1 }, .best = { .score = -1 },
.pq = { .score = -1 },
};
add_entry(&lg4k60, 80, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_RGB, DCP_COLORIMETRY_SRGB, DCP_COLOR_YCBCR_RANGE_FULL, 8, DCP_COLOR_FLAG_UNSAFE);
add_entry(&lg4k60, 125, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_BT601, DCP_COLOR_YCBCR_RANGE_LIMITED, 8, DCP_COLOR_FLAG_DOWNSTREAM_420);
add_entry(&lg4k60, 123, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_SRGB, DCP_COLOR_YCBCR_RANGE_LIMITED, 8, DCP_COLOR_FLAG_DOWNSTREAM_420);
add_entry(&lg4k60, 137, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_BT2020_YCC, DCP_COLOR_YCBCR_RANGE_LIMITED, 10, DCP_COLOR_FLAG_UNSAFE | DCP_COLOR_FLAG_DOWNSTREAM_420);
add_entry(&lg4k60, 141, DCP_EOTF_ST_2084, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_BT2020_YCC, DCP_COLOR_YCBCR_RANGE_LIMITED, 10, DCP_COLOR_FLAG_UNSAFE | DCP_COLOR_FLAG_DOWNSTREAM_420);
/* Auto SDR BT2020_RGB with uncapped, 12-bpc, or 8-bpc picks safe 4:2:0 SDR (125), never unsafe 80 */
req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, .allow_hdr = true, .allow_unsafe_sdr = true };
assert(!dcp_select_color_mode(&lg4k60, &req, &out) && out.id == 125 && out.format == DCP_COLOR_FORMAT_YCBCR420);
req.max_bpc = 12;
assert(!dcp_select_color_mode(&lg4k60, &req, &out) && out.id == 125 && out.format == DCP_COLOR_FORMAT_YCBCR420);
req.max_bpc = 8;
assert(!dcp_select_color_mode(&lg4k60, &req, &out) && out.id == 125 && out.format == DCP_COLOR_FORMAT_YCBCR420);
/* Fallback also succeeds when all cached default slots are unpopulated */
lg4k60.sdr_rgb.score = -1;
req.max_bpc = 0;
assert(!dcp_select_color_mode(&lg4k60, &req, &out) && out.id == 125 && out.format == DCP_COLOR_FORMAT_YCBCR420);
/* max_bpc too low even for safe 8-bit SDR still rejects */
req.max_bpc = 6;
assert(dcp_select_color_mode(&lg4k60, &req, &out) == -EINVAL);
/* Explicit format, range, colorspace, or HDR still reject */
req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB,
.color_format = DRM_CONNECTOR_COLOR_FORMAT_RGB444, .allow_unsafe_sdr = true };
assert(dcp_select_color_mode(&lg4k60, &req, &out) == -EINVAL);
req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB,
.broadcast_rgb = DRM_HDMI_BROADCAST_RGB_LIMITED, .allow_unsafe_sdr = true };
assert(dcp_select_color_mode(&lg4k60, &req, &out) == -EINVAL);
req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_YCC, .allow_unsafe_sdr = true };
assert(dcp_select_color_mode(&lg4k60, &req, &out) == -EINVAL);
req = (struct dcp_color_request){ .eotf = HDMI_EOTF_SMPTE_ST2084,
.colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, .allow_hdr = true, .allow_unsafe_sdr = true };
assert(dcp_select_color_mode(&lg4k60, &req, &out) == -EINVAL);

/* Uncapped unsafe-only timing: fallback respects allow_unsafe_sdr and never picks PQ for SDR */
struct dcp_display_mode unsafe_only = {
.color_mode_id = 80, .sdr_usable = false, .sdr_unsafe = true,
.sdr_rgb = { .score = 100, .id = 80, .eotf = DCP_EOTF_SDR_GAMMA,
.format = DCP_COLOR_FORMAT_RGB, .colorimetry = DCP_COLORIMETRY_SRGB,
.range = DCP_COLOR_YCBCR_RANGE_FULL, .depth = 8 },
.sdr_444 = { .score = -1 }, .sdr = { .score = -1 }, .best = { .score = -1 }, .pq = { .score = -1 },
};
add_entry(&unsafe_only, 80, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_RGB, DCP_COLORIMETRY_SRGB, DCP_COLOR_YCBCR_RANGE_FULL, 8, DCP_COLOR_FLAG_UNSAFE);
req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, .allow_unsafe_sdr = false };
assert(dcp_select_color_mode(&unsafe_only, &req, &out) == -EINVAL);
req.allow_unsafe_sdr = true;
assert(!dcp_select_color_mode(&unsafe_only, &req, &out) && out.id == 80);

struct dcp_display_mode pq_only = {
.color_mode_id = 141, .sdr_usable = false, .sdr_unsafe = false,
.sdr_rgb = { .score = -1 }, .sdr_444 = { .score = -1 }, .sdr = { .score = -1 },
.best = { .score = 100, .id = 141, .eotf = DCP_EOTF_ST_2084,
.format = DCP_COLOR_FORMAT_RGB, .colorimetry = DCP_COLORIMETRY_BT2020_RGB,
.range = DCP_COLOR_YCBCR_RANGE_FULL, .depth = 10 },
.pq = { .score = 100, .id = 141, .eotf = DCP_EOTF_ST_2084,
.format = DCP_COLOR_FORMAT_RGB, .colorimetry = DCP_COLORIMETRY_BT2020_RGB,
.range = DCP_COLOR_YCBCR_RANGE_FULL, .depth = 10 },
};
add_entry(&pq_only, 141, DCP_EOTF_ST_2084, DCP_COLOR_FORMAT_RGB, DCP_COLORIMETRY_BT2020_RGB, DCP_COLOR_YCBCR_RANGE_FULL, 10, 0);
req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, .allow_hdr = false };
assert(dcp_select_color_mode(&pq_only, &req, &out) == -EINVAL);
req.allow_hdr = true;
assert(dcp_select_color_mode(&pq_only, &req, &out) == -EINVAL);

puts("PASS: colour mode selection across RGB/YCbCr 444/422/420, 8/10/12 bpc, Full/Limited, wide gamut, PQ/HLG, and -EINVAL rejection");
return 0;
}
Expand Down
Loading