Merge pull request #9318 from ahocevar/smart-cache-size

Smart cache size
This commit is contained in:
Andreas Hocevar
2019-03-13 08:17:24 +01:00
committed by GitHub
14 changed files with 171 additions and 115 deletions

View File

@@ -1,10 +1,9 @@
/**
* @module ol/renderer/Layer
*/
import {getUid, abstract} from '../util.js';
import {abstract} from '../util.js';
import ImageState from '../ImageState.js';
import Observable from '../Observable.js';
import TileState from '../TileState.js';
import {listen} from '../events.js';
import EventType from '../events/EventType.js';
import SourceState from '../source/State.js';
@@ -165,108 +164,6 @@ class LayerRenderer extends Observable {
}
}
/**
* @param {import("../PluggableMap.js").FrameState} frameState Frame state.
* @param {import("../source/Tile.js").default} tileSource Tile source.
* @protected
*/
scheduleExpireCache(frameState, tileSource) {
if (tileSource.canExpireCache()) {
/**
* @param {import("../source/Tile.js").default} tileSource Tile source.
* @param {import("../PluggableMap.js").default} map Map.
* @param {import("../PluggableMap.js").FrameState} frameState Frame state.
*/
const postRenderFunction = function(tileSource, map, frameState) {
const tileSourceKey = getUid(tileSource);
if (tileSourceKey in frameState.usedTiles) {
tileSource.expireCache(frameState.viewState.projection,
frameState.usedTiles[tileSourceKey]);
}
}.bind(null, tileSource);
frameState.postRenderFunctions.push(
/** @type {import("../PluggableMap.js").PostRenderFunction} */ (postRenderFunction)
);
}
}
/**
* @param {!Object<string, !Object<string, boolean>>} usedTiles Used tiles.
* @param {import("../source/Tile.js").default} tileSource Tile source.
* @param {import('../Tile.js').default} tile Tile.
* @protected
*/
updateUsedTiles(usedTiles, tileSource, tile) {
// FIXME should we use tilesToDrawByZ instead?
const tileSourceKey = getUid(tileSource);
if (!(tileSourceKey in usedTiles)) {
usedTiles[tileSourceKey] = {};
}
usedTiles[tileSourceKey][tile.getKey()] = true;
}
/**
* Manage tile pyramid.
* This function performs a number of functions related to the tiles at the
* current zoom and lower zoom levels:
* - registers idle tiles in frameState.wantedTiles so that they are not
* discarded by the tile queue
* - enqueues missing tiles
* @param {import("../PluggableMap.js").FrameState} frameState Frame state.
* @param {import("../source/Tile.js").default} tileSource Tile source.
* @param {import("../tilegrid/TileGrid.js").default} tileGrid Tile grid.
* @param {number} pixelRatio Pixel ratio.
* @param {import("../proj/Projection.js").default} projection Projection.
* @param {import("../extent.js").Extent} extent Extent.
* @param {number} currentZ Current Z.
* @param {number} preload Load low resolution tiles up to 'preload' levels.
* @param {function(import("../Tile.js").default)=} opt_tileCallback Tile callback.
* @protected
*/
manageTilePyramid(
frameState,
tileSource,
tileGrid,
pixelRatio,
projection,
extent,
currentZ,
preload,
opt_tileCallback
) {
const tileSourceKey = getUid(tileSource);
if (!(tileSourceKey in frameState.wantedTiles)) {
frameState.wantedTiles[tileSourceKey] = {};
}
const wantedTiles = frameState.wantedTiles[tileSourceKey];
const tileQueue = frameState.tileQueue;
const minZoom = tileGrid.getMinZoom();
let tile, tileRange, tileResolution, x, y, z;
for (z = minZoom; z <= currentZ; ++z) {
tileRange = tileGrid.getTileRangeForExtentAndZ(extent, z, tileRange);
tileResolution = tileGrid.getResolution(z);
for (x = tileRange.minX; x <= tileRange.maxX; ++x) {
for (y = tileRange.minY; y <= tileRange.maxY; ++y) {
if (currentZ - z <= preload) {
tile = tileSource.getTile(z, x, y, pixelRatio, projection);
if (tile.getState() == TileState.IDLE) {
wantedTiles[tile.getKey()] = true;
if (!tileQueue.isKeyQueued(tile.getKey())) {
tileQueue.enqueue([tile, tileSourceKey,
tileGrid.getTileCoordCenter(tile.tileCoord), tileResolution]);
}
}
if (opt_tileCallback !== undefined) {
opt_tileCallback(tile);
}
} else {
tileSource.useTile(z, x, y, projection);
}
}
}
}
}
}
export default LayerRenderer;

