use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.
the class DalvikPurgeableDecoder method decodeFromEncodedImage.
/**
* Creates a bitmap from encoded bytes.
*
* @param encodedImage the encoded image with reference to the encoded bytes
* @param bitmapConfig the {@link android.graphics.Bitmap.Config}
* used to create the decoded Bitmap
* @return the bitmap
* @throws TooManyBitmapsException if the pool is full
* @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
*/
@Override
public CloseableReference<Bitmap> decodeFromEncodedImage(final EncodedImage encodedImage, Bitmap.Config bitmapConfig) {
BitmapFactory.Options options = getBitmapFactoryOptions(encodedImage.getSampleSize(), bitmapConfig);
CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef();
Preconditions.checkNotNull(bytesRef);
try {
Bitmap bitmap = decodeByteArrayAsPurgeable(bytesRef, options);
return pinBitmap(bitmap);
} finally {
CloseableReference.closeSafely(bytesRef);
}
}
use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.
the class DalvikPurgeableDecoder method decodeJPEGFromEncodedImage.
/**
* Creates a bitmap from encoded JPEG bytes. Supports a partial JPEG image.
*
* @param encodedImage the encoded image with reference to the encoded bytes
* @param length the number of encoded bytes in the buffer
* @param bitmapConfig the {@link android.graphics.Bitmap.Config}
* used to create the decoded Bitmap
* @return the bitmap
* @throws TooManyBitmapsException if the pool is full
* @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
*/
@Override
public CloseableReference<Bitmap> decodeJPEGFromEncodedImage(final EncodedImage encodedImage, Bitmap.Config bitmapConfig, int length) {
BitmapFactory.Options options = getBitmapFactoryOptions(encodedImage.getSampleSize(), bitmapConfig);
final CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef();
Preconditions.checkNotNull(bytesRef);
try {
Bitmap bitmap = decodeJPEGByteArrayAsPurgeable(bytesRef, length, options);
return pinBitmap(bitmap);
} finally {
CloseableReference.closeSafely(bytesRef);
}
}
use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.
the class WebpTranscodeProducer method transcodeLastResult.
private void transcodeLastResult(final EncodedImage originalResult, final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
Preconditions.checkNotNull(originalResult);
final EncodedImage encodedImageCopy = EncodedImage.cloneOrNull(originalResult);
final StatefulProducerRunnable<EncodedImage> runnable = new StatefulProducerRunnable<EncodedImage>(consumer, producerContext.getListener(), PRODUCER_NAME, producerContext.getId()) {
@Override
protected EncodedImage getResult() throws Exception {
PooledByteBufferOutputStream outputStream = mPooledByteBufferFactory.newOutputStream();
try {
doTranscode(encodedImageCopy, outputStream);
CloseableReference<PooledByteBuffer> ref = CloseableReference.of(outputStream.toByteBuffer());
try {
EncodedImage encodedImage = new EncodedImage(ref);
encodedImage.copyMetaDataFrom(encodedImageCopy);
return encodedImage;
} finally {
CloseableReference.closeSafely(ref);
}
} finally {
outputStream.close();
}
}
@Override
protected void disposeResult(EncodedImage result) {
EncodedImage.closeSafely(result);
}
@Override
protected void onSuccess(EncodedImage result) {
EncodedImage.closeSafely(encodedImageCopy);
super.onSuccess(result);
}
@Override
protected void onFailure(Exception e) {
EncodedImage.closeSafely(encodedImageCopy);
super.onFailure(e);
}
@Override
protected void onCancellation() {
EncodedImage.closeSafely(encodedImageCopy);
super.onCancellation();
}
};
mExecutor.execute(runnable);
}
use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.
the class EncodedMemoryCacheProducer method produceResults.
@Override
public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
final String requestId = producerContext.getId();
final ProducerListener listener = producerContext.getListener();
listener.onProducerStart(requestId, PRODUCER_NAME);
final ImageRequest imageRequest = producerContext.getImageRequest();
final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, producerContext.getCallerContext());
CloseableReference<PooledByteBuffer> cachedReference = mMemoryCache.get(cacheKey);
try {
if (cachedReference != null) {
EncodedImage cachedEncodedImage = new EncodedImage(cachedReference);
cachedEncodedImage.setEncodedCacheKey(cacheKey);
try {
listener.onProducerFinishWithSuccess(requestId, PRODUCER_NAME, listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "true") : null);
consumer.onProgressUpdate(1f);
consumer.onNewResult(cachedEncodedImage, true);
return;
} finally {
EncodedImage.closeSafely(cachedEncodedImage);
}
}
if (producerContext.getLowestPermittedRequestLevel().getValue() >= ImageRequest.RequestLevel.ENCODED_MEMORY_CACHE.getValue()) {
listener.onProducerFinishWithSuccess(requestId, PRODUCER_NAME, listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
consumer.onNewResult(null, true);
return;
}
Consumer consumerOfInputProducer = new EncodedMemoryCacheConsumer(consumer, mMemoryCache, cacheKey);
listener.onProducerFinishWithSuccess(requestId, PRODUCER_NAME, listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
mInputProducer.produceResults(consumerOfInputProducer, producerContext);
} finally {
CloseableReference.closeSafely(cachedReference);
}
}
use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.
the class EncodedImage method isCompleteAt.
/**
* Returns true if the image is a JPEG and its data is already complete at the specified length,
* false otherwise.
*/
public boolean isCompleteAt(int length) {
if (mImageFormat != DefaultImageFormats.JPEG) {
return true;
}
// If the image is backed by FileInputStreams return true since they will always be complete.
if (mInputStreamSupplier != null) {
return true;
}
// The image should be backed by a ByteBuffer
Preconditions.checkNotNull(mPooledByteBufferRef);
PooledByteBuffer buf = mPooledByteBufferRef.get();
return (buf.read(length - 2) == (byte) JfifUtil.MARKER_FIRST_BYTE) && (buf.read(length - 1) == (byte) JfifUtil.MARKER_EOI);
}
Aggregations