diff --git a/src/source/custom_source.js b/src/source/custom_source.js index 03bab71e927..6fbdbfd7a87 100644 --- a/src/source/custom_source.js +++ b/src/source/custom_source.js @@ -2,8 +2,8 @@ import Tile from './tile.js'; import window from '../util/window.js'; +import Texture from '../render/texture.js'; import TileBounds from './tile_bounds.js'; -import RasterTileSource from './raster_tile_source.js'; import {extend, pick} from '../util/util.js'; import {Event, ErrorEvent, Evented} from '../util/evented.js'; import {makeFQID} from '../util/fqid.js'; @@ -314,24 +314,31 @@ class CustomSource extends Evented implements Source { loadTileData(tile: Tile, data: T): void { // Only raster data supported at the moment - RasterTileSource.loadTileData(tile, (data: any), this._map.painter); - } - - unloadTileData(tile: Tile): void { - // Only raster data supported at the moment - RasterTileSource.unloadTileData(tile, this._map.painter); + tile.setTexture((data: any), this._map.painter); } // $FlowFixMe[method-unbinding] unloadTile(tile: Tile, callback: Callback): void { - this.unloadTileData(tile); + // Only raster data supported at the moment + // Cache the tile texture to avoid re-allocating Textures if they'll just be reloaded + if (tile.texture && tile.texture instanceof Texture) { + // Clean everything else up owned by the tile, but preserve the texture. + // Destroy first to prevent racing with the texture cache being popped. + tile.destroy(true); + + // Save the texture to the cache + if (tile.texture && tile.texture instanceof Texture) { + this._map.painter.saveTileTexture(tile.texture); + } + } else { + tile.destroy(); + } + if (this._implementation.unloadTile) { const {x, y, z} = tile.tileID.canonical; this._implementation.unloadTile({x, y, z}); } - tile.destroy(); - callback(); } diff --git a/src/source/raster_tile_source.js b/src/source/raster_tile_source.js index db3b6b20533..a0262d01ea8 100644 --- a/src/source/raster_tile_source.js +++ b/src/source/raster_tile_source.js @@ -14,12 +14,10 @@ import {makeFQID} from '../util/fqid.js'; import type {Source} from './source.js'; import type {OverscaledTileID} from './tile_id.js'; import type Map from '../ui/map.js'; -import type Painter from '../render/painter.js'; import type Dispatcher from '../util/dispatcher.js'; import type Tile from './tile.js'; import type {Callback} from '../types/callback.js'; import type {Cancelable} from '../types/cancelable.js'; -import type {TextureImage} from "../render/texture.js"; import type { RasterSourceSpecification, RasterDEMSourceSpecification @@ -219,15 +217,6 @@ class RasterTileSource extends Evented implements Source { }); } - static loadTileData(tile: Tile, data: TextureImage, painter: Painter) { - tile.setTexture(data, painter); - } - - // eslint-disable-next-line no-unused-vars - static unloadTileData(tile: Tile, painter: Painter) { - // Texture caching on unload occurs in unloadTile - } - // $FlowFixMe[method-unbinding] abortTile(tile: Tile, callback: Callback) { if (tile.request) {