Search in sources :

Example 21 with CloseableImage

use of com.facebook.imagepipeline.image.CloseableImage in project fresco by facebook.

the class BitmapMemoryCacheProducer method produceResults.

@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext producerContext) {
    try {
        if (FrescoSystrace.isTracing()) {
            FrescoSystrace.beginSection("BitmapMemoryCacheProducer#produceResults");
        }
        final ProducerListener2 listener = producerContext.getProducerListener();
        listener.onProducerStart(producerContext, getProducerName());
        final ImageRequest imageRequest = producerContext.getImageRequest();
        final Object callerContext = producerContext.getCallerContext();
        final CacheKey cacheKey = mCacheKeyFactory.getBitmapCacheKey(imageRequest, callerContext);
        final boolean isBitmapCacheEnabledForRead = producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.BITMAP_READ);
        CloseableReference<CloseableImage> cachedReference = isBitmapCacheEnabledForRead ? mMemoryCache.get(cacheKey) : null;
        if (cachedReference != null) {
            maybeSetExtrasFromCloseableImage(cachedReference.get(), producerContext);
            boolean isFinal = cachedReference.get().getQualityInfo().isOfFullQuality();
            if (isFinal) {
                listener.onProducerFinishWithSuccess(producerContext, getProducerName(), listener.requiresExtraMap(producerContext, getProducerName()) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "true") : null);
                listener.onUltimateProducerReached(producerContext, getProducerName(), true);
                producerContext.putOriginExtra("memory_bitmap", getOriginSubcategory());
                consumer.onProgressUpdate(1f);
            }
            consumer.onNewResult(cachedReference, BaseConsumer.simpleStatusForIsLast(isFinal));
            cachedReference.close();
            if (isFinal) {
                return;
            }
        }
        if (producerContext.getLowestPermittedRequestLevel().getValue() >= ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE.getValue()) {
            listener.onProducerFinishWithSuccess(producerContext, getProducerName(), listener.requiresExtraMap(producerContext, getProducerName()) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
            listener.onUltimateProducerReached(producerContext, getProducerName(), false);
            producerContext.putOriginExtra("memory_bitmap", getOriginSubcategory());
            consumer.onNewResult(null, Consumer.IS_LAST);
            return;
        }
        Consumer<CloseableReference<CloseableImage>> wrappedConsumer = wrapConsumer(consumer, cacheKey, producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.BITMAP_WRITE));
        listener.onProducerFinishWithSuccess(producerContext, getProducerName(), listener.requiresExtraMap(producerContext, getProducerName()) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
        if (FrescoSystrace.isTracing()) {
            FrescoSystrace.beginSection("mInputProducer.produceResult");
        }
        mInputProducer.produceResults(wrappedConsumer, producerContext);
        if (FrescoSystrace.isTracing()) {
            FrescoSystrace.endSection();
        }
    } finally {
        if (FrescoSystrace.isTracing()) {
            FrescoSystrace.endSection();
        }
    }
}
Also used : ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CloseableReference(com.facebook.common.references.CloseableReference) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) CacheKey(com.facebook.cache.common.CacheKey)

Example 22 with CloseableImage

use of com.facebook.imagepipeline.image.CloseableImage in project fresco by facebook.

the class PipelineDraweeController method createDrawable.

@Override
protected Drawable createDrawable(CloseableReference<CloseableImage> image) {
    try {
        if (FrescoSystrace.isTracing()) {
            FrescoSystrace.beginSection("PipelineDraweeController#createDrawable");
        }
        Preconditions.checkState(CloseableReference.isValid(image));
        CloseableImage closeableImage = image.get();
        maybeUpdateDebugOverlay(closeableImage);
        Drawable drawable = maybeCreateDrawableFromFactories(mCustomDrawableFactories, closeableImage);
        if (drawable != null) {
            return drawable;
        }
        drawable = maybeCreateDrawableFromFactories(mGlobalDrawableFactories, closeableImage);
        if (drawable != null) {
            return drawable;
        }
        drawable = mDefaultDrawableFactory.createDrawable(closeableImage);
        if (drawable != null) {
            return drawable;
        }
        throw new UnsupportedOperationException("Unrecognized image class: " + closeableImage);
    } finally {
        if (FrescoSystrace.isTracing()) {
            FrescoSystrace.endSection();
        }
    }
}
Also used : Drawable(android.graphics.drawable.Drawable) ScaleTypeDrawable(com.facebook.drawee.drawable.ScaleTypeDrawable) DebugControllerOverlayDrawable(com.facebook.drawee.debug.DebugControllerOverlayDrawable) CloseableImage(com.facebook.imagepipeline.image.CloseableImage)

