Skip to content
100 changes: 100 additions & 0 deletions crates/render/src/cancel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! Stopping a frame no longer wanted (#1750). The desktop app draws a settled flat map's frame on a
//! worker; when the map moves again before it is done, the frame is out of date. The worker
//! installs a flag on its thread ([`install`]) and sets it to stop: the renderer looks at it between
//! layers, between a tilted view's bands, before each chunk of a streamed layer and every
//! [`EVERY`] features or [`EVERY_VERTICES`] vertices of a layer, and a frame stopped that way comes
//! back as `None`, without its tiles, labels or effects worked out.
//! With none installed (an export, the CLI, the browser) frames always finish. The flag is the
//! thread's own: threads the renderer starts for a part of the frame (a raster's rows) don't look
//! at it, and finish their part. A long step without a look (projecting or thinning a layer's
//! geometry for the first time, a raster, a single feature's path) runs to its end.

use std::cell::RefCell;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

/// How many features of a layer are drawn between looks at the flag.
pub(crate) const EVERY: usize = 2048;

/// How many vertices of a layer are drawn between looks at the flag, so a layer of a few heavy
/// features (a detailed coastline) stops part-way too.
pub(crate) const EVERY_VERTICES: usize = 65_536;

thread_local! {
static FLAG: RefCell<Option<Arc<AtomicBool>>> = const { RefCell::new(None) };
}

/// Makes `flag` stop the frames drawn on this thread until the guard is dropped, which puts back
/// the one before.
pub fn install(flag: Arc<AtomicBool>) -> Installed {
Installed(FLAG.with(|f| f.replace(Some(flag))))
}

/// Puts back the flag before when dropped (see [`install`]).
pub struct Installed(Option<Arc<AtomicBool>>);

impl Drop for Installed {
fn drop(&mut self) {
let before = self.0.take();
let _ = FLAG.try_with(|f| f.replace(before));
}
}

/// Whether the frame being drawn on this thread was asked to stop.
pub(crate) fn requested() -> bool {
// Never during the thread's teardown: no frame is drawn that late.
FLAG.try_with(|f| f.borrow().as_ref().is_some_and(|flag| flag.load(Ordering::Relaxed))).unwrap_or(false)
}

#[cfg(test)]
thread_local! {
/// Features drawn on this test thread, to see a stopped frame stop drawing.
pub(crate) static DRAWN: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn a_flag_stops_frames_on_its_thread_only_while_installed() {
let flag = Arc::new(AtomicBool::new(false));
assert!(!requested());
{
let _guard = install(flag.clone());
assert!(!requested());
flag.store(true, Ordering::Relaxed);
assert!(requested());
// Another thread draws on.
assert!(!std::thread::spawn(requested).join().unwrap_or(true));
}
assert!(!requested());
}

#[test]
fn a_stopped_frame_is_no_frame() {
let mut map = rustgis_map::Map::new("m");
if let Some(d) = crate::world_dataset("Countries", crate::sample_data::COUNTRIES) {
map.add_dataset(d);
}
let view = crate::View { width: 64.0, height: 48.0, pixels_per_point: 1.0, camera: rustgis_map::Camera::world(), sky: false };
let flag = Arc::new(AtomicBool::new(false));
let _guard = install(flag.clone());
DRAWN.set(0);
assert!(crate::render(&mut map, &view).is_some());
assert!(DRAWN.get() > 100, "{} countries drawn", DRAWN.get());
// Stopped, it draws no feature at all, not just hands back nothing.
flag.store(true, Ordering::Relaxed);
DRAWN.set(0);
assert!(crate::render(&mut map, &view).is_none());
assert_eq!(DRAWN.get(), 0);
// So does a tilted one, drawn band by band.
let tilted = crate::View { camera: rustgis_map::Camera { pitch: 60.0, ..rustgis_map::Camera::world() }, ..view };
DRAWN.set(0);
assert!(crate::render(&mut map, &tilted).is_none());
assert_eq!(DRAWN.get(), 0);
flag.store(false, Ordering::Relaxed);
assert!(crate::render(&mut map, &tilted).is_some());
assert!(DRAWN.get() > 0);
}
}
64 changes: 64 additions & 0 deletions crates/render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! the labels too, for export and the command line.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]

