mln-rs-renderer 0.0.13

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//! A tile's transitions. [`TileMut`] is the one place that settles a tile,
//! submits its jobs, replaces what it shows, and reports its actions: a
//! `StartParse` when it becomes pending, an `EndParse` when contents install
//! (after a `StartParse` if none was reported), and a `Cancelled` when a
//! pending tile is reset or let go of.

use std::rc::Rc;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use mln_storage::response::Reason;
use mln_storage::{Request, Response};
use mln_style::custom_tile_loader::SubscriberKey;
use mln_style::sources::geojson::GeoJSONData;
use mln_util::error::Error;
use mln_util::hash::HashSet;
use mln_util::logging::{Event, Log};
use mln_util::subdivision_granularity::SubdivisionGranularitySetting;
use mln_vec_map::VecSet;

use crate::buckets::hillshade_bucket::HillshadeBucket;
use crate::buckets::raster_bucket::RasterBucket;
use crate::layout::tile_layout::{Dependencies, LayoutParams};
use crate::render_orchestrator::drop_on_workers;
use crate::tile::contents::{Contents, GeometryContents, release};
use crate::tile::inbox::{SourceData, TileEvent};
use crate::tile::job::{DataInput, DataKey, Job, JobId, JobResult, LayoutJob, LayoutOutput, Run, SourceStyle};
use crate::tile::loading::{self, CustomLoad, CustomLoader, DataOrigin, Fetch, Method, OriginLoad, TilesetAnswered};
use crate::tile::pipeline::{
    DecodeWork, DependencyWait, InFlight, Layout, LayoutData, LayoutWork, Pipeline, Resume, Running, Waiting,
    outdated_by,
};
use crate::tile::store::{SourceInfo, StoreContext};
use crate::tile::tile::{Shown, Tile, TileInput, TileNecessity, TileOperation, TileUpdateParameters};
use crate::tile::tile_workers::{InlineWorkers, Lane, ReplyBody};

/// A tile, its source and the store, borrowed for a transition.
pub(crate) struct TileMut<'a> {
    source: &'a mut SourceInfo,
    tile: &'a mut Tile,
    store: &'a mut StoreContext,
    /// Whether the tile was pending when its parse events last said so.
    reported: bool,
    /// The tile showed new contents.
    pub(super) installed: bool,
}

