Cesium3DTilesetCache.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. var 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(tileset, tile, unloadCallback) {
  33. var node = tile.cacheNode;
  34. if (!defined(node)) {
  35. return;
  36. }
  37. this._list.remove(node);
  38. tile.cacheNode = undefined;
  39. unloadCallback(tileset, tile);
  40. };
  41. Cesium3DTilesetCache.prototype.unloadTiles = function(tileset, unloadCallback) {
  42. var trimTiles = this._trimTiles;
  43. this._trimTiles = false;
  44. var list = this._list;
  45. var maximumMemoryUsageInBytes = tileset.maximumMemoryUsage * 1024 * 1024;
  46. // Traverse the list only to the sentinel since tiles/nodes to the
  47. // right of the sentinel were used this frame.
  48. //
  49. // The sub-list to the left of the sentinel is ordered from LRU to MRU.
  50. var sentinel = this._sentinel;
  51. var node = list.head;
  52. while ((node !== sentinel) && ((tileset.totalMemoryUsageInBytes > maximumMemoryUsageInBytes) || trimTiles)) {
  53. var tile = node.item;
  54. node = node.next;
  55. this.unloadTile(tileset, tile, unloadCallback);
  56. }
  57. };
  58. Cesium3DTilesetCache.prototype.trim = function() {
  59. this._trimTiles = true;
  60. };
  61. export default Cesium3DTilesetCache;