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);
}
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);
}
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();
}
}
}
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);
}
}
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();
}
}
}
Aggregations