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
//! The provider of a custom source's tile data: [`CustomProvider`] asks the
//! host for each canonical tile that a tile needs, and hands the host's
//! answers to the tiles. It is generic over the data it hands them, and
//! reaches a tile through [`CustomTileSubscriber`].

use std::sync::{Arc, Weak};

use mln_style::error::Error;
use mln_style::geo::LatLngBounds;
use mln_style::hash::HashMap;
use mln_style::sources::custom::{
    CustomGeometrySourceOptions, CustomSourceHandle, CustomTileResponder, CustomVectorSourceOptions,
    CustomVectorTileData, TileFunction, TileFunctions,
};
use mln_style::sources::custom_geometry_source::tile_features;
use mln_style::sources::geojson::TileFeatures;
use mln_style::tile_id::CanonicalTileID;
use parking_lot::Mutex;

use crate::exec::pool::{Pool, TaskGroup};
use crate::tile::OverscaledTileID;
use crate::tile::tile_range::TileRange;

/// One of the host's tile functions, as the provider calls it: with the
/// canonical ID of a tile that the map needs, or no longer needs. It runs on
/// a pool thread (see [`CustomProvider`]).
pub(crate) type HostFunction = Box<dyn Fn(&CanonicalTileID) + Send + Sync>;

/// The host's fetch and cancel functions.
pub(crate) type HostFunctions = (Option<HostFunction>, Option<HostFunction>);

/// A tile that receives a canonical tile's data from a custom source's
/// provider. Each call sends a message to the tile, which runs it on the
/// tile's own thread.
pub(crate) trait CustomTileSubscriber<D>: Send {
    fn set_tile_data(&self, data: D);
    fn set_tile_error(&self, error: Error);
    /// Marks the tile's data stale, so that it fetches again when it is next
    /// needed.
    fn invalidate_tile_data(&self);
}

/// Identifies a tile to the provider. Wrapped and overscaled tiles share their
/// canonical tile's data, and `TilePyramid` changes the wrap of a tile's ID,
/// so a tile is identified by this key rather than by its ID.
pub(crate) type SubscriberKey = u64;

/// A tile that receives a canonical tile's data.
struct Subscriber<D> {
    key: SubscriberKey,
    tile: Box<dyn CustomTileSubscriber<D>>,
    /// Whether the tile still needs the data, rather than having cancelled.
    required: bool,
}

struct TileState<D> {
    subscribers: Vec<Subscriber<D>>,
    /// Whether the host was asked for the data and hasn't answered yet.
    fetching: bool,
}

impl<D> Default for TileState<D> {
    fn default() -> Self {
        TileState { subscribers: Vec::new(), fetching: false }
    }
}

impl<D> TileState<D> {
    fn find(&mut self, key: SubscriberKey) -> Option<&mut Subscriber<D>> {
        self.subscribers.iter_mut().find(|subscriber| subscriber.key == key)
    }

    fn required(&self) -> bool {
        self.subscribers.iter().any(|subscriber| subscriber.required)
    }
}

/// What a provider asks of the host.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum HostCall {
    Fetch(CanonicalTileID),
    Cancel(CanonicalTileID),
}

/// Which canonical tiles the tiles need, and the host's answers: it asks
/// the host for each canonical tile once, and hands the answer to every
/// tile that shares it. It records what to ask of the host in `calls`.
struct CustomTileLoader<D: Clone> {
    tile_states: HashMap<CanonicalTileID, TileState<D>>,
    /// Keep around a cache of tile data to serve back for wrapped and
    /// over-zoomed tiles.
    data_cache: HashMap<CanonicalTileID, D>,
    calls: Vec<HostCall>,
}

impl<D: Clone> CustomTileLoader<D> {
    fn new() -> Self {
        CustomTileLoader { tile_states: HashMap::default(), data_cache: HashMap::default(), calls: Vec::new() }
    }

