Source: core/renderers/webgl/TextureGarbageCollector.js

core/renderers/webgl/TextureGarbageCollector.js

  1. import { GC_MODES } from '../../const';
  2. import settings from '../../settings';
  3. /**
  4. * TextureGarbageCollector. This class manages the GPU and ensures that it does not get clogged
  5. * up with textures that are no longer being used.
  6. *
  7. * @class
  8. * @memberof PIXI
  9. */
  10. export default class TextureGarbageCollector
  11. {
  12. /**
  13. * @param {PIXI.WebGLRenderer} renderer - The renderer this manager works for.
  14. */
  15. constructor(renderer)
  16. {
  17. this.renderer = renderer;
  18. this.count = 0;
  19. this.checkCount = 0;
  20. this.maxIdle = settings.GC_MAX_IDLE;
  21. this.checkCountMax = settings.GC_MAX_CHECK_COUNT;
  22. this.mode = settings.GC_MODE;
  23. }
  24. /**
  25. * Checks to see when the last time a texture was used
  26. * if the texture has not been used for a specified amount of time it will be removed from the GPU
  27. */
  28. update()
  29. {
  30. this.count++;
  31. if (this.mode === GC_MODES.MANUAL)
  32. {
  33. return;
  34. }
  35. this.checkCount++;
  36. if (this.checkCount > this.checkCountMax)
  37. {
  38. this.checkCount = 0;
  39. this.run();
  40. }
  41. }
  42. /**
  43. * Checks to see when the last time a texture was used
  44. * if the texture has not been used for a specified amount of time it will be removed from the GPU
  45. */
  46. run()
  47. {
  48. const tm = this.renderer.textureManager;
  49. const managedTextures = tm._managedTextures;
  50. let wasRemoved = false;
  51. for (let i = 0; i < managedTextures.length; i++)
  52. {
  53. const texture = managedTextures[i];
  54. // only supports non generated textures at the moment!
  55. if (!texture._glRenderTargets && this.count - texture.touched > this.maxIdle)
  56. {
  57. tm.destroyTexture(texture, true);
  58. managedTextures[i] = null;
  59. wasRemoved = true;
  60. }
  61. }
  62. if (wasRemoved)
  63. {
  64. let j = 0;
  65. for (let i = 0; i < managedTextures.length; i++)
  66. {
  67. if (managedTextures[i] !== null)
  68. {
  69. managedTextures[j++] = managedTextures[i];
  70. }
  71. }
  72. managedTextures.length = j;
  73. }
  74. }
  75. /**
  76. * Removes all the textures within the specified displayObject and its children from the GPU
  77. *
  78. * @param {PIXI.DisplayObject} displayObject - the displayObject to remove the textures from.
  79. */
  80. unload(displayObject)
  81. {
  82. const tm = this.renderer.textureManager;
  83. // only destroy non generated textures
  84. if (displayObject._texture && displayObject._texture._glRenderTargets)
  85. {
  86. tm.destroyTexture(displayObject._texture, true);
  87. }
  88. for (let i = displayObject.children.length - 1; i >= 0; i--)
  89. {
  90. this.unload(displayObject.children[i]);
  91. }
  92. }
  93. }