Skip to main content

mln_engine/source/
render_source.rs

1// A render source is the renderer's side of a style source: what every one
2// has is `RenderSourceBase`, what each type does the `RenderSource` trait. A
3// source's tiles are in the renderer's tile store, which the source's
4// methods are given.
5
6use std::sync::Arc;
7
8use mln_style::Source;
9use mln_style::error::Error;
10
11use crate::draw::context::Context;
12use crate::layer::layer_type::ErasedEvaluatedLayer;
13use crate::registry::Registry;
14use crate::render::paint_parameters::TransformParameters;
15use crate::render::tile::{RenderTile, TileContent};
16use crate::render::update_parameters::UpdateParameters;
17use crate::source::Supplied;
18use crate::source::render_image_source::RenderImageSource;
19use crate::source::render_tile_source::RenderTileSource;
20use crate::tile_store::store::TileStore;
21use crate::tile_store::tile_parameters::TileParameters;
22use crate::worker::job::SourceKey;
23
24pub(crate) struct RenderTileParameters<'a> {
25    pub(crate) transform: &'a TransformParameters,
26    /// Whether a layer of the frame draws the source's tiles in
27    /// [`TileOrder::ByY`], which `update_render_tiles` then sorts them in.
28    pub(crate) sort_by_y: bool,
29}
30
31/// What every render source has, whatever its type.
32pub(crate) struct RenderSourceBase {
33    pub(crate) style_source: Source,
34    pub(crate) enabled: bool,
35}
36
37impl RenderSourceBase {
38    pub(crate) fn new(style_source: Source) -> Self {
39        RenderSourceBase { style_source, enabled: false }
40    }
41}
42
43/// An order a layer draws its source's tiles in.
44#[derive(Clone, Copy, Debug, PartialEq, Eq)]
45pub enum TileOrder {
46    /// By tile id, without the tiles held for fade.
47    ById,
48    /// By opposite y position, so that the lower of two tiles' overlapping
49    /// symbols draw over the higher's.
50    ByY,
51}
52
53pub(crate) trait RenderSource {
54    fn base(&self) -> &RenderSourceBase;
55
56    fn is_loaded(&self, store: &TileStore) -> bool;
57
58    fn update(
59        &mut self,
60        store: &mut TileStore,
61        style_source: Source,
62        layers: &[Arc<dyn ErasedEvaluatedLayer>],
63        needs_rendering: bool,
64        needs_relayout: bool,
65        parameters: &TileParameters<'_>,
66    );
67
68    /// Makes the source's render tiles of the frame.
69    fn update_render_tiles(&mut self, store: &mut TileStore, parameters: &RenderTileParameters<'_>);
70
71    /// Readies the buckets the frame draws, before its layers make their
72    /// draws, which read what `needs` says, uploading their indexes through
73    /// `context`.
74    fn upload(&mut self, store: &mut TileStore, context: &mut Context);
75
76    /// Releases the render data from the last `update_render_tiles` while
77    /// the source is disabled.
78    fn release_render_data(&mut self) {}
79
80    /// The source's tiles in the tile store, for a source with tiles.
81    fn tiles(&self) -> Option<SourceKey> {
82        None
83    }
84
85    /// The source's render tiles, as of the last `update_render_tiles`.
86    fn render_tiles(&self) -> &[RenderTile] {
87        &[]
88    }
89
90    /// The source's tiles in `order`, as indexes into `render_tiles`, as of
91    /// the last `update_render_tiles`; `None` for a source without tiles.
92    fn tile_order(&self, _order: TileOrder) -> Option<&[usize]> {
93        None
94    }
95
96    /// Adds to `contents` what each of the source's render tiles shows, at
97    /// its index, from `store`.
98    fn tile_contents<'a>(&'a self, _store: &'a TileStore, _contents: &mut Vec<Option<TileContent<'a>>>) {}
99
100    /// Queries a feature extension; sources without extensions return a null
101    /// value.
102    fn query_feature_extensions(
103        &self,
104        _feature: &crate::tile::feature::Feature,
105        _extension: &str,
106        _extension_field: &str,
107        _args: Option<&mln_style::feature::PropertyMap>,
108    ) -> Result<crate::query::FeatureExtensionValue, Error> {
109        Ok(crate::query::FeatureExtensionValue::default())
110    }
111
112    /// Has the source's origin take in `style_source`, as `update` does
113    /// first, so that its tiles' cover is of that version: for a source
114    /// another part of the update reads before the sources update.
115    fn update_origin(&mut self, _store: &mut TileStore, _style_source: &Source, _parameters: &UpdateParameters) {}
116
117    fn set_fast_pfor_enabled(&mut self, _enable: bool) {}
118
119    fn dump_debug_logs(&self);
120
121    /// The deepest zoom the source has tiles of; `None` for a source without
122    /// tiles.
123    fn max_zoom(&self) -> Option<u8>;
124
125    fn is_enabled(&self) -> bool {
126        self.base().enabled
127    }
128}
129
130/// Creates the render source of a style source through its type in
131/// `registry`, whose tiles, if it has any, `store` keeps. `None` for a type
132/// that the registry lacks, which breaks the contract that a map and its
133/// renderer register the same types.
134pub(crate) fn create(registry: &Registry, source: &Source, store: &mut TileStore) -> Option<Box<dyn RenderSource>> {
135    let name = source.info().name;
136    let render_source = registry.source_type(name).map(|source_type| -> Box<dyn RenderSource> {
137        match source_type.supply(source) {
138            Supplied::Tiles(origin) => Box::new(RenderTileSource::new(source.clone(), source_type, origin, store)),
139            Supplied::Image(image_of) => Box::new(RenderImageSource::new(source.clone(), image_of)),
140        }
141    });
142    debug_assert!(render_source.is_some(), "the renderer's registry has no source type '{name}'");
143    render_source
144}