mln-rs-renderer 0.0.13

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

use std::collections::BTreeSet;
use std::num::NonZeroU32;

use geometry::Point;
use glam::Vec2;
use mln_map_types::{TileProjector, TransformState};
use mln_tile_id::OverscaledTileID;
use mln_util::constants::EXTENT;
use mln_util::hash::HashSet;
use mln_vec_map::{VecMap, VecSet};

use crate::bucket::Bucket;
use crate::buckets::symbol_bucket::SymbolBucket;
use crate::layout::symbol_instance::{SymbolInstance, SymbolInstancePools};
use crate::render_layer::RenderLayer;
use crate::render_tile::RenderTile;

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct IndexedSymbolInstance {
    pub(crate) cross_tile_id: NonZeroU32,
    pub(crate) coord: Point<i64>,
}

impl IndexedSymbolInstance {
    #[must_use]
    pub(crate) fn new(cross_tile_id: NonZeroU32, coord: Point<i64>) -> Self {
        IndexedSymbolInstance { cross_tile_id, coord }
    }
}

/// An indexed symbol and where its key is in its index's `keys`.
#[derive(Clone, Copy, Debug)]
struct IndexedSymbol {
    key_start: u32,
    key_len: u32,
    instance: IndexedSymbolInstance,
}

#[derive(Clone, Debug)]
pub(crate) struct TileLayerIndex {
    pub(crate) coord: OverscaledTileID,
    pub(crate) bucket_instance_id: u32,
    pub(crate) bucket_leader_id: String,
    /// The keys of `symbols`, end to end. The render thread builds an index
    /// each time it adds a bucket, so the keys share one allocation rather
    /// than taking one each.
    keys: Vec<u16>,
    /// The bucket's symbols that have a cross-tile id, sorted by key, and in
    /// the bucket's order among equal keys.
    symbols: Vec<IndexedSymbol>,
}

impl TileLayerIndex {
    #[must_use]
    pub(crate) fn new(
        coord: OverscaledTileID,
        symbol_instances: &[SymbolInstance],
        pools: &SymbolInstancePools,
        bucket_instance_id: u32,
        bucket_leader_id: String,
    ) -> Self {
        let mut index =
            TileLayerIndex { coord, bucket_instance_id, bucket_leader_id, keys: Vec::new(), symbols: Vec::new() };
        let indexed = || symbol_instances.iter().filter(|symbol_instance| symbol_instance.cross_tile_id().is_some());
        index.keys.reserve_exact(indexed().map(|symbol_instance| pools.key(symbol_instance).len()).sum());
        index.symbols.reserve_exact(indexed().count());
        for symbol_instance in indexed() {
            let Some(cross_tile_id) = symbol_instance.cross_tile_id() else {
                continue;
            };
            let key = pools.key(symbol_instance);
            index.symbols.push(IndexedSymbol {
                key_start: index.keys.len() as u32,
                key_len: key.len() as u32,
                instance: IndexedSymbolInstance::new(
                    cross_tile_id,
                    index.get_scaled_coordinates(symbol_instance, &coord),
                ),
            });
            index.keys.extend_from_slice(key);
        }
        // A stable sort, so symbols with one key stay in the bucket's order.
        let keys = &index.keys;
        index.symbols.sort_by(|a, b| key_of(keys, a).cmp(key_of(keys, b)));
        index
    }

    /// The indexed symbols with `key`, in their bucket's order.
    fn symbols_with_key(&self, key: &[u16]) -> &[IndexedSymbol] {
        let start = self.symbols.partition_point(|symbol| key_of(&self.keys, symbol) < key);
        let len = self.symbols[start..].partition_point(|symbol| key_of(&self.keys, symbol) == key);
        &self.symbols[start..][..len]
    }

    #[must_use]
    pub(crate) fn get_scaled_coordinates(
        &self,
        symbol_instance: &SymbolInstance,
        child_tile_coord: &OverscaledTileID,
    ) -> Point<i64> {
        // Round anchor positions to roughly 4 pixel grid
        let rounding_factor: f64 = 512.0 / f64::from(EXTENT) / 2.0;
        let scale =
            rounding_factor / 2f64.powi(i32::from(child_tile_coord.canonical.z) - i32::from(self.coord.canonical.z));
        // `childTileCoord.canonical.x * util::EXTENT` is uint32_t arithmetic;
        // adding the float anchor makes it a float, the scale a double.
        let anchor = symbol_instance.get_anchor().point;
        let x = child_tile_coord.canonical.x.wrapping_mul(EXTENT as u32) as f32 + anchor.x;
        let y = child_tile_coord.canonical.y.wrapping_mul(EXTENT as u32) as f32 + anchor.y;
        Point::new((f64::from(x) * scale).floor() as i64, (f64::from(y) * scale).floor() as i64)
    }

