1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// C++ `Bucket` is an abstract class with data members: the data is
// `BucketBase`, the virtual interface is the `Bucket` trait. Buckets are
// built on the worker thread and then shared with the render thread
// (`std::shared_ptr<Bucket>` in `LayerRenderData`): `BucketPtr = Arc<dyn
// Bucket>`, `Bucket: Send + Sync`. The render thread only reads them, except
// `upload` (an atomic flag, as in C++) and the geometry it moves out to draw
// (bucket_geometry.rs).
use std::any::Any;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use mln_style_types::image::ImagePositions;
use mln_tile::TileLayer;
use mln_tile_id::CanonicalTileID;
use mln_tile_id::geometry_tile_data::{GeometryCollection, GeometryTileFeature};
use mln_util::error::Error;
use mln_util::identity::SimpleIdentity;
use mln_wire::{Reader, Wire, Writer};
use crate::gfx::context::Context;
use crate::layout::pattern_layout::PatternLayerMap;
pub(crate) type BucketPtr = Arc<dyn Bucket>;
pub(crate) type FeatureStates = mln_util::hash::HashMap<String, mln_style::expression::expression::FeatureState>;
pub type LayerFeatureStates = mln_util::hash::HashMap<String, FeatureStates>;
/// The data members of `Bucket`.
///
/// Port-only: the codec writes what a worker sets (`tile/worker_codec`); the
/// upload flag, the id and the version are the side's that has the bucket.
#[derive(Debug, mln_wire::Wire)]
pub(crate) struct BucketBase {
#[wire(skip)]
pub(crate) uploaded: AtomicBool,
#[wire(skip, default = "SimpleIdentity::new")]
pub(crate) bucket_id: SimpleIdentity,
/// Bumped by `update` (feature state); the render layers compare it to
/// refresh their drawables' copies of the vertex data. C++ updates the
/// shared vertex vectors in place instead.
#[wire(skip)]
pub(crate) version: AtomicU64,
}
impl Default for BucketBase {
fn default() -> Self {
BucketBase { version: AtomicU64::new(0), uploaded: AtomicBool::new(false), bucket_id: SimpleIdentity::new() }
}
}
/// The error of a polygon with more vertices than a 16-bit index can
/// address. The tile that holds it fails to lay out.
#[must_use]
pub(crate) fn geometry_too_long() -> Error {
Error::native("a polygon has more vertices than a 16-bit index can address")
}
pub(crate) trait Bucket: Any + Send + Sync {
fn base(&self) -> &BucketBase;
/// Feature geometries are also used to populate the feature index.
/// Obtaining these is a costly operation, so we do it only once, and
/// pass-by-const-ref the geometries as a second parameter.
///
/// Fails with [`geometry_too_long`] for a polygon that a bucket can't
/// index; the worker reports it as the tile's error.
fn add_feature(
&mut self,
_feature: &dyn GeometryTileFeature,
_geometries: &GeometryCollection,
_image_positions: &ImagePositions,
_patterns: &PatternLayerMap,
_index: usize,
_canonical: &CanonicalTileID,
) -> Result<(), Error> {
Ok(())
}
/// Re-evaluates the feature-state dependent attribute values. Takes
/// `&self` because the bucket is shared (see the file comment);
/// implementations lock their binders.
fn update(
&self,
_states: &FeatureStates,
_layer: &TileLayer<'_>,
_layer_id: &str,
_image_positions: &ImagePositions,
) {
}
/// How far above the ground the bucket's highest content is raised,
/// for a layer whose content elevation is an expression.
fn max_content_elevation(&self) -> f32 {
0.0
}
/// The `version` counter (see `BucketBase::version`).
fn get_version(&self) -> u64 {
self.base().version.load(Ordering::SeqCst)
}
fn record_sdf_pattern(&mut self, _layer_id: &str, _sdf: bool) {}
fn is_sdf_pattern(&self, _layer_id: &str) -> bool {
false
}
/// As long as this bucket has a Prepare render pass, this function is
/// getting called. Typically, this only happens once when the bucket is
/// being rendered for the first time.
fn upload(&self, context: &Context);
fn has_data(&self) -> bool;
/// Writes the bucket for its layer type's
/// [`read_bucket`](crate::layer_type::ErasedLayerType::read_bucket).
/// A bucket that no layout makes fails `w`.
fn write_wire(&self, w: &mut Writer);
fn needs_upload(&self) -> bool {
self.has_data() && !self.base().uploaded.load(Ordering::SeqCst)
}
fn get_id(&self) -> SimpleIdentity {
self.base().bucket_id
}
}
/// The bucket of a layer type that doesn't lay out: there is no value.
pub(crate) enum NoBucket {}
impl Bucket for NoBucket {
fn base(&self) -> &BucketBase {
match *self {}
}
fn upload(&self, _context: &Context) {
match *self {}
}
fn has_data(&self) -> bool {
match *self {}
}
fn write_wire(&self, _w: &mut Writer) {
match *self {}
}
}
impl Wire for NoBucket {
fn write(&self, _w: &mut Writer) {
match *self {}
}
fn read(r: &mut Reader<'_>) -> Option<Self> {
r.fail("a bucket of a layer type that doesn't lay out")
}
}
impl dyn Bucket {
/// Downcasts to the bucket type `B`; `None` if it is another type.
pub(crate) fn downcast_ref<B: Bucket>(&self) -> Option<&B> {
let any: &dyn Any = self;
any.downcast_ref()
}
}