{"version":3,"file":"TextureGCSystem.mjs","sources":["../../src/textures/TextureGCSystem.ts"],"sourcesContent":["import { GC_MODES } from '@pixi/constants';\nimport { extensions, ExtensionType } from '@pixi/extensions';\n\nimport type { ExtensionMetadata } from '@pixi/extensions';\nimport type { Renderer } from '../Renderer';\nimport type { RenderTexture } from '../renderTexture/RenderTexture';\nimport type { ISystem } from '../system/ISystem';\nimport type { Texture } from './Texture';\n\nexport interface IUnloadableTexture\n{\n    _texture: Texture | RenderTexture;\n    children: IUnloadableTexture[];\n}\n\n/**\n * System plugin to the renderer to manage texture garbage collection on the GPU,\n * ensuring that it does not get clogged up with textures that are no longer being used.\n * @memberof PIXI\n */\nexport class TextureGCSystem implements ISystem\n{\n    /**\n     * Default garbage collection mode.\n     * @static\n     * @type {PIXI.GC_MODES}\n     * @default PIXI.GC_MODES.AUTO\n     * @see PIXI.TextureGCSystem#mode\n     */\n    public static defaultMode = GC_MODES.AUTO;\n\n    /**\n     * Default maximum idle frames before a texture is destroyed by garbage collection.\n     * @static\n     * @default 3600\n     * @see PIXI.TextureGCSystem#maxIdle\n     */\n    public static defaultMaxIdle = 60 * 60;\n\n    /**\n     * Default frames between two garbage collections.\n     * @static\n     * @default 600\n     * @see PIXI.TextureGCSystem#checkCountMax\n     */\n    public static defaultCheckCountMax = 60 * 10;\n\n    /** @ignore */\n    static extension: ExtensionMetadata = {\n        type: ExtensionType.RendererSystem,\n        name: 'textureGC',\n    };\n\n    /**\n     * Frame count since started.\n     * @readonly\n     */\n    public count: number;\n\n    /**\n     * Frame count since last garbage collection.\n     * @readonly\n     */\n    public checkCount: number;\n\n    /**\n     * Maximum idle frames before a texture is destroyed by garbage collection.\n     * @see PIXI.TextureGCSystem.defaultMaxIdle\n     */\n    public maxIdle: number;\n\n    /**\n     * Frames between two garbage collections.\n     * @see PIXI.TextureGCSystem.defaultCheckCountMax\n     */\n    public checkCountMax: number;\n\n    /**\n     * Current garbage collection mode.\n     * @see PIXI.TextureGCSystem.defaultMode\n     */\n    public mode: GC_MODES;\n    private renderer: Renderer;\n\n    /** @param renderer - The renderer this System works for. */\n    constructor(renderer: Renderer)\n    {\n        this.renderer = renderer;\n\n        this.count = 0;\n        this.checkCount = 0;\n        this.maxIdle = TextureGCSystem.defaultMaxIdle;\n        this.checkCountMax = TextureGCSystem.defaultCheckCountMax;\n        this.mode = TextureGCSystem.defaultMode;\n    }\n\n    /**\n     * Checks to see when the last time a texture was used.\n     * If the texture has not been used for a specified amount of time, it will be removed from the GPU.\n     */\n    protected postrender(): void\n    {\n        if (!this.renderer.objectRenderer.renderingToScreen)\n        {\n            return;\n        }\n\n        this.count++;\n\n        if (this.mode === GC_MODES.MANUAL)\n        {\n            return;\n        }\n\n        this.checkCount++;\n\n        if (this.checkCount > this.checkCountMax)\n        {\n            this.checkCount = 0;\n\n            this.run();\n        }\n    }\n\n    /**\n     * Checks to see when the last time a texture was used.\n     * If the texture has not been used for a specified amount of time, it will be removed from the GPU.\n     */\n    run(): void\n    {\n        const tm = this.renderer.texture;\n        const managedTextures = tm.managedTextures;\n        let wasRemoved = false;\n\n        for (let i = 0; i < managedTextures.length; i++)\n        {\n            const texture = managedTextures[i];\n\n            // Only supports non generated textures at the moment!\n            if (!(texture as any).framebuffer && this.count - texture.touched > this.maxIdle)\n            {\n                tm.destroyTexture(texture, true);\n                managedTextures[i] = null;\n                wasRemoved = true;\n            }\n        }\n\n        if (wasRemoved)\n        {\n            let j = 0;\n\n            for (let i = 0; i < managedTextures.length; i++)\n            {\n                if (managedTextures[i] !== null)\n                {\n                    managedTextures[j++] = managedTextures[i];\n                }\n            }\n\n            managedTextures.length = j;\n        }\n    }\n\n    /**\n     * Removes all the textures within the specified displayObject and its children from the GPU.\n     * @param {PIXI.DisplayObject} displayObject - the displayObject to remove the textures from.\n     */\n    unload(displayObject: IUnloadableTexture): void\n    {\n        const tm = this.renderer.texture;\n        const texture = displayObject._texture as RenderTexture;\n\n        // only destroy non generated textures\n        if (texture && !texture.framebuffer)\n        {\n            tm.destroyTexture(texture);\n        }\n\n        for (let i = displayObject.children.length - 1; i >= 0; i--)\n        {\n            this.unload(displayObject.children[i]);\n        }\n    }\n\n    destroy(): void\n    {\n        this.renderer = null;\n    }\n}\n\nextensions.add(TextureGCSystem);\n"],"names":[],"mappings":";;;AAoBO,MAAM,mBAAN,MACP;AAAA,EAgEI,YAAY,QACZ,EAAA;AACI,IAAA,IAAA,CAAK,QAAW,GAAA,QAAA,CAAA;AAEhB,IAAA,IAAA,CAAK,KAAQ,GAAA,CAAA,CAAA;AACb,IAAA,IAAA,CAAK,UAAa,GAAA,CAAA,CAAA;AAClB,IAAA,IAAA,CAAK,UAAU,gBAAgB,CAAA,cAAA,CAAA;AAC/B,IAAA,IAAA,CAAK,gBAAgB,gBAAgB,CAAA,oBAAA,CAAA;AACrC,IAAA,IAAA,CAAK,OAAO,gBAAgB,CAAA,WAAA,CAAA;AAAA,GAChC;AAAA,EAMA,UACA,GAAA;AACI,IAAA,IAAI,CAAC,IAAA,CAAK,QAAS,CAAA,cAAA,CAAe,iBAClC,EAAA;AACI,MAAA,OAAA;AAAA,KACJ;AAEA,IAAK,IAAA,CAAA,KAAA,EAAA,CAAA;AAEL,IAAI,IAAA,IAAA,CAAK,IAAS,KAAA,QAAA,CAAS,MAC3B,EAAA;AACI,MAAA,OAAA;AAAA,KACJ;AAEA,IAAK,IAAA,CAAA,UAAA,EAAA,CAAA;AAEL,IAAI,IAAA,IAAA,CAAK,UAAa,GAAA,IAAA,CAAK,aAC3B,EAAA;AACI,MAAA,IAAA,CAAK,UAAa,GAAA,CAAA,CAAA;AAElB,MAAA,IAAA,CAAK,GAAI,EAAA,CAAA;AAAA,KACb;AAAA,GACJ;AAAA,EAMA,GACA,GAAA;AACI,IAAM,MAAA,EAAA,GAAK,KAAK,QAAS,CAAA,OAAA,CAAA;AACzB,IAAA,MAAM,kBAAkB,EAAG,CAAA,eAAA,CAAA;AAC3B,IAAA,IAAI,UAAa,GAAA,KAAA,CAAA;AAEjB,IAA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