    pub(crate) fn find_matches(
        &self,
        symbol_instances: &mut [SymbolInstance],
        pools: &SymbolInstancePools,
        pending: &mut [bool],
        bucket_leader_id: &str,
        new_coord: &OverscaledTileID,
        zoom_cross_tile_ids: &mut BTreeSet<NonZeroU32>,
    ) {
        let tolerance: f32 = if self.coord.canonical.z < new_coord.canonical.z {
            1.0
        } else {
            2f64.powi(i32::from(self.coord.canonical.z) - i32::from(new_coord.canonical.z)) as f32
        };

        if bucket_leader_id != self.bucket_leader_id {
            return;
        }

        for (symbol_instance, pending) in symbol_instances.iter_mut().zip(pending.iter_mut()) {
            if !*pending {
                // already has a match, or is skipped
                continue;
            }

            let indexed = self.symbols_with_key(pools.key(symbol_instance));
            if indexed.is_empty() {
                // No symbol with this key in this bucket
                continue;
            }

            let scaled_symbol_coord = self.get_scaled_coordinates(symbol_instance, new_coord);

            for this_tile_symbol in indexed.iter().map(|symbol| &symbol.instance) {
                // Return any symbol with the same keys whose coordinates are within
                // 1 grid unit. (with a 4px grid, this covers a 12px by 12px area)
                if ((this_tile_symbol.coord.x - scaled_symbol_coord.x).abs() as f32) <= tolerance
                    && ((this_tile_symbol.coord.y - scaled_symbol_coord.y).abs() as f32) <= tolerance
                    && !zoom_cross_tile_ids.contains(&this_tile_symbol.cross_tile_id)
                {
                    // Once we've marked ourselves duplicate against this parent
                    // symbol, don't let any other symbols at the same zoom level
                    // duplicate against the same parent (see issue #10844)
                    zoom_cross_tile_ids.insert(this_tile_symbol.cross_tile_id);
                    symbol_instance.set_cross_tile_id(Some(this_tile_symbol.cross_tile_id));
                    *pending = false;
                    break;
                }
            }
        }
    }
}

/// The key of `symbol`, in its index's `keys`.
fn key_of<'a>(keys: &'a [u16], symbol: &IndexedSymbol) -> &'a [u16] {
    &keys[symbol.key_start as usize..][..symbol.key_len as usize]
}

/// Whether `point`, `elevation` meters above the ground, is in or near the
/// viewport.
fn is_in_viewport(projector: &TileProjector<'_>, point: Vec2, elevation: f32) -> bool {
    // buffer covers area of the next zoom level (current zoom - 1 covered area).
    const BUFFER: f64 = 1.0;
    const EDGE: f64 = 1.0 + BUFFER;

    let projected =
        projector.project_as_drawn(&Point::new(f64::from(point.x), f64::from(point.y)), f64::from(elevation));
    let p = &projected.point;
    !projected.occluded && (p.x > -EDGE && p.y > -EDGE && p.x < EDGE && p.y < EDGE)
}

/// Frees the cross-tile ids of a removed bucket at `zoom`. A free function
/// over the used-ID sets so callers can pass an index that is still borrowed
/// from `CrossTileSymbolLayerIndex::indexes`.
fn remove_bucket_cross_tile_ids(
    used_cross_tile_ids: &mut VecMap<u8, BTreeSet<NonZeroU32>>,
    zoom: u8,
    removed_bucket: &TileLayerIndex,
) {
    for symbol in &removed_bucket.symbols {
        used_cross_tile_ids.entry(zoom).or_default().remove(&symbol.instance.cross_tile_id);
    }
}

#[derive(Debug)]
pub(crate) struct CrossTileSymbolLayerIndex {
    indexes: VecMap<u8, VecMap<OverscaledTileID, TileLayerIndex>>,
    used_cross_tile_ids: VecMap<u8, BTreeSet<NonZeroU32>>,
    lng: f32,
}

