UNPKG

2.87 kBJavaScriptView Raw
1"use strict";
2var constants = require("@pixi/constants"), extensions = require("@pixi/extensions");
3const _TextureGCSystem = class _TextureGCSystem2 {
4 /** @param renderer - The renderer this System works for. */
5 constructor(renderer) {
6 this.renderer = renderer, this.count = 0, this.checkCount = 0, this.maxIdle = _TextureGCSystem2.defaultMaxIdle, this.checkCountMax = _TextureGCSystem2.defaultCheckCountMax, this.mode = _TextureGCSystem2.defaultMode;
7 }
8 /**
9 * Checks to see when the last time a texture was used.
10 * If the texture has not been used for a specified amount of time, it will be removed from the GPU.
11 */
12 postrender() {
13 this.renderer.objectRenderer.renderingToScreen && (this.count++, this.mode !== constants.GC_MODES.MANUAL && (this.checkCount++, this.checkCount > this.checkCountMax && (this.checkCount = 0, this.run())));
14 }
15 /**
16 * Checks to see when the last time a texture was used.
17 * If the texture has not been used for a specified amount of time, it will be removed from the GPU.
18 */
19 run() {
20 const tm = this.renderer.texture, managedTextures = tm.managedTextures;
21 let wasRemoved = !1;
22 for (let i = 0; i < managedTextures.length; i++) {
23 const texture = managedTextures[i];
24 texture.resource && this.count - texture.touched > this.maxIdle && (tm.destroyTexture(texture, !0), managedTextures[i] = null, wasRemoved = !0);
25 }
26 if (wasRemoved) {
27 let j = 0;
28 for (let i = 0; i < managedTextures.length; i++)
29 managedTextures[i] !== null && (managedTextures[j++] = managedTextures[i]);
30 managedTextures.length = j;
31 }
32 }
33 /**
34 * Removes all the textures within the specified displayObject and its children from the GPU.
35 * @param {PIXI.DisplayObject} displayObject - the displayObject to remove the textures from.
36 */
37 unload(displayObject) {
38 const tm = this.renderer.texture, texture = displayObject._texture;
39 texture && !texture.framebuffer && tm.destroyTexture(texture);
40 for (let i = displayObject.children.length - 1; i >= 0; i--)
41 this.unload(displayObject.children[i]);
42 }
43 destroy() {
44 this.renderer = null;
45 }
46};
47_TextureGCSystem.defaultMode = constants.GC_MODES.AUTO, /**
48* Default maximum idle frames before a texture is destroyed by garbage collection.
49* @static
50* @default 3600
51* @see PIXI.TextureGCSystem#maxIdle
52*/
53_TextureGCSystem.defaultMaxIdle = 60 * 60, /**
54* Default frames between two garbage collections.
55* @static
56* @default 600
57* @see PIXI.TextureGCSystem#checkCountMax
58*/
59_TextureGCSystem.defaultCheckCountMax = 60 * 10, /** @ignore */
60_TextureGCSystem.extension = {
61 type: extensions.ExtensionType.RendererSystem,
62 name: "textureGC"
63};
64let TextureGCSystem = _TextureGCSystem;
65extensions.extensions.add(TextureGCSystem);
66exports.TextureGCSystem = TextureGCSystem;
67//# sourceMappingURL=TextureGCSystem.js.map