1use std::marker::PhantomData;
11use std::sync::Arc;
12
13use mln_style::bitmap::PremultipliedImage;
14use mln_style::layer::{SourceKind, StyleLayerType};
15
16use crate::draw::identity::SimpleIdentity;
17use crate::draw::tile_render_data::TileAtlasTextures;
18use crate::layer::bucket::Bucket;
19use crate::layer::bucket_geometry::BucketGeometry;
20use crate::layer::layer_type::EvaluatedLayer;
21use crate::layer::layout::Layout;
22use crate::layer::render_layer::Layer;
23use crate::query::feature_index::FeatureIndex;
24use crate::render::tile::{TileContent, ViewPlace};
25use crate::source::render_image_source::{ImageQuad, ImageTile};
26use crate::source::{DemTiles, RasterTiles};
27use crate::text::atlas::ImagePosition;
28use crate::tile_store::contents::GeometryContents;
29use crate::tile_store::dem::DemContents;
30use crate::tile_store::raster::RasterContents;
31
32#[expect(private_bounds, reason = "the engine's tile kinds are the only ones: `input::Find` seals the trait")]
35pub trait TileInput<T: StyleLayerType>: input::Find<T> + 'static {
36 type Data<'a>: Copy;
38}
39
40pub type TileData<'a, L> = <<L as Layer>::Input as TileInput<<L as Layer>::Style>>::Data<'a>;
43
44pub struct Features<L>(PhantomData<fn() -> L>);
47
48impl<T: StyleLayerType, L: Layout<T>> TileInput<T> for Features<L> {
49 type Data<'a> = LayerBucket<'a, T, L::Bucket>;
50}
51
52pub enum NoTiles {}
55
56impl<T: StyleLayerType> TileInput<T> for NoTiles {
57 type Data<'a> = std::convert::Infallible;
58}
59
60impl<T: StyleLayerType> TileInput<T> for RasterTiles {
63 type Data<'a> = RasterTile<'a>;
64}
65
66impl<T: StyleLayerType> TileInput<T> for DemTiles {
68 type Data<'a> = &'a DemContents;
69}
70
71pub struct LayerBucket<'a, T: StyleLayerType, B> {
75 pub bucket: &'a B,
76 pub(crate) id: SimpleIdentity,
78 pub bucket_index: usize,
81 pub layer: &'a EvaluatedLayer<T>,
83 contents: &'a GeometryContents,
84}
85
86impl<T: StyleLayerType, B> Clone for LayerBucket<'_, T, B> {
87 fn clone(&self) -> Self {
88 *self
89 }
90}
91
92impl<T: StyleLayerType, B> Copy for LayerBucket<'_, T, B> {}
93
94impl<'a, T: StyleLayerType, B: Bucket> LayerBucket<'a, T, B> {
95 pub(crate) fn laid(contents: &'a GeometryContents, index: usize) -> Option<Self> {
98 let laid = contents.result.layer_render_data.laid(index)?;
99 let layer: &dyn std::any::Any = laid.layer;
100 Some(LayerBucket {
101 bucket: laid.bucket.downcast_ref::<B>()?,
102 id: laid.bucket.id(),
103 bucket_index: laid.bucket_index,
104 layer: layer.downcast_ref()?,
105 contents,
106 })
107 }
108}
109
110impl<'a, T: StyleLayerType, B> LayerBucket<'a, T, B> {
111 #[must_use]
114 pub fn geometry(self) -> &'a BucketGeometry {
115 &self.contents.bucket_geometry
116 }
117
118 #[must_use]
121 pub fn feature_index(self) -> &'a Arc<FeatureIndex> {
122 &self.contents.result.feature_index
123 }
124
125 #[must_use]
127 pub fn pattern(self, pattern: &str) -> Option<ImagePosition> {
128 self.contents.pattern_positions()?.get(pattern).cloned()
129 }
130
131 #[must_use]
133 pub fn atlas_textures(self) -> &'a TileAtlasTextures {
134 &self.contents.textures
135 }
136}
137
138#[derive(Clone, Copy)]
141pub enum RasterTile<'a> {
142 Tile(&'a RasterContents),
145 Image(&'a ImageTile),
148}
149
150#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
153pub struct ImageId(SimpleIdentity);
154
155impl ImageId {
156 pub(crate) fn new(id: SimpleIdentity) -> Self {
157 ImageId(id)
158 }
159}
160
161impl<'a> RasterTile<'a> {
162 #[must_use]
164 pub fn id(self) -> ImageId {
165 ImageId(match self {
166 RasterTile::Tile(raster) => raster.id(),
167 RasterTile::Image(image) => image.id(),
168 })
169 }
170
171 #[must_use]
173 pub fn image(self) -> &'a PremultipliedImage {
174 match self {
175 RasterTile::Tile(raster) => &raster.image,
176 RasterTile::Image(image) => &image.image,
177 }
178 }
179
180 #[must_use]
184 pub fn quad(self) -> ImageQuad {
185 match self {
186 RasterTile::Tile(_) => ImageQuad::TILE,
187 RasterTile::Image(image) => image.quad,
188 }
189 }
190}
191
192pub(crate) mod input {
193 use std::sync::Arc;
194
195 use mln_style::error::Error;
196 use mln_wire::Reader;
197
198 use super::{
199 DemTiles, Features, LayerBucket, NoTiles, RasterTile, RasterTiles, SourceKind, StyleLayerType, TileContent,
200 TileInput, ViewPlace,
201 };
202 use crate::layer::bucket::{ErasedBucket, Stored};
203 use crate::layer::layer_drawing::MadeFrom;
204 use crate::layer::layer_type::{self, ErasedEvaluatedLayer, ErasedLayout};
205 use crate::layer::layout::{Layout, LayoutParameters};
206 use crate::render::pass::RenderPass;
207 use crate::source::GeometryTiles;
208 use crate::source::kind::Deliver;
209 use crate::tile_store::contents::{Contents, GeometryContents};
210
211 pub(crate) trait Find<T: StyleLayerType> {
216 const KIND: SourceKind;
218
219 fn needs_resources(_group: &[Arc<dyn ErasedEvaluatedLayer>]) -> bool {
222 false
223 }
224
225 fn create_layout(
230 _group: &[Arc<dyn ErasedEvaluatedLayer>],
231 _parameters: LayoutParameters<'_>,
232 ) -> Result<Option<Box<dyn ErasedLayout>>, Error> {
233 Ok(None)
234 }
235
236 fn read_bucket(r: &mut Reader<'_>) -> Option<Box<dyn ErasedBucket>> {
238 r.fail("a bucket of a layer type that doesn't lay out")
239 }
240
241 fn laid<'a>(_contents: &'a GeometryContents, _layer_id: &str) -> Option<<Self as TileInput<T>>::Data<'a>>
244 where
245 Self: TileInput<T>,
246 {
247 None
248 }
249
250 fn find<'a>(content: TileContent<'a>, layer_id: &str) -> Option<(<Self as TileInput<T>>::Data<'a>, ViewPlace)>
253 where
254 Self: TileInput<T>;
255
256 fn at(content: TileContent<'_>, place: ViewPlace) -> Option<<Self as TileInput<T>>::Data<'_>>
259 where
260 Self: TileInput<T>;
261
262 fn made_from(data: <Self as TileInput<T>>::Data<'_>) -> MadeFrom
265 where
266 Self: TileInput<T>;
267
268 fn is_for(_data: <Self as TileInput<T>>::Data<'_>, _layer_id: &str) -> bool
271 where
272 Self: TileInput<T>,
273 {
274 true
275 }
276
277 fn passes(_data: <Self as TileInput<T>>::Data<'_>) -> RenderPass
280 where
281 Self: TileInput<T>,
282 {
283 RenderPass::NONE
284 }
285 }
286
287 impl<T: StyleLayerType, L: Layout<T>> Find<T> for Features<L> {
288 const KIND: SourceKind = <GeometryTiles as Deliver>::KIND;
289
290 fn needs_resources(group: &[Arc<dyn ErasedEvaluatedLayer>]) -> bool {
291 layer_type::needs_resources::<T, L>(group)
292 }
293
294 fn create_layout(
295 group: &[Arc<dyn ErasedEvaluatedLayer>],
296 parameters: LayoutParameters<'_>,
297 ) -> Result<Option<Box<dyn ErasedLayout>>, Error> {
298 layer_type::create_layout::<T, L>(group, parameters).map(Some)
299 }
300
301 fn read_bucket(r: &mut Reader<'_>) -> Option<Box<dyn ErasedBucket>> {
302 Some(Box::new(Stored::new(<L::Bucket as mln_wire::Wire>::read(r)?)))
303 }
304
305 fn laid<'a>(contents: &'a GeometryContents, layer_id: &str) -> Option<<Self as TileInput<T>>::Data<'a>> {
306 LayerBucket::laid(contents, contents.result.layer_render_data.find(layer_id)?)
307 }
308
309 fn find<'a>(content: TileContent<'a>, layer_id: &str) -> Option<(<Self as TileInput<T>>::Data<'a>, ViewPlace)> {
310 let TileContent::Tile(contents) = content else { return None };
311 let contents = contents.geometry()?;
312 let index = contents.result.layer_render_data.find(layer_id)?;
313 Some((LayerBucket::laid(contents, index)?, ViewPlace::Laid(index)))
314 }
315
316 fn at(content: TileContent<'_>, place: ViewPlace) -> Option<<Self as TileInput<T>>::Data<'_>> {
317 let ViewPlace::Laid(index) = place else {
318 debug_assert!(false, "a tile's own contents' place for a laid-out layer");
319 return None;
320 };
321 let TileContent::Tile(contents) = content else { return None };
322 LayerBucket::laid(contents.geometry()?, index)
323 }
324
325 fn made_from(data: <Self as TileInput<T>>::Data<'_>) -> MadeFrom {
326 (data.id, [0, 0])
327 }
328
329 fn is_for(data: <Self as TileInput<T>>::Data<'_>, layer_id: &str) -> bool {
330 data.layer.style.id == layer_id
331 }
332
333 fn passes(data: <Self as TileInput<T>>::Data<'_>) -> RenderPass {
334 RenderPass(data.layer.render_passes)
335 }
336 }
337
338 impl<T: StyleLayerType> Find<T> for RasterTiles {
339 const KIND: SourceKind = <RasterTiles as Deliver>::KIND;
340
341 fn find<'a>(
342 content: TileContent<'a>,
343 _layer_id: &str,
344 ) -> Option<(<Self as TileInput<T>>::Data<'a>, ViewPlace)> {
345 Some((<Self as Find<T>>::at(content, ViewPlace::Own)?, ViewPlace::Own))
346 }
347
348 fn at(content: TileContent<'_>, place: ViewPlace) -> Option<<Self as TileInput<T>>::Data<'_>> {
349 debug_assert_eq!(place, ViewPlace::Own, "a laid-out layer's place for a tile's own contents");
350 match content {
351 TileContent::Tile(Contents::Raster(raster)) => Some(RasterTile::Tile(raster)),
352 TileContent::Image(image) => Some(RasterTile::Image(image)),
353 TileContent::Tile(_) => None,
354 }
355 }
356
357 fn made_from(data: <Self as TileInput<T>>::Data<'_>) -> MadeFrom {
358 match data {
359 RasterTile::Tile(raster) => raster.stamp(),
360 RasterTile::Image(image) => image.stamp(),
361 }
362 }
363 }
364
365 impl<T: StyleLayerType> Find<T> for DemTiles {
366 const KIND: SourceKind = <DemTiles as Deliver>::KIND;
367
368 fn find<'a>(
369 content: TileContent<'a>,
370 _layer_id: &str,
371 ) -> Option<(<Self as TileInput<T>>::Data<'a>, ViewPlace)> {
372 Some((<Self as Find<T>>::at(content, ViewPlace::Own)?, ViewPlace::Own))
373 }
374
375 fn at(content: TileContent<'_>, place: ViewPlace) -> Option<<Self as TileInput<T>>::Data<'_>> {
376 debug_assert_eq!(place, ViewPlace::Own, "a laid-out layer's place for a tile's own contents");
377 match content {
378 TileContent::Tile(Contents::Dem(dem)) => Some(dem),
379 _ => None,
380 }
381 }
382
383 fn made_from(data: <Self as TileInput<T>>::Data<'_>) -> MadeFrom {
384 data.stamp()
385 }
386 }
387
388 impl<T: StyleLayerType> Find<T> for NoTiles {
389 const KIND: SourceKind = SourceKind::None;
390
391 fn find<'a>(
392 _content: TileContent<'a>,
393 _layer_id: &str,
394 ) -> Option<(<Self as TileInput<T>>::Data<'a>, ViewPlace)> {
395 None
396 }
397
398 fn at(_content: TileContent<'_>, _place: ViewPlace) -> Option<<Self as TileInput<T>>::Data<'_>> {
399 None
400 }
401
402 fn made_from(data: <Self as TileInput<T>>::Data<'_>) -> MadeFrom {
403 match data {}
404 }
405 }
406}