impl CrossTileSymbolLayerIndex {
    pub(crate) fn new() -> Self {
        CrossTileSymbolLayerIndex { indexes: VecMap::new(), used_cross_tile_ids: VecMap::new(), lng: 0.0 }
    }

    /// Sometimes when a user pans across the antimeridian the longitude value
    /// gets wrapped. To prevent labels from flashing out and in we adjust the
    /// tileID values in the indexes so that they match the new wrapped
    /// version of the map.
    pub(crate) fn handle_wrap_jump(&mut self, new_lng: f32) {
        let wrap_delta = ((new_lng - self.lng) / 360.0).round() as i32;
        if wrap_delta != 0 {
            let mut new_indexes: VecMap<u8, VecMap<OverscaledTileID, TileLayerIndex>> = VecMap::new();
            for (zoom, zoom_index) in std::mem::take(&mut self.indexes) {
                let mut new_zoom_index: VecMap<OverscaledTileID, TileLayerIndex> = VecMap::new();
                for (_, mut index) in zoom_index {
                    // change the tileID's wrap and move its index
                    index.coord = index.coord.unwrap_to((i32::from(index.coord.wrap) + wrap_delta) as i16);
                    new_zoom_index.entry(index.coord).or_insert(index);
                }
                new_indexes.entry(zoom).or_insert(new_zoom_index);
            }

            self.indexes = new_indexes;
        }

        self.lng = new_lng;
    }

    /// Matches `bucket`'s symbols with the ones already indexed, and gives
    /// each new symbol the id after `max_cross_tile_id`, which it advances.
    pub(crate) fn add_bucket(
        &mut self,
        tile_id: &OverscaledTileID,
        projector: &TileProjector<'_>,
        bucket: &SymbolBucket,
        max_cross_tile_id: &mut u32,
    ) -> bool {
        let mut state = bucket.state();
        let state = &mut *state;

        let this_zoom_indexes = self.indexes.entry(tile_id.overscaled_z).or_default();
        if !this_zoom_indexes.contains_key(tile_id) {
            // The globe moves a loaded tile to another wrap instead of loading
            // it again: same bucket, same symbols.
            let moved = this_zoom_indexes
                .iter()
                .find(|(key, index)| {
                    index.bucket_instance_id == bucket.bucket_instance_id && key.canonical == tile_id.canonical
                })
                .map(|(key, _)| *key);
            if let Some(mut index) = moved.and_then(|moved| this_zoom_indexes.remove(&moved)) {
                index.coord = *tile_id;
                this_zoom_indexes.insert(*tile_id, index);
            }
        }

        if let Some(previous_index) = this_zoom_indexes.get(tile_id) {
            if previous_index.bucket_instance_id == bucket.bucket_instance_id && !state.has_uninitialized_symbols {
                return false;
            }
            // We're replacing this bucket with an updated version
            // Remove the old bucket's "used crossTileIDs" now so that the new
            // bucket can claim them. We have to keep the old index entries
            // themselves until the end of 'addBucket' so that we can copy them
            // with 'findMatches'.
            remove_bucket_cross_tile_ids(&mut self.used_cross_tile_ids, tile_id.overscaled_z, previous_index);
        }

        state.has_uninitialized_symbols = false;

        // Symbols that still need an id; skipped ones keep `None`.
        let mut pending: Vec<bool> = Vec::with_capacity(state.symbol_instances.len());
        if tile_id.overscale_factor() > 1 {
            // For overscaled tiles the viewport might be showing only a small part of the tile,
            // so we filter out the off-screen symbols to improve the performance.
            for symbol_instance in &mut state.symbol_instances {
                symbol_instance.set_cross_tile_id(None);
                let in_viewport =
                    is_in_viewport(projector, symbol_instance.get_anchor().point, symbol_instance.height_offset());
                pending.push(in_viewport);
                state.has_uninitialized_symbols |= !in_viewport;
            }
        } else {
            for symbol_instance in &mut state.symbol_instances {
                symbol_instance.set_cross_tile_id(None);
            }
            pending.resize(state.symbol_instances.len(), true);
        }

        let this_zoom_used_cross_tile_ids = self.used_cross_tile_ids.entry(tile_id.overscaled_z).or_default();

        for (&zoom, zoom_indexes) in &self.indexes {
            if zoom > tile_id.overscaled_z {
                for child_index in zoom_indexes.values() {
                    if child_index.coord.is_child_of(tile_id) {
                        child_index.find_matches(
                            &mut state.symbol_instances,
                            &state.symbol_instance_pools,
                            &mut pending,
                            &bucket.bucket_leader_id,
                            tile_id,
                            this_zoom_used_cross_tile_ids,
                        );
                    }
                }
            } else {
                let parent_tile_id = tile_id.scaled_to(zoom);
                if let Some(parent_index) = zoom_indexes.get(&parent_tile_id) {
                    parent_index.find_matches(
                        &mut state.symbol_instances,
                        &state.symbol_instance_pools,
                        &mut pending,
                        &bucket.bucket_leader_id,
                        tile_id,
                        this_zoom_used_cross_tile_ids,
                    );
                }
            }
        }

        for (symbol_instance, pending) in state.symbol_instances.iter_mut().zip(&pending) {
            if *pending {
                // symbol did not match any known symbol, assign a new id
                let id = loop {
                    *max_cross_tile_id = max_cross_tile_id.wrapping_add(1);
                    if let Some(id) = NonZeroU32::new(*max_cross_tile_id) {
                        break id;
                    }
                };
                symbol_instance.set_cross_tile_id(Some(id));
                this_zoom_used_cross_tile_ids.insert(id);
            }
        }

        let this_zoom_indexes = self.indexes.entry(tile_id.overscaled_z).or_default();
        this_zoom_indexes.remove(tile_id);
        this_zoom_indexes.insert(
            *tile_id,
            TileLayerIndex::new(
                *tile_id,
                &state.symbol_instances,
                &state.symbol_instance_pools,
                bucket.bucket_instance_id,
                bucket.bucket_leader_id.clone(),
            ),
        );
        true
    }

