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();
}
}
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);
}
});
}
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;
}
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();
}
}
}
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;
}
Aggregations