const BAND_PIXELS: usize = 16_384;
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 {
let threads = threads.max(1);
#[cfg(not(target_arch = "wasm32"))]
{
let pool = (threads > 1)
.then(|| {
rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.thread_name(|index| format!("alpha2-cpu-render-{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;
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 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 {
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)
}
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");
}
}
}