Search in sources :

Example 16 with CloseableImage

use of com.facebook.imagepipeline.image.CloseableImage in project fresco by facebook.

the class BaseFrescoStethoPlugin method memcache.

private void memcache(PrintStream writer, List<String> args) throws DumpException {
    CountingMemoryCacheInspector.DumpInfo<CacheKey, CloseableImage> dumpInfo = mBitmapMemoryCacheInspector.dumpCacheContent();
    try {
        writer.println(mBitmapMemoryCacheInspector.getClass().getSimpleName());
        writer.println();
        writer.println("Params:");
        writer.println(formatStrLocaleSafe("Max size:          %7.2fMB", dumpInfo.maxSize / (1024.0 * KB)));
        writer.println(formatStrLocaleSafe("Max entries count: %9d", dumpInfo.maxEntriesCount));
        writer.println(formatStrLocaleSafe("Max entry size:    %7.2fMB", dumpInfo.maxEntrySize / (1024.0 * KB)));
        writer.println();
        writer.println("Summary of current content:");
        writer.println(formatStrLocaleSafe("Total size:        %7.2fMB (includes in-use content)", dumpInfo.size / (1024.0 * KB)));
        writer.println(formatStrLocaleSafe("Entries count:     %9d", dumpInfo.lruEntries.size() + dumpInfo.sharedEntries.size()));
        writer.println(formatStrLocaleSafe("LRU size:          %7.2fMB", dumpInfo.lruSize / (1024.0 * KB)));
        writer.println(formatStrLocaleSafe("LRU count:         %9d", dumpInfo.lruEntries.size()));
        writer.println(formatStrLocaleSafe("Shared size:       %7.2fMB", (dumpInfo.size - dumpInfo.lruSize) / (1024.0 * KB)));
        writer.println(formatStrLocaleSafe("Shared count:      %9d", dumpInfo.sharedEntries.size()));
        writer.println();
        writer.println("The cache consists of two parts: Things " + "currently being used and things not.");
        writer.println("Those things that are *not* currently being used are in the LRU.");
        writer.println("Things currently being used are considered to be shared. They will be added");
        writer.println("to the LRU if/when they stop being used.");
        writer.println();
        writer.println("LRU contents: (things near the top will be evicted first)");
        for (CountingMemoryCacheInspector.DumpInfoEntry entry : dumpInfo.lruEntries) {
            writeCacheEntry(writer, entry);
        }
        writer.println();
        writer.println("Shared contents:");
        for (CountingMemoryCacheInspector.DumpInfoEntry entry : dumpInfo.sharedEntries) {
            writeCacheEntry(writer, entry);
        }
        if (!args.isEmpty() && "-g".equals(args.get(0))) {
            getFiles(writer, dumpInfo);
        }
    } catch (IOException e) {
        throw new DumpException(e.getMessage());
    } finally {
        dumpInfo.release();
    }
}
Also used : DumpException(com.facebook.stetho.dumpapp.DumpException) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) IOException(java.io.IOException) CacheKey(com.facebook.cache.common.CacheKey) BitmapMemoryCacheKey(com.facebook.imagepipeline.cache.BitmapMemoryCacheKey) CountingMemoryCacheInspector(com.facebook.imagepipeline.cache.CountingMemoryCacheInspector)

Example 17 with CloseableImage

use of com.facebook.imagepipeline.image.CloseableImage in project fresco by facebook.

the class AnimatedFrameCache method getForReuse.

/**
   * Gets the image to be reused, or null if there is no such image.
   *
   * <p> The returned image is the least recently used image that has no more clients referencing
   * it, and it has not yet been evicted from the cache.
   *
   * <p> The client can freely modify the bitmap of the returned image and can cache it again
   * without any restrictions.
   */
