use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class LruCountingMemoryCacheTest method testTrimming.
@Test
public void testTrimming() {
MemoryTrimType memoryTrimType = MemoryTrimType.OnCloseToDalvikHeapLimit;
mParams = new MemoryCacheParams(1100, 10, 1100, 10, 110, PARAMS_CHECK_INTERVAL_MS);
when(mParamsSupplier.get()).thenReturn(mParams);
PowerMockito.when(SystemClock.uptimeMillis()).thenReturn(PARAMS_CHECK_INTERVAL_MS);
InOrder inOrder = inOrder(mReleaser);
// create original references
CloseableReference<Integer>[] originalRefs = new CloseableReference[10];
for (int i = 0; i < 10; i++) {
originalRefs[i] = newReference(100 + i);
}
// cache items & close the original references
CloseableReference<Integer>[] cachedRefs = new CloseableReference[10];
for (int i = 0; i < 10; i++) {
cachedRefs[i] = mCache.cache(KEYS[i], originalRefs[i]);
originalRefs[i].close();
}
// cache should keep alive the items until evicted
inOrder.verify(mReleaser, never()).release(anyInt());
// trimming cannot evict shared entries
when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(1.00);
mCache.trim(memoryTrimType);
assertSharedWithCount(KEYS[0], 100, 1);
assertSharedWithCount(KEYS[1], 101, 1);
assertSharedWithCount(KEYS[2], 102, 1);
assertSharedWithCount(KEYS[3], 103, 1);
assertSharedWithCount(KEYS[4], 104, 1);
assertSharedWithCount(KEYS[5], 105, 1);
assertSharedWithCount(KEYS[6], 106, 1);
assertSharedWithCount(KEYS[7], 107, 1);
assertSharedWithCount(KEYS[8], 108, 1);
assertSharedWithCount(KEYS[9], 109, 1);
assertTotalSize(10, 1045);
assertExclusivelyOwnedSize(0, 0);
// close 7 client references
cachedRefs[8].close();
cachedRefs[2].close();
cachedRefs[7].close();
cachedRefs[3].close();
cachedRefs[6].close();
cachedRefs[4].close();
cachedRefs[5].close();
assertSharedWithCount(KEYS[0], 100, 1);
assertSharedWithCount(KEYS[1], 101, 1);
assertSharedWithCount(KEYS[9], 109, 1);
assertExclusivelyOwned(KEYS[8], 108);
assertExclusivelyOwned(KEYS[2], 102);
assertExclusivelyOwned(KEYS[7], 107);
assertExclusivelyOwned(KEYS[3], 103);
assertExclusivelyOwned(KEYS[6], 106);
assertExclusivelyOwned(KEYS[4], 104);
assertExclusivelyOwned(KEYS[5], 105);
assertTotalSize(10, 1045);
assertExclusivelyOwnedSize(7, 735);
// Trim cache by 45%. This means that out of total of 1045 bytes cached, 574 should remain.
// 310 bytes is used by the clients, which leaves 264 for the exclusively owned items.
// Only the two most recent exclusively owned items fit, and they occupy 209 bytes.
when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(0.45);
mCache.trim(memoryTrimType);
assertSharedWithCount(KEYS[0], 100, 1);
assertSharedWithCount(KEYS[1], 101, 1);
assertSharedWithCount(KEYS[9], 109, 1);
assertExclusivelyOwned(KEYS[4], 104);
assertExclusivelyOwned(KEYS[5], 105);
assertNotCached(KEYS[8], 108);
assertNotCached(KEYS[2], 102);
assertNotCached(KEYS[7], 107);
assertNotCached(KEYS[3], 103);
assertNotCached(KEYS[6], 106);
assertTotalSize(5, 519);
assertExclusivelyOwnedSize(2, 209);
inOrder.verify(mReleaser).release(108);
inOrder.verify(mReleaser).release(102);
inOrder.verify(mReleaser).release(107);
inOrder.verify(mReleaser).release(103);
inOrder.verify(mReleaser).release(106);
// Full trim. All exclusively owned items should be evicted.
when(mCacheTrimStrategy.getTrimRatio(memoryTrimType)).thenReturn(1.00);
mCache.trim(memoryTrimType);
assertSharedWithCount(KEYS[0], 100, 1);
assertSharedWithCount(KEYS[1], 101, 1);
assertSharedWithCount(KEYS[9], 109, 1);
assertNotCached(KEYS[8], 108);
assertNotCached(KEYS[2], 102);
assertNotCached(KEYS[7], 107);
assertNotCached(KEYS[3], 103);
assertNotCached(KEYS[6], 106);
assertNotCached(KEYS[6], 104);
assertNotCached(KEYS[6], 105);
assertTotalSize(3, 310);
assertExclusivelyOwnedSize(0, 0);
inOrder.verify(mReleaser).release(104);
inOrder.verify(mReleaser).release(105);
}
use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class ImagePipelineTest method testFetchLocalEncodedImage.
@Test
public void testFetchLocalEncodedImage() {
Producer<CloseableReference<PooledByteBuffer>> encodedSequence = mock(Producer.class);
when(mProducerSequenceFactory.getEncodedImageProducerSequence(mImageRequest)).thenReturn(encodedSequence);
when(mImageRequest.getSourceUri()).thenReturn(Uri.parse("file:///local/file"));
DataSource<CloseableReference<PooledByteBuffer>> dataSource = mImagePipeline.fetchEncodedImage(mImageRequest, mCallerContext);
assertFalse(dataSource.isFinished());
ArgumentCaptor<ImageRequest> argumentCaptor = ArgumentCaptor.forClass(ImageRequest.class);
verify(mRequestListener1).onRequestStart(argumentCaptor.capture(), eq(mCallerContext), eq("0"), eq(false));
ImageRequest capturedImageRequest = argumentCaptor.getValue();
assertSame(mImageRequest.getSourceUri(), capturedImageRequest.getSourceUri());
verify(mRequestListener2).onRequestStart(argumentCaptor.capture(), eq(mCallerContext), eq("0"), eq(false));
capturedImageRequest = argumentCaptor.getValue();
assertSame(mImageRequest.getSourceUri(), capturedImageRequest.getSourceUri());
ArgumentCaptor<ProducerContext> producerContextArgumentCaptor = ArgumentCaptor.forClass(ProducerContext.class);
verify(encodedSequence).produceResults(any(Consumer.class), producerContextArgumentCaptor.capture());
assertTrue(producerContextArgumentCaptor.getValue().isIntermediateResultExpected());
assertEquals(producerContextArgumentCaptor.getValue().getPriority(), Priority.HIGH);
}
use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class ImagePipelineTest method testFetchNetworkEncodedImage.
@Test
public void testFetchNetworkEncodedImage() {
Producer<CloseableReference<PooledByteBuffer>> encodedSequence = mock(Producer.class);
when(mProducerSequenceFactory.getEncodedImageProducerSequence(mImageRequest)).thenReturn(encodedSequence);
when(mImageRequest.getSourceUri()).thenReturn(Uri.parse("http://test"));
DataSource<CloseableReference<PooledByteBuffer>> dataSource = mImagePipeline.fetchEncodedImage(mImageRequest, mCallerContext);
assertFalse(dataSource.isFinished());
ArgumentCaptor<ImageRequest> argumentCaptor = ArgumentCaptor.forClass(ImageRequest.class);
verify(mRequestListener1).onRequestStart(argumentCaptor.capture(), eq(mCallerContext), eq("0"), eq(false));
ImageRequest capturedImageRequest = argumentCaptor.getValue();
assertSame(mImageRequest.getSourceUri(), capturedImageRequest.getSourceUri());
verify(mRequestListener2).onRequestStart(argumentCaptor.capture(), eq(mCallerContext), eq("0"), eq(false));
capturedImageRequest = argumentCaptor.getValue();
assertSame(mImageRequest.getSourceUri(), capturedImageRequest.getSourceUri());
ArgumentCaptor<ProducerContext> producerContextArgumentCaptor = ArgumentCaptor.forClass(ProducerContext.class);
verify(encodedSequence).produceResults(any(Consumer.class), producerContextArgumentCaptor.capture());
assertTrue(producerContextArgumentCaptor.getValue().isIntermediateResultExpected());
assertEquals(producerContextArgumentCaptor.getValue().getPriority(), Priority.HIGH);
}
use of com.facebook.common.references.CloseableReference in project ride-read-android by Ride-Read.
the class PersonalityMapActivity method addMoment2Map.
private void addMoment2Map(Moment moment) {
LatLng latLng = new LatLng(moment.getLatitude(), moment.getLongitude());
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(moment.getPictures().get(0) + QiNiuUtils.CROP_SMALL_100)).setProgressiveRenderingEnabled(true).build();
ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, Utils.getAppContext());
dataSource.subscribe(new BaseBitmapDataSubscriber() {
@Override
public void onNewResultImpl(@Nullable Bitmap bitmap) {
addMarker(latLng, bitmap, moment);
}
@Override
public void onFailureImpl(DataSource dataSource) {
}
}, CallerThreadExecutor.getInstance());
}
use of com.facebook.common.references.CloseableReference in project DevRing by LJYcoder.
the class FrescoManager method getBitmap.
@Override
public void getBitmap(Context context, String url, final ImageListener<Bitmap> imageListener) {
// 参考自https://github.com/hpdx/fresco-helper/blob/master/fresco-helper/src/main/java/com/facebook/fresco/helper/ImageLoader.java
Uri uri = Uri.parse(url);
ImagePipeline imagePipeline = Fresco.getImagePipeline();
ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
ImageRequest imageRequest = builder.build();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context);
dataSource.subscribe(new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
@Override
public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
if (!dataSource.isFinished()) {
return;
}
CloseableReference<CloseableImage> imageReference = dataSource.getResult();
if (imageReference != null) {
final CloseableReference<CloseableImage> closeableReference = imageReference.clone();
try {
CloseableImage closeableImage = closeableReference.get();
// 动图处理
if (closeableImage instanceof CloseableAnimatedImage) {
AnimatedImageResult animatedImageResult = ((CloseableAnimatedImage) closeableImage).getImageResult();
if (animatedImageResult != null && animatedImageResult.getImage() != null) {
int imageWidth = animatedImageResult.getImage().getWidth();
int imageHeight = animatedImageResult.getImage().getHeight();
Bitmap.Config bitmapConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = Bitmap.createBitmap(imageWidth, imageHeight, bitmapConfig);
animatedImageResult.getImage().getFrame(0).renderFrame(imageWidth, imageHeight, bitmap);
if (imageListener != null) {
imageListener.onSuccess(bitmap);
}
}
} else // 非动图处理
if (closeableImage instanceof CloseableBitmap) {
CloseableBitmap closeableBitmap = (CloseableBitmap) closeableImage;
Bitmap bitmap = closeableBitmap.getUnderlyingBitmap();
if (bitmap != null && !bitmap.isRecycled()) {
// https://github.com/facebook/fresco/issues/648
final Bitmap tempBitmap = bitmap.copy(bitmap.getConfig(), false);
if (imageListener != null) {
imageListener.onSuccess(tempBitmap);
}
}
}
} finally {
imageReference.close();
closeableReference.close();
}
}
}
@Override
public void onFailureImpl(DataSource dataSource) {
Throwable throwable = dataSource.getFailureCause();
if (imageListener != null) {
imageListener.onFail(throwable);
}
}
}, UiThreadImmediateExecutorService.getInstance());
}
Aggregations