    /// Records that the tile needs its data. It gets the cached data if there
    /// is any; otherwise the host is asked for it, unless it already was.
    fn fetch_tile(&mut self, tile_id: &OverscaledTileID, key: SubscriberKey, tile: Box<dyn CustomTileSubscriber<D>>) {
        let canonical = tile_id.canonical;
        let state = self.tile_states.entry(canonical).or_default();
        let index = if let Some(index) = state.subscribers.iter().position(|subscriber| subscriber.key == key) {
            state.subscribers[index].required = true;
            index
        } else {
            state.subscribers.push(Subscriber { key, tile, required: true });
            state.subscribers.len() - 1
        };
        if let Some(cached) = self.data_cache.get(&canonical) {
            state.subscribers[index].tile.set_tile_data(cached.clone());
        } else if !state.fetching {
            state.fetching = true;
            self.calls.push(HostCall::Fetch(canonical));
        }
    }

    /// Records that the tile no longer needs its data. It stays subscribed, so
    /// it fetches again when it is next needed.
    fn cancel_tile(&mut self, tile_id: &OverscaledTileID, key: SubscriberKey) {
        let canonical = tile_id.canonical;
        let Some(state) = self.tile_states.get_mut(&canonical) else {
            return;
        };
        let Some(subscriber) = state.find(key) else {
            return;
        };
        if !subscriber.required {
            return;
        }
        subscriber.required = false;
        self.cancel_if_unneeded(&canonical);
    }

    /// Forgets a tile that is gone. The cached data goes with the last tile
    /// that shares it.
    fn remove_tile(&mut self, tile_id: &OverscaledTileID, key: SubscriberKey) {
        let canonical = tile_id.canonical;
        let Some(state) = self.tile_states.get_mut(&canonical) else {
            return;
        };
        let Some(index) = state.subscribers.iter().position(|subscriber| subscriber.key == key) else {
            return;
        };
        let removed = state.subscribers.remove(index);
        let now_empty = state.subscribers.is_empty();
        if removed.required {
            self.cancel_if_unneeded(&canonical);
        }
        if now_empty {
            self.data_cache.remove(&canonical);
            self.tile_states.remove(&canonical);
        }
    }

    /// Whether a tile still needs the canonical tile's data, so that an
    /// answer from the host would be used.
    fn is_required(&self, tile_id: &CanonicalTileID) -> bool {
        self.tile_states.get(tile_id).is_some_and(TileState::required)
    }

    /// Hands over the host's data for a tile. It is dropped unless a tile
    /// needs it.
    fn set_tile_data(&mut self, tile_id: &CanonicalTileID, data: D) {
        let Some(state) = self.tile_states.get_mut(tile_id) else {
            return;
        };
        if !state.required() {
            return;
        }
        state.fetching = false;
        for subscriber in &state.subscribers {
            subscriber.tile.set_tile_data(data.clone());
        }
        self.data_cache.insert(*tile_id, data);
    }

    /// Reports that the host failed to load a tile. The tiles stay subscribed,
    /// so invalidating the failed tile or needing it again fetches it again.
    fn set_tile_error(&mut self, tile_id: &CanonicalTileID, error: &Error) {
        let Some(state) = self.tile_states.get_mut(tile_id) else {
            return;
        };
        if !state.required() {
            return;
        }
        state.fetching = false;
        for subscriber in &state.subscribers {
            subscriber.tile.set_tile_error(error.clone());
        }
        self.data_cache.remove(tile_id);
    }

    fn invalidate_tile(&mut self, tile_id: &CanonicalTileID) {
        if let Some(state) = self.tile_states.remove(tile_id) {
            self.invalidate(tile_id, &state);
        }
    }

    /// Invalidates each tile that `bounds` intersect, at the tile's own zoom.
    fn invalidate_region(&mut self, bounds: &LatLngBounds) {
        let mut tile_ranges: HashMap<u8, TileRange> = HashMap::default();
        let intersecting: Vec<CanonicalTileID> = self
            .tile_states
            .keys()
            .filter(|tile_id| {
                tile_ranges
                    .entry(tile_id.z)
                    .or_insert_with(|| TileRange::from_lat_lng_bounds(bounds, tile_id.z))
                    .contains(tile_id)
            })
            .copied()
            .collect();
        for tile_id in intersecting {
            self.invalidate_tile(&tile_id);
        }
    }

