Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/geo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Transform {
_constraining: boolean;
_posMatrixCache: {[_: string]: Float32Array};
_alignedPosMatrixCache: {[_: string]: Float32Array};
_coveringTilesCache: {[_: string]: Array<any>};

constructor(minZoom: ?number, maxZoom: ?number, minPitch: ?number, maxPitch: ?number, renderWorldCopies: boolean | void) {
this.tileSize = 512; // constant
Expand All @@ -80,6 +81,7 @@ class Transform {
this._edgeInsets = new EdgeInsets();
this._posMatrixCache = {};
this._alignedPosMatrixCache = {};
this._coveringTilesCache = {};
}

clone(): Transform {
Expand Down Expand Up @@ -318,10 +320,19 @@ class Transform {
minzoom?: number,
maxzoom?: number,
roundZoom?: boolean,
reparseOverscaled?: boolean,
renderWorldCopies?: boolean
reparseOverscaled?: boolean
}
): Array<OverscaledTileID> {

const loadCacheEntry = (cacheEntry): Array<OverscaledTileID> => {
return cacheEntry.map(it => new OverscaledTileID(it.overscaledZ, it.wrap, it.zoom, it.x, it.y));
};

const optionsKey = JSON.stringify([options, this._renderWorldCopies]);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the ugliest part about this patch:

  • Using an array to add this._renderWorldCopies into the hash key, to ensure that changes to that will invalidate the cache.
  • Using JSON.stringify on all arguments to construct a unique hash key.

if (this._coveringTilesCache[optionsKey]) {
return loadCacheEntry(this._coveringTilesCache[optionsKey]);
}

let z = this.coveringZoomLevel(options);
const actualZ = z;

Expand Down Expand Up @@ -356,7 +367,7 @@ class Transform {

// Do a depth-first traversal to find visible tiles and proper levels of detail
const stack = [];
const result = [];
const cacheEntry = [];
const maxZoom = z;
const overscaledZ = options.reparseOverscaled ? actualZ : z;

Expand Down Expand Up @@ -399,8 +410,13 @@ class Transform {

// Have we reached the target depth or is the tile too far away to be any split further?
if (it.zoom === maxZoom || (longestDim > distToSplit && it.zoom >= minZoom)) {
result.push({
tileID: new OverscaledTileID(it.zoom === maxZoom ? overscaledZ : it.zoom, it.wrap, it.zoom, x, y),
cacheEntry.push({
tileParameters: {
overscaledZ: it.zoom === maxZoom ? overscaledZ : it.zoom,
wrap: it.wrap,
zoom: it.zoom,
x, y
},
distanceSq: vec2.sqrLen([centerPoint[0] - 0.5 - x, centerPoint[1] - 0.5 - y])
});
continue;
Expand All @@ -414,7 +430,10 @@ class Transform {
}
}

return result.sort((a, b) => a.distanceSq - b.distanceSq).map(a => a.tileID);
const sortedCacheEntry = cacheEntry.sort((a, b) => a.distanceSq - b.distanceSq).map(a => a.tileParameters);
this._coveringTilesCache[optionsKey] = sortedCacheEntry;

return loadCacheEntry(sortedCacheEntry);
}

resize(width: number, height: number) {
Expand Down Expand Up @@ -765,6 +784,7 @@ class Transform {

this._posMatrixCache = {};
this._alignedPosMatrixCache = {};
this._coveringTilesCache = {};
}

maxPitchScaleFactor() {
Expand Down