New Faster Cache for Mobile - Cache2k, Add Border Masking Option

This commit is contained in:
Anthony Calosa
2019-10-07 12:59:52 +08:00
parent d4b2897b49
commit 87e9c93264
14 changed files with 248 additions and 46 deletions

View File

@@ -11,6 +11,7 @@ import forge.game.Game;
import forge.game.spellability.SpellAbility;
import forge.game.trigger.TriggerType;
import forge.game.zone.ZoneType;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
@@ -106,7 +107,7 @@ public class BlockEffect extends SpellAbilityEffect {
}
}
sb.append(String.join(", ", blockers)).append(" block ").append(String.join(", ", attackers));
sb.append(StringUtils.join(blockers.toArray(), ", ")).append(" block ").append(StringUtils.join(attackers.toArray(), ", "));
return sb.toString();
}

View File

@@ -104,6 +104,24 @@
<artifactId>gdx-backend-android</artifactId>
<version>1.9.10</version>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-base-bom</artifactId>
<version>1.2.4.Final</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-core</artifactId>
<version>1.2.4.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-api</artifactId>
<version>1.2.4.Final</version>
<scope>compile</scope>
</dependency>
</dependencies>
<profiles>

View File

@@ -28,6 +28,20 @@
-dontwarn org.slf4j.**
-dontwarn javax.**
# mandatory proguard rules for cache2k to keep the core implementation
-dontwarn org.cache2k.impl.xmlConfiguration.**
-dontwarn org.cache2k.impl.serverSide.**
-keep interface org.cache2k.spi.Cache2kCoreProvider
-keep public class * extends org.cache2k.spi.Cache2kCoreProvider
# optional proguard rules for cache2k, to keep XML configuration code
# if only programmatic configuration is used, these rules may be ommitted
-keep interface org.cache2k.core.spi.CacheConfigurationProvider
-keep public class * extends org.cache2k.core.spi.CacheConfigurationProvider
-keepclassmembers public class * extends org.cache2k.configuration.ConfigurationBean {
public void set*(...);
public ** get*();
}
-keep class forge.** { *; }
-keep class com.thoughtworks.xstream.** { *; }
-keep class org.apache.commons.lang3.** { *; }

View File

@@ -75,5 +75,23 @@
<artifactId>gdx-backend-robovm</artifactId>
<version>1.9.10</version>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-base-bom</artifactId>
<version>1.2.4.Final</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-core</artifactId>
<version>1.2.4.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-api</artifactId>
<version>1.2.4.Final</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -80,5 +80,23 @@
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-base-bom</artifactId>
<version>1.2.4.Final</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-core</artifactId>
<version>1.2.4.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-api</artifactId>
<version>1.2.4.Final</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -70,6 +70,24 @@
<artifactId>gdx-freetype</artifactId>
<version>1.9.10</version>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-base-bom</artifactId>
<version>1.2.4.Final</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-core</artifactId>
<version>1.2.4.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-api</artifactId>
<version>1.2.4.Final</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -34,7 +34,11 @@ public abstract class CachedCardImage implements ImageFetcher.Callback {
}
public Texture getImage() {
return ImageCache.getImage(key, true);
return ImageCache.getImage(key, true, false);
}
public Texture getImage(boolean mask) {
return ImageCache.getImage(key, true, mask);
}
public abstract void onImageFetched();

View File