impl<'a> TileMut<'a> {
    /// Runs `f` on `tile`, then reports `StartParse` if `f` left it pending.
    /// Returns `f`'s result and whether the tile showed new contents.
    pub(super) fn with<R>(
        source: &'a mut SourceInfo,
        tile: &'a mut Tile,
        store: &'a mut StoreContext,
        f: impl FnOnce(&mut TileMut<'a>) -> R,
    ) -> (R, bool) {
        let reported = tile.is_pending();
        let mut tile = TileMut { source, tile, store, reported, installed: false };
        let result = f(&mut tile);
        tile.report();
        (result, tile.installed)
    }

    pub(crate) fn tile(&self) -> &Tile {
        self.tile
    }

    #[cfg(test)]
    pub(crate) fn parts(&mut self) -> (&mut SourceInfo, &mut Tile) {
        (self.source, self.tile)
    }

    /// Starts a new tile's load; a custom source's waits until the tile is
    /// required.
    pub(crate) fn start_load(&mut self) {
        match &self.source.origin {
            Some(DataOrigin::Tileset { file_source: None, .. }) => self.fail(Error::native("Can't load tile.")),
            // The first request only looks in the cache, even for a required
            // tile, so that it can go on if the tile becomes optional.
            Some(DataOrigin::Tileset { file_source: Some(file_source), .. }) => {
                if file_source.supports_cache_only() {
                    self.request(Method::Cache);
                }
            }
            Some(DataOrigin::GeoJson(data)) => {
                let data = Arc::clone(data);
                self.load_geojson(&data, false);
            }
            Some(DataOrigin::Custom { .. }) | None => {}
        }
    }

    /// Starts a new tile from `copy`, of the tile in another world copy: it
    /// shows the copy's contents at once. With the copy's load, it takes
    /// that as a cache's answer, and lays out the data for contents of its
    /// own; otherwise it loads as any new tile.
    pub(crate) fn start_as_copy(&mut self, copy: WorldCopy) {
        let WorldCopy { shown, load } = copy;
        self.tile.shown = Some(shown);
        self.installed = true;
        self.store.changed();
        let taken = match (load, &mut self.tile.load.origin) {
            (Some((answered, data)), OriginLoad::Tileset(tileset)) => tileset.take_answered(answered).then_some(data),
            _ => None,
        };
        let Some(data) = taken else {
            self.start_load();
            return;
        };
        self.tile.settled = true;
        if let Some(data) = data {
            self.change_layout(|layout| layout.data = Some(data));
        } else {
            // A raster or raster-dem tile shows all it will: its parse
            // starts and ends at once.
            self.action(TileOperation::StartParse);
            self.action(TileOperation::EndParse);
        }
    }

    /// A required tile loads from the network or its custom loader; an
    /// optional one stops, unless it is a custom tile that is renderable.
    pub(crate) fn set_necessity(&mut self, necessity: TileNecessity) {
        let unchanged = std::mem::replace(&mut self.tile.load.necessity, necessity) == necessity;
        let required = necessity == TileNecessity::Required;
        let renderable = self.tile.is_renderable();
        match &mut self.tile.load.origin {
            OriginLoad::Tileset(tileset) if !unchanged => {
                if required {
                    if tileset.is_idle() {
                        self.request(Method::Network);
                    }
                } else if matches!(tileset.fetch, Fetch::Network { .. }) {
                    // Only a network-only request is aborted.
                    tileset.fetch = Fetch::Idle;
                }
            }
            // Fetching a custom tile's data is assumed to be expensive: only
            // required tiles fetch.
            OriginLoad::Custom(custom) if !unchanged || !matches!(custom, CustomLoad::Subscribed(_)) => {
                match (required, *custom) {
                    (true, CustomLoad::Subscribed(key)) => self.fetch(key),
                    (true, CustomLoad::Stale(_) | CustomLoad::Unsubscribed) => {
                        if matches!(self.source.origin, Some(DataOrigin::Custom { .. })) {
                            let key = self.source.next();
                            *custom = CustomLoad::Subscribed(key);
                            self.fetch(key);
                        }
                    }
                    (false, CustomLoad::Subscribed(key) | CustomLoad::Stale(key)) if !renderable => {
                        if let Some((loader, _)) = self.custom_loader() {
                            loader.cancel(self.tile.id, key);
                        }
                    }
                    (false, _) => {}
                }
            }
            OriginLoad::Tileset(_) | OriginLoad::Custom(_) | OriginLoad::GeoJson { .. } => {}
        }
    }

    /// A network request under way is made again with the new parameters.
    pub(crate) fn set_update_parameters(&mut self, parameters: TileUpdateParameters) {
        let OriginLoad::Tileset(tileset) = &mut self.tile.load.origin else {
            return;
        };
        if std::mem::replace(&mut tileset.update_parameters, parameters) != parameters
            && matches!(tileset.fetch, Fetch::Network { .. })
        {
            tileset.fetch = Fetch::Idle;
            self.request(Method::Network);
        }
    }

    /// Requests a required tile whose last response was an error again.
    pub(crate) fn retry_failed_load(&mut self) {
        if let OriginLoad::Tileset(tileset) = &mut self.tile.load.origin
            && self.tile.load.necessity == TileNecessity::Required
            && matches!(tileset.fetch, Fetch::Failed)
        {
            tileset.fetch = Fetch::Idle;
            self.tile.settled = false;
            self.request(Method::Network);
        }
    }

    /// Applies the response to request `load`, if the tile waits for it.
    pub(crate) fn apply_response(&mut self, load: u64, res: &Response) {
        let OriginLoad::Tileset(tileset) = &mut self.tile.load.origin else {
            return;
        };
        let method = match tileset.fetch {
            Fetch::Cache { load: current, .. } if current == load => Method::Cache,
            Fetch::Network { load: current, .. } if current == load => Method::Network,
            Fetch::Idle | Fetch::Failed | Fetch::Cache { .. } | Fetch::Network { .. } => return,
        };
        tileset.fetch = Fetch::Idle;
        if method == Method::Network {
            self.take_response(method, res);
            return;
        }
        tileset.cache_answered = true;
        // A cache-only request that found nothing isn't an error. It can
        // still bring expired data that Cache-Control forbids using, which
        // the network request revalidates.
        let not_found = res.error.as_ref().is_some_and(|e| e.reason == Reason::NotFound);
        if not_found {
            let resource = &mut tileset.resource;
            resource.prior_modified = res.modified;
            resource.prior_expires = res.expires;
            resource.prior_etag = res.etag.clone();
            resource.prior_data = res.data.clone();
        }
        self.store.changed();
        if !not_found {
            self.take_response(method, res);
        }
        if self.tile.load.necessity == TileNecessity::Required
            && matches!(&self.tile.load.origin, OriginLoad::Tileset(tileset) if tileset.is_idle())
        {
            self.request(Method::Network);
        }
    }

    /// Applies what a GeoJSON or custom source gave load `load`, if current.
    pub(crate) fn apply_source_data(&mut self, load: u64, data: SourceData) {
        if !self.tile.load.accepts(load) {
            return;
        }
        match data {
            SourceData::Data(data) => self.take_input(TileInput::Geometry { data, missing: false }),
            SourceData::Error(error) => self.fail(error),
            SourceData::Stale => {
                if let OriginLoad::Custom(custom) = &mut self.tile.load.origin {
                    if let CustomLoad::Subscribed(key) = *custom {
                        *custom = CustomLoad::Stale(key);
                    }
                    self.store.changed();
                }
            }
        }
    }

    /// With `relayout`, the tile first drops its data and style.
    pub(crate) fn load_geojson(&mut self, data: &Arc<dyn GeoJSONData>, relayout: bool) {
        if relayout {
            self.reset();
        }
        let load = self.source.next();
        if let OriginLoad::GeoJson { load: current } = &mut self.tile.load.origin {
            *current = Some(load);
        }
        let key = self.source.tile_key(self.tile);
        if let Some(data) = loading::get_geojson_tile(
            data,
            key,
            load,
            &self.store.inbox,
            (!self.source.synchronous).then(|| self.store.pool.pool()),
        ) {
            self.take_input(TileInput::Geometry { data: Some(data), missing: false });
        }
    }

    /// Takes in what a load delivered. A raster tile without an image shows
    /// nothing at once, renderable unless `missing`.
    pub(crate) fn take_input(&mut self, input: TileInput) {
        match input {
            TileInput::Geometry { data, missing } => {
                let key = DataKey { source: self.source.key, tile: self.tile.id.canonical, load: self.source.next() };
                self.change_layout(|layout| layout.data = Some(LayoutData { key, data, missing }));
            }
            TileInput::Image { data, missing } => {
                let Pipeline::Decode(decode) = &mut self.tile.pipeline else {
                    return;
                };
                let has_image = data.is_some();
                decode.generation = self.source.next();
                decode.work = match std::mem::replace(&mut decode.work, DecodeWork::Current) {
                    // A decode in flight goes on, stale, until it replies.
                    DecodeWork::Running { job, .. } => {
                        job.superseded.store(true, Ordering::Relaxed);
                        DecodeWork::Running { job, image: data }
                    }
                    DecodeWork::Current | DecodeWork::Queued(_) => data.map_or(DecodeWork::Current, DecodeWork::Queued),
                };
                if !has_image {
                    self.install((!missing).then_some(Contents::Empty));
                }
            }
        }
        // A request that follows, such as for the network after the cache's
        // data, comes after the parse it starts.
        self.report();
    }

    /// The tile couldn't load or lay out: it keeps what it shows.
    pub(crate) fn fail(&mut self, error: Error) {
        self.tile.settled = true;
        self.action(TileOperation::Error);
        self.store.error(&self.source.id, self.tile.id, error);
    }

    pub(crate) fn set_style(
        &mut self,
        style: &Arc<SourceStyle>,
        pixel_ratio: f32,
        granularity: SubdivisionGranularitySetting,
    ) {
        self.change_layout(|layout| {
            layout.style = Some(Arc::clone(style));
            layout.params.pixel_ratio = pixel_ratio;
            layout.params.subdivision_granularity = granularity;
        });
    }

    pub(crate) fn set_show_collision_boxes(&mut self, show: bool) {
        if self.tile.pipeline.layout().is_some_and(|layout| layout.params.show_collision_boxes != show) {
            self.change_layout(|layout| layout.params.show_collision_boxes = show);
        }
    }

    /// Lays the tile out again if the images in `changed` touch what it shows
    /// or its work. A first try's reply tells for itself.
    pub(crate) fn images_changed(&mut self, changed: &HashSet<String>, available: &VecSet<String>) {
        let names = |deps: &Dependencies| deps.images.keys().any(|id| changed.contains(id));
        let shown = self.tile.contents().and_then(Contents::geometry).is_some_and(|contents| names(&contents.deps));
        let work = self.tile.pipeline.layout().is_some_and(|layout| match &layout.work {
            LayoutWork::Running(Running { known: Some(known), .. }) | LayoutWork::Ready(Resume { known, .. }) => {
                names(known)
            }
            LayoutWork::Waiting(Waiting { needs, .. }) => {
                needs.unavailable_images.iter().any(|id| changed.contains(id) && available.contains(id))
                    || needs.images.keys().any(|id| changed.contains(id) && !available.contains(id))
            }
            LayoutWork::Running(_) | LayoutWork::Current | LayoutWork::Outdated => false,
        });
        if shown || work {
            self.relayout();
        }
    }

    /// Lays the tile out again if it lacked glyphs of a range that failed.
    pub(crate) fn retry_failed_glyphs(&mut self) {
        let running = self.tile.pipeline.layout().is_some_and(|layout| match &layout.work {
            LayoutWork::Running(Running { answer, .. }) | LayoutWork::Ready(Resume { answer, .. }) => {
                answer.glyphs_failed
            }
            LayoutWork::Current | LayoutWork::Outdated | LayoutWork::Waiting(_) => false,
        });
        let shown =
            self.tile.contents().and_then(Contents::geometry).is_some_and(|contents| contents.answer.glyphs_failed);
        if running || shown {
            self.relayout();
        }
    }

    /// Resolves the dependencies the tile waits for, and starts its next job.
    pub(crate) fn drive(&mut self) {
        if let Pipeline::Layout(layout) = &mut self.tile.pipeline
            && let LayoutWork::Waiting(waiting) = &mut layout.work
            && let Some(resume) = self.store.resolve(waiting)
        {
            layout.work = LayoutWork::Ready(resume);
        }
        if self.source.synchronous {
            self.run_inline();
        } else if let Some((lane, run)) = self.next_job(false) {
            self.store.jobs.submit(lane, run, &self.store.inbox);
        }
    }

    pub(crate) fn apply_reply(&mut self, lane: Lane, id: JobId, body: ReplyBody) {
        if id.generation != self.tile.pipeline.generation() {
            // The work goes on with the current generation.
            match &mut self.tile.pipeline {
                Pipeline::Layout(layout) => layout.work = LayoutWork::Outdated,
                Pipeline::Decode(decode) => decode.requeue(),
            }
            drop_on_workers(&self.store.pool, body);
        } else if let Some(result) = self.store.jobs.decode(lane, body) {
            self.apply_result(id, result);
        }
        // Else a reply of a worker lost since: the loss, which comes next,
        // runs the job again.
    }

    /// The worker of `lane` is gone: the work it held starts again.
    pub(crate) fn lane_lost(&mut self, lane: Lane) {
        if self.tile.pipeline.lane() != Some(lane) {
            return;
        }
        let generation = self.source.next();
        match &mut self.tile.pipeline {
            Pipeline::Layout(layout) => {
                layout.bump(generation);
                layout.work = LayoutWork::Outdated;
            }
            Pipeline::Decode(decode) => {
                decode.generation = generation;
                decode.requeue();
            }
        }
    }

    #[cfg(test)]
    pub(crate) fn apply_decode_now(&mut self, result: JobResult) {
        let id = JobId { tile: self.source.tile_key(self.tile), generation: self.tile.pipeline.generation() };
        assert!(matches!(self.tile.pipeline, Pipeline::Decode(_)), "a raster or raster-dem tile");
        self.apply_result(id, result);
    }

    fn action(&mut self, op: TileOperation) {
        self.store.action(&self.source.id, self.tile.id, op);
    }

    /// Emits `StartParse` if the tile became pending since it last said so.
    fn report(&mut self) {
        let pending = self.tile.is_pending();
        if pending && !self.reported {
            self.action(TileOperation::StartParse);
        }
        self.reported = pending;
    }

    /// Shows `contents`, with the source's feature state applied.
    fn install(&mut self, contents: Option<Contents>) {
        let shown = contents.map(|contents| {
            let mut version = 0;
            if let Some(geometry) = contents.geometry() {
                geometry.set_feature_state(self.source.feature_state.current_states());
                version = self.source.feature_state.version();
            }
            Shown::new(contents, version)
        });
        if let Some(old) = std::mem::replace(&mut self.tile.shown, shown) {
            release(old.contents, &self.store.pool);
        }
        self.tile.settled = true;
        // A parse nothing reported, such as of a raster tile without an
        // image, starts and ends at once.
        if !self.reported {
            self.action(TileOperation::StartParse);
        }
        self.action(TileOperation::EndParse);
        self.reported = self.tile.is_pending();
        self.installed = true;
        self.store.changed();
    }

    /// Drops the tile's data and style, and leaves it waiting for new ones.
    fn reset(&mut self) {
        if self.tile.is_pending() {
            self.action(TileOperation::Cancelled);
        }
        self.reported = false;
        self.tile.settled = false;
        let generation = self.source.next();
        if let Pipeline::Layout(layout) = &mut self.tile.pipeline {
            layout.data = None;
            layout.style = None;
            layout.bump(generation);
        }
    }

    /// Changes a geometry tile's layout through `f`, at a new generation.
    fn change_layout(&mut self, f: impl FnOnce(&mut Layout)) {
        if let Pipeline::Layout(layout) = &mut self.tile.pipeline {
            f(layout);
            layout.bump(self.source.next());
            if self.source.synchronous {
                self.run_inline();
            }
        }
    }

    /// Lays the tile out again, without the answer of the layout it shows.
    fn relayout(&mut self) {
        if let Some(shown) = &mut self.tile.shown {
            shown.answer_outdated = true;
        }
        self.change_layout(|_| {});
    }

    /// Runs the tile's jobs here until it shows contents or waits.
    fn run_inline(&mut self) {
        loop {
            self.report();
            let Some((_, run)) = self.next_job(true) else {
                break;
            };
            let reply = InlineWorkers::run(run);
            self.apply_result(reply.id, reply.result);
        }
    }

    /// Puts the tile's next job in flight, if it has one and a lane has room.
    fn next_job(&mut self, inline: bool) -> Option<(Lane, Run)> {
        let tile = self.source.tile_key(self.tile);
        let superseded = Arc::new(AtomicBool::new(false));
        let in_flight = |id, lane| InFlight { id, lane, superseded: Arc::clone(&superseded) };
        let (lane, id, job) = match &mut self.tile.pipeline {
            Pipeline::Layout(layout) => {
                let layout: &mut Layout = layout;
                let (Some(data), Some(style)) = (&layout.data, &layout.style) else {
                    return None;
                };
                if !matches!(layout.work, LayoutWork::Outdated | LayoutWork::Ready(_)) {
                    return None;
                }
                let (lane, cached) =
                    if inline { (Lane(0), false) } else { self.store.jobs.place(data.key, data.data.is_none())? };
                let shown = self.tile.shown.as_ref();
                let (known, answer, images) = match std::mem::replace(&mut layout.work, LayoutWork::Outdated) {
                    LayoutWork::Ready(Resume { known, answer, images, .. }) => (Some(known), answer, images),
                    // A first try starts with the answer the tile shows,
                    // unless it is outdated.
                    LayoutWork::Current | LayoutWork::Outdated | LayoutWork::Running(_) | LayoutWork::Waiting(_) => {
                        let answer = shown
                            .filter(|shown| !shown.answer_outdated)
                            .and_then(|shown| shown.contents.geometry())
                            .map(|contents| contents.answer.clone())
                            .unwrap_or_default();
                        (None, answer, Arc::clone(self.store.images.available_images()))
                    }
                };
                let input = if cached {
                    DataInput::Cached(data.key)
                } else {
                    DataInput::Full { key: data.key, data: data.data.clone() }
                };
                let job = Job::Layout(LayoutJob {
                    style: Arc::clone(style),
                    available_images: Arc::clone(&images),
                    data: input,
                    // A world copy's contents have none of the layers that
                    // a first load fades in.
                    params: LayoutParams { first_load: shown.is_none_or(|shown| shown.world_copy), ..layout.params },
                    resolved: Arc::clone(&answer.resolved),
                });
                let id = JobId { tile, generation: layout.generation };
                layout.work = LayoutWork::Running(Running { job: in_flight(id, lane), known, answer, images });
                (lane, id, job)
            }
            Pipeline::Decode(decode) => {
                let DecodeWork::Queued(image) = &decode.work else {
                    return None;
                };
                let lane = if inline { Lane(0) } else { self.store.jobs.lane()? };
                let image = Arc::clone(image);
                let data = Arc::clone(&image);
                let job = match decode.dem {
                    Some(encoding) => Job::Dem { data, encoding },
                    None => Job::Raster { data },
                };
                let id = JobId { tile, generation: decode.generation };
                decode.work = DecodeWork::Running { job: in_flight(id, lane), image: Some(image) };
                (lane, id, job)
            }
        };
        Some((lane, Run { id, job, superseded }))
    }

    /// Applies `result`, the reply to job `id` of the current generation.
    fn apply_result(&mut self, id: JobId, result: JobResult) {
        debug_assert_eq!(id.generation, self.tile.pipeline.generation(), "a reply of the current generation");
        let layout = match &mut self.tile.pipeline {
            Pipeline::Layout(layout) => layout,
            Pipeline::Decode(decode) => {
                decode.work = DecodeWork::Current;
                return match result {
                    JobResult::Raster(image) => {
                        self.install(Some(Contents::Raster(Arc::new(RasterBucket::new(image)))));
                    }
                    JobResult::Dem(dem) => {
                        self.install(Some(Contents::Dem(Arc::new(HillshadeBucket::with_dem_data(dem)))));
                    }
                    JobResult::Failed(error) => self.fail(error),
                    JobResult::Laid(_) | JobResult::NeedsDeps(_) | JobResult::NeedsData => {
                        self.fail(Error::native("a raster tile's decode answered as a layout"));
                    }
                };
            }
        };
        let Running { job, known, answer, images } = match std::mem::replace(&mut layout.work, LayoutWork::Outdated) {
            LayoutWork::Running(running) => running,
            work => {
                layout.work = work;
                return;
            }
        };
        let available = self.store.images.available_images();
        if !Arc::ptr_eq(&images, available) && outdated_by(&result, &images, available) {
            // The reply branched on an image that came or went since: the
            // tile lays out again, without the answer it shows.
            drop_on_workers(&self.store.pool, result);
            layout.bump(self.source.next());
            if let Some(shown) = &mut self.tile.shown {
                shown.answer_outdated = true;
            }
            return;
        }
        match result {
            JobResult::Laid(LayoutOutput { mut result, deps }) => {
                layout.work = LayoutWork::Current;
                // A first try's answer may be the layout before's, which can
                // hold more than this one uses.
                let answer = if known.is_none() {
                    answer.trimmed(&deps, &mut self.store.atlas).unwrap_or(answer)
                } else {
                    answer
                };
                let (data, missing) =
                    layout.data.as_ref().map_or((None, false), |data| (data.data.clone(), data.missing));
                // The index crossed without its data, or with the tile's.
                if let Some(feature_index) = Arc::get_mut(&mut result.feature_index) {
                    feature_index.set_data(data);
                }
                self.install(Some(Contents::Geometry(GeometryContents::new(result, missing, answer, deps))));
            }
            JobResult::NeedsDeps(needs) if known.is_none() => {
                let wait = DependencyWait::default();
                layout.work = LayoutWork::Waiting(Waiting { lane: job.lane, needs, wait, images });
            }
            JobResult::NeedsData => {
                // A resume keeps its answer and generation; a first try runs
                // again with the images as they are then.
                layout.work = match known {
                    Some(known) => LayoutWork::Ready(Resume { lane: None, known, answer, images }),
                    None => LayoutWork::Outdated,
                };
                if let Some(data) = &layout.data {
                    self.store.jobs.forget(data.key);
                }
            }
            JobResult::NeedsDeps(_) => {
                layout.work = LayoutWork::Current;
                Log::error(
                    Event::General,
                    format!("Tile {} asked again for the dependencies it was given", job.id.tile.id),
                );
                self.fail(Error::native("a layout asked again for the dependencies it was given"));
            }
            JobResult::Failed(error) => {
                layout.work = LayoutWork::Current;
                self.fail(error);
            }
            JobResult::Raster(_) | JobResult::Dem(_) => {
                layout.work = LayoutWork::Current;
                self.fail(Error::native("a geometry tile's layout answered as a decode"));
            }
        }
    }

    fn request(&mut self, method: Method) {
        let Some(file_source) = self.source.file_source() else {
            self.fail(Error::native("Can't load tile."));
            return;
        };
        self.action(match method {
            Method::Cache => TileOperation::RequestedFromCache,
            Method::Network => TileOperation::RequestedFromNetwork,
        });
        let load = self.source.next();
        let key = self.source.tile_key(self.tile);
        let OriginLoad::Tileset(tileset) = &mut self.tile.load.origin else {
            return;
        };
        let inbox = self.store.inbox.clone();
        // One response per request: a reload is a new request.
        let (request, responder) =
            Request::once(move |response| inbox.send(TileEvent::Response { tile: key, load, response }));
        file_source.load(tileset.resource_for(method), responder);
        debug_assert!(tileset.is_idle(), "one request at a time");
        tileset.fetch = match method {
            Method::Cache => Fetch::Cache { load, request },
            Method::Network => Fetch::Network { load, request },
        };
    }

    fn take_response(&mut self, method: Method, res: &Response) {
        let OriginLoad::Tileset(tileset) = &mut self.tile.load.origin else {
            return;
        };
        if let Some(error) = &res.error
            && error.reason != Reason::NotFound
        {
            tileset.fetch = Fetch::Failed;
            self.fail(Error::native(error.message.clone()));
            return;
        }
        let resource = &mut tileset.resource;
        if !res.not_modified {
            resource.prior_modified = res.modified;
            resource.prior_etag = res.etag.clone();
        }
        resource.prior_expires = res.expires;
        (tileset.modified, tileset.expires) = (res.modified, res.expires);
        self.action(match method {
            Method::Network => TileOperation::LoadFromNetwork,
            Method::Cache => TileOperation::LoadFromCache,
        });
        if res.not_modified {
            // The tile keeps its data: it has done what it can, unless work
            // on that data is under way, which settles it as it ends.
            if !self.tile.pipeline.is_pending(true) {
                self.tile.settled = true;
            }
        } else if let Some(origin) = &self.source.origin {
            let input = origin.input(self.source.kind, res);
            self.take_input(input);
        }
    }

    fn custom_loader(&self) -> Option<(Arc<dyn CustomLoader>, bool)> {
        match &self.source.origin {
            Some(DataOrigin::Custom { loader, fast_pfor_enabled }) => Some((Arc::clone(loader), *fast_pfor_enabled)),
            Some(DataOrigin::Tileset { .. } | DataOrigin::GeoJson(_)) | None => None,
        }
    }

    fn fetch(&mut self, key: SubscriberKey) {
        if let Some((loader, fast_pfor_enabled)) = self.custom_loader() {
            let tile_key = self.source.tile_key(self.tile);
            loader.fetch(self.tile.id, key, tile_key, &self.store.inbox, fast_pfor_enabled);
        }
    }
}

/// What a new tile starts with from the tile in another world copy: what
/// that tile shows and, for a tileset tile whose contents are of its latest
/// data, its load: what its requests know, and the data a geometry tile lays
/// out.
pub(crate) struct WorldCopy {
    shown: Shown,
    load: Option<(TilesetAnswered, Option<LayoutData>)>,
}

impl WorldCopy {
    /// A copy of `tile`, if it is renderable. A geometry tile's copy draws
    /// its buckets, apart from those of the layers that are placed: these
    /// hold what placement made of them where their tile is. A raster or
    /// raster-dem tile's copy has a bucket of its own, since each is masked
    /// where it is.
    pub(crate) fn of(tile: &Tile) -> Option<WorldCopy> {
        let shown = tile.shown.as_ref().filter(|shown| shown.contents.is_renderable())?;
        let contents = match &*shown.contents {
            Contents::Geometry(contents) => {
                let mut contents = contents.clone();
                contents.result.layer_render_data.retain(|_, data| !data.layer.style().info().cross_tile_index);
                Contents::Geometry(contents)
            }
            Contents::Raster(bucket) => {
                Contents::Raster(Arc::new(RasterBucket::with_shared_image(bucket.image.clone())))
            }
            Contents::Dem(bucket) => {
                Contents::Dem(Arc::new(HillshadeBucket::with_dem_data(bucket.get_dem_data().clone())))
            }
            Contents::Empty => Contents::Empty,
        };
        let load = match (&tile.load.origin, &tile.pipeline) {
            // The copy lays the latest data out itself.
            (OriginLoad::Tileset(tileset), Pipeline::Layout(layout)) => {
                layout.data.clone().map(|data| (tileset.answered(), Some(data)))
            }
            // An image still to decode is newer than the one shown.
            (OriginLoad::Tileset(tileset), Pipeline::Decode(_)) => {
                (!tile.is_pending()).then(|| (tileset.answered(), None))
            }
            (OriginLoad::GeoJson { .. } | OriginLoad::Custom(_), _) => None,
        };
        let world_copy = matches!(contents, Contents::Geometry(_));
        Some(WorldCopy {
            shown: Shown {
                contents: Rc::new(contents),
                feature_state_version: shown.feature_state_version,
                answer_outdated: shown.answer_outdated,
                world_copy,
            },
            load,
        })
    }
}

/// Lets go of `tile`: its job needn't run, its custom loader forgets it, its
/// request drops and its contents are freed.
pub(crate) fn retire(source: &SourceInfo, mut tile: Tile, store: &mut StoreContext) {
    if tile.is_pending() {
        store.action(&source.id, tile.id, TileOperation::Cancelled);
    }
    if let Some(job) = tile.pipeline.in_flight() {
        job.superseded.store(true, Ordering::Relaxed);
    }
    if let (Some(key), Some(DataOrigin::Custom { loader, .. })) = (tile.load.subscription(), &source.origin) {
        loader.remove(tile.id, key);
    }
    if let Some(shown) = tile.shown.take() {
        release(shown.contents, &store.pool);
    }
}