mln-rs-engine 0.0.18

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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
//! 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_style::error::Error;
use mln_style::hash::{HashSet, hash_bytes};
use mln_style::subdivision_granularity::SubdivisionGranularitySetting;
use mln_vec_map::VecSet;

use crate::layer::layer_drawing::ChangedBuckets;
use crate::layer::tile_layout::{Dependencies, LayoutParams};
use crate::log_target;
use crate::render::orchestrator::drop_on_workers;
use crate::storage::response::Reason;
use crate::storage::{Request, Response};
use crate::text::glyph_store::GlyphRanges;
use crate::tile_store::contents::{Contents, GeometryContents, LayoutResult, release};
use crate::tile_store::custom_tile_loader::SubscriberKey;
use crate::tile_store::dem::DemContents;
use crate::tile_store::inbox::{SourceData, TileEvent};
use crate::tile_store::loading::{
    self, CustomLoad, CustomLoader, DataOrigin, Fetch, Fetched, Method, OriginLoad, TilesetAnswered,
};
use crate::tile_store::pipeline::{
    DecodeWork, DependencyWait, InFlight, Layout, LayoutData, LayoutWork, Pipeline, Resume, Running, Waiting,
    outdated_by,
};
use crate::tile_store::raster::RasterContents;
use crate::tile_store::store::{SourceInfo, StoreContext};
use crate::tile_store::tile::{Shown, Tile, TileInput, TileNecessity, TileOperation, TileUpdateParameters};
use crate::worker::job::{DataInput, DataKey, Job, JobId, JobResult, LayoutJob, LayoutOutput, Run, SourceStyle};
use crate::worker::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 { .. }) => self.load_geojson(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 asks the network, unless it has data from there that
    /// never expires, or loads from 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() && !tileset.has_lasting_data() {
                        self.request(Method::Network);
                    }
                } else if matches!(tileset.fetch, Fetch::Network { .. }) {
                    // Only a network-only request is aborted, with the
                    // file source's retries and revalidations.
                    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,
    /// now rather than after the file source's backoff.
    pub(crate) fn retry_failed_load(&mut self) {
        if let OriginLoad::Tileset(tileset) = &mut self.tile.load.origin
            && self.tile.load.necessity == TileNecessity::Required
            && tileset.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::Cache { .. } | Fetch::Network { .. } => return,
        };
        let now = crate::storage::chrono::now();
        if method == Method::Network {
            // The request stays open: the file source answers it again after
            // an error, and when the data expires.
            if let Some(error) = res.error.as_ref().filter(|error| error.reason != Reason::NotFound) {
                // A streak of errors reports once.
                if !std::mem::replace(&mut tileset.failed, true) {
                    self.fail(Error::native(error.message.clone()));
                }
                return;
            }
            tileset.failed = false;
            // A 304 renews the copy the tile held back, if the file source
            // didn't deliver it.
            let delivered = res.not_modified.then(|| tileset.resource.deliver(res.clone()));
            self.take_response(method, delivered.as_ref().unwrap_or(res));
            return;
        }
        // A cache-only request answers once.
        tileset.fetch = Fetch::Idle;
        tileset.cache_answered = true;
        self.store.changed();
        // A cache-only request that found nothing isn't an error. A copy
        // shows while it revalidates unless its caching rules forbid that;
        // then the tile holds it back for the network request to revalidate.
        // An empty copy has nothing to hold back: the network loads anew.
        if res.error.is_none() {
            if res.caching.is_usable(now) {
                tileset.resource.set_prior(res);
                self.take_response(method, res);
            } else if !res.no_content {
                // The network request carries it, so that a 304 delivers it.
                tileset.resource.set_prior(res);
                tileset.resource.prior_data.clone_from(&res.data);
            }
        }
        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();
                }
            }
        }
    }

    /// Loads the tile from its source's GeoJSON data: on the workers unless
    /// the source updates synchronously. With `relayout`, the tile first
    /// drops its data and style.
    pub(crate) fn load_geojson(&mut self, relayout: bool) {
        let Some(DataOrigin::GeoJson { data, cuts }) = &self.source.origin else {
            return;
        };
        let (data, cuts) = (Arc::clone(data), (!self.source.synchronous).then(|| cuts.clone()));
        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::request_geojson_tile(&data, key, load, &self.store.inbox, cuts.as_ref()) {
            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;
        // Data that failed isn't fresh: the tile requests it again when
        // required again, and no longer holds it for the tiles cut from it,
        // nor for itself.
        if let OriginLoad::Tileset(tileset) = &mut self.tile.load.origin {
            tileset.from_network = false;
            tileset.held = None;
        }
        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;
        });
    }

    /// 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) {
        self.relayout_if_lacked(|failed| !failed.is_empty());
    }

    /// Lays the tile out again if it lacked glyphs of a range in
    /// `recovered`, ranges that loaded after failing.
    pub(crate) fn glyphs_recovered(&mut self, recovered: &GlyphRanges) {
        self.relayout_if_lacked(|failed| failed.iter().any(|range| recovered.contains(range)));
    }

    /// Lays the tile out again if `lacked` holds for the failed glyph
    /// ranges of the answer it shows or its work has.
    fn relayout_if_lacked(&mut self, lacked: impl Fn(&GlyphRanges) -> bool) {
        let running = self.tile.pipeline.layout().is_some_and(|layout| match &layout.work {
            LayoutWork::Running(Running { answer, .. }) | LayoutWork::Ready(Resume { answer, .. }) => {
                lacked(&answer.failed_glyphs)
            }
            LayoutWork::Current | LayoutWork::Outdated | LayoutWork::Waiting(_) => false,
        });
        let shown = (self.tile.contents().and_then(Contents::geometry))
            .is_some_and(|contents| lacked(&contents.answer.failed_glyphs));
        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(|mut contents| {
            let mut version = 0;
            if let Some(geometry) = contents.geometry_mut() {
                // New buckets, which no draws copied yet.
                let mut changed = ChangedBuckets::default();
                geometry.set_feature_state(self.source.feature_state.current_states(), &mut changed);
                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;
        // The tile has no data to compare the next with.
        if let OriginLoad::Tileset(tileset) = &mut self.tile.load.origin {
            tileset.data_hash = None;
        }
        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(RasterContents::new(image))));
                    }
                    JobResult::Dem(dem) => {
                        self.install(Some(Contents::Dem(DemContents::new(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!(
                    target: log_target::GENERAL,
                    "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, mut method: Method) {
        let fresh = matches!(&self.tile.load.origin, OriginLoad::Tileset(tileset) if tileset.is_fresh(crate::storage::chrono::now()));
        if !fresh && self.take_in_memory() {
            // A required tile whose data expires still asks the network,
            // which the file source holds until the data expires.
            let lasting = matches!(&self.tile.load.origin, OriginLoad::Tileset(tileset) if tileset.has_lasting_data());
            if self.tile.load.necessity != TileNecessity::Required || lasting {
                return;
            }
            method = Method::Network;
        }
        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();
        // A cache-only request answers once; a network request stays open
        // while the tile is required.
        let (request, responder) = match method {
            Method::Cache => {
                Request::once(move |response| inbox.send(TileEvent::Response { tile: key, load, response }))
            }
            Method::Network => {
                Request::new(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");
        // A new request's errors are a new streak, which fails the tile.
        tileset.failed = false;
        tileset.fetch = match method {
            Method::Cache => Fetch::Cache { load, request },
            Method::Network => Fetch::Network { load, request },
        };
    }

    /// A tile whose data at its tileset's last zoom another tile's load
    /// brought fresh from the network, whether a tile cut from it or the
    /// tile itself, takes its data from that, as that load's answer, instead
    /// of loading it again.
    fn take_in_memory(&mut self) -> bool {
        let (Some(origin), OriginLoad::Tileset(tileset)) = (&mut self.source.origin, &mut self.tile.load.origin) else {
            return false;
        };
        let now = crate::storage::chrono::now();
        let Some((data, fetched, held)) = origin.in_memory(&self.tile.id, &tileset.resource.url, now) else {
            return false;
        };
        let Fetched { modified, caching, etag } = fetched;
        let resource = &mut tileset.resource;
        (resource.prior_modified, resource.prior_etag) = (modified, etag);
        resource.prior_caching = caching;
        tileset.took(modified, caching, true);
        tileset.data_hash = None;
        tileset.held = held;
        self.take_input(TileInput::Geometry { data: Some(data), missing: false });
        true
    }

    /// Takes a copy of the tile's data that `method` brought, which caching
    /// rules allow it to show: new data, or, for a 304, the data it has. The
    /// same bytes as the data it has count as a 304.
    fn take_response(&mut self, method: Method, res: &Response) {
        let OriginLoad::Tileset(tileset) = &mut self.tile.load.origin else {
            return;
        };
        if res.not_modified {
            tileset.resource.prior_caching = res.caching;
        } else {
            tileset.resource.set_prior(res);
        }
        tileset.took(res.modified, res.caching, method == Method::Network);
        let hash = res.data.as_deref().filter(|_| !res.no_content).map(|data| hash_bytes(data));
        let unchanged = res.not_modified || hash.is_some_and(|hash| tileset.data_hash == Some(hash));
        if !unchanged {
            tileset.data_hash = hash;
        }
        self.action(match method {
            Method::Network => TileOperation::LoadFromNetwork,
            Method::Cache => TileOperation::LoadFromCache,
        });
        if unchanged {
            // 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), OriginLoad::Tileset(tileset)) =
            (&mut self.source.origin, &mut self.tile.load.origin)
        {
            let network = method == Method::Network;
            let (input, held) = origin.input(self.source.kind, &self.tile.id, &tileset.resource.url, res, network);
            tileset.held = held;
            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 layer_render_data =
                    contents.result.layer_render_data.copy_where(|layer| !layer.style().info().cross_tile_index);
                Contents::Geometry(GeometryContents {
                    result: LayoutResult::new(layer_render_data, Arc::clone(&contents.result.feature_index)),
                    missing: contents.missing,
                    answer: contents.answer.clone(),
                    deps: Rc::clone(&contents.deps),
                    textures: contents.textures.clone(),
                    bucket_geometry: contents.bucket_geometry.clone(),
                })
            }
            Contents::Raster(raster) => Contents::Raster(RasterContents::with_shared_image(Arc::clone(&raster.image))),
            Contents::Dem(dem) => Contents::Dem(DemContents::new(dem.dem().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,
                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);
    }
}