mln-rs-runtime 0.0.23

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
//! The tiles that a map loads for its view, and what it draws while they
//! load.

use std::collections::BTreeSet;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use mln_engine::TileOperation;
use mln_engine::camera::CameraOptions;
use mln_engine::map::MapEvent;
use mln_engine::storage::{Kind, Response};
use mln_runtime::{DemandMode, ResourceRequest, RuntimeEventKind};
use mln_style::size::Size;
use mln_style::tile_id::CanonicalTileID;
use parking_lot::Mutex;

use crate::display::Display;
use crate::support::still::{StillMap, StillOptions, file_response, ll, not_found, read_bytes, serve};
use crate::support::wait;

fn zoom(zoom: f64) -> CameraOptions {
    CameraOptions { zoom: Some(zoom), ..Default::default() }
}

fn no_content() -> Response {
    Response { no_content: true, ..Response::default() }
}

/// A map loads tiles only at the zooms where a layer of their source shows.
#[test]
fn a_source_loads_tiles_only_at_the_zooms_its_layers_show() {
    let map = StillMap::still();
    map.load_json(
        r#"{
  "sources": {
    "a": { "type": "vector", "tiles": [ "a/{z}/{x}/{y}" ] }
  },
  "layers": [{
    "id": "a",
    "type": "fill",
    "source": "a",
    "source-layer": "a",
    "minzoom": 0.3,
    "maxzoom": 1.6
  }]
}"#,
    );
    let requests = serve(&map.runtime, |_| no_content());

    let expected = |z: f64| -> BTreeSet<String> {
        let tiles: &[&str] = if z == 0.3 {
            // The layer's minzoom is 0.3: no tiles load below it.
            &["a/0/0/0"]
        } else if z == 1.0 {
            &["a/1/1/0", "a/1/0/1", "a/1/0/0", "a/1/1/1"]
        } else {
            // The layer's maxzoom is 1.6: no z2 or z3 tiles load.
            &[]
        };
        tiles.iter().map(ToString::to_string).collect()
    };

    // Each tenth of a zoom level from 0 to 3, computed from an integer so it
    // compares exactly.
    for tenths in 0..=30u32 {
        let z = f64::from(tenths) / 10.0;
        requests.clear();
        map.jump(zoom(z));
        map.render();
        let loaded: BTreeSet<String> = requests.urls(Kind::Tile).into_iter().collect();
        assert_eq!(expected(z), loaded, "zoom level {z}");
    }
}

/// A continuous map whose style switches to one without layers, past its
/// tiles' zooms, goes idle: the tiles of the style before it fade out and
/// leave no repaint behind.
#[test]
fn a_map_goes_idle_once_a_new_style_drops_its_fading_tiles() {
    let map = StillMap::continuous();
    serve(&map.runtime, |_| file_response("test/fixtures/map/issue12432/0-0-0.mvt"));
    map.jump(zoom(3.0));
    map.load_json(
        r#"{
      "version": 8,
      "sources": {
        "mapbox": {
          "type": "vector",
          "tiles": ["http://example.com/{z}-{x}-{y}.vector.pbf"]
        }
      },
      "layers": [{
        "id": "water",
        "type": "fill",
        "source": "mapbox",
        "source-layer": "water"
      }]
    }"#,
    );
    map.settle();

    map.load_json(
        r#"{
      "version": 8,
      "sources": {
        "mapbox": {
          "type": "vector",
          "tiles": ["http://example.com/{z}-{x}-{y}.vector.pbf"]
        }
      },
      "layers": []
    }"#,
    );
    map.jump(zoom(20.0));
    map.settle();
}

