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
//! Cameras that move faster than the network answers. Each frame of a fast
//! zoom or pan asks for tiles that the next frame no longer shows; the view
//! the camera stops at must not wait for them.
//!
//! The harness updates a vector source drawn by a fill layer and a raster
//! source, as a map's frames do, over a network that has `SLOTS` requests in
//! flight at once, as the online file source does, and answers each
//! `LATENCY` frames after it got a slot, fresh for an hour. It counts the
//! frames until the view the camera stops at has all its tiles, or the
//! requests each tile got.

use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::sync::Arc;

use mln_map::transform::Transform;
use mln_map_types::{CameraOptions, ConstrainMode, MapDebugOptions, MapMode, TileLodMode, ViewportMode};
use mln_storage::{FileSource, Resource, Responder, Response};
use mln_style::Source;
use mln_style::layer::Layer;
use mln_style::layer_type::LayerStyle;
use mln_style::layers::{Fill, Raster};
use mln_style::sources::{raster_source, vector_source};
use mln_util::chrono::{Timestamp, now};
use mln_util::constants::DEFAULT_MAXIMUM_CONCURRENT_REQUESTS;
use mln_util::geo::LatLng;
use mln_util::pool::TaskGroup;
use mln_util::queue::Queue;
use mln_util::size::Size;
use mln_util::subdivision_granularity::SubdivisionGranularitySetting;
use mln_util::tileset::PartialTileset;
use parking_lot::Mutex;

use crate::Registry;
use crate::layer_type::{ErasedEvaluatedLayer, EvaluatedLayer};
use crate::render_source::RenderSource;
use crate::tile::source_tiles::SourceTiles;
use crate::tile::store::TileStore;
use crate::tile_parameters::TileParameters;

/// The requests in flight at once.
const SLOTS: usize = DEFAULT_MAXIMUM_CONCURRENT_REQUESTS as usize;
/// The frames a request in flight takes: 100 ms at 60 frames a second.
const LATENCY: usize = 6;

fn read_file(relative: &str) -> Vec<u8> {
    let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("testdata").join(relative);
    std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()))
}

// MARK: - The network

/// An open request: the tile's `source/z/x/y`, the callback until the
/// network answers, and the frame it got a slot in.
struct Pending {
    tile: String,
    responder: Responder,
    in_flight_since: Option<usize>,
}

/// The open requests, in the order they were made, the frames ended, and
/// how many requests each tile got.
#[derive(Default)]
struct Requests {
    open: VecDeque<Pending>,
    frame: usize,
    made: BTreeMap<String, usize>,
}

/// Answers every tile of the vector source with the same vector tile, and
/// every tile of the raster source with the same image.
struct Network {
    requests: Mutex<Requests>,
    vector_tile: Arc<Vec<u8>>,
    raster_tile: Arc<Vec<u8>>,
}

impl Network {
    fn new() -> Self {
        Network {
            requests: Mutex::default(),
            vector_tile: Arc::new(read_file("test/fixtures/map/issue12432/0-0-0.mvt")),
            raster_tile: Arc::new(read_file("test/fixtures/annotations/emerald.png")),
        }
    }

    /// Ends a frame: the requests that have been open the longest take the
    /// free slots, and those in flight for `LATENCY` frames are answered.
    fn end_frame(&self) {
        // Answered with the lock released: an answer's callback may request
        // again.
        let answered: VecDeque<Pending> = {
            let mut requests = self.requests.lock();
            let frame = requests.frame;
            requests.frame += 1;
            requests.open.retain(|pending| !pending.responder.is_cancelled());
            for pending in requests.open.iter_mut().take(SLOTS) {
                pending.in_flight_since.get_or_insert(frame);
            }
            let (answered, open) = std::mem::take(&mut requests.open)
                .into_iter()
                .partition(|pending| pending.in_flight_since.is_some_and(|since| frame + 1 - since >= LATENCY));
            requests.open = open;
            answered
        };
        for pending in answered {
            // An earlier answer in the batch may have cancelled it.
            if pending.responder.is_cancelled() {
                continue;
            }
            let mut response = Response::new();
            let data = if pending.tile.starts_with("vector") { &self.vector_tile } else { &self.raster_tile };
            response.data = Some(Arc::clone(data));
            response.expires = Some(Timestamp::from_secs(now().secs_since_epoch() + 3600));
            pending.responder.send(response);
        }
    }

    /// The tiles of the open requests, oldest first.
    fn open_requests(&self) -> Vec<String> {
        let requests = self.requests.lock();
        requests.open.iter().filter(|pending| !pending.responder.is_cancelled()).map(|p| p.tile.clone()).collect()
    }

