Merge pull request #10478 from ahocevar/abort-without-dispose

Do not abort and dispose of tiles
This commit is contained in:
Andreas Hocevar
2020-01-09 09:52:07 +01:00
committed by GitHub
26 changed files with 66 additions and 413 deletions

View File

@@ -58,20 +58,6 @@ class ImageTile extends Tile {
} }
/**
* @inheritDoc
*/
disposeInternal() {
if (this.state == TileState.LOADING) {
this.unlistenImage_();
this.image_ = getBlankImage();
}
if (this.interimTile) {
this.interimTile.dispose();
}
super.disposeInternal();
}
/** /**
* Get the HTML image element for this tile (may be a Canvas, Image, or Video). * Get the HTML image element for this tile (may be a Canvas, Image, or Video).
* @return {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement} Image. * @return {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement} Image.

View File

@@ -145,10 +145,9 @@ class Tile extends EventTarget {
} }
/** /**
* @inheritDoc * Called by the tile cache when the tile is removed from the cache due to expiry
*/ */
disposeInternal() { release() {
this.setState(TileState.ABORT);
} }
/** /**

View File

@@ -6,15 +6,6 @@ import {fromKey, getKey} from './tilecoord.js';
class TileCache extends LRUCache { class TileCache extends LRUCache {
/**
* @param {number=} opt_highWaterMark High water mark.
*/
constructor(opt_highWaterMark) {
super(opt_highWaterMark);
}
/** /**
* @param {!Object<string, import("./TileRange.js").default>} usedTiles Used tiles. * @param {!Object<string, import("./TileRange.js").default>} usedTiles Used tiles.
*/ */
@@ -24,7 +15,7 @@ class TileCache extends LRUCache {
if (tile.getKey() in usedTiles) { if (tile.getKey() in usedTiles) {
break; break;
} else { } else {
this.pop().dispose(); this.pop().release();
} }
} }
} }
@@ -42,7 +33,7 @@ class TileCache extends LRUCache {
this.forEach(function(tile) { this.forEach(function(tile) {
if (tile.tileCoord[0] !== z) { if (tile.tileCoord[0] !== z) {
this.remove(getKey(tile.tileCoord)); this.remove(getKey(tile.tileCoord));
tile.dispose(); tile.release();
} }
}.bind(this)); }.bind(this));
} }

View File

