Files
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

216 lines
7.3 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'models.dart';
import 'player/player.dart';
import 'player/video_rect_support.dart';
/// Video widget for displaying player output.
///
/// This widget displays the video output from a [Player] instance
/// and optionally overlays custom controls.
///
/// Example usage:
/// ```dart
/// final player = Player();
///
/// Video(
/// player: player,
/// controls: (context) => MyCustomControls(),
/// )
/// ```
class Video extends StatefulWidget {
final Player player;
final Widget Function(BuildContext context)? controls;
final Color backgroundColor;
final ValueListenable<bool>? hasFirstFrame;
const Video({
super.key,
required this.player,
this.controls,
this.backgroundColor = Colors.black,
this.hasFirstFrame,
});
@override
State<Video> createState() => _VideoState();
}
class _VideoState extends State<Video> {
// The integer physical bounds last handed to the native side, and the scale
// that went with them. Cached as what was *sent*, not as the logical rect it
// was derived from, because the rounding in _updateVideoRect is what decides
// whether a layout change is visible to the plane at all.
bool _hasSentRect = false;
int _sentLeft = 0;
int _sentTop = 0;
int _sentRight = 0;
int _sentBottom = 0;
double _sentDevicePixelRatio = 0;
bool _hasFirstFrame = false;
StreamSubscription<void>? _playbackRestartSubscription;
@override
void initState() {
super.initState();
_hasFirstFrame = widget.hasFirstFrame?.value ?? false;
widget.hasFirstFrame?.addListener(_syncExternalFirstFrame);
_listenForPlaybackRestart();
}
@override
void didUpdateWidget(covariant Video oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.hasFirstFrame != widget.hasFirstFrame) {
oldWidget.hasFirstFrame?.removeListener(_syncExternalFirstFrame);
widget.hasFirstFrame?.addListener(_syncExternalFirstFrame);
_syncExternalFirstFrame();
}
if (oldWidget.player != widget.player) {
_playbackRestartSubscription?.cancel();
_listenForPlaybackRestart();
_syncExternalFirstFrame();
// The cache describes the old player's native surface. Keeping it would
// let the next frame short-circuit as "geometry unchanged", and the
// replacement surface stays sizeless — invisible, with no Texture
// fallback left to cover for it.
_hasSentRect = false;
}
}
@override
void dispose() {
widget.hasFirstFrame?.removeListener(_syncExternalFirstFrame);
_playbackRestartSubscription?.cancel();
super.dispose();
}
void _listenForPlaybackRestart() {
_playbackRestartSubscription = widget.player.streams.playbackRestart.listen((_) {
_setHasFirstFrame(true);
});
}
void _syncExternalFirstFrame() {
final external = widget.hasFirstFrame;
if (external == null) return;
_setHasFirstFrame(external.value);
}
void _setHasFirstFrame(bool value) {
if (_hasFirstFrame == value || !mounted) return;
setState(() => _hasFirstFrame = value);
}
@override
Widget build(BuildContext context) {
return ColoredBox(
color: _hasFirstFrame ? Colors.transparent : widget.backgroundColor,
child: Stack(
fit: StackFit.expand,
children: [
// Video rendering area
_buildVideoSurface(),
// Controls overlay
if (widget.controls != null) widget.controls!(context),
],
),
);
}
Widget _buildVideoSurface() {
if (widget.player is VideoRectSupport) {
return LayoutBuilder(
builder: (context, constraints) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_updateVideoRect(context, constraints);
});
return const SizedBox.expand();
},
);
}
return const SizedBox.expand();
}
void _updateVideoRect(BuildContext context, BoxConstraints _) {
final renderBox = context.findRenderObject() as RenderBox?;
if (renderBox == null || !renderBox.hasSize) return;
final position = renderBox.localToGlobal(Offset.zero);
final size = renderBox.size;
final dpr = MediaQuery.devicePixelRatioOf(context);
// Rounded outward, the same way the native SetRect biases: it floors the
// position and rounds the buffer size up so the plane always covers at
// least the region Flutter cut out for it. Truncating the far edges here
// would undo that a layer earlier - at a fractional layout position the
// plane comes up a physical pixel short and the desktop shows through the
// seam, where the point of the plane is that the seam is black. Ceil and
// floor are identity on an already-integral value, so an integral layout
// sends exactly the numbers it sent before.
final left = (position.dx * dpr).floor();
final top = (position.dy * dpr).floor();
final right = ((position.dx + size.width) * dpr).ceil();
final bottom = ((position.dy + size.height) * dpr).ceil();
// Keyed on the four integers actually sent rather than on a logical-pixel
// tolerance. A sub-logical-pixel move is a real move at scale 2 or 3 -
// worth up to three physical pixels of stale placement - while anything too
// small to change one of these numbers cannot reach the plane at all and is
// not worth the channel round-trip.
//
// The scale is part of what the native side is being told, so it has to be
// part of what decides whether to tell it. Moving a window between a
// scale-1 and a scale-2 output can leave all four bounds identical while
// devicePixelRatio changes, and dropping that call leaves the native
// surface at the old buffer resolution: soft at half resolution after
// docking to a HiDPI output, overdrawn at double after undocking. The Steam
// Deck's dock is exactly this.
if (_hasSentRect &&
_sentLeft == left &&
_sentTop == top &&
_sentRight == right &&
_sentBottom == bottom &&
_sentDevicePixelRatio == dpr) {
return;
}
_hasSentRect = true;
_sentLeft = left;
_sentTop = top;
_sentRight = right;
_sentBottom = bottom;
_sentDevicePixelRatio = dpr;
final player = widget.player as VideoRectSupport;
player.setVideoRect(left: left, top: top, right: right, bottom: bottom, devicePixelRatio: dpr).catchError((
Object e,
) {
// Geometry is the only thing that makes the native surface visible,
// so a rejected rect is a black video area, not a cosmetic glitch.
// Post-frame callbacks have nobody to rethrow to, so route it to the
// player's error stream rather than leaving an unhandled async error.
//
// Drop the sent-rect cache too: it was recorded before the call
// resolved, and keeping it would short-circuit every later identical
// layout pass, freezing the failure in place. Cleared, the next layout
// or resize retries for free.
if (mounted &&
_sentLeft == left &&
_sentTop == top &&
_sentRight == right &&
_sentBottom == bottom &&
_sentDevicePixelRatio == dpr) {
_hasSentRect = false;
}
if (!player.errorController.isClosed) {
player.errorController.add(PlayerError('Failed to set video rect: $e'));
}
});
}
}