Files
plezy/linux/runner/mpv/plane_geometry_test.cc
T
edde746 bcd6fe9906 feat(linux): HDR video on a native Wayland plane
Video on Linux went through a Flutter texture: 8-bit sRGB, which cannot carry
HDR at all, and which forced a whole-window Flutter recomposite for every video
frame. This moves it onto a wl_subsurface stacked below the Flutter surface, with
mpv rendering into an EGL window surface on it through the libmpv render API. The
subsurface is desynchronized, so video and UI now present independently.

With the plane in place HDR follows: the surface is described to the compositor
through wp_color_manager_v1 as the source's own curve and gamut - PQ or HLG,
BT.2020 - carrying whatever HDR10 static metadata the stream actually declares.
The description and the buffer it describes land on the same commit, staged and
validated before mpv is switched, so a PQ frame is never presented labelled sRGB.
A five-second watchdog bounds the one wait a compositor could otherwise leave
hanging. A session that cannot host the plane - X11, or a compositor without
wl_subcompositor - fails initialize with VIDEO_PLANE_UNSUPPORTED naming the
reason: the texture path is gone, and refusing by name beats degrading to
something the user cannot see. An SDR output, a missing capability or an 8-bit
config keep the plane and simply leave it undescribed.

The output's colour state is trusted only when it has been earned. Every landed
property step records itself as it lands; a reset or sequence that cannot
finish downgrades its result to unknown and marks the applied-output cache
untrusted until a clean apply earns it back. A plane whose output state cannot
be named is quarantined - hidden, its description withdrawn - and the
quarantine is recorded state: an unrelated visibility change cannot put a
mislabelled plane back on screen, and only a commit that resolves to a nameable
outcome lifts it. A rect collapsing to zero detaches the buffer exactly as
hiding does, a refused setVideoRect drops the Dart-side sent-rect cache so the
next layout pass retries for free, and a refused tone-mapping pick tells the
user instead of dying in a log.

NVIDIA's Wayland EGL (through at least 610.xx) offers no 10-bit unorm window
configs, so the plane takes half-float as the tier between 10-bit unorm and
8-bit, declares the whole surface opaque so the compositor never reads the
alpha those configs carry, and states GL_RGBA16F rather than a 10-bit lie.
Whether the output is in HDR is read from luminance headroom above its own
reference white rather than from the preferred transfer function, which current
KWin no longer answers PQ for; the margin is half a stop, because KWin reports
an undimmed maximum over a software-dimmed SDR white. Validated on an RTX 4090
(driver 610.57.04) under KWin 6.7.4 with locked-exposure photographs.

Who tone-maps is a user choice. The default is the compositor: photographed on a
400-nit HDR output against a PQ chart it keeps 400 -> 1000 nits monotonic and
separated where the player leg flattens them, because the player path drives
mpv's legacy vo_gpu, whose own standalone output scores the same. The gap is the
renderer, not the wiring.

The decision itself - what the source carries, what the output supports, what to
tell mpv and what to tell the compositor - lives in hdr_metadata.h, free of
Wayland and GTK so its luminance validation can be tested without a display
server. Sending an incoherent luminance set is a protocol error that disconnects
the client, so the rules are worth a unit test.

The deb, rpm and pacman packages now declare wayland-client, wayland-egl and EGL:
the plane links them directly and bundle-libs.sh deliberately never bundles them,
since they are coupled to the running compositor and GPU driver.

lib/dev/harness_main.dart is a second entrypoint for measuring this on hardware -
it drives one clip with scripted mpv properties and reports the colour state mpv
actually settled on. Nothing imports it, so it is tree-shaken out of the app.

Verified on a Steam Deck against an external 400-nit HDR display: the compositor
reports PQ / BT.2020, the connector carries HDR_OUTPUT_METADATA, and against mpv
vo=gpu-next on the same frame the shipped build sits 4.90 counts away overall -
closer to the reference HDR player than to its own SDR fallback.
2026-08-10 08:48:13 +02:00

269 lines
12 KiB
C++

