const BAND_PIXELS: usize = 16_384;
const BANDS_WANTED_PER_THREAD: usize = 2;
type Progress = std::sync::Arc<dyn Fn() + Send + Sync>;
std::thread_local! {
static BAND_PROGRESS: std::cell::RefCell<Option<Progress>> =
const { std::cell::RefCell::new(None) };
}
#[must_use = "the report ends when the guard drops"]
pub fn report_band_progress(progress: Progress) -> BandProgress {
let previous = BAND_PROGRESS.with(|current| current.replace(Some(progress)));
BandProgress {
previous,
thread: std::marker::PhantomData,
}
}
pub struct BandProgress {
previous: Option<Progress>,
thread: std::marker::PhantomData<*const ()>,
}
impl Drop for BandProgress {
fn drop(&mut self) {
let previous = self.previous.take();
BAND_PROGRESS.with(|current| {
current.replace(previous);
});
}
}
fn band_progress() -> impl Fn() + Send + Sync {
let progress = BAND_PROGRESS.with(|current| current.borrow().clone());
move || {
if let Some(progress) = &progress {
progress();
}
}
}
pub(crate) struct Workers {
#[cfg(not(target_arch = "wasm32"))]
pool: Option<rayon::ThreadPool>,
threads: usize,
fixed_rows: Option<usize>,
}
impl std::fmt::Debug for Workers {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("Workers")
.field("threads", &self.threads)
.finish_non_exhaustive()
}
}
impl Workers {
pub(crate) fn new(threads: usize) -> Self {
Self::named(threads, "alpha2-cpu-render")
}
pub(crate) fn named(threads: usize, name: &'static str) -> Self {
let threads = threads.max(1);
#[cfg(not(target_arch = "wasm32"))]
{
let pool = (threads > 1)
.then(|| {
rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.thread_name(move |index| format!("{name}-{index}"))
.build()
.ok()
})
.flatten();
let threads = if pool.is_some() { threads } else { 1 };
Self {
pool,
threads,
fixed_rows: None,
}
}
#[cfg(target_arch = "wasm32")]
{
let _ = (threads, name);
Self {
threads: 1,
fixed_rows: None,
}
}
}
#[cfg(test)]
pub(crate) fn with_band_rows(threads: usize, rows: usize) -> Self {
Self {
fixed_rows: Some(rows.max(1)),
..Self::new(threads)
}
}
pub(crate) fn band_rows(&self, width: usize) -> usize {
self.fixed_rows.unwrap_or_else(|| band_rows(width))
}
pub(crate) const fn threads(&self) -> usize {
self.threads
}
#[cfg_attr(target_arch = "wasm32", allow(clippy::unused_self))]
pub(crate) fn bands<T: Send>(
&self,
pixels: &mut [T],
width: usize,
draw: impl Fn(usize, &mut [T]) + Sync + Send,
) {
if width == 0 || pixels.is_empty() {
return;
}
let rows = self.band_rows(width);
let progress = band_progress();
let draw = |row, chunk: &mut [T]| {
progress();
draw(row, chunk);
};
#[cfg(not(target_arch = "wasm32"))]
if let Some(pool) = &self.pool {
use rayon::prelude::*;
pool.install(|| {
pixels
.par_chunks_mut(rows.saturating_mul(width))
.enumerate()
.for_each(|(band, chunk)| draw(band * rows, chunk));
});
return;
}
for (band, chunk) in pixels.chunks_mut(rows.saturating_mul(width)).enumerate() {
draw(band * rows, chunk);
}
}
pub(crate) fn bands_with<T: Send, S: Send>(
&self,
pixels: &mut [T],
width: usize,
init: impl Fn() -> S + Sync + Send,
draw: impl Fn(&mut S, usize, &mut [T]) + Sync + Send,
) {
if width == 0 || pixels.is_empty() {
return;
}
let rows = self.band_rows(width);
self.chunks_with(pixels, rows.saturating_mul(width), rows, init, draw);
}
pub(crate) fn weighted_bands_with<T: Send, S: Send>(
&self,
pixels: &mut [T],
width: usize,
row_work_pixels: usize,
init: impl Fn() -> S + Sync + Send,
draw: impl Fn(&mut S, usize, &mut [T]) + Sync + Send,
) {
if width == 0 || pixels.is_empty() {
return;
}
let rows = self.fixed_rows.unwrap_or_else(|| {
weighted_band_rows(self.threads, width, pixels.len() / width, row_work_pixels)
});
self.chunks_with(pixels, rows.saturating_mul(width), rows, init, draw);
}
pub(crate) fn items_with<T: Send, S: Send>(
&self,
items: &mut [T],
item_pixels: usize,
init: impl Fn() -> S + Sync + Send,
draw: impl Fn(&mut S, usize, &mut [T]) + Sync + Send,
) {
if item_pixels == 0 || items.is_empty() {
return;
}
let rows = self.band_rows(item_pixels);
self.chunks_with(items, rows, rows, init, draw);
}
#[cfg_attr(target_arch = "wasm32", allow(clippy::unused_self))]
fn chunks_with<T: Send, S: Send>(
&self,
items: &mut [T],
length: usize,
step: usize,
init: impl Fn() -> S + Sync + Send,
draw: impl Fn(&mut S, usize, &mut [T]) + Sync + Send,
) {
let progress = band_progress();
let draw = |scratch: &mut S, row, chunk: &mut [T]| {
progress();
draw(scratch, row, chunk);
};
#[cfg(not(target_arch = "wasm32"))]
if let Some(pool) = self.pool.as_ref().filter(|_| items.len() > length) {
use rayon::prelude::*;
use std::sync::{Mutex, TryLockError};
let slots = (0..=self.threads)
.map(|_| Mutex::new(None::<S>))
.collect::<Vec<_>>();
pool.install(|| {
items
.par_chunks_mut(length)
.enumerate()
.for_each(|(band, chunk)| {
let slot = rayon::current_thread_index()
.map_or(self.threads, |index| index.min(self.threads));
match slots[slot].try_lock() {
Ok(mut held) => {
let scratch = held.get_or_insert_with(&init);
draw(scratch, band * step, chunk);
}
Err(TryLockError::WouldBlock | TryLockError::Poisoned(_)) => {
draw(&mut init(), band * step, chunk);
}
}
});
});
return;
}
let mut scratch = init();
for (band, chunk) in items.chunks_mut(length).enumerate() {
draw(&mut scratch, band * step, chunk);
}
}
#[cfg_attr(target_arch = "wasm32", allow(clippy::unused_self))]
pub(crate) fn for_each(&self, count: usize, job: &(dyn Fn(usize) + Sync)) {
#[cfg(not(target_arch = "wasm32"))]
if let Some(pool) = &self.pool {
use rayon::prelude::*;
pool.install(|| (0..count).into_par_iter().for_each(job));
return;
}
(0..count).for_each(job);
}
#[cfg_attr(target_arch = "wasm32", allow(clippy::unused_self))]
pub(crate) fn map<T: Send>(
&self,
count: usize,
work: impl Fn(usize) -> T + Sync + Send,
) -> Vec<T> {
#[cfg(not(target_arch = "wasm32"))]
if let Some(pool) = &self.pool {
use rayon::prelude::*;
return pool.install(|| (0..count).into_par_iter().map(&work).collect());
}
(0..count).map(work).collect()
}
}
fn band_rows(width: usize) -> usize {
(BAND_PIXELS / width).clamp(1, 64)
}
fn weighted_band_rows(threads: usize, width: usize, rows: usize, row_work_pixels: usize) -> usize {
let unweighted = band_rows(width);
let bands_wanted = threads * BANDS_WANTED_PER_THREAD;
if threads == 1 || rows.div_ceil(unweighted) >= bands_wanted {
return unweighted;
}
let rows_of_one_band_of_work = (BAND_PIXELS / row_work_pixels.max(1)).max(1);
let rows_of_the_bands_wanted = rows.div_ceil(bands_wanted);
unweighted.min(rows_of_one_band_of_work.max(rows_of_the_bands_wanted))
}
impl crate::layout_pack::Bands for Workers {
fn rows_per_band(&self, _chroma_rows: usize, row_pixels: usize) -> usize {
self.band_rows(row_pixels)
}
fn run(&self, count: usize, job: &(dyn Fn(usize) + Sync)) {
self.for_each(count, job);
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
#[test]
fn every_band_reports_progress_to_the_calling_threads_watcher_on_any_thread() {
let width = 512;
let rows = band_rows(width);
for threads in [1, 4] {
let workers = Workers::new(threads);
let reported = Arc::new(AtomicUsize::new(0));
let counter = Arc::clone(&reported);
let report = report_band_progress(Arc::new(move || {
counter.fetch_add(1, Ordering::Relaxed);
}));
let mut pixels = vec![0_u8; width * rows * 9];
workers.bands(&mut pixels, width, |_, chunk| chunk.fill(1));
workers.bands_with(&mut pixels, width, || (), |(), _, chunk| chunk.fill(2));
assert_eq!(reported.load(Ordering::Relaxed), 18, "{threads} threads");
drop(report);
workers.bands(&mut pixels, width, |_, chunk| chunk.fill(3));
assert_eq!(reported.load(Ordering::Relaxed), 18, "the report ended");
}
}
#[test]
fn rows_that_cost_more_than_their_pixels_get_lower_bands_only_while_threads_lack_bands() {
for width in [1, 100, 204, 256, 1280, 1920, 3840, 20_000] {
let unweighted = band_rows(width);
for rows in [1, 7, 64, 113, 720, 1080, 2160] {
for threads in [1, 2, 8, 24, 64] {
assert_eq!(
weighted_band_rows(threads, width, rows, width),
unweighted,
"rows that cost their pixels: {width} x {rows} on {threads} threads"
);
for row_work in [width * 2, width * 64, width * 4096] {
let weighted = weighted_band_rows(threads, width, rows, row_work);
assert!((1..=unweighted).contains(&weighted));
let bands_wanted = threads * BANDS_WANTED_PER_THREAD;
if threads == 1 || rows.div_ceil(unweighted) >= bands_wanted {
assert_eq!(weighted, unweighted);
} else {
assert!(rows.div_ceil(weighted) <= bands_wanted);
}
}
}
}
}
assert_eq!(weighted_band_rows(24, 204, 113, 204 * 8 * 64), 3);
assert_eq!(weighted_band_rows(8, 204, 113, 204 * 8 * 64), 8);
assert_eq!(weighted_band_rows(24, 204, 113, 204 * 4), 20);
}
#[test]
fn one_band_is_drawn_on_the_calling_thread() {
let workers = Workers::new(4);
let caller = std::thread::current().id();
let mut pixels = vec![0_u8; 64];
workers.bands_with(
&mut pixels,
8,
|| (),
|(), _, chunk| {
assert_eq!(std::thread::current().id(), caller);
chunk.fill(1);
},
);
assert!(pixels.iter().all(|pixel| *pixel == 1));
}
}