use of com.facebook.imagepipeline.image.CloseableImage in project fresco by facebook.
the class LocalVideoThumbnailProducer method produceResults.
@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext producerContext) {
final ProducerListener listener = producerContext.getListener();
final String requestId = producerContext.getId();
final ImageRequest imageRequest = producerContext.getImageRequest();
final StatefulProducerRunnable cancellableProducerRunnable = new StatefulProducerRunnable<CloseableReference<CloseableImage>>(consumer, listener, PRODUCER_NAME, requestId) {
@Override
protected CloseableReference<CloseableImage> getResult() throws Exception {
Bitmap thumbnailBitmap = ThumbnailUtils.createVideoThumbnail(imageRequest.getSourceFile().getPath(), calculateKind(imageRequest));
if (thumbnailBitmap == null) {
return null;
}
return CloseableReference.<CloseableImage>of(new CloseableStaticBitmap(thumbnailBitmap, SimpleBitmapReleaser.getInstance(), ImmutableQualityInfo.FULL_QUALITY, 0));
}
@Override
protected Map<String, String> getExtraMapOnSuccess(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.imagepipeline.image.CloseableImage in project fresco by facebook.
the class PostprocessedBitmapMemoryCacheProducer method produceResults.
@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext producerContext) {
final ProducerListener listener = producerContext.getListener();
final String requestId = producerContext.getId();
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(requestId, getProducerName());
final CacheKey cacheKey = mCacheKeyFactory.getPostprocessedBitmapCacheKey(imageRequest, callerContext);
CloseableReference<CloseableImage> cachedReference = mMemoryCache.get(cacheKey);
if (cachedReference != null) {
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(VALUE_FOUND, "true") : null);
consumer.onProgressUpdate(1.0f);
consumer.onNewResult(cachedReference, true);
cachedReference.close();
} else {
final boolean isRepeatedProcessor = postprocessor instanceof RepeatedPostprocessor;
Consumer<CloseableReference<CloseableImage>> cachedConsumer = new CachedPostprocessorConsumer(consumer, cacheKey, isRepeatedProcessor, mMemoryCache);
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(VALUE_FOUND, "false") : null);
mInputProducer.produceResults(cachedConsumer, producerContext);
}
}
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) {
final ProducerListener listener = producerContext.getListener();
final String requestId = producerContext.getId();
listener.onProducerStart(requestId, getProducerName());
final ImageRequest imageRequest = producerContext.getImageRequest();
final Object callerContext = producerContext.getCallerContext();
final CacheKey cacheKey = mCacheKeyFactory.getBitmapCacheKey(imageRequest, callerContext);
CloseableReference<CloseableImage> cachedReference = mMemoryCache.get(cacheKey);
if (cachedReference != null) {
boolean isFinal = cachedReference.get().getQualityInfo().isOfFullQuality();
if (isFinal) {
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "true") : null);
consumer.onProgressUpdate(1f);
}
consumer.onNewResult(cachedReference, isFinal);
cachedReference.close();
if (isFinal) {
return;
}
}
if (producerContext.getLowestPermittedRequestLevel().getValue() >= ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE.getValue()) {
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
consumer.onNewResult(null, true);
return;
}
Consumer<CloseableReference<CloseableImage>> wrappedConsumer = wrapConsumer(consumer, cacheKey);
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
mInputProducer.produceResults(wrappedConsumer, producerContext);
}
use of com.facebook.imagepipeline.image.CloseableImage in project fresco by facebook.
the class BitmapMemoryCacheProducer method wrapConsumer.
protected Consumer<CloseableReference<CloseableImage>> wrapConsumer(final Consumer<CloseableReference<CloseableImage>> consumer, final CacheKey cacheKey) {
return new DelegatingConsumer<CloseableReference<CloseableImage>, CloseableReference<CloseableImage>>(consumer) {
@Override
public void onNewResultImpl(CloseableReference<CloseableImage> newResult, boolean isLast) {
// ignore invalid intermediate results and forward the null result if last
if (newResult == null) {
if (isLast) {
getConsumer().onNewResult(null, true);
}
return;
}
// stateful results cannot be cached and are just forwarded
if (newResult.get().isStateful()) {
getConsumer().onNewResult(newResult, isLast);
return;
}
// forward the already cached result and don't cache the new result.
if (!isLast) {
CloseableReference<CloseableImage> currentCachedResult = mMemoryCache.get(cacheKey);
if (currentCachedResult != null) {
try {
QualityInfo newInfo = newResult.get().getQualityInfo();
QualityInfo cachedInfo = currentCachedResult.get().getQualityInfo();
if (cachedInfo.isOfFullQuality() || cachedInfo.getQuality() >= newInfo.getQuality()) {
getConsumer().onNewResult(currentCachedResult, false);
return;
}
} finally {
CloseableReference.closeSafely(currentCachedResult);
}
}
}
// cache and forward the new result
CloseableReference<CloseableImage> newCachedResult = mMemoryCache.cache(cacheKey, newResult);
try {
if (isLast) {
getConsumer().onProgressUpdate(1f);
}
getConsumer().onNewResult((newCachedResult != null) ? newCachedResult : newResult, isLast);
} finally {
CloseableReference.closeSafely(newCachedResult);
}
}
};
}
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);
}
}
Aggregations