Search in sources :

Example 21 with CloseableReference

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);
}
Also used : MemoryTrimType(com.facebook.common.memory.MemoryTrimType) InOrder(org.mockito.InOrder) CloseableReference(com.facebook.common.references.CloseableReference) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with CloseableReference

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);
}
Also used : Consumer(com.facebook.imagepipeline.producers.Consumer) ProducerContext(com.facebook.imagepipeline.producers.ProducerContext) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CloseableReference(com.facebook.common.references.CloseableReference) Test(org.junit.Test)

Example 23 with CloseableReference

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);
}
Also used : Consumer(com.facebook.imagepipeline.producers.Consumer) ProducerContext(com.facebook.imagepipeline.producers.ProducerContext) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CloseableReference(com.facebook.common.references.CloseableReference) Test(org.junit.Test)

Example 24 with CloseableReference

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());
}
Also used : Bitmap(android.graphics.Bitmap) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CloseableReference(com.facebook.common.references.CloseableReference) LatLng(com.amap.api.maps.model.LatLng) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline) BaseBitmapDataSubscriber(com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber) DataSource(com.facebook.datasource.DataSource)

Example 25 with CloseableReference

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());
}
Also used : CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage) DiskCacheConfig(com.facebook.cache.disk.DiskCacheConfig) ImageConfig(com.ljy.devring.image.support.ImageConfig) ImagePipelineConfig(com.facebook.imagepipeline.core.ImagePipelineConfig) SimpleProgressiveJpegConfig(com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig) CloseableReference(com.facebook.common.references.CloseableReference) AnimatedImageResult(com.facebook.imagepipeline.animated.base.AnimatedImageResult) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) Uri(android.net.Uri) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) DataSource(com.facebook.datasource.DataSource) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) Bitmap(android.graphics.Bitmap) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline) ImageRequestBuilder(com.facebook.imagepipeline.request.ImageRequestBuilder)

Aggregations

CloseableReference (com.facebook.common.references.CloseableReference)50 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)24 Bitmap (android.graphics.Bitmap)17 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)16 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)14 DataSource (com.facebook.datasource.DataSource)9 BaseBitmapDataSubscriber (com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber)8 Uri (android.net.Uri)7 CloseableStaticBitmap (com.facebook.imagepipeline.image.CloseableStaticBitmap)7 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)5 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)5 ImageRequestBuilder (com.facebook.imagepipeline.request.ImageRequestBuilder)5 Nullable (javax.annotation.Nullable)5 Test (org.junit.Test)5 SuppressLint (android.annotation.SuppressLint)4 CacheKey (com.facebook.cache.common.CacheKey)3 MemoryTrimType (com.facebook.common.memory.MemoryTrimType)3 ResizeOptions (com.facebook.imagepipeline.common.ResizeOptions)3 CloseableBitmap (com.facebook.imagepipeline.image.CloseableBitmap)3 FakeClock (com.facebook.imagepipeline.testing.FakeClock)3