TextureCache.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import defined from '../Core/defined.js';
  2. import defineProperties from '../Core/defineProperties.js';
  3. import destroyObject from '../Core/destroyObject.js';
  4. /**
  5. * @private
  6. */
  7. function TextureCache() {
  8. this._textures = {};
  9. this._numberOfTextures = 0;
  10. this._texturesToRelease = {};
  11. }
  12. defineProperties(TextureCache.prototype, {
  13. numberOfTextures : {
  14. get : function() {
  15. return this._numberOfTextures;
  16. }
  17. }
  18. });
  19. TextureCache.prototype.getTexture = function(keyword) {
  20. var cachedTexture = this._textures[keyword];
  21. if (!defined(cachedTexture)) {
  22. return undefined;
  23. }
  24. // No longer want to release this if it was previously released.
  25. delete this._texturesToRelease[keyword];
  26. ++cachedTexture.count;
  27. return cachedTexture.texture;
  28. };
  29. TextureCache.prototype.addTexture = function(keyword, texture) {
  30. var cachedTexture = {
  31. texture : texture,
  32. count : 1
  33. };
  34. texture.finalDestroy = texture.destroy;
  35. var that = this;
  36. texture.destroy = function() {
  37. if (--cachedTexture.count === 0) {
  38. that._texturesToRelease[keyword] = cachedTexture;
  39. }
  40. };
  41. this._textures[keyword] = cachedTexture;
  42. ++this._numberOfTextures;
  43. };
  44. TextureCache.prototype.destroyReleasedTextures = function() {
  45. var texturesToRelease = this._texturesToRelease;
  46. for (var keyword in texturesToRelease) {
  47. if (texturesToRelease.hasOwnProperty(keyword)) {
  48. var cachedTexture = texturesToRelease[keyword];
  49. delete this._textures[keyword];
  50. cachedTexture.texture.finalDestroy();
  51. --this._numberOfTextures;
  52. }
  53. }
  54. this._texturesToRelease = {};
  55. };
  56. TextureCache.prototype.isDestroyed = function() {
  57. return false;
  58. };
  59. TextureCache.prototype.destroy = function() {
  60. var textures = this._textures;
  61. for (var keyword in textures) {
  62. if (textures.hasOwnProperty(keyword)) {
  63. textures[keyword].texture.finalDestroy();
  64. }
  65. }
  66. return destroyObject(this);
  67. };
  68. export default TextureCache;