Skip to content

Commit

Permalink
dry up dem source, inherit xhr-avoiding logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Jul 31, 2018
1 parent 40b15e5 commit 0ac9e35
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 54 deletions.
51 changes: 14 additions & 37 deletions src/source/raster_dem_tile_source.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// @flow

import { getImage, ResourceType } from '../util/ajax';
import { extend } from '../util/util';
import { Evented } from '../util/evented';
import { normalizeTileURL as normalizeURL } from '../util/mapbox';
import browser from '../util/browser';
import { OverscaledTileID } from './tile_id';
import RasterTileSource from './raster_tile_source';
Expand Down Expand Up @@ -39,54 +37,33 @@ class RasterDEMTileSource extends RasterTileSource implements Source {
}

loadTile(tile: Tile, callback: Callback<void>) {
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) {
delete tile.request;
if (tile.aborted) {
tile.state = 'unloaded';
callback(null);
} else if (err) {
tile.state = 'errored';
callback(err);
} else if (img) {
if (this.map._refreshExpiredTiles) tile.setExpiryData(img);
delete (img: any).cacheControl;
delete (img: any).expires;

const rawImageData = browser.getImageData(img);
const params = {
uid: tile.uid,
coord: tile.tileID,
source: this.id,
rawImageData: rawImageData,
encoding: this.encoding
};

if (!tile.workerID || tile.state === 'expired') {
tile.workerID = this.dispatcher.send('loadDEMTile', params, done.bind(this));
}
}
}
super.loadTile(tile, callback);
}

onTileLoad(tile: Tile, img: HTMLImageElement, callback: Callback<void>) {
if (tile.workerID && tile.state !== 'expired') return;

function done(err, dem) {
tile.workerID = this.dispatcher.send('loadDEMTile', {
uid: tile.uid,
coord: tile.tileID,
source: this.id,
rawImageData: browser.getImageData(img),
encoding: this.encoding
}, (err, dem) => {
if (err) {
tile.state = 'errored';
callback(err);
}

if (dem) {
} else if (dem) {
tile.dem = dem;
tile.needsHillshadePrepare = true;
tile.state = 'loaded';
callback(null);
}
}
});
}


_getNeighboringTiles(tileID: OverscaledTileID) {
const canonical = tileID.canonical;
const dim = Math.pow(2, canonical.z);
Expand Down
38 changes: 21 additions & 17 deletions src/source/raster_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,31 @@ class RasterTileSource extends Evented implements Source {
this._avoidXHR = true;
}

const context = this.map.painter.context;
const gl = context.gl;
tile.texture = this.map.painter.getTileTexture(img.width);
if (tile.texture) {
tile.texture.update(img, { useMipmap: true });
} else {
tile.texture = new Texture(context, img, gl.RGBA, { useMipmap: true });
tile.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE, gl.LINEAR_MIPMAP_NEAREST);

if (context.extTextureFilterAnisotropic) {
gl.texParameterf(gl.TEXTURE_2D, context.extTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT, context.extTextureFilterAnisotropicMax);
}
}

tile.state = 'loaded';

callback(null);
this.onTileLoad(tile, img, callback);
}
}, this._avoidXHR);
}

onTileLoad(tile: Tile, img: HTMLImageElement, callback: Callback<void>) {
const context = this.map.painter.context;
const gl = context.gl;
tile.texture = this.map.painter.getTileTexture(img.width);
if (tile.texture) {
tile.texture.update(img, { useMipmap: true });
} else {
tile.texture = new Texture(context, img, gl.RGBA, { useMipmap: true });
tile.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE, gl.LINEAR_MIPMAP_NEAREST);

if (context.extTextureFilterAnisotropic) {
gl.texParameterf(gl.TEXTURE_2D, context.extTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT, context.extTextureFilterAnisotropicMax);
}
}

tile.state = 'loaded';

callback(null);
}

abortTile(tile: Tile, callback: Callback<void>) {
if (tile.request) {
tile.request.cancel();
Expand Down

0 comments on commit 0ac9e35

Please sign in to comment.