use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class BufferedDiskCache method put.
/**
* Associates encodedImage with given key in disk cache. Disk write is performed on background
* thread, so the caller of this method is not blocked
*/
public void put(final CacheKey key, EncodedImage encodedImage) {
try {
if (FrescoSystrace.isTracing()) {
FrescoSystrace.beginSection("BufferedDiskCache#put");
}
Preconditions.checkNotNull(key);
Preconditions.checkArgument(EncodedImage.isValid(encodedImage));
// Store encodedImage in staging area
mStagingArea.put(key, encodedImage);
// Write to disk cache. This will be executed on background thread, so increment the ref
// count. When this write completes (with success/failure), then we will bump down the
// ref count again.
final EncodedImage finalEncodedImage = EncodedImage.cloneOrNull(encodedImage);
try {
final Object token = FrescoInstrumenter.onBeforeSubmitWork("BufferedDiskCache_putAsync");
mWriteExecutor.execute(new Runnable() {
@Override
public void run() {
final Object currentToken = FrescoInstrumenter.onBeginWork(token, null);
try {
// NULLSAFE_FIXME[Parameter Not Nullable]
writeToDiskCache(key, finalEncodedImage);
} catch (Throwable th) {
FrescoInstrumenter.markFailure(token, th);
throw th;
} finally {
// NULLSAFE_FIXME[Parameter Not Nullable]
mStagingArea.remove(key, finalEncodedImage);
EncodedImage.closeSafely(finalEncodedImage);
FrescoInstrumenter.onEndWork(currentToken);
}
}
});
} catch (Exception exception) {
// We failed to enqueue cache write. Log failure and decrement ref count
// TODO: 3697790
FLog.w(TAG, exception, "Failed to schedule disk-cache write for %s", key.getUriString());
mStagingArea.remove(key, encodedImage);
EncodedImage.closeSafely(finalEncodedImage);
}
} finally {
if (FrescoSystrace.isTracing()) {
FrescoSystrace.endSection();
}
}
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class BufferedDiskCache method checkInStagingAreaAndFileCache.
/**
* Performs key-value loop up in staging area and file cache. Any error manifests itself as a
* miss, i.e. returns false.
*
* @param key
* @return true if the image is found in staging area or File cache, false if not found
*/
private boolean checkInStagingAreaAndFileCache(final CacheKey key) {
EncodedImage result = mStagingArea.get(key);
if (result != null) {
result.close();
FLog.v(TAG, "Found image for %s in staging area", key.getUriString());
mImageCacheStatsTracker.onStagingAreaHit(key);
return true;
} else {
FLog.v(TAG, "Did not find image for %s in staging area", key.getUriString());
mImageCacheStatsTracker.onStagingAreaMiss(key);
try {
return mFileCache.hasKey(key);
} catch (Exception exception) {
return false;
}
}
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class ProducerSequenceFactory method getBackgroundLocalContentUriFetchToEncodeMemorySequence.
/**
* background-thread hand-off -> multiplex -> encoded cache -> disk cache -> (webp transcode) ->
* local content resolver fetch
*/
private synchronized Producer<EncodedImage> getBackgroundLocalContentUriFetchToEncodeMemorySequence() {
if (FrescoSystrace.isTracing()) {
FrescoSystrace.beginSection("ProducerSequenceFactory#getBackgroundLocalContentUriFetchToEncodeMemorySequence");
}
if (mBackgroundLocalContentUriFetchToEncodedMemorySequence == null) {
if (FrescoSystrace.isTracing()) {
FrescoSystrace.beginSection("ProducerSequenceFactory#getBackgroundLocalContentUriFetchToEncodeMemorySequence:init");
}
final LocalContentUriFetchProducer localFileFetchProducer = mProducerFactory.newLocalContentUriFetchProducer();
final Producer<EncodedImage> toEncodedMultiplexProducer = newEncodedCacheMultiplexToTranscodeSequence(localFileFetchProducer);
mBackgroundLocalContentUriFetchToEncodedMemorySequence = mProducerFactory.newBackgroundThreadHandoffProducer(toEncodedMultiplexProducer, mThreadHandoffProducerQueue);
if (FrescoSystrace.isTracing()) {
FrescoSystrace.endSection();
}
}
if (FrescoSystrace.isTracing()) {
FrescoSystrace.endSection();
}
return mBackgroundLocalContentUriFetchToEncodedMemorySequence;
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class ProducerSequenceFactory method newDiskCacheSequence.
private Producer<EncodedImage> newDiskCacheSequence(Producer<EncodedImage> inputProducer) {
Producer<EncodedImage> cacheWriteProducer;
if (FrescoSystrace.isTracing()) {
FrescoSystrace.beginSection("ProducerSequenceFactory#newDiskCacheSequence");
}
if (mPartialImageCachingEnabled) {
Producer<EncodedImage> partialDiskCacheProducer = mProducerFactory.newPartialDiskCacheProducer(inputProducer);
cacheWriteProducer = mProducerFactory.newDiskCacheWriteProducer(partialDiskCacheProducer);
} else {
cacheWriteProducer = mProducerFactory.newDiskCacheWriteProducer(inputProducer);
}
DiskCacheReadProducer result = mProducerFactory.newDiskCacheReadProducer(cacheWriteProducer);
if (FrescoSystrace.isTracing()) {
FrescoSystrace.endSection();
}
return result;
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class StagingArea method containsKey.
/**
* Determine if an valid entry for the key exists in the staging area.
*/
public synchronized boolean containsKey(CacheKey key) {
Preconditions.checkNotNull(key);
if (!mMap.containsKey(key)) {
return false;
}
EncodedImage storedEncodedImage = mMap.get(key);
synchronized (storedEncodedImage) {
if (!EncodedImage.isValid(storedEncodedImage)) {
// Reference is not valid, this means that someone cleared reference while it was still in
// use. Log error
// TODO: 3697790
mMap.remove(key);
FLog.w(TAG, "Found closed reference %d for key %s (%d)", System.identityHashCode(storedEncodedImage), key.getUriString(), System.identityHashCode(key));
return false;
}
return true;
}
}
Aggregations