pub mod cancel;
mod canvas;
mod diagrams;
mod effects;
Expand Down Expand Up @@ -2090,6 +2091,10 @@ fn render_with_backend(map: &mut Map, view: &View, full_res_rasters: bool, backe
fn render_any(map: &mut Map, view: &View, full_res_rasters: bool, backend: Backend, record: bool) -> Option<RenderOutput> {
let revisions = archive_revisions([&*map]);
let output = render_pass(map, view, full_res_rasters, backend, record);
// A frame stopped part-way (see `cancel`) is no frame.
if cancel::requested() {
return None;
}
revisions.current([&*map]).then_some(output).flatten()
}

Expand Down Expand Up @@ -2462,6 +2467,11 @@ fn render_tilted(map: &mut Map, view: &View, full_res_rasters: bool, sources_rea
let (mut attribution, mut tiles_pending, mut tile_errors) = (Vec::<String>::new(), false, Vec::<String>::new());
let (mut partial, mut pending_in_place) = (Vec::<u32>::new(), Vec::<u32>::new());
for (i, b) in bands_of.into_iter().enumerate() {
// A stopped frame (see `cancel`) draws no further band, nor its drape or warp.
if cancel::requested() {
Comment thread
giswqs marked this conversation as resolved.
Resolved
map.label_lock = lock;
return None;
}
let v = band_view(view, &b);
let Some(RenderOutput::Cpu(r)) =
render_frame(map, &v, full_res_rasters, Backend::Cpu, Labels::Tilted(view, &mut candidates), false)
Expand Down Expand Up @@ -2517,6 +2527,10 @@ fn render_tilted(map: &mut Map, view: &View, full_res_rasters: bool, sources_rea
}
}
map.label_lock = lock;
// A frame stopped during its last band goes no further (see `cancel`).
if cancel::requested() {
return None;
}
match &ground {
Some(g) => {
drape_terrain(pm.pixels(), view, map, &kept, g);
Expand Down Expand Up @@ -2741,10 +2755,19 @@ fn gpu_turned(map: &mut Map, view: &View, full_res_rasters: bool, sources_ready:
let mut plain = true;
let last = bands.len().saturating_sub(1);
for (i, b) in bands.iter().enumerate() {
// A stopped frame (see `cancel`) draws no further band, nor falls back to the CPU.
if cancel::requested() {
map.label_lock = lock;
return None;
}
let v = band_view(view, b);
let Some(RenderOutput::Gpu(scene)) =
render_frame(map, &v, full_res_rasters, Backend::Gpu, Labels::Tilted(view, &mut candidates), false)
else {
if cancel::requested() {
Comment thread
giswqs marked this conversation as resolved.
Resolved
map.label_lock = lock;
return None;
}
plain = false;
break;
};
Expand Down Expand Up @@ -2800,6 +2823,10 @@ fn gpu_turned(map: &mut Map, view: &View, full_res_rasters: bool, sources_ready:
cmds.extend(scene.cmds);
}
map.label_lock = lock;
// A frame stopped during its last band goes no further (see `cancel`).
if cancel::requested() {
return None;
}
if !plain {
return None;
}
Expand Down Expand Up @@ -3926,6 +3953,10 @@ fn render_frame(
// The layer drawn last, whose tile layers go over it before the next.
let mut below: Option<u32> = None;
for li in order {
// A frame no longer wanted stops between layers (see `cancel`).
if cancel::requested() {
Comment thread
giswqs marked this conversation as resolved.
Resolved
break;
}
Comment thread
giswqs marked this conversation as resolved.
Resolved
if let Some(id) = below.take() {
draw_tiles!(Some(id));
}
Expand Down Expand Up @@ -4101,6 +4132,10 @@ fn render_frame(
// polygons.
let mut extruded_charts: Option<Canvas> = None;
let mut draw_chunk = |layer: &mut rustgis_map::Layer| -> rustgis_data::Result<()> {
// A stopped frame draws no more of a layer or a streamed chunk (see `cancel`).
if cancel::requested() {
return Ok(());
}
// The definition query and the display filters that apply at this scale (a feature draws
// only if it matches all of them).
let mut rows = layer.drawn_rows(scale);
Expand Down Expand Up @@ -4316,8 +4351,20 @@ fn render_frame(
// Polygons with stacked fills (patterns) draw them in place of their single fill.
let has_fills = layer.has_fills();
let mut stacked: Vec<(&[Vec<Coord>], Symbol, &[LineStroke])> = Vec::new();
// Vertices drawn since the stop was last looked at (see `cancel`).
let mut since_look = 0usize;
for (row, g) in drawn.iter().enumerate() {
// And every few thousand features, or vertices, of a large layer.
if (row % cancel::EVERY == 0 && row > 0 || since_look >= cancel::EVERY_VERTICES) && cancel::requested() {
Comment thread
giswqs marked this conversation as resolved.
Resolved
break;
Comment thread
giswqs marked this conversation as resolved.
Resolved
}
if since_look >= cancel::EVERY_VERTICES {
since_look = 0;
}
#[cfg(test)]
cancel::DRAWN.set(cancel::DRAWN.get() + 1);
let Some(g) = g else { continue };
since_look += rustgis_map::lod::vertices(g);
let e = extents.get(row).copied().unwrap_or_else(|| g.extent());
// A point's marks, shifted from it, may show while the point is off the view.
let marked_in = || {
Expand Down Expand Up @@ -4514,6 +4561,10 @@ fn render_frame(
} else {
Vec::new()
};
// A stopped frame places no labels for the rest of the layer (see `cancel`).
if cancel::requested() {
return Ok(());
}
if label_fields.iter().any(Option::is_some) {
let label_factor = layer.size_factor(label_scale);
// Each class's font, where it is installed. An export (`full_res_rasters`: see
Expand Down Expand Up @@ -4917,6 +4968,10 @@ fn render_frame(
// All coordinates are read here: filtering only to the viewport can change labels,
// shadows and screen-space effects whose geometry lies beyond its boundary.
let result = visit_complete(source.as_ref(), &mut |features| {
// A frame no longer wanted reads no further (see `cancel`).
if cancel::requested() {
return Err(rustgis_data::DataError::Cancelled);
}
chunk.clear_cache();
chunk.selection =
features.iter().enumerate().filter_map(|(r, f)| selected_order.contains_key(&f.id).then_some(r)).collect();
Expand All @@ -4928,6 +4983,10 @@ fn render_frame(
Ok(())
});
if let Err(e) = result {
// Stopped, not failed: the source is fine.
if cancel::requested() {
return None;
}
if let Some(s) = &mut layer.source {
s.error = Some(e.to_string());
}
Expand Down Expand Up @@ -4970,6 +5029,11 @@ fn render_frame(
cpu_dirty = true;
}
}
// A frame stopped part-way (see `cancel`) is no frame: its tiles, labels and effects aren't
// worth working out.
if cancel::requested() {
return None;
}
if let Some(id) = below {
draw_tiles!(Some(id));
}
Expand Down
72 changes: 72 additions & 0 deletions crates/render/src/stream_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1436,3 +1436,75 @@ fn streamed_extrusions_keep_their_charts_on_top() {
assert!(n * 10 >= whole * 9, "chunks of {size}: {n} chart px, {whole} resident");
}
}

/// A frame stopped while a streamed layer is read reads no further chunk (#1750), and the stop
/// isn't taken for a failure of the source.
#[test]
fn a_stopped_frame_reads_no_further_chunk_and_leaves_its_source_unfailed() {
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
/// Chunks that set the frame's stop flag once the first is drawn.
struct StopAfterFirst {
chunks: Chunks,
stop: Arc<AtomicBool>,
}
impl FeatureSource for StopAfterFirst {
fn info(&self) -> &SourceInfo {
self.chunks.info()
}
fn read(&self, r: &Request) -> rustgis_data::Result<Batch> {
self.chunks.read(r)
}
fn streams_chunks(&self) -> bool {
true
}
fn visit_chunks(
&self,
e: Option<Extent>,
n: usize,
visit: &mut dyn FnMut(Vec<Feature>) -> rustgis_data::Result<()>,
) -> rustgis_data::Result<Batch> {
self.chunks.visit_chunks(e, n, &mut |chunk| {
let r = visit(chunk);
self.stop.store(true, Ordering::Relaxed);
r
})
}
}
let mut m = Map::new("m");
if let Some(d) = world_dataset("Countries", sample_data::COUNTRIES) {
m.add_dataset(d);
}
let ds = m.layers[0].dataset.clone();
assert!(ds.features.len() > 100);
let info = SourceInfo {
name: ds.name.clone(),
format: "test",
geometry_type: ds.geometry_type,
fields: ds.fields.clone(),
extent: ds.extent(),
count: Some(ds.features.len() as u64),
crs: "EPSG:4326".into(),
indexed: true,
notes: Vec::new(),
zm_dropped: false,
};
let stop = Arc::new(AtomicBool::new(false));
let mut layer = m.layers[0].clone();
layer.dataset.features = ds.features.iter().take(1).cloned().collect::<Vec<_>>().into();
layer.source = Some(SourceLayer::from_path(
"test.shp",
None,
Box::new(StopAfterFirst { chunks: Chunks { info, ds, size: 20 }, stop: stop.clone() }),
));
m.layers[0] = layer;
let view = View { width: 64.0, height: 48.0, pixels_per_point: 1.0, camera: Camera::world(), sky: false };
settle(&mut m, &view);
stop.store(false, Ordering::Relaxed);
let _guard = crate::cancel::install(stop.clone());
crate::cancel::DRAWN.set(0);
assert!(render(&mut m, &view).is_none(), "a stopped frame is no frame");
let drawn = crate::cancel::DRAWN.get();
assert!(drawn <= 20, "{drawn} features drawn: the read went on past the first chunk");
assert!(m.layers[0].source.as_ref().is_some_and(|s| s.error.is_none()), "a stop isn't a source error");
}
Loading
Loading