@Nullable
public CloseableReference<CloseableImage> getForReuse() {
    while (true) {
        CacheKey key = popFirstFreeItemKey();
        if (key == null) {
            return null;
        }
        CloseableReference<CloseableImage> imageRef = mBackingCache.reuse(key);
        if (imageRef != null) {
            return imageRef;
        }
    }
}
Also used : CloseableImage(com.facebook.imagepipeline.image.CloseableImage) CacheKey(com.facebook.cache.common.CacheKey) Nullable(javax.annotation.Nullable)

Example 18 with CloseableImage

use of com.facebook.imagepipeline.image.CloseableImage in project fresco by facebook.

the class BitmapMemoryCacheProducerTest method testCacheIntermediateImageAsBetterScan.

@Test
public void testCacheIntermediateImageAsBetterScan() {
    setupBitmapMemoryCacheGetNotFound();
    setupInputProducerStreamingSuccess();
    CloseableImage closeableImage = mock(CloseableImage.class);
    when(closeableImage.getQualityInfo()).thenReturn(ImmutableQualityInfo.of(INTERMEDIATE_SCAN_1, false, false));
    CloseableReference<CloseableImage> closeableImageRef = CloseableReference.of(closeableImage);
    when(mMemoryCache.get(mBitmapMemoryCacheKey)).thenReturn(null).thenReturn(closeableImageRef);
    mBitmapMemoryCacheProducer.produceResults(mConsumer, mProducerContext);
    verify(mMemoryCache).cache(mBitmapMemoryCacheKey, mIntermediateImageReference);
    verify(mMemoryCache).cache(mBitmapMemoryCacheKey, mFinalImageReference);
    verify(mConsumer).onNewResult(mIntermediateImageReferenceClone, false);
    verify(mConsumer).onNewResult(mFinalImageReferenceClone, true);
    Assert.assertTrue(!mIntermediateImageReferenceClone.isValid());
    Assert.assertTrue(!mFinalImageReferenceClone.isValid());
    Assert.assertEquals(0, closeableImageRef.getUnderlyingReferenceTestOnly().getRefCountTestOnly());
    verify(mProducerListener).onProducerStart(mRequestId, PRODUCER_NAME);
    Map<String, String> extraMap = ImmutableMap.of(BitmapMemoryCacheProducer.EXTRA_CACHED_VALUE_FOUND, "false");
    verify(mProducerListener).onProducerFinishWithSuccess(mRequestId, PRODUCER_NAME, extraMap);
}
Also used : CloseableImage(com.facebook.imagepipeline.image.CloseableImage)

Example 19 with CloseableImage

use of com.facebook.imagepipeline.image.CloseableImage in project fresco by facebook.

the class BitmapMemoryCacheProducerTest method testDontCacheIntermediateImageAsAlreadyHaveFullQuality.

@Test
public void testDontCacheIntermediateImageAsAlreadyHaveFullQuality() {
    setupBitmapMemoryCacheGetNotFound();
    setupInputProducerStreamingSuccess();
    CloseableImage closeableImage = mock(CloseableImage.class);
    when(closeableImage.getQualityInfo()).thenReturn(ImmutableQualityInfo.FULL_QUALITY);
    CloseableReference<CloseableImage> closeableImageRef = CloseableReference.of(closeableImage);
    when(mMemoryCache.get(mBitmapMemoryCacheKey)).thenReturn(null).thenReturn(closeableImageRef);
    mBitmapMemoryCacheProducer.produceResults(mConsumer, mProducerContext);
    verify(mMemoryCache, never()).cache(mBitmapMemoryCacheKey, mIntermediateImageReference);
    verify(mMemoryCache).cache(mBitmapMemoryCacheKey, mFinalImageReference);
    verify(mConsumer).onNewResult(closeableImageRef, false);
    verify(mConsumer).onNewResult(mFinalImageReferenceClone, true);
    Assert.assertTrue(!mFinalImageReferenceClone.isValid());
    Assert.assertEquals(0, closeableImageRef.getUnderlyingReferenceTestOnly().getRefCountTestOnly());
    verify(mProducerListener).onProducerStart(mRequestId, PRODUCER_NAME);
    Map<String, String> extraMap = ImmutableMap.of(BitmapMemoryCacheProducer.EXTRA_CACHED_VALUE_FOUND, "false");
    verify(mProducerListener).onProducerFinishWithSuccess(mRequestId, PRODUCER_NAME, extraMap);
}
Also used : CloseableImage(com.facebook.imagepipeline.image.CloseableImage)

