Repository navigation
fix(tiles): raster tiles and WMS pictures are sharp on HiDPI displays #2392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9938a59
e6b10b4
27244a5
7e04da7
c221e1a
b659578
07956c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -608,6 +608,17 @@ fn tiles_for(src: &Source, view: &Extent, frame: &Frame, zoom: f32, pending: &mu | |
| tiles_in(src, view, None, frame, zoom, pending, error).items | ||
| } | ||
|
|
||
| /// The zoom of a raster source's `tile_size`-pixel tiles drawn at `frame` over `view`: by the | ||
| /// display's own pixels on a HiDPI one when sharp tiles are on (#2390), up to twice a logical | ||
| /// pixel's detail; back towards a logical pixel's zoom where the view's tiles there would pass the | ||
| /// most a source draws (a 5K or 6K display), rather than leave the view's corners out. | ||
| fn raster_zoom(frame: &Frame, view: &Extent, tile_size: u32, min_zoom: u8, max_zoom: u8) -> u8 { | ||
| let logical = template::zoom_for(frame.m_per_px * f64::from(frame.ppp), tile_size, max_zoom); | ||
| let sharp = template::zoom_for(frame.m_per_px * f64::from(frame.ppp / template::detail(frame.ppp)), tile_size, max_zoom); | ||
| // Never below the source's own coarsest zoom, which would draw nothing. | ||
| if sharp > logical { template::fitting_zoom(view, sharp, logical.max(min_zoom)) } else { sharp } | ||
| } | ||
|
|
||
| /// The tiles of a source in view and out to `reach` (those in the cache only), with stand-ins | ||
| /// for those not here yet. Expanded destinations retain the supplying document's reach; | ||
| /// refused tiles are counted in `error` and network history without requesting or reusing bytes. Valid | ||
|
|
@@ -624,8 +635,7 @@ pub(crate) fn tiles_in( | |
| let (tiles, from, min_z, max_z, want, z) = match src { | ||
| Source::Raster { tiles, from, tile_size, min_zoom, max_zoom, .. } | ||
| | Source::RasterDem { tiles, from, tile_size, min_zoom, max_zoom, .. } => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance (confidence ~40%): sharp zoom also applies to This arm now goes through If the extra elevation detail is wanted, please say so in |
||
| let z = template::zoom_for(frame.m_per_px * f64::from(frame.ppp), *tile_size, *max_zoom); | ||
| (tiles, from, *min_zoom, *max_zoom, Want::Image, z) | ||
| (tiles, from, *min_zoom, *max_zoom, Want::Image, raster_zoom(frame, view, *tile_size, *min_zoom, *max_zoom)) | ||
| } | ||
| Source::Vector { tiles, from, min_zoom, max_zoom, .. } => { | ||
| (tiles, from, *min_zoom, *max_zoom, Want::Vector, (zoom.floor() as u8).min(*max_zoom)) | ||
|
|
@@ -1318,6 +1328,27 @@ fn area(r: &[(f32, f32)]) -> f32 { | |
|
|
||
| #[cfg(test)] | ||
| pub(crate) mod tests { | ||
| /// On a HiDPI display a raster source takes the next zoom's tiles while the view's fit the most | ||
| /// a source draws, and goes back towards a logical pixel's zoom where they don't (#2390). | ||
| #[test] | ||
| fn sharp_raster_tiles_keep_to_the_tile_budget() { | ||
| use super::{Frame, raster_zoom, template}; | ||
| // A zoom-10 logical pixel (152.9 m), at 2 device pixels a point. | ||
| let logical = 2.0 * template::HALF_WORLD / (256.0 * 1024.0); | ||
| let zoom_of = |w: f64, h: f64, ppp: f32, min: u8| { | ||
| let frame = Frame { left: 0.0, top: h * logical, m_per_px: logical / f64::from(ppp), ppp }; | ||
| raster_zoom(&frame, &rustgis_geom::Extent::new(0.0, 0.0, w * logical, h * logical), 256, min, 22) | ||
| }; | ||
| // A 1500 × 900 point view: 6 × 4 tiles of zoom 10, 12 × 7 of zoom 11. | ||
| assert_eq!(zoom_of(1500.0, 900.0, 1.0, 0), 10); | ||
| assert_eq!(zoom_of(1500.0, 900.0, 2.0, 0), 11); | ||
| // A 6000 × 3400 point one: 47 × 27 of zoom 11 pass 400; zoom 10's fit. | ||
| assert_eq!(zoom_of(6000.0, 3400.0, 2.0, 0), 10); | ||
| // A source drawn from zoom 11 keeps it, its tiles nearest the view's centre, rather than | ||
| // falling back to a zoom it has none of. | ||
| assert_eq!(zoom_of(6000.0, 3400.0, 2.0, 11), 11); | ||
| } | ||
|
|
||
| #[test] | ||
| fn refusal_counts_are_not_substring_duplicates() { | ||
| let mut error = Some("12 tiles left out: refused".to_string()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -766,6 +766,9 @@ pub struct UiState { | |
| /// Whether datum grids a coordinate system needs are downloaded from PROJ's grid server | ||
| /// (#2032 A4): off unless turned on, as it reaches a server. | ||
| pub download_grids: bool, | ||
| /// Whether raster tiles and single pictures are fetched for a HiDPI display's own pixels | ||
| /// (#2390): on unless turned off. | ||
| pub sharp_tiles: bool, | ||
| /// Key combinations the native menu bar keeps for itself (macOS: Quit, Close Window, Hide…): | ||
| /// a shortcut on one would never reach the app, so the shortcut editor refuses them. | ||
| pub reserved_shortcuts: Vec<egui::KeyboardShortcut>, | ||
|
|
@@ -1041,6 +1044,7 @@ impl Default for UiState { | |
| start_with: StartWith::Sample, | ||
| ask_layers: true, | ||
| download_grids: false, | ||
| sharp_tiles: true, | ||
| reserved_shortcuts: Vec::new(), | ||
| choosing: Vec::new(), | ||
| last_project: None, | ||
|
|
@@ -1650,6 +1654,8 @@ impl RustGisApp { | |
| self.ui.ask_layers = self.services.load_pref.as_ref().and_then(|load| load("ask_file_layers")).is_none_or(|v| v != "off"); | ||
| self.ui.download_grids = self.services.load_pref.as_ref().and_then(|load| load("download_datum_grids")).is_some_and(|v| v == "on"); | ||
| rustgis_engine::grids::set_enabled(self.ui.download_grids); | ||
| self.ui.sharp_tiles = self.services.load_pref.as_ref().and_then(|load| load("sharp_tiles")).is_none_or(|v| v != "off"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AGENTS.md "Never silent" (confidence ~30%): an unreadable saved value is quietly read as "on".
The same pattern is used for |
||
| rustgis_engine::render::rustgis_tiles::template::set_sharp(self.ui.sharp_tiles); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| // A stored projection this version can't use means Web Mercator. | ||
| if let Some(code) = self.services.load_pref.as_ref().and_then(|load| load("start_projection")) | ||
| && let Ok(p) = rustgis_engine::map::Projection::from_code(&code) | ||
|
|
@@ -3622,6 +3628,22 @@ impl RustGisApp { | |
| } | ||
| return Ok(json!({"on": self.ui.download_grids})); | ||
| } | ||
| // Whether raster tiles are sharp on a HiDPI display (#2390). | ||
| "ui.sharpTiles" => { | ||
| // Without `on` it toggles; a value that isn't true or false is refused. | ||
| self.ui.sharp_tiles = match p.get("on") { | ||
| None | Some(Value::Null) => !self.ui.sharp_tiles, | ||
| Some(v) => v.as_bool().ok_or("on must be true or false")?, | ||
| }; | ||
| rustgis_engine::render::rustgis_tiles::template::set_sharp(self.ui.sharp_tiles); | ||
| if let Some(save) = &self.services.save_pref { | ||
| save("sharp_tiles", if self.ui.sharp_tiles { "on" } else { "off" }); | ||
| } | ||
| // The map and an open layout page are drawn again with tiles at the new detail. | ||
| self.map.invalidate(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| self.layout.redraw(); | ||
| return Ok(json!({"on": self.ui.sharp_tiles})); | ||
| } | ||
| "ui.recover" => return autosave::recover(self, p), | ||
| "ui.startup" => { | ||
| // `project` (#377) or, as before, `blank`; `project` wins when both are given. | ||
|
|
@@ -9220,6 +9242,31 @@ mod datum_grid_setting_tests { | |
| use rustgis_engine::{Project, Session}; | ||
| use serde_json::json; | ||
|
|
||
| /// Sharp tiles on HiDPI displays (#2390) are on by default, kept as a choice, read at start | ||
| /// and turned off by it; a value that isn't true or false is refused. | ||
| #[test] | ||
| fn sharp_tiles_are_a_kept_choice_on_by_default() { | ||
| let saved = std::sync::Arc::new(std::sync::Mutex::new(Vec::new())); | ||
| let log = saved.clone(); | ||
| let services = crate::Services { | ||
| load_pref: Some(Box::new(|k| (k == "sharp_tiles").then(|| "off".to_string()))), | ||
| save_pref: Some(Box::new(move |k, v| log.lock().unwrap().push((k.to_string(), v.to_string())))), | ||
| ..crate::Services::default() | ||
| }; | ||
| let mut app = RustGisApp::new(Session::new(Project::default())).with_services(services); | ||
| // The setting itself is shared by the tests running beside this one: only the choice is | ||
| // looked at, and it is left on. | ||
| assert!(!app.ui.sharp_tiles, "read at start"); | ||
| assert_eq!(app.run("ui.sharpTiles", json!({"on": true})).unwrap(), json!({"on": true})); | ||
| assert_eq!(saved.lock().unwrap().last().cloned(), Some(("sharp_tiles".to_string(), "on".to_string()))); | ||
| for junk in [json!({"on": "false"}), json!({"on": 0})] { | ||
| assert!(app.run("ui.sharpTiles", junk).is_err()); | ||
| assert!(app.ui.sharp_tiles); | ||
| } | ||
| // Without a choice kept: on. | ||
| assert!(RustGisApp::new(Session::new(Project::default())).ui.sharp_tiles); | ||
| } | ||
|
|
||
| /// Downloading datum grids is off unless turned on (it reaches a server), and the choice is | ||
| /// kept as a preference. Turned on only by `rustgis-engine`'s own test, which reaches no | ||
| /// network: here, where other tests run beside it, it stays off. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.