    /// Makes every subscriber fetch the tile again when it next needs it, and
    /// drops the tile's cached data.
    fn invalidate(&mut self, tile_id: &CanonicalTileID, state: &TileState<D>) {
        for subscriber in &state.subscribers {
            subscriber.tile.invalidate_tile_data();
        }
        if state.required() {
            self.calls.push(HostCall::Cancel(*tile_id));
        }
        self.data_cache.remove(tile_id);
    }

    /// Once no tile needs the data, tells the host to stop, and forgets the
    /// request so that the next tile to need the data asks for it again.
    fn cancel_if_unneeded(&mut self, tile_id: &CanonicalTileID) {
        let Some(state) = self.tile_states.get_mut(tile_id) else {
            return;
        };
        if !state.required() {
            state.fetching = false;
            self.calls.push(HostCall::Cancel(*tile_id));
        }
    }
}

/// The provider of a custom source's tile data, between the tiles and the
/// host: it asks the host for each canonical tile that a tile needs, once,
/// and hands the host's answer to every tile that shares it.
///
/// Every call, from the tiles or from the host, is a task of the provider's
/// serial group on the pool, so the calls run one at a time, in the order
/// they were made. The host's fetch and cancel functions run in those
/// tasks, in the order the tiles asked, with no lock held, so a host may
/// answer from inside its fetch function, on any thread: the answer runs
/// after the fetch. A call that runs after the provider is gone does
/// nothing.
pub(crate) struct CustomProvider<D: Clone + Send + 'static> {
    fetch_tile_function: Option<HostFunction>,
    cancel_tile_function: Option<HostFunction>,
    loader: Mutex<CustomTileLoader<D>>,
    calls: TaskGroup,
    this: Weak<Self>,
}

impl<D: Clone + Send + 'static> CustomProvider<D> {
    /// A provider whose host functions, which `host` makes for it, run on
    /// `pool`. `host` gets the provider, for the handle that the functions
    /// give the host.
    #[must_use]
    pub(crate) fn new(pool: &Pool, host: impl FnOnce(&Weak<Self>) -> HostFunctions) -> Arc<Self> {
        Arc::new_cyclic(|this| {
            let (fetch_tile_function, cancel_tile_function) = host(this);
            CustomProvider {
                fetch_tile_function,
                cancel_tile_function,
                loader: Mutex::new(CustomTileLoader::new()),
                calls: TaskGroup::serial(pool),
                this: Weak::clone(this),
            }
        })
    }

    /// Records that the tile `key`, `tile_id`, needs its data, which goes to
    /// `tile`: the cached data if there is any, or else the host's answer.
    pub(crate) fn fetch_tile(
        &self,
        tile_id: OverscaledTileID,
        key: SubscriberKey,
        tile: Box<dyn CustomTileSubscriber<D>>,
    ) {
        self.with_loader(move |loader| loader.fetch_tile(&tile_id, key, tile));
    }

    /// Records that the tile no longer needs its data. It stays subscribed,
    /// so it fetches again when it is next needed.
    pub(crate) fn cancel_tile(&self, tile_id: OverscaledTileID, key: SubscriberKey) {
        self.with_loader(move |loader| loader.cancel_tile(&tile_id, key));
    }

    /// Forgets a tile that is gone. The cached data goes with the last tile
    /// that shares it.
    pub(crate) fn remove_tile(&self, tile_id: OverscaledTileID, key: SubscriberKey) {
        self.with_loader(move |loader| loader.remove_tile(&tile_id, key));
    }

    /// Hands over the host's data for a tile. It is dropped unless a tile
    /// needs it.
    #[cfg(test)]
    pub(crate) fn set_tile_data(&self, tile_id: CanonicalTileID, data: D) {
        self.with_loader(move |loader| loader.set_tile_data(&tile_id, data));
    }

    /// Hands over the data that `make` makes, in the provider's turn and
    /// outside the lock, if a tile needs the canonical tile's data.
    pub(crate) fn set_tile_data_with(&self, tile_id: CanonicalTileID, make: impl FnOnce() -> D + Send + 'static) {
        self.call(move |provider| {
            if provider.loader.lock().is_required(&tile_id) {
                let data = make();
                provider.locked(|loader| loader.set_tile_data(&tile_id, data));
            }
        });
    }

    /// Reports that the host failed to load a tile. The tiles stay
    /// subscribed, so invalidating the tile or needing it again fetches it
    /// again.
    pub(crate) fn set_tile_error(&self, tile_id: CanonicalTileID, error: Error) {
        self.with_loader(move |loader| loader.set_tile_error(&tile_id, &error));
    }

    /// Drops the tile's data, and asks the host for it again if a tile
    /// needs it.
    pub(crate) fn invalidate_tile(&self, tile_id: CanonicalTileID) {
        self.with_loader(move |loader| loader.invalidate_tile(&tile_id));
    }

    /// Invalidates each tile that `bounds` intersect, at the tile's own zoom.
    pub(crate) fn invalidate_region(&self, bounds: LatLngBounds) {
        self.with_loader(move |loader| loader.invalidate_region(&bounds));
    }

    /// Runs `change` on the tiles' state in the provider's turn, then the
    /// host calls it asked for.
    fn with_loader(&self, change: impl FnOnce(&mut CustomTileLoader<D>) + Send + 'static) {
        self.call(move |provider| provider.locked(change));
    }

    /// Runs `change` under the lock, then, outside it, the host calls it
    /// asked for. Only the provider's group's tasks call it.
    fn locked(&self, change: impl FnOnce(&mut CustomTileLoader<D>)) {
        let calls = {
            let mut loader = self.loader.lock();
            change(&mut loader);
            std::mem::take(&mut loader.calls)
        };
        for call in calls {
            let (function, tile_id) = match call {
                HostCall::Fetch(tile_id) => (&self.fetch_tile_function, tile_id),
                HostCall::Cancel(tile_id) => (&self.cancel_tile_function, tile_id),
            };
            if let Some(function) = function {
                function(&tile_id);
            }
        }
    }

    /// Runs `call` in the provider's serial group, if the provider is still
    /// there when its turn comes.
    fn call(&self, call: impl FnOnce(&Self) + Send + 'static) {
        let this = Weak::clone(&self.this);
        self.calls.spawn(move || {
            if let Some(provider) = this.upgrade() {
                call(&provider);
            }
        });
    }
}