Example 23 with CloseableImage

use of com.facebook.imagepipeline.image.CloseableImage in project fresco by facebook.

the class ImagePipeline method isInBitmapMemoryCache.

/**
 * Returns whether the image is stored in the bitmap memory cache.
 *
 * @param imageRequest the imageRequest for the image to be looked up.
 * @return true if the image was found in the bitmap memory cache, false otherwise.
 */
public boolean isInBitmapMemoryCache(final ImageRequest imageRequest) {
    if (imageRequest == null) {
        return false;
    }
    final CacheKey cacheKey = mCacheKeyFactory.getBitmapCacheKey(imageRequest, null);
    CloseableReference<CloseableImage> ref = mBitmapMemoryCache.get(cacheKey);
    try {
        return CloseableReference.isValid(ref);
    } finally {
        CloseableReference.closeSafely(ref);
    }
}
Also used : CloseableImage(com.facebook.imagepipeline.image.CloseableImage) CacheKey(com.facebook.cache.common.CacheKey)

Example 24 with CloseableImage

use of com.facebook.imagepipeline.image.CloseableImage in project fresco by facebook.

the class FrescoDrawable2ImplTest method setup.

@Before
public void setup() {
    mFrescoDrawable = new FrescoDrawable2Impl(false, null, new BaseVitoImagePerfListener());
    mLatch = new CountDownLatch(1);
    mCloseableImage = new DummyCloseableImage();
    mCloseableReference = CloseableReference.of(mCloseableImage, new ResourceReleaser<CloseableImage>() {

        @Override
        public void release(CloseableImage value) {
            value.close();
            mLatch.countDown();
        }
    });
}
Also used : ResourceReleaser(com.facebook.common.references.ResourceReleaser) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) CountDownLatch(java.util.concurrent.CountDownLatch) Before(org.junit.Before)

Example 25 with CloseableImage

use of com.facebook.imagepipeline.image.CloseableImage in project fresco by facebook.

the class BitmapDrawableFactoryTest method testCreateDrawable_whenImageUnknown_thenReturnNull.

@Test
public void testCreateDrawable_whenImageUnknown_thenReturnNull() {
    final CloseableImage closeableImage = mock(CloseableImage.class);
    final ImageOptions options = mock(ImageOptions.class);
    Drawable drawable = mDrawableFactory.createDrawable(closeableImage, options);
    assertThat(drawable).isNull();
}
Also used : BitmapDrawable(android.graphics.drawable.BitmapDrawable) Drawable(android.graphics.drawable.Drawable) RoundedBitmapDrawable(com.facebook.drawee.drawable.RoundedBitmapDrawable) ImageOptions(com.facebook.fresco.vito.options.ImageOptions) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) Test(org.junit.Test)

Aggregations

CloseableImage (com.facebook.imagepipeline.image.CloseableImage)29 CloseableReference (com.facebook.common.references.CloseableReference)14 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)8 Bitmap (android.graphics.Bitmap)7 CacheKey (com.facebook.cache.common.CacheKey)7 CloseableBitmap (com.facebook.imagepipeline.image.CloseableBitmap)5 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)4 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)4 Nullable (javax.annotation.Nullable)4 Drawable (android.graphics.drawable.Drawable)3 Uri (android.net.Uri)3 DataSource (com.facebook.datasource.DataSource)3 CloseableStaticBitmap (com.facebook.imagepipeline.image.CloseableStaticBitmap)3 ImageRequestBuilder (com.facebook.imagepipeline.request.ImageRequestBuilder)3 IOException (java.io.IOException)3 Before (org.junit.Before)3 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)2 DiskCacheConfig (com.facebook.cache.disk.DiskCacheConfig)2 ScaleTypeDrawable (com.facebook.drawee.drawable.ScaleTypeDrawable)2 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)2