Fix #7524: the canvas's frame fits the display's largest texture side

The canvas rendered its art at the display's pixels per point whatever
the canvas's size, so a canvas more device pixels across than egui's
max_texture_side asked for a texture larger than the display supports.
The tiled panes did the same, and Pixel Preview allowed regions of up to
16,384 document pixels. In
canvas::panes::tests::tiled_documents_fit_their_panes_at_every_ui_scaling,
the pass where a new UI zoom takes effect sizes the screen from the
default 10,000-point viewport, and the test failed whenever the render
worker finished that frame before the next pass.

- The canvas and the tiled panes lower the frame's pixels per point when
  a side would pass max_texture_side, and draw the frame over the whole
  canvas at that lower resolution.
- Pans, scrolls and scroll bar drags move the view by whole pixels of
  that frame, so a pan still copies the pixels it moved.
- Pixel Preview shows the normal render when its region of document
  pixels is larger than max_texture_side.
- Tests that drew a canvas wider than egui's default limit of 2,048
  pixels now state the limit their display reports.
This commit is contained in:
dotCooCoo
2026-11-14 17:07:48 -08:00
parent 895e9493d2
commit b599942e21
8 changed files with 191 additions and 22 deletions
+5 -3
View File
@@ -65,7 +65,7 @@ use overlays::selected_anchor;
use overlays::{HandleLook, blend_widgets_overlay, draw_inside_overlay, draw_overlays, hover_highlight, ime_geometry, selection_overlay};
pub use panes::Panes;
pub(crate) use panes::{consolidate, pane, tile, tiled_area};
use raster::{changed_pixels, pixel_region, upload};
use raster::{changed_pixels, frame_ppp, pixel_region, upload};
pub(crate) use raster::{color_image, color_image_owned, render_job};
pub use selection_marks::SelectionMarks;
#[cfg(test)]
@@ -322,7 +322,9 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
}
// Artwork raster.
let ppp = ui.ctx().pixels_per_point();
let device_ppp = ui.ctx().pixels_per_point();
let max_side = ui.ctx().input(|i| i.max_texture_side);
let ppp = frame_ppp(rect.size(), device_ppp, max_side);
// Match Renderer::render_as before recording the cache extent or building its view.
let (w, h) = (
((rect.width() * ppp).round().max(1.0) as u32).min(u32::from(u16::MAX)),
@@ -330,7 +332,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
);
// View › Pixel Preview: the art as it rasterizes, one pixel per point, while a document pixel
// is bigger than a screen pixel (below that the screen render already shows it).
let pixel = (app.ui.view.pixel_preview && xf.zoom * ppp as f64 > 1.0).then(|| pixel_region(&xf)).flatten();
let pixel = (app.ui.view.pixel_preview && xf.zoom * device_ppp as f64 > 1.0).then(|| pixel_region(&xf, max_side)).flatten();
// File Handling › Display Bitmaps as Anti-aliased Images in Pixel Preview: off, images show
// their pixels as they rasterize, hard-edged.
let smooth_images = pixel.is_none() || app.session.prefs.anti_aliased_bitmaps;
+3 -3
View File
@@ -5,8 +5,8 @@ use super::drag::{
tool_drag_moved, tool_frame_moves, tool_holds, tool_press_doc_id,
};
use super::view::{
HOLD_DELAY, HOLD_ZOOM, SCRUB_SLOP, SCRUB_ZOOM, WheelTurn, animated_zoom, pan_pixels, rotate_about, scroll_view, touch_gestures, trackpad_turn,
wheel, whole_pixels, zoom_about,
HOLD_DELAY, HOLD_ZOOM, SCRUB_SLOP, SCRUB_ZOOM, WheelTurn, animated_zoom, pan_pixels, pan_ppp, rotate_about, scroll_view, touch_gestures,
trackpad_turn, wheel, whole_pixels, zoom_about,
};
use super::{Color32, Drag, Mods, Point, PointerEvent, PointerKind, Pos2, Shape, Stroke, Ui, VectorcraftApp, View, Xf, json};
@@ -361,7 +361,7 @@ fn drag_held(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Pos2)
let ui_zoom = app.canvas_ui_zoom;
match d {
Drag::Pan { start, center, .. } => {
let ppp = ui.ctx().pixels_per_point();
let ppp = pan_ppp(app, ui);
if let Some(vm) = app.view_mut() {
vm.center = center;
pan_pixels(vm, (ppp, ui_zoom), whole_pixels(p - start, ppp));
+6 -3
View File
@@ -12,7 +12,7 @@ use super::outline::{c32, node_outline, stroke_path, too_many_anchors};
use super::view::fit_zoom;
use super::{
Affine, CacheKey, Point, Rect, RulerScale, Sense, Stroke, StrokeKind, Tokens, Ui, VectorcraftApp, View, Xf, artboard_edges, cache_key,
dim_on_canvas, frame_options, frame_view, grid, guides_of, json, paint_paper, pixel_region, placed_key, plugin_key, pos2, rulers,
dim_on_canvas, frame_options, frame_ppp, frame_view, grid, guides_of, json, paint_paper, pixel_region, placed_key, plugin_key, pos2, rulers,
shows_artboards, textured_quad, vec2, view_rects,
};
use crate::doc_windows::Win;
@@ -336,10 +336,13 @@ fn draw_pane(app: &mut VectorcraftApp, ui: &Ui, w: Win, cell: egui::Rect) -> boo
if let Some(look) = grid_look.filter(|_| grids_in_back) {
grid(&painter, &xf, st.doc.grid.spacing, st.doc.grid.subdivisions, look);
}
// View › Pixel Preview, as the canvas draws it.
let max_side = ui.ctx().input(|i| i.max_texture_side);
let pixel = (app.ui.view.pixel_preview && xf.zoom * ppp as f64 > 1.0).then(|| pixel_region(&xf, max_side)).flatten();
// The pane's frame fits the largest texture side the display supports, as the canvas's does.
let ppp = frame_ppp(rect.size(), ppp, max_side);
let size = |l: f32| ((l * ppp).round().max(1.0) as u32).min(u32::from(u16::MAX));
let (pw, ph) = (size(rect.width()), size(rect.height()));
// View › Pixel Preview, as the canvas draws it.
let pixel = (app.ui.view.pixel_preview && xf.zoom * ppp as f64 > 1.0).then(|| pixel_region(&xf)).flatten();
let smooth_images = pixel.is_none() || app.session.prefs.anti_aliased_bitmaps;
let plugins = plugin_key(&mut app.canvas.plugin_keys, &app.session, st);
let placed = placed_key(&mut app.canvas.placed_keys, &app.session, st);
+34
View File
@@ -323,6 +323,40 @@ fn other_panes_show_pixel_preview() {
assert!(x1 > x0 && y1 > y0);
}
/// A pane more device pixels across than the largest texture side the display supports renders
/// its document within that size, as the canvas does, and the frame covers the pane.
#[test]
fn other_panes_render_within_the_largest_texture_side() {
let mut app = app_with(2);
app.canvas.panes.worker_started = true; // every pane renders on this thread
let ctx = egui::Context::default();
ctx.set_pixels_per_point(2.0);
app.run("window.arrange.tile", json!({})).unwrap();
// The first frame sets the pixels a point, the second draws at them.
for _ in 0..2 {
let raw = egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, vec2(1200.0, 800.0))),
max_texture_side: Some(1024),
..Default::default()
};
ctx.run_ui(raw, |ui| app.canvas_area(ui)).textures_delta.clear();
}
let uid = (app.session.documents()[0].uid, 1);
let (key, tex) = app.canvas.panes.shown.get(&uid).map(|(k, t)| (k.clone(), t.size())).expect("the other document's pane rendered");
assert_eq!(tex[0].max(tex[1]), 1024, "the pane's frame is {tex:?} pixels");
let (docs, cells) = panes(&app);
let pane = docs.iter().zip(cells).find(|(i, _)| **i == 0).map(|(_, r)| r).unwrap();
let rect = view_rects(&app, pane, ctx.pixels_per_point()).1;
assert!(rect.width().max(rect.height()) * ctx.pixels_per_point() > 1024.0, "the pane is {rect:?} at {} pixels a point", ctx.pixels_per_point());
assert!(
(key.w as f32 / key.ppp - rect.width()).abs() < 1.0 && (key.h as f32 / key.ppp - rect.height()).abs() < 1.0,
"the frame covers the pane {rect:?}: {} × {} pixels at {} a point",
key.w,
key.h,
key.ppp
);
}
/// A pane's render key takes its document's [`CacheKey::placed`] value ([`placed_key`]), which a
/// bitmap made for a test running beside this one does not move: the document holds no images.
#[test]
+15 -3
View File
@@ -241,9 +241,20 @@ pub(super) fn upload(app: &mut VectorcraftApp, ctx: &egui::Context, img: vectorc
}
}
/// The device pixels a point that the art's frame of a canvas `size` points across renders at:
/// the display's `ppp`, lowered when a side of the frame would be more than `max_side` pixels, the
/// largest texture side the display supports (egui's `max_texture_side`). The frame is then drawn
/// scaled up over the canvas, at a lower resolution.
pub(super) fn frame_ppp(size: egui::Vec2, ppp: f32, max_side: usize) -> f32 {
let longest = size.x.max(size.y) * ppp;
let max = max_side.clamp(1, usize::from(u16::MAX)) as f32;
if longest.round() <= max { ppp } else { ppp * (max / longest) }
}
/// Pixel Preview: the whole document pixels (x0, y0, x1, y1) under the canvas, rotated view
/// included. None when the view is degenerate.
pub(super) fn pixel_region(xf: &Xf) -> Option<[i64; 4]> {
/// included. None when the view is degenerate, or when a side of the region is more than
/// `max_side` pixels, the largest texture side the display supports.
pub(super) fn pixel_region(xf: &Xf, max_side: usize) -> Option<[i64; 4]> {
let r = xf.rect;
let corners = [r.left_top(), r.right_top(), r.right_bottom(), r.left_bottom()].map(|p| xf.to_doc(p));
let (mut x0, mut y0, mut x1, mut y1) = (f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
@@ -252,6 +263,7 @@ pub(super) fn pixel_region(xf: &Xf) -> Option<[i64; 4]> {
}
let (x0, y0, x1, y1) = (x0.floor(), y0.floor(), x1.ceil(), y1.ceil());
// A document pixel is bigger than a screen pixel here, so the region is about canvas-sized.
let ok = [x0, y0, x1, y1].iter().all(|c| c.is_finite() && c.abs() < 1e9) && x1 > x0 && y1 > y0 && x1 - x0 <= 16384.0 && y1 - y0 <= 16384.0;
let max = max_side.min(16384) as f64;
let ok = [x0, y0, x1, y1].iter().all(|c| c.is_finite() && c.abs() < 1e9) && x1 > x0 && y1 > y0 && x1 - x0 <= max && y1 - y0 <= max;
ok.then_some([x0 as i64, y0 as i64, x1 as i64, y1 as i64])
}
+107 -2
View File
@@ -2408,6 +2408,45 @@ fn an_edit_renders_only_its_part_of_the_canvas() {
assert!(canvas(&app, delta).is_some_and(|(pos, _)| pos.is_none()), "a new view renders all of it");
}
/// A canvas more device pixels across than the largest texture side the display supports renders
/// its art at fewer pixels a point, within that size, and draws it over the whole canvas. Pixel
/// Preview's region of document pixels stays within it too.
#[test]
fn the_art_renders_within_the_largest_texture_side() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width":400,"height":300})).unwrap();
app.canvas.worker_started = true; // every frame on this thread, however long it takes
let ctx = egui::Context::default();
ctx.set_pixels_per_point(2.0);
// The first frame sets the pixels a point, the second draws at them.
for _ in 0..2 {
let raw = egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0))),
max_texture_side: Some(1024),
..Default::default()
};
let mut out = ctx.run_ui(raw, |ui| show(&mut app, ui));
out.textures_delta.clear();
}
let rect = app.canvas_rect.unwrap();
assert!(rect.width() * ctx.pixels_per_point() > 1024.0, "the canvas is {rect:?} at {} pixels a point", ctx.pixels_per_point());
let size = app.canvas.texture.as_ref().map(|t| t.size()).unwrap();
assert_eq!(size[0].max(size[1]), 1024, "the frame is {size:?} pixels");
let k = app.canvas.key.clone().unwrap();
assert!(
(k.w as f32 / k.ppp - rect.width()).abs() < 1.0 && (k.h as f32 / k.ppp - rect.height()).abs() < 1.0,
"the frame covers the canvas {rect:?}: {} × {} pixels at {} a point",
k.w,
k.h,
k.ppp
);
let xf = Xf { rect: egui::Rect::from_min_size(Pos2::ZERO, vec2(1000.0, 500.0)), zoom: 0.5, center: Point::new(0.0, 0.0), rot: 0.0 };
let [x0, _, x1, _] = pixel_region(&xf, 4096).expect("a region 2,000 document pixels wide");
assert_eq!(x1 - x0, 2000);
assert_eq!(pixel_region(&xf, 1024), None, "more than the display's largest texture");
}
/// A placed document's bitmap made in the background renders the canvas again only where the
/// objects showing its file are: both copies of the file are in the part rendered again.
#[test]
@@ -3149,7 +3188,8 @@ fn a_drag_back_to_its_start_renders_again_after_it() {
assert!(!app.canvas.moved);
}
/// Cache metadata, worker requests and view transforms use the renderer's bounded raster size.
/// Cache metadata, worker requests and view transforms use the renderer's bounded raster size,
/// on a display that reports a larger texture side than that.
/// Keep the worker controlled so huge virtual viewports never allocate huge test images.
#[test]
fn canvas_render_jobs_use_renderer_limited_dimensions() {
@@ -3160,7 +3200,11 @@ fn canvas_render_jobs_use_renderer_limited_dimensions() {
app.canvas.worker_started = true;
app.ui.view.rulers = false;
let frame = |app: &mut VectorcraftApp, size, ppp| {
let mut input = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(pos2(0.0, 0.0), size)), ..Default::default() };
let mut input = egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(pos2(0.0, 0.0), size)),
max_texture_side: Some(1 << 20),
..Default::default()
};
input.viewports.get_mut(&egui::ViewportId::ROOT).unwrap().native_pixels_per_point = Some(ppp);
let mut output = ctx.run_ui(input, |ui| show(app, ui));
output.textures_delta.clear();
@@ -4741,6 +4785,67 @@ fn scrolls_and_pans_move_the_view_by_whole_device_pixels() {
frame(&mut app, vec![primary(p0 + vec2(-5.55, 22.71), false)]);
}
/// On a canvas whose frame renders at fewer pixels a point than the display's ([`frame_ppp`]),
/// the wheel and the Hand tool move the view by whole pixels of that frame.
#[test]
fn scrolls_and_pans_move_a_lowered_frame_by_whole_pixels() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
app.canvas.worker_started = true; // every frame on this thread
let ctx = egui::Context::default();
ctx.set_pixels_per_point(2.0);
let screen = egui::Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0));
let mut time = 0.0;
let mut frame = |app: &mut VectorcraftApp, events: Vec<egui::Event>| {
time += 0.016;
let input = egui::RawInput { time: Some(time), screen_rect: Some(screen), max_texture_side: Some(1024), events, ..Default::default() };
ctx.run_ui(input, |ui| show(app, ui)).textures_delta.clear();
};
frame(&mut app, vec![]);
app.view_mut().unwrap().center = Point::new(200.3, 150.7);
frame(&mut app, vec![]);
let ppp = app.canvas.key.as_ref().map(|k| k.ppp).unwrap();
assert!(ppp < 2.0, "the frame renders at {ppp} pixels a point");
// The pan of the canvas's render view, in pixels of its frame.
let pan = |app: &VectorcraftApp| {
let v = app.view().unwrap();
(Affine::scale(v.zoom * f64::from(ppp)) * Affine::translate(-v.center.to_vec2())).translation()
};
let whole = |by: vectorcraft_geom::Vec2| (by - vectorcraft_geom::Vec2::new(by.x.round(), by.y.round())).hypot() < 1e-6;
let wheel = |d: egui::Vec2| egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: d,
phase: egui::TouchPhase::Move,
modifiers: Default::default(),
source: egui::MouseWheelSource::Unknown,
};
let rect = app.canvas_rect.unwrap();
frame(&mut app, vec![egui::Event::PointerMoved(rect.center())]);
let start = pan(&app);
for (k, d) in [vec2(0.37, 11.13), vec2(-12.71, 0.0), vec2(21.9, -3.83)].into_iter().enumerate() {
frame(&mut app, vec![wheel(d)]);
let by = pan(&app) - start;
assert!(whole(by), "scroll {k}: the view moved {by:?} pixels of the frame");
}
for k in 0..60 {
frame(&mut app, vec![]);
let by = pan(&app) - start;
assert!(whole(by), "frame {k} after the scrolls: the view moved {by:?} pixels of the frame");
}
assert!((pan(&app) - start).hypot() > 10.0, "the wheel moved the view");
app.select_tool("hand");
let p0 = rect.center() + vec2(0.3, 0.1);
frame(&mut app, vec![egui::Event::PointerMoved(p0)]);
let start = pan(&app);
frame(&mut app, vec![primary(p0, true)]);
for (k, d) in [vec2(13.37, -7.61), vec2(40.19, 3.33)].into_iter().enumerate() {
frame(&mut app, vec![egui::Event::PointerMoved(p0 + d)]);
let by = pan(&app) - start;
assert!(whole(by), "drag {k}: the view moved {by:?} pixels of the frame");
}
frame(&mut app, vec![primary(p0 + vec2(40.19, 3.33), false)]);
}
/// The part of a device pixel a scroll leaves is added to the next scroll of the same view, and
/// dropped when the view was zoomed, turned or moved in between, when the next scroll is in
/// another document, or when the device pixels a point changed.
@@ -10,10 +10,15 @@ use vectorcraft_engine::Session;
const WINDOW: egui::Vec2 = vec2(1200.0, 900.0);
/// One canvas frame in a `WINDOW` window at `native` device pixels a point, with the interface
/// zoomed by `ui_zoom`.
/// zoomed by `ui_zoom`, on a display whose textures are up to 8,192 pixels a side.
fn frame(app: &mut VectorcraftApp, ctx: &egui::Context, (native, ui_zoom): (f32, f32), events: Vec<egui::Event>) {
ctx.set_zoom_factor(ui_zoom);
let mut input = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, WINDOW / ui_zoom)), events, ..Default::default() };
let mut input = egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, WINDOW / ui_zoom)),
max_texture_side: Some(8192),
events,
..Default::default()
};
input.viewports.entry(egui::ViewportId::ROOT).or_default().native_pixels_per_point = Some(native);
let mut out = ctx.run_ui(input, |ui| show(app, ui));
out.textures_delta.clear();
+14 -6
View File
@@ -139,10 +139,18 @@ pub(super) fn whole_pixels(d: egui::Vec2, ppp: f32) -> (f64, f64) {
((f64::from(d.x) * f64::from(ppp)).round(), (f64::from(d.y) * f64::from(ppp)).round())
}
/// The pixels a point that the canvas's art frame renders at ([`super::frame_ppp`]): the display's,
/// or fewer on a canvas wider than the largest texture side. Pans and scrolls move the view by
/// whole pixels of that frame.
pub(super) fn pan_ppp(app: &VectorcraftApp, ui: &Ui) -> f32 {
let ppp = ui.ctx().pixels_per_point();
app.canvas_rect.map_or(ppp, |r| super::frame_ppp(r.size(), ppp, ui.ctx().input(|i| i.max_texture_side)))
}
/// What the scrolls of the view have left to move ([`scroll_view`]).
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(super) struct ScrollRest {
/// Device pixels along the screen's axes, at most half a pixel each.
/// Pixels of the canvas's frame along the screen's axes, at most half a pixel each.
pub(super) x: f64,
pub(super) y: f64,
/// The document, the view's center, zoom and rotation, and the device pixels a point that the
@@ -151,11 +159,11 @@ pub(super) struct ScrollRest {
}
/// Scroll the active view so that its content moves `by` screen points (the wheel, a trackpad, a
/// touch screen, a scroll bar), in whole device pixels: the part of a pixel left over is added to
/// the next scroll. It is dropped when the view changed in between (a zoom, a turn, Fit, a pan
/// of another kind, another document) or the device pixels a point did.
/// touch screen, a scroll bar), in whole pixels of the canvas's frame ([`pan_ppp`]): the part of a
/// pixel left over is added to the next scroll. It is dropped when the view changed in between (a
/// zoom, a turn, Fit, a pan of another kind, another document) or the pixels a point did.
pub(super) fn scroll_view(app: &mut VectorcraftApp, ui: &Ui, by: egui::Vec2) {
let ppp = ui.ctx().pixels_per_point();
let ppp = pan_ppp(app, ui);
let ui_zoom = app.canvas_ui_zoom;
let Some(doc) = app.session.active().map(|st| st.uid) else { return };
let Some(vm) = app.view_mut() else { return };
@@ -344,7 +352,7 @@ pub(super) fn scroll_bars(app: &mut VectorcraftApp, ui: &Ui, rect: egui::Rect, o
let hot = drag.is_some() || hover.is_some_and(|p| thumb.contains(p));
painter.rect_filled(thumb, CornerRadius::same(3), if hot { t.text_dim } else { t.text_disabled });
}
let ppp = ui.ctx().pixels_per_point();
let ppp = pan_ppp(app, ui);
if let Some((c, by)) = center
&& let Some(vm) = app.view_mut()
{