From deb0d7e2f18bca9816cbcb1f01436b243b7bc217 Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Mon, 11 May 2020 10:22:13 +0800 Subject: [PATCH] Fix CardArtCache,LoadingCache Dispose Textures --- forge-gui-mobile/src/forge/Forge.java | 1 + .../src/forge/assets/ImageCache.java | 21 +++++++++++++++++++ .../src/forge/card/CardAvatarImage.java | 9 ++++---- .../src/forge/card/CardRenderer.java | 4 ++++ .../forge/screens/match/MatchController.java | 1 + .../screens/match/winlose/ControlWinLose.java | 5 ++++- .../forge/planarconquest/ConquestRegion.java | 5 +++++ 7 files changed, 40 insertions(+), 6 deletions(-) diff --git a/forge-gui-mobile/src/forge/Forge.java b/forge-gui-mobile/src/forge/Forge.java index 9e82559f3da..7b3dd63124f 100644 --- a/forge-gui-mobile/src/forge/Forge.java +++ b/forge-gui-mobile/src/forge/Forge.java @@ -405,6 +405,7 @@ public class Forge implements ApplicationListener { try { endKeyInput(); //end key input before switching screens ForgeAnimation.endAll(); //end all active animations before switching screens + ImageCache.disposeTexture(); currentScreen = screen0; currentScreen.setSize(screenWidth, screenHeight); diff --git a/forge-gui-mobile/src/forge/assets/ImageCache.java b/forge-gui-mobile/src/forge/assets/ImageCache.java index 5d472e6e1aa..078e9332c92 100644 --- a/forge-gui-mobile/src/forge/assets/ImageCache.java +++ b/forge-gui-mobile/src/forge/assets/ImageCache.java @@ -24,8 +24,12 @@ import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.google.common.cache.CacheBuilder; import com.google.common.cache.LoadingCache; +import com.google.common.cache.RemovalCause; +import com.google.common.cache.RemovalListener; +import com.google.common.cache.RemovalNotification; import forge.ImageKeys; import forge.card.CardEdition; +import forge.card.CardRenderer; import forge.game.card.CardView; import forge.game.player.IHasIcon; import forge.item.IPaperCard; @@ -61,6 +65,14 @@ public class ImageCache { private static final LoadingCache cache = CacheBuilder.newBuilder() .maximumSize(400) .expireAfterAccess(15, TimeUnit.MINUTES) + .removalListener(new RemovalListener() { + @Override + public void onRemoval(RemovalNotification removalNotification) { + if(removalNotification.wasEvicted()||removalNotification.getCause() == RemovalCause.EXPIRED) { + removalNotification.getValue().dispose(); + } + } + }) .build(new ImageLoader()); public static final Texture defaultImage; public static FImage BlackBorder = FSkinImage.IMG_BORDER_BLACK; @@ -85,9 +97,18 @@ public class ImageCache { public static void clear() { cache.invalidateAll(); + cache.cleanUp(); missingIconKeys.clear(); } + public static void disposeTexture(){ + for (Texture t: cache.asMap().values()) { + t.dispose(); + } + CardRenderer.clearcardArtCache(); + clear(); + } + public static Texture getImage(InventoryItem ii) { return getImage(ii.getImageKey(false), true); } diff --git a/forge-gui-mobile/src/forge/card/CardAvatarImage.java b/forge-gui-mobile/src/forge/card/CardAvatarImage.java index eb909b5746e..6609ca06c35 100644 --- a/forge-gui-mobile/src/forge/card/CardAvatarImage.java +++ b/forge-gui-mobile/src/forge/card/CardAvatarImage.java @@ -31,11 +31,10 @@ public class CardAvatarImage implements FImage { @Override public void draw(Graphics g, float x, float y, float w, float h) { - if (image == null) { //attempt to retrieve card art if needed - image = CardRenderer.getCardArt(imageKey, false, false, false); - if (image == null) { - return; //can't draw anything if can't be loaded yet - } + //force to get the avatar since the the cardartcache & loadingcache is always cleared on screen change or the battle bar will display black + image = CardRenderer.getCardArt(imageKey, false, false, false); + if (image == null) { + return; //can't draw anything if can't be loaded yet } //draw scaled image into clipped region so it fills box while maintain aspect ratio diff --git a/forge-gui-mobile/src/forge/card/CardRenderer.java b/forge-gui-mobile/src/forge/card/CardRenderer.java index d0cc8b457b0..46133714499 100644 --- a/forge-gui-mobile/src/forge/card/CardRenderer.java +++ b/forge-gui-mobile/src/forge/card/CardRenderer.java @@ -196,6 +196,10 @@ public class CardRenderer { public static final float CARD_ART_RATIO = 1.302f; public static final float CARD_ART_HEIGHT_PERCENTAGE = 0.43f; + public static void clearcardArtCache(){ + cardArtCache.clear(); + } + //extract card art from the given card public static FImageComplex getCardArt(IPaperCard pc) { return getCardArt(pc, false); diff --git a/forge-gui-mobile/src/forge/screens/match/MatchController.java b/forge-gui-mobile/src/forge/screens/match/MatchController.java index ec7bf10906a..6e0d269c725 100644 --- a/forge-gui-mobile/src/forge/screens/match/MatchController.java +++ b/forge-gui-mobile/src/forge/screens/match/MatchController.java @@ -445,6 +445,7 @@ public class MatchController extends AbstractGuiGame { @Override public void afterGameEnd() { Forge.back(); + ImageCache.disposeTexture(); //view = null; } diff --git a/forge-gui-mobile/src/forge/screens/match/winlose/ControlWinLose.java b/forge-gui-mobile/src/forge/screens/match/winlose/ControlWinLose.java index 400ef7497be..2c0d0e8e413 100644 --- a/forge-gui-mobile/src/forge/screens/match/winlose/ControlWinLose.java +++ b/forge-gui-mobile/src/forge/screens/match/winlose/ControlWinLose.java @@ -1,6 +1,7 @@ package forge.screens.match.winlose; import forge.Forge; +import forge.assets.ImageCache; import forge.game.GameView; import forge.game.player.PlayerView; import forge.screens.match.MatchController; @@ -82,8 +83,10 @@ public class ControlWinLose { try { MatchController.getHostedMatch().endCurrentGame(); } catch (NullPointerException e) {} view.hide(); - if(humancount == 0) + if(humancount == 0) { Forge.back(); + ImageCache.disposeTexture(); + } } /** diff --git a/forge-gui/src/main/java/forge/planarconquest/ConquestRegion.java b/forge-gui/src/main/java/forge/planarconquest/ConquestRegion.java index d4278084fb8..50c1594e9a2 100644 --- a/forge-gui/src/main/java/forge/planarconquest/ConquestRegion.java +++ b/forge-gui/src/main/java/forge/planarconquest/ConquestRegion.java @@ -45,7 +45,12 @@ public class ConquestRegion { return name; } + public void clearArt() { + art = null; + } + public ISkinImage getArt() { + clearArt(); //force clear this so it will be redrawn since loadingcache invalidates the cache every screen change if (art == null) { PaperCard pc = cardPool.getCard(artCardName);