Java Code Examples for com.facebook.common.references.CloseableReference#close()
The following examples show how to use
com.facebook.common.references.CloseableReference#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultImageDecoder.java From fresco with MIT License | 6 votes |
/** * @param encodedImage input image (encoded bytes plus meta data) * @return a CloseableStaticBitmap */ public CloseableStaticBitmap decodeStaticImage( final EncodedImage encodedImage, ImageDecodeOptions options) { CloseableReference<Bitmap> bitmapReference = mPlatformDecoder.decodeFromEncodedImageWithColorSpace( encodedImage, options.bitmapConfig, null, options.colorSpace); try { maybeApplyTransformation(options.bitmapTransformation, bitmapReference); return new CloseableStaticBitmap( bitmapReference, ImmutableQualityInfo.FULL_QUALITY, encodedImage.getRotationAngle(), encodedImage.getExifOrientation()); } finally { bitmapReference.close(); } }
Example 2
Source File: PipelineDraweeController.java From fresco with MIT License | 6 votes |
@Override protected @Nullable CloseableReference<CloseableImage> getCachedImage() { if (FrescoSystrace.isTracing()) { FrescoSystrace.beginSection("PipelineDraweeController#getCachedImage"); } try { if (mMemoryCache == null || mCacheKey == null) { return null; } // We get the CacheKey CloseableReference<CloseableImage> closeableImage = mMemoryCache.get(mCacheKey); if (closeableImage != null && !closeableImage.get().getQualityInfo().isOfFullQuality()) { closeableImage.close(); return null; } return closeableImage; } finally { if (FrescoSystrace.isTracing()) { FrescoSystrace.endSection(); } } }
Example 3
Source File: CountingMemoryCacheTest.java From fresco with MIT License | 6 votes |
@Test public void testCanReuseExclusive() { CloseableReference<Integer> cachedRef = mCache.cache(KEY, newReference(100), mEntryStateObserver); cachedRef.close(); verify(mEntryStateObserver).onExclusivityChanged(KEY, true); assertTotalSize(1, 100); assertExclusivelyOwnedSize(1, 100); cachedRef = mCache.reuse(KEY); assertNotNull(cachedRef); verify(mEntryStateObserver).onExclusivityChanged(KEY, false); assertTotalSize(0, 0); assertExclusivelyOwnedSize(0, 0); cachedRef.close(); verify(mEntryStateObserver).onExclusivityChanged(KEY, true); }
Example 4
Source File: CountingMemoryCacheTest.java From fresco with MIT License | 6 votes |
@Test public void testClear() { CloseableReference<Integer> originalRef1 = newReference(110); CloseableReference<Integer> cachedRef1 = mCache.cache(KEYS[1], originalRef1); originalRef1.close(); CountingMemoryCache.Entry<String, Integer> entry1 = mCache.mCachedEntries.get(KEYS[1]); CloseableReference<Integer> originalRef2 = newReference(120); CloseableReference<Integer> cachedRef2 = mCache.cache(KEYS[2], originalRef2); originalRef2.close(); cachedRef2.close(); mCache.clear(); assertTotalSize(0, 0); assertExclusivelyOwnedSize(0, 0); assertOrphanWithCount(entry1, 1); assertNotCached(KEYS[2], 120); verify(mReleaser).release(120); cachedRef1.close(); verify(mReleaser).release(110); }
Example 5
Source File: FrescoFrameCacheTest.java From fresco with MIT License | 6 votes |
@Test public void testExtractAndClose_whenBitmapReferenceInvalid_thenReturnReference() throws Exception { when(mBitmapReference.isValid()).thenReturn(false); CloseableReference<Bitmap> extractedReference = FrescoFrameCache.convertToBitmapReferenceAndClose(mImageReference); // We only detach the reference and do not care if the bitmap reference is valid assertThat(extractedReference).isNotNull(); assertThat(extractedReference.get()).isEqualTo(mUnderlyingBitmap); extractedReference.close(); verify(mImageReference).close(); }
Example 6
Source File: CountingMemoryCacheTest.java From fresco with MIT License | 6 votes |
@Test public void testContains() { assertFalse(mCache.contains(KEY)); CloseableReference<Integer> newRef = mCache.cache(KEY, newReference(100)); assertTrue(mCache.contains(KEY)); assertFalse(mCache.contains(KEYS[0])); newRef.close(); assertTrue(mCache.contains(KEY)); assertFalse(mCache.contains(KEYS[0])); CloseableReference<Integer> reuse = mCache.reuse(KEY); reuse.close(); assertFalse(mCache.contains(KEY)); assertFalse(mCache.contains(KEYS[0])); }
Example 7
Source File: CountingMemoryCacheTest.java From fresco with MIT License | 6 votes |
@Test public void testReuseExclusive_CacheSameItemWithDifferentKey() { CloseableReference<Integer> cachedRef = mCache.cache(KEY, newReference(100), mEntryStateObserver); cachedRef.close(); verify(mEntryStateObserver).onExclusivityChanged(KEY, true); assertTotalSize(1, 100); assertExclusivelyOwnedSize(1, 100); cachedRef = mCache.reuse(KEY); assertNotNull(cachedRef); verify(mEntryStateObserver).onExclusivityChanged(KEY, false); assertTotalSize(0, 0); assertExclusivelyOwnedSize(0, 0); CloseableReference<Integer> newItem = mCache.cache(KEYS[2], cachedRef); cachedRef.close(); assertTotalSize(1, 100); assertExclusivelyOwnedSize(0, 0); newItem.close(); verify(mEntryStateObserver).onExclusivityChanged(KEY, true); assertTotalSize(1, 100); assertExclusivelyOwnedSize(1, 100); }
Example 8
Source File: CountingMemoryCache.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 6 votes |
/** * Removes all entries from the cache. Exclusively owned references are closed, and shared values * becomes orphans. */ public void clear() { Collection<CloseableReference<V>> evictedEntries; synchronized (this) { evictedEntries = trimEvictionQueueTo(0, 0); for (CacheEntry<K, V> cacheEntry : Lists.newArrayList(mCachedEntries.keySet())) { moveFromCachedEntriesToOrphans(cacheEntry); mMemoryCacheIndex.removeEntry(cacheEntry.key, cacheEntry.value); } } for (CloseableReference<V> reference : evictedEntries) { reference.close(); } }
Example 9
Source File: CloseableStaticBitmap.java From fresco with MIT License | 5 votes |
/** Releases the bitmap to the pool. */ @Override public void close() { CloseableReference<Bitmap> reference = detachBitmapReference(); if (reference != null) { reference.close(); } }
Example 10
Source File: ImagePipeline.java From fresco with MIT License | 5 votes |
/** * Returns a reference to the cached image * * @param cacheKey * @return a closeable reference or null if image was not present in cache */ @Nullable public CloseableReference<CloseableImage> getCachedImage(@Nullable CacheKey cacheKey) { MemoryCache<CacheKey, CloseableImage> memoryCache = mBitmapMemoryCache; if (memoryCache == null || cacheKey == null) { return null; } CloseableReference<CloseableImage> closeableImage = memoryCache.get(cacheKey); if (closeableImage != null && !closeableImage.get().getQualityInfo().isOfFullQuality()) { closeableImage.close(); return null; } return closeableImage; }
Example 11
Source File: FrescoFrameCacheTest.java From fresco with MIT License | 5 votes |
@Test public void testExtractAndClose() throws Exception { CloseableReference<Bitmap> extractedReference = FrescoFrameCache.convertToBitmapReferenceAndClose(mImageReference); assertThat(extractedReference).isNotNull(); assertThat(extractedReference.get()).isEqualTo(mUnderlyingBitmap); verify(mImageReference).close(); extractedReference.close(); }
Example 12
Source File: AnimatedFrameCacheTest.java From fresco with MIT License | 5 votes |
@Test public void testContainsFullReuseFlowWithMultipleItems() { assertFalse(mAnimatedFrameCache.contains(1)); assertFalse(mAnimatedFrameCache.contains(2)); CloseableReference<CloseableImage> ret = mAnimatedFrameCache.cache(1, mFrame1); CloseableReference<CloseableImage> ret2 = mAnimatedFrameCache.cache(2, mFrame2); assertTrue(mAnimatedFrameCache.contains(1)); assertTrue(mAnimatedFrameCache.contains(2)); ret.close(); ret2.close(); assertTrue(mAnimatedFrameCache.contains(1)); assertTrue(mAnimatedFrameCache.contains(2)); CloseableReference<CloseableImage> free = mAnimatedFrameCache.getForReuse(); free.close(); assertFalse(mAnimatedFrameCache.contains(1)); assertTrue(mAnimatedFrameCache.contains(2)); free = mAnimatedFrameCache.getForReuse(); free.close(); assertFalse(mAnimatedFrameCache.contains(1)); assertFalse(mAnimatedFrameCache.contains(2)); }
Example 13
Source File: AnimatedFrameCacheTest.java From fresco with MIT License | 5 votes |
@Test public void testReuse() { CloseableReference<CloseableImage> ret = mAnimatedFrameCache.cache(1, mFrame1); ret.close(); CloseableReference<CloseableImage> free = mAnimatedFrameCache.getForReuse(); assertNotNull(free); }
Example 14
Source File: CloseableStaticBitmap.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 5 votes |
/** * Releases the bitmap to the pool. */ @Override public void close() { CloseableReference<Bitmap> reference; synchronized (this) { if (mBitmapReference == null) { return; } reference = mBitmapReference; mBitmapReference = null; mBitmap = null; } reference.close(); }
Example 15
Source File: CountingMemoryCacheTest.java From fresco with MIT License | 5 votes |
@Test public void testClosingClientReference() { CloseableReference<Integer> cachedRef = mCache.cache(KEY, newReference(100)); // cached item should get exclusively owned cachedRef.close(); assertTotalSize(1, 100); assertExclusivelyOwnedSize(1, 100); assertExclusivelyOwned(KEY, 100); verify(mReleaser, never()).release(anyInt()); }
Example 16
Source File: CountingMemoryCacheTest.java From fresco with MIT License | 4 votes |
@Test public void testRemoveAllMatchingPredicate() { CloseableReference<Integer> originalRef1 = newReference(110); CloseableReference<Integer> valueRef1 = mCache.cache(KEYS[1], originalRef1); originalRef1.close(); valueRef1.close(); CloseableReference<Integer> originalRef2 = newReference(120); CloseableReference<Integer> valueRef2 = mCache.cache(KEYS[2], originalRef2); originalRef2.close(); valueRef2.close(); CloseableReference<Integer> originalRef3 = newReference(130); CloseableReference<Integer> valueRef3 = mCache.cache(KEYS[3], originalRef3); originalRef3.close(); CountingMemoryCache.Entry<String, Integer> entry3 = mCache.mCachedEntries.get(KEYS[3]); CloseableReference<Integer> originalRef4 = newReference(150); CloseableReference<Integer> valueRef4 = mCache.cache(KEYS[4], originalRef4); originalRef4.close(); int numEvictedEntries = mCache.removeAll( new Predicate<String>() { @Override public boolean apply(String key) { return key.equals(KEYS[2]) || key.equals(KEYS[3]); } }); assertEquals(2, numEvictedEntries); assertTotalSize(2, 260); assertExclusivelyOwnedSize(1, 110); assertExclusivelyOwned(KEYS[1], 110); assertNotCached(KEYS[2], 120); assertOrphanWithCount(entry3, 1); assertSharedWithCount(KEYS[4], 150, 1); verify(mReleaser).release(120); verify(mReleaser, never()).release(130); valueRef3.close(); verify(mReleaser).release(130); }
Example 17
Source File: CountingMemoryCacheTest.java From fresco with MIT License | 4 votes |
@Test public void testInUseCount() { CloseableReference<Integer> cachedRef1 = mCache.cache(KEY, newReference(100)); CloseableReference<Integer> cachedRef2a = mCache.get(KEY); CloseableReference<Integer> cachedRef2b = cachedRef2a.clone(); assertTotalSize(1, 100); assertExclusivelyOwnedSize(0, 0); assertSharedWithCount(KEY, 100, 2); CloseableReference<Integer> cachedRef3a = mCache.get(KEY); CloseableReference<Integer> cachedRef3b = cachedRef3a.clone(); CloseableReference<Integer> cachedRef3c = cachedRef3b.clone(); assertTotalSize(1, 100); assertExclusivelyOwnedSize(0, 0); assertSharedWithCount(KEY, 100, 3); cachedRef1.close(); assertTotalSize(1, 100); assertExclusivelyOwnedSize(0, 0); assertSharedWithCount(KEY, 100, 2); // all copies of cachedRef2a need to be closed for usage count to drop cachedRef2a.close(); assertTotalSize(1, 100); assertExclusivelyOwnedSize(0, 0); assertSharedWithCount(KEY, 100, 2); cachedRef2b.close(); assertTotalSize(1, 100); assertExclusivelyOwnedSize(0, 0); assertSharedWithCount(KEY, 100, 1); // all copies of cachedRef3a need to be closed for usage count to drop cachedRef3c.close(); assertTotalSize(1, 100); assertExclusivelyOwnedSize(0, 0); assertSharedWithCount(KEY, 100, 1); cachedRef3b.close(); assertTotalSize(1, 100); assertExclusivelyOwnedSize(0, 0); assertSharedWithCount(KEY, 100, 1); cachedRef3a.close(); assertTotalSize(1, 100); assertExclusivelyOwnedSize(1, 100); assertExclusivelyOwned(KEY, 100); }
Example 18
Source File: MemoryCacheProducer.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 4 votes |
@Override public void produceResults( final Consumer<CloseableReference<T>> consumer, final ProducerContext producerContext) { final ProducerListener listener = producerContext.getListener(); final String requestId = producerContext.getId(); listener.onProducerStart(requestId, getProducerName()); final K cacheKey = getCacheKey(producerContext.getImageRequest()); CloseableReference<T> cachedReference = mMemoryCache.get(cacheKey, null); if (cachedReference != null) { boolean shouldStartNextProducer = shouldStartNextProducer(cachedReference); if (!shouldStartNextProducer) { listener.onProducerFinishWithSuccess( requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(CACHED_VALUE_FOUND, "true") : null); } consumer.onNewResult(cachedReference, !shouldStartNextProducer); cachedReference.close(); if (!shouldStartNextProducer) { return; } } Consumer<CloseableReference<T>> consumerOfNextProducer; if (!shouldCacheReturnedValues()) { consumerOfNextProducer = consumer; } else { consumerOfNextProducer = new BaseConsumer<CloseableReference<T>>() { @Override public void onNewResultImpl( CloseableReference<T> newResult, boolean isLast) { CloseableReference<T> cachedResult = null; if (newResult != null && shouldCacheResult(newResult, cacheKey, isLast)) { cachedResult = mMemoryCache.cache(cacheKey, newResult); } if (cachedResult != null) { consumer.onNewResult(cachedResult, isLast); cachedResult.close(); } else { consumer.onNewResult(newResult, isLast); } } @Override public void onFailureImpl(Throwable t) { consumer.onFailure(t); } @Override protected void onCancellationImpl() { consumer.onCancellation(); } }; } listener.onProducerFinishWithSuccess( requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(CACHED_VALUE_FOUND, "false") : null); mNextProducer.produceResults(consumerOfNextProducer, producerContext); }
Example 19
Source File: AnimatedFrameCacheTest.java From fresco with MIT License | 4 votes |
@Test public void testStillThereIfClosed() { CloseableReference<CloseableImage> ret = mAnimatedFrameCache.cache(1, mFrame1); ret.close(); assertNotNull(mAnimatedFrameCache.get(1)); }
Example 20
Source File: AnimatedImageCompositor.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 4 votes |
/** * Given a frame number, prepares the canvas to render based on the nearest cached frame * at or before the frame. On return the canvas will be prepared as if the nearest cached * frame had been rendered and disposed. The returned index is the next frame that needs to be * composited onto the canvas. * * @param previousFrameNumber the frame number that is ones less than the one we're rendering * @param canvas the canvas to prepare * @return the index of the the next frame to process */ private int prepareCanvasWithClosestCachedFrame(int previousFrameNumber, Canvas canvas) { for (int index = previousFrameNumber; index >= 0; index--) { FrameNeededResult neededResult = isFrameNeededForRendering(index); switch (neededResult) { case REQUIRED: AnimatedDrawableFrameInfo frameInfo = mAnimatedDrawableBackend.getFrameInfo(index); CloseableReference<Bitmap> startBitmap = mCallback.getCachedBitmap(index); if (startBitmap != null) { try { canvas.drawBitmap(startBitmap.get(), 0, 0, null); if (frameInfo.disposalMethod == DisposalMethod.DISPOSE_TO_BACKGROUND) { canvas.drawRect( frameInfo.xOffset, frameInfo.yOffset, frameInfo.width, frameInfo.height, mTransparentFillPaint); } return index + 1; } finally { startBitmap.close(); } } else { if (!frameInfo.shouldBlendWithPreviousFrame) { return index; } else { // Keep going. break; } } case NOT_REQUIRED: return index + 1; case ABORT: return index; case SKIP: default: // Keep going. } } return 0; }