Switch to a LRU cache for beatmap background images.
Created a separate BeatmapImageCache class to handle cache operations. The cache now uses File objects as keys, rather than Beatmap objects (which was buggy). Also renamed "OsuHitObjectResult" helper class to "HitObjectResult". Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
@@ -22,12 +22,10 @@ import itdelatrisu.opsu.Options;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import org.newdawn.slick.Color;
|
||||
import org.newdawn.slick.Image;
|
||||
import org.newdawn.slick.SlickException;
|
||||
import org.newdawn.slick.util.Log;
|
||||
|
||||
/**
|
||||
@@ -37,11 +35,13 @@ public class Beatmap implements Comparable<Beatmap> {
|
||||
/** Game modes. */
|
||||
public static final byte MODE_OSU = 0, MODE_TAIKO = 1, MODE_CTB = 2, MODE_MANIA = 3;
|
||||
|
||||
/** Map of all loaded background images. */
|
||||
private static HashMap<Beatmap, Image> bgImageMap = new HashMap<Beatmap, Image>();
|
||||
/** Background image cache. */
|
||||
private static final BeatmapImageCache bgImageCache = new BeatmapImageCache();
|
||||
|
||||
/** Maximum number of cached images before all get erased. */
|
||||
private static final int MAX_CACHE_SIZE = 10;
|
||||
/**
|
||||
* Returns the background image cache.
|
||||
*/
|
||||
public static BeatmapImageCache getBackgroundImageCache() { return bgImageCache; }
|
||||
|
||||
/** The OSU File object associated with this beatmap. */
|
||||
private File file;
|
||||
@@ -156,11 +156,11 @@ public class Beatmap implements Comparable<Beatmap> {
|
||||
* [Events]
|
||||
*/
|
||||
|
||||
/** Background image file name. */
|
||||
public String bg;
|
||||
/** Background image file. */
|
||||
public File bg;
|
||||
|
||||
/** Background video file name. */
|
||||
// public String video;
|
||||
/** Background video file. */
|
||||
// public File video;
|
||||
|
||||
/** All break periods (start time, end time, ...). */
|
||||
public ArrayList<Integer> breaks;
|
||||
@@ -201,30 +201,6 @@ public class Beatmap implements Comparable<Beatmap> {
|
||||
/** Last object end time (in ms). */
|
||||
public int endTime = -1;
|
||||
|
||||
/**
|
||||
* Destroys all cached background images and resets the cache.
|
||||
*/
|
||||
public static void clearImageCache() {
|
||||
for (Image img : bgImageMap.values()) {
|
||||
if (img != null && !img.isDestroyed()) {
|
||||
try {
|
||||
img.destroy();
|
||||
} catch (SlickException e) {
|
||||
Log.warn(String.format("Failed to destroy image '%s'.", img.getResourceReference()), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
resetImageCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the image cache.
|
||||
* This does NOT destroy images, so be careful of memory leaks!
|
||||
*/
|
||||
public static void resetImageCache() {
|
||||
bgImageMap = new HashMap<Beatmap, Image>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param file the file associated with this beatmap
|
||||
@@ -287,12 +263,10 @@ public class Beatmap implements Comparable<Beatmap> {
|
||||
if (bg == null)
|
||||
return false;
|
||||
try {
|
||||
Image bgImage = bgImageMap.get(this);
|
||||
Image bgImage = bgImageCache.get(this);
|
||||
if (bgImage == null) {
|
||||
if (bgImageMap.size() > MAX_CACHE_SIZE)
|
||||
clearImageCache();
|
||||
bgImage = new Image(new File(file.getParentFile(), bg).getAbsolutePath());
|
||||
bgImageMap.put(this, bgImage);
|
||||
bgImage = new Image(bg.getAbsolutePath());
|
||||
bgImageCache.put(this, bgImage);
|
||||
}
|
||||
|
||||
int swidth = width;
|
||||
|
||||
86
src/itdelatrisu/opsu/beatmap/BeatmapImageCache.java
Normal file
86
src/itdelatrisu/opsu/beatmap/BeatmapImageCache.java
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* opsu! - an open-source osu! client
|
||||
* Copyright (C) 2014, 2015 Jeffrey Han
|
||||
*
|
||||
* opsu! is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* opsu! is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package itdelatrisu.opsu.beatmap;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.newdawn.slick.Image;
|
||||
import org.newdawn.slick.SlickException;
|
||||
import org.newdawn.slick.util.Log;
|
||||
|
||||
/**
|
||||
* LRU cache for beatmap background images.
|
||||
*/
|
||||
public class BeatmapImageCache {
|
||||
/** Maximum number of cached images. */
|
||||
private static final int MAX_CACHE_SIZE = 10;
|
||||
|
||||
/** Map of all loaded background images. */
|
||||
private LinkedHashMap<File, Image> cache;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public BeatmapImageCache() {
|
||||
this.cache = new LinkedHashMap<File, Image>(MAX_CACHE_SIZE + 1, 1.1f, true) {
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<File, Image> eldest) {
|
||||
if (size() > MAX_CACHE_SIZE) {
|
||||
// destroy the eldest image
|
||||
Image img = eldest.getValue();
|
||||
if (img != null && !img.isDestroyed()) {
|
||||
try {
|
||||
img.destroy();
|
||||
} catch (SlickException e) {
|
||||
Log.warn(String.format("Failed to destroy image '%s'.", img.getResourceReference()), e);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the image mapped to the specified beatmap.
|
||||
* @param beatmap the Beatmap
|
||||
* @return the Image, or {@code null} if no such mapping exists
|
||||
*/
|
||||
public Image get(Beatmap beatmap) { return cache.get(beatmap.bg); }
|
||||
|
||||
/**
|
||||
* Creates a mapping from the specified beatmap to the given image.
|
||||
* @param beatmap the Beatmap
|
||||
* @param image the Image
|
||||
* @return the previously mapped Image, or {@code null} if no such mapping existed
|
||||
*/
|
||||
public Image put(Beatmap beatmap, Image image) { return cache.put(beatmap.bg, image); }
|
||||
|
||||
/**
|
||||
* Removes all entries from the cache.
|
||||
* <p>
|
||||
* NOTE: This does NOT destroy the images in the cache, and will cause
|
||||
* memory leaks if all images have not been destroyed.
|
||||
*/
|
||||
public void clear() { cache.clear(); }
|
||||
}
|
||||
@@ -440,7 +440,7 @@ public class BeatmapParser {
|
||||
tokens[2] = tokens[2].replaceAll("^\"|\"$", "");
|
||||
String ext = BeatmapParser.getExtension(tokens[2]);
|
||||
if (ext.equals("jpg") || ext.equals("png"))
|
||||
beatmap.bg = getDBString(tokens[2]);
|
||||
beatmap.bg = new File(dir, getDBString(tokens[2]));
|
||||
break;
|
||||
case "2": // break periods
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user