    pub(crate) fn remove_stale_buckets(&mut self, current_ids: &HashSet<u32>) -> bool {
        let mut tiles_changed = false;
        let zooms: Vec<u8> = self.indexes.keys().copied().collect();
        for zoom in zooms {
            let Some(indexes) = self.indexes.get_mut(&zoom) else { continue };
            indexes.retain(|_, index| {
                let current = current_ids.contains(&index.bucket_instance_id);
                if !current {
                    remove_bucket_cross_tile_ids(&mut self.used_cross_tile_ids, zoom, index);
                    tiles_changed = true;
                }
                current
            });
        }
        tiles_changed
    }
}

/// What adding a layer to the index changed.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct AddLayerResult {
    pub(crate) buckets_added: bool,
    pub(crate) buckets_removed: bool,
}

#[derive(Debug, Default)]
pub(crate) struct CrossTileSymbolIndex {
    layer_indexes: VecMap<String, CrossTileSymbolLayerIndex>,
    /// The last cross-tile id given to a symbol, in any layer.
    max_cross_tile_id: u32,
}

impl CrossTileSymbolIndex {
    pub(crate) fn add_layer(&mut self, layer: &dyn RenderLayer, lng: f32, state: &TransformState) -> AddLayerResult {
        let layer_index =
            self.layer_indexes.entry(layer.get_id().to_owned()).or_insert_with(CrossTileSymbolLayerIndex::new);

        let mut result = AddLayerResult::default();
        let mut current_bucket_ids: HashSet<u32> = HashSet::default();

        layer_index.handle_wrap_jump(lng);

        for item in layer.get_placement_data() {
            let render_tile = item.tile();
            let pair = register_at_cross_tile_index(
                &*item.bucket,
                layer_index,
                render_tile,
                state,
                &mut self.max_cross_tile_id,
            );
            debug_assert!(pair.0 != 0);
            result.buckets_added |= pair.1;
            current_bucket_ids.insert(pair.0);
        }

        result.buckets_removed = layer_index.remove_stale_buckets(&current_bucket_ids);

        result
    }

    pub(crate) fn prune_unused_layers(&mut self, used_layers: &VecSet<String>) {
        self.layer_indexes.retain(|id, _| used_layers.contains(id));
    }

    pub(crate) fn reset(&mut self) {
        self.layer_indexes.clear();
    }
}

/// Registers `bucket` at the cross-tile index. Only symbol buckets register;
/// other buckets return `(0, false)`.
pub(crate) fn register_at_cross_tile_index(
    bucket: &dyn Bucket,
    index: &mut CrossTileSymbolLayerIndex,
    render_tile: &RenderTile,
    state: &TransformState,
    max_cross_tile_id: &mut u32,
) -> (u32, bool) {
    match bucket.downcast_ref::<SymbolBucket>() {
        Some(symbol_bucket) => symbol_bucket.register_at_cross_tile_index(index, render_tile, state, max_cross_tile_id),
        None => (0, false),
    }
}

