From f4e375a2cd02c7943953f2c7481abd00b2bade62 Mon Sep 17 00:00:00 2001 From: Davide Tantillo Date: Fri, 2 May 2025 14:30:36 +0200 Subject: [PATCH] Refcounter for asset manager and gl resources disposal for webcomponent. --- spine-ts/spine-core/src/AssetManagerBase.ts | 19 +- .../spine-webgl/example/webcomponent-gui.html | 4 +- .../example/webcomponent-tutorial.html | 163 ++++++++++++++---- .../src/SpineWebComponentWidget.ts | 105 +++++++++-- 4 files changed, 233 insertions(+), 58 deletions(-) diff --git a/spine-ts/spine-core/src/AssetManagerBase.ts b/spine-ts/spine-core/src/AssetManagerBase.ts index 946a10aae..8997c6f2a 100644 --- a/spine-ts/spine-core/src/AssetManagerBase.ts +++ b/spine-ts/spine-core/src/AssetManagerBase.ts @@ -36,6 +36,7 @@ export class AssetManagerBase implements Disposable { private textureLoader: (image: HTMLImageElement | ImageBitmap) => Texture; private downloader: Downloader; private assets: StringMap = {}; + private assetsRefCount: StringMap = {}; private assetsLoaded: StringMap> = {}; private errors: StringMap = {}; private toLoad = 0; @@ -56,6 +57,7 @@ export class AssetManagerBase implements Disposable { this.toLoad--; this.loaded++; this.assets[path] = asset; + this.assetsRefCount[path] = (this.assetsRefCount[path] || 0) + 1; if (callback) callback(path, asset); } @@ -332,15 +334,16 @@ export class AssetManagerBase implements Disposable { remove (path: string) { path = this.pathPrefix + path; let asset = this.assets[path]; - if ((asset).dispose) (asset).dispose(); + if (asset.dispose) asset.dispose(); delete this.assets[path]; + delete this.assetsRefCount[path]; return asset; } removeAll () { - for (let key in this.assets) { - let asset = this.assets[key]; - if ((asset).dispose) (asset).dispose(); + for (let path in this.assets) { + let asset = this.assets[path]; + if (asset.dispose) asset.dispose(); } this.assets = {}; } @@ -361,6 +364,14 @@ export class AssetManagerBase implements Disposable { this.removeAll(); } + // dispose asset only if it's not used by others + disposeAsset(path: string, a?: string) { + if (--this.assetsRefCount[path] === 0) { + const asset = this.assets[path]; + if (asset.dispose) asset.dispose(); + } + } + hasErrors () { return Object.keys(this.errors).length > 0; } diff --git a/spine-ts/spine-webgl/example/webcomponent-gui.html b/spine-ts/spine-webgl/example/webcomponent-gui.html index d62a0c2cb..0bfcb0e34 100644 --- a/spine-ts/spine-webgl/example/webcomponent-gui.html +++ b/spine-ts/spine-webgl/example/webcomponent-gui.html @@ -58,7 +58,7 @@ + +
+

+                
+            
+
+ + + + + + + + +