Example 20 with CloseableImage

use of com.facebook.imagepipeline.image.CloseableImage in project SherlockAdapter by EvilBT.

the class FrescoUtil method save.

public static Boolean save(@NonNull String url, @NonNull File outputFile) throws IOException {
    ImagePipelineFactory factory = Fresco.getImagePipelineFactory();
    ImagePipeline pipeline = factory.getImagePipeline();
    boolean isInCache = pipeline.isInDiskCacheSync(Uri.parse(url));
    if (isInCache) {
        BinaryResource resource = factory.getMainFileCache().getResource(new SimpleCacheKey(url));
        if (resource instanceof FileBinaryResource) {
            FileBinaryResource fileResource = (FileBinaryResource) resource;
            FileChannel input = new FileInputStream(fileResource.getFile()).getChannel();
            FileChannel output = new FileOutputStream(outputFile).getChannel();
            output.transferFrom(input, 0, input.size());
            input.close();
            output.close();
            return true;
        }
    }
    boolean isMemoryCache = pipeline.isInBitmapMemoryCache(Uri.parse(url));
    if (!isMemoryCache) {
        return false;
    }
    ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url)).build();
    DataSource<CloseableReference<CloseableImage>> dataSource = pipeline.fetchImageFromBitmapCache(request, null);
    if (!dataSource.isFinished()) {
        return false;
    }
    CloseableReference<CloseableImage> closeableImageRef = dataSource.getResult();
    Bitmap bitmap = null;
    if (closeableImageRef != null && closeableImageRef.get() instanceof CloseableBitmap) {
        bitmap = ((CloseableBitmap) closeableImageRef.get()).getUnderlyingBitmap();
    }
    if (bitmap == null) {
        return false;
    }
    FileOutputStream outputStream = new FileOutputStream(outputFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    outputStream.flush();
    outputStream.close();
    return true;
}
Also used : ImagePipelineFactory(com.facebook.imagepipeline.core.ImagePipelineFactory) FileChannel(java.nio.channels.FileChannel) CloseableReference(com.facebook.common.references.CloseableReference) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileInputStream(java.io.FileInputStream) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) Bitmap(android.graphics.Bitmap) BinaryResource(com.facebook.binaryresource.BinaryResource) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileOutputStream(java.io.FileOutputStream) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline)

Aggregations

CloseableImage (com.facebook.imagepipeline.image.CloseableImage)20 CloseableReference (com.facebook.common.references.CloseableReference)8 CacheKey (com.facebook.cache.common.CacheKey)7 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)5 Bitmap (android.graphics.Bitmap)4 CloseableBitmap (com.facebook.imagepipeline.image.CloseableBitmap)3 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)2 BitmapMemoryCacheKey (com.facebook.imagepipeline.cache.BitmapMemoryCacheKey)2 CountingMemoryCacheInspector (com.facebook.imagepipeline.cache.CountingMemoryCacheInspector)2 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)2 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)2 CloseableStaticBitmap (com.facebook.imagepipeline.image.CloseableStaticBitmap)2 SingleUsePostprocessorConsumer (com.facebook.imagepipeline.producers.PostprocessorProducer.SingleUsePostprocessorConsumer)2 IOException (java.io.IOException)2 SuppressLint (android.annotation.SuppressLint)1 PendingIntent (android.app.PendingIntent)1 ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Drawable (android.graphics.drawable.Drawable)1