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