-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
source_cache.js
606 lines (519 loc) · 19 KB
/
source_cache.js
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
'use strict';
const Source = require('./source');
const Tile = require('./tile');
const Evented = require('../util/evented');
const TileCoord = require('./tile_coord');
const Cache = require('../util/lru_cache');
const Coordinate = require('../geo/coordinate');
const util = require('../util/util');
const EXTENT = require('../data/extent');
/**
* `SourceCache` is responsible for
*
* - creating an instance of `Source`
* - forwarding events from `Source`
* - caching tiles loaded from an instance of `Source`
* - loading the tiles needed to render a given viewport
* - unloading the cached tiles not needed to render a given viewport
*
* @private
*/
class SourceCache extends Evented {
constructor(id, options, dispatcher) {
super();
this.id = id;
this.dispatcher = dispatcher;
this.on('source.load', function() {
this._sourceLoaded = true;
});
this.on('error', function() {
this._sourceErrored = true;
});
this.on('data', function(event) {
if (this._sourceLoaded && event.dataType === 'source') {
this.reload();
if (this.transform) {
this.update(this.transform);
}
}
});
this._source = Source.create(id, options, dispatcher, this);
this._tiles = {};
this._cache = new Cache(0, this.unloadTile.bind(this));
this._timers = {};
this._cacheTimers = {};
this._isIdRenderable = this._isIdRenderable.bind(this);
}
onAdd(map) {
this.map = map;
if (this._source && this._source.onAdd) {
this._source.onAdd(map);
}
}
onRemove(map) {
if (this._source && this._source.onRemove) {
this._source.onRemove(map);
}
}
/**
* Return true if no tile data is pending, tiles will not change unless
* an additional API call is received.
* @returns {boolean}
* @private
*/
loaded() {
if (this._sourceErrored) { return true; }
if (!this._sourceLoaded) { return false; }
for (const t in this._tiles) {
const tile = this._tiles[t];
if (tile.state !== 'loaded' && tile.state !== 'errored')
return false;
}
return true;
}
/**
* @returns {Source} The underlying source object
* @private
*/
getSource() {
return this._source;
}
loadTile(tile, callback) {
return this._source.loadTile(tile, callback);
}
unloadTile(tile) {
if (this._source.unloadTile)
return this._source.unloadTile(tile);
}
abortTile(tile) {
if (this._source.abortTile)
return this._source.abortTile(tile);
}
serialize() {
return this._source.serialize();
}
prepare() {
if (this._sourceLoaded && this._source.prepare)
return this._source.prepare();
}
/**
* Return all tile ids ordered with z-order, and cast to numbers
* @returns {Array<number>} ids
* @private
*/
getIds() {
return Object.keys(this._tiles).map(Number).sort(compareKeyZoom);
}
getRenderableIds() {
return this.getIds().filter(this._isIdRenderable);
}
_isIdRenderable(id) {
return this._tiles[id].hasData() && !this._coveredTiles[id];
}
reload() {
this._cache.reset();
for (const i in this._tiles) {
this.reloadTile(i, 'reloading');
}
}
reloadTile(id, state) {
const tile = this._tiles[id];
// The difference between "loading" tiles and "reloading" or "expired"
// tiles is that "reloading"/"expired" tiles are "renderable".
// Therefore, a "loading" tile cannot become a "reloading" tile without
// first becoming a "loaded" tile.
if (tile.state !== 'loading') {
tile.state = state;
}
this.loadTile(tile, this._tileLoaded.bind(this, tile, id));
}
_tileLoaded(tile, id, err) {
if (err) {
tile.state = 'errored';
this._source.fire('error', {tile: tile, error: err});
return;
}
tile.sourceCache = this;
tile.timeAdded = new Date().getTime();
this._setTileReloadTimer(id, tile);
this._source.fire('data', {tile: tile, coord: tile.coord, dataType: 'tile'});
// HACK this is necessary to fix https://github.com/mapbox/mapbox-gl-js/issues/2986
if (this.map) this.map.painter.tileExtentVAO.vao = null;
}
/**
* Get a specific tile by TileCoordinate
* @param {TileCoordinate} coord
* @returns {Object} tile
* @private
*/
getTile(coord) {
return this.getTileByID(coord.id);
}
/**
* Get a specific tile by id
* @param {number|string} id
* @returns {Object} tile
* @private
*/
getTileByID(id) {
return this._tiles[id];
}
/**
* get the zoom level adjusted for the difference in map and source tilesizes
* @param {Object} transform
* @returns {number} zoom level
* @private
*/
getZoom(transform) {
return transform.zoom + transform.scaleZoom(transform.tileSize / this._source.tileSize);
}
/**
* Recursively find children of the given tile (up to maxCoveringZoom) that are already loaded;
* adds found tiles to retain object; returns true if any child is found.
*
* @param {Coordinate} coord
* @param {number} maxCoveringZoom
* @param {boolean} retain
* @returns {boolean} whether the operation was complete
* @private
*/
findLoadedChildren(coord, maxCoveringZoom, retain) {
let found = false;
for (const id in this._tiles) {
let tile = this._tiles[id];
// only consider renderable tiles on higher zoom levels (up to maxCoveringZoom)
if (retain[id] || !tile.hasData() || tile.coord.z <= coord.z || tile.coord.z > maxCoveringZoom) continue;
// disregard tiles that are not descendants of the given tile coordinate
const z2 = Math.pow(2, Math.min(tile.coord.z, this._source.maxzoom) - Math.min(coord.z, this._source.maxzoom));
if (Math.floor(tile.coord.x / z2) !== coord.x ||
Math.floor(tile.coord.y / z2) !== coord.y)
continue;
// found loaded child
retain[id] = true;
found = true;
// loop through parents; retain the topmost loaded one if found
while (tile && tile.coord.z - 1 > coord.z) {
const parentId = tile.coord.parent(this._source.maxzoom).id;
tile = this._tiles[parentId];
if (tile && tile.hasData()) {
delete retain[id];
retain[parentId] = true;
}
}
}
return found;
}
/**
* Find a loaded parent of the given tile (up to minCoveringZoom);
* adds the found tile to retain object and returns the tile if found
*
* @param {Coordinate} coord
* @param {number} minCoveringZoom
* @param {boolean} retain
* @returns {Tile} tile object
* @private
*/
findLoadedParent(coord, minCoveringZoom, retain) {
for (let z = coord.z - 1; z >= minCoveringZoom; z--) {
coord = coord.parent(this._source.maxzoom);
const tile = this._tiles[coord.id];
if (tile && tile.hasData()) {
retain[coord.id] = true;
return tile;
}
if (this._cache.has(coord.id)) {
retain[coord.id] = true;
return this._cache.get(coord.id);
}
}
}
/**
* Resizes the tile cache based on the current viewport's size.
*
* Larger viewports use more tiles and need larger caches. Larger viewports
* are more likely to be found on devices with more memory and on pages where
* the map is more important.
*
* @private
*/
updateCacheSize(transform) {
const widthInTiles = Math.ceil(transform.width / transform.tileSize) + 1;
const heightInTiles = Math.ceil(transform.height / transform.tileSize) + 1;
const approxTilesInView = widthInTiles * heightInTiles;
const commonZoomRange = 5;
this._cache.setMaxSize(Math.floor(approxTilesInView * commonZoomRange));
}
/**
* Removes tiles that are outside the viewport and adds new tiles that
* are inside the viewport.
* @private
*/
update(transform) {
if (!this._sourceLoaded) { return; }
let i;
let coord;
let tile;
let parentTile;
this.updateCacheSize(transform);
// Determine the overzooming/underzooming amounts.
const zoom = (this._source.roundZoom ? Math.round : Math.floor)(this.getZoom(transform));
const minCoveringZoom = Math.max(zoom - SourceCache.maxOverzooming, this._source.minzoom);
const maxCoveringZoom = Math.max(zoom + SourceCache.maxUnderzooming, this._source.minzoom);
// Retain is a list of tiles that we shouldn't delete, even if they are not
// the most ideal tile for the current viewport. This may include tiles like
// parent or child tiles that are *already* loaded.
const retain = {};
// Covered is a list of retained tiles who's areas are full covered by other,
// better, retained tiles. They are not drawn separately.
this._coveredTiles = {};
let visibleCoords;
if (!this.used) {
visibleCoords = [];
} else if (this._source.coord) {
visibleCoords = [this._source.coord];
} else {
visibleCoords = transform.coveringTiles({
tileSize: this._source.tileSize,
minzoom: this._source.minzoom,
maxzoom: this._source.maxzoom,
roundZoom: this._source.roundZoom,
reparseOverscaled: this._source.reparseOverscaled
});
}
for (i = 0; i < visibleCoords.length; i++) {
coord = visibleCoords[i];
tile = this.addTile(coord);
retain[coord.id] = true;
if (tile.hasData())
continue;
// The tile we require is not yet loaded.
// Retain child or parent tiles that cover the same area.
if (!this.findLoadedChildren(coord, maxCoveringZoom, retain)) {
parentTile = this.findLoadedParent(coord, minCoveringZoom, retain);
if (parentTile) {
this.addTile(parentTile.coord);
}
}
}
const parentsForFading = {};
if (isRasterType(this._source.type)) {
const ids = Object.keys(retain);
for (let k = 0; k < ids.length; k++) {
const id = ids[k];
coord = TileCoord.fromID(id);
tile = this._tiles[id];
if (!tile) continue;
// If the drawRasterTile has never seen this tile, then
// tile.fadeEndTime may be unset. In that case, or if
// fadeEndTime is in the future, then this tile is still
// fading in. Find tiles to cross-fade with it.
if (typeof tile.fadeEndTime === 'undefined' || tile.fadeEndTime >= Date.now()) {
if (this.findLoadedChildren(coord, maxCoveringZoom, retain)) {
retain[id] = true;
}
parentTile = this.findLoadedParent(coord, minCoveringZoom, parentsForFading);
if (parentTile) {
this.addTile(parentTile.coord);
}
}
}
}
let fadedParent;
for (fadedParent in parentsForFading) {
if (!retain[fadedParent]) {
// If a tile is only needed for fading, mark it as covered so that it isn't rendered on it's own.
this._coveredTiles[fadedParent] = true;
}
}
for (fadedParent in parentsForFading) {
retain[fadedParent] = true;
}
// Remove the tiles we don't need anymore.
const remove = util.keysDifference(this._tiles, retain);
for (i = 0; i < remove.length; i++) {
this.removeTile(+remove[i]);
}
this.transform = transform;
}
/**
* Add a tile, given its coordinate, to the pyramid.
* @param {Coordinate} coord
* @returns {Coordinate} the coordinate.
* @private
*/
addTile(coord) {
let tile = this._tiles[coord.id];
if (tile)
return tile;
const wrapped = coord.wrapped();
tile = this._tiles[wrapped.id];
if (!tile) {
tile = this._cache.get(wrapped.id);
if (tile) {
tile.redoPlacement(this._source);
if (this._cacheTimers[wrapped.id]) {
clearTimeout(this._cacheTimers[wrapped.id]);
this._cacheTimers[wrapped.id] = undefined;
this._setTileReloadTimer(wrapped.id, tile);
}
}
}
if (!tile) {
const zoom = coord.z;
const overscaling = zoom > this._source.maxzoom ? Math.pow(2, zoom - this._source.maxzoom) : 1;
tile = new Tile(wrapped, this._source.tileSize * overscaling, this._source.maxzoom);
this.loadTile(tile, this._tileLoaded.bind(this, tile, coord.id));
}
tile.uses++;
this._tiles[coord.id] = tile;
this._source.fire('dataloading', {tile: tile, coord: tile.coord, dataType: 'tile'});
return tile;
}
_setTileReloadTimer(id, tile) {
const tileExpires = tile.getExpiry();
if (tileExpires) {
this._timers[id] = setTimeout(() => {
this.reloadTile(id, 'expired');
this._timers[id] = undefined;
}, tileExpires - new Date().getTime());
}
}
_setCacheInvalidationTimer(id, tile) {
const tileExpires = tile.getExpiry();
if (tileExpires) {
this._cacheTimers[id] = setTimeout(() => {
this._cache.remove(id);
this._cacheTimers[id] = undefined;
}, tileExpires - new Date().getTime());
}
}
/**
* Remove a tile, given its id, from the pyramid
* @param {string|number} id tile id
* @returns {undefined} nothing
* @private
*/
removeTile(id) {
const tile = this._tiles[id];
if (!tile)
return;
tile.uses--;
delete this._tiles[id];
if (this._timers[id]) {
clearTimeout(this._timers[id]);
this._timers[id] = undefined;
}
this._source.fire('data', { tile: tile, coord: tile.coord, dataType: 'tile' });
if (tile.uses > 0)
return;
if (tile.hasData()) {
const wrappedId = tile.coord.wrapped().id;
this._cache.add(wrappedId, tile);
this._setCacheInvalidationTimer(wrappedId, tile);
} else {
tile.aborted = true;
this.abortTile(tile);
this.unloadTile(tile);
}
}
/**
* Remove all tiles from this pyramid
* @private
*/
clearTiles() {
for (const id in this._tiles)
this.removeTile(id);
this._cache.reset();
}
/**
* Search through our current tiles and attempt to find the tiles that
* cover the given bounds.
* @param {Array<Coordinate>} queryGeometry coordinates of the corners of bounding rectangle
* @returns {Array<Object>} result items have {tile, minX, maxX, minY, maxY}, where min/max bounding values are the given bounds transformed in into the coordinate space of this tile.
* @private
*/
tilesIn(queryGeometry) {
const tileResults = {};
const ids = this.getIds();
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
const z = queryGeometry[0].zoom;
for (let k = 0; k < queryGeometry.length; k++) {
const p = queryGeometry[k];
minX = Math.min(minX, p.column);
minY = Math.min(minY, p.row);
maxX = Math.max(maxX, p.column);
maxY = Math.max(maxY, p.row);
}
for (let i = 0; i < ids.length; i++) {
const tile = this._tiles[ids[i]];
const coord = TileCoord.fromID(ids[i]);
const tileSpaceBounds = [
coordinateToTilePoint(coord, tile.sourceMaxZoom, new Coordinate(minX, minY, z)),
coordinateToTilePoint(coord, tile.sourceMaxZoom, new Coordinate(maxX, maxY, z))
];
if (tileSpaceBounds[0].x < EXTENT && tileSpaceBounds[0].y < EXTENT &&
tileSpaceBounds[1].x >= 0 && tileSpaceBounds[1].y >= 0) {
const tileSpaceQueryGeometry = [];
for (let j = 0; j < queryGeometry.length; j++) {
tileSpaceQueryGeometry.push(coordinateToTilePoint(coord, tile.sourceMaxZoom, queryGeometry[j]));
}
let tileResult = tileResults[tile.coord.id];
if (tileResult === undefined) {
tileResult = tileResults[tile.coord.id] = {
tile: tile,
coord: coord,
queryGeometry: [],
scale: Math.pow(2, this.transform.zoom - tile.coord.z)
};
}
// Wrapped tiles share one tileResult object but can have multiple queryGeometry parts
tileResult.queryGeometry.push(tileSpaceQueryGeometry);
}
}
const results = [];
for (const t in tileResults) {
results.push(tileResults[t]);
}
return results;
}
redoPlacement() {
const ids = this.getIds();
for (let i = 0; i < ids.length; i++) {
const tile = this.getTileByID(ids[i]);
tile.redoPlacement(this._source);
}
}
getVisibleCoordinates() {
const coords = this.getRenderableIds().map(TileCoord.fromID);
for (const coord of coords) {
coord.posMatrix = this.transform.calculatePosMatrix(coord, this._source.maxzoom);
}
return coords;
}
}
SourceCache.maxOverzooming = 10;
SourceCache.maxUnderzooming = 3;
/**
* Convert a coordinate to a point in a tile's coordinate space.
* @param {Coordinate} tileCoord
* @param {Coordinate} coord
* @returns {Object} position
* @private
*/
function coordinateToTilePoint(tileCoord, sourceMaxZoom, coord) {
const zoomedCoord = coord.zoomTo(Math.min(tileCoord.z, sourceMaxZoom));
return {
x: (zoomedCoord.column - (tileCoord.x + tileCoord.w * Math.pow(2, tileCoord.z))) * EXTENT,
y: (zoomedCoord.row - tileCoord.y) * EXTENT
};
}
function compareKeyZoom(a, b) {
return (a % 32) - (b % 32);
}
function isRasterType(type) {
return type === 'raster' || type === 'image' || type === 'video';
}
module.exports = SourceCache;