#include "plane_geometry.h"
#include <iostream>
#include <limits>
namespace {
int failures = 0;
void Expect(bool condition, const char* expression, int line) {
if (condition) return;
std::cerr << "line " << line << ": check failed: " << expression << '\n';
++failures;
}
#define EXPECT(condition) Expect(static_cast<bool>(condition), #condition, __LINE__)
constexpr int32_t kInt32Max = std::numeric_limits<int32_t>::max();
// The common case: a rect Flutter already sized to a whole number of physical
// pixels must pass through untouched, at every scale. Rounding a legal size is
// not free - it grows the plane past the hole in the UI - so it must not happen
// when there is nothing to round.
void TestExactMultiplesAreUnchanged() {
EXPECT(mpv::PlaneBufferExtent(0, 1920, 1) == 1920);
EXPECT(mpv::PlaneBufferExtent(0, 1920, 2) == 1920);
EXPECT(mpv::PlaneBufferExtent(0, 1920, 3) == 1920);
EXPECT(mpv::PlaneBufferExtent(0, 1080, 2) == 1080);
EXPECT(mpv::PlaneBufferExtent(0, 1083, 3) == 1083);
}
// The rule the compositor kills us over: a size that is not a whole multiple of
// the buffer scale is a fatal invalid_size on commit. It must round *up* - a
// size rounded down is smaller than the region Flutter cut out, and the desktop
// shows through the seam.
void TestSizesOnePixelOverRoundUpNeverDown() {
EXPECT(mpv::PlaneBufferExtent(0, 1921, 2) == 1922);
EXPECT(mpv::PlaneBufferExtent(0, 1921, 3) == 1923);
EXPECT(mpv::PlaneBufferExtent(0, 1922, 3) == 1923);
// One short of a multiple is the other side of the same boundary.
EXPECT(mpv::PlaneBufferExtent(0, 1919, 2) == 1920);
EXPECT(mpv::PlaneBufferExtent(0, 1919, 3) == 1920);
// Scale 1 makes every size legal, so nothing may move.
EXPECT(mpv::PlaneBufferExtent(0, 1921, 1) == 1921);
}
// Dart sends a 0x0 layout before the first real one, and a rect can be scrolled
// down to a sliver. Zero is not a legal buffer size and neither is anything
// below one whole scale unit, so the floor has to hold at every scale.
void TestDegenerateSizesYieldOneScaleUnit() {
EXPECT(mpv::PlaneBufferExtent(0, 0, 1) == 1);
EXPECT(mpv::PlaneBufferExtent(0, 0, 2) == 2);
EXPECT(mpv::PlaneBufferExtent(0, 0, 3) == 3);
EXPECT(mpv::PlaneBufferExtent(0, 1, 2) == 2);
EXPECT(mpv::PlaneBufferExtent(0, 2, 3) == 3);
// A negative extent is not reachable from a sane layout, but it is reachable
// from an int32 cast of an unvalidated channel argument, and it must not
// become a negative buffer size.
EXPECT(mpv::PlaneBufferExtent(0, -4096, 2) == 2);
}
// The round-up adds up to scale-1 to its input, so a size near the type's
// maximum overflows unless it is clamped first - and a negative width reaching
// wl_egl_window_resize is exactly the corruption the clamp exists to stop.
void TestSizesNearIntMaxDoNotOverflow() {
EXPECT(mpv::PlaneBufferExtent(0, kInt32Max, 1) == kInt32Max);
EXPECT(mpv::PlaneBufferExtent(0, kInt32Max, 2) == kInt32Max - 1);
EXPECT(mpv::PlaneBufferExtent(0, kInt32Max, 3) == kInt32Max - 1);
EXPECT(mpv::PlaneBufferExtent(0, kInt32Max - 1, 3) == kInt32Max - 1);
}
// Every size the function can return must still be legal to commit: positive, a
// whole multiple of the scale, and never smaller than what was asked for. The
// individual cases above pin the interesting numbers; this pins the rule.
void TestBufferExtentInvariantsHold() {
// Up to 16 because that is what the plugin clamps devicePixelRatio to before
// handing it over as the buffer scale, so every one of these is reachable.
for (int32_t scale = 1; scale <= 16; ++scale) {
for (int32_t extent = -8; extent <= 64; ++extent) {
const int32_t rounded = mpv::PlaneBufferExtent(0, extent, scale);
EXPECT(rounded >= scale);
EXPECT(rounded % scale == 0);
EXPECT(rounded >= extent);
// Rounding up, not up-and-then-some: the plane grows by less than a scale
// unit, never a whole one.
EXPECT(extent < scale || rounded - extent < scale);
}
}
}
// The one that matters, and the one neither rule can promise alone: wherever
// Flutter put the rect, the plane has to cover all of it. Flooring the origin
// moves the near edge outward and does nothing for the far edge, so an extent
// rounded from the width on its own leaves the far edge short by whatever the
// floor gave away - and the toplevel is transparent, so that strip shows the
// desktop rather than black.
//
// Swept over every scale the plugin accepts and both signs of origin, at rect
// sizes a window can actually have. Coverage is not universal and cannot be: a
// rect whose far edge needs more than INT32_MAX physical pixels is not
// representable, and TestAnUnrepresentableRectStaysLegal below pins what
// happens there instead.
void TestThePlaneAlwaysCoversTheRect() {
for (int32_t scale = 1; scale <= 16; ++scale) {
for (int32_t x = -40; x <= 40; ++x) {
for (int32_t width = 1; width <= 80; ++width) {
// Physical pixels, which is the frame the rect itself is in.
const int64_t origin = static_cast<int64_t>(mpv::PlaneSurfacePosition(x, scale, 0)) * scale;
const int64_t extent = mpv::PlaneBufferExtent(x, width, scale);
EXPECT(origin <= x);
EXPECT(origin + extent >= static_cast<int64_t>(x) + width);
// Still legal to commit, which the far-edge rounding must not cost.
EXPECT(extent % scale == 0);
// And no more generous than it has to be: the cover is tight to within
// one scale unit at each edge.
EXPECT(x - origin < scale);
EXPECT((origin + extent) - (static_cast<int64_t>(x) + width) < scale);
}
}
}
}
// Past the end of int32 the plane cannot cover the rect, because the rect is
// not representable. What still has to hold is the one whose failure is fatal:
// a buffer size that is not a whole multiple of the scale makes wl_surface
// .commit an invalid_size protocol error and disconnects the whole client. So
// this asserts legality rather than coverage, and pins the largest legal answer
// so a future clamp cannot quietly give away a whole scale unit.
void TestAnUnrepresentableRectStaysLegal() {
const int32_t huge = std::numeric_limits<int32_t>::max();
for (int32_t scale = 1; scale <= 16; ++scale) {
for (const int32_t x : {-1, 0, 1, 40}) {
const int32_t extent = mpv::PlaneBufferExtent(x, huge, scale);
EXPECT(extent > 0);
EXPECT(extent % scale == 0);
// The largest multiple of the scale that fits, not one block less.
// Recomputing `(huge / scale) * scale` here would just be the cap
// expression from the header again, so the interesting scales carry
// literals: an oracle that is a copy of the code cannot fail with it.
if (scale == 1) EXPECT(extent == 2147483647);
if (scale == 2) EXPECT(extent == 2147483646);
if (scale == 3) EXPECT(extent == 2147483646);
if (scale == 8) EXPECT(extent == 2147483640);
if (scale == 16) EXPECT(extent == 2147483632);
}
}
}
// An origin already on a scale boundary converts exactly, so the plane lands
// where Flutter put it.
void TestExactPositionMultiplesConvertExactly() {
EXPECT(mpv::PlaneSurfacePosition(0, 2, 0) == 0);
EXPECT(mpv::PlaneSurfacePosition(640, 1, 0) == 640);
EXPECT(mpv::PlaneSurfacePosition(640, 2, 0) == 320);
EXPECT(mpv::PlaneSurfacePosition(639, 3, 0) == 213);
EXPECT(mpv::PlaneSurfacePosition(-640, 2, 0) == -320);
EXPECT(mpv::PlaneSurfacePosition(-639, 3, 0) == -213);
}
// A positive origin off the boundary floors down, which for positives is what
// plain integer division already does. Pinned so the flooring below cannot be
// "fixed" into rounding.
void TestPositivePositionsFloorDown() {
EXPECT(mpv::PlaneSurfacePosition(641, 2, 0) == 320);
EXPECT(mpv::PlaneSurfacePosition(1, 2, 0) == 0);
EXPECT(mpv::PlaneSurfacePosition(2, 3, 0) == 0);
EXPECT(mpv::PlaneSurfacePosition(641, 3, 0) == 213);
EXPECT(mpv::PlaneSurfacePosition(641, 1, 0) == 641);
}
// The case C gets wrong. A video rect scrolled partly off the left or top has a
// negative origin, and integer division truncates *toward zero* - which moves
// the plane inward by up to scale-1 physical pixels while the size deliberately
// grows outward, uncovering the very edge the size was widened to cover.
void TestNegativePositionsFloorAwayFromZero() {
EXPECT(mpv::PlaneSurfacePosition(-1, 2, 0) == -1); // truncation gives 0
EXPECT(mpv::PlaneSurfacePosition(-3, 2, 0) == -2); // truncation gives -1
EXPECT(mpv::PlaneSurfacePosition(-1, 3, 0) == -1); // truncation gives 0
EXPECT(mpv::PlaneSurfacePosition(-4, 3, 0) == -2); // truncation gives -1
EXPECT(mpv::PlaneSurfacePosition(-641, 2, 0) == -321);
// Scale 1 divides evenly, so there is nothing to floor and negatives survive.
EXPECT(mpv::PlaneSurfacePosition(-641, 1, 0) == -641);
}
// The flooring must never place the plane's origin to the right of, or below,
// the rect it is covering: converted back to physical pixels the result is at
// or before the requested origin, and within one scale unit of it.
void TestPositionNeverBiasesInward() {
for (int32_t scale = 1; scale <= 16; ++scale) {
for (int32_t position = -32; position <= 32; ++position) {
const int32_t local = mpv::PlaneSurfacePosition(position, scale, 0);
EXPECT(local * scale <= position);
EXPECT(position - local * scale < scale);
}
}
}
// The FlView is inset inside the toplevel whenever GTK draws client-side
// decorations, and wl_subsurface_set_position is relative to the toplevel. The
// offset is already in logical units, so it is added *after* the divide - adding
// it before would scale it and slide the plane by the wrong amount.
void TestViewOffsetIsAddedInSurfaceLocalUnits() {
EXPECT(mpv::PlaneSurfacePosition(640, 2, 37) == 357);
EXPECT(mpv::PlaneSurfacePosition(641, 2, 37) == 357);
EXPECT(mpv::PlaneSurfacePosition(-3, 2, 37) == 35);
EXPECT(mpv::PlaneSurfacePosition(639, 3, 8) == 221);
EXPECT(mpv::PlaneSurfacePosition(640, 1, 8) == 648);
// Had the offset been scaled instead of added straight, this would be 320+18.
EXPECT(mpv::PlaneSurfacePosition(640, 2, 36) != 338);
}
// Server-side decorations - a KWin session, which is what this is developed on -
// make the offset zero. That path must be indistinguishable from having no
// offset at all, or the CSD fix would have quietly moved the plane everywhere it
// was already correct.
void TestZeroViewOffsetChangesNothing() {
for (int32_t scale = 1; scale <= 16; ++scale) {
for (int32_t position = -32; position <= 32; ++position) {
const int32_t zero = mpv::PlaneSurfacePosition(position, scale, 0);
// Stated as a property rather than by recomputing the implementation's own
// formula: an oracle that is a copy of the code cannot fail for any change
// made to both, including the flooring direction this is named for. The
// property is that the origin lands on or before the rect and within one
// scale unit of it.
EXPECT(static_cast<int64_t>(zero) * scale <= position);
EXPECT(position - static_cast<int64_t>(zero) * scale < scale);
// And an offset really is just an addition on top of that answer.
for (const int32_t offset : {-37, -1, 0, 1, 37}) {
EXPECT(mpv::PlaneSurfacePosition(position, scale, offset) == zero + offset);
}
}
}
}
// Scale reaches both rules as an int32 cast of an unvalidated channel argument.
// Zero would divide by zero and a negative would invert the rounding, so both
// collapse to the identity scale instead.
void TestNonPositiveScaleIsTreatedAsOne() {
EXPECT(mpv::NormalizePlaneScale(0) == 1);
EXPECT(mpv::NormalizePlaneScale(-4) == 1);
EXPECT(mpv::NormalizePlaneScale(1) == 1);
EXPECT(mpv::NormalizePlaneScale(3) == 3);
EXPECT(mpv::PlaneBufferExtent(0, 1921, 0) == 1921);
EXPECT(mpv::PlaneBufferExtent(0, 0, -4) == 1);
EXPECT(mpv::PlaneSurfacePosition(-641, 0, 0) == -641);
EXPECT(mpv::PlaneSurfacePosition(-641, -4, 7) == -634);
}
} // namespace
int main() {
TestExactMultiplesAreUnchanged();
TestSizesOnePixelOverRoundUpNeverDown();
TestDegenerateSizesYieldOneScaleUnit();
TestSizesNearIntMaxDoNotOverflow();
TestBufferExtentInvariantsHold();
TestThePlaneAlwaysCoversTheRect();
TestAnUnrepresentableRectStaysLegal();
TestExactPositionMultiplesConvertExactly();
TestPositivePositionsFloorDown();
TestNegativePositionsFloorAwayFromZero();
TestPositionNeverBiasesInward();
TestViewOffsetIsAddedInSurfaceLocalUnits();
TestZeroViewOffsetChangesNothing();
TestNonPositiveScaleIsTreatedAsOne();
return failures == 0 ? 0 : 1;
}