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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
#include <mbgl/tile/geometry_tile.hpp>
#include <mbgl/tile/geometry_tile_worker.hpp>
#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/tile/tile_observer.hpp>
#include <mbgl/style/layer_impl.hpp>
#include <mbgl/style/layers/background_layer.hpp>
#include <mbgl/style/layers/custom_layer.hpp>
#include <mbgl/renderer/tile_parameters.hpp>
#include <mbgl/renderer/layers/render_background_layer.hpp>
#include <mbgl/renderer/layers/render_custom_layer.hpp>
#include <mbgl/renderer/layers/render_symbol_layer.hpp>
#include <mbgl/renderer/buckets/symbol_bucket.hpp>
#include <mbgl/renderer/query.hpp>
#include <mbgl/text/glyph_atlas.hpp>
#include <mbgl/renderer/image_atlas.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/geometry/feature_index.hpp>
#include <mbgl/map/transform_state.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/actor/scheduler.hpp>
#include <iostream>
namespace mbgl {
using namespace style;
/*
Correlation between GeometryTile and GeometryTileWorker is safeguarded by two
correlation schemes:
GeometryTile's 'correlationID' is used for ensuring the tile will be flagged
as non-pending only when the placement coming from the last operation (as in
'setData', 'setLayers', 'setShowCollisionBoxes') occurs. This is important for
still mode rendering as we want to render only when all layout and placement
operations are completed.
GeometryTileWorker's 'imageCorrelationID' is used for checking whether an
image request reply coming from `GeometryTile` is valid. Previous image
request replies are ignored as they result in incomplete placement attempts
that could flag the tile as non-pending too early.
*/
GeometryTile::GeometryTile(const OverscaledTileID& id_,
std::string sourceID_,
const TileParameters& parameters)
: Tile(id_),
sourceID(std::move(sourceID_)),
mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())),
worker(parameters.workerScheduler,
ActorRef<GeometryTile>(*this, mailbox),
id_,
sourceID,
obsolete,
parameters.mode,
parameters.pixelRatio,
parameters.debugOptions & MapDebugOptions::Collision),
glyphManager(parameters.glyphManager),
imageManager(parameters.imageManager),
mode(parameters.mode),
showCollisionBoxes(parameters.debugOptions & MapDebugOptions::Collision) {
}
GeometryTile::~GeometryTile() {
glyphManager.removeRequestor(*this);
imageManager.removeRequestor(*this);
markObsolete();
}
void GeometryTile::cancel() {
markObsolete();
}
void GeometryTile::markObsolete() {
obsolete = true;
}
void GeometryTile::setError(std::exception_ptr err) {
loaded = true;
observer->onTileError(*this, err);
}
void GeometryTile::setData(std::unique_ptr<const GeometryTileData> data_) {
// Mark the tile as pending again if it was complete before to prevent signaling a complete
// state despite pending parse operations.
pending = true;
++correlationID;
worker.self().invoke(&GeometryTileWorker::setData, std::move(data_), correlationID);
}
void GeometryTile::setLayers(const std::vector<Immutable<Layer::Impl>>& layers) {
// Mark the tile as pending again if it was complete before to prevent signaling a complete
// state despite pending parse operations.
pending = true;
std::vector<Immutable<Layer::Impl>> impls;
for (const auto& layer : layers) {
// Skip irrelevant layers.
if (layer->type == LayerType::Background ||
layer->type == LayerType::Custom ||
layer->source != sourceID ||
id.overscaledZ < std::floor(layer->minZoom) ||
id.overscaledZ >= std::ceil(layer->maxZoom) ||
layer->visibility == VisibilityType::None) {
continue;
}
impls.push_back(layer);
}
++correlationID;
worker.self().invoke(&GeometryTileWorker::setLayers, std::move(impls), correlationID);
}
void GeometryTile::setShowCollisionBoxes(const bool showCollisionBoxes_) {
if (showCollisionBoxes != showCollisionBoxes_) {
showCollisionBoxes = showCollisionBoxes_;
++correlationID;
worker.self().invoke(&GeometryTileWorker::setShowCollisionBoxes, showCollisionBoxes, correlationID);
}
}
void GeometryTile::onLayout(LayoutResult result, const uint64_t resultCorrelationID) {
loaded = true;
renderable = true;
if (resultCorrelationID == correlationID) {
pending = false;
}
buckets = std::move(result.buckets);
latestFeatureIndex = std::move(result.featureIndex);
if (result.glyphAtlasImage) {
glyphAtlasImage = std::move(*result.glyphAtlasImage);
}
if (result.iconAtlasImage) {
iconAtlasImage = std::move(*result.iconAtlasImage);
}
observer->onTileChanged(*this);
}
void GeometryTile::onError(std::exception_ptr err, const uint64_t resultCorrelationID) {
loaded = true;
if (resultCorrelationID == correlationID) {
pending = false;
}
observer->onTileError(*this, err);
}
void GeometryTile::onGlyphsAvailable(GlyphMap glyphs) {
worker.self().invoke(&GeometryTileWorker::onGlyphsAvailable, std::move(glyphs));
}
void GeometryTile::getGlyphs(GlyphDependencies glyphDependencies) {
glyphManager.getGlyphs(*this, std::move(glyphDependencies));
}
void GeometryTile::onImagesAvailable(ImageMap images, uint64_t imageCorrelationID) {
worker.self().invoke(&GeometryTileWorker::onImagesAvailable, std::move(images), imageCorrelationID);
}
void GeometryTile::getImages(ImageRequestPair pair) {
imageManager.getImages(*this, std::move(pair));
}
void GeometryTile::upload(gl::Context& context) {
auto uploadFn = [&] (Bucket& bucket) {
if (bucket.needsUpload()) {
bucket.upload(context);
}
};
for (auto& entry : buckets) {
uploadFn(*entry.second);
}
if (glyphAtlasImage) {
glyphAtlasTexture = context.createTexture(*glyphAtlasImage, 0);
glyphAtlasImage = {};
}
if (iconAtlasImage) {
iconAtlasTexture = context.createTexture(*iconAtlasImage, 0);
iconAtlasImage = {};
}
}
Bucket* GeometryTile::getBucket(const Layer::Impl& layer) const {
const auto it = buckets.find(layer.id);
if (it == buckets.end()) {
return nullptr;
}
assert(it->second);
return it->second.get();
}
float GeometryTile::getQueryPadding(const std::vector<const RenderLayer*>& layers) {
float queryPadding = 0;
for (const RenderLayer* layer : layers) {
auto bucket = getBucket(*layer->baseImpl);
if (bucket && bucket->hasData()) {
queryPadding = std::max(queryPadding, bucket->getQueryRadius(*layer));
}
}
return queryPadding;
}
void GeometryTile::queryRenderedFeatures(
std::unordered_map<std::string, std::vector<Feature>>& result,
const GeometryCoordinates& queryGeometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const RenderedQueryOptions& options,
const mat4& projMatrix) {
if (!getData()) return;
const float queryPadding = getQueryPadding(layers);
mat4 posMatrix;
transformState.matrixFor(posMatrix, id.toUnwrapped());
matrix::multiply(posMatrix, projMatrix, posMatrix);
latestFeatureIndex->query(result,
queryGeometry,
transformState,
posMatrix,
util::tileSize * id.overscaleFactor(),
std::pow(2, transformState.getZoom() - id.overscaledZ),
options,
id.toUnwrapped(),
layers,
queryPadding * transformState.maxPitchScaleFactor());
}
void GeometryTile::querySourceFeatures(
std::vector<Feature>& result,
const SourceQueryOptions& options) {
// Data not yet available, or tile is empty
if (!getData()) {
return;
}
// No source layers, specified, nothing to do
if (!options.sourceLayers) {
Log::Warning(Event::General, "At least one sourceLayer required");
return;
}
for (auto sourceLayer : *options.sourceLayers) {
// Go throught all sourceLayers, if any
// to gather all the features
auto layer = getData()->getLayer(sourceLayer);
if (layer) {
auto featureCount = layer->featureCount();
for (std::size_t i = 0; i < featureCount; i++) {
auto feature = layer->getFeature(i);
// Apply filter, if any
if (options.filter && !(*options.filter)(style::expression::EvaluationContext { static_cast<float>(this->id.overscaledZ), feature.get() })) {
continue;
}
result.push_back(convertFeature(*feature, id.canonical));
}
}
}
}
bool GeometryTile::holdForFade() const {
return mode == MapMode::Continuous &&
(fadeState == FadeState::NeedsFirstPlacement || fadeState == FadeState::NeedsSecondPlacement);
}
void GeometryTile::markRenderedIdeal() {
fadeState = FadeState::Loaded;
}
void GeometryTile::markRenderedPreviously() {
if (fadeState == FadeState::Loaded) {
fadeState = FadeState::NeedsFirstPlacement;
}
}
void GeometryTile::performedFadePlacement() {
if (fadeState == FadeState::NeedsFirstPlacement) {
fadeState = FadeState::NeedsSecondPlacement;
} else if (fadeState == FadeState::NeedsSecondPlacement) {
fadeState = FadeState::CanRemove;
}
}
} // namespace mbgl
|