Merge pull request #1069 from kevlahnota/master

add EvictingQueue for loaded images
This commit is contained in:
Anthony Calosa
2022-07-08 11:27:05 +08:00
committed by GitHub
2 changed files with 36 additions and 8 deletions

View File

@@ -19,7 +19,10 @@ package forge.assets;
import java.io.File; import java.io.File;
import java.util.List; 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.assets.loaders.TextureLoader.TextureParameter;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap; 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.graphics.glutils.PixmapTextureData;
import com.badlogic.gdx.utils.ObjectMap; import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.ObjectSet; 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.gui.FThreads;
import forge.util.FileUtil; import forge.util.FileUtil;
import forge.util.TextUtil; import forge.util.TextUtil;
@@ -71,6 +77,8 @@ public class ImageCache {
private static final ObjectSet<String> missingIconKeys = new ObjectSet<>(); private static final ObjectSet<String> missingIconKeys = new ObjectSet<>();
private static List<String> borderlessCardlistKey = FileUtil.readFile(ForgeConstants.BORDERLESS_CARD_LIST_FILE); private static List<String> borderlessCardlistKey = FileUtil.readFile(ForgeConstants.BORDERLESS_CARD_LIST_FILE);
static int maxCardCapacity = 400; //default card capacity static int maxCardCapacity = 400; //default card capacity
static EvictingQueue<String> q;
static Queue<String> syncQ;
static TextureParameter defaultParameter = new TextureParameter(); static TextureParameter defaultParameter = new TextureParameter();
static TextureParameter filtered = new TextureParameter(); static TextureParameter filtered = new TextureParameter();
public static void initCache(int capacity) { public static void initCache(int capacity) {
@@ -80,6 +88,10 @@ public class ImageCache {
filtered.magFilter = Texture.TextureFilter.Linear; filtered.magFilter = Texture.TextureFilter.Linear;
//override maxCardCapacity //override maxCardCapacity
maxCardCapacity = capacity; maxCardCapacity = capacity;
//init q
q = EvictingQueue.create(capacity);
//init syncQ for threadsafe use
syncQ = Queues.synchronizedQueue(q);
} }
public static final Texture defaultImage; public static final Texture defaultImage;
public static FImage BlackBorder = FSkinImage.IMG_BORDER_BLACK; public static FImage BlackBorder = FSkinImage.IMG_BORDER_BLACK;
@@ -259,10 +271,9 @@ public class ImageCache {
static Texture loadAsset(String imageKey, File file, boolean otherCache) { static Texture loadAsset(String imageKey, File file, boolean otherCache) {
if (file == null) if (file == null)
return null; return null;
syncQ.add(file.getPath());
if (!otherCache && Forge.getAssets(false).manager.getLoadedAssets() > maxCardCapacity) { if (!otherCache && Forge.getAssets(false).manager.getLoadedAssets() > maxCardCapacity) {
//when maxCardCapacity is reached, clear to refresh unloadCardTextures(Forge.getAssets(false).manager);
Forge.getAssets(false).manager.clear();
CardRenderer.clearcardArtCache();
return null; return null;
} }
String fileName = file.getPath(); String fileName = file.getPath();
@@ -286,6 +297,20 @@ public class ImageCache {
return t; return t;
} }
} }
static void unloadCardTextures(AssetManager manager) {
//get latest images from syncQ
Set<String> newQ = Sets.newHashSet(syncQ);
//get loaded images from assetmanager
Set<String> old = Sets.newHashSet(manager.getAssetNames());
//get all images not in newQ (old images to unload)
Set<String> 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<String> keys) { public static void preloadCache(Iterable<String> keys) {
if (FModel.getPreferences().getPrefBoolean(ForgePreferences.FPref.UI_DISABLE_CARD_IMAGES)) if (FModel.getPreferences().getPrefBoolean(ForgePreferences.FPref.UI_DISABLE_CARD_IMAGES))
return; return;

View File

@@ -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))); 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 //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))); 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; 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))); 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; 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))); 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); cardArtCache.put("Meld_primary_"+imageKey, cardArt);
else else if (!CardImageRenderer.forgeArt.equals(cardArt))
cardArtCache.put("Meld_secondary_"+imageKey, cardArt); cardArtCache.put("Meld_secondary_"+imageKey, cardArt);
} }
} }