{"version":3,"file":"TextureGCSystem.js","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.resource && 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":["_TextureGCSystem","GC_MODES","ExtensionType","extensions"],"mappings":";;AAoBO,MAAM,mBAAN,MAAMA,kBACb;AAAA;AAAA,EAgEI,YAAY,UACZ;AACI,SAAK,WAAW,UAEhB,KAAK,QAAQ,GACb,KAAK,aAAa,GAClB,KAAK,UAAUA,kBAAgB,gBAC/B,KAAK,gBAAgBA,kBAAgB,sBACrC,KAAK,OAAOA,kBAAgB;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,aACV;AACS,SAAK,SAAS,eAAe,sBAKlC,KAAK,SAED,KAAK,SAASC,UAAS,SAAA,WAK3B,KAAK,cAED,KAAK,aAAa,KAAK,kBAEvB,KAAK,aAAa,GAElB,KAAK,IAAI;AAAA,EAEjB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MACA;AACI,UAAM,KAAK,KAAK,SAAS,SACnB,kBAAkB,GAAG;AAC3B,QAAI,aAAa;AAEjB,aAAS,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAC5C;AACU,YAAA,UAAU,gBAAgB,CAAC;AAG7B,cAAQ,YAAY,KAAK,QAAQ,QAAQ,UAAU,KAAK,YAExD,GAAG,eAAe,SAAS,EAAI,GAC/B,gBAAgB,CAAC,IAAI,MACrB,aAAa;AAAA,IAErB;AAEA,QAAI,YACJ;AACI,UAAI,IAAI;AAER,eAAS,IAAI,GAAG,IAAI,gBAAgB,QAAQ;AAEpC,wBAAgB,CAAC,MAAM,SAEvB,gBAAgB,GAAG,IAAI,gBAAgB,CAAC;AAIhD,sBAAgB,SAAS;AAAA,IAC7B;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,eACP;AACI,UAAM,KAAK,KAAK,SAAS,SACnB,UAAU,cAAc;A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