Canvas (web): an edit while a heavy document's view moves draws only what it changed

In the web build (no render worker), while the view of a document whose
last render took more than 8 ms moves, the canvas draws the last frame
moved with the view and renders once the view is still. An edit in
that time rendered the whole canvas at the new view, every frame: a
tool drag held at an edge of the canvas (#8551) or a wheel scroll during
a drag did that on each step.

Such an edit is now drawn into the frame shown, at the view it was
rendered for: only the part the edit changed (changed_pixels). The
frame stays drawn reprojected and renders whole once the view has been
still for STILL_SECS, as for a view change without an edit. An edit
that changes more than half of the frame, or one while more than the
view and the document change, renders whole at once as before. The
partial redraw of draw_frame moves into draw_changed, shared by both.

Fixes #8688
This commit is contained in:
HassanKhan
2026-11-19 21:30:16 +05:00
parent 644f5bc8e3
commit 4c5ba5516d
4 changed files with 105 additions and 29 deletions
+70 -22
View File
@@ -355,8 +355,10 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
poll_worker(app, ui, key.doc);
// With `waits_for_still` set (the web build, which has no render worker), a heavy document is
// not rendered while only the view changes: the frame shown is drawn reprojected below, and the
// first frame after the view has been still for STILL_SECS renders.
// first frame after the view has been still for STILL_SECS renders. An edit meanwhile is drawn
// into the frame shown ([`edit_in_frame_shown`]).
let wait = if app.canvas.waits_for_still {
edit_in_frame_shown(app, &key, &doc, &effects, &drawn, (mask_view, smooth_images));
let slow = app.canvas.last_ms > 8.0 && app.canvas.texture.is_some();
still_wait(&mut app.canvas.still, app.canvas.key.as_ref(), &key, slow, ui.input(|i| i.time))
} else {
@@ -881,6 +883,72 @@ struct Frame<'a> {
/// shows the document's paper without the art: about a frame at 60 Hz.
pub(crate) const FIRST_FRAME_GRACE: std::time::Duration = std::time::Duration::from_millis(16);
/// Draw `region` of `frame` again, into the texture of the frame shown, which then shows `frame`'s
/// key: only the document changed since the frame shown, in that part of it
/// ([`changed_pixels`]). `None`: nothing in view changed. `last_ms` stays the whole frame's.
fn draw_changed(app: &mut VectorcraftApp, frame: &Frame, region: Option<[u32; 4]>) {
let Frame { key, doc, effects, view, opts, pixel, .. } = frame;
let t0 = now_ms();
if region.is_none() {
// Nothing in view to draw again: what was made to measure the edit isn't drawn.
app.canvas.renderer.let_go_of_measured();
}
let display = app.display_profile.transform(app.session.prefs.monitor_profile_display);
if let (Some([x0, y0, x1, y1]), Some(tex)) = (region, &mut app.canvas.texture) {
let renderer = &mut app.canvas.renderer;
let part_view = Affine::translate((-f64::from(x0), -f64::from(y0))) * *view;
let mut part = effects.view(|| renderer.render(doc, x1 - x0, y1 - y0, part_view, opts));
let width = part.width;
crate::display_profile::convert_over(display.as_deref(), &mut part.pixels, width, part_view, key.over());
let color = color_image(&part);
let options = if pixel.is_some() { egui::TextureOptions::NEAREST } else { egui::TextureOptions::LINEAR };
tex.set_partial([x0 as usize, y0 as usize], color, options);
app.canvas.moved |= app.canvas.renderer.stats.moved > 0;
}
app.canvas.key = Some((*key).clone());
app.canvas.shown = Some(((*doc).clone(), opts.clone()));
app.perf.render_ms = now_ms() - t0;
}
/// With `waits_for_still` set (the web build) and the last render slow, an edit while the view
/// moves (a tool's drag that scrolls the view at an edge of the canvas, or a wheel scroll during a
/// drag) is drawn into the frame shown, at the view it was rendered for: the part the edit changed
/// only ([`changed_pixels`]). The frame shown then stays drawn reprojected with the view, and the
/// canvas renders whole once the view has been still for [`STILL_SECS`], as for a view change
/// without an edit (#8688). When more than the view and the document differ from the frame shown,
/// or the edit changed more than half of it, nothing is drawn here: the frame renders whole.
fn edit_in_frame_shown(
app: &mut VectorcraftApp,
key: &CacheKey,
doc: &std::sync::Arc<vectorcraft_doc::Document>,
effects: &vectorcraft_plugins::deferred::Deferrals,
drawn: &View,
(mask_view, smooth_images): (Option<vectorcraft_doc::NodeId>, bool),
) {
let slow = app.canvas.last_ms > 8.0 && app.canvas.texture.is_some();
let Some(shown) = app.canvas.key.clone().filter(|_| slow) else { return };
let at_shown = CacheKey {
zoom: shown.zoom,
cx: shown.cx,
cy: shown.cy,
w: shown.w,
h: shown.h,
ppp: shown.ppp,
rot: shown.rot,
pixel: shown.pixel,
..key.clone()
};
if at_shown == shown || at_shown == *key || shown.pixel.is_some() != key.pixel.is_some() {
return;
}
let v = View { center: Point::new(shown.cx, shown.cy), zoom: shown.zoom, rotation: shown.rot, ..*drawn };
let (w, h, view) = frame_view(at_shown.pixel, (at_shown.w, at_shown.h), &v, at_shown.ppp);
let opts = frame_options(app, &at_shown, mask_view, smooth_images);
if let Some(region) = changed_pixels(app, &at_shown, doc, &opts, view, w, h) {
draw_changed(app, &Frame { key: &at_shown, doc, effects, size: (w, h), view, opts, pixel: at_shown.pixel }, region);
}
}
fn draw_frame(app: &mut VectorcraftApp, ui: &Ui, frame: Frame) {
let Frame { key, doc, effects, size: (w, h), view, opts, pixel } = frame;
// Frames that would be slow on this thread (the last one took over 8 ms) go
@@ -904,27 +972,7 @@ fn draw_frame(app: &mut VectorcraftApp, ui: &Ui, frame: Frame) {
match (&mut app.canvas.worker, heavy, changed) {
// Only the document changed since the frame shown, in a part of it: that part is
// rendered again, here, into the texture. `last_ms` stays the whole frame's.
(_, _, Some(region)) => {
let t0 = now_ms();
if region.is_none() {
// Nothing in view to draw again: what was made to measure the edit isn't drawn.
app.canvas.renderer.let_go_of_measured();
}
if let (Some([x0, y0, x1, y1]), Some(tex)) = (region, &mut app.canvas.texture) {
let renderer = &mut app.canvas.renderer;
let part_view = Affine::translate((-f64::from(x0), -f64::from(y0))) * view;
let mut part = effects.view(|| renderer.render(doc, x1 - x0, y1 - y0, part_view, &opts));
let width = part.width;
crate::display_profile::convert_over(display.as_deref(), &mut part.pixels, width, part_view, key.over());
let color = color_image(&part);
let options = if pixel.is_some() { egui::TextureOptions::NEAREST } else { egui::TextureOptions::LINEAR };
tex.set_partial([x0 as usize, y0 as usize], color, options);
app.canvas.moved |= app.canvas.renderer.stats.moved > 0;
}
app.canvas.key = Some(key.clone());
app.canvas.shown = Some((doc.clone(), opts.clone()));
app.perf.render_ms = now_ms() - t0;
}
(_, _, Some(region)) => draw_changed(app, &Frame { key, doc, effects, size: (w, h), view, opts, pixel }, region),
(Some(worker), true, None) => {
// The worker draws the frame: what this thread's renderer made to measure the edit
// isn't drawn here.
+29 -4
View File
@@ -4357,7 +4357,8 @@ fn a_pan_fills_the_area() {
/// With the web build's setting (`waits_for_still`, no render worker), a heavy document's canvas
/// is not rendered while the view moves: each frame draws the frame shown moved and scaled with
/// the view and asks for a frame within STILL_SECS. The canvas renders once the view has been
/// still for STILL_SECS, and at once after an edit. With a worker's build settings the same frames render at once, as
/// still for STILL_SECS. An edit while the view moves draws the part it changed into the frame
/// shown, and one that changes most of the frame renders it whole at once. With a worker's build settings the same frames render at once, as
/// before.
#[test]
fn without_a_worker_a_heavy_canvas_is_reprojected_until_the_view_is_still() {
@@ -4421,15 +4422,39 @@ fn without_a_worker_a_heavy_canvas() {
let now = app.canvas.key.clone().unwrap();
assert_eq!((now.cx, now.zoom), (app.view().unwrap().center.x, app.view().unwrap().zoom), "rendered for the view");
assert!((b.min - rect.min).length() < 0.01 && (b.max - rect.max).length() < 0.01, "{b:?} over {rect:?}");
// An edit renders at once, even while the view moves.
// An edit while the view moves shows at once: the part it changed is drawn into the frame
// shown, at the view that frame was rendered for, and the frame stays drawn reprojected until
// the view is still (#8688).
app.canvas.last_ms = 50.0;
app.view_mut().unwrap().center.y += 10.0;
frame(&mut app);
assert_eq!(app.canvas.key.as_ref(), Some(&now), "waiting");
app.session.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();
app.session.execute("shape.rectangle", &json!({"x": 110, "y": 60, "width": 20, "height": 20})).unwrap();
let render_ms = app.canvas.last_ms;
let (b, delay) = frame(&mut app);
let k = app.canvas.key.clone().unwrap();
assert_eq!(k.revision, app.session.active().unwrap().revision, "the edit is drawn");
assert_eq!((k.cx, k.cy, k.zoom), (now.cx, now.cy, now.zoom), "at the view of the frame shown");
assert_eq!(app.canvas.last_ms, render_ms, "the frame was not rendered whole");
let (old, new) = (Xf::new(rect, &View { center: Point::new(now.cx, now.cy), ..*app.view().unwrap() }), Xf::new(rect, app.view().unwrap()));
let (a, z) = (new.to_screen(old.to_doc(rect.min)), new.to_screen(old.to_doc(rect.max)));
assert!((b.min - a).length() < 0.01 && (b.max - z).length() < 0.01, "{b:?}: the frame shown at {a:?} to {z:?}");
assert!(delay <= std::time::Duration::from_secs_f64(STILL_SECS), "a frame within STILL_SECS: {delay:?}");
let mut frames = 0;
while app.canvas.key.as_ref() == Some(&k) && frames < 60 {
frames += 1;
frame(&mut app);
}
let k = app.canvas.key.clone().unwrap();
assert_eq!((k.revision, k.cy), (app.session.active().unwrap().revision, app.view().unwrap().center.y), "rendered once still");
// An edit that changes more than half of the frame renders whole at once, view and all.
app.canvas.last_ms = 50.0;
app.view_mut().unwrap().center.y += 10.0;
frame(&mut app);
app.session.execute("shape.rectangle", &json!({"x": -1000, "y": -1000, "width": 3000, "height": 3000})).unwrap();
frame(&mut app);
let k = app.canvas.key.clone().unwrap();
assert_eq!((k.revision, k.cy), (app.session.active().unwrap().revision, app.view().unwrap().center.y), "rendered after the edit");
assert_eq!((k.revision, k.cy), (app.session.active().unwrap().revision, app.view().unwrap().center.y), "rendered whole after the edit");
// With a worker's build settings and no worker (VECTORCRAFT_SYNC_RENDER), a heavy document
// renders every change of view at once.
app.canvas.waits_for_still = false;
+3 -2
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long