Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

#323: windows cross the Web Worker boundary

Nathaniel Ledford4b83f8bb5e70
diff --git a/apps/lightcraft-web/src/wire.rs b/apps/lightcraft-web/src/wire.rs
index fad1a816f..81a76e6c9 100644
--- a/apps/lightcraft-web/src/wire.rs
+++ b/apps/lightcraft-web/src/wire.rs
@@ -53,6 +53,9 @@ pub struct WireJob {
/// The request's diagnostic overlay (`Overlay::to_parts`).
#[serde(default)]
pub overlay: (u8, f64),
+ /// Render only this window `[x, y, w, h]` of the output `max_w × max_h` describes (a zoomed view).
+ #[serde(default)]
+ pub window: Option<[usize; 4]>,
}
impl WireJob {
@@ -83,6 +86,7 @@ impl WireJob {
thumb_cached,
stages: job.stages.is_some().then(|| view.to_string()),
overlay: job.request.overlay.to_parts(),
+ window: job.request.window.map(|w| [w.x, w.y, w.w, w.h]),
}
}
@@ -108,7 +112,7 @@ impl WireJob {
space: lightcraft_engine::pipeline::OutputSpace::Srgb,
depth: lightcraft_engine::pipeline::OutputDepth::U8,
proof: None,
- window: None,
+ window: self.window.map(|[x, y, w, h]| lightcraft_engine::pipeline::PixelWindow { x, y, w, h }),
}
}
@@ -349,6 +353,36 @@ mod tests {
assert!(reopened.contains(&new));
}
+ // Issue #323: a worker given a zoomed view's window renders that window, not the whole frame
+ #[test]
+ fn a_window_job_crosses_the_wire_and_renders_the_same_window() {
+ use lightcraft_engine::pipeline::PixelWindow;
+ let mut s = Session::with_demo();
+ let id = s.visible_cloned()[0];
+ let win = PixelWindow { x: 300, y: 200, w: 160, h: 120 };
+ let job = s.region_job(id, 1920, 1280, win, true).unwrap();
+ let wire = WireJob::from_job(&job, false, "Region");
+ let wire: WireJob = serde_json::from_str(&serde_json::to_string(&wire).unwrap()).unwrap();
+ assert_eq!(wire.request().window, Some(win));
+ let mut core = WorkerCore::default();
+ let a = core.render(&wire, None).unwrap();
+ let b = job.run().rendered.unwrap();
+ assert_eq!((a.image.width, a.image.height), (160, 120));
+ assert_eq!(a.image.data, b.image.data);
+ }
+
+ // a job from before windows existed has none
+ #[test]
+ fn a_job_without_a_window_field_has_no_window() {
+ let mut s = Session::with_demo();
+ let id = s.visible_cloned()[0];
+ let wire = WireJob::from_job(&s.thumb_job(id, 128).unwrap(), false, "Thumb");
+ let mut v = serde_json::to_value(&wire).unwrap();
+ v.as_object_mut().unwrap().remove("window");
+ let old: WireJob = serde_json::from_value(v).unwrap();
+ assert_eq!(old.request().window, None);
+ }
+
#[test]
fn wire_job_renders_like_the_engine() {
let mut s = Session::with_demo();
@@ -394,6 +428,7 @@ mod tests {
thumb_cached: false,
stages: Some("Main".into()),
overlay: (0, 0.0),
+ window: None,
};
let mut core = WorkerCore::default();
assert_eq!(core.needs_original(&job).as_deref(), Some(hash.as_str()));
diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rs
index ed080e141..fb6d9115f 100644
--- a/crates/ui-egui/src/panels/detail.rs
+++ b/crates/ui-egui/src/panels/detail.rs
@@ -355,7 +355,7 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
&& view_overlay(app, &d) == lightcraft_pipeline::Overlay::None;
let drawn_long = target_rect.width().max(target_rect.height()) * ppp;
let settled_edge = app.ui.settings.loupe_edge(drawn_long, native_long);
- let region_possible = plain_view && app.renderer.can_render_windows() && crate::region::needed(drawn_long, settled_edge);
+ let region_possible = plain_view && crate::region::needed(drawn_long, settled_edge);
let look = d.hash64();
let mut region_full = None;
if region_possible {
diff --git a/crates/ui-egui/src/region.rs b/crates/ui-egui/src/region.rs
index d2ab97635..2b961c958 100644
--- a/crates/ui-egui/src/region.rs
+++ b/crates/ui-egui/src/region.rs
@@ -13,7 +13,7 @@ pub const SNAP: usize = 256;
/// The most context rendered beyond the visible pixels (a multiple of [`SNAP`]).
pub const MAX_MARGIN: usize = 768;
/// The most a window may span on either axis: a bound on its texture whatever the window size.
-pub const MAX_SPAN: usize = 6144;
+pub const MAX_SPAN: usize = if cfg!(target_arch = "wasm32") { 3072 } else { 6144 };
/// A window render the loupe asked for: which photo, the size of the zoomed frame it is a window
/// of, and the window. The texture that comes back is drawn at this place of the frame.
diff --git a/crates/ui-egui/src/render.rs b/crates/ui-egui/src/render.rs
index 37e4b5c2a..e955d8d4b 100644
--- a/crates/ui-egui/src/render.rs
+++ b/crates/ui-egui/src/render.rs
@@ -230,12 +230,6 @@ impl Renderer {
}
true
}
- /// Whether windows of the frame can be rendered here: not in the browser build, whose workers
- /// rebuild the request from a wire format that has no window yet.
- pub fn can_render_windows(&self) -> bool {
- self.offload.is_none() && !cfg!(target_arch = "wasm32")
- }
-
/// Free a view slot's texture and stage cache (the view is no longer shown).
pub fn release(&mut self, slot: Slot) {
self.textures.remove(&slot);