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