    /// The tiles requested more than once, and how many times.
    fn requested_again(&self) -> BTreeMap<String, usize> {
        let requests = self.requests.lock();
        requests.made.iter().filter(|(_, count)| **count > 1).map(|(tile, count)| (tile.clone(), *count)).collect()
    }
}

impl FileSource for Network {
    fn load(&self, resource: &Resource, responder: Responder) {
        let data = resource.tile_data.as_ref().expect("only tiles are requested");
        let source = resource.url.split('/').next().unwrap_or_default();
        let tile = format!("{source}/{}/{}/{}", data.z, data.x, data.y);
        // A tile that is still fresh, which the online file source holds
        // until it expires: never answered.
        if resource.prior_expires.is_some_and(|expires| expires > now()) {
            return;
        }
        let mut requests = self.requests.lock();
        *requests.made.entry(tile.clone()).or_default() += 1;
        requests.open.push_back(Pending { tile, responder, in_flight_since: None });
    }

    fn can_request(&self, _resource: &Resource) -> bool {
        true
    }
}

// MARK: - The view

/// A vector source of 512-pixel tiles up to zoom 14 under a fill layer, and
/// a raster source of 256-pixel tiles up to zoom 6, as the Liberty style
/// draws its land and water at low zoom levels.
struct Scene {
    run_loop: Queue<()>,
    thread_pool: TaskGroup,
    network: Arc<Network>,
    transform: Transform,
    vector: Source,
    raster: Source,
    fill_layers: Vec<Arc<dyn ErasedEvaluatedLayer>>,
    raster_layers: Vec<Arc<dyn ErasedEvaluatedLayer>>,
    vector_render_source: Box<dyn RenderSource>,
    raster_render_source: Box<dyn RenderSource>,
    store: TileStore,
    laid_out: bool,
}

impl Scene {
    fn new(size: Size) -> Self {
        let run_loop = Queue::new();
        let thread_pool = TaskGroup::new(&crate::tests::pool());
        let network = Arc::new(Network::new());

        let tileset = |url: &str, max_zoom| PartialTileset {
            tiles: Some(vec![url.to_owned()]),
            max_zoom: Some(max_zoom),
            ..Default::default()
        };
        let vector = crate::tests::ready(vector_source("vector", None, tileset("vector/{z}/{x}/{y}", 14)));
        let raster = crate::tests::ready(raster_source("raster", None, tileset("raster/{z}/{x}/{y}", 6), 256));

        let mut fill = Layer::new(Registry::with_builtins().style(), "fill", "water", "vector").unwrap();
        fill.set_source_layer("water");
        let fill_layers: Vec<Arc<dyn ErasedEvaluatedLayer>> =
            vec![Arc::new(EvaluatedLayer::<Fill>::new(LayerStyle::recover(fill.style()).unwrap()))];
        let raster_layer = Layer::new(Registry::with_builtins().style(), "raster", "raster", "raster").unwrap();
        let raster_layers: Vec<Arc<dyn ErasedEvaluatedLayer>> =
            vec![Arc::new(EvaluatedLayer::<Raster>::new(LayerStyle::recover(raster_layer.style()).unwrap()))];

        let mut transform = Transform::new(ConstrainMode::HeightOnly, ViewportMode::Default);
        transform.resize(size).unwrap();

        let mut store = crate::tests::pool_store(&thread_pool);
        Scene {
            vector_render_source: crate::tests::create_render_source(&vector, &mut store),
            raster_render_source: crate::tests::create_render_source(&raster, &mut store),
            store,
            run_loop,
            thread_pool,
            network,
            transform,
            vector,
            raster,
            fill_layers,
            raster_layers,
            laid_out: false,
        }
    }

    fn pyramids(&self) -> [&SourceTiles; 2] {
        [&self.vector_render_source, &self.raster_render_source]
            .map(|source| source.tiles().and_then(|key| self.store.source(key)).expect("the source's tiles"))
    }

    /// A frame at `zoom` over `center`, north up.
    fn frame(&mut self, zoom: f64, center: (f64, f64)) {
        self.frame_rotated(zoom, center, 0.0);
    }

