All files / engine/Source/Scene Cesium3DTilesetCache.js

100% Statements 31/31
100% Branches 9/9
100% Functions 7/7
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                      671x 671x 671x     1x       5843x     1x 18834x 18834x 8222x       1x 1461x 1445x       1x         63x 63x 1x     62x 62x 62x     1x       5842x 5842x   5842x           5842x 5842x 5842x       58x 58x 58x       1x 13x      
import defined from "../Core/defined.js";
import DoublyLinkedList from "../Core/DoublyLinkedList.js";
 
/**
 * Stores tiles with content loaded.
 *
 * @private
 */
function Cesium3DTilesetCache() {
  // [head, sentinel) -> tiles that weren't selected this frame and may be removed from the cache
  // (sentinel, tail] -> tiles that were selected this frame
  this._list = new DoublyLinkedList();
  this._sentinel = this._list.add();
  this._trimTiles = false;
}
 
Cesium3DTilesetCache.prototype.reset = function () {
  // Move sentinel node to the tail so, at the start of the frame, all tiles
  // may be potentially replaced.  Tiles are moved to the right of the sentinel
  // when they are selected so they will not be replaced.
  this._list.splice(this._list.tail, this._sentinel);
};
 
Cesium3DTilesetCache.prototype.touch = function (tile) {
  const node = tile.cacheNode;
  if (defined(node)) {
    this._list.splice(this._sentinel, node);
  }
};
 
Cesium3DTilesetCache.prototype.add = function (tile) {
  if (!defined(tile.cacheNode)) {
    tile.cacheNode = this._list.add(tile);
  }
};
 
Cesium3DTilesetCache.prototype.unloadTile = function (
  tileset,
  tile,
  unloadCallback,
) {
  const node = tile.cacheNode;
  if (!defined(node)) {
    return;
  }
 
  this._list.remove(node);
  tile.cacheNode = undefined;
  unloadCallback(tileset, tile);
};
 
Cesium3DTilesetCache.prototype.unloadTiles = function (
  tileset,
  unloadCallback,
) {
  const trimTiles = this._trimTiles;
  this._trimTiles = false;
 
  const list = this._list;
 
  // Traverse the list only to the sentinel since tiles/nodes to the
  // right of the sentinel were used this frame.
  //
  // The sub-list to the left of the sentinel is ordered from LRU to MRU.
  const sentinel = this._sentinel;
  let node = list.head;
  while (
    node !== sentinel &&
    (tileset.totalMemoryUsageInBytes > tileset.cacheBytes || trimTiles)
  ) {
    const tile = node.item;
    node = node.next;
    this.unloadTile(tileset, tile, unloadCallback);
  }
};
 
Cesium3DTilesetCache.prototype.trim = function () {
  this._trimTiles = true;
};
export default Cesium3DTilesetCache;