diff --git a/src/java/com/threerings/cast/bundle/BundledComponentRepository.java b/src/java/com/threerings/cast/bundle/BundledComponentRepository.java index 6d8f5d3d..6ea60e8e 100644 --- a/src/java/com/threerings/cast/bundle/BundledComponentRepository.java +++ b/src/java/com/threerings/cast/bundle/BundledComponentRepository.java @@ -355,10 +355,13 @@ public class BundledComponentRepository { // we don't need our images prepared for screen rendering BufferedImage src = _imgr.getImage(getImageKey(path), zations); + float percentageOfDataBuffer = 1; if (bounds != null) { + percentageOfDataBuffer = + (bounds.height * bounds.width) / (float)(src.getHeight() * src.getWidth()); src = src.getSubimage(bounds.x, bounds.y, bounds.width, bounds.height); } - return new BufferedMirage(src); + return new BufferedMirage(src, percentageOfDataBuffer); } /** The resource bundle from which we obtain image data. */ diff --git a/src/java/com/threerings/media/image/BufferedMirage.java b/src/java/com/threerings/media/image/BufferedMirage.java index 39b223f0..4e5a67b4 100644 --- a/src/java/com/threerings/media/image/BufferedMirage.java +++ b/src/java/com/threerings/media/image/BufferedMirage.java @@ -30,8 +30,19 @@ import java.awt.image.BufferedImage; public class BufferedMirage implements Mirage { public BufferedMirage (BufferedImage image) + { + this(image, 1); + } + + /** + * @param percentageOfDataBuffer - the percentage of image's data buffer used by image for use + * in getEstimatedMemory. ie if this image is a subimage of another image, and they share a + * data buffer, this is the percentage of the size of this image compared to the source. + */ + public BufferedMirage (BufferedImage image, float percentageOfDataBuffer) { _image = image; + _percentageOfDataBuffer = percentageOfDataBuffer; } // documentation inherited from interface @@ -61,7 +72,8 @@ public class BufferedMirage implements Mirage // documentation inherited from interface public long getEstimatedMemoryUsage () { - return ImageUtil.getEstimatedMemoryUsage(_image.getRaster()); + return (long)(ImageUtil.getEstimatedMemoryUsage(_image.getRaster()) * + _percentageOfDataBuffer); } // documentation inherited from interface @@ -69,6 +81,8 @@ public class BufferedMirage implements Mirage { return _image; } + + protected float _percentageOfDataBuffer; protected BufferedImage _image; } diff --git a/src/java/com/threerings/media/image/ImageManager.java b/src/java/com/threerings/media/image/ImageManager.java index f6e3718c..81e82053 100644 --- a/src/java/com/threerings/media/image/ImageManager.java +++ b/src/java/com/threerings/media/image/ImageManager.java @@ -273,7 +273,7 @@ public class ImageManager } if (crec != null) { // Log.info("Cache hit [key=" + key + ", crec=" + crec + "]."); - return crec.getImage(zations); + return crec.getImage(zations, _ccache); } // Log.info("Cache miss [key=" + key + ", crec=" + crec + "]."); @@ -298,7 +298,7 @@ public class ImageManager // periodically report our image cache performance reportCachePerformance(); - return crec.getImage(zations); + return crec.getImage(zations, _ccache); } /** @@ -345,6 +345,7 @@ public class ImageManager { BufferedImage src = null; + float percentageOfDataBuffer = 1; if (bounds == null) { // if they specified no bounds, we need to load up the raw image and determine its // bounds so that we can pass those along to the created mirage @@ -353,6 +354,8 @@ public class ImageManager } else if (!_prepareImages.getValue()) { src = getImage(key, zations); + percentageOfDataBuffer = + (bounds.width * bounds.height)/(float)(src.getHeight() * src.getWidth()); src = src.getSubimage(bounds.x, bounds.y, bounds.width, bounds.height); } @@ -361,7 +364,7 @@ public class ImageManager } else if (_prepareImages.getValue()) { return new CachedVolatileMirage(this, key, bounds, zations); } else { - return new BufferedMirage(src); + return new BufferedMirage(src, percentageOfDataBuffer); } } @@ -493,9 +496,8 @@ public class ImageManager } eff = _ccache.getTrackedEffectiveness(); } - Log.info("ImageManager LRU [mem=" + (size / 1024) + "k" + ", size=" + _ccache.size() + - ", hits=" + eff[0] + ", misses=" + eff[1] + - ", totalKeys=" + _keySet.size() + "]."); + Log.info("ImageManager LRU [mem=" + (size / 1024) + "k, size=" + _ccache.size() + + ", hits=" + eff[0] + ", misses=" + eff[1] + ", totalKeys=" + _keySet.size() + "]."); } /** Maintains a source image and a set of colorized versions in the image cache. */ @@ -507,30 +509,31 @@ public class ImageManager _source = source; } - public BufferedImage getImage (Colorization[] zations) + public BufferedImage getImage (Colorization[] zations, LRUHashMap cache) { if (zations == null) { return _source; } if (_colorized == null) { - _colorized = new ArrayList(); + _colorized = new ArrayList>(); } // we search linearly through our list of colorized copies because it is not likely to // be very long int csize = _colorized.size(); for (int ii = 0; ii < csize; ii++) { - Tuple tup = (Tuple)_colorized.get(ii); - Colorization[] tzations = (Colorization[])tup.left; + Tuple tup = _colorized.get(ii); + Colorization[] tzations = tup.left; if (Arrays.equals(zations, tzations)) { - return (BufferedImage)tup.right; + return tup.right; } } try { BufferedImage cimage = ImageUtil.recolorImage(_source, zations); - _colorized.add(new Tuple(zations, cimage)); + _colorized.add(new Tuple(zations, cimage)); + cache.adjustSize((int)ImageUtil.getEstimatedMemoryUsage(cimage)); return cimage; } catch (Exception re) { @@ -543,7 +546,13 @@ public class ImageManager public long getEstimatedMemoryUsage () { - return ImageUtil.getEstimatedMemoryUsage(_source); + long usage = ImageUtil.getEstimatedMemoryUsage(_source); + if (_colorized != null) { + for (Tuple tup : _colorized) { + usage += ImageUtil.getEstimatedMemoryUsage(tup.right); + } + } + return usage; } public String toString () @@ -554,7 +563,7 @@ public class ImageManager protected ImageKey _key; protected BufferedImage _source; - protected ArrayList _colorized; + protected ArrayList> _colorized; } /** A reference to the resource manager via which we load image data by default. */ @@ -588,7 +597,7 @@ public class ImageManager /** Register our image cache size with the runtime adjustments framework. */ protected static RuntimeAdjust.IntAdjust _cacheSize = new RuntimeAdjust.IntAdjust( "Size (in kb of memory used) of the image manager LRU cache [requires restart]", - "narya.media.image.cache_size", MediaPrefs.config, 2048); + "narya.media.image.cache_size", MediaPrefs.config, 32768); /** Controls whether or not we prepare images or use raw versions. */ protected static RuntimeAdjust.BooleanAdjust _prepareImages = new RuntimeAdjust.BooleanAdjust(