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::rc::Rc;
5use std::sync::Arc;
6use std::time::Duration;
7
8use mln_map_types::MapMode;
9use mln_storage::Data;
10use mln_tile::TileData;
11use mln_tile_id::OverscaledTileID;
12use mln_util::logging::{Event, Log};
13
14use crate::layout::tile_layout::Dependencies;
15use crate::tile::contents::Contents;
16use crate::tile::loading::Load;
17use crate::tile::pipeline::{Layout, Pipeline};
18
19/// An optional tile only takes low-cost actions to get its data, such as
20/// loading stale data from the cache; a required one loads it from the
21/// network and keeps it fresh.
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub(crate) enum TileNecessity {
24    Optional,
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
89#[derive(Clone, Copy, Debug, PartialEq, Eq)]
90pub(crate) struct TileUpdateParameters {
91    pub(crate) minimum_update_interval: Duration,
92    pub(crate) is_volatile: bool,
93}
94
95#[derive(Clone, Copy, Debug, PartialEq)]
96pub(crate) enum TileKind {
97    Geometry,
98    Raster,
99    Dem(mln_util::tileset::RasterEncoding),
100}
101
102/// What a load delivers. `data` is `None` for a tile without any (204, no
103/// tile in an archive); `missing`, the tileset has no tile here, so a tile
104/// of another zoom level draws in its place.
105pub(crate) enum TileInput {
106    Geometry { data: Option<TileData>, missing: bool },
107    Image { data: Option<Data>, missing: bool },
108}
109
110pub(crate) struct Shown {
111    pub(crate) contents: Rc<Contents>,
112    /// The feature state version the contents' buckets hold; 0 for none.
113    pub(crate) feature_state_version: u64,
114    /// The layout's answer resolved glyphs or images that changed since: no
115    /// first try starts with it.
116    pub(crate) answer_outdated: bool,
117    /// The contents are those of the tile in another world copy, without
118    /// the layers that are placed, which the tile has yet to lay out.
119    pub(crate) world_copy: bool,
120}
121
122impl Shown {
123    pub(crate) fn new(contents: Contents, feature_state_version: u64) -> Self {
124        Shown { contents: Rc::new(contents), feature_state_version, answer_outdated: false, world_copy: false }
125    }
126}
127
128impl crate::source_state::FeatureStateTarget for Shown {
129    fn feature_state_version(&mut self) -> &mut u64 {
130        &mut self.feature_state_version
131    }
132
133    fn set_feature_state(&self, states: &crate::bucket::LayerFeatureStates) {
134        if let Some(contents) = self.contents.geometry() {
135            contents.set_feature_state(states);
136        }
137    }
138}
139
140/// Where a render entry stands in fading its symbols out: it is held for
141/// two placements, the first starting the fade and the second ending it.
142#[derive(Clone, Copy, Debug, PartialEq, Eq)]
143pub(crate) enum FadeState {
144    Loaded,
145    NeedsFirstPlacement,
146    NeedsSecondPlacement,
147    CanRemove,
148}
149
150impl FadeState {
151    /// An entry the last update rendered, but not this one.
152    #[must_use]
153    pub(crate) fn rendered_previously(self) -> Self {
154        match self {
155            FadeState::Loaded => FadeState::NeedsFirstPlacement,
156            other => other,
157        }
158    }
159
160    #[must_use]
161    pub(crate) fn stepped(self) -> Self {
162        match self {
163            FadeState::NeedsFirstPlacement => FadeState::NeedsSecondPlacement,
164            FadeState::NeedsSecondPlacement => FadeState::CanRemove,
165            other => other,
166        }
167    }
168
169    #[must_use]
170    pub(crate) fn holds(self) -> bool {
171        matches!(self, FadeState::NeedsFirstPlacement | FadeState::NeedsSecondPlacement)
172    }
173}
174
175pub(crate) struct Tile {
176    pub(crate) id: OverscaledTileID,
177    pub(crate) load: Load,
178    pub(crate) pipeline: Pipeline,
179    pub(crate) shown: Option<Shown>,
180    /// The tile has shown contents, failed, or heard its data is still fresh
181    /// since it last started loading: it has done what it can for now.
182    pub(super) settled: bool,
183}
184
185impl Tile {
186    pub(crate) fn new(id: OverscaledTileID, load: Load, pipeline: Pipeline) -> Self {
187        Tile { id, load, pipeline, shown: None, settled: false }
188    }
189
190    pub(crate) fn contents(&self) -> Option<&Contents> {
191        self.shown.as_ref().map(|shown| &*shown.contents)
192    }
193
194    /// Whether the tile can draw, if only some of its layers.
195    pub(crate) fn is_renderable(&self) -> bool {
196        self.contents().is_some_and(Contents::is_renderable)
197    }
198
199    pub(crate) fn is_settled(&self) -> bool {
200        self.settled
201    }
202
203    pub(crate) fn is_pending(&self) -> bool {
204        self.pipeline.is_pending(self.settled)
205    }
206
207    /// Whether the tile has done all it can to become renderable, which it
208    /// may not be.
209    pub(crate) fn is_complete(&self) -> bool {
210        self.settled && !self.is_pending()
211    }
212
213    pub(crate) fn has_work(&self) -> bool {
214        self.pipeline.has_work()
215    }
216
217    /// Whether the tile's render entries are held after they're no longer
218    /// ideal, so that its symbols fade out.
219    pub(crate) fn fades_out(&self) -> bool {
220        self.pipeline.layout().is_some_and(|layout| layout.params.mode == MapMode::Continuous)
221    }
222
223    /// Calls `f` with the dependencies of the layout shown and of the one
224    /// under way.
225    pub(crate) fn dependencies(&self, mut f: impl FnMut(&Dependencies)) {
226        if let Some(contents) = self.contents().and_then(Contents::geometry) {
227            f(&contents.deps);
228        }
229        if let Some(deps) = self.pipeline.layout().and_then(Layout::dependencies) {
230            f(deps);
231        }
232    }
233
234    /// Returns whether the tile draws the layer.
235    pub(crate) fn layer_properties_updated(
236        &mut self,
237        layer_properties: &Arc<dyn crate::layer_type::ErasedEvaluatedLayer>,
238    ) -> bool {
239        self.shown
240            .as_mut()
241            .is_some_and(|shown| Contents::layer_properties_updated(&mut shown.contents, layer_properties))
242    }
243
244    pub(crate) fn dump_debug_logs(&self, kind: TileKind) {
245        let kind = match kind {
246            TileKind::Geometry => "Geometry",
247            TileKind::Raster => "Raster",
248            TileKind::Dem(_) => "RasterDEM",
249        };
250        let yes = |value: bool| if value { "yes" } else { "no" };
251        Log::info(Event::General, format!("Tile::Kind: {kind}"));
252        Log::info(Event::General, format!("Tile::id: {}", self.id));
253        Log::info(Event::General, format!("Tile::renderable: {}", yes(self.is_renderable())));
254        Log::info(Event::General, format!("Tile::complete: {}", yes(self.is_complete())));
255        Log::info(Event::General, format!("Tile::loaded: {}", yes(self.settled)));
256    }
257}