use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::sync::{mpsc, Arc, Mutex, OnceLock, Weak};
use std::time::{Duration, Instant};
use gpui::{DevicePixels, ImageId, RenderImage, Size};
use sk_resample::downscale;
const RESAMPLE_BELOW: f32 = 0.8;
const STEPS: f32 = 4.0;
const SYNC_PIXELS: u64 = 1_300_000;
const SYNC_BUDGET: Duration = Duration::from_millis(4);
const SETTLE: Duration = Duration::from_millis(100);
const BUDGET_BYTES: usize = 64 << 20;
const REDUCE_AFTER: Duration = Duration::from_secs(2);
const USE_WINDOW: Duration = Duration::from_secs(15);
const MIN_SAVING: usize = 256 << 10;
const UPSCALE_TOLERANCE: f32 = 1.2;
type Key = (ImageId, usize, i32, i32);
struct Copy {
image: Arc<RenderImage>,
source: Weak<RenderImage>,
bytes: usize,
used: u64,
}
#[derive(Default)]
struct Cache {
copies: HashMap<Key, Copy>,
in_flight: HashSet<Key>,
waiting: HashMap<Key, (Weak<RenderImage>, Instant)>,
clock: u64,
bytes: usize,
sync_spent: Duration,
retired: Vec<Arc<RenderImage>>,
}
thread_local! {
static CACHE: RefCell<Cache> = RefCell::new(Cache::default());
static HOLD_REPLAYS: Cell<bool> = const { Cell::new(false) };
static USES: RefCell<HashMap<ImageId, Use>> = RefCell::new(HashMap::new());
static WANT_FULL: RefCell<HashSet<ImageId>> = RefCell::new(HashSet::new());
}
struct Use {
image: Weak<RenderImage>,
first: Instant,
cur: (i32, i32),
prev: (i32, i32),
rotated: Instant,
last: (i32, i32),
}
fn note_use(image: &Arc<RenderImage>, device: Size<DevicePixels>, now: Instant) {
let d = (device.width.0, device.height.0);
if image.is_reduced() {
let px = image.size(0);
let over = |d: i32, p: i32| d as f32 > p as f32 * UPSCALE_TOLERANCE;
if over(d.0, px.width.0) || over(d.1, px.height.0) {
WANT_FULL.with(|w| w.borrow_mut().insert(image.id));
}
}
USES.with(|u| {
let mut u = u.borrow_mut();
let e = u.entry(image.id).or_insert_with(|| Use {
image: Arc::downgrade(image),
first: now,
cur: (0, 0),
prev: (0, 0),
rotated: now,
last: d,
});
let since = now.duration_since(e.rotated);
if since >= USE_WINDOW {
e.prev = if since >= USE_WINDOW * 2 {
(0, 0)
} else {
e.cur
};
e.cur = (0, 0);
e.rotated = now;
}
e.cur = (e.cur.0.max(d.0), e.cur.1.max(d.1));
e.last = d;
});
}
fn recent_use(id: ImageId, now: Instant) -> Option<((i32, i32), Duration)> {
USES.with(|u| {
let u = u.borrow();
let e = u.get(&id)?;
let since = now.duration_since(e.rotated);
let (cur, prev) = if since >= USE_WINDOW * 2 {
((0, 0), (0, 0))
} else if since >= USE_WINDOW {
((0, 0), e.cur)
} else {
(e.cur, e.prev)
};
let w = cur.0.max(prev.0).max(e.last.0);
let h = cur.1.max(prev.1).max(e.last.1);
Some(((w, h), now.duration_since(e.first)))
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum KeepFull {
NotStill,
Undrawn,
Young,
DrawnLarge,
NoCopy,
SmallSaving,
}
pub(crate) fn reduced_copy(
full: &Arc<RenderImage>,
now: Instant,
) -> Result<Arc<RenderImage>, KeepFull> {
if full.is_reduced() || full.is_dynamic() || full.frame_count() != 1 {
return Err(KeepFull::NotStill);
}
let (drawn, age) = recent_use(full.id, now).ok_or(KeepFull::Undrawn)?;
if drawn.0 <= 0 || drawn.1 <= 0 {
return Err(KeepFull::Undrawn);
}
if age < REDUCE_AFTER {
return Err(KeepFull::Young);
}
let natural = full.size(0);
let (nw, nh) = (natural.width.0, natural.height.0);
if nw <= 0 || nh <= 0 {
return Err(KeepFull::NotStill);
}
let need = (
step(drawn.0 as f32 / nw as f32),
step(drawn.1 as f32 / nh as f32),
);
if need.0 == 0 || need.1 == 0 {
return Err(KeepFull::DrawnLarge);
}
let full_bytes = nw as usize * nh as usize * 4;
CACHE.with(|c| {
let mut c = c.borrow_mut();
let key = c
.copies
.keys()
.filter(|k| k.0 == full.id && k.1 == 0 && k.2 >= need.0 && k.3 >= need.1)
.min_by_key(|k| (k.2 + k.3, k.2))
.copied()
.ok_or(KeepFull::NoCopy)?;
let bytes = c.copies.get(&key).ok_or(KeepFull::NoCopy)?.bytes;
if bytes.saturating_mul(4) > full_bytes || full_bytes - bytes < MIN_SAVING {
return Err(KeepFull::SmallSaving);
}
let copy = c.copies.remove(&key).ok_or(KeepFull::NoCopy)?;
c.bytes -= copy.bytes;
Ok(copy.image)
})
}
pub(crate) fn take_wanted_full() -> HashSet<ImageId> {
WANT_FULL.with(|w| std::mem::take(&mut *w.borrow_mut()))
}
pub(crate) fn want_full(id: ImageId) {
WANT_FULL.with(|w| w.borrow_mut().insert(id));
}
pub(crate) fn prune_uses() {
USES.with(|u| u.borrow_mut().retain(|_, e| e.image.strong_count() > 0));
}
pub(crate) fn hold_replays_too() {
HOLD_REPLAYS.with(|h| h.set(true));
}
struct Job {
key: Key,
source: Arc<RenderImage>,
target: (u32, u32),
}
type Landed = (Key, Weak<RenderImage>, Option<RenderImage>);
static DONE: Mutex<Vec<Landed>> = Mutex::new(Vec::new());
static JOBS: OnceLock<Mutex<mpsc::Sender<Job>>> = OnceLock::new();
fn enabled() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| {
let off = std::env::var("SK_NO_RESAMPLE").map(|v| {
let v = v.trim().to_ascii_lowercase();
!(v.is_empty() || v == "0" || v == "false" || v == "off")
});
!off.unwrap_or(false)
})
}
pub(crate) fn forget(image: &RenderImage) {
CACHE.with(|c| {
let mut c = c.borrow_mut();
let keys: Vec<Key> = c
.copies
.keys()
.filter(|k| k.0 == image.id)
.copied()
.collect();
for k in keys {
evict(&mut c, k);
}
c.waiting.retain(|k, _| k.0 != image.id);
});
}
pub(crate) fn holds_replays() -> bool {
HOLD_REPLAYS.with(Cell::get)
}
pub(crate) fn step(scale: f32) -> i32 {
if !(scale > 0.0) || scale >= RESAMPLE_BELOW {
return 0;
}
((scale.log2() * STEPS).ceil() as i32).min(-1)
}
pub(crate) fn stepped(natural: u32, step: i32) -> u32 {
if step == 0 {
return natural;
}
((natural as f32 * (step as f32 / STEPS).exp2()).ceil() as u32).clamp(1, natural)
}
fn resampler(
image: &Arc<RenderImage>,
frame: usize,
device: Size<DevicePixels>,
) -> Option<Arc<RenderImage>> {
note_use(image, device, Instant::now());
let out = choose(image, frame, device);
if TRACK.load(std::sync::atomic::Ordering::Relaxed) {
note_seen(image, device, out.is_none());
}
out
}
static TRACK: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
struct Seen {
image: Weak<RenderImage>,
bytes: usize,
natural: (i32, i32),
drawn: (i32, i32),
full_drawn: bool,
}
thread_local! {
static SEEN: RefCell<HashMap<ImageId, Seen>> = RefCell::new(HashMap::new());
}
pub fn set_tracking(on: bool) {
TRACK.store(on, std::sync::atomic::Ordering::Relaxed);
}
fn note_seen(image: &Arc<RenderImage>, device: Size<DevicePixels>, full: bool) {
SEEN.with(|s| {
let mut s = s.borrow_mut();
let e = s.entry(image.id).or_insert_with(|| {
let natural = image.size(0);
let bytes = (0..image.frame_count())
.map(|i| {
let z = image.size(i);
(z.width.0.max(0) as usize) * (z.height.0.max(0) as usize) * 4
})
.sum();
Seen {
image: Arc::downgrade(image),
bytes,
natural: (natural.width.0, natural.height.0),
drawn: (0, 0),
full_drawn: false,
}
});
e.drawn = (
e.drawn.0.max(device.width.0),
e.drawn.1.max(device.height.0),
);
e.full_drawn |= full;
});
}
#[derive(Default, Debug, Clone)]
pub struct ImageStats {
pub tracking: bool,
pub live: usize,
pub live_bytes: usize,
pub live_bytes_at_drawn: usize,
pub oversized: usize,
pub oversized_bytes: usize,
pub full_in_atlas: usize,
pub full_in_atlas_bytes: usize,
pub gone: usize,
pub copies: usize,
pub copies_bytes: usize,
pub copies_in_flight: usize,
pub largest: Vec<(i32, i32, i32, i32, usize)>,
}
pub fn image_stats() -> ImageStats {
let mut out = ImageStats {
tracking: TRACK.load(std::sync::atomic::Ordering::Relaxed),
..Default::default()
};
CACHE.with(|c| {
let c = c.borrow();
out.copies = c.copies.len();
out.copies_bytes = c.bytes;
out.copies_in_flight = c.in_flight.len();
});
SEEN.with(|s| {
let s = s.borrow();
let mut largest: Vec<(i32, i32, i32, i32, usize)> = Vec::new();
for e in s.values() {
if e.full_drawn {
out.full_in_atlas += 1;
out.full_in_atlas_bytes += e.bytes;
}
if e.image.strong_count() == 0 {
out.gone += 1;
continue;
}
out.live += 1;
out.live_bytes += e.bytes;
let (nw, nh) = (e.natural.0.max(1) as usize, e.natural.1.max(1) as usize);
let (dw, dh) = (
e.drawn.0.clamp(1, nw as i32) as usize,
e.drawn.1.clamp(1, nh as i32) as usize,
);
let ratio = (dw * dh) as f64 / (nw * nh) as f64;
out.live_bytes_at_drawn += (e.bytes as f64 * ratio) as usize;
if nw * nh >= 4 * dw * dh {
out.oversized += 1;
out.oversized_bytes += e.bytes;
}
largest.push((e.natural.0, e.natural.1, e.drawn.0, e.drawn.1, e.bytes));
}
largest.sort_by(|a, b| b.4.cmp(&a.4));
largest.truncate(5);
out.largest = largest;
});
out
}
fn choose(
image: &Arc<RenderImage>,
frame: usize,
device: Size<DevicePixels>,
) -> Option<Arc<RenderImage>> {
let natural = image.size(frame);
let (nw, nh) = (natural.width.0, natural.height.0);
if nw <= 0 || nh <= 0 || device.width.0 <= 0 || device.height.0 <= 0 {
return None;
}
let steps = (
step(device.width.0 as f32 / nw as f32),
step(device.height.0 as f32 / nh as f32),
);
if steps == (0, 0) {
return None;
}
let key = (image.id, frame, steps.0, steps.1);
CACHE.with(|c| {
let mut c = c.borrow_mut();
c.clock += 1;
let clock = c.clock;
if let Some(copy) = c.copies.get_mut(&key) {
copy.used = clock;
return Some(copy.image.clone());
}
match nearest(&c, image.id, frame, steps) {
None => {
if c.in_flight.iter().any(|k| k.0 == image.id && k.1 == frame) {
return None;
}
let target = (stepped(nw as u32, steps.0), stepped(nh as u32, steps.1));
if (nw as u64) * (nh as u64) <= SYNC_PIXELS && c.sync_spent < SYNC_BUDGET {
let t = Instant::now();
let made = Arc::new(make(image, frame, target)?);
c.sync_spent += t.elapsed();
crate::perf::note_resample(Some(t.elapsed().as_secs_f32() * 1000.0));
insert(&mut c, key, made.clone(), Arc::downgrade(image), clock);
Some(made)
} else {
c.in_flight.insert(key);
crate::perf::note_resample(None);
submit(Job {
key,
source: image.clone(),
target,
});
None
}
}
Some(near) => {
if !c.in_flight.contains(&key) {
c.waiting
.entry(key)
.or_insert_with(|| (Arc::downgrade(image), Instant::now()));
}
let copy = c.copies.get_mut(&near)?;
copy.used = clock;
Some(copy.image.clone())
}
}
})
}
fn nearest(c: &Cache, id: ImageId, frame: usize, steps: (i32, i32)) -> Option<Key> {
c.copies
.keys()
.filter(|k| k.0 == id && k.1 == frame)
.min_by_key(|k| {
let distance = (k.2 - steps.0).abs() + (k.3 - steps.1).abs();
let smaller = k.2 < steps.0 || k.3 < steps.1;
(distance, smaller)
})
.copied()
}
fn insert(c: &mut Cache, key: Key, image: Arc<RenderImage>, source: Weak<RenderImage>, clock: u64) {
let size = image.size(0);
let bytes = (size.width.0.max(0) as usize) * (size.height.0.max(0) as usize) * 4;
c.bytes += bytes;
if let Some(old) = c.copies.insert(
key,
Copy {
image,
source,
bytes,
used: clock,
},
) {
c.bytes -= old.bytes;
c.retired.push(old.image);
}
}
fn evict(c: &mut Cache, key: Key) {
if let Some(copy) = c.copies.remove(&key) {
c.bytes -= copy.bytes;
c.retired.push(copy.image);
}
}
fn make(image: &RenderImage, frame: usize, target: (u32, u32)) -> Option<RenderImage> {
let size = image.size(frame);
let bytes = image.as_bytes(frame)?;
let out = downscale(
bytes,
size.width.0 as u32,
size.height.0 as u32,
target.0,
target.1,
false,
);
let buffer = image::RgbaImage::from_raw(target.0, target.1, out)?;
Some(
RenderImage::new(vec![image::Frame::new(buffer)])
.with_natural_size(image.natural_size(frame)),
)
}
fn submit(job: Job) {
let jobs = JOBS.get_or_init(|| {
let (tx, rx) = mpsc::channel::<Job>();
std::thread::Builder::new()
.name("sk-resample".into())
.spawn(move || {
while let Ok(job) = rx.recv() {
let made = make(&job.source, job.key.1, job.target);
let landed = (job.key, Arc::downgrade(&job.source), made);
drop(job.source);
DONE.lock().unwrap_or_else(|e| e.into_inner()).push(landed);
}
})
.expect("spawn the resampling thread");
Mutex::new(tx)
});
let _ = jobs.lock().unwrap_or_else(|e| e.into_inner()).send(job);
}
pub(crate) fn tick(window: &mut gpui::Window, cx: &mut gpui::App) -> bool {
if enabled() {
gpui::set_image_resampler(resampler);
}
let landed: Vec<Landed> = std::mem::take(&mut *DONE.lock().unwrap_or_else(|e| e.into_inner()));
let now = Instant::now();
let (moved, retired) = CACHE.with(|c| {
let mut c = c.borrow_mut();
c.sync_spent = Duration::ZERO;
let mut moved = false;
for (key, source, made) in landed {
c.in_flight.remove(&key);
if let Some(made) = made.filter(|_| source.strong_count() > 0) {
let clock = c.clock;
insert(&mut c, key, Arc::new(made), source, clock);
moved = true;
}
}
let due: Vec<Key> = c
.waiting
.iter()
.filter(|(_, (source, since))| {
source.strong_count() == 0 || now.duration_since(*since) >= SETTLE
})
.map(|(k, _)| *k)
.collect();
for key in due {
let Some((source, _)) = c.waiting.remove(&key) else {
continue;
};
let Some(source) = source.upgrade() else {
continue;
};
if c.copies.contains_key(&key) || c.in_flight.contains(&key) {
continue;
}
let natural = source.size(key.1);
let target = (
stepped(natural.width.0 as u32, key.2),
stepped(natural.height.0 as u32, key.3),
);
c.in_flight.insert(key);
submit(Job {
key,
source,
target,
});
}
let gone: Vec<Key> = c
.copies
.iter()
.filter(|(_, v)| v.source.strong_count() == 0)
.map(|(k, _)| *k)
.collect();
for key in gone {
evict(&mut c, key);
}
while c.bytes > BUDGET_BYTES {
let Some(oldest) = c.copies.iter().min_by_key(|(_, v)| v.used).map(|(k, _)| *k) else {
break;
};
evict(&mut c, oldest);
}
(moved, std::mem::take(&mut c.retired))
});
HOLD_REPLAYS.with(|h| h.set(moved || !retired.is_empty()));
for image in retired {
cx.drop_image(image, Some(window));
}
moved
}
#[cfg(test)]
mod tests {
use super::*;
fn checker(w: u32, h: u32) -> Vec<u8> {
let mut v = Vec::with_capacity((w * h * 4) as usize);
for y in 0..h {
for x in 0..w {
let c = if (x + y) % 2 == 0 { 255 } else { 0 };
v.extend_from_slice(&[c, c, c, 255]);
}
}
v
}
fn image(bytes: Vec<u8>, w: u32, h: u32) -> Arc<RenderImage> {
Arc::new(RenderImage::new(vec![image::Frame::new(
image::RgbaImage::from_raw(w, h, bytes).unwrap(),
)]))
}
fn device(w: i32, h: i32) -> Size<DevicePixels> {
Size {
width: DevicePixels(w),
height: DevicePixels(h),
}
}
#[test]
fn steps_round_up_by_a_quarter_octave_and_leave_near_sizes_alone() {
assert_eq!(step(1.0), 0);
assert_eq!(step(2.0), 0);
assert_eq!(step(0.8), 0);
assert_eq!(step(0.79), -1);
assert_eq!(step(0.5), -4);
assert_eq!(step(40.0 / 1080.0), -19);
for drawn in [40u32, 41, 57, 80, 120, 333] {
let s = stepped(1080, step(drawn as f32 / 1080.0));
assert!(
s >= drawn && s as f32 <= drawn as f32 * 1.19 + 1.0,
"{drawn} -> {s}"
);
}
assert_eq!(stepped(1080, 0), 1080);
assert_eq!(stepped(3, -40), 1);
}
fn new_frame() {
CACHE.with(|c| c.borrow_mut().sync_spent = Duration::ZERO);
}
#[test]
fn the_first_copy_is_made_at_once_and_reused() {
new_frame();
let img = image(checker(200, 200), 200, 200);
assert!(resampler(&img, 0, device(200, 200)).is_none());
assert!(
resampler(&img, 0, device(180, 180)).is_none(),
"0.9 of its size: drawn as it is"
);
assert!(
resampler(&img, 0, device(400, 400)).is_none(),
"drawn larger: as it is"
);
let first = resampler(&img, 0, device(40, 40))
.expect("a small image's first copy is made on the spot");
let size = first.size(0);
assert!(size.width.0 >= 40 && size.width.0 <= 48, "{size:?}");
assert!(
Arc::ptr_eq(&first, &resampler(&img, 0, device(41, 41)).unwrap()),
"same step, same copy"
);
let other = resampler(&img, 0, device(100, 100)).unwrap();
assert!(Arc::ptr_eq(&first, &other));
let key = (img.id, 0, step(0.5), step(0.5));
assert!(CACHE.with(|c| c.borrow().waiting.contains_key(&key)));
let since = CACHE.with(|c| c.borrow().waiting[&key].1);
let _ = resampler(&img, 0, device(40, 40));
let _ = resampler(&img, 0, device(100, 100));
assert_eq!(CACHE.with(|c| c.borrow().waiting[&key].1), since);
new_frame();
let wide = image(checker(400, 2), 400, 2);
let copy = resampler(&wide, 0, device(40, 2)).unwrap();
assert_eq!(
(copy.size(0).height.0, copy.size(0).width.0 >= 40),
(2, true)
);
forget(&img);
assert!(CACHE.with(|c| c.borrow().copies.keys().all(|k| k.0 != img.id)));
assert!(CACHE.with(|c| c.borrow().retired.iter().any(|r| Arc::ptr_eq(r, &first))));
}
#[test]
fn past_the_frames_budget_a_first_copy_is_made_off_the_painting_thread() {
CACHE.with(|c| c.borrow_mut().sync_spent = SYNC_BUDGET);
let img = image(checker(200, 200), 200, 200);
assert!(resampler(&img, 0, device(40, 40)).is_none());
assert!(CACHE.with(|c| c.borrow().in_flight.iter().any(|k| k.0 == img.id)));
}
#[test]
fn a_large_image_is_resampled_off_the_painting_thread() {
let img = image(checker(1200, 1200), 1200, 1200);
assert!(
resampler(&img, 0, device(60, 60)).is_none(),
"drawn as it is until the copy lands"
);
assert!(
resampler(&img, 0, device(60, 60)).is_none(),
"and asked for once"
);
assert_eq!(CACHE.with(|c| c.borrow().in_flight.len()), 1);
let deadline = Instant::now() + Duration::from_secs(20);
let landed = loop {
let mut done = DONE.lock().unwrap();
if let Some(i) = done.iter().position(|l| l.0 .0 == img.id) {
break done.remove(i);
}
drop(done);
assert!(Instant::now() < deadline, "the copy never landed");
std::thread::sleep(Duration::from_millis(5));
};
let (key, source, made) = landed;
let made = made.expect("a copy");
assert_eq!(made.size(0).width.0, stepped(1200, step(0.05)) as i32);
CACHE.with(|c| {
let mut c = c.borrow_mut();
c.in_flight.remove(&key);
insert(&mut c, key, Arc::new(made), source, 0);
});
assert!(resampler(&img, 0, device(60, 60)).is_some());
}
fn settled() -> Instant {
Instant::now() + REDUCE_AFTER + Duration::from_millis(50)
}
#[test]
fn a_copy_stands_in_for_a_decode_drawn_only_small() {
new_frame();
let img = image(checker(600, 600), 600, 600);
let copy = resampler(&img, 0, device(40, 40)).expect("the first copy, on the spot");
assert_eq!(
copy.natural_size(0),
img.size(0),
"a copy carries the picture's size"
);
assert_eq!(
reduced_copy(&img, Instant::now()).err(),
Some(KeepFull::Young),
"drawn for less than REDUCE_AFTER: the decode stays"
);
let reduced =
reduced_copy(&img, settled()).expect("drawn only at 40 px: the copy stands in");
assert!(Arc::ptr_eq(&reduced, ©));
assert!(reduced.is_reduced());
CACHE.with(|c| {
let c = c.borrow();
assert!(c.copies.values().all(|v| !Arc::ptr_eq(&v.image, &reduced)));
assert!(c.retired.iter().all(|r| !Arc::ptr_eq(r, &reduced)));
});
let _ = take_wanted_full();
assert!(resampler(&reduced, 0, device(40, 40)).is_none());
assert!(take_wanted_full().is_empty());
assert_eq!(
reduced_copy(&reduced, settled()).err(),
Some(KeepFull::NotStill)
);
let _ = resampler(&reduced, 0, device(300, 300));
assert!(take_wanted_full().contains(&reduced.id));
}
#[test]
fn a_decode_stays_when_a_copy_would_save_little() {
new_frame();
let big = image(checker(600, 600), 600, 600);
let _ = resampler(&big, 0, device(40, 40));
let _ = resampler(&big, 0, device(400, 400));
assert_eq!(reduced_copy(&big, settled()).err(), Some(KeepFull::NoCopy));
new_frame();
let small = image(checker(120, 120), 120, 120);
assert!(resampler(&small, 0, device(20, 20)).is_some());
assert_eq!(
reduced_copy(&small, settled()).err(),
Some(KeepFull::SmallSaving)
);
let unseen = image(checker(600, 600), 600, 600);
assert_eq!(
reduced_copy(&unseen, settled()).err(),
Some(KeepFull::Undrawn)
);
}
}