Imagery.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import defined from "../Core/defined.js";
  2. import destroyObject from "../Core/destroyObject.js";
  3. import ImageryState from "./ImageryState.js";
  4. /**
  5. * Stores details about a tile of imagery.
  6. *
  7. * @alias Imagery
  8. * @private
  9. */
  10. function Imagery(imageryLayer, x, y, level, rectangle) {
  11. this.imageryLayer = imageryLayer;
  12. this.x = x;
  13. this.y = y;
  14. this.level = level;
  15. this.request = undefined;
  16. if (level !== 0) {
  17. const parentX = (x / 2) | 0;
  18. const parentY = (y / 2) | 0;
  19. const parentLevel = level - 1;
  20. this.parent = imageryLayer.getImageryFromCache(
  21. parentX,
  22. parentY,
  23. parentLevel
  24. );
  25. }
  26. this.state = ImageryState.UNLOADED;
  27. this.imageUrl = undefined;
  28. this.image = undefined;
  29. this.texture = undefined;
  30. this.textureWebMercator = undefined;
  31. this.credits = undefined;
  32. this.referenceCount = 0;
  33. if (!defined(rectangle) && imageryLayer.imageryProvider.ready) {
  34. const tilingScheme = imageryLayer.imageryProvider.tilingScheme;
  35. rectangle = tilingScheme.tileXYToRectangle(x, y, level);
  36. }
  37. this.rectangle = rectangle;
  38. }
  39. Imagery.createPlaceholder = function (imageryLayer) {
  40. const result = new Imagery(imageryLayer, 0, 0, 0);
  41. result.addReference();
  42. result.state = ImageryState.PLACEHOLDER;
  43. return result;
  44. };
  45. Imagery.prototype.addReference = function () {
  46. ++this.referenceCount;
  47. };
  48. Imagery.prototype.releaseReference = function () {
  49. --this.referenceCount;
  50. if (this.referenceCount === 0) {
  51. this.imageryLayer.removeImageryFromCache(this);
  52. if (defined(this.parent)) {
  53. this.parent.releaseReference();
  54. }
  55. if (defined(this.image) && defined(this.image.destroy)) {
  56. this.image.destroy();
  57. }
  58. if (defined(this.texture)) {
  59. this.texture.destroy();
  60. }
  61. if (
  62. defined(this.textureWebMercator) &&
  63. this.texture !== this.textureWebMercator
  64. ) {
  65. this.textureWebMercator.destroy();
  66. }
  67. destroyObject(this);
  68. return 0;
  69. }
  70. return this.referenceCount;
  71. };
  72. Imagery.prototype.processStateMachine = function (
  73. frameState,
  74. needGeographicProjection,
  75. skipLoading
  76. ) {
  77. if (this.state === ImageryState.UNLOADED && !skipLoading) {
  78. this.state = ImageryState.TRANSITIONING;
  79. this.imageryLayer._requestImagery(this);
  80. }
  81. if (this.state === ImageryState.RECEIVED) {
  82. this.state = ImageryState.TRANSITIONING;
  83. this.imageryLayer._createTexture(frameState.context, this);
  84. }
  85. // If the imagery is already ready, but we need a geographic version and don't have it yet,
  86. // we still need to do the reprojection step. This can happen if the Web Mercator version
  87. // is fine initially, but the geographic one is needed later.
  88. const needsReprojection =
  89. this.state === ImageryState.READY &&
  90. needGeographicProjection &&
  91. !this.texture;
  92. if (this.state === ImageryState.TEXTURE_LOADED || needsReprojection) {
  93. this.state = ImageryState.TRANSITIONING;
  94. this.imageryLayer._reprojectTexture(
  95. frameState,
  96. this,
  97. needGeographicProjection
  98. );
  99. }
  100. };
  101. export default Imagery;