Skip to main content

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