@@ -84,8 +84,7 @@ class TileQueue extends PriorityQueue {
handleTileChange(event) { handleTileChange(event) {
const tile = /** @type {import("./Tile.js").default} */ (event.target); const tile = /** @type {import("./Tile.js").default} */ (event.target);
const state = tile.getState(); const state = tile.getState();
if (tile.hifi && state === TileState.LOADED || state === TileState.ERROR || if (tile.hifi && state === TileState.LOADED || state === TileState.ERROR || state === TileState.EMPTY) {
state === TileState.EMPTY || state === TileState.ABORT) {
tile.removeEventListener(EventType.CHANGE, this.boundHandleTileChange_); tile.removeEventListener(EventType.CHANGE, this.boundHandleTileChange_);
const tileKey = tile.getKey(); const tileKey = tile.getKey();
if (tileKey in this.tilesLoadingKeys_) { if (tileKey in this.tilesLoadingKeys_) {
@@ -102,27 +101,19 @@ class TileQueue extends PriorityQueue {
*/ */
loadMoreTiles(maxTotalLoading, maxNewLoads) { loadMoreTiles(maxTotalLoading, maxNewLoads) {
let newLoads = 0; let newLoads = 0;
let abortedTiles = false;
let state, tile, tileKey; let state, tile, tileKey;
while (this.tilesLoading_ < maxTotalLoading && newLoads < maxNewLoads && while (this.tilesLoading_ < maxTotalLoading && newLoads < maxNewLoads &&
this.getCount() > 0) { this.getCount() > 0) {
tile = /** @type {import("./Tile.js").default} */ (this.dequeue()[0]); tile = /** @type {import("./Tile.js").default} */ (this.dequeue()[0]);
tileKey = tile.getKey(); tileKey = tile.getKey();
state = tile.getState(); state = tile.getState();
if (state === TileState.ABORT) { if (state === TileState.IDLE && !(tileKey in this.tilesLoadingKeys_)) {
abortedTiles = true;
} else if (state === TileState.IDLE && !(tileKey in this.tilesLoadingKeys_)) {
this.tilesLoadingKeys_[tileKey] = true; this.tilesLoadingKeys_[tileKey] = true;
++this.tilesLoading_; ++this.tilesLoading_;
++newLoads; ++newLoads;
tile.load(); tile.load();
} }
} }
if (newLoads === 0 && abortedTiles) {
// Do not stop the render loop when all wanted tiles were aborted due to
// a small, saturated tile cache.
this.tileChangeCallback_();
}
} }
} }

View File

@@ -14,6 +14,5 @@ export default {
* @type {number} * @type {number}
*/ */
ERROR: 3, ERROR: 3,
EMPTY: 4, EMPTY: 4
ABORT: 5
}; };

View File

@@ -4,7 +4,6 @@
import {getUid} from './util.js'; import {getUid} from './util.js';
import Tile from './Tile.js'; import Tile from './Tile.js';
import {createCanvasContext2D} from './dom.js'; import {createCanvasContext2D} from './dom.js';
import {unlistenByKey} from './events.js';
/** /**
@@ -19,6 +18,10 @@ import {unlistenByKey} from './events.js';
* @property {number} renderedTileZ * @property {number} renderedTileZ
*/ */
/**
* @type {Array<HTMLCanvasElement>}
*/
const canvasPool = [];
class VectorRenderTile extends Tile { class VectorRenderTile extends Tile {
@@ -26,13 +29,10 @@ class VectorRenderTile extends Tile {
* @param {import("./tilecoord.js").TileCoord} tileCoord Tile coordinate. * @param {import("./tilecoord.js").TileCoord} tileCoord Tile coordinate.
* @param {import("./TileState.js").default} state State. * @param {import("./TileState.js").default} state State.
* @param {import("./tilecoord.js").TileCoord} urlTileCoord Wrapped tile coordinate for source urls. * @param {import("./tilecoord.js").TileCoord} urlTileCoord Wrapped tile coordinate for source urls.
* @param {import("./tilegrid/TileGrid.js").default} sourceTileGrid Tile grid of the source.
* @param {function(VectorRenderTile):Array<import("./VectorTile").default>} getSourceTiles Function * @param {function(VectorRenderTile):Array<import("./VectorTile").default>} getSourceTiles Function
* to get an source tiles for this tile. * to get source tiles for this tile.
* @param {function(VectorRenderTile):void} removeSourceTiles Function to remove this tile from its
* source tiles's consumer count.
*/ */
constructor(tileCoord, state, urlTileCoord, sourceTileGrid, getSourceTiles, removeSourceTiles) { constructor(tileCoord, state, urlTileCoord, getSourceTiles) {
super(tileCoord, state, {transition: 0}); super(tileCoord, state, {transition: 0});
@@ -71,6 +71,11 @@ class VectorRenderTile extends Tile {
*/ */
this.replayState_ = {}; this.replayState_ = {};
/**
* @type {Array<import("./VectorTile.js").default>}
*/
this.sourceTiles = null;
/** /**
* @type {number} * @type {number}
*/ */
@@ -79,23 +84,7 @@ class VectorRenderTile extends Tile {
/** /**
* @type {!function():Array<import("./VectorTile.js").default>} * @type {!function():Array<import("./VectorTile.js").default>}
*/ */
this.getSourceTiles = getSourceTiles.bind(this, this); this.getSourceTiles = getSourceTiles.bind(undefined, this);
/**
* @type {!function(import("./VectorRenderTile.js").default):void}
*/
this.removeSourceTiles_ = removeSourceTiles;
/**
* @private
* @type {import("./tilegrid/TileGrid.js").default}
*/
this.sourceTileGrid_ = sourceTileGrid;
/**
* @type {Array<import("./events.js").EventsKey>}
*/
this.sourceTileListenerKeys = [];
/** /**
* z of the source tiles of the last getSourceTiles call. * z of the source tiles of the last getSourceTiles call.
@@ -115,27 +104,6 @@ class VectorRenderTile extends Tile {
this.wrappedTileCoord = urlTileCoord; this.wrappedTileCoord = urlTileCoord;
} }
/**
* @inheritDoc
*/
disposeInternal() {
this.sourceTileListenerKeys.forEach(unlistenByKey);
this.sourceTileListenerKeys.length = 0;
this.removeSourceTiles_(this);
for (const key in this.context_) {
const canvas = this.context_[key].canvas;
canvas.width = 0;
canvas.height = 0;
}
for (const key in this.executorGroups) {
const executorGroups = this.executorGroups[key];
for (let i = 0, ii = executorGroups.length; i < ii; ++i) {
executorGroups[i].disposeInternal();
}
}
super.disposeInternal();
}
/** /**
* @param {import("./layer/Layer.js").default} layer Layer. * @param {import("./layer/Layer.js").default} layer Layer.
* @return {CanvasRenderingContext2D} The rendering context. * @return {CanvasRenderingContext2D} The rendering context.
@@ -143,7 +111,7 @@ class VectorRenderTile extends Tile {
getContext(layer) { getContext(layer) {
const key = getUid(layer); const key = getUid(layer);
if (!(key in this.context_)) { if (!(key in this.context_)) {
this.context_[key] = createCanvasContext2D(); this.context_[key] = createCanvasContext2D(1, 1, canvasPool);
} }
return this.context_[key]; return this.context_[key];
} }
@@ -192,6 +160,16 @@ class VectorRenderTile extends Tile {
load() { load() {
this.getSourceTiles(); this.getSourceTiles();
} }
/**
* @inheritDoc
*/
release() {
for (const key in this.context_) {
canvasPool.push(this.context_[key].canvas);
}
super.release();
}
} }

View File

@@ -18,11 +18,6 @@ class VectorTile extends Tile {
super(tileCoord, state, opt_options); super(tileCoord, state, opt_options);
/**
* @type {number}
*/
this.consumers = 0;
/** /**
* Extent of this tile; set by the source. * Extent of this tile; set by the source.
* @type {import("./extent.js").Extent} * @type {import("./extent.js").Extent}

View File

@@ -7,10 +7,12 @@
* Create an html canvas element and returns its 2d context. * Create an html canvas element and returns its 2d context.
* @param {number=} opt_width Canvas width. * @param {number=} opt_width Canvas width.
* @param {number=} opt_height Canvas height. * @param {number=} opt_height Canvas height.
* @param {Array<HTMLCanvasElement>=} opt_canvasPool Canvas pool to take existing canvas from.
* @return {CanvasRenderingContext2D} The context. * @return {CanvasRenderingContext2D} The context.
*/ */
export function createCanvasContext2D(opt_width, opt_height) { export function createCanvasContext2D(opt_width, opt_height, opt_canvasPool) {
const canvas = document.createElement('canvas'); const canvas = opt_canvasPool && opt_canvasPool.length ?
opt_canvasPool.shift() : document.createElement('canvas');
if (opt_width) { if (opt_width) {
canvas.width = opt_width; canvas.width = opt_width;
} }

View File

@@ -18,7 +18,6 @@ import {
} from '../../transform.js'; } from '../../transform.js';
import {createCanvasContext2D} from '../../dom.js'; import {createCanvasContext2D} from '../../dom.js';
import {labelCache, defaultTextAlign, measureTextHeight, measureAndCacheTextWidth, measureTextWidths} from '../canvas.js'; import {labelCache, defaultTextAlign, measureTextHeight, measureAndCacheTextWidth, measureTextWidths} from '../canvas.js';
import Disposable from '../../Disposable.js';
import RBush from 'rbush/rbush.js'; import RBush from 'rbush/rbush.js';
@@ -52,7 +51,7 @@ const p3 = [];
const p4 = []; const p4 = [];
class Executor extends Disposable { class Executor {
/** /**
* @param {number} resolution Resolution. * @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio. * @param {number} pixelRatio Pixel ratio.
@@ -60,7 +59,6 @@ class Executor extends Disposable {
* @param {SerializableInstructions} instructions The serializable instructions * @param {SerializableInstructions} instructions The serializable instructions
*/ */
constructor(resolution, pixelRatio, overlaps, instructions) { constructor(resolution, pixelRatio, overlaps, instructions) {
super();
/** /**
* @protected * @protected
@@ -156,15 +154,6 @@ class Executor extends Disposable {
this.widths_ = {}; this.widths_ = {};
} }
/**
* @inheritDoc
*/
disposeInternal() {
labelCache.release(this);
super.disposeInternal();
}
/** /**
* @param {string} text Text. * @param {string} text Text.
* @param {string} textKey Text style key. * @param {string} textKey Text style key.

View File

@@ -10,7 +10,6 @@ import {isEmpty} from '../../obj.js';
import BuilderType from './BuilderType.js'; import BuilderType from './BuilderType.js';
import {create as createTransform, compose as composeTransform} from '../../transform.js'; import {create as createTransform, compose as composeTransform} from '../../transform.js';
import Executor from './Executor.js'; import Executor from './Executor.js';
import Disposable from '../../Disposable.js';
/** /**
* @const * @const
@@ -26,7 +25,7 @@ const ORDER = [
]; ];
class ExecutorGroup extends Disposable { class ExecutorGroup {
/** /**
* @param {import("../../extent.js").Extent} maxExtent Max extent for clipping. When a * @param {import("../../extent.js").Extent} maxExtent Max extent for clipping. When a
* `maxExtent` was set on the Buillder for this executor group, the same `maxExtent` * `maxExtent` was set on the Buillder for this executor group, the same `maxExtent`
@@ -40,7 +39,6 @@ class ExecutorGroup extends Disposable {
* @param {number=} opt_renderBuffer Optional rendering buffer. * @param {number=} opt_renderBuffer Optional rendering buffer.
*/ */
constructor(maxExtent, resolution, pixelRatio, overlaps, allInstructions, opt_renderBuffer) { constructor(maxExtent, resolution, pixelRatio, overlaps, allInstructions, opt_renderBuffer) {
super();
/** /**
* @private * @private
@@ -128,24 +126,6 @@ class ExecutorGroup extends Disposable {
} }
} }
/**
* @inheritDoc
*/
disposeInternal() {
for (const z in this.executorsByZIndex_) {
const executors = this.executorsByZIndex_[z];
for (const key in executors) {
executors[key].disposeInternal();
}
}
if (this.hitDetectionContext_) {
const canvas = this.hitDetectionContext_.canvas;
canvas.width = 0;
canvas.height = 0;
}
super.disposeInternal();
}
/** /**
* @param {Array<BuilderType>} executors Executors. * @param {Array<BuilderType>} executors Executors.

View File

@@ -1,4 +1,3 @@
import {getUid} from '../../util.js';
import LRUCache from '../../structs/LRUCache.js'; import LRUCache from '../../structs/LRUCache.js';
/** /**
@@ -11,59 +10,11 @@ import LRUCache from '../../structs/LRUCache.js';
*/ */
class LabelCache extends LRUCache { class LabelCache extends LRUCache {
/** expireCache() {
* @inheritDoc
*/
constructor(opt_highWaterMark) {
super(opt_highWaterMark);
this.consumers = {};
}
clear() {
this.consumers = {};
super.clear();
}
/**
* @override
* @param {string} key Label key.
* @param {import("./Executor.js").default} consumer Label consumer.
* @return {HTMLCanvasElement} Label.
*/
get(key, consumer) {
const canvas = super.get(key);
const consumerId = getUid(consumer);
if (!(consumerId in this.consumers)) {
this.consumers[consumerId] = {};
}
this.consumers[consumerId][key] = true;
return canvas;
}
prune() {
outer:
while (this.canExpireCache()) { while (this.canExpireCache()) {
const key = this.peekLastKey(); this.pop();
for (const consumerId in this.consumers) {
if (key in this.consumers[consumerId]) {
break outer;
}
}
const canvas = this.pop();
canvas.width = 0;
canvas.height = 0;
for (const consumerId in this.consumers) {
delete this.consumers[consumerId][key];
}
} }
} }
/**
* @param {import("./Executor.js").default} consumer Label consumer.
*/
release(consumer) {
delete this.consumers[getUid(consumer)];
}
} }
export default LabelCache; export default LabelCache;

View File

@@ -131,8 +131,6 @@ class CanvasTextBuilder extends CanvasBuilder {
* @type {string} * @type {string}
*/ */
this.strokeKey_ = ''; this.strokeKey_ = '';
labelCache.prune();
} }
/** /**
@@ -140,6 +138,7 @@ class CanvasTextBuilder extends CanvasBuilder {
*/ */
finish() { finish() {
const instructions = super.finish(); const instructions = super.finish();
labelCache.expireCache();
instructions.textStates = this.textStates; instructions.textStates = this.textStates;
instructions.fillStates = this.fillStates; instructions.fillStates = this.fillStates;
instructions.strokeStates = this.strokeStates; instructions.strokeStates = this.strokeStates;

View File

@@ -362,7 +362,6 @@ class CanvasTileLayerRenderer extends CanvasLayerRenderer {
this.manageTilePyramid(frameState, tileSource, tileGrid, pixelRatio, this.manageTilePyramid(frameState, tileSource, tileGrid, pixelRatio,
projection, extent, z, tileLayer.getPreload()); projection, extent, z, tileLayer.getPreload());
this.updateCacheSize_(frameState, tileSource);
this.scheduleExpireCache(frameState, tileSource); this.scheduleExpireCache(frameState, tileSource);
this.postRender(context, frameState); this.postRender(context, frameState);
@@ -474,27 +473,6 @@ class CanvasTileLayerRenderer extends CanvasLayerRenderer {
usedTiles[tileSourceKey][tile.getKey()] = true; usedTiles[tileSourceKey][tile.getKey()] = true;
} }
/**
* Check if the cache is big enough, and increase its size if necessary.
* @param {import("../../PluggableMap.js").FrameState} frameState Frame state.
* @param {import("../../source/Tile.js").default} tileSource Tile source.
* @private
*/
updateCacheSize_(frameState, tileSource) {
const tileSourceKey = getUid(tileSource);
let size = 0;
if (tileSourceKey in frameState.usedTiles) {
size += Object.keys(frameState.usedTiles[tileSourceKey]).length;
}
if (tileSourceKey in frameState.wantedTiles) {
size += Object.keys(frameState.wantedTiles[tileSourceKey]).length;
}
const tileCache = tileSource.tileCache;
if (tileCache.highWaterMark < size) {
tileCache.highWaterMark = size;
}
}
/** /**
* Manage tile pyramid. * Manage tile pyramid.
* This function performs a number of functions related to the tiles at the * This function performs a number of functions related to the tiles at the

View File

@@ -387,9 +387,6 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
return true; return true;
} }
if (this.replayGroup_) {
this.replayGroup_.dispose();
}
this.replayGroup_ = null; this.replayGroup_ = null;
this.dirty_ = false; this.dirty_ = false;

View File

@@ -117,8 +117,7 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
let render; let render;
const tileUid = getUid(tile); const tileUid = getUid(tile);
const state = tile.getState(); const state = tile.getState();
if (((state === TileState.LOADED && tile.hifi) || if (((state === TileState.LOADED && tile.hifi) || state === TileState.ERROR) &&
state === TileState.ERROR || state === TileState.ABORT) &&
tileUid in this.tileListenerKeys_) { tileUid in this.tileListenerKeys_) {
unlistenByKey(this.tileListenerKeys_[tileUid]); unlistenByKey(this.tileListenerKeys_[tileUid]);
delete this.tileListenerKeys_[tileUid]; delete this.tileListenerKeys_[tileUid];
@@ -219,12 +218,6 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
const sourceTiles = source.getSourceTiles(pixelRatio, projection, tile); const sourceTiles = source.getSourceTiles(pixelRatio, projection, tile);
const layerUid = getUid(layer); const layerUid = getUid(layer);
const executorGroups = tile.executorGroups[layerUid];
if (executorGroups) {
for (let i = 0, ii = executorGroups.length; i < ii; ++i) {
executorGroups[i].dispose();
}
}
delete tile.hitDetectionImageData[layerUid]; delete tile.hitDetectionImageData[layerUid];
tile.executorGroups[layerUid] = []; tile.executorGroups[layerUid] = [];
for (let t = 0, tt = sourceTiles.length; t < tt; ++t) { for (let t = 0, tt = sourceTiles.length; t < tt; ++t) {
@@ -468,9 +461,6 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
const clipZs = []; const clipZs = [];
for (let i = tiles.length - 1; i >= 0; --i) { for (let i = tiles.length - 1; i >= 0; --i) {
const tile = /** @type {import("../../VectorRenderTile.js").default} */ (tiles[i]); const tile = /** @type {import("../../VectorRenderTile.js").default} */ (tiles[i]);
if (tile.getState() == TileState.ABORT) {
continue;
}
const tileCoord = tile.tileCoord; const tileCoord = tile.tileCoord;
const tileExtent = tileGrid.getTileCoordExtent(tile.wrappedTileCoord); const tileExtent = tileGrid.getTileCoordExtent(tile.wrappedTileCoord);
const worldOffset = tileGrid.getTileCoordExtent(tileCoord, this.tmpExtent)[0] - tileExtent[0]; const worldOffset = tileGrid.getTileCoordExtent(tileCoord, this.tmpExtent)[0] - tileExtent[0];

View File

@@ -203,16 +203,6 @@ class ReprojTile extends Tile {
} }
} }
/**
* @inheritDoc
*/
disposeInternal() {
if (this.state == TileState.LOADING) {
this.unlistenSources_();
}
super.disposeInternal();
}
/** /**
* Get the HTML Canvas element for this tile. * Get the HTML Canvas element for this tile.
* @return {HTMLCanvasElement} Canvas. * @return {HTMLCanvasElement} Canvas.

View File

@@ -139,7 +139,7 @@ class UrlTile extends TileSource {
} else if (uid in this.tileLoadingKeys_) { } else if (uid in this.tileLoadingKeys_) {
delete this.tileLoadingKeys_[uid]; delete this.tileLoadingKeys_[uid];
type = tileState == TileState.ERROR ? TileEventType.TILELOADERROR : type = tileState == TileState.ERROR ? TileEventType.TILELOADERROR :
(tileState == TileState.LOADED || tileState == TileState.ABORT) ? tileState == TileState.LOADED ?
TileEventType.TILELOADEND : undefined; TileEventType.TILELOADEND : undefined;
} }
if (type != undefined) { if (type != undefined) {

View File

@@ -12,8 +12,8 @@ import {createXYZ, extentFromProjection, createForProjection} from '../tilegrid.
import {buffer as bufferExtent, getIntersection, intersects} from '../extent.js'; import {buffer as bufferExtent, getIntersection, intersects} from '../extent.js';
import EventType from '../events/EventType.js'; import EventType from '../events/EventType.js';
import {loadFeaturesXhr} from '../featureloader.js'; import {loadFeaturesXhr} from '../featureloader.js';
import {equals, remove} from '../array.js'; import {equals} from '../array.js';
import {listen, unlistenByKey} from '../events.js'; import TileCache from '../TileCache.js';
/** /**
* @typedef {Object} Options * @typedef {Object} Options
@@ -140,15 +140,9 @@ class VectorTile extends UrlTile {
/** /**
* @private * @private
* @type {Object<string, import("../VectorTile.js").default>} * @type {TileCache}
*/ */
this.sourceTileByKey_ = {}; this.sourceTileCache = new TileCache(this.tileCache.highWaterMark);
/**
* @private
* @type {Object<string, Array<import("../VectorTile.js").default>>}
*/
this.sourceTilesByTileKey_ = {};
/** /**
* @private * @private
@@ -228,8 +222,7 @@ class VectorTile extends UrlTile {
*/ */
clear() { clear() {
this.tileCache.clear(); this.tileCache.clear();
this.sourceTileByKey_ = {}; this.sourceTileCache.clear();
this.sourceTilesByTileKey_ = {};
} }
/** /**
@@ -254,7 +247,7 @@ class VectorTile extends UrlTile {
const sourceZ = sourceTileGrid.getZForResolution(resolution, 1); const sourceZ = sourceTileGrid.getZForResolution(resolution, 1);
const minZoom = sourceTileGrid.getMinZoom(); const minZoom = sourceTileGrid.getMinZoom();
const previousSourceTiles = this.sourceTilesByTileKey_[tile.getKey()]; const previousSourceTiles = tile.sourceTiles;
let sourceTiles, covered, loadedZ; let sourceTiles, covered, loadedZ;
if (previousSourceTiles && previousSourceTiles.length > 0 && previousSourceTiles[0].tileCoord[0] === sourceZ) { if (previousSourceTiles && previousSourceTiles.length > 0 && previousSourceTiles[0].tileCoord[0] === sourceZ) {
sourceTiles = previousSourceTiles; sourceTiles = previousSourceTiles;
@@ -270,8 +263,8 @@ class VectorTile extends UrlTile {
const tileUrl = this.tileUrlFunction(sourceTileCoord, pixelRatio, projection); const tileUrl = this.tileUrlFunction(sourceTileCoord, pixelRatio, projection);
let sourceTile; let sourceTile;
if (tileUrl !== undefined) { if (tileUrl !== undefined) {
if (tileUrl in this.sourceTileByKey_) { if (this.sourceTileCache.containsKey(tileUrl)) {
sourceTile = this.sourceTileByKey_[tileUrl]; sourceTile = this.sourceTileCache.get(tileUrl);
const state = sourceTile.getState(); const state = sourceTile.getState();
if (state === TileState.LOADED || state === TileState.ERROR || state === TileState.EMPTY) { if (state === TileState.LOADED || state === TileState.ERROR || state === TileState.EMPTY) {
sourceTiles.push(sourceTile); sourceTiles.push(sourceTile);
@@ -283,7 +276,7 @@ class VectorTile extends UrlTile {
sourceTile.extent = sourceTileGrid.getTileCoordExtent(sourceTileCoord); sourceTile.extent = sourceTileGrid.getTileCoordExtent(sourceTileCoord);
sourceTile.projection = projection; sourceTile.projection = projection;
sourceTile.resolution = sourceTileGrid.getResolution(sourceTileCoord[0]); sourceTile.resolution = sourceTileGrid.getResolution(sourceTileCoord[0]);
this.sourceTileByKey_[tileUrl] = sourceTile; this.sourceTileCache.set(tileUrl, sourceTile);
sourceTile.addEventListener(EventType.CHANGE, this.handleTileChange.bind(this)); sourceTile.addEventListener(EventType.CHANGE, this.handleTileChange.bind(this));
sourceTile.load(); sourceTile.load();
} }
@@ -294,13 +287,12 @@ class VectorTile extends UrlTile {
} }
if (sourceTile.getState() !== TileState.EMPTY && tile.getState() === TileState.IDLE) { if (sourceTile.getState() !== TileState.EMPTY && tile.getState() === TileState.IDLE) {
tile.loadingSourceTiles++; tile.loadingSourceTiles++;
const key = listen(sourceTile, EventType.CHANGE, function() { sourceTile.addEventListener(EventType.CHANGE, function listenChange() {
const state = sourceTile.getState(); const state = sourceTile.getState();
const sourceTileKey = sourceTile.getKey(); const sourceTileKey = sourceTile.getKey();
if (state === TileState.LOADED || state === TileState.ERROR) { if (state === TileState.LOADED || state === TileState.ERROR) {
if (state === TileState.LOADED) { if (state === TileState.LOADED) {
remove(tile.sourceTileListenerKeys, key); sourceTile.removeEventListener(EventType.CHANGE, listenChange);
unlistenByKey(key);
tile.loadingSourceTiles--; tile.loadingSourceTiles--;
delete tile.errorSourceTileKeys[sourceTileKey]; delete tile.errorSourceTileKeys[sourceTileKey];
} else if (state === TileState.ERROR) { } else if (state === TileState.ERROR) {
@@ -314,7 +306,6 @@ class VectorTile extends UrlTile {
} }
} }
}); });
tile.sourceTileListenerKeys.push(key);
} }
}.bind(this)); }.bind(this));
if (!covered) { if (!covered) {
@@ -332,43 +323,13 @@ class VectorTile extends UrlTile {
if (tile.getState() < TileState.LOADED) { if (tile.getState() < TileState.LOADED) {
tile.setState(TileState.LOADED); tile.setState(TileState.LOADED);
} else if (!previousSourceTiles || !equals(sourceTiles, previousSourceTiles)) { } else if (!previousSourceTiles || !equals(sourceTiles, previousSourceTiles)) {
this.removeSourceTiles(tile); tile.sourceTiles = sourceTiles;
this.addSourceTiles(tile, sourceTiles);
} }
} }
this.sourceTileCache.expireCache({});
return sourceTiles; return sourceTiles;
} }
/**
* @param {VectorRenderTile} tile Tile.
* @param {Array<import("../VectorTile").default>} sourceTiles Source tiles.
*/
addSourceTiles(tile, sourceTiles) {
this.sourceTilesByTileKey_[tile.getKey()] = sourceTiles;
for (let i = 0, ii = sourceTiles.length; i < ii; ++i) {
sourceTiles[i].consumers++;
}
}
/**
* @param {VectorRenderTile} tile Tile.
*/
removeSourceTiles(tile) {
const tileKey = tile.getKey();
if (tileKey in this.sourceTilesByTileKey_) {
const sourceTiles = this.sourceTilesByTileKey_[tileKey];
for (let i = 0, ii = sourceTiles.length; i < ii; ++i) {
const sourceTile = sourceTiles[i];
sourceTile.consumers--;
if (sourceTile.consumers === 0) {
sourceTile.dispose();
delete this.sourceTileByKey_[sourceTile.getKey()];
}
}
}
delete this.sourceTilesByTileKey_[tileKey];
}
/** /**
* @inheritDoc * @inheritDoc
*/ */
@@ -410,9 +371,7 @@ class VectorTile extends UrlTile {
tileCoord, tileCoord,
empty ? TileState.EMPTY : TileState.IDLE, empty ? TileState.EMPTY : TileState.IDLE,
urlTileCoord, urlTileCoord,
this.tileGrid, this.getSourceTiles.bind(this, pixelRatio, projection));
this.getSourceTiles.bind(this, pixelRatio, projection),
this.removeSourceTiles.bind(this));
newTile.key = key; newTile.key = key;
if (tile) { if (tile) {

View File

@@ -83,21 +83,4 @@ describe('ol.ImageTile', function() {
}); });
describe('dispose', function() {
it('sets image src to a blank image data uri', function() {
const tileCoord = [0, 0, 0];
const state = TileState.IDLE;
const src = 'spec/ol/data/osm-0-0-0.png';
const tileLoadFunction = defaultImageLoadFunction;
const tile = new ImageTile(tileCoord, state, src, null, tileLoadFunction);
tile.load();
expect(tile.getState()).to.be(TileState.LOADING);
tile.dispose();
expect(tile.getState()).to.be(TileState.ABORT);
expect(tile.getImage().src).to.be(ImageTile.blankImageUrl);
});
});
}); });

View File

@@ -2,25 +2,11 @@ import LabelCache from '../../../../../src/ol/render/canvas/LabelCache.js';
describe('ol.render.canvas.LabelCache', function() { describe('ol.render.canvas.LabelCache', function() {
it('#prune()', function() { it('#expireCache()', function() {
const labelCache = new LabelCache(1); const labelCache = new LabelCache(1);
labelCache.set('key1', document.createElement('canvas')); labelCache.set('key1', document.createElement('canvas'));
labelCache.set('key2', document.createElement('canvas')); labelCache.set('key2', document.createElement('canvas'));
labelCache.prune(); labelCache.expireCache();
expect(labelCache.getCount()).to.be(1);
});
it('#prune() leaves used labels untouched until consumer is released', function() {
const labelCache = new LabelCache(1);
labelCache.set('key1', document.createElement('canvas'));
labelCache.set('key2', document.createElement('canvas'));
const consumer = {};
labelCache.get('key1', consumer);
labelCache.get('key2', consumer);
labelCache.prune();
expect(labelCache.getCount()).to.be(2);
labelCache.release(consumer);
labelCache.prune();
expect(labelCache.getCount()).to.be(1); expect(labelCache.getCount()).to.be(1);
}); });

View File

@@ -78,15 +78,6 @@ describe('ol.renderer.canvas.TileLayer', function() {
disposeMap(map); disposeMap(map);
}); });
it('increases the cache size if necessary', function(done) {
const tileCache = layer.getSource().tileCache;
expect(tileCache.highWaterMark).to.be(1);
map.once('rendercomplete', function() {
expect(tileCache.highWaterMark).to.be(2);
done();
});
});
it('respects the source\'s zDirection setting', function(done) { it('respects the source\'s zDirection setting', function(done) {
layer.getSource().zDirection = 1; layer.getSource().zDirection = 1;
map.getView().setZoom(5.8); // would lead to z6 tile request with the default zDirection map.getView().setZoom(5.8); // would lead to z6 tile request with the default zDirection

View File

@@ -255,11 +255,10 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
sourceTile.getImage = function() { sourceTile.getImage = function() {
return document.createElement('canvas'); return document.createElement('canvas');
}; };
const tile = new VectorRenderTile([0, 0, 0], 1, [0, 0, 0], createXYZ(), const tile = new VectorRenderTile([0, 0, 0], 1, [0, 0, 0],
function() { function() {
return sourceTile; return sourceTile;
}, });
function() {});
tile.transition_ = 0; tile.transition_ = 0;
tile.setState(TileState.LOADED); tile.setState(TileState.LOADED);
layer.getSource().getTile = function() { layer.getSource().getTile = function() {
@@ -316,11 +315,11 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
tileClass: TileClass, tileClass: TileClass,
tileGrid: createXYZ() tileGrid: createXYZ()
}); });
source.sourceTileByKey_[sourceTile.getKey()] = sourceTile; source.sourceTileCache.set('0/0/0.mvt', sourceTile);
source.sourceTilesByTileKey_[sourceTile.getKey()] = [sourceTile];
executorGroup = {}; executorGroup = {};
source.getTile = function() { source.getTile = function() {
const tile = VectorTileSource.prototype.getTile.apply(source, arguments); const tile = VectorTileSource.prototype.getTile.apply(source, arguments);
tile.sourceTiles = [sourceTile];
tile.executorGroups[getUid(layer)] = [executorGroup]; tile.executorGroups[getUid(layer)] = [executorGroup];
return tile; return tile;
}; };

View File

@@ -218,19 +218,6 @@ describe('ol.source.TileImage', function() {
const tile = source.getTile(0, 0, 0, 1, getProjection('EPSG:3857')); const tile = source.getTile(0, 0, 0, 1, getProjection('EPSG:3857'));
tile.load(); tile.load();
}); });
it('dispatches tileloadend events for aborted tiles', function() {
source.setTileLoadFunction(function() {});
const startSpy = sinon.spy();
source.on('tileloadstart', startSpy);
const endSpy = sinon.spy();
source.on('tileloadend', endSpy);
const tile = source.getTile(0, 0, 0, 1, getProjection('EPSG:3857'));
tile.load();
tile.dispose();
expect(startSpy.callCount).to.be(1);
expect(endSpy.callCount).to.be(1);
});
}); });
}); });

View File

@@ -32,8 +32,6 @@ describe('ol.TileCache', function() {
'2/1/0', '2/1/0',
'2/0/0' '2/0/0'
]); ]);
expect(tiles[0].dispose.calledOnce).to.be(true);
}); });
}); });
}); });

