The cue picture cache is keyed by the cue, not by its moment
Browse files at this commit16925fdc82d0a8cfd2989bfa77130342cd3d5a5bParent: f7e750c- fix
- the cue picture cache is keyed by the cue, not by its moment
3 changed files +111 -10
- crates/app/src/main.rs +5 -2
- crates/app/src/tests/view.rs +57 -0
- crates/app/src/ui/preview.rs +49 -8
crates/app/src/main.rsmodified+5 -2
| @@ -138,8 +138,11 @@ struct Player { | |||
| 138 | /// The *lane* is half the key because the four PGS tracks of a remux are | 138 | /// The *lane* is half the key because the four PGS tracks of a remux are |
| 139 | /// one film's subtitles in four languages: placed one over the other they | 139 | /// one film's subtitles in four languages: placed one over the other they |
| 140 | /// start at the same microsecond, and a lane whose eye was shut would leave | 140 | /// start at the same microsecond, and a lane whose eye was shut would leave |
| 141 | - /// the one before it on screen. | 141 | + /// the one before it on screen. The other half is the cue itself |
| 142 | - sub_image: Option<((Lane, i64), Arc<RenderImage>)>, | 142 | + /// ([`crate::ui::preview::SubImageKey`]): two different cues can share a |
| 143 | + /// lane and a start too, and a key of those two alone served one caption's | ||
| 144 | + /// picture for another's. | ||
| 145 | + sub_image: Option<(crate::ui::preview::SubImageKey, Arc<RenderImage>)>, | ||
| 143 | /// A frame that arrived before its time; shown on the tick it comes due. | 146 | /// A frame that arrived before its time; shown on the tick it comes due. |
| 144 | /// The pump's buffer, not transport state -- but a frame waiting here is | 147 | /// The pump's buffer, not transport state -- but a frame waiting here is |
| 145 | /// what keeps a finished decoder from reading as [`Transport::Ended`] one | 148 | /// what keeps a finished decoder from reading as [`Transport::Ended`] one |
crates/app/src/tests/view.rsmodified+57 -0
| @@ -3,6 +3,63 @@ | |||
| 3 | 3 | ||
| 4 | use super::*; | 4 | use super::*; |
| 5 | 5 | ||
| 6 | +/// The preview's one cached cue picture is keyed by the *cue* and not only by | ||
| 7 | +/// where it happens to sit: a lane and a start microsecond are not an identity -- | ||
| 8 | +/// lift a bitmap caption off a lane, drop another track's caption at the same | ||
| 9 | +/// frame, and a key of those two matched the cache, painting the picture of the | ||
| 10 | +/// cue that is gone over the one that is up. The picture's own allocation is the | ||
| 11 | +/// rest of the key, and an identical repeat still hits, which is the whole cache. | ||
| 12 | +#[test] | ||
| 13 | +fn a_cached_cue_picture_is_keyed_by_the_cue_not_only_its_moment() { | ||
| 14 | + use crate::ui::preview::sub_image_key; | ||
| 15 | + let (lane, other) = (Lane::S1, Lane::A1); | ||
| 16 | + // Two cues that share the lane and the start, and nothing else: one lifted, | ||
| 17 | + // one placed at the same frame. | ||
| 18 | + let a = sub_image_key(lane, 1_000_000, 3_000_000, 0x1000); | ||
| 19 | + let b = sub_image_key(lane, 1_000_000, 3_000_000, 0x2000); | ||
| 20 | + assert_ne!( | ||
| 21 | + a, b, | ||
| 22 | + "two different cues at one moment on one lane are one cache key again, so \ | ||
| 23 | + the second paints the first's picture" | ||
| 24 | + ); | ||
| 25 | + // The same cue repainted: still a hit. | ||
| 26 | + assert_eq!( | ||
| 27 | + a, | ||
| 28 | + sub_image_key(lane, 1_000_000, 3_000_000, 0x1000), | ||
| 29 | + "the cache misses on every repaint of the same cue, so it decodes an 8 MB \ | ||
| 30 | + display set per frame" | ||
| 31 | + ); | ||
| 32 | + // The same picture recut over the same start is a different cue. | ||
| 33 | + assert_ne!( | ||
| 34 | + a, | ||
| 35 | + sub_image_key(lane, 1_000_000, 2_500_000, 0x1000), | ||
| 36 | + "a cue's own end is no part of the key, so a recut cue serves the old window" | ||
| 37 | + ); | ||
| 38 | + // And the other half of the old key still holds: the four PGS tracks of a | ||
| 39 | + // remux start at the same microsecond, so the lane separates them. | ||
| 40 | + assert_ne!( | ||
| 41 | + a, | ||
| 42 | + sub_image_key(other, 1_000_000, 3_000_000, 0x1000), | ||
| 43 | + "the lane is no longer part of the key, so a lane's eye shut leaves the \ | ||
| 44 | + other lane's caption on screen" | ||
| 45 | + ); | ||
| 46 | + // And the door builds its key this way, out of the cue's own picture: the | ||
| 47 | + // cache is only keyed by the cue while the lookup goes through this | ||
| 48 | + // constructor, and a test of the constructor alone would pass with the door | ||
| 49 | + // still keying on `(lane, start_us)`. | ||
| 50 | + let preview_rs = src_text("ui/preview.rs"); | ||
| 51 | + assert!( | ||
| 52 | + preview_rs.contains( | ||
| 53 | + "let key = sub_image_key(lane, start_us, end_us, Arc::as_ptr(image) as usize);" | ||
| 54 | + ), | ||
| 55 | + "the cue picture is cached under a key that is not the cue's own" | ||
| 56 | + ); | ||
| 57 | + assert!( | ||
| 58 | + preview_rs.contains("*up == key"), | ||
| 59 | + "the cache no longer compares the key it built" | ||
| 60 | + ); | ||
| 61 | +} | ||
| 62 | + | ||
| 6 | /// What a drop reads: the frame under the pointer, through the same scale | 63 | /// What a drop reads: the frame under the pointer, through the same scale |
| 7 | /// the boxes are drawn through. Zoomed in, the same pixel is a different | 64 | /// the boxes are drawn through. Zoomed in, the same pixel is a different |
| 8 | /// frame -- which is the whole reason `Player::frame_under` goes through | 65 | /// frame -- which is the whole reason `Player::frame_under` goes through |
crates/app/src/ui/preview.rsmodified+49 -8
| @@ -5,6 +5,44 @@ use crate::ui::type_scale::{self, Typeset}; | |||
| 5 | use crate::*; | 5 | use crate::*; |
| 6 | use std::path::{Path, PathBuf}; | 6 | use std::path::{Path, PathBuf}; |
| 7 | 7 | ||
| 8 | +/// What the preview's one cached cue picture is *of*: the lane, the moment, | ||
| 9 | +/// the cue's own end, and the picture itself. | ||
| 10 | +/// | ||
| 11 | +/// The lane and the start microsecond alone are not a cue. Two bitmap cues can | ||
| 12 | +/// share both -- lift a caption off a lane and drop another track's caption at | ||
| 13 | +/// the same frame -- and with those two as the whole key the cache's `up == key` | ||
| 14 | +/// test matched, painting the picture of the cue that is gone over the one that | ||
| 15 | +/// is up. | ||
| 16 | +/// | ||
| 17 | +/// The picture's own allocation ([`Arc::as_ptr`]) is the identity: the cue list | ||
| 18 | +/// a repaint is handed holds the same `Arc` for as long as that cue is in it, so | ||
| 19 | +/// two repaints of one cue compare equal -- which is the whole cache hit -- while | ||
| 20 | +/// two different cues never do. The bytes are never compared: a display set is | ||
| 21 | +/// tens of kilobytes and this test runs on every frame. | ||
| 22 | +#[derive(Clone, Copy, PartialEq, Eq, Debug)] | ||
| 23 | +pub(crate) struct SubImageKey { | ||
| 24 | + pub(crate) lane: Lane, | ||
| 25 | + pub(crate) start_us: i64, | ||
| 26 | + /// The cue's own end, so the same picture recut over the same start reads as | ||
| 27 | + /// a different cue here too. | ||
| 28 | + pub(crate) end_us: i64, | ||
| 29 | + /// Where that picture lives -- [`Arc::as_ptr`] of the cue's image. | ||
| 30 | + pub(crate) picture: usize, | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +/// [`SubImageKey`] for one cue's picture: the one place the key is built, so the | ||
| 34 | +/// lookup that trusts a hit and the test that pins what a hit means cannot drift | ||
| 35 | +/// apart. `picture` is where the cue's image lives -- the caller passes | ||
| 36 | +/// `Arc::as_ptr` of it, and nothing about the bytes is ever read here. | ||
| 37 | +pub(crate) fn sub_image_key(lane: Lane, start_us: i64, end_us: i64, picture: usize) -> SubImageKey { | ||
| 38 | + SubImageKey { | ||
| 39 | + lane, | ||
| 40 | + start_us, | ||
| 41 | + end_us, | ||
| 42 | + picture, | ||
| 43 | + } | ||
| 44 | +} | ||
| 45 | + | ||
| 8 | impl Player { | 46 | impl Player { |
| 9 | /// The darkroom's own preview indicator (MOCK-SPEC.md): a plate | 47 | /// The darkroom's own preview indicator (MOCK-SPEC.md): a plate |
| 10 | /// (canvas-on-panel, §4), mono for what the film says, and a ghost | 48 | /// (canvas-on-panel, §4), mono for what the film says, and a ghost |
| @@ -314,8 +352,10 @@ impl Player { | |||
| 314 | // lane is all there is here. | 352 | // lane is all there is here. |
| 315 | let picture = cues | 353 | let picture = cues |
| 316 | .iter() | 354 | .iter() |
| 317 | - .find_map(|cue| Some((cue.start_us, cue.image.as_ref()?))) | 355 | + .find_map(|cue| Some((cue.start_us, cue.end_us, cue.image.as_ref()?))) |
| 318 | - .and_then(|(start_us, image)| self.sub_picture(lane, start_us, image, window)); | 356 | + .and_then(|(start_us, end_us, image)| { |
| 357 | + self.sub_picture(lane, start_us, end_us, image, window) | ||
| 358 | + }); | ||
| 319 | // A picture is fitted onto the whole region and a plate hangs off the | 359 | // A picture is fitted onto the whole region and a plate hangs off the |
| 320 | // bottom of it, and a track is one or the other -- so they are two | 360 | // bottom of it, and a track is one or the other -- so they are two |
| 321 | // shapes and not one with the parts switched off. | 361 | // shapes and not one with the parts switched off. |
| @@ -386,10 +426,10 @@ impl Player { | |||
| 386 | ) | 426 | ) |
| 387 | } | 427 | } |
| 388 | 428 | ||
| 389 | - /// The cue `lane` starts at `start_us` as a drawable picture, decoded on the | 429 | + /// The cue `lane` plays from `start_us` to `end_us` as a drawable picture, |
| 390 | - /// first repaint it is up for and kept until another cue takes its place | 430 | + /// decoded on the first repaint it is up for and kept until another cue |
| 391 | - /// ([`Player::sub_image`]). `None` for a display set the decoder refuses, | 431 | + /// takes its place ([`Player::sub_image`]). `None` for a display set the |
| 392 | - /// which draws nothing rather than failing the frame. | 432 | + /// decoder refuses, which draws nothing rather than failing the frame. |
| 393 | /// | 433 | /// |
| 394 | /// Its atlas tile is released as the video's is: every [`RenderImage`] gets | 434 | /// Its atlas tile is released as the video's is: every [`RenderImage`] gets |
| 395 | /// a fresh id and its own tile, so a film's worth of cues would grow the | 435 | /// a fresh id and its own tile, so a film's worth of cues would grow the |
| @@ -398,10 +438,11 @@ impl Player { | |||
| 398 | &mut self, | 438 | &mut self, |
| 399 | lane: Lane, | 439 | lane: Lane, |
| 400 | start_us: i64, | 440 | start_us: i64, |
| 401 | - image: &engine::subtitle::CueImage, | 441 | + end_us: i64, |
| 442 | + image: &Arc<engine::subtitle::CueImage>, | ||
| 402 | window: &mut Window, | 443 | window: &mut Window, |
| 403 | ) -> Option<Arc<RenderImage>> { | 444 | ) -> Option<Arc<RenderImage>> { |
| 404 | - let key = (lane, start_us); | 445 | + let key = sub_image_key(lane, start_us, end_us, Arc::as_ptr(image) as usize); |
| 405 | if let Some((up, ready)) = &self.sub_image | 446 | if let Some((up, ready)) = &self.sub_image |
| 406 | && *up == key | 447 | && *up == key |
| 407 | { | 448 | { |