mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-21 20:28:51 +00:00
Merge pull request #8764 from storytold/train/carry8736
Canvas (web): an edit while a heavy document's view moves draws only what it changed (#8736)
This commit is contained in:
@@ -25,8 +25,8 @@ pub struct CanvasCache {
|
||||
/// last render took more than 8 ms, the canvas draws the frame shown reprojected and renders
|
||||
/// once the view has been still for [`canvas::STILL_SECS`].
|
||||
pub waits_for_still: bool,
|
||||
/// The cache key the canvas last asked for and when it first asked for it (seconds, egui's
|
||||
/// input time), kept while [`Self::waits_for_still`] is set.
|
||||
/// The cache key the canvas last asked for and when it first asked for its view (seconds,
|
||||
/// egui's input time), kept while [`Self::waits_for_still`] is set.
|
||||
pub still: Option<(CacheKey, f64)>,
|
||||
/// The low-resolution frame that covers the parts of the canvas the frame shown leaves
|
||||
/// uncovered while it is drawn for a view it was not rendered for ([`canvas::Fill`]).
|
||||
|
||||
@@ -359,8 +359,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 {
|
||||
@@ -1150,6 +1152,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
|
||||
@@ -1173,27 +1241,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.
|
||||
|
||||
@@ -51,11 +51,13 @@ pub(super) fn hover_wait(moved: &mut Option<(ViewKey, f64)>, key: ViewKey, now:
|
||||
|
||||
/// Note that the canvas asks for `key` at time `now` (seconds, egui's input time) → how many more
|
||||
/// seconds it draws the frame `shown` reprojected instead of rendering `key`: while the last
|
||||
/// render was `slow`, only the view differs between the two, and the canvas has asked for `key`
|
||||
/// for less than [`STILL_SECS`]. `still` holds the key last asked for and when the canvas first
|
||||
/// asked for it. None: the canvas renders `key` now, unless it is the frame shown.
|
||||
/// render was `slow`, only the view differs between the two, and the canvas has asked for the view
|
||||
/// of `key` for less than [`STILL_SECS`]. `still` holds the key last asked for and when the canvas first
|
||||
/// asked for its view. An edit leaves that time as it is: with the view still and edits drawn into
|
||||
/// the frame shown at each step of a drag, the canvas still renders whole once the view has been
|
||||
/// still for [`STILL_SECS`]. None: the canvas renders `key` now, unless it is the frame shown.
|
||||
pub(super) fn still_wait(still: &mut Option<(CacheKey, f64)>, shown: Option<&CacheKey>, key: &CacheKey, slow: bool, now: f64) -> Option<f64> {
|
||||
if still.as_ref().is_none_or(|(k, _)| k != key) {
|
||||
if still.as_ref().is_none_or(|(k, _)| !same_view(k, key)) {
|
||||
*still = Some((key.clone(), now));
|
||||
}
|
||||
let waited = still.as_ref().map_or(0.0, |(_, at)| now - at);
|
||||
@@ -64,6 +66,12 @@ pub(super) fn still_wait(still: &mut Option<(CacheKey, f64)>, shown: Option<&Cac
|
||||
(slow && waiting && shown.is_some_and(|s| s != key && only_view_differs(s, key))).then_some(STILL_SECS - waited)
|
||||
}
|
||||
|
||||
/// Whether two cache keys are of the same document at the same view (zoom, center, rotation,
|
||||
/// canvas size, pixel density and Pixel Preview's document pixels), whatever else differs.
|
||||
fn same_view(a: &CacheKey, b: &CacheKey) -> bool {
|
||||
(a.doc, a.zoom, a.cx, a.cy, a.w, a.h, a.ppp, a.rot, a.pixel) == (b.doc, b.zoom, b.cx, b.cy, b.w, b.h, b.ppp, b.rot, b.pixel)
|
||||
}
|
||||
|
||||
/// Whether two cache keys differ only in the view (zoom, center, rotation, canvas size, pixel
|
||||
/// density and Pixel Preview's document pixels), with Pixel Preview on in both or off in both.
|
||||
pub(super) fn only_view_differs(a: &CacheKey, b: &CacheKey) -> bool {
|
||||
|
||||
@@ -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;
|
||||
@@ -4439,6 +4464,48 @@ fn without_a_worker_a_heavy_canvas() {
|
||||
assert_eq!(app.canvas.key.as_ref().map(|k| k.cx), Some(app.view().unwrap().center.x), "rendered at once");
|
||||
}
|
||||
|
||||
/// With the web build's setting (`waits_for_still`, no render worker), a heavy document's view
|
||||
/// that stops moving while a drag goes on editing the document renders whole once the view has
|
||||
/// been still for STILL_SECS: each edit is drawn into the frame shown and does not start the wait
|
||||
/// again.
|
||||
#[test]
|
||||
fn without_a_worker_edits_while_the_view_is_still_do_not_hold_off_the_render() {
|
||||
keeping_color_settings(edits_while_the_view_is_still);
|
||||
}
|
||||
|
||||
fn edits_while_the_view_is_still() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
app.session.execute("shape.rectangle", &json!({"x": 100, "y": 50, "width": 200, "height": 200})).unwrap();
|
||||
app.canvas.worker_started = true; // no worker, as in the web build
|
||||
app.canvas.waits_for_still = true;
|
||||
let ctx = egui::Context::default();
|
||||
let frame = |app: &mut VectorcraftApp| {
|
||||
let raw = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0))), ..Default::default() };
|
||||
let mut out = ctx.run_ui(raw, |ui| show(app, ui));
|
||||
out.textures_delta.clear();
|
||||
};
|
||||
frame(&mut app); // the first frame fits the view to the artboard
|
||||
frame(&mut app);
|
||||
app.canvas.last_ms = 50.0;
|
||||
// The view moves once (a drag at an edge of the canvas scrolls it) and then stays still.
|
||||
app.view_mut().unwrap().center.y += 10.0;
|
||||
frame(&mut app);
|
||||
let shown = app.canvas.key.clone().unwrap();
|
||||
assert_ne!(shown.cy, app.view().unwrap().center.y, "waiting");
|
||||
// The drag edits the document on every frame, at 60 fps (egui's input time without a clock).
|
||||
let mut frames = 0;
|
||||
while app.canvas.key.as_ref().is_none_or(|k| k.cy != app.view().unwrap().center.y) && frames < 60 {
|
||||
frames += 1;
|
||||
let x = 110 + (frames % 10) * 5;
|
||||
app.session.execute("shape.rectangle", &json!({"x": x, "y": 60, "width": 4, "height": 4})).unwrap();
|
||||
frame(&mut app);
|
||||
let k = app.canvas.key.as_ref().unwrap();
|
||||
assert_eq!(k.revision, app.session.active().unwrap().revision, "each edit is drawn");
|
||||
}
|
||||
assert!((11..=13).contains(&frames), "rendered whole about STILL_SECS after the view stopped: {frames} frames");
|
||||
}
|
||||
|
||||
/// [`still_wait`] waits while only the view changes on a slow canvas: each change starts
|
||||
/// STILL_SECS again. It does not wait for a light document, a still view, a clock that went
|
||||
/// back, an edit, a switch into Pixel Preview or a canvas with no frame shown.
|
||||
|
||||
+3
-2
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user