diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 45b79b8..d5cecc3 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -138,8 +138,11 @@ struct Player { /// The *lane* is half the key because the four PGS tracks of a remux are /// one film's subtitles in four languages: placed one over the other they /// start at the same microsecond, and a lane whose eye was shut would leave - /// the one before it on screen. - sub_image: Option<((Lane, i64), Arc)>, + /// the one before it on screen. The other half is the cue itself + /// ([`crate::ui::preview::SubImageKey`]): two different cues can share a + /// lane and a start too, and a key of those two alone served one caption's + /// picture for another's. + sub_image: Option<(crate::ui::preview::SubImageKey, Arc)>, /// A frame that arrived before its time; shown on the tick it comes due. /// The pump's buffer, not transport state -- but a frame waiting here is /// what keeps a finished decoder from reading as [`Transport::Ended`] one diff --git a/crates/app/src/tests/view.rs b/crates/app/src/tests/view.rs index 0ff148f..a8c45d1 100644 --- a/crates/app/src/tests/view.rs +++ b/crates/app/src/tests/view.rs @@ -3,6 +3,63 @@ use super::*; +/// The preview's one cached cue picture is keyed by the *cue* and not only by +/// where it happens to sit: a lane and a start microsecond are not an identity -- +/// lift a bitmap caption off a lane, drop another track's caption at the same +/// frame, and a key of those two matched the cache, painting the picture of the +/// cue that is gone over the one that is up. The picture's own allocation is the +/// rest of the key, and an identical repeat still hits, which is the whole cache. +#[test] +fn a_cached_cue_picture_is_keyed_by_the_cue_not_only_its_moment() { + use crate::ui::preview::sub_image_key; + let (lane, other) = (Lane::S1, Lane::A1); + // Two cues that share the lane and the start, and nothing else: one lifted, + // one placed at the same frame. + let a = sub_image_key(lane, 1_000_000, 3_000_000, 0x1000); + let b = sub_image_key(lane, 1_000_000, 3_000_000, 0x2000); + assert_ne!( + a, b, + "two different cues at one moment on one lane are one cache key again, so \ + the second paints the first's picture" + ); + // The same cue repainted: still a hit. + assert_eq!( + a, + sub_image_key(lane, 1_000_000, 3_000_000, 0x1000), + "the cache misses on every repaint of the same cue, so it decodes an 8 MB \ + display set per frame" + ); + // The same picture recut over the same start is a different cue. + assert_ne!( + a, + sub_image_key(lane, 1_000_000, 2_500_000, 0x1000), + "a cue's own end is no part of the key, so a recut cue serves the old window" + ); + // And the other half of the old key still holds: the four PGS tracks of a + // remux start at the same microsecond, so the lane separates them. + assert_ne!( + a, + sub_image_key(other, 1_000_000, 3_000_000, 0x1000), + "the lane is no longer part of the key, so a lane's eye shut leaves the \ + other lane's caption on screen" + ); + // And the door builds its key this way, out of the cue's own picture: the + // cache is only keyed by the cue while the lookup goes through this + // constructor, and a test of the constructor alone would pass with the door + // still keying on `(lane, start_us)`. + let preview_rs = src_text("ui/preview.rs"); + assert!( + preview_rs.contains( + "let key = sub_image_key(lane, start_us, end_us, Arc::as_ptr(image) as usize);" + ), + "the cue picture is cached under a key that is not the cue's own" + ); + assert!( + preview_rs.contains("*up == key"), + "the cache no longer compares the key it built" + ); +} + /// What a drop reads: the frame under the pointer, through the same scale /// the boxes are drawn through. Zoomed in, the same pixel is a different /// frame -- which is the whole reason `Player::frame_under` goes through diff --git a/crates/app/src/ui/preview.rs b/crates/app/src/ui/preview.rs index 49534a0..ae6997a 100644 --- a/crates/app/src/ui/preview.rs +++ b/crates/app/src/ui/preview.rs @@ -5,6 +5,44 @@ use crate::ui::type_scale::{self, Typeset}; use crate::*; use std::path::{Path, PathBuf}; +/// What the preview's one cached cue picture is *of*: the lane, the moment, +/// the cue's own end, and the picture itself. +/// +/// The lane and the start microsecond alone are not a cue. Two bitmap cues can +/// share both -- lift a caption off a lane and drop another track's caption at +/// the same frame -- and with those two as the whole key the cache's `up == key` +/// test matched, painting the picture of the cue that is gone over the one that +/// is up. +/// +/// The picture's own allocation ([`Arc::as_ptr`]) is the identity: the cue list +/// a repaint is handed holds the same `Arc` for as long as that cue is in it, so +/// two repaints of one cue compare equal -- which is the whole cache hit -- while +/// two different cues never do. The bytes are never compared: a display set is +/// tens of kilobytes and this test runs on every frame. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(crate) struct SubImageKey { + pub(crate) lane: Lane, + pub(crate) start_us: i64, + /// The cue's own end, so the same picture recut over the same start reads as + /// a different cue here too. + pub(crate) end_us: i64, + /// Where that picture lives -- [`Arc::as_ptr`] of the cue's image. + pub(crate) picture: usize, +} + +/// [`SubImageKey`] for one cue's picture: the one place the key is built, so the +/// lookup that trusts a hit and the test that pins what a hit means cannot drift +/// apart. `picture` is where the cue's image lives -- the caller passes +/// `Arc::as_ptr` of it, and nothing about the bytes is ever read here. +pub(crate) fn sub_image_key(lane: Lane, start_us: i64, end_us: i64, picture: usize) -> SubImageKey { + SubImageKey { + lane, + start_us, + end_us, + picture, + } +} + impl Player { /// The darkroom's own preview indicator (MOCK-SPEC.md): a plate /// (canvas-on-panel, ยง4), mono for what the film says, and a ghost @@ -314,8 +352,10 @@ impl Player { // lane is all there is here. let picture = cues .iter() - .find_map(|cue| Some((cue.start_us, cue.image.as_ref()?))) - .and_then(|(start_us, image)| self.sub_picture(lane, start_us, image, window)); + .find_map(|cue| Some((cue.start_us, cue.end_us, cue.image.as_ref()?))) + .and_then(|(start_us, end_us, image)| { + self.sub_picture(lane, start_us, end_us, image, window) + }); // A picture is fitted onto the whole region and a plate hangs off the // bottom of it, and a track is one or the other -- so they are two // shapes and not one with the parts switched off. @@ -386,10 +426,10 @@ impl Player { ) } - /// The cue `lane` starts at `start_us` as a drawable picture, decoded on the - /// first repaint it is up for and kept until another cue takes its place - /// ([`Player::sub_image`]). `None` for a display set the decoder refuses, - /// which draws nothing rather than failing the frame. + /// The cue `lane` plays from `start_us` to `end_us` as a drawable picture, + /// decoded on the first repaint it is up for and kept until another cue + /// takes its place ([`Player::sub_image`]). `None` for a display set the + /// decoder refuses, which draws nothing rather than failing the frame. /// /// Its atlas tile is released as the video's is: every [`RenderImage`] gets /// a fresh id and its own tile, so a film's worth of cues would grow the @@ -398,10 +438,11 @@ impl Player { &mut self, lane: Lane, start_us: i64, - image: &engine::subtitle::CueImage, + end_us: i64, + image: &Arc, window: &mut Window, ) -> Option> { - let key = (lane, start_us); + let key = sub_image_key(lane, start_us, end_us, Arc::as_ptr(image) as usize); if let Some((up, ready)) = &self.sub_image && *up == key {