private long getFreeMemoryCacheBytes() { return memoryCache.getMaxSize() - memoryCache.getCurrentSize(); }
@VisibleForTesting PreFillQueue generateAllocationOrder(PreFillType... preFillSizes) { final long maxSize = memoryCache.getMaxSize() - memoryCache.getCurrentSize() + bitmapPool.getMaxSize(); int totalWeight = 0; for (PreFillType size : preFillSizes) { totalWeight += size.getWeight(); } final float bytesPerWeight = maxSize / (float) totalWeight; Map<PreFillType, Integer> attributeToCount = new HashMap<>(); for (PreFillType size : preFillSizes) { int bytesForSize = Math.round(bytesPerWeight * size.getWeight()); int bytesPerBitmap = getSizeInBytes(size); int bitmapsForSize = bytesForSize / bytesPerBitmap; attributeToCount.put(size, bitmapsForSize); } return new PreFillQueue(attributeToCount); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(pool.getMaxSize()).thenReturn(poolSize); when(pool.getDirty(anyInt(), anyInt(), any(Bitmap.Config.class))) .thenAnswer(new CreateBitmap()); when(cache.getMaxSize()).thenReturn(cacheSize); bitmapPreFiller = new BitmapPreFiller(cache, pool, DecodeFormat.DEFAULT); }
@Test public void testAddsBitmapsToMemoryCacheIfMemoryCacheHasEnoughSpaceRemaining() { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(cache.getMaxSize()).thenReturn(Long.valueOf(Util.getBitmapByteSize(bitmap))); PreFillType size = new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()).setConfig(bitmap.getConfig()) .build(); Map<PreFillType, Integer> allocationOrder = new HashMap<>(); allocationOrder.put(size, 1); getHandler(allocationOrder).run(); verify(cache).put(any(Key.class), anyResource()); verify(pool, never()).put(any(Bitmap.class)); // TODO(b/20335397): This code was relying on Bitmap equality which Robolectric removed // assertThat(addedBitmaps).containsExactly(bitmap); }
@Test public void testAddsBitmapsToBitmapPoolIfMemoryCacheIsFull() { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(cache.getMaxSize()).thenReturn(0L); PreFillType size = new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()).setConfig(bitmap.getConfig()) .build(); Map<PreFillType, Integer> allocationOrder = new HashMap<>(); allocationOrder.put(size, 1); getHandler(allocationOrder).run(); verify(cache, never()).put(any(Key.class), anyResource()); // TODO(b/20335397): This code was relying on Bitmap equality which Robolectric removed // verify(pool).put(eq(bitmap)); // assertThat(addedBitmaps).containsExactly(bitmap); }
@Test public void testAddsBitmapsToPoolIfMemoryCacheIsNotFullButCannotFitBitmap() { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(cache.getMaxSize()).thenReturn((long) Util.getBitmapByteSize(bitmap) / 2); PreFillType size = new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()).setConfig(bitmap.getConfig()) .build(); Map<PreFillType, Integer> allocationOrder = new HashMap<>(); allocationOrder.put(size, 1); getHandler(allocationOrder).run(); verify(cache, never()).put(any(Key.class), anyResource()); // TODO(b/20335397): This code was relying on Bitmap equality which Robolectric removed //verify(pool).put(eq(bitmap)); //assertThat(addedBitmaps).containsExactly(bitmap); }
@Test public void testAllocationOrderRoundRobinsDifferentSizes() { when(pool.getMaxSize()).thenReturn(defaultBitmapSize); when(cache.getMaxSize()).thenReturn(defaultBitmapSize); PreFillType smallWidth = new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT) .setConfig(defaultBitmapConfig).build(); PreFillType smallHeight = new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2) .setConfig(defaultBitmapConfig).build(); PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder(smallWidth, smallHeight); List<PreFillType> attributes = new ArrayList<>(); while (!allocationOrder.isEmpty()) { attributes.add(allocationOrder.remove()); } // Either width, height, width, height or height, width, height, width. try { assertThat(attributes).containsExactly(smallWidth, smallHeight, smallWidth, smallHeight) .inOrder(); } catch (AssertionError e) { assertThat(attributes).containsExactly(smallHeight, smallWidth, smallHeight, smallWidth) .inOrder(); } }
private int getFreeMemoryCacheBytes() { return memoryCache.getMaxSize() - memoryCache.getCurrentSize(); }
private void refresh() { final BitmapPool pool = glide.getBitmapPool(); final String total = getSizeString(pool.getMaxSize()); final String memCacheCurrent = getSizeString(memoryCache.getCurrentSize()); final String memCacheMax = getSizeString(memoryCache.getMaxSize()); poolSizeLabel.setText(total); memCacheCurrentLabel.setText(memCacheCurrent); memCacheMaxLabel.setText(memCacheMax); }
PreFillQueue generateAllocationOrder(PreFillType[] preFillSizes) { final int maxSize = memoryCache.getMaxSize() - memoryCache.getCurrentSize() + bitmapPool.getMaxSize(); int totalWeight = 0; for (PreFillType size : preFillSizes) { totalWeight += size.getWeight(); } final float bytesPerWeight = maxSize / (float) totalWeight; Map<PreFillType, Integer> attributeToCount = new HashMap<PreFillType, Integer>(); for (PreFillType size : preFillSizes) { int bytesForSize = Math.round(bytesPerWeight * size.getWeight()); int bytesPerBitmap = getSizeInBytes(size); int bitmapsForSize = bytesForSize / bytesPerBitmap; attributeToCount.put(size, bitmapsForSize); } return new PreFillQueue(attributeToCount); }
private int getFreeMemoryCacheBytes() { return memoryCache.getMaxSize() - memoryCache.getCurrentSize(); }
PreFillQueue generateAllocationOrder(PreFillType... preFillSizes) { final int maxSize = memoryCache.getMaxSize() - memoryCache.getCurrentSize() + bitmapPool.getMaxSize(); int totalWeight = 0; for (PreFillType size : preFillSizes) { totalWeight += size.getWeight(); } final float bytesPerWeight = maxSize / (float) totalWeight; Map<PreFillType, Integer> attributeToCount = new HashMap<>(); for (PreFillType size : preFillSizes) { int bytesForSize = Math.round(bytesPerWeight * size.getWeight()); int bytesPerBitmap = getSizeInBytes(size); int bitmapsForSize = bytesForSize / bytesPerBitmap; attributeToCount.put(size, bitmapsForSize); } return new PreFillQueue(attributeToCount); }