Replaced "BeatmapImageCache" with a generic LRU cache class.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-09-02 13:29:52 -05:00
parent c516d93d1e
commit d5154b567e
4 changed files with 82 additions and 91 deletions

View File

@ -126,7 +126,7 @@ public class Container extends AppGameContainer {
// reset image references // reset image references
GameImage.clearReferences(); GameImage.clearReferences();
GameData.Grade.clearReferences(); GameData.Grade.clearReferences();
Beatmap.getBackgroundImageCache().clear(); Beatmap.clearBackgroundImageCache();
// prevent loading tracks from re-initializing OpenAL // prevent loading tracks from re-initializing OpenAL
MusicController.reset(); MusicController.reset();

View File

@ -23,9 +23,11 @@ import itdelatrisu.opsu.Options;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Map;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.Image; import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.Log; import org.newdawn.slick.util.Log;
/** /**
@ -36,12 +38,28 @@ public class Beatmap implements Comparable<Beatmap> {
public static final byte MODE_OSU = 0, MODE_TAIKO = 1, MODE_CTB = 2, MODE_MANIA = 3; public static final byte MODE_OSU = 0, MODE_TAIKO = 1, MODE_CTB = 2, MODE_MANIA = 3;
/** Background image cache. */ /** Background image cache. */
private static final BeatmapImageCache bgImageCache = new BeatmapImageCache(); @SuppressWarnings("serial")
private static final LRUCache<File, Image> bgImageCache = new LRUCache<File, Image>(10) {
@Override
public void eldestRemoved(Map.Entry<File, Image> eldest) {
Image img = eldest.getValue();
if (img != null && !img.isDestroyed()) {
try {
img.destroy(); // destroy the removed image
} catch (SlickException e) {
Log.warn(String.format("Failed to destroy image '%s'.", img.getResourceReference()), e);
}
}
}
};
/** /**
* Returns the background image cache. * Clears the background image 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 static BeatmapImageCache getBackgroundImageCache() { return bgImageCache; } public static void clearBackgroundImageCache() { bgImageCache.clear(); }
/** The OSU File object associated with this beatmap. */ /** The OSU File object associated with this beatmap. */
private File file; private File file;
@ -269,7 +287,7 @@ public class Beatmap implements Comparable<Beatmap> {
Image bgImage = bgImageCache.get(this); Image bgImage = bgImageCache.get(this);
if (bgImage == null) { if (bgImage == null) {
bgImage = new Image(bg.getAbsolutePath()); bgImage = new Image(bg.getAbsolutePath());
bgImageCache.put(this, bgImage); bgImageCache.put(bg, bgImage);
} }
int swidth = width; int swidth = width;

View File

@ -1,86 +0,0 @@
/*
* 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(); }
}

View File

@ -0,0 +1,59 @@
/*
* 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.util.LinkedHashMap;
import java.util.Map;
/**
* Least recently used cache.
*
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
*/
@SuppressWarnings("serial")
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
/** The cache capacity. */
private final int capacity;
/**
* Creates a least recently used cache with the given capacity.
* @param capacity the capacity
*/
public LRUCache(int capacity) {
super(capacity + 1, 1.1f, true);
this.capacity = capacity;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
if (size() > capacity) {
eldestRemoved(eldest);
return true;
}
return false;
}
/**
* Notification that the eldest entry was removed.
* Can be used to clean up any resources when this happens (via override).
* @param eldest the removed entry
*/
public void eldestRemoved(Map.Entry<K, V> eldest) {}
}