/// The provider of a custom geometry source: it hands tiles their features,
/// cut from the host's GeoJSON.
pub(crate) type CustomGeometryProvider = CustomProvider<Arc<TileFeatures>>;

/// The provider of a custom vector source.
pub(crate) type CustomVectorProvider = CustomProvider<CustomVectorTileData>;

impl CustomGeometryProvider {
    /// The provider of the tiles of a source of `options`, whose host
    /// functions run on `pool`. It cuts the host's GeoJSON in its turn,
    /// only for a tile that needs it.
    pub(crate) fn of_source(options: &CustomGeometrySourceOptions, pool: &Pool) -> Arc<Self> {
        let tile_options = Arc::clone(&options.tile_options);
        CustomProvider::new(pool, |provider| {
            let make: MakeData<_, Arc<TileFeatures>> = Arc::new(move |geojson, tile_id: &CanonicalTileID| {
                Arc::new(tile_features(geojson, tile_id, &tile_options))
            });
            host_functions(&options.tile_functions, Responder { provider: Weak::clone(provider), make })
        })
    }
}

impl CustomVectorProvider {
    /// The provider of the tiles of a source of `options`, whose host
    /// functions run on `pool`.
    pub(crate) fn of_source(options: &CustomVectorSourceOptions, pool: &Pool) -> Arc<Self> {
        CustomProvider::new(pool, |provider| {
            let make: MakeData<CustomVectorTileData, CustomVectorTileData> = Arc::new(|data, _| data);
            host_functions(&options.tile_functions, Responder { provider: Weak::clone(provider), make })
        })
    }
}

/// The tiles' data of the host's answer `A` for a tile.
type MakeData<A, D> = Arc<dyn Fn(A, &CanonicalTileID) -> D + Send + Sync>;

