This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
tile_pyramid.cpp
286 lines (233 loc) · 10.1 KB
/
tile_pyramid.cpp
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
#include <mbgl/renderer/tile_pyramid.hpp>
#include <mbgl/renderer/render_tile.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/render_source.hpp>
#include <mbgl/renderer/tile_parameters.hpp>
#include <mbgl/renderer/query.hpp>
#include <mbgl/map/transform.hpp>
#include <mbgl/text/placement_config.hpp>
#include <mbgl/math/clamp.hpp>
#include <mbgl/util/tile_cover.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/algorithm/update_renderables.hpp>
#include <mapbox/geometry/envelope.hpp>
#include <algorithm>
namespace mbgl {
using namespace style;
static TileObserver nullObserver;
TilePyramid::TilePyramid()
: observer(&nullObserver) {
}
TilePyramid::~TilePyramid() = default;
bool TilePyramid::isLoaded() const {
for (const auto& pair : tiles) {
if (!pair.second->isComplete()) {
return false;
}
}
return true;
}
void TilePyramid::startRender(PaintParameters& parameters) {
for (auto& tile : renderTiles) {
tile.startRender(parameters);
}
}
void TilePyramid::finishRender(PaintParameters& parameters) {
for (auto& tile : renderTiles) {
tile.finishRender(parameters);
}
}
std::vector<std::reference_wrapper<RenderTile>> TilePyramid::getRenderTiles() {
return { renderTiles.begin(), renderTiles.end() };
}
void TilePyramid::update(const std::vector<Immutable<style::Layer::Impl>>& layers,
const bool needsRendering,
const bool needsRelayout,
const TileParameters& parameters,
const SourceType type,
const uint16_t tileSize,
const Range<uint8_t> zoomRange,
std::function<std::unique_ptr<Tile> (const OverscaledTileID&)> createTile) {
// If we need a relayout, abandon any cached tiles; they're now stale.
if (needsRelayout) {
cache.clear();
}
// If we're not going to render anything, move our existing tiles into
// the cache (if they're not stale) or abandon them, and return.
if (!needsRendering) {
if (!needsRelayout) {
for (auto& entry : tiles) {
cache.add(entry.first, std::move(entry.second));
}
}
tiles.clear();
renderTiles.clear();
return;
}
// Determine the overzooming/underzooming amounts and required tiles.
int32_t overscaledZoom = util::coveringZoomLevel(parameters.transformState.getZoom(), type, tileSize);
int32_t tileZoom = overscaledZoom;
int32_t panZoom = zoomRange.max;
std::vector<UnwrappedTileID> idealTiles;
std::vector<UnwrappedTileID> panTiles;
if (overscaledZoom >= zoomRange.min) {
int32_t idealZoom = std::min<int32_t>(zoomRange.max, overscaledZoom);
// Make sure we're not reparsing overzoomed raster tiles.
if (type == SourceType::Raster) {
tileZoom = idealZoom;
// FIXME: Prefetching is only enabled for raster
// tiles until we fix #7026.
// Request lower zoom level tiles (if configure to do so) in an attempt
// to show something on the screen faster at the cost of a little of bandwidth.
if (parameters.prefetchZoomDelta) {
panZoom = std::max<int32_t>(tileZoom - parameters.prefetchZoomDelta, zoomRange.min);
}
if (panZoom < tileZoom) {
panTiles = util::tileCover(parameters.transformState, panZoom);
}
}
idealTiles = util::tileCover(parameters.transformState, idealZoom);
}
// Stores a list of all the tiles that we're definitely going to retain. There are two
// kinds of tiles we need: the ideal tiles determined by the tile cover. They may not yet be in
// use because they're still loading. In addition to that, we also need to retain all tiles that
// we're actively using, e.g. as a replacement for tile that aren't loaded yet.
std::set<OverscaledTileID> retain;
auto retainTileFn = [&](Tile& tile, TileNecessity necessity) -> void {
if (retain.emplace(tile.id).second) {
tile.setNecessity(necessity);
}
if (needsRelayout) {
tile.setLayers(layers);
}
};
auto getTileFn = [&](const OverscaledTileID& tileID) -> Tile* {
auto it = tiles.find(tileID);
return it == tiles.end() ? nullptr : it->second.get();
};
auto createTileFn = [&](const OverscaledTileID& tileID) -> Tile* {
std::unique_ptr<Tile> tile = cache.get(tileID);
if (!tile) {
tile = createTile(tileID);
if (tile) {
tile->setObserver(observer);
tile->setLayers(layers);
}
}
if (!tile) {
return nullptr;
}
return tiles.emplace(tileID, std::move(tile)).first->second.get();
};
auto renderTileFn = [&](const UnwrappedTileID& tileID, Tile& tile) {
renderTiles.emplace_back(tileID, tile);
};
renderTiles.clear();
if (!panTiles.empty()) {
algorithm::updateRenderables(getTileFn, createTileFn, retainTileFn,
[](const UnwrappedTileID&, Tile&) {}, panTiles, zoomRange, panZoom);
}
algorithm::updateRenderables(getTileFn, createTileFn, retainTileFn, renderTileFn,
idealTiles, zoomRange, tileZoom);
if (type != SourceType::Annotations) {
size_t conservativeCacheSize =
std::max((float)parameters.transformState.getSize().width / tileSize, 1.0f) *
std::max((float)parameters.transformState.getSize().height / tileSize, 1.0f) *
(parameters.transformState.getMaxZoom() - parameters.transformState.getMinZoom() + 1) *
0.5;
cache.setSize(conservativeCacheSize);
}
// Remove stale tiles. This goes through the (sorted!) tiles map and retain set in lockstep
// and removes items from tiles that don't have the corresponding key in the retain set.
{
auto tilesIt = tiles.begin();
auto retainIt = retain.begin();
while (tilesIt != tiles.end()) {
if (retainIt == retain.end() || tilesIt->first < *retainIt) {
if (!needsRelayout) {
tilesIt->second->setNecessity(TileNecessity::Optional);
cache.add(tilesIt->first, std::move(tilesIt->second));
}
tiles.erase(tilesIt++);
} else {
if (!(*retainIt < tilesIt->first)) {
++tilesIt;
}
++retainIt;
}
}
}
for (auto& pair : tiles) {
const PlacementConfig config { parameters.transformState.getAngle(),
parameters.transformState.getPitch(),
parameters.transformState.getCameraToCenterDistance(),
parameters.transformState.getCameraToTileDistance(pair.first.toUnwrapped()),
parameters.debugOptions & MapDebugOptions::Collision };
pair.second->setPlacementConfig(config);
}
}
std::unordered_map<std::string, std::vector<Feature>> TilePyramid::queryRenderedFeatures(const ScreenLineString& geometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const RenderedQueryOptions& options) const {
std::unordered_map<std::string, std::vector<Feature>> result;
if (renderTiles.empty() || geometry.empty()) {
return result;
}
LineString<double> queryGeometry;
for (const auto& p : geometry) {
queryGeometry.push_back(TileCoordinate::fromScreenCoordinate(
transformState, 0, { p.x, transformState.getSize().height - p.y }).p);
}
mapbox::geometry::box<double> box = mapbox::geometry::envelope(queryGeometry);
std::vector<std::reference_wrapper<const RenderTile>> sortedTiles{ renderTiles.begin(),
renderTiles.end() };
std::sort(sortedTiles.begin(), sortedTiles.end(), [](const RenderTile& a, const RenderTile& b) {
return std::tie(a.id.canonical.z, a.id.canonical.y, a.id.wrap, a.id.canonical.x) <
std::tie(b.id.canonical.z, b.id.canonical.y, b.id.wrap, b.id.canonical.x);
});
for (const RenderTile& renderTile : sortedTiles) {
GeometryCoordinate tileSpaceBoundsMin = TileCoordinate::toGeometryCoordinate(renderTile.id, box.min);
if (tileSpaceBoundsMin.x >= util::EXTENT || tileSpaceBoundsMin.y >= util::EXTENT) {
continue;
}
GeometryCoordinate tileSpaceBoundsMax = TileCoordinate::toGeometryCoordinate(renderTile.id, box.max);
if (tileSpaceBoundsMax.x < 0 || tileSpaceBoundsMax.y < 0) {
continue;
}
GeometryCoordinates tileSpaceQueryGeometry;
tileSpaceQueryGeometry.reserve(queryGeometry.size());
for (const auto& c : queryGeometry) {
tileSpaceQueryGeometry.push_back(TileCoordinate::toGeometryCoordinate(renderTile.id, c));
}
renderTile.tile.queryRenderedFeatures(result,
tileSpaceQueryGeometry,
transformState,
layers,
options);
}
return result;
}
std::vector<Feature> TilePyramid::querySourceFeatures(const SourceQueryOptions& options) const {
std::vector<Feature> result;
for (const auto& pair : tiles) {
pair.second->querySourceFeatures(result, options);
}
return result;
}
void TilePyramid::setCacheSize(size_t size) {
cache.setSize(size);
}
void TilePyramid::onLowMemory() {
cache.clear();
}
void TilePyramid::setObserver(TileObserver* observer_) {
observer = observer_;
}
void TilePyramid::dumpDebugLogs() const {
for (const auto& pair : tiles) {
pair.second->dumpDebugLogs();
}
}
} // namespace mbgl