From fed4ab99c139f108fd3daf510f721336bd397781 Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Fri, 8 Jul 2022 11:04:53 +0800 Subject: [PATCH] add EvictingQueue for loaded images -use the Queue as reference for removing old entries on the AssetManager -fix cardArtCache storing forgeArt, should fix missing cardArt --- .../src/forge/assets/ImageCache.java | 31 +++++++++++++++++-- .../src/forge/card/CardRenderer.java | 13 +++++--- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/forge-gui-mobile/src/forge/assets/ImageCache.java b/forge-gui-mobile/src/forge/assets/ImageCache.java index 7c45beb4155..66e6f6da2de 100644 --- a/forge-gui-mobile/src/forge/assets/ImageCache.java +++ b/forge-gui-mobile/src/forge/assets/ImageCache.java @@ -19,7 +19,10 @@ package forge.assets; import java.io.File; import java.util.List; +import java.util.Queue; +import java.util.Set; +import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.Pixmap; @@ -27,6 +30,9 @@ import com.badlogic.gdx.graphics.TextureData; import com.badlogic.gdx.graphics.glutils.PixmapTextureData; import com.badlogic.gdx.utils.ObjectMap; import com.badlogic.gdx.utils.ObjectSet; +import com.google.common.collect.EvictingQueue; +import com.google.common.collect.Queues; +import com.google.common.collect.Sets; import forge.gui.FThreads; import forge.util.FileUtil; import forge.util.TextUtil; @@ -71,6 +77,8 @@ public class ImageCache { private static final ObjectSet missingIconKeys = new ObjectSet<>(); private static List borderlessCardlistKey = FileUtil.readFile(ForgeConstants.BORDERLESS_CARD_LIST_FILE); static int maxCardCapacity = 400; //default card capacity + static EvictingQueue q; + static Queue syncQ; static TextureParameter defaultParameter = new TextureParameter(); static TextureParameter filtered = new TextureParameter(); public static void initCache(int capacity) { @@ -80,6 +88,10 @@ public class ImageCache { filtered.magFilter = Texture.TextureFilter.Linear; //override maxCardCapacity maxCardCapacity = capacity; + //init q + q = EvictingQueue.create(capacity); + //init syncQ for threadsafe use + syncQ = Queues.synchronizedQueue(q); } public static final Texture defaultImage; public static FImage BlackBorder = FSkinImage.IMG_BORDER_BLACK; @@ -259,10 +271,9 @@ public class ImageCache { static Texture loadAsset(String imageKey, File file, boolean otherCache) { if (file == null) return null; + syncQ.add(file.getPath()); if (!otherCache && Forge.getAssets(false).manager.getLoadedAssets() > maxCardCapacity) { - //when maxCardCapacity is reached, clear to refresh - Forge.getAssets(false).manager.clear(); - CardRenderer.clearcardArtCache(); + unloadCardTextures(Forge.getAssets(false).manager); return null; } String fileName = file.getPath(); @@ -286,6 +297,20 @@ public class ImageCache { return t; } } + static void unloadCardTextures(AssetManager manager) { + //get latest images from syncQ + Set newQ = Sets.newHashSet(syncQ); + //get loaded images from assetmanager + Set old = Sets.newHashSet(manager.getAssetNames()); + //get all images not in newQ (old images to unload) + Set toUnload = Sets.difference(old, newQ); + //unload from assetmanager to save RAM + for (String asset : toUnload) { + manager.unload(asset); + } + //clear cachedArt since this is dependant to the loaded texture + CardRenderer.clearcardArtCache(); + } public static void preloadCache(Iterable keys) { if (FModel.getPreferences().getPrefBoolean(ForgePreferences.FPref.UI_DISABLE_CARD_IMAGES)) return; diff --git a/forge-gui-mobile/src/forge/card/CardRenderer.java b/forge-gui-mobile/src/forge/card/CardRenderer.java index 412371e8254..a9d53f9f142 100644 --- a/forge-gui-mobile/src/forge/card/CardRenderer.java +++ b/forge-gui-mobile/src/forge/card/CardRenderer.java @@ -304,7 +304,8 @@ public class CardRenderer { } cardArt = new FTextureRegionImage(new TextureRegion(image, Math.round(x), Math.round(y), Math.round(w), Math.round(h))); } - cardArtCache.put(imageKey, cardArt); + if (!CardImageRenderer.forgeArt.equals(cardArt)) + cardArtCache.put(imageKey, cardArt); } } //fix display for effect @@ -339,7 +340,8 @@ public class CardRenderer { cardArt = new FTextureRegionImage(new TextureRegion(image, Math.round(x), Math.round(y), Math.round(w), Math.round(h))); } - cardArtCache.put("Aftermath_second_"+imageKey, cardArt); + if (!CardImageRenderer.forgeArt.equals(cardArt)) + cardArtCache.put("Aftermath_second_"+imageKey, cardArt); } } return cardArt; @@ -385,7 +387,8 @@ public class CardRenderer { } cardArt = new FTextureRegionImage(new TextureRegion(image, Math.round(x), Math.round(y), Math.round(w), Math.round(h))); } - cardArtCache.put("Alternate_"+imageKey, cardArt); + if (!CardImageRenderer.forgeArt.equals(cardArt)) + cardArtCache.put("Alternate_"+imageKey, cardArt); } } return cardArt; @@ -419,9 +422,9 @@ public class CardRenderer { cardArt = new FTextureRegionImage(new TextureRegion(image, Math.round(x), Math.round(y), Math.round(w), Math.round(h))); } - if (!bottom) + if (!bottom && !CardImageRenderer.forgeArt.equals(cardArt)) cardArtCache.put("Meld_primary_"+imageKey, cardArt); - else + else if (!CardImageRenderer.forgeArt.equals(cardArt)) cardArtCache.put("Meld_secondary_"+imageKey, cardArt); } }