    /// A frame at `zoom` over `center`, turned by `bearing` degrees: the
    /// sources update for the camera, then the network answers what is due,
    /// and the tile workers lay out what it answered.
    fn frame_rotated(&mut self, zoom: f64, center: (f64, f64), bearing: f64) {
        self.transform.jump_to(&CameraOptions {
            center: Some(LatLng::new(center.0, center.1).unwrap()),
            zoom: Some(zoom),
            bearing: Some(bearing),
            ..Default::default()
        });
        let file_source: Arc<dyn FileSource> = Arc::<Network>::clone(&self.network);
        let parameters = TileParameters {
            pixel_ratio: 1.0,
            debug_options: MapDebugOptions::NO_DEBUG,
            transform_state: self.transform.get_state(),
            file_source: Some(file_source),
            mode: MapMode::Continuous,
            // The map's default.
            prefetch_zoom_delta: 4,
            tile_lod_min_radius: 3.0,
            tile_lod_scale: 1.0,
            tile_lod_pitch_threshold: (60.0 / 180.0) * std::f64::consts::PI,
            tile_lod_zoom_shift: 0.0,
            tile_lod_mode: TileLodMode::Default,
            subdivision_granularity: SubdivisionGranularitySetting::none(),
            global_state: None,
            is_update_synchronous: false,
            font_faces: None,
        };
        // The first frame lays the tiles out.
        let relayout = !self.laid_out;
        self.laid_out = true;
        let store = &mut self.store;
        self.vector_render_source.update(store, self.vector.clone(), &self.fill_layers, true, relayout, &parameters);
        self.raster_render_source.update(store, self.raster.clone(), &self.raster_layers, true, relayout, &parameters);

        self.network.end_frame();
        // Until neither the tile workers nor the replies they sent have
        // anything left to do.
        loop {
            self.thread_pool.wait();
            let ran = self.run_loop.run_once(&mut ());
            crate::tests::update(&mut self.store);
            let work = self.pyramids().iter().any(|source| source.slots().any(|slot| slot.tile.has_work()));
            if !ran && !work && self.store.is_idle() {
                break;
            }
        }
    }

    /// Whether every tile the view wants has loaded.
    fn is_complete(&self) -> bool {
        self.pyramids().iter().all(|pyramid| pyramid.is_loaded())
    }

    /// Frames at `zoom` over `center` until the view is complete; the frames
    /// it took.
    fn frames_until_complete(&mut self, zoom: f64, center: (f64, f64)) -> usize {
        for frames in 1..=500 {
            self.frame(zoom, center);
            if self.is_complete() {
                return frames;
            }
        }
        panic!("the view didn't complete in 500 frames; open requests: {:?}", self.network.open_requests());
    }

    /// The frames that the requests for the view's tiles take at most, in
    /// batches of `SLOTS`, with a batch to spare for the requests in flight
    /// when the camera stopped.
    fn frames_needed(&self) -> usize {
        let tiles: usize = self.pyramids().iter().map(|source| source.cover().count()).sum();
        (1 + tiles.div_ceil(SLOTS)) * LATENCY
    }

    /// The world copies that the vector source draws.
    fn vector_wraps(&self) -> BTreeSet<i16> {
        self.pyramids()[0].rendered().map(|rendered| rendered.id.wrap).collect()
    }
}

// MARK: - Tests

/// A pinch out from a loaded view at zoom 5.5 over Canada to the whole world,
/// a frame at each of a few zoom levels, faster than the network answers.
/// Each world copy of the vector tiles, and of the raster tiles, shows as
/// soon as the network can answer their requests: the tiles of the zoom
/// levels that the pinch passed stop loading rather than go first.
#[test]
fn fast_zoom_out_loads_the_world_copies_first() {
    let mut scene = Scene::new(Size::new(1600, 900));
    let canada = (58.3, -110.5);
    scene.frames_until_complete(5.5, canada);

    for (zoom, center) in [(4.5, (50.0, -111.0)), (3.5, (40.0, -112.0)), (2.5, (25.0, -113.0)), (1.5, (10.0, -114.0))] {
        scene.frame(zoom, center);
    }
    let open = scene.network.open_requests();
    let frames = scene.frames_until_complete(0.0, (0.0, -114.4));

    let needed = scene.frames_needed();
    assert!(
        frames <= needed,
        "the world took {frames} frames to load, its tiles' requests {needed}; requests open after the pinch: \
         {open:?}"
    );
    assert_eq!(BTreeSet::from([-1, 0, 1]), scene.vector_wraps(), "the view shows three world copies");
}