/// The host's `functions` as a provider calls them, each with a handle
/// over `responder`.
fn host_functions<A: 'static>(
    functions: &TileFunctions<A>,
    responder: impl CustomTileResponder<A> + 'static,
) -> HostFunctions {
    let handle = CustomSourceHandle::new(Arc::new(responder));
    let bind = |function: &Option<TileFunction<A>>| -> Option<HostFunction> {
        let (function, handle) = (Arc::clone(function.as_ref()?), handle.clone());
        Some(Box::new(move |tile_id: &CanonicalTileID| function(tile_id, &handle)))
    };
    (bind(&functions.fetch_tile), bind(&functions.cancel_tile))
}

/// What a custom source's handle reaches: a provider, while it lives, and
/// how it makes the tiles' data of the host's answers.
struct Responder<A, D: Clone + Send + 'static> {
    provider: Weak<CustomProvider<D>>,
    make: MakeData<A, D>,
}

impl<A: Send + 'static, D: Clone + Send + 'static> CustomTileResponder<A> for Responder<A, D> {
    fn set_tile_data(&self, tile_id: CanonicalTileID, data: A) {
        if let Some(provider) = self.provider.upgrade() {
            let make = Arc::clone(&self.make);
            provider.set_tile_data_with(tile_id, move || make(data, &tile_id));
        }
    }

    fn set_tile_error(&self, tile_id: CanonicalTileID, error: Error) {
        if let Some(provider) = self.provider.upgrade() {
            provider.set_tile_error(tile_id, error);
        }
    }

    fn invalidate_tile(&self, tile_id: CanonicalTileID) {
        if let Some(provider) = self.provider.upgrade() {
            provider.invalidate_tile(tile_id);
        }
    }

    fn invalidate_region(&self, bounds: LatLngBounds) {
        if let Some(provider) = self.provider.upgrade() {
            provider.invalidate_region(bounds);
        }
    }

    fn is_closed(&self) -> bool {
        self.provider.strong_count() == 0
    }
}

#[cfg(test)]
mod tests {
    use std::thread::ThreadId;

    use super::*;

    /// A tile that records the data it receives.
    struct Recorder(Arc<Mutex<Vec<u32>>>);

    impl CustomTileSubscriber<u32> for Recorder {
        fn set_tile_data(&self, data: u32) {
            self.0.lock().push(data);
        }
        fn set_tile_error(&self, _error: Error) {}
        fn invalidate_tile_data(&self) {}
    }

    // The host's fetches run on a pool thread, in the order the tiles asked,
    // and a fetch that answers from inside itself doesn't deadlock.
    #[test]
    fn host_calls_run_off_the_calling_thread_in_order_and_may_reenter() {
        let pool = Pool::new(4);
        let fetches: Arc<Mutex<Vec<(ThreadId, CanonicalTileID)>>> = Arc::default();
        let provider = CustomProvider::new(&pool, |provider: &Weak<CustomProvider<u32>>| {
            let (fetches, provider) = (Arc::clone(&fetches), Weak::clone(provider));
            let fetch: HostFunction = Box::new(move |tile_id: &CanonicalTileID| {
                fetches.lock().push((std::thread::current().id(), *tile_id));
                if let Some(provider) = provider.upgrade() {
                    provider.set_tile_data(*tile_id, tile_id.x);
                }
            });
            (Some(fetch), None)
        });

        let received: Arc<Mutex<Vec<u32>>> = Arc::default();
        let tiles: Vec<_> = (0..8).map(|x| CanonicalTileID::new(3, x, 0)).collect();
        for (key, tile) in tiles.iter().enumerate() {
            let id = OverscaledTileID::new(3, 0, *tile);
            provider.fetch_tile(id, key as SubscriberKey, Box::new(Recorder(Arc::clone(&received))));
        }
        // Each fetch answers before the next, so the last answer comes last.
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
        while received.lock().len() < tiles.len() {
            assert!(std::time::Instant::now() < deadline, "the host's answers didn't arrive");
            std::thread::yield_now();
        }

        let fetches = fetches.lock();
        assert_eq!(tiles, fetches.iter().map(|(_, tile)| *tile).collect::<Vec<_>>());
        let caller = std::thread::current().id();
        assert!(fetches.iter().all(|(thread, _)| *thread != caller));
        assert_eq!((0..8).collect::<Vec<u32>>(), *received.lock());
    }
}