Search in sources :

Example 66 with ImageRequest

use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.

the class PartialDiskCacheProducer method produceResults.

public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
    final ImageRequest imageRequest = producerContext.getImageRequest();
    final boolean isDiskCacheEnabledForRead = producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.DISK_READ);
    final boolean isDiskCacheEnabledForWrite = producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.DISK_WRITE);
    if (!isDiskCacheEnabledForRead && !isDiskCacheEnabledForWrite) {
        mInputProducer.produceResults(consumer, producerContext);
        return;
    }
    final ProducerListener2 listener = producerContext.getProducerListener();
    listener.onProducerStart(producerContext, PRODUCER_NAME);
    final Uri uriForPartialCacheKey = createUriForPartialCacheKey(imageRequest);
    final CacheKey partialImageCacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, uriForPartialCacheKey, producerContext.getCallerContext());
    if (!isDiskCacheEnabledForRead) {
        listener.onProducerFinishWithSuccess(producerContext, PRODUCER_NAME, getExtraMap(listener, producerContext, false, 0));
        startInputProducer(consumer, producerContext, partialImageCacheKey, null);
        return;
    }
    final AtomicBoolean isCancelled = new AtomicBoolean(false);
    final Task<EncodedImage> diskLookupTask = mDefaultBufferedDiskCache.get(partialImageCacheKey, isCancelled);
    final Continuation<EncodedImage, Void> continuation = onFinishDiskReads(consumer, producerContext, partialImageCacheKey);
    diskLookupTask.continueWith(continuation);
    subscribeTaskForRequestCancellation(isCancelled, producerContext);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) Uri(android.net.Uri) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CacheKey(com.facebook.cache.common.CacheKey)

Example 67 with ImageRequest

use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.

the class LocalExifThumbnailProducer method produceResults.

@Override
public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
    final ProducerListener2 listener = producerContext.getProducerListener();
    final ImageRequest imageRequest = producerContext.getImageRequest();
    producerContext.putOriginExtra("local", "exif");
    final StatefulProducerRunnable cancellableProducerRunnable = new StatefulProducerRunnable<EncodedImage>(consumer, listener, producerContext, PRODUCER_NAME) {

        @Override
        @Nullable
        protected EncodedImage getResult() throws Exception {
            final Uri sourceUri = imageRequest.getSourceUri();
            final ExifInterface exifInterface = getExifInterface(sourceUri);
            if (exifInterface == null || !exifInterface.hasThumbnail()) {
                return null;
            }
            byte[] bytes = Preconditions.checkNotNull(exifInterface.getThumbnail());
            PooledByteBuffer pooledByteBuffer = mPooledByteBufferFactory.newByteBuffer(bytes);
            return buildEncodedImage(pooledByteBuffer, exifInterface);
        }

        @Override
        protected void disposeResult(@Nullable EncodedImage result) {
            EncodedImage.closeSafely(result);
        }

        @Override
        protected Map<String, String> getExtraMapOnSuccess(@Nullable final EncodedImage result) {
            return ImmutableMap.of(CREATED_THUMBNAIL, Boolean.toString(result != null));
        }
    };
    producerContext.addCallbacks(new BaseProducerContextCallbacks() {

        @Override
        public void onCancellationRequested() {
            cancellableProducerRunnable.cancel();
        }
    });
    mExecutor.execute(cancellableProducerRunnable);
}
Also used : ImageRequest(com.facebook.imagepipeline.request.ImageRequest) ExifInterface(android.media.ExifInterface) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) Uri(android.net.Uri) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) Nullable(javax.annotation.Nullable)

Example 68 with ImageRequest

use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.

the class EncodedMemoryCacheProducer method produceResults.

@Override
public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
    try {
        if (FrescoSystrace.isTracing()) {
            FrescoSystrace.beginSection("EncodedMemoryCacheProducer#produceResults");
        }
        final ProducerListener2 listener = producerContext.getProducerListener();
        listener.onProducerStart(producerContext, PRODUCER_NAME);
        final ImageRequest imageRequest = producerContext.getImageRequest();
        final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, producerContext.getCallerContext());
        final boolean isEncodedCacheEnabledForRead = producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.ENCODED_READ);
        CloseableReference<PooledByteBuffer> cachedReference = isEncodedCacheEnabledForRead ? mMemoryCache.get(cacheKey) : null;
        try {
            if (cachedReference != null) {
                EncodedImage cachedEncodedImage = new EncodedImage(cachedReference);
                try {
                    listener.onProducerFinishWithSuccess(producerContext, PRODUCER_NAME, listener.requiresExtraMap(producerContext, PRODUCER_NAME) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "true") : null);
                    listener.onUltimateProducerReached(producerContext, PRODUCER_NAME, true);
                    producerContext.putOriginExtra("memory_encoded");
                    consumer.onProgressUpdate(1f);
                    consumer.onNewResult(cachedEncodedImage, Consumer.IS_LAST);
                    return;
                } finally {
                    EncodedImage.closeSafely(cachedEncodedImage);
                }
            }
            if (producerContext.getLowestPermittedRequestLevel().getValue() >= ImageRequest.RequestLevel.ENCODED_MEMORY_CACHE.getValue()) {
                listener.onProducerFinishWithSuccess(producerContext, PRODUCER_NAME, listener.requiresExtraMap(producerContext, PRODUCER_NAME) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
                listener.onUltimateProducerReached(producerContext, PRODUCER_NAME, false);
                producerContext.putOriginExtra("memory_encoded", "nil-result");
                consumer.onNewResult(null, Consumer.IS_LAST);
                return;
            }
            Consumer consumerOfInputProducer = new EncodedMemoryCacheConsumer(consumer, mMemoryCache, cacheKey, producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.ENCODED_WRITE), producerContext.getImagePipelineConfig().getExperiments().isEncodedCacheEnabled());
            listener.onProducerFinishWithSuccess(producerContext, PRODUCER_NAME, listener.requiresExtraMap(producerContext, PRODUCER_NAME) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
            mInputProducer.produceResults(consumerOfInputProducer, producerContext);
        } finally {
            CloseableReference.closeSafely(cachedReference);
        }
    } finally {
        if (FrescoSystrace.isTracing()) {
            FrescoSystrace.endSection();
        }
    }
}
Also used : ImageRequest(com.facebook.imagepipeline.request.ImageRequest) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CacheKey(com.facebook.cache.common.CacheKey)

