Skip to content
Snippets Groups Projects
Commit 1c6ff588 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Use references in Cache

SoftReferences make in inefficient
parent 97830784
No related branches found
No related tags found
No related merge requests found
......@@ -31,20 +31,15 @@ import android.support.v4.util.LruCache;
import android.util.Log;
import org.videolan.libvlc.util.AndroidUtil;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import java.lang.ref.SoftReference;
import org.videolan.vlc.util.Strings;
public class BitmapCache {
public final static String TAG = "VLC/BitmapCache";
private final static boolean LOG_ENABLED = false;
private final static String TAG = "VLC/BitmapCache";
private static final String CONE_KEY = "res:"+ R.drawable.cone;
private static final String CONE_O_KEY = "res:"+ R.drawable.ic_cone_o;
private static BitmapCache mInstance;
private final LruCache<String, CacheableBitmap> mMemCache;
private final LruCache<String, Bitmap> mMemCache;
public synchronized static BitmapCache getInstance() {
if (mInstance == null)
......@@ -64,35 +59,30 @@ public class BitmapCache {
// Use 1/5th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 5;
Log.i(TAG, "LRUCache size set to " + cacheSize);
Log.i(TAG, "LRUCache size set to " + Strings.readableSize(cacheSize));
mMemCache = new LruCache<String, CacheableBitmap>(cacheSize) {
mMemCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, CacheableBitmap value) {
return value.getSize();
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
};
}
public synchronized Bitmap getBitmapFromMemCache(String key) {
final CacheableBitmap cacheableBitmap = mMemCache.get(key);
if (cacheableBitmap == null)
return null;
Bitmap b = cacheableBitmap.get();
final Bitmap b = mMemCache.get(key);
if (b == null){
mMemCache.remove(key);
return null;
}
if (LOG_ENABLED)
Log.d(TAG, (b == null) ? "Cache miss" : "Cache found");
return b;
}
public synchronized void addBitmapToMemCache(String key, Bitmap bitmap) {
if (key != null && bitmap != null && getBitmapFromMemCache(key) == null) {
final CacheableBitmap cacheableBitmap = new CacheableBitmap(bitmap);
mMemCache.put(key, cacheableBitmap);
mMemCache.put(key, bitmap);
}
}
......@@ -117,28 +107,4 @@ public class BitmapCache {
}
return bitmap;
}
private static class CacheableBitmap {
final int mSize;
final SoftReference<Bitmap> mReference;
CacheableBitmap(Bitmap bitmap){
mReference = new SoftReference<Bitmap>(bitmap);
mSize = bitmap == null ? 0 : bitmap.getRowBytes() * bitmap.getHeight();
}
public SoftReference<Bitmap> getReference(){
return mReference;
}
public Bitmap get(){
if (mReference != null)
return mReference.get();
return null;
}
public int getSize(){
return mSize;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment