1 | import { GC_MODES } from '@pixi/constants';
|
2 | import { ExtensionType, extensions } from '@pixi/extensions';
|
3 |
|
4 | const _TextureGCSystem = class {
|
5 | constructor(renderer) {
|
6 | this.renderer = renderer;
|
7 | this.count = 0;
|
8 | this.checkCount = 0;
|
9 | this.maxIdle = _TextureGCSystem.defaultMaxIdle;
|
10 | this.checkCountMax = _TextureGCSystem.defaultCheckCountMax;
|
11 | this.mode = _TextureGCSystem.defaultMode;
|
12 | }
|
13 | postrender() {
|
14 | if (!this.renderer.objectRenderer.renderingToScreen) {
|
15 | return;
|
16 | }
|
17 | this.count++;
|
18 | if (this.mode === GC_MODES.MANUAL) {
|
19 | return;
|
20 | }
|
21 | this.checkCount++;
|
22 | if (this.checkCount > this.checkCountMax) {
|
23 | this.checkCount = 0;
|
24 | this.run();
|
25 | }
|
26 | }
|
27 | run() {
|
28 | const tm = this.renderer.texture;
|
29 | const managedTextures = tm.managedTextures;
|
30 | let wasRemoved = false;
|
31 | for (let i = 0; i < managedTextures.length; i++) {
|
32 | const texture = managedTextures[i];
|
33 | if (!texture.framebuffer && this.count - texture.touched > this.maxIdle) {
|
34 | tm.destroyTexture(texture, true);
|
35 | managedTextures[i] = null;
|
36 | wasRemoved = true;
|
37 | }
|
38 | }
|
39 | if (wasRemoved) {
|
40 | let j = 0;
|
41 | for (let i = 0; i < managedTextures.length; i++) {
|
42 | if (managedTextures[i] !== null) {
|
43 | managedTextures[j++] = managedTextures[i];
|
44 | }
|
45 | }
|
46 | managedTextures.length = j;
|
47 | }
|
48 | }
|
49 | unload(displayObject) {
|
50 | const tm = this.renderer.texture;
|
51 | const texture = displayObject._texture;
|
52 | if (texture && !texture.framebuffer) {
|
53 | tm.destroyTexture(texture);
|
54 | }
|
55 | for (let i = displayObject.children.length - 1; i >= 0; i--) {
|
56 | this.unload(displayObject.children[i]);
|
57 | }
|
58 | }
|
59 | destroy() {
|
60 | this.renderer = null;
|
61 | }
|
62 | };
|
63 | let TextureGCSystem = _TextureGCSystem;
|
64 | TextureGCSystem.defaultMode = GC_MODES.AUTO;
|
65 | TextureGCSystem.defaultMaxIdle = 60 * 60;
|
66 | TextureGCSystem.defaultCheckCountMax = 60 * 10;
|
67 | TextureGCSystem.extension = {
|
68 | type: ExtensionType.RendererSystem,
|
69 | name: "textureGC"
|
70 | };
|
71 | extensions.add(TextureGCSystem);
|
72 |
|
73 | export { TextureGCSystem };
|
74 |
|