Search in sources :

Example 91 with EncodedImage

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();
        }
    }
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException)

Example 92 with EncodedImage

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;
        }
    }
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException)

Example 93 with EncodedImage

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;
}
Also used : LocalContentUriFetchProducer(com.facebook.imagepipeline.producers.LocalContentUriFetchProducer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 94 with EncodedImage

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;
}
Also used : DiskCacheReadProducer(com.facebook.imagepipeline.producers.DiskCacheReadProducer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 95 with EncodedImage

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;
    }
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Aggregations

EncodedImage (com.facebook.imagepipeline.image.EncodedImage)108 Test (org.junit.Test)32 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)20 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)12 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)12 CacheKey (com.facebook.cache.common.CacheKey)11 Rect (android.graphics.Rect)9 TrivialPooledByteBuffer (com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)9 InputStream (java.io.InputStream)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)9 AnimatedDrawableBackend (com.facebook.imagepipeline.animated.base.AnimatedDrawableBackend)8 AnimatedImageCompositor (com.facebook.imagepipeline.animated.impl.AnimatedImageCompositor)8 ImageDecodeOptions (com.facebook.imagepipeline.common.ImageDecodeOptions)8 Before (org.junit.Before)8 Bitmap (android.graphics.Bitmap)7 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)7 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)7 FakeClock (com.facebook.imagepipeline.testing.FakeClock)6 TestExecutorService (com.facebook.imagepipeline.testing.TestExecutorService)6