use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class LocalVideoThumbnailProducer method produceResults.
@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext producerContext) {
final ProducerListener2 listener = producerContext.getProducerListener();
final ImageRequest imageRequest = producerContext.getImageRequest();
producerContext.putOriginExtra("local", "video");
final StatefulProducerRunnable cancellableProducerRunnable = new StatefulProducerRunnable<CloseableReference<CloseableImage>>(consumer, listener, producerContext, PRODUCER_NAME) {
@Override
protected void onSuccess(@Nullable CloseableReference<CloseableImage> result) {
super.onSuccess(result);
listener.onUltimateProducerReached(producerContext, PRODUCER_NAME, result != null);
producerContext.putOriginExtra("local");
}
@Override
protected void onFailure(Exception e) {
super.onFailure(e);
listener.onUltimateProducerReached(producerContext, PRODUCER_NAME, false);
producerContext.putOriginExtra("local");
}
@Override
@Nullable
protected CloseableReference<CloseableImage> getResult() throws Exception {
Bitmap thumbnailBitmap;
String path;
try {
path = getLocalFilePath(imageRequest);
} catch (IllegalArgumentException e) {
path = null;
}
thumbnailBitmap = path != null ? ThumbnailUtils.createVideoThumbnail(path, calculateKind(imageRequest)) : createThumbnailFromContentProvider(mContentResolver, imageRequest.getSourceUri());
if (thumbnailBitmap == null) {
return null;
}
CloseableStaticBitmap closeableStaticBitmap = new CloseableStaticBitmap(thumbnailBitmap, SimpleBitmapReleaser.getInstance(), ImmutableQualityInfo.FULL_QUALITY, 0);
producerContext.setExtra(ProducerContext.ExtraKeys.IMAGE_FORMAT, "thumbnail");
closeableStaticBitmap.setImageExtras(producerContext.getExtras());
return CloseableReference.<CloseableImage>of(closeableStaticBitmap);
}
@Override
protected Map<String, String> getExtraMapOnSuccess(@Nullable final CloseableReference<CloseableImage> result) {
return ImmutableMap.of(CREATED_THUMBNAIL, String.valueOf(result != null));
}
@Override
protected void disposeResult(CloseableReference<CloseableImage> result) {
CloseableReference.closeSafely(result);
}
};
producerContext.addCallbacks(new BaseProducerContextCallbacks() {
@Override
public void onCancellationRequested() {
cancellableProducerRunnable.cancel();
}
});
mExecutor.execute(cancellableProducerRunnable);
}
use of com.facebook.common.references.CloseableReference 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.common.references.CloseableReference 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();
}
}
}
use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class ProducerSequenceFactory method newBitmapCacheGetToBitmapCacheSequence.
/**
* Bitmap cache get -> thread hand off -> multiplex -> bitmap cache
*
* @param inputProducer producer providing the input to the bitmap cache
* @return bitmap cache get to bitmap cache sequence
*/
private Producer<CloseableReference<CloseableImage>> newBitmapCacheGetToBitmapCacheSequence(Producer<CloseableReference<CloseableImage>> inputProducer) {
BitmapMemoryCacheProducer bitmapMemoryCacheProducer = mProducerFactory.newBitmapMemoryCacheProducer(inputProducer);
BitmapMemoryCacheKeyMultiplexProducer bitmapKeyMultiplexProducer = mProducerFactory.newBitmapMemoryCacheKeyMultiplexProducer(bitmapMemoryCacheProducer);
Producer<CloseableReference<CloseableImage>> threadHandoffProducer = mProducerFactory.newBackgroundThreadHandoffProducer(bitmapKeyMultiplexProducer, mThreadHandoffProducerQueue);
if (mIsEncodedMemoryCacheProbingEnabled || mIsDiskCacheProbingEnabled) {
BitmapMemoryCacheGetProducer bitmapMemoryCacheGetProducer = mProducerFactory.newBitmapMemoryCacheGetProducer(threadHandoffProducer);
return mProducerFactory.newBitmapProbeProducer(bitmapMemoryCacheGetProducer);
}
return mProducerFactory.newBitmapMemoryCacheGetProducer(threadHandoffProducer);
}
use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class ProducerSequenceFactory method newBitmapCacheGetToDecodeSequence.
/**
* Same as {@code newBitmapCacheGetToBitmapCacheSequence} but with an extra DecodeProducer.
*
* @param inputProducer producer providing the input to the decode
* @return bitmap cache get to decode sequence
*/
private Producer<CloseableReference<CloseableImage>> newBitmapCacheGetToDecodeSequence(Producer<EncodedImage> inputProducer) {
if (FrescoSystrace.isTracing()) {
FrescoSystrace.beginSection("ProducerSequenceFactory#newBitmapCacheGetToDecodeSequence");
}
DecodeProducer decodeProducer = mProducerFactory.newDecodeProducer(inputProducer);
Producer<CloseableReference<CloseableImage>> result = newBitmapCacheGetToBitmapCacheSequence(decodeProducer);
if (FrescoSystrace.isTracing()) {
FrescoSystrace.endSection();
}
return result;
}
Aggregations