Search in sources :

Example 41 with EncodedImage

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

the class HoneycombBitmapFactory method createBitmapInternal.

/**
   * Creates a bitmap of the specified width and height.
   *
   * @param width the width of the bitmap
   * @param height the height of the bitmap
   * @param bitmapConfig the {@link android.graphics.Bitmap.Config}
   * used to create the decoded Bitmap
   * @return a reference to the bitmap
   * @throws TooManyBitmapsException if the pool is full
   * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
   */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public CloseableReference<Bitmap> createBitmapInternal(int width, int height, Bitmap.Config bitmapConfig) {
    CloseableReference<PooledByteBuffer> jpgRef = mJpegGenerator.generate((short) width, (short) height);
    try {
        EncodedImage encodedImage = new EncodedImage(jpgRef);
        encodedImage.setImageFormat(DefaultImageFormats.JPEG);
        try {
            CloseableReference<Bitmap> bitmapRef = mPurgeableDecoder.decodeJPEGFromEncodedImage(encodedImage, bitmapConfig, jpgRef.get().size());
            bitmapRef.get().setHasAlpha(true);
            bitmapRef.get().eraseColor(Color.TRANSPARENT);
            return bitmapRef;
        } finally {
            EncodedImage.closeSafely(encodedImage);
        }
    } finally {
        jpgRef.close();
    }
}
Also used : Bitmap(android.graphics.Bitmap) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) TargetApi(android.annotation.TargetApi)

Example 42 with EncodedImage

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

the class SplitCachesByImageSizeDiskCachePolicy method createAndStartCacheReadTask.

@Override
public Task<EncodedImage> createAndStartCacheReadTask(ImageRequest imageRequest, Object callerContext, final AtomicBoolean isCancelled) {
    final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, callerContext);
    final boolean alreadyInSmall = mSmallImageBufferedDiskCache.containsSync(cacheKey);
    final boolean alreadyInMain = mDefaultBufferedDiskCache.containsSync(cacheKey);
    final BufferedDiskCache firstCache;
    final BufferedDiskCache secondCache;
    if (alreadyInSmall || !alreadyInMain) {
        firstCache = mSmallImageBufferedDiskCache;
        secondCache = mDefaultBufferedDiskCache;
    } else {
        firstCache = mDefaultBufferedDiskCache;
        secondCache = mSmallImageBufferedDiskCache;
    }
    return firstCache.get(cacheKey, isCancelled).continueWithTask(new Continuation<EncodedImage, Task<EncodedImage>>() {

        @Override
        public Task<EncodedImage> then(Task<EncodedImage> task) throws Exception {
            if (isTaskCancelled(task) || (!task.isFaulted() && task.getResult() != null)) {
                return task;
            }
            return secondCache.get(cacheKey, isCancelled);
        }
    });
}
Also used : Task(bolts.Task) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CacheKey(com.facebook.cache.common.CacheKey) CancellationException(java.util.concurrent.CancellationException)

Example 43 with EncodedImage

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

the class StagingArea method remove.

/**
   * Removes key-value from the StagingArea. Both key and value must match.
   * @param key
   * @param encodedImage value corresponding to key
   * @return true if item was removed
   */
public synchronized boolean remove(final CacheKey key, final EncodedImage encodedImage) {
    Preconditions.checkNotNull(key);
    Preconditions.checkNotNull(encodedImage);
    Preconditions.checkArgument(EncodedImage.isValid(encodedImage));
    final EncodedImage oldValue = mMap.get(key);
    if (oldValue == null) {
        return false;
    }
    CloseableReference<PooledByteBuffer> oldRef = oldValue.getByteBufferRef();
    CloseableReference<PooledByteBuffer> ref = encodedImage.getByteBufferRef();
    try {
        if (oldRef == null || ref == null || oldRef.get() != ref.get()) {
            return false;
        }
        mMap.remove(key);
    } finally {
        CloseableReference.closeSafely(ref);
        CloseableReference.closeSafely(oldRef);
        EncodedImage.closeSafely(oldValue);
    }
    logStats();
    return true;
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 44 with EncodedImage

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

the class StagingArea method clearAll.

/**
   * Removes all items from the StagingArea.
   */
public void clearAll() {
    final List<EncodedImage> old;
    synchronized (this) {
        old = new ArrayList<>(mMap.values());
        mMap.clear();
    }
    for (int i = 0; i < old.size(); i++) {
        EncodedImage encodedImage = old.get(i);
        if (encodedImage != null) {
            encodedImage.close();
        }
    }
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 45 with EncodedImage

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

the class StagingArea method get.

/**
   * @param key
   * @return value associated with given key or null if no value is associated
   */
public synchronized EncodedImage get(final CacheKey key) {
    Preconditions.checkNotNull(key);
    EncodedImage storedEncodedImage = mMap.get(key);
    if (storedEncodedImage != null) {
        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 null;
            }
            storedEncodedImage = EncodedImage.cloneOrNull(storedEncodedImage);
        }
    }
    return storedEncodedImage;
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Aggregations

EncodedImage (com.facebook.imagepipeline.image.EncodedImage)91 Test (org.junit.Test)24 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)19 CacheKey (com.facebook.cache.common.CacheKey)9 TrivialPooledByteBuffer (com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)9 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)9 InputStream (java.io.InputStream)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 Bitmap (android.graphics.Bitmap)7 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)7 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)6 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)6 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)6 FakeClock (com.facebook.imagepipeline.testing.FakeClock)6 TestExecutorService (com.facebook.imagepipeline.testing.TestExecutorService)6 Before (org.junit.Before)6 Rect (android.graphics.Rect)5 MultiCacheKey (com.facebook.cache.common.MultiCacheKey)5 ArrayList (java.util.ArrayList)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5