use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class LocalThumbnailBitmapProducer method produceResults.
@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext context) {
final ProducerListener2 listener = context.getProducerListener();
final ImageRequest imageRequest = context.getImageRequest();
context.putOriginExtra("local", "thumbnail_bitmap");
final CancellationSignal cancellationSignal = new CancellationSignal();
final StatefulProducerRunnable<CloseableReference<CloseableImage>> cancellableProducerRunnable = new StatefulProducerRunnable<CloseableReference<CloseableImage>>(consumer, listener, context, PRODUCER_NAME) {
@Override
protected void onSuccess(@Nullable CloseableReference<CloseableImage> result) {
super.onSuccess(result);
listener.onUltimateProducerReached(context, PRODUCER_NAME, result != null);
context.putOriginExtra("local");
}
@Override
protected void onFailure(Exception e) {
super.onFailure(e);
listener.onUltimateProducerReached(context, PRODUCER_NAME, false);
context.putOriginExtra("local");
}
@Override
@Nullable
protected CloseableReference<CloseableImage> getResult() throws IOException {
final Bitmap thumbnailBitmap = mContentResolver.loadThumbnail(imageRequest.getSourceUri(), new Size(imageRequest.getPreferredWidth(), imageRequest.getPreferredHeight()), cancellationSignal);
if (thumbnailBitmap == null) {
return null;
}
CloseableStaticBitmap closeableStaticBitmap = new CloseableStaticBitmap(thumbnailBitmap, SimpleBitmapReleaser.getInstance(), ImmutableQualityInfo.FULL_QUALITY, 0);
context.setExtra(ProducerContext.ExtraKeys.IMAGE_FORMAT, "thumbnail");
closeableStaticBitmap.setImageExtras(context.getExtras());
return CloseableReference.<CloseableImage>of(closeableStaticBitmap);
}
@Override
protected void onCancellation() {
super.onCancellation();
cancellationSignal.cancel();
}
@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);
}
};
context.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 BitmapMemoryCacheProducer method wrapConsumer.
protected Consumer<CloseableReference<CloseableImage>> wrapConsumer(final Consumer<CloseableReference<CloseableImage>> consumer, final CacheKey cacheKey, final boolean isBitmapCacheEnabledForWrite) {
return new DelegatingConsumer<CloseableReference<CloseableImage>, CloseableReference<CloseableImage>>(consumer) {
@Override
public void onNewResultImpl(@Nullable CloseableReference<CloseableImage> newResult, @Status int status) {
try {
if (FrescoSystrace.isTracing()) {
FrescoSystrace.beginSection("BitmapMemoryCacheProducer#onNewResultImpl");
}
final boolean isLast = isLast(status);
// ignore invalid intermediate results and forward the null result if last
if (newResult == null) {
if (isLast) {
getConsumer().onNewResult(null, status);
}
return;
}
// stateful and partial results cannot be cached and are just forwarded
if (newResult.get().isStateful() || statusHasFlag(status, IS_PARTIAL_RESULT)) {
getConsumer().onNewResult(newResult, status);
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, status);
return;
}
} finally {
CloseableReference.closeSafely(currentCachedResult);
}
}
}
// cache, if needed, and forward the new result
CloseableReference<CloseableImage> newCachedResult = null;
if (isBitmapCacheEnabledForWrite) {
newCachedResult = mMemoryCache.cache(cacheKey, newResult);
}
try {
if (isLast) {
getConsumer().onProgressUpdate(1f);
}
getConsumer().onNewResult((newCachedResult != null) ? newCachedResult : newResult, status);
} finally {
CloseableReference.closeSafely(newCachedResult);
}
} finally {
if (FrescoSystrace.isTracing()) {
FrescoSystrace.endSection();
}
}
}
};
}
use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class PostprocessorProducer method produceResults.
@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, ProducerContext context) {
final ProducerListener2 listener = context.getProducerListener();
final Postprocessor postprocessor = context.getImageRequest().getPostprocessor();
Preconditions.checkNotNull(postprocessor);
final PostprocessorConsumer basePostprocessorConsumer = new PostprocessorConsumer(consumer, listener, postprocessor, context);
final Consumer<CloseableReference<CloseableImage>> postprocessorConsumer;
if (postprocessor instanceof RepeatedPostprocessor) {
postprocessorConsumer = new RepeatedPostprocessorConsumer(basePostprocessorConsumer, (RepeatedPostprocessor) postprocessor, context);
} else {
postprocessorConsumer = new SingleUsePostprocessorConsumer(basePostprocessorConsumer);
}
mInputProducer.produceResults(postprocessorConsumer, context);
}
use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class FrescoController2Impl method fetch.
@Override
public boolean fetch(final FrescoDrawableInterface drawable, final VitoImageRequest imageRequest, @Nullable final Object callerContext, @Nullable final ContextChain contextChain, @Nullable final ImageListener listener, @Nullable final OnFadeListener onFadeListener, @Nullable final Rect viewportDimensions) {
if (!(drawable instanceof FrescoDrawable2Impl)) {
throw new IllegalArgumentException("Drawable not supported " + drawable);
}
final FrescoDrawable2Impl frescoDrawable = (FrescoDrawable2Impl) drawable;
// Save viewport dimension for future use
frescoDrawable.setViewportDimensions(viewportDimensions);
// Check if we already fetched the image
if (frescoDrawable.getDrawableDataSubscriber() == this && frescoDrawable.isFetchSubmitted() && imageRequest.equals(frescoDrawable.getImageRequest())) {
frescoDrawable.cancelReleaseNextFrame();
frescoDrawable.cancelReleaseDelayed();
// already set
return true;
}
if (frescoDrawable.isFetchSubmitted()) {
frescoDrawable.getImagePerfListener().onDrawableReconfigured(frescoDrawable);
}
// We didn't -> Reset everything
frescoDrawable.close();
// Basic setup
frescoDrawable.setDrawableDataSubscriber(this);
frescoDrawable.setImageRequest(imageRequest);
frescoDrawable.setCallerContext(callerContext);
frescoDrawable.setImageListener(listener);
frescoDrawable.setVitoImageRequestListener(mGlobalImageListener);
frescoDrawable.setOnFadeListener(onFadeListener);
// Set layers that are always visible
frescoDrawable.setOverlayDrawable(mHierarcher.buildOverlayDrawable(imageRequest.resources, imageRequest.imageOptions));
// We're fetching a new image, so we're updating the ID
final long imageId = VitoUtils.generateIdentifier();
frescoDrawable.setImageId(imageId);
Extras extras = obtainExtras(null, null, frescoDrawable);
// Notify listeners that we're about to fetch an image
frescoDrawable.getInternalListener().onSubmit(imageId, imageRequest, callerContext, extras);
frescoDrawable.getImagePerfListener().onImageFetch(frescoDrawable);
// Check if the image is in cache
CloseableReference<CloseableImage> cachedImage = mImagePipeline.getCachedImage(imageRequest);
try {
if (CloseableReference.isValid(cachedImage)) {
frescoDrawable.setImageOrigin(ImageOrigin.MEMORY_BITMAP_SHORTCUT);
// Immediately display the actual image.
setActualImage(frescoDrawable, imageRequest, cachedImage, true, null);
frescoDrawable.setFetchSubmitted(true);
mDebugOverlayFactory.update(frescoDrawable, extras);
return true;
}
} finally {
CloseableReference.closeSafely(cachedImage);
}
// The image is not in cache -> Set up layers visible until the image is available
frescoDrawable.setProgressDrawable(mHierarcher.buildProgressDrawable(imageRequest.resources, imageRequest.imageOptions));
// Immediately show the progress image and set progress to 0
frescoDrawable.setProgress(0f);
frescoDrawable.showProgressImmediately();
Drawable placeholder = mHierarcher.buildPlaceholderDrawable(imageRequest.resources, imageRequest.imageOptions);
frescoDrawable.setPlaceholderDrawable(placeholder);
frescoDrawable.setImageDrawable(null);
frescoDrawable.getInternalListener().onPlaceholderSet(imageId, imageRequest, placeholder);
// Fetch the image
final Runnable fetchRunnable = new Runnable() {
@Override
public void run() {
if (imageId != frescoDrawable.getImageId()) {
// We're trying to load a different image -> ignore
return;
}
DataSource<CloseableReference<CloseableImage>> dataSource = mImagePipeline.fetchDecodedImage(imageRequest, callerContext, frescoDrawable.getImageOriginListener(), imageId);
frescoDrawable.setDataSource(imageId, dataSource);
dataSource.subscribe(frescoDrawable, mUiThreadExecutor);
}
};
if (mConfig.submitFetchOnBgThread()) {
mLightweightBackgroundThreadExecutor.execute(fetchRunnable);
} else {
fetchRunnable.run();
}
frescoDrawable.setFetchSubmitted(true);
mDebugOverlayFactory.update(frescoDrawable, null);
return false;
}
use of com.facebook.common.references.CloseableReference in project fresco by facebook.
the class RetainingDataSourceSupplierFragment method onViewCreated.
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
final SimpleDraweeView simpleDraweeView = view.findViewById(R.id.drawee_view);
final RetainingDataSourceSupplier<CloseableReference<CloseableImage>> retainingSupplier = new RetainingDataSourceSupplier<>();
simpleDraweeView.setController(Fresco.newDraweeControllerBuilder().setDataSourceSupplier(retainingSupplier).setControllerListener(controllerListener).build());
replaceImage(retainingSupplier);
simpleDraweeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceImage(retainingSupplier);
}
});
}
Aggregations