TileReplacementQueue.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import defined from "../Core/defined.js";
  2. /**
  3. * A priority queue of tiles to be replaced, if necessary, to make room for new tiles. The queue
  4. * is implemented as a linked list.
  5. *
  6. * @alias TileReplacementQueue
  7. * @private
  8. */
  9. function TileReplacementQueue() {
  10. this.head = undefined;
  11. this.tail = undefined;
  12. this.count = 0;
  13. this._lastBeforeStartOfFrame = undefined;
  14. }
  15. /**
  16. * Marks the start of the render frame. Tiles before (closer to the head) this tile in the
  17. * list were used last frame and must not be unloaded.
  18. */
  19. TileReplacementQueue.prototype.markStartOfRenderFrame = function () {
  20. this._lastBeforeStartOfFrame = this.head;
  21. };
  22. /**
  23. * Reduces the size of the queue to a specified size by unloading the least-recently used
  24. * tiles. Tiles that were used last frame will not be unloaded, even if that puts the number
  25. * of tiles above the specified maximum.
  26. *
  27. * @param {Number} maximumTiles The maximum number of tiles in the queue.
  28. */
  29. TileReplacementQueue.prototype.trimTiles = function (maximumTiles) {
  30. let tileToTrim = this.tail;
  31. let keepTrimming = true;
  32. while (
  33. keepTrimming &&
  34. defined(this._lastBeforeStartOfFrame) &&
  35. this.count > maximumTiles &&
  36. defined(tileToTrim)
  37. ) {
  38. // Stop trimming after we process the last tile not used in the
  39. // current frame.
  40. keepTrimming = tileToTrim !== this._lastBeforeStartOfFrame;
  41. const previous = tileToTrim.replacementPrevious;
  42. if (tileToTrim.eligibleForUnloading) {
  43. tileToTrim.freeResources();
  44. remove(this, tileToTrim);
  45. }
  46. tileToTrim = previous;
  47. }
  48. };
  49. function remove(tileReplacementQueue, item) {
  50. const previous = item.replacementPrevious;
  51. const next = item.replacementNext;
  52. if (item === tileReplacementQueue._lastBeforeStartOfFrame) {
  53. tileReplacementQueue._lastBeforeStartOfFrame = next;
  54. }
  55. if (item === tileReplacementQueue.head) {
  56. tileReplacementQueue.head = next;
  57. } else {
  58. previous.replacementNext = next;
  59. }
  60. if (item === tileReplacementQueue.tail) {
  61. tileReplacementQueue.tail = previous;
  62. } else {
  63. next.replacementPrevious = previous;
  64. }
  65. item.replacementPrevious = undefined;
  66. item.replacementNext = undefined;
  67. --tileReplacementQueue.count;
  68. }
  69. /**
  70. * Marks a tile as rendered this frame and moves it before the first tile that was not rendered
  71. * this frame.
  72. *
  73. * @param {TileReplacementQueue} item The tile that was rendered.
  74. */
  75. TileReplacementQueue.prototype.markTileRendered = function (item) {
  76. const head = this.head;
  77. if (head === item) {
  78. if (item === this._lastBeforeStartOfFrame) {
  79. this._lastBeforeStartOfFrame = item.replacementNext;
  80. }
  81. return;
  82. }
  83. ++this.count;
  84. if (!defined(head)) {
  85. // no other tiles in the list
  86. item.replacementPrevious = undefined;
  87. item.replacementNext = undefined;
  88. this.head = item;
  89. this.tail = item;
  90. return;
  91. }
  92. if (defined(item.replacementPrevious) || defined(item.replacementNext)) {
  93. // tile already in the list, remove from its current location
  94. remove(this, item);
  95. }
  96. item.replacementPrevious = undefined;
  97. item.replacementNext = head;
  98. head.replacementPrevious = item;
  99. this.head = item;
  100. };
  101. export default TileReplacementQueue;