1 package com.bumptech.glide.load.engine.bitmap_recycle; 2 3 import android.graphics.Bitmap; 4 import com.bumptech.glide.util.Util; 5 6 /** 7 * A strategy for reusing bitmaps that requires any returned bitmap's dimensions to exactly match those request. 8 */ 9 class AttributeStrategy implements LruPoolStrategy { 10 private final KeyPool keyPool = new KeyPool(); 11 private final GroupedLinkedMap<Key, Bitmap> groupedMap = new GroupedLinkedMap<Key, Bitmap>(); 12 put(Bitmap bitmap)13 public void put(Bitmap bitmap) { 14 final Key key = keyPool.get(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); 15 16 groupedMap.put(key, bitmap); 17 } 18 19 @Override get(int width, int height, Bitmap.Config config)20 public Bitmap get(int width, int height, Bitmap.Config config) { 21 final Key key = keyPool.get(width, height, config); 22 23 return groupedMap.get(key); 24 } 25 26 @Override removeLast()27 public Bitmap removeLast() { 28 return groupedMap.removeLast(); 29 } 30 31 @Override logBitmap(Bitmap bitmap)32 public String logBitmap(Bitmap bitmap) { 33 return getBitmapString(bitmap); 34 } 35 36 @Override logBitmap(int width, int height, Bitmap.Config config)37 public String logBitmap(int width, int height, Bitmap.Config config) { 38 return getBitmapString(width, height, config); 39 } 40 41 @Override getSize(Bitmap bitmap)42 public int getSize(Bitmap bitmap) { 43 return Util.getSize(bitmap); 44 } 45 46 @Override toString()47 public String toString() { 48 return "AttributeStrategy:\n " + groupedMap; 49 } 50 getBitmapString(Bitmap bitmap)51 private static String getBitmapString(Bitmap bitmap) { 52 return getBitmapString(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); 53 } 54 getBitmapString(int width, int height, Bitmap.Config config)55 private static String getBitmapString(int width, int height, Bitmap.Config config) { 56 return "[" + width + "x" + height + "], " + config; 57 } 58 59 private static class KeyPool extends BaseKeyPool<Key> { get(int width, int height, Bitmap.Config config)60 public Key get(int width, int height, Bitmap.Config config) { 61 Key result = get(); 62 result.init(width, height, config); 63 return result; 64 } 65 66 @Override create()67 protected Key create() { 68 return new Key(this); 69 } 70 } 71 72 private static class Key implements Poolable { 73 private final KeyPool pool; 74 private int width; 75 private int height; 76 // Config can be null :( 77 private Bitmap.Config config; 78 Key(KeyPool pool)79 public Key(KeyPool pool) { 80 this.pool = pool; 81 } 82 init(int width, int height, Bitmap.Config config)83 public void init(int width, int height, Bitmap.Config config) { 84 this.width = width; 85 this.height = height; 86 this.config = config; 87 } 88 89 @Override equals(Object o)90 public boolean equals(Object o) { 91 if (this == o) return true; 92 if (o == null || getClass() != o.getClass()) return false; 93 94 Key key = (Key) o; 95 96 if (height != key.height) return false; 97 if (width != key.width) return false; 98 if (config != key.config) return false; 99 100 return true; 101 } 102 103 @Override hashCode()104 public int hashCode() { 105 int result = width; 106 result = 31 * result + height; 107 result = 31 * result + (config != null ? config.hashCode() : 0); 108 return result; 109 } 110 111 @Override toString()112 public String toString() { 113 return getBitmapString(width, height, config); 114 } 115 116 @Override offer()117 public void offer() { 118 pool.offer(this); 119 } 120 } 121 } 122