@@ -20,18 +20,17 @@ package forge.assets;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.LoadingCache;
import forge.ImageKeys;
import forge.game.player.IHasIcon;
import forge.item.InventoryItem;
import forge.properties.ForgeConstants;
import forge.util.ImageUtil;
import org.apache.commons.lang3.StringUtils;
import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
@@ -52,10 +51,14 @@ public class ImageCache {
// short prefixes to save memory
private static final Set<String> missingIconKeys = new HashSet<>();
private static final LoadingCache<String, Texture> cache = CacheBuilder.newBuilder()
.maximumSize(400)
.expireAfterAccess(15,TimeUnit.MINUTES)
.build(new ImageLoader());
private static final Cache<String, Texture> cache = new Cache2kBuilder<String, Texture>() {}
.name("cache")
.entryCapacity(500)
.expireAfterWrite(15, TimeUnit.MINUTES)
.refreshAhead(true)
.permitNullValues(true)
.loader(new ImageLoader())
.build();
public static final Texture defaultImage;
private static boolean imageLoaded, delayLoadRequested;
@@ -76,12 +79,16 @@ public class ImageCache {
}
public static void clear() {
cache.invalidateAll();
cache.clear();
missingIconKeys.clear();
}
public static Texture getImage(InventoryItem ii) {
return getImage(ii.getImageKey(false), true);
return getImage(ii.getImageKey(false), true, false);
}
public static Texture getImage(InventoryItem ii, Boolean mask) {
return getImage(ii.getImageKey(false), true, mask);
}
/**
@@ -107,6 +114,9 @@ public class ImageCache {
* </p>
*/
public static Texture getImage(String imageKey, boolean useDefaultIfNotFound) {
return getImage(imageKey, useDefaultIfNotFound, false);
}
public static Texture getImage(String imageKey, boolean useDefaultIfNotFound, boolean maskCard) {
if (StringUtils.isEmpty(imageKey)) {
return null;
}
@@ -125,7 +135,10 @@ public class ImageCache {
Texture image;
if (useDefaultIfNotFound) {
// Load from file and add to cache if not found in cache initially.
image = cache.getIfPresent(imageKey);
if (maskCard)//if we add pixmap modification here, it will slow performance so we do this on the image loader lol :)...
imageKey += "#drawroundcorner#";
image = cache.get(imageKey);
if (image != null) { return image; }
if (imageLoaded) { //prevent loading more than one image each render for performance
@@ -139,15 +152,7 @@ public class ImageCache {
imageLoaded = true;
}
try {
image = cache.get(imageKey);
}
catch (final ExecutionException ex) {
if (!(ex.getCause() instanceof NullPointerException)) {
ex.printStackTrace();
}
image = null;
}
try { image = cache.get(imageKey); }
catch (final Exception ex) {
image = null;
}

View File

@@ -3,8 +3,12 @@ package forge.assets;
import java.io.File;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.google.common.cache.CacheLoader;
import com.badlogic.gdx.graphics.TextureData;
import com.badlogic.gdx.graphics.glutils.PixmapTextureData;
import org.cache2k.integration.CacheLoader;
import forge.Forge;
import forge.ImageKeys;
@@ -12,27 +16,49 @@ import forge.ImageKeys;
final class ImageLoader extends CacheLoader<String, Texture> {
@Override
public Texture load(String key) {
File file = ImageKeys.getImageFile(key);
boolean mask = key.contains("#drawroundcorner#");
boolean alphaCard = false;
boolean textureFilter = Forge.isTextureFilteringEnabled();
if (key.length() > 4){
if ((key.substring(0,4).contains("LEA/")) || (key.substring(0,2).contains("A/")))
alphaCard = true;
//TODO dont add border on some sets???
}
File file = ImageKeys.getImageFile(key.replaceAll("#drawroundcorner#",""));
if (file != null) {
FileHandle fh = new FileHandle(file);
try {
if (Forge.isTextureFilteringEnabled()) {
Texture t = new Texture(fh, true);
t.setFilter(Texture.TextureFilter.MipMapLinearLinear, Texture.TextureFilter.Linear);
/* // Optional experimental feature: Anisotropic filtering
GL20 gl = Gdx.gl20;
if (gl != null && Gdx.graphics.supportsExtension("GL_EXT_texture_filter_anisotropic")) {
FloatBuffer buffer = BufferUtils.newFloatBuffer(16);
gl.glGetFloatv(GL20.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, buffer);
float maxAniso = buffer.get(0);
t.bind();
gl.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAniso);
} */
Texture t;
if (mask) {
Pixmap pImage = new Pixmap(fh);
int w = pImage.getWidth();
int h = pImage.getHeight();
int radius = alphaCard ? (h - w) / 6 : (h - w) / 8;
Pixmap pMask = createRoundedRectangle(w, h, radius, Color.RED);
drawPixelstoMask(pImage, pMask);
TextureData textureData = new PixmapTextureData(
pMask, //pixmap to use
Pixmap.Format.RGBA8888,
textureFilter, //use mipmaps
false, true);
if (textureFilter)
{
t = new Texture(textureData);
t.setFilter(Texture.TextureFilter.MipMapLinearLinear, Texture.TextureFilter.Linear);
} else {
t = new Texture(textureData);
}
pImage.dispose();
pMask.dispose();
return t;
} else {
return new Texture(fh);
if (textureFilter) {
t = new Texture(fh, true);
t.setFilter(Texture.TextureFilter.MipMapLinearLinear, Texture.TextureFilter.Linear);
return t;
}
else
return new Texture(fh);
}
}
catch (Exception ex) {
@@ -41,4 +67,57 @@ final class ImageLoader extends CacheLoader<String, Texture> {
}
return null;
}
public Pixmap createRoundedRectangle(int width, int height, int cornerRadius, Color color) {
Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
Pixmap ret = new Pixmap(width, height, Pixmap.Format.RGBA8888);
pixmap.setColor(color);
//round corners
pixmap.fillCircle(cornerRadius, cornerRadius, cornerRadius);
pixmap.fillCircle(width - cornerRadius - 1, cornerRadius, cornerRadius);
pixmap.fillCircle(cornerRadius, height - cornerRadius - 1, cornerRadius);
pixmap.fillCircle(width - cornerRadius - 1, height - cornerRadius - 1, cornerRadius);
//two rectangle parts
pixmap.fillRectangle(cornerRadius, 0, width - cornerRadius * 2, height);
pixmap.fillRectangle(0, cornerRadius, width, height - cornerRadius * 2);
//draw rounded rectangle
ret.setColor(color);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (pixmap.getPixel(x, y) != 0) ret.drawPixel(x, y);
}
}
pixmap.dispose();
return ret;
}
public void drawPixelstoMask(Pixmap pixmap, Pixmap mask){
int pixmapWidth = mask.getWidth();
int pixmapHeight = mask.getHeight();
Color pixelColor = new Color();
for (int x=0; x<pixmapWidth; x++){
for (int y=0; y<pixmapHeight; y++){
if (mask.getPixel(x, y) != 0) {
Color.rgba8888ToColor(pixelColor, pixmap.getPixel(x, y));
mask.setColor(pixelColor);
mask.drawPixel(x, y);
}
}
}
}
public void blendPixmaps(Pixmap pixmap, Pixmap mask, Pixmap model){
int pixmapWidth = pixmap.getWidth();
int pixmapHeight = pixmap.getHeight();
Color pixelColor = new Color();
Color maskPixelColor = new Color();
for (int x=0; x<pixmapWidth; x++){
for (int y=0; y<pixmapHeight; y++){
Color.rgba8888ToColor(pixelColor, pixmap.getPixel(x, y));
Color.rgba8888ToColor(maskPixelColor, mask.getPixel(x, y));
pixelColor.a = pixelColor.a * maskPixelColor.a;
model.setColor(pixelColor);
model.drawPixel(x, y);
}
}
}
}

View File

@@ -8,6 +8,8 @@ import forge.assets.ImageCache;
import forge.card.CardRenderer.CardStackPosition;
import forge.game.card.CardView;
import forge.item.PaperCard;
import forge.model.FModel;
import forge.properties.ForgePreferences;
import forge.toolbox.FCardPanel;
public class CardImage implements FImage {
@@ -17,6 +19,9 @@ public class CardImage implements FImage {
public CardImage(PaperCard card0) {
card = card0;
}
private static boolean isPreferenceEnabled(ForgePreferences.FPref preferenceName) {
return FModel.getPreferences().getPrefBoolean(preferenceName);
}
@Override
public float getWidth() {
@@ -34,8 +39,12 @@ public class CardImage implements FImage {
@Override
public void draw(Graphics g, float x, float y, float w, float h) {
if (image == null) { //attempt to retrieve card image if needed
image = ImageCache.getImage(card);
boolean mask = isPreferenceEnabled(ForgePreferences.FPref.UI_ENABLE_BORDER_MASKING);
image = ImageCache.getImage(card, mask);
if (image == null) {
if (mask) //render this if mask is still loading
CardImageRenderer.drawCardImage(g, CardView.getCardForUi(card), false, x, y, w, h, CardStackPosition.Top);
return; //can't draw anything if can't be loaded yet
}
}

View File

@@ -325,7 +325,8 @@ public class CardImageRenderer {
}
public static void drawZoom(Graphics g, CardView card, GameView gameView, boolean altState, float x, float y, float w, float h, float dispW, float dispH, boolean isCurrentCard) {
final Texture image = ImageCache.getImage(card.getState(altState).getImageKey(MatchController.instance.getLocalPlayers()), true);
boolean mask = isPreferenceEnabled(ForgePreferences.FPref.UI_ENABLE_BORDER_MASKING);
final Texture image = ImageCache.getImage(card.getState(altState).getImageKey(MatchController.instance.getLocalPlayers()), true, mask);
if (image == null) { //draw details if can't draw zoom
drawDetails(g, card, gameView, altState, x, y, w, h);
return;
@@ -521,4 +522,8 @@ public class CardImageRenderer {
g.drawRect(BORDER_THICKNESS, Color.BLACK, x, y, w, h);
g.drawText(ptText, PT_FONT, Color.BLACK, x, y, w, h, false, Align.center, true);
}
private static boolean isPreferenceEnabled(ForgePreferences.FPref preferenceName) {
return FModel.getPreferences().getPrefBoolean(preferenceName);
}
}

View File

@@ -392,7 +392,8 @@ public class CardRenderer {
}
public static void drawCard(Graphics g, IPaperCard pc, float x, float y, float w, float h, CardStackPosition pos) {
Texture image = new RendererCachedCardImage(pc, false).getImage();
boolean mask = isPreferenceEnabled(FPref.UI_ENABLE_BORDER_MASKING);
Texture image = new RendererCachedCardImage(pc, false).getImage(mask);
if (image != null) {
if (image == ImageCache.defaultImage) {
@@ -409,13 +410,17 @@ public class CardRenderer {
drawFoilEffect(g, card, x, y, w, h, false);
}
}
else { //draw cards without textures as just a black rectangle
g.fillRect(Color.BLACK, x, y, w, h);
else {
if (mask) //render this if mask is still loading
CardImageRenderer.drawCardImage(g, CardView.getCardForUi(pc), false, x, y, w, h, pos);
else //draw cards without textures as just a black rectangle
g.fillRect(Color.BLACK, x, y, w, h);
}
}
public static void drawCard(Graphics g, CardView card, float x, float y, float w, float h, CardStackPosition pos, boolean rotate) {
Texture image = new RendererCachedCardImage(card, false).getImage();
boolean mask = isPreferenceEnabled(FPref.UI_ENABLE_BORDER_MASKING);
Texture image = new RendererCachedCardImage(card, false).getImage(mask);
if (image != null) {
if (image == ImageCache.defaultImage) {
@@ -430,8 +435,11 @@ public class CardRenderer {
}
drawFoilEffect(g, card, x, y, w, h, false);
}
else { //draw cards without textures as just a black rectangle
g.fillRect(Color.BLACK, x, y, w, h);
else {
if (mask) //render this if mask is still loading
CardImageRenderer.drawCardImage(g, card, false, x, y, w, h, pos);
else //draw cards without textures as just a black rectangle
g.fillRect(Color.BLACK, x, y, w, h);
}
}

View File

@@ -301,6 +301,10 @@ public class SettingsPage extends TabPage<SettingsScreen> {
localizer.getMessage("lblDisableCardEffect"),
localizer.getMessage("nlDisableCardEffect")),
4);
lstSettings.addItem(new BooleanSetting(FPref.UI_ENABLE_BORDER_MASKING,
"Enable Round Border Mask",
"When enabled, the card corners are rounded (Longer Caching)."),
4);
lstSettings.addItem(new CustomSelectSetting(FPref.UI_CARD_COUNTER_DISPLAY_TYPE,
localizer.getMessage("cbpCounterDisplayType"),

View File

@@ -134,6 +134,7 @@ public class ForgePreferences extends PreferencesStore<ForgePreferences.FPref> {
UI_ROTATE_SPLIT_CARDS("true"),
UI_DYNAMIC_PLANECHASE_BG("false"),
UI_DISABLE_IMAGES_EFFECT_CARDS("false"),
UI_ENABLE_BORDER_MASKING("false"),
UI_ALLOW_ORDER_GRAVEYARD_WHEN_NEEDED ("Never"),
UI_DEFAULT_FONT_SIZE("12"),
UI_SELECT_FROM_CARD_DISPLAYS("true"),