View File

@@ -89,20 +89,6 @@ describe('ol.TileQueue', function() {
}); });
it('calls #tileChangeCallback_ when all wanted tiles are aborted', function() {
const tileChangeCallback = sinon.spy();
const queue = new TileQueue(noop, tileChangeCallback);
const numTiles = 20;
for (let i = 0; i < numTiles; ++i) {
const tile = createImageTile();
tile.state = TileState.ABORT;
queue.enqueue([tile]);
}
const maxLoading = numTiles / 2;
queue.loadMoreTiles(maxLoading, maxLoading);
expect(tileChangeCallback.callCount).to.be(1);
});
}); });
describe('heapify', function() { describe('heapify', function() {
@@ -144,7 +130,7 @@ describe('ol.TileQueue', function() {
describe('tile change event', function() { describe('tile change event', function() {
const noop = function() {}; const noop = function() {};
it('abort queued tiles', function() { it('loaded tiles', function() {
const tq = new TileQueue(noop, noop); const tq = new TileQueue(noop, noop);
const tile = createImageTile(); const tile = createImageTile();
expect(tile.hasListener('change')).to.be(false); expect(tile.hasListener('change')).to.be(false);
@@ -152,12 +138,11 @@ describe('ol.TileQueue', function() {
tq.enqueue([tile]); tq.enqueue([tile]);
expect(tile.hasListener('change')).to.be(true); expect(tile.hasListener('change')).to.be(true);
tile.dispose(); tile.setState(TileState.LOADED);
expect(tile.hasListener('change')).to.be(false); expect(tile.hasListener('change')).to.be(false);
expect(tile.getState()).to.eql(5); // ABORT
}); });
it('abort loading tiles', function() { it('error tiles', function() {
const tq = new TileQueue(noop, noop); const tq = new TileQueue(noop, noop);
const tile = createImageTile(noop); const tile = createImageTile(noop);
@@ -166,10 +151,9 @@ describe('ol.TileQueue', function() {
expect(tq.getTilesLoading()).to.eql(1); expect(tq.getTilesLoading()).to.eql(1);
expect(tile.getState()).to.eql(1); // LOADING expect(tile.getState()).to.eql(1); // LOADING
tile.dispose(); tile.setState(TileState.ERROR);
expect(tq.getTilesLoading()).to.eql(0); expect(tq.getTilesLoading()).to.eql(0);
expect(tile.hasListener('change')).to.be(false); expect(tile.hasListener('change')).to.be(false);
expect(tile.getState()).to.eql(5); // ABORT
}); });

View File

@@ -1,9 +1,8 @@
import TileState from '../../../src/ol/TileState.js'; import TileState from '../../../src/ol/TileState.js';
import {defaultLoadFunction} from '../../../src/ol/source/VectorTile.js'; import {defaultLoadFunction} from '../../../src/ol/source/VectorTile.js';
import VectorTileSource from '../../../src/ol/source/VectorTile.js'; import VectorTileSource from '../../../src/ol/source/VectorTile.js';
import {listen, listenOnce, unlistenByKey} from '../../../src/ol/events.js'; import {listen, unlistenByKey} from '../../../src/ol/events.js';
import GeoJSON from '../../../src/ol/format/GeoJSON.js'; import GeoJSON from '../../../src/ol/format/GeoJSON.js';
import {createXYZ} from '../../../src/ol/tilegrid.js';
import TileGrid from '../../../src/ol/tilegrid/TileGrid.js'; import TileGrid from '../../../src/ol/tilegrid/TileGrid.js';
import EventType from '../../../src/ol/events/EventType.js'; import EventType from '../../../src/ol/events/EventType.js';
@@ -95,52 +94,4 @@ describe('ol.VectorRenderTile', function() {
}); });
}); });
it('#dispose() while loading', function() {
const source = new VectorTileSource({
format: new GeoJSON(),
url: 'spec/ol/data/point.json',
tileGrid: createXYZ()
});
source.getTileGridForProjection = function() {
return createXYZ({tileSize: 512});
};
const tile = source.getTile(0, 0, 0, 1, source.getProjection());
tile.load();
expect(tile.getState()).to.be(TileState.LOADING);
tile.dispose();
expect(source.sourceTilesByTileKey_[tile.getKey()]).to.be(undefined);
expect(tile.getState()).to.be(TileState.ABORT);
});
it('#dispose() when source tiles are loaded', function(done) {
const source = new VectorTileSource({
format: new GeoJSON(),
url: 'spec/ol/data/point.json?{z}/{x}/{y}',
tileGrid: createXYZ()
});
source.getTileGridForProjection = function() {
return createXYZ({tileSize: 512});
};
const tile = source.getTile(0, 0, 0, 1, source.getProjection());
tile.load();
listenOnce(tile, 'change', function() {
expect(tile.getState()).to.be(TileState.LOADED);
expect(tile.loadingSourceTiles).to.be(0);
const sourceTiles = source.getSourceTiles(1, source.getProjection(), tile);
expect(sourceTiles.length).to.be(4);
for (let i = 0, ii = sourceTiles.length; i < ii; ++i) {
expect(sourceTiles[i].consumers).to.be(1);
}
tile.dispose();
expect(tile.getState()).to.be(TileState.ABORT);
for (let i = 0, ii = sourceTiles.length; i < ii; ++i) {
expect(sourceTiles[i].consumers).to.be(0);
expect(sourceTiles[i].getState()).to.be(TileState.ABORT);
}
done();
});
});
}); });