Example 69 with ImageRequest

use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.

the class PostprocessedBitmapMemoryCacheProducer method produceResults.

@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext producerContext) {
    final ProducerListener2 listener = producerContext.getProducerListener();
    final ImageRequest imageRequest = producerContext.getImageRequest();
    final Object callerContext = producerContext.getCallerContext();
    // If there's no postprocessor or the postprocessor doesn't require caching, forward results.
    final Postprocessor postprocessor = imageRequest.getPostprocessor();
    if (postprocessor == null || postprocessor.getPostprocessorCacheKey() == null) {
        mInputProducer.produceResults(consumer, producerContext);
        return;
    }
    listener.onProducerStart(producerContext, getProducerName());
    final CacheKey cacheKey = mCacheKeyFactory.getPostprocessedBitmapCacheKey(imageRequest, callerContext);
    final boolean isBitmapCacheEnabledForRead = producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.BITMAP_READ);
    CloseableReference<CloseableImage> cachedReference = isBitmapCacheEnabledForRead ? mMemoryCache.get(cacheKey) : null;
    if (cachedReference != null) {
        listener.onProducerFinishWithSuccess(producerContext, getProducerName(), listener.requiresExtraMap(producerContext, getProducerName()) ? ImmutableMap.of(VALUE_FOUND, "true") : null);
        listener.onUltimateProducerReached(producerContext, PRODUCER_NAME, true);
        producerContext.putOriginExtra("memory_bitmap", "postprocessed");
        consumer.onProgressUpdate(1.0f);
        consumer.onNewResult(cachedReference, Consumer.IS_LAST);
        cachedReference.close();
    } else {
        final boolean isRepeatedProcessor = postprocessor instanceof RepeatedPostprocessor;
        final boolean isBitmapCacheEnabledForWrite = producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.BITMAP_WRITE);
        Consumer<CloseableReference<CloseableImage>> cachedConsumer = new CachedPostprocessorConsumer(consumer, cacheKey, isRepeatedProcessor, mMemoryCache, isBitmapCacheEnabledForWrite);
        listener.onProducerFinishWithSuccess(producerContext, getProducerName(), listener.requiresExtraMap(producerContext, getProducerName()) ? ImmutableMap.of(VALUE_FOUND, "false") : null);
        mInputProducer.produceResults(cachedConsumer, producerContext);
    }
}
Also used : ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CloseableReference(com.facebook.common.references.CloseableReference) Postprocessor(com.facebook.imagepipeline.request.Postprocessor) RepeatedPostprocessor(com.facebook.imagepipeline.request.RepeatedPostprocessor) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) CacheKey(com.facebook.cache.common.CacheKey) RepeatedPostprocessor(com.facebook.imagepipeline.request.RepeatedPostprocessor)

Example 70 with ImageRequest

use of com.facebook.imagepipeline.request.ImageRequest 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)

Aggregations

ImageRequest (com.facebook.imagepipeline.request.ImageRequest)100 ResizeOptions (com.facebook.imagepipeline.common.ResizeOptions)26 DraweeController (com.facebook.drawee.interfaces.DraweeController)25 CloseableReference (com.facebook.common.references.CloseableReference)24 Uri (android.net.Uri)18 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)17 Test (org.junit.Test)14 Bitmap (android.graphics.Bitmap)13 CacheKey (com.facebook.cache.common.CacheKey)12 File (java.io.File)11 DataSource (com.facebook.datasource.DataSource)10 BaseBitmapDataSubscriber (com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber)9 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)8 ImageRequestBuilder (com.facebook.imagepipeline.request.ImageRequestBuilder)8 Animatable (android.graphics.drawable.Animatable)7 PipelineDraweeController (com.facebook.drawee.backends.pipeline.PipelineDraweeController)7 BaseControllerListener (com.facebook.drawee.controller.BaseControllerListener)7 ImageOptions (com.facebook.fresco.vito.options.ImageOptions)7 ImageInfo (com.facebook.imagepipeline.image.ImageInfo)7 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)6