TileReplacementQueue.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. var tileToTrim = this.tail;
  31. var keepTrimming = true;
  32. while (keepTrimming &&
  33. defined(this._lastBeforeStartOfFrame) &&
  34. this.count > maximumTiles &&
  35. defined(tileToTrim)) {
  36. // Stop trimming after we process the last tile not used in the
  37. // current frame.
  38. keepTrimming = tileToTrim !== this._lastBeforeStartOfFrame;
  39. var previous = tileToTrim.replacementPrevious;
  40. if (tileToTrim.eligibleForUnloading) {
  41. tileToTrim.freeResources();
  42. remove(this, tileToTrim);
  43. }
  44. tileToTrim = previous;
  45. }
  46. };
  47. function remove(tileReplacementQueue, item) {
  48. var previous = item.replacementPrevious;
  49. var next = item.replacementNext;
  50. if (item === tileReplacementQueue._lastBeforeStartOfFrame) {
  51. tileReplacementQueue._lastBeforeStartOfFrame = next;
  52. }
  53. if (item === tileReplacementQueue.head) {
  54. tileReplacementQueue.head = next;
  55. } else {
  56. previous.replacementNext = next;
  57. }
  58. if (item === tileReplacementQueue.tail) {
  59. tileReplacementQueue.tail = previous;
  60. } else {
  61. next.replacementPrevious = previous;
  62. }
  63. item.replacementPrevious = undefined;
  64. item.replacementNext = undefined;
  65. --tileReplacementQueue.count;
  66. }
  67. /**
  68. * Marks a tile as rendered this frame and moves it before the first tile that was not rendered
  69. * this frame.
  70. *
  71. * @param {TileReplacementQueue} item The tile that was rendered.
  72. */
  73. TileReplacementQueue.prototype.markTileRendered = function(item) {
  74. var head = this.head;
  75. if (head === item) {
  76. if (item === this._lastBeforeStartOfFrame) {
  77. this._lastBeforeStartOfFrame = item.replacementNext;
  78. }
  79. return;
  80. }
  81. ++this.count;
  82. if (!defined(head)) {
  83. // no other tiles in the list
  84. item.replacementPrevious = undefined;
  85. item.replacementNext = undefined;
  86. this.head = item;
  87. this.tail = item;
  88. return;
  89. }
  90. if (defined(item.replacementPrevious) || defined(item.replacementNext)) {
  91. // tile already in the list, remove from its current location
  92. remove(this, item);
  93. }
  94. item.replacementPrevious = undefined;
  95. item.replacementNext = head;
  96. head.replacementPrevious = item;
  97. this.head = item;
  98. };
  99. export default TileReplacementQueue;