Skip to content

Commit

Permalink
Duplicate tile reloading logic from RasterSource in CustomSource
Browse files Browse the repository at this point in the history
  • Loading branch information
stepankuzmin committed Dec 4, 2023
1 parent 94b9007 commit 833d170
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
27 changes: 17 additions & 10 deletions src/source/custom_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -314,24 +314,31 @@ class CustomSource<T> 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>): 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();
}

Expand Down
11 changes: 0 additions & 11 deletions src/source/raster_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<void>) {
if (tile.request) {
Expand Down

0 comments on commit 833d170

Please sign in to comment.