use of com.facebook.common.memory.PooledByteBuffer 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.common.memory.PooledByteBuffer in project fresco by facebook.
the class KitKatPurgeableDecoder method decodeJPEGByteArrayAsPurgeable.
/**
* Decodes a byteArray containing jpeg encoded bytes into a purgeable bitmap
*
* <p> Adds a JFIF End-Of-Image marker if needed before decoding.
*
* @param bytesRef the byte buffer that contains the encoded bytes
* @return
*/
@Override
protected Bitmap decodeJPEGByteArrayAsPurgeable(CloseableReference<PooledByteBuffer> bytesRef, int length, BitmapFactory.Options options) {
byte[] suffix = endsWithEOI(bytesRef, length) ? null : EOI;
final PooledByteBuffer pooledByteBuffer = bytesRef.get();
Preconditions.checkArgument(length <= pooledByteBuffer.size());
// allocate bigger array in case EOI needs to be added
final CloseableReference<byte[]> encodedBytesArrayRef = mFlexByteArrayPool.get(length + 2);
try {
byte[] encodedBytesArray = encodedBytesArrayRef.get();
pooledByteBuffer.read(0, encodedBytesArray, 0, length);
if (suffix != null) {
putEOI(encodedBytesArray, length);
length += 2;
}
Bitmap bitmap = BitmapFactory.decodeByteArray(encodedBytesArray, 0, length, options);
return Preconditions.checkNotNull(bitmap, "BitmapFactory returned null");
} finally {
CloseableReference.closeSafely(encodedBytesArrayRef);
}
}
use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.
the class KitKatPurgeableDecoder method decodeByteArrayAsPurgeable.
/**
* Decodes a byteArray into a purgeable bitmap
*
* @param bytesRef the byte buffer that contains the encoded bytes
* @return
*/
@Override
protected Bitmap decodeByteArrayAsPurgeable(CloseableReference<PooledByteBuffer> bytesRef, BitmapFactory.Options options) {
final PooledByteBuffer pooledByteBuffer = bytesRef.get();
final int length = pooledByteBuffer.size();
final CloseableReference<byte[]> encodedBytesArrayRef = mFlexByteArrayPool.get(length);
try {
final byte[] encodedBytesArray = encodedBytesArrayRef.get();
pooledByteBuffer.read(0, encodedBytesArray, 0, length);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodedBytesArray, 0, length, options);
return Preconditions.checkNotNull(bitmap, "BitmapFactory returned null");
} finally {
CloseableReference.closeSafely(encodedBytesArrayRef);
}
}
use of com.facebook.common.memory.PooledByteBuffer 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.common.memory.PooledByteBuffer in project fresco by facebook.
the class EncodedImageTest method testIsJpegCompleteAt_notComplete.
@Test
public void testIsJpegCompleteAt_notComplete() {
byte[] encodedBytes = new byte[ENCODED_BYTES_LENGTH];
encodedBytes[ENCODED_BYTES_LENGTH - 2] = 0;
encodedBytes[ENCODED_BYTES_LENGTH - 1] = 0;
PooledByteBuffer buf = new TrivialPooledByteBuffer(encodedBytes);
EncodedImage encodedImage = new EncodedImage(CloseableReference.of(buf));
encodedImage.setImageFormat(DefaultImageFormats.JPEG);
assertFalse(encodedImage.isCompleteAt(ENCODED_BYTES_LENGTH));
}
Aggregations