impl SymbolBucket {
    pub(crate) fn register_at_cross_tile_index(
        &self,
        index: &mut CrossTileSymbolLayerIndex,
        render_tile: &RenderTile,
        state: &TransformState,
        max_cross_tile_id: &mut u32,
    ) -> (u32, bool) {
        let first_time_added = index.add_bucket(
            render_tile.get_overscaled_tile_id(),
            &TileProjector::with_data(state, &render_tile.id, render_tile.projection),
            self,
            max_cross_tile_id,
        );
        (self.bucket_instance_id, first_time_added)
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use mln_style::layers::symbol_layer_properties::SymbolLayoutProperties;
    use mln_style::property_value::PropertyValue;
    use mln_style_types::image::ImagePositions;
    use mln_style_types::{
        AnchorOffsetPair, ProjectionDefinition, SymbolAnchorType, SymbolPlacementType, VariableAnchorOffsetCollection,
    };
    use mln_tile_id::geometry_tile_data::GeometryCoordinates;
    use mln_util::Padding;
    use mln_util::geo::LatLng;
    use mln_util::size::Size;

    use super::*;
    use crate::buckets::symbol_bucket::SortKeyRange;
    use crate::geometry::anchor::Anchor;
    use crate::geometry::feature_index::IndexedSubfeature;
    use crate::layout::symbol_instance::{ShapedTextOrientations, SymbolContent, SymbolInstanceSharedData};
    use crate::text::collision_index::OverlapMode;

    fn make_symbol_instance(x: f32, y: f32, key: &str, pools: &mut SymbolInstancePools) -> SymbolInstance {
        let line = GeometryCoordinates::new();
        let image_map = ImagePositions::default();
        let shaping = ShapedTextOrientations::default();
        let layout = SymbolLayoutProperties::Values::default();
        let subfeature = IndexedSubfeature {
            index: 0,
            source_layer_name: Arc::from(""),
            bucket_leader_id: Arc::from(""),
            sort_index: 0,
            bucket_instance_id: 0,
            collision_group_id: 0,
            overlap_mode: OverlapMode::Never,
        };
        let anchor = Anchor::new(x, y, 0.0, Some(0));
        let text_offset = [0.0f32, 0.0];
        let icon_offset = [0.0f32, 0.0];
        let variable_text_offset = [0.0f32, 0.0];
        let anchor_offsets = vec![AnchorOffsetPair::new(SymbolAnchorType::Left, variable_text_offset)];
        let variable_anchor_offset_collection = VariableAnchorOffsetCollection::new(anchor_offsets);
        let placement_type = SymbolPlacementType::Point;

        let shared_data = Arc::new(SymbolInstanceSharedData::new(
            line,
            &shaping,
            None.as_ref(),
            None.as_ref(),
            &layout,
            placement_type,
            text_offset,
            &image_map,
            0.0,
            SymbolContent::IconSDF,
            false,
            false,
        ));
        SymbolInstance::new(
            anchor,
            shared_data,
            &shaping,
            None.as_ref(),
            None.as_ref(),
            0.0,
            0.0,
            placement_type,
            text_offset,
            0.0,
            Padding::uniform(0.0),
            icon_offset,
            &subfeature,
            0,
            0,
            &key.encode_utf16().collect::<Vec<u16>>(),
            0.0,
            0.0,
            0.0,
            Some(variable_anchor_offset_collection).as_ref(),
            false,
            SymbolContent::IconSDF,
            0.0,
            pools,
        )
    }

    /// A bucket of symbols at `(x, y)` with the text `key`.
    fn make_bucket(symbols: Vec<(f32, f32, &str)>, bucket_instance_id: u32) -> SymbolBucket {
        let mut pools = SymbolInstancePools::default();
        let instances = symbols.into_iter().map(|(x, y, key)| make_symbol_instance(x, y, key, &mut pools)).collect();
        let layout = Arc::new(SymbolLayoutProperties::PossiblyEvaluated::default());
        let ranges: Vec<SortKeyRange> = Vec::new();
        let mut bucket = SymbolBucket::new(
            layout,
            &VecMap::new(),
            &PropertyValue::Constant(16.0f32),
            &PropertyValue::Constant(1.0f32),
            0.0,
            false,
            false,
            "test".to_owned(),
            instances,
            pools,
            ranges,
            1.0,
            false,
            Vec::new(),
            false, /*iconsInText*/
        );
        bucket.bucket_instance_id = bucket_instance_id;
        bucket
    }

    fn cross_tile_id(bucket: &SymbolBucket, i: usize) -> Option<u32> {
        bucket.state().symbol_instances[i].cross_tile_id().map(NonZeroU32::get)
    }

    fn camera_at(lat: f64, lon: f64, zoom: f64, globe: bool) -> TransformState {
        let mut state = TransformState::default();
        state.set_size(Size { width: 512, height: 512 });
        if globe {
            state.set_projection_definition(&ProjectionDefinition::from_name("vertical-perspective"));
        }
        state.set_lat_lng_zoom(&LatLng::new(lat, lon).expect("valid LatLng"), zoom);
        state
    }

    // Only overscaled tiles check the viewport, so any camera does for the
    // others.
    fn any_camera() -> TransformState {
        camera_at(0.0, 0.0, 0.0, false)
    }

    // gtest: CrossTileSymbolLayerIndex.addBucket
    #[test]
    fn add_bucket() {
        let any_camera = any_camera();
        let mut max_bucket_instance_id = 0u32;
        let mut index = CrossTileSymbolLayerIndex::new();
        let mut max_cross_tile_id = 0;

        let main_id = OverscaledTileID::with_zxy(6, 0, 6, 8, 8);
        max_bucket_instance_id += 1;
        let main_bucket =
            make_bucket(vec![(1000.0, 1000.0, "Detroit"), (2000.0, 2000.0, "Toronto")], max_bucket_instance_id);
        index.add_bucket(
            &main_id,
            &TileProjector::new(&any_camera, &main_id.to_unwrapped()),
            &main_bucket,
            &mut max_cross_tile_id,
        );

        // Assigned new IDs
        assert_eq!(cross_tile_id(&main_bucket, 0), Some(1));
        assert_eq!(cross_tile_id(&main_bucket, 1), Some(2));

        let child_id = OverscaledTileID::with_zxy(7, 0, 7, 16, 16);
        max_bucket_instance_id += 1;
        let child_bucket = make_bucket(
            vec![
                (2000.0, 2000.0, "Detroit"),
                (2000.0, 2000.0, "Windsor"),
                (3000.0, 3000.0, "Toronto"),
                (4001.0, 4001.0, "Toronto"),
            ],
            max_bucket_instance_id,
        );
        index.add_bucket(
            &child_id,
            &TileProjector::new(&any_camera, &child_id.to_unwrapped()),
            &child_bucket,
            &mut max_cross_tile_id,
        );

        // matched parent tile
        assert_eq!(cross_tile_id(&child_bucket, 0), Some(1));
        // does not match because of different key
        assert_eq!(cross_tile_id(&child_bucket, 1), Some(3));
        // does not match because of different location
        assert_eq!(cross_tile_id(&child_bucket, 2), Some(4));
        // matches with a slightly different location
        assert_eq!(cross_tile_id(&child_bucket, 3), Some(2));

        let parent_id = OverscaledTileID::with_zxy(5, 0, 5, 4, 4);
        max_bucket_instance_id += 1;
        let parent_bucket = make_bucket(vec![(500.0, 500.0, "Detroit")], max_bucket_instance_id);
        index.add_bucket(
            &parent_id,
            &TileProjector::new(&any_camera, &parent_id.to_unwrapped()),
            &parent_bucket,
            &mut max_cross_tile_id,
        );

        // matched child tile
        assert_eq!(cross_tile_id(&parent_bucket, 0), Some(1));

        let mut current_ids: HashSet<u32> = HashSet::default();
        current_ids.insert(main_bucket.bucket_instance_id);
        index.remove_stale_buckets(&current_ids);

        // grandchild
        let grandchild_id = OverscaledTileID::with_zxy(8, 0, 8, 32, 32);
        max_bucket_instance_id += 1;
        let grandchild_bucket =
            make_bucket(vec![(4000.0, 4000.0, "Detroit"), (4000.0, 4000.0, "Windsor")], max_bucket_instance_id);
        index.add_bucket(
            &grandchild_id,
            &TileProjector::new(&any_camera, &grandchild_id.to_unwrapped()),
            &grandchild_bucket,
            &mut max_cross_tile_id,
        );

        // Matches the symbol in `mainBucket`
        assert_eq!(cross_tile_id(&grandchild_bucket, 0), Some(1));
        // Does not match the previous value for Windsor because that tile was removed
        assert_eq!(cross_tile_id(&grandchild_bucket, 1), Some(5));
    }

    // gtest: CrossTileSymbolLayerIndex.tileMovedToAnotherWrapKeepsItsSymbols
    #[test]
    fn tile_moved_to_another_wrap_keeps_its_symbols() {
        let any_camera = any_camera();
        // The globe moves a loaded tile to another wrap instead of loading it
        // again: its bucket keeps the IDs.
        let mut index = CrossTileSymbolLayerIndex::new();
        let mut max_cross_tile_id = 0;

        let bucket = make_bucket(vec![(1000.0, 1000.0, "Detroit"), (2000.0, 2000.0, "Toronto")], 1);

        let id = OverscaledTileID::with_zxy(0, 0, 0, 0, 0);
        assert!(index.add_bucket(
            &id,
            &TileProjector::new(&any_camera, &id.to_unwrapped()),
            &bucket,
            &mut max_cross_tile_id
        ));
        assert_eq!(cross_tile_id(&bucket, 0), Some(1));
        assert_eq!(cross_tile_id(&bucket, 1), Some(2));

        let moved = id.unwrap_to(1);
        assert!(!index.add_bucket(
            &moved,
            &TileProjector::new(&any_camera, &moved.to_unwrapped()),
            &bucket,
            &mut max_cross_tile_id
        ));
        assert_eq!(cross_tile_id(&bucket, 0), Some(1));
        assert_eq!(cross_tile_id(&bucket, 1), Some(2));
        let current_ids: HashSet<u32> = [1].into_iter().collect();
        assert!(!index.remove_stale_buckets(&current_ids));
    }

    // gtest: CrossTileSymbolLayerIndex.resetIDs
    #[test]
    fn reset_ids() {
        let any_camera = any_camera();
        let mut index = CrossTileSymbolLayerIndex::new();
        let mut max_cross_tile_id = 0;

        let main_id = OverscaledTileID::with_zxy(6, 0, 6, 8, 8);
        let main_bucket = make_bucket(vec![(1000.0, 1000.0, "Detroit")], 1);

        let child_id = OverscaledTileID::with_zxy(7, 0, 7, 16, 16);
        let child_bucket = make_bucket(vec![(2000.0, 2000.0, "Detroit")], 2);

        // assigns a new id
        index.add_bucket(
            &main_id,
            &TileProjector::new(&any_camera, &main_id.to_unwrapped()),
            &main_bucket,
            &mut max_cross_tile_id,
        );
        assert_eq!(cross_tile_id(&main_bucket, 0), Some(1));

        // removes the tile
        let current_ids: HashSet<u32> = HashSet::default();
        index.remove_stale_buckets(&current_ids);

        // assigns a new id
        index.add_bucket(
            &child_id,
            &TileProjector::new(&any_camera, &child_id.to_unwrapped()),
            &child_bucket,
            &mut max_cross_tile_id,
        );
        assert_eq!(cross_tile_id(&child_bucket, 0), Some(2));

        // overwrites the old id to match the already-added tile
        index.add_bucket(
            &main_id,
            &TileProjector::new(&any_camera, &main_id.to_unwrapped()),
            &main_bucket,
            &mut max_cross_tile_id,
        );
        assert_eq!(cross_tile_id(&main_bucket, 0), Some(2));
    }

    // gtest: CrossTileSymbolLayerIndex.noDuplicatesWithinZoomLevel
    #[test]
    fn no_duplicates_within_zoom_level() {
        let any_camera = any_camera();
        let mut index = CrossTileSymbolLayerIndex::new();
        let mut max_cross_tile_id = 0;

        let main_id = OverscaledTileID::with_zxy(6, 0, 6, 8, 8);
        let main_bucket = make_bucket(
            vec![
                (1000.0, 1000.0, ""), // A
                (1000.0, 1000.0, ""), // B
            ],
            1,
        );

        let child_id = OverscaledTileID::with_zxy(7, 0, 7, 16, 16);
        let child_bucket = make_bucket(
            vec![
                (2000.0, 2000.0, ""), // A'
                (2000.0, 2000.0, ""), // B'
                (2000.0, 2000.0, ""), // C'
            ],
            2,
        );

        // assigns new ids
        index.add_bucket(
            &main_id,
            &TileProjector::new(&any_camera, &main_id.to_unwrapped()),
            &main_bucket,
            &mut max_cross_tile_id,
        );
        assert_eq!(cross_tile_id(&main_bucket, 0), Some(1));
        assert_eq!(cross_tile_id(&main_bucket, 1), Some(2));

        // copies parent ids without duplicate ids in this tile
        index.add_bucket(
            &child_id,
            &TileProjector::new(&any_camera, &child_id.to_unwrapped()),
            &child_bucket,
            &mut max_cross_tile_id,
        );
        assert_eq!(cross_tile_id(&child_bucket, 0), Some(1)); // A' copies from A
        assert_eq!(cross_tile_id(&child_bucket, 1), Some(2)); // B' copies from B
        assert_eq!(cross_tile_id(&child_bucket, 2), Some(3)); // C' gets new ID
    }

    // gtest: CrossTileSymbolLayerIndex.bucketReplacement
    #[test]
    fn bucket_replacement() {
        let any_camera = any_camera();
        let mut index = CrossTileSymbolLayerIndex::new();
        let mut max_cross_tile_id = 0;

        let tile_id = OverscaledTileID::with_zxy(6, 0, 6, 8, 8);
        let first_bucket = make_bucket(
            vec![
                (1000.0, 1000.0, ""), // A
                (1000.0, 1000.0, ""), // B
            ],
            1,
        );

        let second_bucket = make_bucket(
            vec![
                (1000.0, 1000.0, ""), // A'
                (1000.0, 1000.0, ""), // B'
                (1000.0, 1000.0, ""), // C'
            ],
            2,
        );

        // assigns new ids
        index.add_bucket(
            &tile_id,
            &TileProjector::new(&any_camera, &tile_id.to_unwrapped()),
            &first_bucket,
            &mut max_cross_tile_id,
        );
        assert_eq!(cross_tile_id(&first_bucket, 0), Some(1));
        assert_eq!(cross_tile_id(&first_bucket, 1), Some(2));

        // copies parent ids without duplicate ids in this tile
        index.add_bucket(
            &tile_id,
            &TileProjector::new(&any_camera, &tile_id.to_unwrapped()),
            &second_bucket,
            &mut max_cross_tile_id,
        );
        assert_eq!(cross_tile_id(&second_bucket, 0), Some(1)); // A' copies from A
        assert_eq!(cross_tile_id(&second_bucket, 1), Some(2)); // B' copies from B
        assert_eq!(cross_tile_id(&second_bucket, 2), Some(3)); // C' gets new ID
    }

    fn expect_offscreen_symbols_skipped(globe: bool) {
        let mut index = CrossTileSymbolLayerIndex::new();
        let mut max_cross_tile_id = 0;

        let tile_id = OverscaledTileID::with_zxy(7, 0, 6, 18, 24);
        let symbol_bucket = make_bucket(vec![(1000.0, 1000.0, "Washington"), (2000.0, 2000.0, "Richmond")], 1);
        let away = camera_at(60.0, 25.0, 7.0, globe);
        index.add_bucket(
            &tile_id,
            &TileProjector::new(&away, &tile_id.to_unwrapped()),
            &symbol_bucket,
            &mut max_cross_tile_id,
        );

        assert_eq!(cross_tile_id(&symbol_bucket, 0), None);
        assert_eq!(cross_tile_id(&symbol_bucket, 1), None);

        let over = camera_at(39.0, -76.0, 7.0, globe);
        index.add_bucket(
            &tile_id,
            &TileProjector::new(&over, &tile_id.to_unwrapped()),
            &symbol_bucket,
            &mut max_cross_tile_id,
        );

        assert_eq!(cross_tile_id(&symbol_bucket, 0), Some(1));
        assert_eq!(cross_tile_id(&symbol_bucket, 1), Some(2));
    }

    // gtest: CrossTileSymbolLayerIndex.offscreenSymbols
    #[test]
    fn offscreen_symbols() {
        expect_offscreen_symbols_skipped(false);
    }

    // gtest: CrossTileSymbolLayerIndex.offscreenSymbolsOnTheGlobe
    // On the globe the anchors go through the sphere, not the tile matrix.
    #[test]
    fn offscreen_symbols_on_the_globe() {
        expect_offscreen_symbols_skipped(true);
    }
}