From 2183ee73c42354686ca330ba22f27553d43954ba Mon Sep 17 00:00:00 2001 From: selikhov Date: Tue, 29 Sep 2026 13:39:12 +0300 Subject: [PATCH] voelin-media: composite a studio scene into pooled frames The studio needs one picture per output frame from any number of sources that each run at their own pace, so the compositor takes each source's newest frame through a latest-wins handoff and draws the scene back to front into a pooled RGBA canvas, in bands of rows on a worker pool. The plan (destination rectangles and filter weights) is kept while neither the scene nor a source size changes, which is what lets a steady composite allocate nothing per frame -- the property tests/studio_alloc.rs pins down. Frames are RGBA because alpha blending and text need it; the streamer's existing conversion turns the canvas into I420 for the encoders in one SIMD pass. Scaling reuses scale.rs's weight tables with an inner loop for interleaved pixels, and a straight copy at 1:1, the common case for a screen that already matches the canvas. FramePool grew a format so the same recycling works for the canvas, and the preview tap scales the canvas down through the same filter. Co-Authored-By: Claude Opus 5 --- crates/voelin-media/src/pool.rs | 60 +- crates/voelin-media/src/scale.rs | 10 +- crates/voelin-media/src/studio/compose.rs | 984 ++++++++++++++++++++++ crates/voelin-media/src/studio/mod.rs | 3 + crates/voelin-media/tests/studio_alloc.rs | 195 +++++ 5 files changed, 1233 insertions(+), 19 deletions(-) create mode 100644 crates/voelin-media/src/studio/compose.rs create mode 100644 crates/voelin-media/tests/studio_alloc.rs diff --git a/crates/voelin-media/src/pool.rs b/crates/voelin-media/src/pool.rs index 34b8c5f..53a68e6 100644 --- a/crates/voelin-media/src/pool.rs +++ b/crates/voelin-media/src/pool.rs @@ -1,7 +1,7 @@ //! Recycled I420 frames. //! //! A [`FramePool`] belongs to one producer (e.g. the capture thread). It -//! hands out frames of one size for writing; the producer shares them as +//! hands out frames of one size and format for writing; the producer shares them as //! `Arc`, and once every consumer dropped its clone the frame is //! free again. Frames are allocated only when all are in use or the size //! changes, so a steady stream reuses the same few buffers. @@ -9,16 +9,22 @@ use std::sync::Arc; use std::time::Duration; -use crate::frame::{FrameData, Plane, VideoFrame, chroma_size}; +use crate::frame::{FrameData, PixelFormat, Plane, VideoFrame, chroma_size}; -/// Recycled I420 frames of one size (tightly packed planes). -#[derive(Default)] +/// Recycled frames of one size and pixel format (tightly packed planes). pub struct FramePool { frames: Vec>, size: (u32, u32), + format: PixelFormat, allocated: u64, } +impl Default for FramePool { + fn default() -> Self { + Self { frames: Vec::new(), size: (0, 0), format: PixelFormat::I420, allocated: 0 } + } +} + impl FramePool { pub fn new() -> Self { Self::default() @@ -29,16 +35,27 @@ impl FramePool { /// share it. Changing the size drops the pool's other frames (those still /// in use are freed by their last user). pub fn get(&mut self, width: u32, height: u32) -> &mut Arc { - if self.size != (width, height) { + self.get_format(width, height, PixelFormat::I420) + } + + /// As [`FramePool::get`], of `format` (I420, NV12, BGRA or RGBA). + pub fn get_format( + &mut self, + width: u32, + height: u32, + format: PixelFormat, + ) -> &mut Arc { + if self.size != (width, height) || self.format != format { self.frames.clear(); self.size = (width, height); + self.format = format; } let free = self.frames.iter_mut().position(|f| Arc::get_mut(f).is_some()); let index = match free { Some(i) => i, None => { self.allocated += 1; - self.frames.push(Arc::new(blank_i420(width, height))); + self.frames.push(Arc::new(blank(width, height, format))); self.frames.len() - 1 } }; @@ -60,19 +77,23 @@ impl FramePool { } } -/// A zeroed I420 frame with tightly packed planes. -fn blank_i420(width: u32, height: u32) -> VideoFrame { +/// A zeroed frame with tightly packed planes. +fn blank(width: u32, height: u32, format: PixelFormat) -> VideoFrame { + let (w, h) = (width as usize, height as usize); let (cw, ch) = chroma_size(width, height); - VideoFrame { - width, - height, - timestamp: Duration::ZERO, - data: FrameData::I420 { - y: Plane::filled(width as usize, height as usize, 0), + let data = match format { + PixelFormat::I420 => FrameData::I420 { + y: Plane::filled(w, h, 0), u: Plane::filled(cw, ch, 0), v: Plane::filled(cw, ch, 0), }, - } + PixelFormat::Nv12 => { + FrameData::Nv12 { y: Plane::filled(w, h, 0), uv: Plane::filled(cw * 2, ch, 0) } + } + PixelFormat::Bgra => FrameData::Bgra(Plane::filled(w * 4, h, 0)), + PixelFormat::Rgba => FrameData::Rgba(Plane::filled(w * 4, h, 0)), + }; + VideoFrame { width, height, timestamp: Duration::ZERO, data } } #[cfg(test)] @@ -96,5 +117,14 @@ mod tests { assert_eq!((d.width, d.height), (32, 16)); d.validate().unwrap(); assert_eq!(pool.len(), 1); + // So does a new format. + drop(d); + let e = pool.get_format(32, 16, PixelFormat::Rgba).clone(); + assert_eq!(e.format(), PixelFormat::Rgba); + e.validate().unwrap(); + assert_eq!(pool.len(), 1); + drop(e); + assert!(pool.get_format(32, 16, PixelFormat::Rgba).format() == PixelFormat::Rgba); + assert_eq!(pool.allocated(), 5, "a frame is reused within one size and format"); } } diff --git a/crates/voelin-media/src/scale.rs b/crates/voelin-media/src/scale.rs index 155662b..0784113 100644 --- a/crates/voelin-media/src/scale.rs +++ b/crates/voelin-media/src/scale.rs @@ -23,7 +23,8 @@ use crate::{Error, Result}; /// Fixed-point scale of filter weights. const ONE: u32 = 1 << 14; /// Fractional bits kept between the vertical and the horizontal pass. -const MID_BITS: u32 = 6; +/// [`crate::studio::compose`] uses the same two-pass layout for RGBA. +pub(crate) const MID_BITS: u32 = 6; /// Filter taps of one output coordinate: source index of the first tap, and /// where its weights start. @@ -36,14 +37,14 @@ struct Taps { /// The filter along one axis. #[derive(Clone, Debug, Default)] -struct Axis { +pub(crate) struct Axis { taps: Vec, weights: Vec, } impl Axis { /// Weights (summing to [`ONE`]) mapping `src` samples to `dst`. - fn new(src: usize, dst: usize) -> Self { + pub(crate) fn new(src: usize, dst: usize) -> Self { let mut axis = Self { taps: Vec::with_capacity(dst), weights: Vec::new() }; let ratio = src as f64 / dst as f64; let mut scratch: Vec<(usize, f64)> = Vec::new(); @@ -92,7 +93,8 @@ impl Axis { self.taps.push(Taps { start: taps[0].0 as u32, len: taps.len() as u32, weights: offset }); } - fn get(&self, o: usize) -> (usize, &[u32]) { + /// Source index of the first tap of output `o`, and its weights. + pub(crate) fn get(&self, o: usize) -> (usize, &[u32]) { let t = self.taps[o]; (t.start as usize, &self.weights[t.weights as usize..(t.weights + t.len) as usize]) } diff --git a/crates/voelin-media/src/studio/compose.rs b/crates/voelin-media/src/studio/compose.rs new file mode 100644 index 0000000..3078645 --- /dev/null +++ b/crates/voelin-media/src/studio/compose.rs @@ -0,0 +1,984 @@ +//! Compositing a scene into one picture. +//! +//! Each source runs on its own thread and publishes its newest picture into a +//! [`Feed`] (a one-slot latest-wins [`Handoff`]); the compositor takes the +//! newest one of every source and draws them back to front into a pooled +//! RGBA frame at the output size. A source that has not delivered a new +//! picture keeps the one it had, so a 5 fps camera does not blink in a 60 fps +//! composite. +//! +//! The plan (which source rectangle goes where, and the filter weights for +//! it) is rebuilt only when the scene, a source size or the output size +//! changes, and the canvas comes from a [`FramePool`], so a steady composite +//! allocates nothing: [`Compositor::compose`] is on the video path. +//! +//! Drawing is split into bands of output rows on a [`Workers`] pool; every +//! band draws every source that reaches into it, in scene order, so the +//! result is the same as drawing the whole canvas source by source. +//! +//! Scaling uses the same separable fixed-point filter as [`crate::scale`] +//! (area averaging down, bilinear up) with an inner loop for interleaved +//! RGBA, and a straight copy at 1:1. Sources with an alpha channel (images +//! and text) and sources below full opacity are blended over what is +//! already there. + +use std::ops::Range; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex, PoisonError}; +use std::time::{Duration, Instant}; + +use crate::frame::{FrameData, PixelFormat, PlaneRef, VideoFrame}; +use crate::handoff::Handoff; +use crate::pool::FramePool; +use crate::scale::{Axis, MID_BITS}; +use crate::studio::scene::{Fit, Scene, SourceKind}; +use crate::workers::{Slots, Workers}; +use crate::{Error, Result}; + +fn lock(m: &Mutex) -> std::sync::MutexGuard<'_, T> { + m.lock().unwrap_or_else(PoisonError::into_inner) +} + +/// What the compositor draws one source from: its newest picture, plus what +/// the source thread wants to report. +/// +/// Sources publish BGRA or RGBA frames (that is what compositing needs); +/// [`crate::convert::to_rgba`] turns a camera's YUV into one. +#[derive(Default)] +pub struct Feed { + frames: Handoff, + delivered: AtomicU64, + /// `width << 32 | height` of the last frame. + size: AtomicU64, + /// Why the source shows nothing (no such camera, the window closed, a + /// file that cannot be read, ...). + error: Mutex>, +} + +impl Feed { + pub fn new() -> Self { + Self::default() + } + + /// Offer the newest picture; an older one the compositor has not taken is + /// replaced. + pub fn put(&self, frame: Arc) { + self.size.store(u64::from(frame.width) << 32 | u64::from(frame.height), Ordering::Relaxed); + self.delivered.fetch_add(1, Ordering::Relaxed); + self.frames.put(frame); + } + + /// The newest picture, if one arrived since the last call. + pub fn take(&self) -> Option> { + self.frames.take() + } + + /// Frames the source delivered. + pub fn delivered(&self) -> u64 { + self.delivered.load(Ordering::Relaxed) + } + + /// Frames replaced before the compositor took them (the source is faster + /// than the composite). + pub fn dropped(&self) -> u64 { + self.frames.replaced() + } + + /// Size of the last frame, or `(0, 0)`. + pub fn size(&self) -> (u32, u32) { + let size = self.size.load(Ordering::Relaxed); + ((size >> 32) as u32, size as u32) + } + + pub fn set_error(&self, error: impl ToString) { + *lock(&self.error) = Some(error.to_string()); + } + + pub fn clear_error(&self) { + *lock(&self.error) = None; + } + + pub fn error(&self) -> Option { + lock(&self.error).clone() + } + + /// Tell the source thread to stop. + pub fn close(&self) { + self.frames.close(); + } + + pub fn is_closed(&self) -> bool { + self.frames.is_closed() + } +} + +/// `v / 255`, rounded. +#[inline] +fn div255(v: u32) -> u8 { + let t = v + 128; + ((t + (t >> 8)) >> 8) as u8 +} + +/// Write one RGB pixel with coverage `a` (0..=255) over `dst`. +#[inline] +fn put_pixel(dst: &mut [u8], rgb: [u8; 3], a: u32) { + if a == 255 { + dst[..3].copy_from_slice(&rgb); + } else if a > 0 { + for c in 0..3 { + dst[c] = div255(u32::from(rgb[c]) * a + u32::from(dst[c]) * (255 - a)); + } + } + // The canvas is opaque: it becomes I420 for the encoders. + dst[3] = 255; +} + +/// One source rectangle mapped onto the canvas, with its filter weights. +struct Blit { + /// Which of the scene's sources this draws. + source: usize, + /// Source rectangle after the crop, in source pixels. + sx: usize, + sy: usize, + sw: usize, + sh: usize, + /// Where the unclipped destination starts on the canvas (may be + /// negative). + dx: isize, + dy: isize, + /// Size of the unclipped destination. + dw: usize, + dh: usize, + /// Destination columns and rows on the canvas, in destination + /// coordinates. + cols: Range, + rows: Range, + x: Axis, + y: Axis, + /// Same size: copy rows instead of filtering. + direct: bool, + /// The source's bytes are B, G, R, A. + bgra: bool, + /// The source's alpha channel means something (images, text). + alpha: bool, + /// 0..=255. + opacity: u8, + /// Mirror horizontally (front cameras). + mirror: bool, +} + +impl Blit { + /// Destination rows of this blit inside canvas rows `band`. + fn band_rows(&self, band: &Range) -> Range { + let first = (band.start as isize - self.dy).max(self.rows.start as isize) as usize; + let last = (band.end as isize - self.dy).clamp(0, self.rows.end as isize) as usize; + first..last.max(first) + } + + /// Draw the rows of this blit that fall in canvas rows `band`. `out` + /// starts at canvas row `band.start`; `tmp` is scratch for one + /// vertically filtered source row. + fn draw( + &self, + src: PlaneRef<'_>, + out: &mut [u8], + out_stride: usize, + band: &Range, + tmp: &mut [u16], + ) { + let rows = self.band_rows(band); + if rows.is_empty() || self.cols.is_empty() { + return; + } + let opaque = self.opacity == 255 && !self.alpha; + for r in rows { + let line = ((self.dy + r as isize) as usize - band.start) * out_stride; + let out_x = (self.dx + self.cols.start as isize) as usize * 4; + let line = &mut out[line + out_x..][..self.cols.len() * 4]; + if self.direct { + self.copy_row(src, line, r, opaque); + } else { + self.filter_row(src, line, r, opaque, tmp); + } + } + } + + /// One destination row at 1:1. + fn copy_row(&self, src: PlaneRef<'_>, line: &mut [u8], r: usize, opaque: bool) { + let row = src.row(self.sy + r, (self.sx + self.sw) * 4); + let first = if self.mirror { self.dw - self.cols.end } else { self.cols.start }; + let row = &row[(self.sx + first) * 4..][..self.cols.len() * 4]; + if opaque && !self.bgra && !self.mirror { + line.copy_from_slice(row); + for px in line.chunks_exact_mut(4) { + px[3] = 255; + } + return; + } + let pixels = row.chunks_exact(4); + // Mirrored: the destination's first column is the source's last. + let opacity = u32::from(self.opacity); + let write = |px: &[u8], dst: &mut [u8]| { + let rgb = if self.bgra { [px[2], px[1], px[0]] } else { [px[0], px[1], px[2]] }; + let a = if self.alpha { opacity * u32::from(px[3]) / 255 } else { opacity }; + put_pixel(dst, rgb, a); + }; + if self.mirror { + for (px, dst) in pixels.rev().zip(line.chunks_exact_mut(4)) { + write(px, dst); + } + } else { + for (px, dst) in pixels.zip(line.chunks_exact_mut(4)) { + write(px, dst); + } + } + } + + /// One destination row through the separable filter: a vertical pass into + /// `tmp` (with [`MID_BITS`] fractional bits), then a horizontal one. + fn filter_row( + &self, + src: PlaneRef<'_>, + line: &mut [u8], + r: usize, + opaque: bool, + tmp: &mut [u16], + ) { + let bytes = self.sw * 4; + let (start, weights) = self.y.get(r); + const CHUNK: usize = 64; + let mut acc = [0u32; CHUNK]; + let mut x0 = 0; + while x0 < bytes { + let n = CHUNK.min(bytes - x0); + let acc = &mut acc[..n]; + acc.fill(0); + for (k, &w) in weights.iter().enumerate() { + let row = src.row(self.sy + start + k, (self.sx + self.sw) * 4); + for (a, &p) in acc.iter_mut().zip(&row[self.sx * 4 + x0..][..n]) { + *a += w * u32::from(p); + } + } + for (t, &a) in tmp[x0..x0 + n].iter_mut().zip(acc.iter()) { + *t = ((a + (1 << (13 - MID_BITS))) >> (14 - MID_BITS)) as u16; + } + x0 += n; + } + let opacity = u32::from(self.opacity); + for (i, dst) in line.chunks_exact_mut(4).enumerate() { + let ox = self.cols.start + i; + let (start, weights) = self.x.get(if self.mirror { self.dw - 1 - ox } else { ox }); + let mut px = [0u32; 4]; + for (k, &w) in weights.iter().enumerate() { + let tap = &tmp[(start + k) * 4..][..4]; + for (p, &t) in px.iter_mut().zip(tap) { + *p += w * u32::from(t); + } + } + const ROUND: u32 = 1 << (13 + MID_BITS); + let s = px.map(|p| ((p + ROUND) >> (14 + MID_BITS)).min(255) as u8); + let rgb = if self.bgra { [s[2], s[1], s[0]] } else { [s[0], s[1], s[2]] }; + let a = if opaque { + 255 + } else if self.alpha { + opacity * u32::from(s[3]) / 255 + } else { + opacity + }; + put_pixel(dst, rgb, a); + } + } +} + +/// How the compositor is doing. Read through [`Compositor::stats`]. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct ComposeStats { + pub frames: u64, + /// Mean time to composite a frame. + pub compose_time: Duration, + /// Times the plan was rebuilt (a scene change, a source that resized). + pub replans: u64, + /// Canvases the pool had to allocate. + pub allocated: u64, + pub threads: usize, +} + +/// Composites a scene into pooled RGBA frames; see the [module docs](self). +pub struct Compositor { + workers: Workers, + pool: FramePool, + size: (u32, u32), + /// Each scene source's newest picture (an empty feed keeps the last one). + held: Vec>>, + /// The source ids `held` belongs to. + held_ids: Vec, + blits: Vec, + /// One vertically filtered source row per band. + scratch: Vec>>, + /// The background colour as one output row, memcpy'd per row. + background: Vec, + /// What the plan was made for. + planned: Option<(u64, (u32, u32))>, + planned_sizes: Vec<(u32, u32)>, + frames: u64, + compose_ns: u64, + replans: u64, +} + +impl Compositor { + /// `threads` as in [`Workers::new`] (0: one per CPU). + pub fn new(threads: usize) -> Self { + Self { + workers: Workers::new("voelin-compose", threads), + pool: FramePool::new(), + size: (0, 0), + held: Vec::new(), + held_ids: Vec::new(), + blits: Vec::new(), + scratch: Vec::new(), + background: Vec::new(), + planned: None, + planned_sizes: Vec::new(), + frames: 0, + compose_ns: 0, + replans: 0, + } + } + + /// Output size of the composite; at least 2x2 and even. + pub fn set_size(&mut self, width: u32, height: u32) { + self.size = (width.max(2) & !1, height.max(2) & !1); + } + + pub fn size(&self) -> (u32, u32) { + self.size + } + + pub fn stats(&self) -> ComposeStats { + ComposeStats { + frames: self.frames, + compose_time: match self.frames { + 0 => Duration::ZERO, + n => Duration::from_nanos(self.compose_ns / n), + }, + replans: self.replans, + allocated: self.pool.allocated(), + threads: self.workers.threads(), + } + } + + /// Drop every picture held from an earlier scene. + pub fn reset(&mut self) { + self.held.clear(); + self.held_ids.clear(); + self.planned = None; + } + + /// Composite `scene` into a pooled RGBA frame stamped `timestamp`. + /// + /// `feeds[i]` is the live input of `scene.sources[i]` (`None`: not + /// running, drawn as nothing). `revision` must change whenever `scene` + /// does; the plan and its filter weights are kept while it and the source + /// sizes stay the same. + pub fn compose( + &mut self, + scene: &Scene, + revision: u64, + feeds: &[Option>], + timestamp: Duration, + ) -> Result> { + let (width, height) = self.size; + if width == 0 || height == 0 { + return Err(Error::InvalidFrame("the studio output size is not set".into())); + } + let started = Instant::now(); + self.collect(scene, feeds); + if self.planned != Some((revision, self.size)) + || self.planned_sizes.len() != scene.sources.len() + || self.held.iter().zip(&self.planned_sizes).any(|(held, planned)| { + held.as_ref().map_or((0, 0), |f| (f.width, f.height)) != *planned + }) { + self.plan(scene, revision); + } + let canvas = self.pool.get_format(width, height, PixelFormat::Rgba); + let frame = Arc::get_mut(canvas).expect("the pool hands out unshared frames"); + frame.timestamp = timestamp; + let FrameData::Rgba(plane) = &mut frame.data else { + return Err(Error::InvalidFrame("the studio canvas is not RGBA".into())); + }; + let stride = plane.stride; + let tasks = self.workers.tasks(height as usize); + let band = (height as usize).div_ceil(tasks); + let tasks = (height as usize).div_ceil(band); + while self.scratch.len() < tasks { + self.scratch.push(Mutex::new(Vec::new())); + } + let longest = self.blits.iter().map(|b| b.sw * 4).max().unwrap_or(0); + for slot in &mut self.scratch { + let tmp = slot.get_mut().unwrap_or_else(PoisonError::into_inner); + if tmp.len() < longest { + tmp.resize(longest, 0); + } + } + let slots = Slots::new(plane.data.chunks_mut(band * stride).take(tasks).enumerate()); + let (blits, held, background, scratch) = + (&self.blits, &self.held, &self.background, &self.scratch); + self.workers.run(slots.len(), &|task| { + let Some((i, out)) = slots.take(task) else { return }; + let rows = i * band..((i + 1) * band).min(height as usize); + for row in 0..rows.len() { + out[row * stride..][..background.len()].copy_from_slice(background); + } + let mut tmp = lock(&scratch[i]); + for blit in blits { + let Some(frame) = held[blit.source].as_ref() else { continue }; + let plane = match &frame.data { + FrameData::Bgra(p) | FrameData::Rgba(p) => p.view(), + // The plan only makes blits for packed sources. + _ => continue, + }; + blit.draw(plane, out, stride, &rows, &mut tmp); + } + }); + self.frames += 1; + self.compose_ns += started.elapsed().as_nanos() as u64; + Ok(canvas.clone()) + } + + /// Take each source's newest picture, keeping the last one of a source + /// that has nothing new. + fn collect(&mut self, scene: &Scene, feeds: &[Option>]) { + if self.held_ids.len() != scene.sources.len() + || self.held_ids.iter().ne(scene.sources.iter().map(|s| &s.id)) + { + // The scene's sources changed: keep each source's picture by id. + let old: Vec<(u64, Option>)> = + self.held_ids.drain(..).zip(self.held.drain(..)).collect(); + for source in &scene.sources { + self.held_ids.push(source.id); + let kept = old.iter().find(|(id, _)| *id == source.id); + self.held.push(kept.and_then(|(_, frame)| frame.clone())); + } + } + for (i, source) in scene.sources.iter().enumerate() { + // No live input, or not drawn: nothing, not even what it had. + let Some(Some(feed)) = feeds.get(i).filter(|_| source.drawn()) else { + self.held[i] = None; + continue; + }; + if let Some(frame) = feed.take() { + self.held[i] = Some(frame); + } + } + } + + /// Work out where every source goes and the filter weights for it. + fn plan(&mut self, scene: &Scene, revision: u64) { + let (width, height) = (self.size.0 as usize, self.size.1 as usize); + self.blits.clear(); + self.planned_sizes.clear(); + self.planned = Some((revision, self.size)); + self.replans += 1; + let rgba = scene.background.to_rgba(); + self.background.clear(); + self.background.reserve(width * 4); + for _ in 0..width { + self.background.extend_from_slice(&[rgba[0], rgba[1], rgba[2], 255]); + } + for (i, source) in scene.sources.iter().enumerate() { + let size = self.held[i].as_ref().map_or((0, 0), |f| (f.width, f.height)); + self.planned_sizes.push(size); + let Some(frame) = &self.held[i] else { continue }; + if !matches!(frame.data, FrameData::Bgra(_) | FrameData::Rgba(_)) { + continue; + } + let (cx, cy, mut cw, mut ch) = source.crop.apply(size.0, size.1); + let (mut sx, mut sy) = (cx as usize, cy as usize); + let t = &source.transform; + // The box the source is drawn into. + let bw = t.width.unwrap_or(cw as f32 * t.scale).max(1.0); + let bh = t.height.unwrap_or(ch as f32 * t.scale).max(1.0); + let (mut dx, mut dy) = (t.x, t.y); + let (mut dw, mut dh) = (bw, bh); + match t.fit { + Fit::Stretch => {} + Fit::Contain => { + // The whole source inside the box, centred. + let s = (bw / cw as f32).min(bh / ch as f32); + dw = (cw as f32 * s).max(1.0); + dh = (ch as f32 * s).max(1.0); + dx += (bw - dw) / 2.0; + dy += (bh - dh) / 2.0; + } + Fit::Cover => { + // Fill the box: crop the source to the box's aspect. + let s = (bw / cw as f32).max(bh / ch as f32); + let (kw, kh) = ((bw / s).round() as u32, (bh / s).round() as u32); + let (kw, kh) = (kw.clamp(1, cw), kh.clamp(1, ch)); + sx += ((cw - kw) / 2) as usize; + sy += ((ch - kh) / 2) as usize; + (cw, ch) = (kw, kh); + } + } + let (dw, dh) = (dw.round().max(1.0) as usize, dh.round().max(1.0) as usize); + let (dx, dy) = (dx.round() as isize, dy.round() as isize); + // Clip to the canvas, in destination coordinates. + let cols = clip(dx, dw, width); + let rows = clip(dy, dh, height); + if cols.is_empty() || rows.is_empty() { + continue; + } + let (sw, sh) = (cw as usize, ch as usize); + let mirror = matches!(source.kind, SourceKind::Camera { mirror: true, .. }); + self.blits.push(Blit { + source: i, + sx, + sy, + sw, + sh, + dx, + dy, + dw, + dh, + cols, + rows, + x: if sw == dw { Axis::default() } else { Axis::new(sw, dw) }, + y: if sh == dh { Axis::default() } else { Axis::new(sh, dh) }, + direct: sw == dw && sh == dh, + bgra: matches!(frame.data, FrameData::Bgra(_)), + alpha: matches!(source.kind, SourceKind::Image { .. } | SourceKind::Text { .. }), + opacity: (source.opacity.clamp(0.0, 1.0) * 255.0).round() as u8, + mirror, + }); + } + } +} + +/// The part of a `len`-wide destination starting at `at` that lies within +/// `0 .. limit`, in destination coordinates. +fn clip(at: isize, len: usize, limit: usize) -> Range { + let start = (-at).clamp(0, len as isize) as usize; + let end = (limit as isize - at).clamp(0, len as isize) as usize; + start..end.max(start) +} + +/// Resizes packed RGBA / BGRA images into RGBA, with the filter of +/// [`Compositor`]. Used for the studio's preview tap; weights are computed +/// once per size pair. +pub struct RgbaScaler { + workers: Workers, + pool: FramePool, + blit: Option, + scratch: Vec>>, +} + +impl RgbaScaler { + /// `threads` as in [`Workers::new`] (0: one per CPU). + pub fn new(threads: usize) -> Self { + Self { + workers: Workers::new("voelin-rgba-scale", threads), + pool: FramePool::new(), + blit: None, + scratch: Vec::new(), + } + } + + /// `frame` (BGRA or RGBA) as a pooled RGBA frame of `width` x `height`, + /// keeping its timestamp. Allocates only when a size changes or every + /// pooled frame is in use. + pub fn scale( + &mut self, + frame: &VideoFrame, + width: u32, + height: u32, + ) -> Result> { + let (width, height) = (width.max(1), height.max(1)); + let plane = match &frame.data { + FrameData::Bgra(p) | FrameData::Rgba(p) => p.view(), + _ => return Err(Error::InvalidFrame("the preview scaler needs BGRA or RGBA".into())), + }; + let (sw, sh) = (frame.width as usize, frame.height as usize); + let (dw, dh) = (width as usize, height as usize); + let bgra = matches!(frame.data, FrameData::Bgra(_)); + let stale = self + .blit + .as_ref() + .is_none_or(|b| (b.sw, b.sh, b.dw, b.dh, b.bgra) != (sw, sh, dw, dh, bgra)); + if stale { + self.blit = Some(Blit { + source: 0, + sx: 0, + sy: 0, + sw, + sh, + dx: 0, + dy: 0, + dw, + dh, + cols: 0..dw, + rows: 0..dh, + x: if sw == dw { Axis::default() } else { Axis::new(sw, dw) }, + y: if sh == dh { Axis::default() } else { Axis::new(sh, dh) }, + direct: sw == dw && sh == dh, + bgra, + alpha: false, + opacity: 255, + mirror: false, + }); + } + let blit = self.blit.as_ref().expect("just set"); + let out = self.pool.get_format(width, height, PixelFormat::Rgba); + let target = Arc::get_mut(out).expect("the pool hands out unshared frames"); + target.timestamp = frame.timestamp; + let FrameData::Rgba(dst) = &mut target.data else { + return Err(Error::InvalidFrame("the preview canvas is not RGBA".into())); + }; + let stride = dst.stride; + let tasks = self.workers.tasks(dh); + let band = dh.div_ceil(tasks); + let tasks = dh.div_ceil(band); + while self.scratch.len() < tasks { + self.scratch.push(Mutex::new(Vec::new())); + } + for slot in &mut self.scratch { + let tmp = slot.get_mut().unwrap_or_else(PoisonError::into_inner); + if tmp.len() < sw * 4 { + tmp.resize(sw * 4, 0); + } + } + let slots = Slots::new(dst.data.chunks_mut(band * stride).take(tasks).enumerate()); + let scratch = &self.scratch; + self.workers.run(slots.len(), &|task| { + let Some((i, out)) = slots.take(task) else { return }; + let rows = i * band..((i + 1) * band).min(dh); + blit.draw(plane, out, stride, &rows, &mut lock(&scratch[i])); + }); + Ok(out.clone()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::frame::Plane; + use crate::studio::scene::{Colour, Crop, Source, Transform}; + + /// A flat `width` x `height` RGBA frame. + fn flat(width: u32, height: u32, rgba: [u8; 4]) -> Arc { + let mut data = Vec::with_capacity(width as usize * height as usize * 4); + for _ in 0..width * height { + data.extend_from_slice(&rgba); + } + Arc::new(VideoFrame { + width, + height, + timestamp: Duration::ZERO, + data: FrameData::Rgba(Plane::new(data, width as usize * 4)), + }) + } + + /// A BGRA frame whose pixel `(x, y)` is `f(x, y)` as RGBA. + fn painted( + width: u32, + height: u32, + bgra: bool, + f: impl Fn(u32, u32) -> [u8; 4], + ) -> Arc { + let mut data = Vec::with_capacity(width as usize * height as usize * 4); + for y in 0..height { + for x in 0..width { + let p = f(x, y); + data.extend_from_slice(&if bgra { [p[2], p[1], p[0], p[3]] } else { p }); + } + } + let plane = Plane::new(data, width as usize * 4); + Arc::new(VideoFrame { + width, + height, + timestamp: Duration::ZERO, + data: if bgra { FrameData::Bgra(plane) } else { FrameData::Rgba(plane) }, + }) + } + + fn pixel(frame: &VideoFrame, x: u32, y: u32) -> [u8; 4] { + let FrameData::Rgba(p) = &frame.data else { panic!("not RGBA") }; + let row = p.row(y as usize, frame.width as usize * 4); + row[x as usize * 4..][..4].try_into().unwrap() + } + + /// Composite a scene whose sources are fed `frames` (one per source). + fn compose( + compositor: &mut Compositor, + scene: &Scene, + revision: u64, + frames: &[Option>], + ) -> Arc { + let feeds: Vec>> = frames + .iter() + .map(|frame| { + let feed = Arc::new(Feed::new()); + if let Some(frame) = frame { + feed.put(frame.clone()); + } + Some(feed) + }) + .collect(); + compositor.compose(scene, revision, &feeds, Duration::from_millis(1)).unwrap() + } + + #[test] + fn draws_sources_where_they_belong() { + let mut compositor = Compositor::new(4); + compositor.set_size(64, 32); + let mut scene = Scene::new(1, "t"); + scene.background = Colour::rgb(10, 20, 30); + // Full-size red background source and a green box at (16, 8) 8x4. + scene.sources.push(Source { + transform: Transform::full(64, 32), + ..Source::new(1, SourceKind::Colour { colour: Colour::WHITE, size: (64, 32) }) + }); + scene.sources.push(Source { + transform: Transform::box_at(16.0, 8.0, 8.0, 4.0), + ..Source::new(2, SourceKind::Colour { colour: Colour::WHITE, size: (8, 4) }) + }); + let red = flat(64, 32, [200, 0, 0, 255]); + let green = flat(8, 4, [0, 200, 0, 255]); + let out = compose(&mut compositor, &scene, 1, &[Some(red), Some(green)]); + assert_eq!((out.width, out.height), (64, 32)); + assert_eq!(out.timestamp, Duration::from_millis(1)); + assert_eq!(pixel(&out, 0, 0), [200, 0, 0, 255], "the full-size source"); + assert_eq!(pixel(&out, 16, 8), [0, 200, 0, 255], "the box's first pixel"); + assert_eq!(pixel(&out, 23, 11), [0, 200, 0, 255], "the box's last pixel"); + assert_eq!(pixel(&out, 24, 8), [200, 0, 0, 255], "one past the box"); + assert_eq!(pixel(&out, 16, 12), [200, 0, 0, 255], "one below the box"); + + // Without the background source the scene's background shows. + scene.sources.remove(0); + let green = flat(8, 4, [0, 200, 0, 255]); + let out = compose(&mut compositor, &scene, 2, &[Some(green)]); + assert_eq!(pixel(&out, 0, 0), [10, 20, 30, 255]); + assert_eq!(pixel(&out, 16, 8), [0, 200, 0, 255]); + } + + #[test] + fn blends_alpha_and_opacity() { + let mut compositor = Compositor::new(2); + compositor.set_size(8, 4); + let mut scene = Scene::new(1, "t"); + scene.background = Colour::rgb(0, 0, 0); + // A half-transparent white image over black, and a fully opaque one + // at half opacity. + scene.sources.push(Source { + transform: Transform::box_at(0.0, 0.0, 4.0, 4.0), + ..Source::new(1, SourceKind::Image { path: "x.png".into() }) + }); + scene.sources.push(Source { + transform: Transform::box_at(4.0, 0.0, 4.0, 4.0), + opacity: 0.5, + ..Source::new(2, SourceKind::Colour { colour: Colour::WHITE, size: (4, 4) }) + }); + let half = flat(4, 4, [255, 255, 255, 128]); + let solid = flat(4, 4, [255, 255, 255, 255]); + let out = compose(&mut compositor, &scene, 1, &[Some(half), Some(solid)]); + // 255 * 128/255 = 128 over black. + assert_eq!(pixel(&out, 0, 0), [128, 128, 128, 255], "image alpha"); + // Opacity 0.5 -> 128/255 coverage. + assert_eq!(pixel(&out, 4, 0), [128, 128, 128, 255], "source opacity"); + + // An invisible source and a zero-opacity one draw nothing. + scene.sources[0].visible = false; + scene.sources[1].opacity = 0.0; + let out = compose( + &mut compositor, + &scene, + 2, + &[Some(flat(4, 4, [255, 255, 255, 128])), Some(flat(4, 4, [255, 255, 255, 255]))], + ); + assert_eq!(pixel(&out, 0, 0), [0, 0, 0, 255]); + assert_eq!(pixel(&out, 4, 0), [0, 0, 0, 255]); + } + + #[test] + fn scales_crops_clips_and_swaps_channels() { + let mut compositor = Compositor::new(4); + compositor.set_size(16, 16); + let mut scene = Scene::new(1, "t"); + // A 4x4 source of four quadrant colours, drawn at 16x16: each + // quadrant is 8x8 in the output. + scene.sources.push(Source { + transform: Transform { fit: Fit::Stretch, ..Transform::full(16, 16) }, + ..Source::new(1, SourceKind::Colour { colour: Colour::WHITE, size: (4, 4) }) + }); + let quad = painted(4, 4, true, |x, y| match (x < 2, y < 2) { + (true, true) => [255, 0, 0, 255], + (false, true) => [0, 255, 0, 255], + (true, false) => [0, 0, 255, 255], + (false, false) => [255, 255, 0, 255], + }); + let out = compose(&mut compositor, &scene, 1, &[Some(quad.clone())]); + // BGRA input came out as RGBA, upscaled. + assert_eq!(pixel(&out, 1, 1), [255, 0, 0, 255]); + assert_eq!(pixel(&out, 14, 1), [0, 255, 0, 255]); + assert_eq!(pixel(&out, 1, 14), [0, 0, 255, 255]); + assert_eq!(pixel(&out, 14, 14), [255, 255, 0, 255]); + + // Crop to the bottom-right quadrant: it fills the output. + scene.sources[0].crop = Crop { left: 2, top: 2, right: 0, bottom: 0 }; + let out = compose(&mut compositor, &scene, 2, &[Some(quad.clone())]); + assert_eq!(pixel(&out, 0, 0), [255, 255, 0, 255]); + assert_eq!(pixel(&out, 15, 15), [255, 255, 0, 255]); + + // Half off the left edge: only the right half of the source shows. + scene.sources[0].crop = Crop::default(); + scene.sources[0].transform = + Transform { fit: Fit::Stretch, ..Transform::box_at(-8.0, 0.0, 16.0, 16.0) }; + let out = compose(&mut compositor, &scene, 3, &[Some(quad)]); + // Output column 4 is the source's column 2.6: the right half. + assert_eq!(pixel(&out, 4, 1), [0, 255, 0, 255], "the source's right half"); + assert_eq!(pixel(&out, 7, 14), [255, 255, 0, 255]); + assert_eq!(pixel(&out, 8, 1), [0, 0, 0, 255], "past the source"); + + // Entirely outside: nothing is drawn. + scene.sources[0].transform = Transform::box_at(100.0, 100.0, 4.0, 4.0); + let out = compose(&mut compositor, &scene, 4, &[Some(flat(4, 4, [9, 9, 9, 255]))]); + assert_eq!(pixel(&out, 0, 0), [0, 0, 0, 255]); + } + + #[test] + fn fit_letterboxes_and_covers() { + let mut compositor = Compositor::new(2); + compositor.set_size(16, 16); + let mut scene = Scene::new(1, "t"); + // A 16x8 source in a 16x16 box. + scene.sources.push(Source { + transform: Transform { fit: Fit::Contain, ..Transform::full(16, 16) }, + ..Source::new(1, SourceKind::Colour { colour: Colour::WHITE, size: (16, 8) }) + }); + let wide = + painted(16, 8, false, |_, y| if y < 4 { [255, 0, 0, 255] } else { [0, 0, 255, 255] }); + let out = compose(&mut compositor, &scene, 1, &[Some(wide.clone())]); + // Contain: 16x8 centred, rows 4..12 drawn, the rest background. + assert_eq!(pixel(&out, 0, 0), [0, 0, 0, 255], "letterbox above"); + assert_eq!(pixel(&out, 0, 4), [255, 0, 0, 255]); + assert_eq!(pixel(&out, 0, 11), [0, 0, 255, 255]); + assert_eq!(pixel(&out, 0, 12), [0, 0, 0, 255], "letterbox below"); + + // Cover: the box is filled, the source's sides are cropped. + scene.sources[0].transform = Transform { fit: Fit::Cover, ..Transform::full(16, 16) }; + let out = compose(&mut compositor, &scene, 2, &[Some(wide)]); + assert_eq!(pixel(&out, 0, 0), [255, 0, 0, 255], "no letterbox"); + assert_eq!(pixel(&out, 15, 15), [0, 0, 255, 255]); + } + + #[test] + fn a_source_keeps_its_last_picture() { + let mut compositor = Compositor::new(2); + compositor.set_size(4, 4); + let mut scene = Scene::new(1, "t"); + scene.sources.push(Source { + transform: Transform::full(4, 4), + ..Source::new(1, SourceKind::Colour { colour: Colour::WHITE, size: (4, 4) }) + }); + let feeds = vec![Some(Arc::new(Feed::new()))]; + let feed = feeds[0].clone().unwrap(); + feed.put(flat(4, 4, [7, 8, 9, 255])); + let out = compositor.compose(&scene, 1, &feeds, Duration::ZERO).unwrap(); + assert_eq!(pixel(&out, 0, 0), [7, 8, 9, 255]); + drop(out); + // Nothing new: the same picture again. + let out = compositor.compose(&scene, 1, &feeds, Duration::from_millis(33)).unwrap(); + assert_eq!(pixel(&out, 0, 0), [7, 8, 9, 255]); + assert_eq!(out.timestamp, Duration::from_millis(33)); + assert_eq!(feed.delivered(), 1); + drop(out); + // A feed that never delivered draws nothing but does not fail. + let out = compositor.compose(&scene, 2, &[None], Duration::ZERO).unwrap(); + assert_eq!(pixel(&out, 0, 0), [0, 0, 0, 255]); + } + + #[test] + fn plans_only_when_something_changed() { + let mut compositor = Compositor::new(2); + compositor.set_size(8, 8); + let mut scene = Scene::new(1, "t"); + scene.sources.push(Source { + transform: Transform::full(8, 8), + ..Source::new(1, SourceKind::Colour { colour: Colour::WHITE, size: (8, 8) }) + }); + let feeds = vec![Some(Arc::new(Feed::new()))]; + let feed = feeds[0].clone().unwrap(); + for _ in 0..5 { + feed.put(flat(8, 8, [1, 2, 3, 255])); + drop(compositor.compose(&scene, 1, &feeds, Duration::ZERO).unwrap()); + } + assert_eq!(compositor.stats().replans, 1, "the plan was kept"); + assert_eq!(compositor.stats().frames, 5); + // A source that resizes replans. + feed.put(flat(4, 4, [1, 2, 3, 255])); + drop(compositor.compose(&scene, 1, &feeds, Duration::ZERO).unwrap()); + assert_eq!(compositor.stats().replans, 2); + // So does a scene change. + feed.put(flat(4, 4, [1, 2, 3, 255])); + drop(compositor.compose(&scene, 2, &feeds, Duration::ZERO).unwrap()); + assert_eq!(compositor.stats().replans, 3); + assert!(compositor.stats().compose_time > Duration::ZERO); + assert_eq!(compositor.stats().threads, 2); + } + + #[test] + fn mirrors_a_camera() { + let mut compositor = Compositor::new(1); + compositor.set_size(4, 2); + let mut scene = Scene::new(1, "t"); + scene.sources.push(Source { + transform: Transform { fit: Fit::Stretch, ..Transform::full(4, 2) }, + ..Source::new( + 1, + SourceKind::Camera { device: String::new(), size: None, fps: None, mirror: true }, + ) + }); + // A column ramp, so mirroring is visible at 1:1 and when scaled. + let ramp = painted(4, 2, false, |x, _| [(x as u8 + 1) * 10, 0, 0, 255]); + let out = compose(&mut compositor, &scene, 1, &[Some(ramp.clone())]); + assert_eq!(pixel(&out, 0, 0)[0], 40); + assert_eq!(pixel(&out, 3, 0)[0], 10); + // Scaled to 8 wide: still mirrored. + compositor.set_size(8, 2); + let out = compose(&mut compositor, &scene, 2, &[Some(ramp)]); + assert!(pixel(&out, 0, 0)[0] > pixel(&out, 7, 0)[0], "mirrored while scaling"); + } + + #[test] + fn preview_scaler_downscales() { + let mut scaler = RgbaScaler::new(2); + // Left half red, right half blue; halved, the halves stay. + let frame = + painted(16, 8, true, |x, _| if x < 8 { [255, 0, 0, 255] } else { [0, 0, 255, 255] }); + let out = scaler.scale(&frame, 8, 4).unwrap(); + assert_eq!((out.width, out.height), (8, 4)); + assert_eq!(out.format(), PixelFormat::Rgba); + assert_eq!(pixel(&out, 0, 0), [255, 0, 0, 255]); + assert_eq!(pixel(&out, 7, 3), [0, 0, 255, 255]); + drop(out); + // 1:1 copies and keeps the timestamp. + let frame = VideoFrame { timestamp: Duration::from_millis(7), ..(*frame).clone() }; + let out = scaler.scale(&frame, 16, 8).unwrap(); + assert_eq!(out.timestamp, Duration::from_millis(7)); + assert_eq!(pixel(&out, 0, 0), [255, 0, 0, 255]); + assert_eq!(pixel(&out, 15, 7), [0, 0, 255, 255]); + // I420 is not a preview input. + assert!(scaler.scale(&VideoFrame::black_i420(4, 4), 2, 2).is_err()); + } + + #[test] + fn an_unset_output_size_fails() { + let mut compositor = Compositor::new(1); + let scene = Scene::new(1, "t"); + assert!(compositor.compose(&scene, 1, &[], Duration::ZERO).is_err()); + compositor.set_size(1, 1); + // Rounded up to the smallest even size. + assert_eq!(compositor.size(), (2, 2)); + compositor.reset(); + assert!(compositor.compose(&scene, 1, &[], Duration::ZERO).is_ok()); + } +} diff --git a/crates/voelin-media/src/studio/mod.rs b/crates/voelin-media/src/studio/mod.rs index cdca2f4..8b1b0bb 100644 --- a/crates/voelin-media/src/studio/mod.rs +++ b/crates/voelin-media/src/studio/mod.rs @@ -3,5 +3,8 @@ //! //! - [`scene`]: the scene graph (scenes, sources, transforms, crops), the //! persisted shape of a studio +//! - [`compose`]: the compositor, which draws the live scene into pooled +//! frames at the output size +pub mod compose; pub mod scene; diff --git a/crates/voelin-media/tests/studio_alloc.rs b/crates/voelin-media/tests/studio_alloc.rs new file mode 100644 index 0000000..d4b6251 --- /dev/null +++ b/crates/voelin-media/tests/studio_alloc.rs @@ -0,0 +1,195 @@ +//! The studio compositor allocates nothing per composed frame: several +//! sources of different sizes, scaled, cropped, blended and mirrored into a +//! 1080p canvas, counted with a global allocator that counts only on the +//! thread that asks. +//! +//! A scene change, a source that resizes and a new output size each replan +//! (and allocate); a steady composite must not. +#![allow(unsafe_code)] + +use std::alloc::{GlobalAlloc, Layout, System}; +use std::cell::Cell; +use std::sync::Arc; +use std::time::Duration; + +use voelin_media::frame::{FrameData, Plane, VideoFrame}; +use voelin_media::studio::compose::{Compositor, Feed, RgbaScaler}; +use voelin_media::studio::scene::{Colour, Crop, Fit, Scene, Source, SourceKind, Transform}; + +thread_local! { + static COUNTING: Cell = const { Cell::new(false) }; + static COUNT: Cell = const { Cell::new(0) }; +} + +fn count() { + // `try_with`: never panics, also while the thread is torn down; const + // thread locals without destructors never allocate. + let _ = COUNTING.try_with(|on| { + if on.get() { + let _ = COUNT.try_with(|n| n.set(n.get() + 1)); + } + }); +} + +struct Counting; + +// SAFETY: every method forwards to `System` with the caller's arguments +// unchanged, so `System`'s guarantees hold; counting touches only +// thread-local cells and never allocates. +unsafe impl GlobalAlloc for Counting { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + count(); + // SAFETY: forwarded unchanged (see above). + unsafe { System.alloc(layout) } + } + + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { + count(); + // SAFETY: forwarded unchanged. + unsafe { System.alloc_zeroed(layout) } + } + + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { + count(); + // SAFETY: forwarded unchanged. + unsafe { System.realloc(ptr, layout, new_size) } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + // SAFETY: forwarded unchanged. + unsafe { System.dealloc(ptr, layout) } + } +} + +#[global_allocator] +static ALLOCATOR: Counting = Counting; + +/// Allocations `f` makes on this thread. +fn allocations(f: impl FnOnce()) -> u64 { + COUNT.with(|n| n.set(0)); + COUNTING.with(|on| on.set(true)); + f(); + COUNTING.with(|on| on.set(false)); + COUNT.with(Cell::get) +} + +/// A `width` x `height` frame whose pixel `(x, y)` is a cheap pattern, RGBA or +/// BGRA. +fn frame(width: u32, height: u32, bgra: bool, seed: u8) -> Arc { + let mut data = Vec::with_capacity(width as usize * height as usize * 4); + for y in 0..height { + for x in 0..width { + let v = (x ^ y) as u8 ^ seed; + data.extend_from_slice(&[v, v.wrapping_mul(3), seed, 128 | seed]); + } + } + let plane = Plane::new(data, width as usize * 4); + Arc::new(VideoFrame { + width, + height, + timestamp: Duration::ZERO, + data: if bgra { FrameData::Bgra(plane) } else { FrameData::Rgba(plane) }, + }) +} + +#[test] +fn composing_allocates_nothing_per_frame() { + // The counter works. + assert_eq!(allocations(|| drop(std::hint::black_box(vec![0u8; 16]))), 1); + + let mut scene = Scene::new(1, "alloc"); + scene.background = Colour::rgb(20, 20, 24); + // A 4K screen scaled down to the canvas. + scene.sources.push(Source { + transform: Transform { fit: Fit::Cover, ..Transform::full(1920, 1080) }, + ..Source::new(1, SourceKind::Screen { monitor: 0, backend: None, cursor: true }) + }); + // A camera, cropped, mirrored, in the corner. + scene.sources.push(Source { + transform: Transform::box_at(1520.0, 800.0, 384.0, 216.0), + crop: Crop { left: 40, top: 0, right: 40, bottom: 0 }, + ..Source::new( + 2, + SourceKind::Camera { device: String::new(), size: None, fps: None, mirror: true }, + ) + }); + // A logo with alpha, at 1:1. + scene.sources.push(Source { + transform: Transform::box_at(32.0, 32.0, 128.0, 128.0), + opacity: 0.75, + ..Source::new(3, SourceKind::Image { path: "logo.png".into() }) + }); + // Text, scaled up. + scene.sources.push(Source { + transform: Transform::box_at(32.0, 960.0, 600.0, 80.0), + ..Source::new( + 4, + SourceKind::Text { + text: "LIVE".into(), + font: None, + size_px: 48.0, + colour: Colour::WHITE, + backdrop: Colour::CLEAR, + align: Default::default(), + padding: 0, + }, + ) + }); + let sizes = [(3840u32, 2160u32), (1280, 720), (128, 128), (600, 80)]; + let bgra = [true, true, false, false]; + let feeds: Vec>> = + (0..scene.sources.len()).map(|_| Some(Arc::new(Feed::new()))).collect(); + let frames: Vec> = sizes + .iter() + .zip(bgra) + .enumerate() + .map(|(i, (&(w, h), bgra))| frame(w, h, bgra, i as u8 * 40)) + .collect(); + + let mut compositor = Compositor::new(4); + compositor.set_size(1920, 1080); + let mut preview = RgbaScaler::new(2); + // Every source delivers every frame except the camera (every third). + let step = |compositor: &mut Compositor, preview: &mut RgbaScaler, n: u64| { + for (i, feed) in feeds.iter().enumerate() { + if i != 1 || n.is_multiple_of(3) { + feed.as_ref().unwrap().put(frames[i].clone()); + } + } + let out = + compositor.compose(&scene, 1, &feeds, Duration::from_millis(n * 16)).expect("composed"); + // The preview tap runs on the same thread in this test. + drop(preview.scale(&out, 480, 270).expect("previewed")); + drop(out); + }; + // Warm up: the plan, the filter weights, the canvas pool and the scratch + // rows are all in place after the first frames. + for n in 0..12 { + step(&mut compositor, &mut preview, n); + } + let count = allocations(|| { + for n in 12..112 { + step(&mut compositor, &mut preview, n); + } + }); + assert_eq!(count, 0, "allocations in 100 composed frames"); + let stats = compositor.stats(); + assert_eq!(stats.frames, 112); + assert_eq!(stats.replans, 1, "the plan was made once"); + // One canvas: the previous one is released before the next compose. + assert_eq!(stats.allocated, 1); + assert!(stats.compose_time > Duration::ZERO); + + // A new output size replans, then allocates nothing again. + compositor.set_size(1280, 720); + for n in 112..124 { + step(&mut compositor, &mut preview, n); + } + assert_eq!(compositor.stats().replans, 2); + let count = allocations(|| { + for n in 124..164 { + step(&mut compositor, &mut preview, n); + } + }); + assert_eq!(count, 0, "allocations after a size change"); +}