//! A recycling pool of decode/encode surfaces. //! //! Surfaces are allocated once, in one `vaCreateSurfaces` call, and handed out //! as [`PooledSurface`]s that return themselves to the free list on drop. //! Two things hold a surface at the same time — the reference picture buffer //! and a frame the caller has not finished with — so ownership is an //! `Rc` and the recycle happens when the last of them goes. //! //! Reallocating per frame would be the alternative, and it is precisely what //! makes a VA-API pipeline stutter: every `vaCreateSurfaces` is a kernel //! allocation plus a driver-side tiling decision. use ec_va::{Surface, SurfaceSpec}; use std::ops::Deref; use std::rc::{Rc, Weak}; use std::cell::RefCell; struct PoolInner { /// Every surface, in allocation order. all: RefCell>>, free: RefCell>>, } /// A fixed-size pool of identically specified surfaces. #[derive(Clone)] pub struct SurfacePool { inner: Rc, spec: SurfaceSpec, } impl SurfacePool { /// Wrap surfaces that were allocated with `spec`. The caller creates them /// with [`ec_va::create_pool`] first, because a decode context needs them as /// its render targets before the pool can own them. pub fn from_surfaces(surfaces: Vec, spec: &SurfaceSpec) -> SurfacePool { let all: Vec> = surfaces.into_iter().map(Rc::new).collect(); SurfacePool { inner: Rc::new(PoolInner { free: RefCell::new(all.clone()), all: RefCell::new(all), }), spec: *spec, } } /// The spec every surface in this pool was allocated with. pub fn spec(&self) -> &SurfaceSpec { &self.spec } /// Total surfaces in the pool. pub fn len(&self) -> usize { self.inner.all.borrow().len() } /// True when the pool holds no surfaces at all. pub fn is_empty(&self) -> bool { self.len() == 0 } /// Add surfaces allocated with the pool's spec, free for [`SurfacePool::acquire`]. pub fn grow(&self, surfaces: Vec) { let surfaces: Vec> = surfaces.into_iter().map(Rc::new).collect(); self.inner.all.borrow_mut().extend(surfaces.iter().cloned()); self.inner.free.borrow_mut().extend(surfaces); } /// Surfaces currently available. pub fn available(&self) -> usize { self.inner.free.borrow().len() } /// Take a free surface, or `None` when every one is still in use. /// /// A `None` is the caller's cue to release output frames, not an error: a /// decoder that has handed out its whole pool is waiting on the consumer. pub fn acquire(&self) -> Option> { let surface = self.inner.free.borrow_mut().pop()?; Some(Rc::new(PooledSurface { surface, pool: Rc::downgrade(&self.inner), })) } } /// A surface on loan from a [`SurfacePool`]; returns to it on drop. pub struct PooledSurface { surface: Rc, pool: Weak, } impl PooledSurface { /// The surface itself, for the calls that want an owned `Rc`. pub fn surface(&self) -> &Rc { &self.surface } } impl Deref for PooledSurface { type Target = Rc; fn deref(&self) -> &Rc { &self.surface } } impl std::fmt::Debug for PooledSurface { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("PooledSurface") .field("id", &self.surface.id()) .finish() } } impl Drop for PooledSurface { fn drop(&mut self) { let Some(pool) = self.pool.upgrade() else { // The pool is gone, so the surface simply drops with this handle. return; }; pool.free.borrow_mut().push(Rc::clone(&self.surface)); } }