Merge branch 'newBranch' into 'master'

Fix CardArtCache,LoadingCache Dispose Textures

See merge request core-developers/forge!2805
This commit is contained in:
Sol
2020-05-11 15:23:08 +00:00
7 changed files with 40 additions and 6 deletions

View File

@@ -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);

View File

@@ -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<String, Texture> cache = CacheBuilder.newBuilder()
.maximumSize(400)
.expireAfterAccess(15, TimeUnit.MINUTES)
.removalListener(new RemovalListener<String, Texture>() {
@Override
public void onRemoval(RemovalNotification<String, Texture> 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);
}

View File

@@ -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

View File

@@ -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);

View File

@@ -445,6 +445,7 @@ public class MatchController extends AbstractGuiGame {
@Override
public void afterGameEnd() {
Forge.back();
ImageCache.disposeTexture();
//view = null;
}

View File

@@ -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();
}
}
/**

View File

@@ -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);