/// On the globe, a tile drawn in place of children that never arrive meets
/// its finer neighbor without a gap, though it draws their shared edge as one
/// chord and the neighbor as two.
#[test]
fn globe_tile_drawn_for_its_children_meets_finer_tiles_without_a_gap() {
    let map = Display::new(StillOptions { size: Size::new(512, 512), ..StillOptions::default() });
    let ocean = Arc::new(read_bytes("test/fixtures/map/globe_tile_masks/ocean.mvt"));
    let hold_children = Arc::new(AtomicBool::new(false));
    let served: Arc<Mutex<BTreeSet<CanonicalTileID>>> = Arc::default();
    let held: Arc<Mutex<Vec<ResourceRequest>>> = Arc::default();
    {
        let (hold_children, served, held) = (Arc::clone(&hold_children), Arc::clone(&served), Arc::clone(&held));
        wait(map.runtime.set_resource_provider(move |request: ResourceRequest| {
            let Some(tile) = request.resource().tile_data.clone() else {
                let _ = request.respond(not_found());
                return;
            };
            // The children of tile 7/20/49.
            if hold_children.load(Ordering::SeqCst) && tile.z == 8 && tile.x / 2 == 20 && tile.y / 2 == 49 {
                held.lock().push(request);
                return;
            }
            served.lock().insert(CanonicalTileID::new(tile.z as u8, tile.x as u32, tile.y as u32));
            let _ = request.respond(Response { data: Some(Arc::clone(&ocean)), ..Response::default() });
        }));
    }
    map.load_json(
        r#"{"version": 8, "projection": {"type": "vertical-perspective"},
                 "sources": {"ocean": {"type": "vector", "tiles": ["a/{z}/{x}/{y}"]}},
                 "layers": [{"id": "background", "type": "background", "paint": {"background-color": "red"}},
                            {"id": "ocean", "type": "fill", "source": "ocean", "source-layer": "water",
                             "paint": {"fill-color": "blue"}}]}"#,
    );

    // The view's center is the corner of tile 7/20/49.
    map.jump(CameraOptions { center: Some(ll(36.6026, -120.9375)), zoom: Some(7.6), ..Default::default() });
    map.settle();

    hold_children.store(true, Ordering::SeqCst);
    served.lock().clear();
    let events_before = map.events.events().len();
    map.jump(zoom(8.2));
    // The frames the map asks for until every tile served has parsed, and
    // one after.
    map.frames_until(
        events_before,
        |events| {
            let parsed: BTreeSet<CanonicalTileID> = events
                .iter()
                .filter_map(|event| match &event.kind {
                    RuntimeEventKind::Map(MapEvent::TileAction { op: TileOperation::EndParse, id, .. }) => {
                        Some(id.canonical)
                    }
                    _ => None,
                })
                .collect();
            let served = served.lock();
            !served.is_empty() && served.is_subset(&parsed)
        },
        |_| false,
    );
    map.frame(DemandMode::Force);
    let image = map.image();

    assert!(!held.lock().is_empty());
    let background = image.data.as_chunks::<4>().0.iter().filter(|pixel| pixel[0] > 128 && pixel[2] < 128).count();
    assert_eq!(0, background);
}

/// The tiles of a map's view load once while its render data stays, and
/// again for each image after the render data is cleared.
#[test]
fn a_session_loads_its_tiles_again_after_its_render_data_is_cleared() {
    let map = StillMap::still();
    let requests = serve(&map.runtime, |resource| match resource.kind {
        Kind::Tile => file_response("test/fixtures/resources/vector.tile"),
        Kind::Glyphs => file_response("test/fixtures/resources/glyphs.pbf"),
        Kind::Style => file_response("test/fixtures/resources/style_vector.json"),
        Kind::Source => file_response("test/fixtures/resources/source_vector.json"),
        Kind::SpriteJSON => file_response("test/fixtures/resources/sprite.json"),
        Kind::SpriteImage => file_response("test/fixtures/resources/sprite.png"),
        _ => not_found(),
    });
    let loads = || requests.count(Kind::Tile) + requests.count(Kind::Glyphs);

    map.jump(zoom(10.0));
    map.load_url("https://example.com/maps/streets");
    let iterations = 3;
    // The view's tiles.
    let resources = 4;

    requests.clear();
    for _ in 1..=iterations {
        map.render();
        assert_eq!(resources, loads(), "{:?}", requests.all());
    }

    requests.clear();
    for i in 1..=iterations {
        wait(map.session.clear_data());
        map.render();
        assert_eq!(resources * i, loads(), "{:?}", requests.all());
    }
}