mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-11-21 10:23:48 +01:00
perf(gui): decode timeline thumbnails off the main thread
The Photos timeline decoded up to four thumbnails per idle pass on the GTK thread, which stuttered a fast scroll. Decode each batch on gio's blocking pool instead and only paint the results on the main thread. Results from before a timeline reset are dropped, and tiles that were unbound or rebound meanwhile are no longer waiting, so they are not painted.
This commit is contained in:
1 parent
3fbe651f94
commit
b854c1e819
2 files changed
+102
-56
No files matched your search
@@ -629,6 +629,7 @@ fn build_window(app: &adw::Application) {
|
||||
thumb_generation: Cell::new(0),
|
||||
decode_queue: RefCell::new(VecDeque::new()),
|
||||
decode_idle: Cell::new(false),
|
||||
decode_inflight: RefCell::default(),
|
||||
thumb_source: RefCell::new(None),
|
||||
relayout_source: RefCell::new(None),
|
||||
bound: RefCell::new(BTreeMap::new()),
|
||||
|
||||
@@ -185,12 +185,14 @@ pub(crate) struct GalleryState {
|
||||
pub(crate) thumb_inflight: RefCell<HashSet<String>>,
|
||||
pub(crate) thumb_generation: Cell<u64>,
|
||||
/// Thumbnails on disk waiting to be turned into textures, as `(uid, path)`.
|
||||
/// Decoding happens on the GTK thread (textures are not `Send`), so it is fed
|
||||
/// a few at a time from an idle callback rather than in one blocking burst
|
||||
/// that would stutter the scroll. [`Self::decode_idle`] is the "callback
|
||||
/// already scheduled" guard.
|
||||
/// [`schedule_decode`] decodes them on worker threads a few at a time, so
|
||||
/// the scroll never waits on a JPEG decoder. [`Self::decode_idle`] is the
|
||||
/// "decoder already running" guard, and [`Self::decode_inflight`] the uids
|
||||
/// being decoded right now, so a tile rebound meanwhile doesn't queue its
|
||||
/// photo a second time.
|
||||
pub(crate) decode_queue: RefCell<VecDeque<(String, String)>>,
|
||||
pub(crate) decode_idle: Cell<bool>,
|
||||
pub(crate) decode_inflight: RefCell<HashSet<String>>,
|
||||
/// Pending debounce timers for the thumbnail queue flush and the section
|
||||
/// re-flow, replaced on each new trigger so only the last one fires.
|
||||
pub(crate) thumb_source: RefCell<Option<glib::SourceId>>,
|
||||
@@ -365,6 +367,9 @@ pub(crate) fn texture_bytes(width: i32, height: i32) -> usize {
|
||||
width.saturating_mul(height).saturating_mul(4).max(1)
|
||||
}
|
||||
|
||||
/// How many thumbnails [`schedule_decode`] decodes at once.
|
||||
const DECODE_BATCH: usize = 4;
|
||||
|
||||
/// How many tiles beyond the realised rows [`prefetch_thumbs`] warms, so a tile
|
||||
/// is decoded before it is scrolled onto rather than after.
|
||||
pub(crate) const THUMB_PREFETCH: usize = 32;
|
||||
@@ -3268,6 +3273,7 @@ pub(crate) fn want_thumb(ui: &Rc<Ui>, photo: &PhotoItem, picture: >k4::Picture
|
||||
|
||||
let mut decode = ui.gallery.decode_queue.borrow_mut();
|
||||
if decode.iter().any(|(uid, _)| uid == &photo.uid)
|
||||
|| ui.gallery.decode_inflight.borrow().contains(&photo.uid)
|
||||
|| ui.gallery.thumb_inflight.borrow().contains(&photo.uid)
|
||||
{
|
||||
return;
|
||||
@@ -3459,9 +3465,16 @@ fn prefetch_thumbs(ui: &Rc<Ui>, mut batch: Vec<String>) -> Vec<String> {
|
||||
batch
|
||||
}
|
||||
|
||||
/// Decode queued thumbnails into textures on an idle callback, a few per pass, so
|
||||
/// a big batch fills in progressively instead of freezing the scroll for the
|
||||
/// length of the whole decode.
|
||||
/// Decode queued thumbnails into textures on worker threads, a few at a time,
|
||||
/// and hand each finished texture to the tiles still waiting for it.
|
||||
///
|
||||
/// Decoding a JPEG takes milliseconds; four of them per idle pass on the GTK
|
||||
/// thread were enough to stutter a scroll. The main thread now only picks the
|
||||
/// next batch (visible tiles first) and paints what comes back. A batch that
|
||||
/// lands after the timeline was reset is dropped, and a tile that was rebound
|
||||
/// or unbound meanwhile is no longer in [`GalleryState::thumb_wanted`], so it
|
||||
/// is not painted. [`GalleryState::decode_idle`] is the "decoder running"
|
||||
/// guard.
|
||||
pub(crate) fn schedule_decode(ui: &Rc<Ui>) {
|
||||
if ui.gallery.decode_idle.get() || ui.gallery.decode_queue.borrow().is_empty() {
|
||||
return;
|
||||
@@ -3469,63 +3482,95 @@ pub(crate) fn schedule_decode(ui: &Rc<Ui>) {
|
||||
ui.gallery.decode_idle.set(true);
|
||||
|
||||
let ui = ui.clone();
|
||||
glib::idle_add_local(move || {
|
||||
let batch: Vec<(String, String)> = {
|
||||
let mut queue = ui.gallery.decode_queue.borrow_mut();
|
||||
let wanted = ui.gallery.thumb_wanted.borrow();
|
||||
let list = &ui.gallery.list;
|
||||
let visible = |uid: &str| {
|
||||
wanted.get(uid).is_some_and(|pictures| {
|
||||
pictures.iter().any(|picture| {
|
||||
if !picture.is_mapped() {
|
||||
return false;
|
||||
}
|
||||
!picture.is_ancestor(list)
|
||||
|| picture.compute_bounds(list).is_some_and(|bounds| {
|
||||
bounds.y() + bounds.height() > 0.0
|
||||
&& bounds.y() < list.height() as f32
|
||||
})
|
||||
})
|
||||
})
|
||||
};
|
||||
queue.retain(|(uid, _)| wanted.contains_key(uid));
|
||||
(0..4)
|
||||
.filter_map(|_| {
|
||||
queue
|
||||
.iter()
|
||||
.position(|(uid, _)| visible(uid))
|
||||
.and_then(|index| queue.remove(index))
|
||||
.or_else(|| queue.pop_front())
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
for (uid, path) in batch {
|
||||
if ui.gallery.photo_tex.borrow().contains_key(&uid) {
|
||||
continue;
|
||||
glib::spawn_future_local(async move {
|
||||
loop {
|
||||
let batch = next_decode_batch(&ui);
|
||||
if batch.is_empty() {
|
||||
break;
|
||||
}
|
||||
let texture = match gtk4::gdk::Texture::from_filename(&path) {
|
||||
Ok(texture) => texture,
|
||||
Err(e) => {
|
||||
tracing::debug!("cannot decode thumbnail {path}: {e}");
|
||||
ui.gallery.photo_nothumb.borrow_mut().insert(uid);
|
||||
let generation = ui.gallery.thumb_generation.get();
|
||||
ui.gallery
|
||||
.decode_inflight
|
||||
.borrow_mut()
|
||||
.extend(batch.iter().map(|(uid, _)| uid.clone()));
|
||||
// All of the batch decodes at once, on the worker pool; the results
|
||||
// are collected in the order they were picked.
|
||||
let jobs: Vec<_> = batch
|
||||
.into_iter()
|
||||
.map(|(uid, path)| {
|
||||
let job = gio::spawn_blocking({
|
||||
let path = path.clone();
|
||||
move || gtk4::gdk::Texture::from_filename(&path)
|
||||
});
|
||||
(uid, path, job)
|
||||
})
|
||||
.collect();
|
||||
for (uid, path, job) in jobs {
|
||||
let decoded = job.await;
|
||||
ui.gallery.decode_inflight.borrow_mut().remove(&uid);
|
||||
if generation != ui.gallery.thumb_generation.get() {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
ui.store_texture(&uid, texture.clone());
|
||||
let waiting = ui.gallery.thumb_wanted.borrow_mut().remove(&uid);
|
||||
for picture in waiting.into_iter().flatten() {
|
||||
picture.set_paintable(Some(&texture));
|
||||
let texture = match decoded {
|
||||
Ok(Ok(texture)) => texture,
|
||||
Ok(Err(e)) => {
|
||||
tracing::debug!("cannot decode thumbnail {path}: {e}");
|
||||
ui.gallery.photo_nothumb.borrow_mut().insert(uid);
|
||||
continue;
|
||||
}
|
||||
Err(_) => {
|
||||
tracing::debug!("thumbnail decoder for {path} panicked");
|
||||
ui.gallery.photo_nothumb.borrow_mut().insert(uid);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if ui.gallery.photo_tex.borrow().contains_key(&uid) {
|
||||
continue;
|
||||
}
|
||||
ui.store_texture(&uid, texture.clone());
|
||||
let waiting = ui.gallery.thumb_wanted.borrow_mut().remove(&uid);
|
||||
for picture in waiting.into_iter().flatten() {
|
||||
picture.set_paintable(Some(&texture));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ui.gallery.decode_queue.borrow().is_empty() {
|
||||
ui.gallery.decode_idle.set(false);
|
||||
return glib::ControlFlow::Break;
|
||||
}
|
||||
glib::ControlFlow::Continue
|
||||
ui.gallery.decode_idle.set(false);
|
||||
});
|
||||
}
|
||||
|
||||
/// Take the next few thumbnails to decode off the queue: the ones on screen
|
||||
/// first, then in queue order, skipping tiles nobody waits for any more and
|
||||
/// photos already decoded.
|
||||
fn next_decode_batch(ui: &Rc<Ui>) -> Vec<(String, String)> {
|
||||
let mut queue = ui.gallery.decode_queue.borrow_mut();
|
||||
let wanted = ui.gallery.thumb_wanted.borrow();
|
||||
let cached = ui.gallery.photo_tex.borrow();
|
||||
let list = &ui.gallery.list;
|
||||
let visible = |uid: &str| {
|
||||
wanted.get(uid).is_some_and(|pictures| {
|
||||
pictures.iter().any(|picture| {
|
||||
if !picture.is_mapped() {
|
||||
return false;
|
||||
}
|
||||
!picture.is_ancestor(list)
|
||||
|| picture.compute_bounds(list).is_some_and(|bounds| {
|
||||
bounds.y() + bounds.height() > 0.0 && bounds.y() < list.height() as f32
|
||||
})
|
||||
})
|
||||
})
|
||||
};
|
||||
queue.retain(|(uid, _)| wanted.contains_key(uid) && !cached.contains_key(uid));
|
||||
(0..DECODE_BATCH)
|
||||
.filter_map(|_| {
|
||||
queue
|
||||
.iter()
|
||||
.position(|(uid, _)| visible(uid))
|
||||
.and_then(|index| queue.remove(index))
|
||||
.or_else(|| queue.pop_front())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Re-flow the sections on screen shortly. Debounced, because the triggers (a
|
||||
/// window resize, a zoom step) arrive in floods and only the final state
|
||||
/// matters.
|
||||
|
||||
Reference in new issue
Block a user