Skip to content

Commit

Permalink
Retrieve tiles from source cache to avoid getting out of sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhamley committed Feb 4, 2019
1 parent 3e2f9a8 commit 03ace48
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
7 changes: 3 additions & 4 deletions src/source/raster_dem_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import type {RasterDEMSourceSpecification} from '../style-spec/types';

class RasterDEMTileSource extends RasterTileSource implements Source {
encoding: "mapbox" | "terrarium";
_textureQueue: Array<Object>;

constructor(id: string, options: RasterDEMSourceSpecification, dispatcher: Dispatcher, eventedParent: Evented) {
super(id, options, dispatcher, eventedParent);
this.type = 'raster-dem';
this.maxzoom = 22;
this._options = extend({}, options);
this.encoding = options.encoding || "mapbox";
this._textureQueue = [];
}

serialize() {
Expand All @@ -40,13 +42,11 @@ class RasterDEMTileSource extends RasterTileSource implements Source {
}

loadTile(tile: Tile, callback: Callback<void>) {
console.log('loadTile', tile);
const url = normalizeURL(tile.tileID.canonical.url(this.tiles, this.scheme), this.url, this.tileSize);
tile.request = getImage(this.map._transformRequest(url, ResourceType.Tile), imageLoaded.bind(this));

tile.neighboringTiles = this._getNeighboringTiles(tile.tileID);
function imageLoaded(err, img) {
console.log('imageLoaded', img);
delete tile.request;
if (tile.aborted) {
tile.state = 'unloaded';
Expand All @@ -58,10 +58,9 @@ class RasterDEMTileSource extends RasterTileSource implements Source {
if (this.map._refreshExpiredTiles) tile.setExpiryData(img);
delete (img: any).cacheControl;
delete (img: any).expires;
this.map.style._textureQueue.push({tile, img, callback: textureCallback.bind(this)});
this._textureQueue.push({tileKey: tile.tileID.key, img, callback: textureCallback.bind(this)});
callback(null);
}
console.log('queue', this.map.style._textureQueue);
}

function textureCallback(tile, img) {
Expand Down
4 changes: 3 additions & 1 deletion src/source/raster_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class RasterTileSource extends Evented implements Source {
dispatcher: Dispatcher;
map: Map;
tiles: Array<string>;
_textureQueue: Array<Object>;

_loaded: boolean;
_options: RasterSourceSpecification | RasterDEMSourceSpecification;
Expand All @@ -54,6 +55,7 @@ class RasterTileSource extends Evented implements Source {
this.scheme = 'xyz';
this.tileSize = 512;
this._loaded = false;
this._textureQueue = [];

this._options = extend({}, options);
extend(this, pick(options, ['url', 'scheme', 'tileSize']));
Expand Down Expand Up @@ -118,7 +120,7 @@ class RasterTileSource extends Evented implements Source {
delete (img: any).cacheControl;
delete (img: any).expires;

this.map.style._textureQueue.push({tile, img, callback: textureCallback.bind(this)});
this._textureQueue.push({tileKey: tile.tileID.key, img, callback: textureCallback.bind(this)});
callback(null);
}

Expand Down
1 change: 1 addition & 0 deletions src/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface Source {
tileID?: CanonicalTileID;
reparseOverscaled?: boolean,
vectorLayerIds?: Array<string>,
_textureQueue?: Array<Object>;

hasTransition(): boolean;

Expand Down
2 changes: 0 additions & 2 deletions src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class Style extends Evented {
_removedLayers: {[string]: StyleLayer};
_updatedPaintProps: {[layer: string]: true};
_layerOrderChanged: boolean;
_textureQueue: Array<Object>;

crossTileSymbolIndex: CrossTileSymbolIndex;
pauseablePlacement: PauseablePlacement;
Expand All @@ -146,7 +145,6 @@ class Style extends Evented {
this.sourceCaches = {};
this.zoomHistory = new ZoomHistory();
this._loaded = false;
this._textureQueue = [];

this._resetUpdates();

Expand Down
37 changes: 22 additions & 15 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1685,22 +1685,29 @@ class Map extends Camera {
// For performance reasons, we limit texture uploading to the GPU to
// a max of two uploads per animation frame
let continueRenderingTextures = false;
console.log('render called');
let max = Math.min(2, this.style._textureQueue.length);
for (let i = 0; i < max; i++) {
const queued = this.style._textureQueue.shift();
console.log('upload texture*********', this.style._textureQueue.length, queued);
if (queued) {
const {tile, img, callback} = queued;
// do not upload aborted tiles to the GPU
if (tile && !tile.aborted) {
callback(tile, img);
const sources = this.style && this.style.sourceCaches;
for (const id in sources) {
const cache = sources[id];
const source = cache._source;
if ((source.type === 'raster' || source.type === 'raster-dem')) {
const textureQueue = Array.isArray(source._textureQueue) ? source._textureQueue : [];
const max = Math.min(2, textureQueue.length);
for (let i = 0; i < max; i++) {
const queued = textureQueue.shift();
if (queued) {
const {tileKey, img, callback} = queued;
const tile = cache.getTileByID(tileKey);
// do not upload aborted tiles to the GPU
if (tile && !tile.aborted) {
console.log('create texture', textureQueue.length);
callback(tile, img);
}
if (textureQueue.length > 0) {
continueRenderingTextures = true;
}
}
}
}
}
}

if (this.style._textureQueue.length > 0) {
continueRenderingTextures = true;
}

// Actually draw
Expand Down

0 comments on commit 03ace48

Please sign in to comment.