/// A fling at zoom 6 from Paris to Beijing, a tile a frame, faster than the
/// network answers. The tiles the view passes stop loading once it has left
/// them behind, and Beijing's load first.
#[test]
fn fast_pan_loads_the_final_view_first() {
    let mut scene = Scene::new(Size::new(1600, 900));
    let (paris, beijing) = ((48.86, 2.35), (39.90, 116.40));
    scene.frames_until_complete(6.0, paris);

    let steps = 20;
    for step in 1..steps {
        let t = f64::from(step) / f64::from(steps);
        scene.frame(6.0, (paris.0 + (beijing.0 - paris.0) * t, paris.1 + (beijing.1 - paris.1) * t));
    }
    let open = scene.network.open_requests();
    let frames = scene.frames_until_complete(6.0, beijing);

    let needed = scene.frames_needed();
    assert!(
        frames <= needed,
        "Beijing took {frames} frames to load, its tiles' requests {needed}; requests open after the fling: {open:?}"
    );
}

/// From 0 at `seconds`: to 1, then between -1 and 1, each swing half a
/// second, back to 0 after each second.
fn swing(seconds: f64) -> f64 {
    4.0 * ((seconds - 0.25).rem_euclid(1.0) - 0.5).abs() - 1.0
}

/// A point of zoom 14's tiles, in tile coordinates.
fn z14_point(x: f64, y: f64) -> (f64, f64) {
    let tiles = 16384.0;
    let latitude = (std::f64::consts::PI * (1.0 - 2.0 * y / tiles)).sinh().atan().to_degrees();
    (latitude, x / tiles * 360.0 - 180.0)
}

/// A camera's zoom, center and bearing.
type Camera = (f64, (f64, f64), f64);

/// Three seconds of the camera swinging back and forth along `path`, its
/// camera at a time in seconds, a frame at a time at 60 Hz,
/// from a loaded view. A tile that the view needs only for moments, too
/// short to load it, goes on loading after it leaves the view, so that each
/// swing back finds it rather than fetching it again.
fn assert_swing_fetches_each_tile_once(path: fn(f64) -> Camera) {
    let mut scene = Scene::new(Size::new(1024, 768));
    let (zoom, center, bearing) = path(0.0);
    for _ in 0..500 {
        scene.frame_rotated(zoom, center, bearing);
        if scene.is_complete() {
            break;
        }
    }
    assert!(scene.is_complete(), "the first view didn't complete");
    for frame in 1..=180 {
        let (zoom, center, bearing) = path(f64::from(frame) / 60.0);
        scene.frame_rotated(zoom, center, bearing);
    }
    assert_eq!(BTreeMap::new(), scene.network.requested_again(), "the tiles requested again during the swings");
}

/// A turn between -90° and 90° around a point 0.23 of a tile south-east of a
/// tile corner: the view reaches the column and the row of tiles two west
/// and two north of that corner only near its widest, for a sixth of each
/// swing.
#[test]
fn turning_back_and_forth_fetches_each_tile_once() {
    assert_swing_fetches_each_tile_once(|seconds| (14.0, z14_point(2620.23, 6332.23), 90.0 * swing(seconds)));
}

/// A pan 0.65 of a tile east and west of a tile's center that zooms out
/// from 14.9 to 14 at each end: the view's edges reach the next tiles only
/// in the last eighth of each swing, as a pinch back and forth does.
#[test]
fn panning_and_zooming_back_and_forth_fetches_each_tile_once() {
    assert_swing_fetches_each_tile_once(|seconds| {
        let swing = swing(seconds);
        (14.0 + 0.9 * (1.0 - swing.abs()), z14_point(2620.5 + 0.65 * swing, 6332.0), 0.0)
    });
}

/// A pan at zoom 0 from a loaded view of one world to the next world copy
/// east, then west. The frame that first shows a world copy draws it, from
/// the tiles of the world that has loaded, and the network isn't asked for
/// them again.
#[test]
fn pan_to_a_new_world_copy_draws_it_at_once() {
    let mut scene = Scene::new(Size::new(512, 512));
    scene.frames_until_complete(0.0, (0.0, 0.0));
    let raster_wraps =
        |scene: &Scene| -> BTreeSet<i16> { scene.pyramids()[1].rendered().map(|rendered| rendered.id.wrap).collect() };

    for (longitude, wraps) in [(90.0, [0, 1]), (-90.0, [-1, 0])] {
        scene.frame(0.0, (0.0, longitude));
        assert_eq!(BTreeSet::from(wraps), scene.vector_wraps(), "the vector tiles' world copies at {longitude}");
        assert_eq!(BTreeSet::from(wraps), raster_wraps(&scene), "the raster tiles' world copies at {longitude}");
        assert!(scene.is_complete(), "the copies' tiles laid out the data of the loaded ones");
    }
    assert_eq!(BTreeMap::new(), scene.network.requested_again());
}