View File

@@ -304,6 +304,7 @@ class CanvasTileLayerRenderer extends CanvasLayerRenderer {
this.manageTilePyramid(frameState, tileSource, tileGrid, pixelRatio,
projection, extent, z, tileLayer.getPreload());
this.updateCacheSize_(frameState, tileSource);
this.scheduleExpireCache(frameState, tileSource);
this.postRender(context, frameState);
@@ -383,6 +384,130 @@ class CanvasTileLayerRenderer extends CanvasLayerRenderer {
return /** @type {import("../../ImageTile.js").default} */ (tile).getImage();
}
/**
* @param {import("../../PluggableMap.js").FrameState} frameState Frame state.
* @param {import("../../source/Tile.js").default} tileSource Tile source.
* @protected
*/
scheduleExpireCache(frameState, tileSource) {
if (tileSource.canExpireCache()) {
/**
* @param {import("../../source/Tile.js").default} tileSource Tile source.
* @param {import("../../PluggableMap.js").default} map Map.
* @param {import("../../PluggableMap.js").FrameState} frameState Frame state.
*/
const postRenderFunction = function(tileSource, map, frameState) {
const tileSourceKey = getUid(tileSource);
if (tileSourceKey in frameState.usedTiles) {
tileSource.expireCache(frameState.viewState.projection,
frameState.usedTiles[tileSourceKey]);
}
}.bind(null, tileSource);
frameState.postRenderFunctions.push(
/** @type {import("../../PluggableMap.js").PostRenderFunction} */ (postRenderFunction)
);
}
}
/**
* @param {!Object<string, !Object<string, boolean>>} usedTiles Used tiles.
* @param {import("../../source/Tile.js").default} tileSource Tile source.
* @param {import('../../Tile.js').default} tile Tile.
* @protected
*/
updateUsedTiles(usedTiles, tileSource, tile) {
// FIXME should we use tilesToDrawByZ instead?
const tileSourceKey = getUid(tileSource);
if (!(tileSourceKey in usedTiles)) {
usedTiles[tileSourceKey] = {};
}
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.
* This function performs a number of functions related to the tiles at the
* current zoom and lower zoom levels:
* - registers idle tiles in frameState.wantedTiles so that they are not
* discarded by the tile queue
* - enqueues missing tiles
* @param {import("../../PluggableMap.js").FrameState} frameState Frame state.
* @param {import("../../source/Tile.js").default} tileSource Tile source.
* @param {import("../../tilegrid/TileGrid.js").default} tileGrid Tile grid.
* @param {number} pixelRatio Pixel ratio.
* @param {import("../../proj/Projection.js").default} projection Projection.
* @param {import("../../extent.js").Extent} extent Extent.
* @param {number} currentZ Current Z.
* @param {number} preload Load low resolution tiles up to 'preload' levels.
* @param {function(import("../../Tile.js").default)=} opt_tileCallback Tile callback.
* @protected
*/
manageTilePyramid(
frameState,
tileSource,
tileGrid,
pixelRatio,
projection,
extent,
currentZ,
preload,
opt_tileCallback
) {
const tileSourceKey = getUid(tileSource);
if (!(tileSourceKey in frameState.wantedTiles)) {
frameState.wantedTiles[tileSourceKey] = {};
}
const wantedTiles = frameState.wantedTiles[tileSourceKey];
const tileQueue = frameState.tileQueue;
const minZoom = tileGrid.getMinZoom();
let tile, tileRange, tileResolution, x, y, z;
for (z = minZoom; z <= currentZ; ++z) {
tileRange = tileGrid.getTileRangeForExtentAndZ(extent, z, tileRange);
tileResolution = tileGrid.getResolution(z);
for (x = tileRange.minX; x <= tileRange.maxX; ++x) {
for (y = tileRange.minY; y <= tileRange.maxY; ++y) {
if (currentZ - z <= preload) {
tile = tileSource.getTile(z, x, y, pixelRatio, projection);
if (tile.getState() == TileState.IDLE) {
wantedTiles[tile.getKey()] = true;
if (!tileQueue.isKeyQueued(tile.getKey())) {
tileQueue.enqueue([tile, tileSourceKey,
tileGrid.getTileCoordCenter(tile.tileCoord), tileResolution]);
}
}
if (opt_tileCallback !== undefined) {
opt_tileCallback(tile);
}
} else {
tileSource.useTile(z, x, y, projection);
}
}
}
}
}
}

View File

@@ -50,7 +50,7 @@ const TOS_ATTRIBUTION = '<a class="ol-attribution-bing-tos" ' +
/**
* @typedef {Object} Options
* @property {number} [cacheSize] Tile cache size. Default is four times as many tiles as a fullscreen map needs.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small.
* @property {boolean} [hidpi=false] If `true` hidpi tiles will be requested.
* @property {string} [culture='en-us'] Culture code.
* @property {string} key Bing Maps API key. Get yours at http://www.bingmapsportal.com/.

View File

@@ -9,7 +9,7 @@ import XYZ from './XYZ.js';
/**
* @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. Default is four times as many tiles as a fullscreen map needs.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -20,7 +20,7 @@ export const ATTRIBUTION = '&#169; ' +
/**
* @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. Default is four times as many tiles as a fullscreen map needs.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -90,7 +90,7 @@ const ProviderConfig = {
/**
* @typedef {Object} Options
* @property {number} [cacheSize] Tile cache size. Default is four times as many tiles as a fullscreen map needs.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small.
* @property {string} layer Layer name.
* @property {number} [minZoom] Minimum zoom.
* @property {number} [maxZoom] Maximum zoom.

View File

@@ -13,7 +13,7 @@ import {appendParams} from '../uri.js';
/**
* @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. Default is four times as many tiles as a fullscreen map needs.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -18,7 +18,7 @@ import {getForProjection as getTileGridForProjection} from '../tilegrid.js';
* @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {boolean} [attributionsCollapsible=true] Attributions are collapsible.
* @property {number} [cacheSize] Tile cache size. Default is four times as many tiles as a fullscreen map needs.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -39,7 +39,7 @@ import {createXYZ, extentFromProjection} from '../tilegrid.js';
/**
* @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. Default is four times as many tiles as a fullscreen map needs.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -20,7 +20,7 @@ import {appendParams} from '../uri.js';
/**
* @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. Default is four times as many tiles as a fullscreen map needs.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -15,7 +15,7 @@ import {appendParams} from '../uri.js';
/**
* @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. Default is four times as many tiles as a fullscreen map needs.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -9,7 +9,7 @@ import {createXYZ, extentFromProjection} from '../tilegrid.js';
* @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {boolean} [attributionsCollapsible=true] Attributions are collapsible.
* @property {number} [cacheSize] Tile cache size. Default is four times as many tiles as a fullscreen map needs.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -82,7 +82,7 @@ export class CustomTile extends ImageTile {
/**
* @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. Default is four times as many tiles as a fullscreen map needs.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -2,6 +2,8 @@ import Map from '../../../../../src/ol/Map.js';
import View from '../../../../../src/ol/View.js';
import TileLayer from '../../../../../src/ol/layer/Tile.js';
import TileWMS from '../../../../../src/ol/source/TileWMS.js';
import XYZ from '../../../../../src/ol/source/XYZ.js';
import {fromLonLat} from '../../../../../src/ol/proj.js';
describe('ol.renderer.canvas.TileLayer', function() {
@@ -49,4 +51,36 @@ describe('ol.renderer.canvas.TileLayer', function() {
});
});
describe('#renderFrame', function() {
let map, layer;
beforeEach(function() {
layer = new TileLayer({
source: new XYZ({
cacheSize: 1,
url: 'rendering/ol/data/tiles/osm/{z}/{x}/{y}.png'
})
});
map = new Map({
target: createMapDiv(100, 100),
layers: [layer],
view: new View({
center: fromLonLat([-122.416667, 37.783333]),
zoom: 5
})
});
});
afterEach(function() {
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();
});
});
});
});