Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
Video plays smoothly at 4K and at 60 fps: frames decoded ahead and shown when due, a faster BGRA conversion, halvings from the decoder's thread, no relayout for a same-size frame
shinyobjectzd7a755c08b59
diff --git a/engine/page/host/src/main.rs b/engine/page/host/src/main.rsindex 02c15dc6..303963ba 100644--- a/engine/page/host/src/main.rs+++ b/engine/page/host/src/main.rs@@ -97,7 +97,10 @@ fn main() {);// on screen, bevy_winit's event loop is the runner (a ScheduleRunner here replaced it: no window, 11-04)if !a.window && !app.is_plugin_added::<ScheduleRunnerPlugin>() {- app.add_plugins(ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(1.0 / 60.0)));+ // 60 frames a second, as most screens; RAEA_PAGE_FPS stands in for another screen's rate (a 120 Hz one, to+ // measure how a 60 fps video plays where the window draws twice a frame)+ let fps = std::env::var("RAEA_PAGE_FPS").ok().and_then(|v| v.parse::<f64>().ok()).filter(|f| (1.0..=480.0).contains(f)).unwrap_or(60.0);+ app.add_plugins(ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(1.0 / fps)));}app.add_plugins(HostPlugin { size: a.size, double_click: Duration::from_millis(500), picking }).add_plugins(brp::plugins(brp::port(), |r| r))diff --git a/io/apple/src/convert.rs b/io/apple/src/convert.rsnew file mode 100644index 00000000..c1fa41e4--- /dev/null+++ b/io/apple/src/convert.rs@@ -0,0 +1,40 @@+//! VideoToolbox's BGRA as the RGBA a Frame holds.++/// A row of BGRA as RGBA, its alpha kept or made 255. Four bytes at a time as one word, which the compiler turns into+/// vector code: a 4K frame took 5.5 ms a byte at a time, most of what playing one cost (RAEA_MEDIA_STATS, 11-13).+pub(crate) fn bgra_to_rgba(src: &[u8], dst: &mut [u8], alpha: bool) {+ let opaque = if alpha { 0 } else { 0xff00_0000 };+ for (d, s) in dst.chunks_exact_mut(4).zip(src.chunks_exact(4)) {+ let v = u32::from_le_bytes([s[0], s[1], s[2], s[3]]);+ let out = (v & 0xff00_ff00) | ((v & 0xff) << 16) | ((v >> 16) & 0xff) | opaque;+ d.copy_from_slice(&out.to_le_bytes());+ }+}++#[cfg(test)]+mod tests {+ use super::bgra_to_rgba;++ #[test]+ fn bgra_becomes_rgba_with_its_alpha_kept_or_made_opaque() {+ let src = [10, 20, 30, 40, 1, 2, 3, 0];+ let mut out = [0u8; 8];+ bgra_to_rgba(&src, &mut out, true);+ assert_eq!(out, [30, 20, 10, 40, 3, 2, 1, 0]);+ bgra_to_rgba(&src, &mut out, false);+ assert_eq!(out, [30, 20, 10, 255, 3, 2, 1, 255]);+ }++ /// A 4K frame's worth, timed (cargo test -p raea-io-apple --release convert -- --nocapture).+ #[test]+ fn a_4k_frame_is_converted_in_a_frame_s_small_part() {+ let src: Vec<u8> = (0..3840 * 2160 * 4).map(|i| (i % 251) as u8).collect();+ let mut out = vec![0u8; src.len()];+ let t = std::time::Instant::now();+ for _ in 0..10 {+ bgra_to_rgba(&src, &mut out, false);+ }+ eprintln!("4K BGRA to RGBA: {:.2} ms a frame", t.elapsed().as_secs_f64() * 100.0);+ assert_eq!(&out[..4], &[src[2], src[1], src[0], 255]);+ }+}diff --git a/io/apple/src/decode.rs b/io/apple/src/decode.rsindex d3c7e580..504fd786 100644--- a/io/apple/src/decode.rs+++ b/io/apple/src/decode.rs@@ -213,13 +213,7 @@ impl Decoder {let mut rgba = vec![0u8; w * h * 4];for y in 0..h {let row = std::slice::from_raw_parts(base.add(y * bpr), w * 4);- let dst = &mut rgba[y * w * 4..(y + 1) * w * 4];- for x in 0..w {- dst[x * 4] = row[x * 4 + 2];- dst[x * 4 + 1] = row[x * 4 + 1];- dst[x * 4 + 2] = row[x * 4];- dst[x * 4 + 3] = if self.alpha { row[x * 4 + 3] } else { 255 };- }+ crate::convert::bgra_to_rgba(row, &mut rgba[y * w * 4..(y + 1) * w * 4], self.alpha);}rgba});diff --git a/io/apple/src/lib.rs b/io/apple/src/lib.rsindex 25e83af4..71b077bc 100644--- a/io/apple/src/lib.rs+++ b/io/apple/src/lib.rs@@ -4,6 +4,7 @@#![cfg(target_vendor = "apple")]mod audio;+mod convert;mod decode;mod encode;mod vt;diff --git a/runtime/dom/media/src/lib.rs b/runtime/dom/media/src/lib.rsindex 83ed4767..f33782ae 100644--- a/runtime/dom/media/src/lib.rs+++ b/runtime/dom/media/src/lib.rs@@ -5,15 +5,18 @@//! A seek is the prop `__seek` ([time, n], set by the dom layer's `currentTime`); playing is `__play` ([on, time, n,//! loop], set by `play()` and `pause()`), `__muted` the element's muted and `__volume` its volume. Playing, the decoder's frames come on the//! source's clock (its sound's: raea-runtime-js media_play.rs), each a `time` news for the element's currentTime, with-//! `timeupdate` four times a second as a browser's, `playing` once it's going, and `ended` at its end. raea-runtime-dom's+//! `timeupdate` four times a second as a browser's, `playing` once it's going, and `ended` at its end. A playing+//! video's frames come ahead of their time, each with the moment it is due: at each sync (the page's own frame) the+//! one due then is shown, so the picture keeps the page's rhythm whatever the decoder's. raea-runtime-dom's//! page host keeps one `Medias` and syncs it after each commit.-use std::collections::HashMap;+use std::collections::{HashMap, VecDeque};use std::sync::Arc;+use std::time::{Duration, Instant};use blitz_dom::BaseDocument;use blitz_dom::node::{ImageData, RasterImageData, SpecialElementData};-use raea_runtime_js::media_html::stream::{Cmd, Msg, Stream};+use raea_runtime_js::media_html::stream::{Cmd, Halvings, Msg, Stream};use serde_json::{Value, json};use raea_sys_page::ops::Nodes;@@ -35,6 +38,14 @@ pub struct Media {duration: f64,/// the latest frame: width, height, pixelsframe: Option<(u32, u32, Arc<Vec<u8>>)>,+ /// its halvings, until they go to the painter with it+ halves: Halvings,+ /// how many halvings its decoder is asked to send with each frame+ halvings: u8,+ /// a playing video's frames not due yet: each its time, its picture and when to show it+ ahead: VecDeque<(f64, (u32, u32, Vec<u8>), Instant, Halvings)>,+ /// when this was last synced, and how far apart syncs have been lately (the page's frame, s)+ synced: (Option<Instant>, f64),/// `__play` as last seen, and whether it asks to playplay: Option<Value>,playing: bool,@@ -47,6 +58,8 @@ pub struct Media {stats: Option<(std::time::Instant, u32, u32, f64, f64)>,/// RAEA_MEDIA_STATS=1: when a frame was last shown, and the longest wait between two this second (ms)shown_gap: (Option<std::time::Instant>, f64),+ /// RAEA_MEDIA_STATS=1, this second: the page's frames, the longest (ms), and how late frames were shown (sum, max; ms)+ paced: (u32, f64, f64, f64),}impl Media {@@ -76,7 +89,7 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &muSome(m) if m.src == src && m.load == load => m,_ => {let stream = (!src.is_empty()).then(|| raea_runtime_js::media_html::stream::open(&raea_sys_page::net::page_path(&src), video)).flatten();- medias.insert(id, Media { src, load, poster: None, seek: None, stream, video, loaded: false, seeking: None, seeked: false, duration: f64::NAN, frame: None, play: None, playing: false, muted: None, volume: None, told: None, shown_gap: (None, 0.0), stats: (std::env::var("RAEA_MEDIA_STATS").as_deref() == Ok("1")).then(|| (std::time::Instant::now(), 0, 0, 0.0, 0.0)) });+ medias.insert(id, Media { src, load, poster: None, seek: None, stream, video, loaded: false, seeking: None, seeked: false, duration: f64::NAN, frame: None, halves: Vec::new(), halvings: 0, ahead: VecDeque::new(), synced: (None, 1.0 / 60.0), paced: (0, 0.0, 0.0, 0.0), play: None, playing: false, muted: None, volume: None, told: None, shown_gap: (None, 0.0), stats: (std::env::var("RAEA_MEDIA_STATS").as_deref() == Ok("1")).then(|| (std::time::Instant::now(), 0, 0, 0.0, 0.0)) });medias.get_mut(&id).unwrap()}};@@ -117,6 +130,7 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &must.ask(Cmd::Play { at: p.get(1).and_then(Value::as_f64).unwrap_or(0.0), looping: p.get(3).and_then(Value::as_bool).unwrap_or(false) });} else {st.ask(Cmd::Pause);+ m.ahead.clear();}}m.play = play;@@ -131,9 +145,12 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &mubevy::log::warn!("raea-page: media: {err}");out.push(json!({"id": id, "type": "error", "message": err}));}- Msg::Frame(t, f) => {+ Msg::Frame(t, f, halves) => {+ // the first frame or a seek's: shown now, and what was on its way for before the seek is not+ m.ahead.clear();let (w, h) = (f.width, f.height);m.frame = Some((w, h, Arc::new(f.rgba)));+ m.halves = halves;redraw = true;came += 1;if !m.loaded {@@ -149,6 +166,12 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &mutold(m, id, t, out);}}+ // a playing video's frame, for later; one that comes after a pause is for nothing+ Msg::Due(t, f, due, halves) => {+ if m.playing {+ m.ahead.push_back((t, (f.width, f.height, f.rgba), due, halves));+ }+ }Msg::Time(t) => {if m.landed(t) {out.push(json!({"id": id, "type": "seeked", "time": t}));@@ -159,13 +182,58 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &muout.push(json!({"id": id, "type": "time", "time": t}));}}- Msg::Playing(t) => out.push(json!({"id": id, "type": "playing", "time": t})),+ Msg::Playing(t) => {+ m.ahead.clear();+ out.push(json!({"id": id, "type": "playing", "time": t}));+ }Msg::Ended(t) => {m.playing = false;out.push(json!({"id": id, "type": "ended", "time": t}));}}}+ // the frame due at this sync: the latest whose moment has come, or comes within half a page frame (shown a+ // touch early rather than a whole page frame late); the ones before it were never seen+ let now = Instant::now();+ if let Some(last) = m.synced.0 {+ let dt = now.duration_since(last).as_secs_f64();+ m.synced.1 += 0.1 * (dt.min(0.1) - m.synced.1);+ if m.stats.is_some() && !m.ahead.is_empty() {+ (m.paced.0, m.paced.1) = (m.paced.0 + 1, m.paced.1.max(dt * 1e3));+ }+ }+ m.synced.0 = Some(now);+ let reach = now + Duration::from_secs_f64((0.5 * m.synced.1).min(0.008));+ let (due, passed) = due_by(&mut m.ahead, reach);+ came += passed;+ if let Some((t, (w, h, rgba), at, halves)) = due {+ m.halves = halves;+ let late = now.saturating_duration_since(at).as_secs_f64() * 1e3;+ (m.paced.2, m.paced.3) = (m.paced.2 + late, m.paced.3.max(late));+ m.frame = Some((w, h, Arc::new(rgba)));+ (redraw, m.seeked) = (true, true);+ came += 1;+ told(m, id, t, out);+ }+ // how many halvings its frames should come with: as many as bring one within twice the size it is drawn at+ // (the painter's rule, blitz-paint mip_level), counted from its box as last laid out+ if m.video && draw && let (Some((w, h, _)), Some(n), Some(st)) = (m.frame.as_ref(), nodes.by_id.get(&id), m.stream.as_mut()) {+ let want = doc.get_node(*n).map_or(0, |node| {+ let (l, scale) = (node.final_layout(), doc.viewport().scale_f64());+ let drawn = (l.content_box_width() as f64 * scale / *w as f64).min(l.content_box_height() as f64 * scale / *h as f64);+ let mut n = 0u8;+ while drawn > 0.0 && drawn * f64::from(1u32 << (n + 1)) <= 1.0 && n < 6 {+ n += 1;+ }+ n+ });+ // RAEA_MEDIA_HALVINGS=0: frames drawn as they come, as before (to compare with)+ let want = if std::env::var("RAEA_MEDIA_HALVINGS").as_deref() == Ok("0") { 0 } else { want };+ if want != m.halvings {+ m.halvings = want;+ st.ask(Cmd::Halvings(want));+ }+ }if redraw && m.video && draw {let pic = match (&m.poster, m.seeked) {(Some(p), false) => poster_image(p).map(|(w, h, px)| (w, h, Arc::new(px))),@@ -175,7 +243,7 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &mu.or_else(|| m.frame.as_ref().map(|(w, h, px)| (*w, *h, px.clone())));if let (Some((w, h, px)), Some(n)) = (pic, nodes.by_id.get(&id)) {let began = std::time::Instant::now();- set_image(doc, *n, w, h, px);+ set_image(doc, *n, w, h, px, std::mem::take(&mut m.halves));if let Some(s) = m.stats.as_mut().filter(|_| came > 0) {let ms = began.elapsed().as_secs_f64() * 1e3;(s.1, s.2, s.3, s.4) = (s.1 + 1, s.2 + came - 1, s.3 + ms, s.4.max(ms));@@ -185,9 +253,10 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &mum.shown_gap.0 = Some(began);let took = s.0.elapsed().as_secs_f64();if took >= 1.0 {- eprintln!("raea-page: video {took:.2} s: {} frames shown ({w}x{h}), {} passed over before the page drew, picture set avg {:.2} ms max {:.2} ms, longest gap {:.0} ms", s.1, s.2, s.3 / s.1.max(1) as f64, s.4, m.shown_gap.1);+ eprintln!("raea-page: video {took:.2} s: {} frames shown ({w}x{h}), {} passed over before the page drew, picture set avg {:.2} ms max {:.2} ms, longest gap {:.0} ms; the page synced {} times, longest apart {:.0} ms; shown late avg {:.1} ms max {:.1} ms", s.1, s.2, s.3 / s.1.max(1) as f64, s.4, m.shown_gap.1, m.paced.0, m.paced.1, m.paced.2 / s.1.max(1) as f64, m.paced.3);*s = (std::time::Instant::now(), 0, 0, 0.0, 0.0);m.shown_gap.1 = 0.0;+ m.paced = (0, 0.0, 0.0, 0.0);}}}@@ -195,6 +264,16 @@ pub fn sync(doc: &mut BaseDocument, nodes: &Nodes, medias: &mut Medias, out: &mu}}+/// Of the frames waiting, the one to show by `reach`: the latest whose moment has come, taken out with the ones before+/// it, and how many those were (frames nobody saw: the page drew less often than they came).+fn due_by<T>(ahead: &mut VecDeque<(f64, T, Instant, Halvings)>, reach: Instant) -> (Option<(f64, T, Instant, Halvings)>, u32) {+ let (mut due, mut passed) = (None, 0);+ while ahead.front().is_some_and(|f| f.2 <= reach) {+ passed += due.replace(ahead.pop_front().unwrap()).is_some() as u32;+ }+ (due, passed)+}+/// A playing element's time: each as `time` (its currentTime), and a `timeupdate` four times a second.fn told(m: &mut Media, id: u64, t: f64, out: &mut Vec<Value>) {out.push(json!({"id": id, "type": "time", "time": t}));@@ -219,12 +298,44 @@ fn poster_image(src: &str) -> Option<(u32, u32, Vec<u8>)> {Some((size.x, size.y, img.data?))}-/// Node `n` drawn as an image: `rgba` as its raster, its layout made again for the picture's size.-fn set_image(doc: &mut BaseDocument, n: blitz_dom::NodeId, w: u32, h: u32, rgba: Arc<Vec<u8>>) {+/// Node `n` drawn as an image: `rgba` as its raster. Its layout is made again only when the picture's size isn't the+/// last one's: a playing video's next frame is the same size, and making its box and its ancestors' layout again for+/// each was a part of every page frame it played in.+fn set_image(doc: &mut BaseDocument, n: blitz_dom::NodeId, w: u32, h: u32, rgba: Arc<Vec<u8>>, halves: Halvings) {let Some(node) = doc.get_node_mut(n) else { return };let Some(el) = node.element_data_mut() else { return };- el.special_data = SpecialElementData::Image(Box::new(ImageData::Raster(RasterImageData { width: w, height: h, data: vello::peniko::Blob::new(rgba) })));+ let same = matches!(&el.special_data, SpecialElementData::Image(i) if matches!(&**i, ImageData::Raster(r) if (r.width, r.height) == (w, h)));+ let image = RasterImageData { width: w, height: h, data: vello::peniko::Blob::new(rgba) };+ if !halves.is_empty() {+ raea_sys_page::preload_mips(&image, halves);+ }+ el.special_data = SpecialElementData::Image(Box::new(ImageData::Raster(image)));+ if same {+ return node.insert_damage(raea_sys_page::REPAINT_DAMAGE);+ }node.clear_layout_cache();node.insert_damage(raea_sys_page::ALL_DAMAGE);node.mark_ancestors_dirty();}++#[cfg(test)]+mod tests {+ use super::*;++ /// At a sync the frame shown is the latest one due by then; earlier ones due are passed over, later ones wait.+ #[test]+ fn the_frame_due_is_the_latest_whose_moment_has_come() {+ let t0 = Instant::now();+ let at = |ms: u64| t0 + Duration::from_millis(ms);+ let mut ahead: VecDeque<(f64, u8, Instant, Halvings)> = (0..4).map(|i| (i as f64 / 60.0, i as u8, at(17 * i as u64), Vec::new())).collect();+ // nothing is due before the first+ assert!(due_by(&mut ahead, t0 - Duration::from_millis(1)).0.is_none());+ // a page drawing as often as the frames come shows each+ assert_eq!(due_by(&mut ahead, at(0)).0.map(|f| f.1), Some(0));+ assert_eq!(due_by(&mut ahead, at(17)).0.map(|f| f.1), Some(1));+ // a page a frame late shows the latest and counts the one it passed+ let (due, passed) = due_by(&mut ahead, at(51));+ assert_eq!((due.map(|f| f.1), passed), (Some(3), 1));+ assert!(ahead.is_empty());+ }+}diff --git a/runtime/js/src/media_html.rs b/runtime/js/src/media_html.rsindex 55b93ba0..188fa922 100644--- a/runtime/js/src/media_html.rs+++ b/runtime/js/src/media_html.rs@@ -12,7 +12,10 @@ pub mod stream {/// where a playing source has got to, that it played to its end, or its failure.pub enum Msg {Meta { w: u32, h: u32, duration: f64 },- Frame(f64, raea_io_media::Frame),+ /// a frame to show now (the first, a seek's), with its halvings when they're asked for (`Cmd::Halvings`)+ Frame(f64, raea_io_media::Frame, Halvings),+ /// a playing video's frame, decoded ahead of its time: the frame at this time, to show at this moment+ Due(f64, raea_io_media::Frame, std::time::Instant, Halvings),/// a playing source's time (an audio one's, which has no frames; a video's when paused)Time(f64),/// playing from this time: the sound is on its way@@ -22,6 +25,35 @@ pub mod stream {Fail(String),}+ /// A frame's halvings, largest first: each a 2x2 mean of the one before (width, height, RGBA). A frame drawn at+ /// under half its size is drawn from one of them, so a 4K frame in a small player is neither sampled across+ /// four of its pixels at a time (it shimmers) nor sent whole to the GPU for every frame.+ pub type Halvings = Vec<(u32, u32, Vec<u8>)>;++ /// `n` halvings of an RGBA picture (fewer once a side is a pixel).+ pub fn halvings(w: u32, h: u32, rgba: &[u8], n: u8) -> Halvings {+ let mut out: Halvings = Vec::new();+ for _ in 0..n {+ let (sw, sh, src) = out.last().map_or((w, h, rgba), |(w, h, px)| (*w, *h, px.as_slice()));+ if sw < 2 || sh < 2 {+ break;+ }+ let (dw, dh) = ((sw / 2) as usize, (sh / 2) as usize);+ let stride = sw as usize * 4;+ let mut px = vec![0u8; dw * dh * 4];+ for (y, row) in px.chunks_exact_mut(dw * 4).enumerate() {+ let (top, bottom) = (&src[2 * y * stride..][..dw * 8], &src[(2 * y + 1) * stride..][..dw * 8]);+ for ((d, a), b) in row.chunks_exact_mut(4).zip(top.chunks_exact(8)).zip(bottom.chunks_exact(8)) {+ for c in 0..4 {+ d[c] = ((a[c] as u16 + a[c + 4] as u16 + b[c] as u16 + b[c + 4] as u16 + 2) / 4) as u8;+ }+ }+ }+ out.push((dw as u32, dh as u32, px));+ }+ out+ }+/// What the element asks of its decoder. Play plays from `at` on the source's clock (its sound's, when it has/// sound and a device): frames as they fall due, each `Frame` news, and `Ended` at its end unless `looping`.pub enum Cmd {@@ -31,6 +63,9 @@ pub mod stream {Muted(bool),/// the element's volume, 0..1: the sound's gain while it isn't mutedVolume(f32),+ /// how many halvings each frame should come with from here on (the page counts them from the size the+ /// video is drawn at; 0 while it is drawn at half its size or more)+ Halvings(u8),}pub struct Stream {@@ -133,7 +168,7 @@ pub mod stream {let i = dec.info().clone();let _ = tx.send(Msg::Meta { w: i.width, h: i.height, duration: i.duration });let _ = tx.send(match dec.frame_at(0.0) {- Ok(f) => Msg::Frame(0.0, f),+ Ok(f) => Msg::Frame(0.0, f, Vec::new()),Err(e) => Msg::Fail(e),});Some(crate::media_play::Source::Video(dec))diff --git a/runtime/js/src/media_play.rs b/runtime/js/src/media_play.rsindex cc2d797e..88732560 100644--- a/runtime/js/src/media_play.rs+++ b/runtime/js/src/media_play.rs@@ -1,13 +1,15 @@//! A `video` or `audio` playing (media_html.rs's stream, on its decoder's thread): seeks answered with the frame at the//! time asked; played, frames as they fall due on the source's clock, which is its sound's (media_sound.rs) when it has-//! sound and the Mac a device, else the wall's. Frames are asked for at the clock's time, so a decoder that falls-//! behind skips frames rather than letting the picture lag its sound.+//! sound and the Mac a device, else the wall's. A playing video's frames are decoded a little ahead of the clock and+//! sent with the moment each is due (`Msg::Due`), so how long a frame takes to decode is no part of when it is+//! shown, and a loop's first frame is ready before its last has gone. A decoder that falls behind the clock skips+//! frames rather than letting the picture lag its sound.use std::path::Path;use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender};use std::time::{Duration, Instant};-use crate::media_html::stream::{Cmd, Msg};+use crate::media_html::stream::{Cmd, Msg, halvings};use crate::media_sound::Sound;/// What was opened: a video's decoder, or an audio source's samples.@@ -33,10 +35,19 @@ pub struct Player<'a> {volume: f32,clock: Option<Clock>,looping: bool,- /// the frame (index at fps) last sent while playing, so a frame isn't decoded twice- shown: Option<i64>,+ /// the frame (index at fps) to decode next while playing+ cursor: i64,+ /// what the cursor's frames are ahead of the clock by, in laps: `duration` once the cursor has gone round a+ /// loop the clock hasn't yet, else 0+ lap: f64,+ /// frames the clock passed before they were decoded, since the last frame sent (for the stats)+ skipped: i64,/// when the last Time news went (an audio source's, which has no frames)told: Option<Instant>,+ /// how many halvings each frame is sent with (`Cmd::Halvings`)+ halvings: u8,+ /// where the picture last sent was in time, while paused: sent again when the halvings asked for change+ still: Option<f64>,/// RAEA_MEDIA_STATS=1: how play is going, said a line a secondstats: Option<Stats>,}@@ -49,7 +60,7 @@ struct Stats {skipped: u32,sum: f64,max: f64,- /// the longest wait between two frames sent, ms+ /// the longest wait between two frames decoded, msgap: f64,last: Option<Instant>,}@@ -81,7 +92,7 @@ impl<'a> Player<'a> {Source::Audio(a) => (a.duration(), 0.0),};let fps = if fps.is_finite() && fps > 1.0 { fps } else { 30.0 };- Player { path, source, duration, fps, sound: None, muted: false, volume: 1.0, clock: None, looping: false, shown: None, told: None, stats: (std::env::var("RAEA_MEDIA_STATS").as_deref() == Ok("1")).then(Stats::new) }+ Player { path, source, duration, fps, sound: None, muted: false, volume: 1.0, clock: None, looping: false, cursor: 0, lap: 0.0, skipped: 0, told: None, halvings: 0, still: Some(0.0), stats: (std::env::var("RAEA_MEDIA_STATS").as_deref() == Ok("1")).then(Stats::new) }}/// Answers the element's asks until it's gone (its stream dropped).@@ -119,6 +130,13 @@ impl<'a> Player<'a> {s.set_volume(v);}}+ Cmd::Halvings(n) => {+ // paused, the picture showing is sent again with what it is now drawn from+ if n > self.halvings && self.clock.is_none() && seek.is_none() {+ seek = self.still;+ }+ self.halvings = n;+ }}}if pause && self.clock.is_some() {@@ -152,20 +170,47 @@ impl<'a> Player<'a> {}}- /// How long until the next frame falls due on the clock. Waking every half frame instead (as this did) sent- /// frames 17 to 50 ms apart at 30 fps, whichever half the wake fell in: the picture juddered, and two frames- /// landing inside one of the page's frames lost the page one of them (RAEA_MEDIA_STATS, 11-13: longest gap 43 ms- /// every second, 1 to 3 frames a second passed over). A sound's clock moves in its device's steps, so a wake a- /// moment early waits again, briefly.+ /// How long until there is something to do: the cursor's frame comes within the lead, or the clock reaches the+ /// end. (Frames were once sent when they fell due, each after its own decode: 22 to 29 ms apart at 60 fps where+ /// 16.7 is even, a 4K frame 6.6 ms late, and a loop's first frame behind a seek; RAEA_MEDIA_STATS, 11-13.)fn until_next(&self) -> Duration {let frame = 1.0 / self.fps;- let wait = match (&self.source, self.shown) {- (Source::Video(_), Some(i)) => ((i + 1) as f64 * frame - self.now()).clamp(0.0005, frame),- _ => 0.5 * frame,+ let wait = match &self.source {+ Source::Video(_) => {+ let now = self.now();+ let decode = if self.at_end() { f64::INFINITY } else { self.cursor_time() - self.lead() - now };+ decode.min(self.duration - now).clamp(0.0005, frame)+ }+ Source::Audio(_) => 0.5 * frame,};Duration::from_secs_f64(wait)}+ /// The cursor's frame's time on the clock.+ fn cursor_time(&self) -> f64 {+ self.cursor as f64 / self.fps + self.lap+ }++ /// The cursor is past the source's last frame (a looping source's cursor goes round instead).+ fn at_end(&self) -> bool {+ !self.looping && self.duration.is_finite() && self.cursor as f64 / self.fps >= self.duration - 0.5 / self.fps+ }++ /// How far ahead of the clock frames are decoded: two frames, 50 ms at the least; and nearing a loop's end 150 ms,+ /// since going back to the start is a seek (70 ms measured) that must be over before the first frame is due. No+ /// more than 200 MB of frames wait at once (a 4K frame is 33 MB).+ fn lead(&self) -> f64 {+ let frame = 1.0 / self.fps;+ let bytes = match &self.source {+ Source::Video(d) => (d.info().width as f64 * d.info().height as f64 * 4.0).max(1.0),+ Source::Audio(_) => 1.0,+ };+ let most = (200e6 / bytes).floor().max(2.0) * frame;+ let near_wrap = self.looping && self.duration.is_finite() && self.duration - self.cursor as f64 * frame < 0.25;+ let want = if near_wrap || self.lap > 0.0 { 0.15 } else { (2.0 * frame).max(0.05) };+ want.min(most)+ }+/// The clock's time now.fn now(&self) -> f64 {match (&self.clock, &self.sound) {@@ -186,8 +231,13 @@ impl<'a> Player<'a> {let volume = self.volume;self.sound = Some(audio.and_then(|a| Sound::open(a, self.muted).inspect(|s| s.set_volume(volume)).inspect_err(|e| bevy::log::debug!("raea-runtime-js: media plays on the wall's clock: {e}")).ok()));}- self.shown = None;+ (self.cursor, self.lap, self.skipped) = ((t * self.fps).floor() as i64, 0.0, 0);self.told = None;+ self.restart(t);+ }++ /// The clock started at `t` (the sound from there, else the wall).+ fn restart(&mut self, t: f64) {self.clock = Some(match &self.sound {Some(Some(s)) => {s.start(t);@@ -211,36 +261,70 @@ impl<'a> Player<'a> {/// The frame at `t`, sent as the frame at exactly `t`; false when the element's gone.fn frame(&mut self, t: f64, tx: &Sender<Msg>) -> bool {match &mut self.source {- Source::Video(dec) => tx.send(dec.frame_at(t).map_or_else(Msg::Fail, |f| Msg::Frame(t, f))).is_ok(),+ Source::Video(dec) => {+ self.still = Some(t);+ let n = self.halvings;+ tx.send(dec.frame_at(t).map_or_else(Msg::Fail, |f| {+ let halves = halvings(f.width, f.height, &f.rgba, n);+ Msg::Frame(t, f, halves)+ }))+ .is_ok()+ }Source::Audio(_) => tx.send(Msg::Time(t)).is_ok(),}}- /// One step of playing: the end (again from the start when looping), or the frame falling due now.+ /// One step of playing: the end (the clock again from the start when looping), or the cursor's frame decoded+ /// and sent with the moment it is due, once it is within the lead. One frame a step, so an ask is never kept+ /// waiting behind more than one decode.fn tick(&mut self, tx: &Sender<Msg>) -> bool {- let t = self.now();- if self.duration.is_finite() && t >= self.duration - 0.5 / self.fps {- if self.looping {- self.go(0.0);- return self.frame(0.0, tx);+ let mut t = self.now();+ let frame = 1.0 / self.fps;+ if self.duration.is_finite() && self.looping && t >= self.duration {+ // round again: the frames already sent for this lap were timed on the old clock's end, which is now+ if self.lap > 0.0 {+ self.lap = 0.0;+ } else {+ self.cursor = 0;}+ self.restart(t - self.duration);+ t = self.now();+ } else if self.duration.is_finite() && !self.looping && t >= self.duration - 0.5 * frame {self.halt();return tx.send(Msg::Ended(self.duration)).is_ok();}match &self.source {Source::Video(_) => {- let i = (t * self.fps).floor() as i64;- if self.shown == Some(i) {+ // a looping source's cursor goes round ahead of the clock+ if self.looping && self.duration.is_finite() && self.lap == 0.0 && self.cursor as f64 * frame >= self.duration - 0.5 * frame {+ (self.cursor, self.lap) = (0, self.duration);+ }+ if self.at_end() || self.cursor_time() - self.lead() > t {return true;}- let skipped = self.shown.map_or(0, |s| i - s - 1);- self.shown = Some(i);+ // behind the clock by more than a frame: the frames it passed are skipped+ if self.cursor_time() + frame < t {+ let i = ((t - self.lap) * self.fps).floor() as i64;+ self.skipped += i - self.cursor;+ self.cursor = i;+ }+ let (at, due_in) = (self.cursor as f64 * frame, self.cursor_time() - t);let began = Instant::now();- let ok = self.frame(t, tx);+ let Source::Video(dec) = &mut self.source else { return true };+ // asked for mid-frame: a source whose frames aren't exactly on fps's grid gives the frame showing then+ let n = self.halvings;+ let got = dec.frame_at(at + 0.5 * frame).map(|f| {+ let halves = halvings(f.width, f.height, &f.rgba, n);+ (f, halves)+ });+ let took = began.elapsed();+ let due = began + Duration::from_secs_f64(due_in.max(0.0));+ self.cursor += 1;if let Some(s) = self.stats.as_mut() {- s.frame(began.elapsed(), skipped, self.fps);+ s.frame(took, std::mem::take(&mut self.skipped), self.fps);}- ok+ self.still = Some(at);+ tx.send(got.map_or_else(Msg::Fail, |(f, halves)| Msg::Due(at, f, due, halves))).is_ok()}Source::Audio(_) => {if self.told.is_some_and(|w| w.elapsed() < Duration::from_millis(100)) {@@ -290,7 +374,7 @@ mod tests {let Some(path) = clip() else { return eprintln!("no ffmpeg: skipped") };let mut st = stream::open(path.to_str().unwrap(), true).expect("a stream");assert!(matches!(next(&st, Duration::from_secs(10)), Some(Msg::Meta { w: 160, h: 90, .. })));- assert!(matches!(next(&st, Duration::from_secs(5)), Some(Msg::Frame(t, _)) if t == 0.0));+ assert!(matches!(next(&st, Duration::from_secs(5)), Some(Msg::Frame(t, ..)) if t == 0.0));let began = Instant::now();st.ask(Cmd::Play { at: 0.0, looping: false });@@ -298,9 +382,12 @@ mod tests {let (mut frames, mut last, mut ended) = (0, -1.0, None);while let Some(m) = next(&st, Duration::from_secs(3)) {match m {- Msg::Frame(t, f) => {+ Msg::Due(t, f, due, _) => {assert!(t > last, "frames in order: {t} after {last}");assert_eq!((f.width, f.height), (160, 90));+ // each is due when its time comes on the clock (the wall's, here), sent before then+ let on_clock = due.duration_since(began).as_secs_f64();+ assert!((on_clock - t).abs() < 0.06, "the frame at {t} is due at {on_clock}");(frames, last) = (frames + 1, t);}Msg::Ended(t) => {@@ -313,17 +400,21 @@ mod tests {let (end, took) = ended.expect("it ended");assert!((end - 1.0).abs() < 0.05, "ended at its duration: {end}");assert!((0.85..1.4).contains(&took), "a second's clip took {took} s");- assert!(frames >= 20, "{frames} frames of 30");+ assert!(frames >= 25, "{frames} frames of 30");st.ask(Cmd::Seek(0.5));- assert!(matches!(next(&st, Duration::from_secs(2)), Some(Msg::Frame(t, _)) if t == 0.5));+ assert!(matches!(next(&st, Duration::from_secs(2)), Some(Msg::Frame(t, ..)) if t == 0.5));st.ask(Cmd::Play { at: 0.5, looping: true });let (mut wrapped, mut last, until) = (false, 0.5, Instant::now() + Duration::from_millis(1200));while Instant::now() < until {match next(&st, Duration::from_millis(200)) {- Some(Msg::Frame(t, _)) => {- wrapped |= t < last;+ Some(Msg::Due(t, _, due, _)) => {+ // the first frame of the next lap is sent before the last frame of this one has gone+ if t < last {+ wrapped = true;+ assert!(due > Instant::now(), "the loop's first frame came after it was due");+ }last = t;}Some(Msg::Ended(_)) => panic!("a looping source doesn't end"),@@ -333,13 +424,22 @@ mod tests {assert!(wrapped, "looping went round");st.ask(Cmd::Pause);- let mut after = 0;- while let Some(m) = next(&st, Duration::from_millis(300)) {- if matches!(m, Msg::Frame(..)) {- after += 1;+ // paused, asked for halvings: the picture showing comes again with them, each half the one before+ while next(&st, Duration::from_millis(300)).is_some() {}+ st.ask(Cmd::Halvings(2));+ match next(&st, Duration::from_secs(2)) {+ Some(Msg::Frame(_, f, halves)) => {+ assert_eq!((f.width, f.height), (160, 90));+ assert_eq!(halves.iter().map(|(w, h, px)| (*w, *h, px.len())).collect::<Vec<_>>(), vec![(80, 45, 80 * 45 * 4), (40, 22, 40 * 22 * 4)]);+ // a level's pixel is the mean of the four it stands for+ let mean = |c: usize| ((f.rgba[c] as u16 + f.rgba[4 + c] as u16 + f.rgba[160 * 4 + c] as u16 + f.rgba[160 * 4 + 4 + c] as u16 + 2) / 4) as u8;+ assert_eq!(&halves[0].2[..4], &[mean(0), mean(1), mean(2), mean(3)]);}+ _ => panic!("no frame with its halvings"),}- assert!(after <= 1, "{after} frames after pausing");+ // what was on its way comes; then nothing more+ while next(&st, Duration::from_millis(300)).is_some() {}+ assert!(next(&st, Duration::from_millis(300)).is_none(), "frames after pausing");let _ = std::fs::remove_file(path);}}diff --git a/systems/page-ops/src/lib.rs b/systems/page-ops/src/lib.rsindex d65d4deb..0985ce8d 100644--- a/systems/page-ops/src/lib.rs+++ b/systems/page-ops/src/lib.rs@@ -434,3 +434,5 @@ pub fn menu_items(d: &BaseDocument, menu: NodeId) -> Vec<NodeId> {/// Everything about an element made again (blitz-dom's ALL_DAMAGE, crate-private there): what loading an image does.pub const ALL_DAMAGE: style::selector_parser::RestyleDamage = style::selector_parser::RestyleDamage::from_bits_retain(0b_0000_0000_1110_1111);+/// Only what an element is painted with changed, its box as it was: a video's next frame, the same size as its last.+pub const REPAINT_DAMAGE: style::selector_parser::RestyleDamage = style::selector_parser::RestyleDamage::REPAINT;diff --git a/systems/page/src/lib.rs b/systems/page/src/lib.rsindex 5b2c19a5..27b97add 100644--- a/systems/page/src/lib.rs+++ b/systems/page/src/lib.rs@@ -314,7 +314,13 @@ pub fn iframes() -> bool {std::env::var_os("RAEA_PAGE_IFRAMES").is_none_or(|v| v != "0")}-pub use ops::ALL_DAMAGE;+pub use ops::{ALL_DAMAGE, REPAINT_DAMAGE};++/// A picture's halvings handed to the painter ready made (blitz-paint preload_mips): a video's frame is then drawn+/// from the one nearest its drawn size. On the page's thread, where it is painted.+pub fn preload_mips(image: &blitz_dom::node::RasterImageData, levels: Vec<(u32, u32, Vec<u8>)>) {+ blitz_paint::preload_mips(image, levels.into_iter().map(|(w, h, px)| blitz_dom::node::RasterImageData::new(w, h, std::sync::Arc::new(px))).collect());+}/// A document for a page `w` x `h` on its own entity: html, head and a body with no margin and no background (the/// page is drawn over 3D and video), which is the root the composition's elements go in. (host.rs opens a root's so.)diff --git a/vendor/blitz-paint/src/lib.rs b/vendor/blitz-paint/src/lib.rsindex 952b58cf..099cf575 100644--- a/vendor/blitz-paint/src/lib.rs+++ b/vendor/blitz-paint/src/lib.rs@@ -36,6 +36,8 @@ pub struct TilePattern {pub tile: kurbo::Size,}+pub use render::preload_mips;+/// raea: the node whose box or text the paint calls going out now draw (a glyph run, the element its text is in), for/// a painter that tells nodes apart: the layer pass's ID scene (raea-sys-layers, docs/review.feature R2b). None outside/// paint_scene.diff --git a/vendor/blitz-paint/src/render.rs b/vendor/blitz-paint/src/render.rsindex 1d37ee1a..37ed9d62 100644--- a/vendor/blitz-paint/src/render.rs+++ b/vendor/blitz-paint/src/render.rs@@ -1516,8 +1516,19 @@ fn halve(src: &RasterImageData) -> RasterImageData {RasterImageData::new(w, h, Arc::new(out))}-/// The level of `image` to sample when it is drawn at `scale` of its size: the image itself, or a halving of it-fn mip_level(image: &RasterImageData, scale: f64) -> RasterImageData {+/// raea: an image's halvings handed over ready made (each half the last, the first half the image): a video's frame,+/// halved on its decoder's thread. Kept while the image lives, as the ones made here are.+pub fn preload_mips(image: &RasterImageData, levels: Vec<RasterImageData>) {+ MIPS.with_borrow_mut(|mips| {+ mips.retain(|_, (weak, _)| weak.upgrade().is_some());+ mips.insert(image.data.id(), (image.data.downgrade(), Some(levels)));+ });+}++/// The level of `image` to sample when it is drawn at `scale` of its size: the image itself, or a halving of it.+/// `make`: halvings not there yet are made now (an img's, once); without it only the ones handed over are used (a+/// video's frame is a new picture each time: nothing is halved for it on the page's thread).+fn mip_level(image: &RasterImageData, scale: f64, make: bool) -> RasterImageData {let mut want = 0;while scale * f64::from(1u32 << (want + 1)) <= 1.0 && image.width >> (want + 1) >= 1 && image.height >> (want + 1) >= 1 {want += 1;@@ -1526,6 +1537,12 @@ fn mip_level(image: &RasterImageData, scale: f64) -> RasterImageData {return image.clone();}MIPS.with_borrow_mut(|mips| {+ if !make {+ return match mips.get(&image.data.id()) {+ Some((_, Some(levels))) if !levels.is_empty() => levels[want.min(levels.len()) - 1].clone(),+ _ => image.clone(),+ };+ }mips.retain(|_, (weak, _)| weak.upgrade().is_some());// an all-zero image is a placeholder whose blob keys a GPU texture's override (raea-page's views, filters):// drawn as is, or the override is lost@@ -2385,10 +2402,12 @@ impl ElementCx<'_, '_> {}}// raea: a minified <img> is drawn from its halving nearest its drawn size (mip_level); a <video>'s frames- // are each a new picture, so they're drawn as they come, as raea's GPU-texture videos are+ // are each a new picture, so one is drawn from the halvings it came with (preload_mips), as it comes+ // when it came with nonelet outer = self.transform.determinant().abs().sqrt();- let mip = quality != peniko::ImageQuality::Low && self.element.name.local == local_name!("img");- let level = if mip { mip_level(image, x_scale.min(y_scale) * outer) } else { image.clone() };+ let img = self.element.name.local == local_name!("img");+ let mip = quality != peniko::ImageQuality::Low && (img || self.element.name.local == local_name!("video"));+ let level = if mip { mip_level(image, x_scale.min(y_scale) * outer, img) } else { image.clone() };let (lx, ly) = (image.width as f64 / level.width as f64, image.height as f64 / level.height as f64);let image = &level;let transform = self