Cesium3DTilesetCache.js 2.2 KB

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