Skip to main content

mln_engine/tile_store/
tile.rs

1//! A tile: plain data the tile store owns. It shows its last contents while
2//! newer data or work is pending.
3
4use std::sync::Arc;
5
6use mln_style::layer::SourceKind;
7
8use crate::layer::tile_layout::Dependencies;
9use crate::log_target;
10use crate::map::MapMode;
11use crate::tile::OverscaledTileID;
12use crate::tile_store::contents::Contents;
13use crate::tile_store::loading::Load;
14use crate::tile_store::pipeline::{Layout, Pipeline};
15
16/// How much the view needs a tile: a required tile is one the view
17/// needs; an optional one is only a fallback or a prefetch the view can do
18/// without, or waits in the cache. An origin may only take low-cost
19/// actions to get an optional tile's data, such as looking in a cache.
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub enum TileNecessity {
22    /// Only a fallback, a prefetch or a cached tile.
23    Optional,
24    /// A tile the view needs.
25    Required,
26}
27
28/// When a tile's network request starts among the requests that wait for a
29/// connection, and its jobs among the other tiles'; lower starts sooner.
30/// Every tile's comes after the requests for anything else (styles,
31/// sources, sprites, glyphs), which leave theirs at zero.
32#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
33pub(crate) struct TileLoadOrder(u32);
34
35impl TileLoadOrder {
36    const RANKS: u32 = 1 << 24;
37
38    /// A tile that no cover of the current view holds.
39    pub(crate) const UNWANTED: TileLoadOrder = TileLoadOrder(u32::MAX);
40
41    /// The `rank`th tile that the coarse cover of the current view retains:
42    /// there are few of them, and each gives a large part of the view
43    /// something to show.
44    #[must_use]
45    pub(crate) fn coarse(rank: usize) -> Self {
46        Self::band(1, rank)
47    }
48
49    /// The `rank`th tile that the ideal cover of the current view retains.
50    #[must_use]
51    pub(crate) fn ideal(rank: usize) -> Self {
52        Self::band(2, rank)
53    }
54
55    fn band(band: u32, rank: usize) -> Self {
56        TileLoadOrder(band * Self::RANKS + (rank as u32).min(Self::RANKS - 1))
57    }
58
59    /// The value for a resource's `RequestOrder`.
60    #[must_use]
61    pub(crate) fn value(self) -> u32 {
62        self.0
63    }
64}
65
66#[derive(Clone, Copy, Debug, PartialEq, Eq)]
67#[repr(u8)]
68pub enum TileOperation {
69    /// A read request from the cache
70    RequestedFromCache,
71    /// A read request from the online source
72    RequestedFromNetwork,
73    /// Tile data from the network has been retrieved
74    LoadFromNetwork,
75    /// Tile data from the cache has been retrieved
76    LoadFromCache,
77    /// Background processing of tile data has been initiated
78    StartParse,
79    /// Background processing of tile data has been completed
80    EndParse,
81    /// An error occurred while loading the tile
82    Error,
83    /// Loading of a tile was cancelled
84    Cancelled,
85    /// No operation has taken place
86    NullOp,
87}
88
89pub(crate) struct Shown {
90    pub(crate) contents: Contents,
91    /// The feature state version the contents' buckets hold; 0 for none.
92    pub(crate) feature_state_version: u64,
93    /// The layout's answer resolved glyphs or images that changed since: no
94    /// first try starts with it.
95    pub(crate) answer_outdated: bool,
96    /// The contents are those of the tile in another world copy, without
97    /// the layers that are placed, which the tile has yet to lay out.
98    pub(crate) world_copy: bool,
99}
100
101impl Shown {
102    pub(crate) fn new(contents: Contents, feature_state_version: u64) -> Self {
103        Shown { contents, feature_state_version, answer_outdated: false, world_copy: false }
104    }
105}
106
107impl crate::source::source_state::FeatureStateTarget for Shown {
108    fn feature_state_version(&mut self) -> &mut u64 {
109        &mut self.feature_state_version
110    }
111
112    fn set_feature_state(
113        &mut self,
114        states: &crate::layer::bucket::LayerFeatureStates,
115        changed: &mut crate::layer::layer_drawing::ChangedBuckets,
116    ) {
117        if let Some(contents) = self.contents.geometry_mut() {
118            contents.set_feature_state(states, changed);
119        }
120    }
121}
122
123pub(crate) struct Tile {
124    pub(crate) id: OverscaledTileID,
125    pub(crate) load: Load,
126    pub(crate) pipeline: Pipeline,
127    pub(crate) shown: Option<Shown>,
128    /// The tile has shown contents, failed, or heard its data is still fresh
129    /// since it last started loading: it has done what it can for now.
130    pub(super) settled: bool,
131}
132
133impl Tile {
134    pub(crate) fn new(id: OverscaledTileID, load: Load, pipeline: Pipeline) -> Self {
135        Tile { id, load, pipeline, shown: None, settled: false }
136    }
137
138    pub(crate) fn contents(&self) -> Option<&Contents> {
139        self.shown.as_ref().map(|shown| &shown.contents)
140    }
141
142    /// The contents, whose buckets the frame changes: their masks, feature
143    /// state, placement and upload.
144    pub(crate) fn contents_mut(&mut self) -> Option<&mut Contents> {
145        self.shown.as_mut().map(|shown| &mut shown.contents)
146    }
147
148    /// Whether the tile can draw, if only some of its layers.
149    pub(crate) fn is_renderable(&self) -> bool {
150        self.contents().is_some_and(Contents::is_renderable)
151    }
152
153    pub(crate) fn is_settled(&self) -> bool {
154        self.settled
155    }
156
157    pub(crate) fn is_pending(&self) -> bool {
158        self.pipeline.is_pending(self.settled)
159    }
160
161    /// Whether the tile has done all it can to become renderable, which it
162    /// may not be.
163    pub(crate) fn is_complete(&self) -> bool {
164        self.settled && !self.is_pending()
165    }
166
167    pub(crate) fn has_work(&self) -> bool {
168        self.pipeline.has_work()
169    }
170
171    /// Whether the tile's render entries are held after they're no longer
172    /// ideal, so that its symbols fade out.
173    pub(crate) fn fades_out(&self) -> bool {
174        self.pipeline.layout().is_some_and(|layout| layout.params.mode == MapMode::Continuous)
175    }
176
177    /// Calls `f` with the dependencies of the layout shown and of the one
178    /// under way.
179    pub(crate) fn dependencies(&self, mut f: impl FnMut(&Dependencies)) {
180        if let Some(contents) = self.contents().and_then(Contents::geometry) {
181            f(&contents.deps);
182        }
183        if let Some(deps) = self.pipeline.layout().and_then(Layout::dependencies) {
184            f(deps);
185        }
186    }
187
188    /// Returns whether the tile draws the layer.
189    pub(crate) fn layer_properties_updated(
190        &mut self,
191        layer_properties: &Arc<dyn crate::layer::layer_type::ErasedEvaluatedLayer>,
192    ) -> bool {
193        self.shown.as_mut().is_some_and(|shown| shown.contents.layer_properties_updated(layer_properties))
194    }
195
196    pub(crate) fn dump_debug_logs(&self, kind: SourceKind) {
197        let kind = match kind {
198            SourceKind::Geometry | SourceKind::None => "Geometry",
199            SourceKind::Raster => "Raster",
200            SourceKind::RasterDem => "RasterDEM",
201        };
202        let yes = |value: bool| if value { "yes" } else { "no" };
203        log::info!(target: log_target::GENERAL, "Tile::Kind: {kind}");
204        log::info!(target: log_target::GENERAL, "Tile::id: {}", self.id);
205        log::info!(target: log_target::GENERAL, "Tile::renderable: {}", yes(self.is_renderable()));
206        log::info!(target: log_target::GENERAL, "Tile::complete: {}", yes(self.is_complete()));
207        log::info!(target: log_target::GENERAL, "Tile::loaded: {}", yes(self.settled));
208    }
209}