Skip to content

Commit

Permalink
Remove intermediate work buffer.
Browse files Browse the repository at this point in the history
It adds complication and data copying and doesn’t seem to make much
of a difference in multiple low/high end devices or emulators. If it
seems like I/O might be a blocking issue, we can try just copying the
file into a byte[] in the ByteBufferGifDecoder the same as we do for the
stream decoder and wrapping it in ByteBuffer.

Progress towards #2471.
  • Loading branch information
sjudd committed Nov 22, 2017
1 parent 7c0cd63 commit 4db20db
Showing 1 changed file with 2 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,6 @@ public class StandardGifDecoder implements GifDecoder {
/** Raw data read working array. */
private byte[] block;

private static final int WORK_BUFFER_SIZE = 16 * 1024;
/**
* Temporary buffer for block reading.
* Reads 16k chunks from the native buffer for processing, to greatly reduce JNI overhead.
*/
private byte[] workBuffer;
private int workBufferSize;
private int workBufferPosition;

private GifHeaderParser parser;

// LZW decoder working arrays.
Expand Down Expand Up @@ -258,9 +249,6 @@ public synchronized Bitmap getNextFrame() {
if (block == null) {
block = bitmapProvider.obtainByteArray(255);
}
if (workBuffer == null) {
workBuffer = bitmapProvider.obtainByteArray(WORK_BUFFER_SIZE);
}

GifFrame currentFrame = header.frames.get(framePointer);
GifFrame previousFrame = null;
Expand Down Expand Up @@ -344,9 +332,6 @@ public void clear() {
if (block != null) {
bitmapProvider.release(block);
}
if (workBuffer != null) {
bitmapProvider.release(workBuffer);
}
}

@Override
Expand Down Expand Up @@ -711,8 +696,6 @@ private int averageColorsNear(int positionInMainPixels, int maxPositionInMainPix
* Decodes LZW image data into pixel array. Adapted from John Cristy's BitmapMagick.
*/
private void decodeBitmapData(GifFrame frame) {
workBufferSize = 0;
workBufferPosition = 0;
if (frame != null) {
// Jump to the frame start position.
rawData.position(frame.bufferFrameStart);
Expand Down Expand Up @@ -840,24 +823,11 @@ private void decodeBitmapData(GifFrame frame) {
Arrays.fill(mainPixels, pi, npix, (byte) COLOR_TRANSPARENT_BLACK);
}

/**
* Reads the next chunk for the intermediate work buffer.
*/
private void readChunkIfNeeded() {
if (workBufferSize > workBufferPosition) {
return;
}
workBufferPosition = 0;
workBufferSize = Math.min(rawData.remaining(), WORK_BUFFER_SIZE);
rawData.get(workBuffer, 0, workBufferSize);
}

/**
* Reads a single byte from the input stream.
*/
private int readByte() {
readChunkIfNeeded();
return workBuffer[workBufferPosition++] & MASK_INT_LOWEST_BYTE;
return rawData.get() & MASK_INT_LOWEST_BYTE;
}

/**
Expand All @@ -870,22 +840,7 @@ private int readBlock() {
if (blockSize <= 0) {
return blockSize;
}
final int remaining = workBufferSize - workBufferPosition;
if (remaining >= blockSize) {
// Block can be read from the current work buffer.
System.arraycopy(workBuffer, workBufferPosition, block, 0, blockSize);
workBufferPosition += blockSize;
} else if (rawData.remaining() + remaining >= blockSize) {
// Block can be read in two passes.
System.arraycopy(workBuffer, workBufferPosition, block, 0, remaining);
workBufferPosition = workBufferSize;
readChunkIfNeeded();
final int secondHalfRemaining = blockSize - remaining;
System.arraycopy(workBuffer, 0, block, remaining, secondHalfRemaining);
workBufferPosition += secondHalfRemaining;
} else {
status = STATUS_FORMAT_ERROR;
}
rawData.get(block, 0, Math.min(blockSize, rawData.remaining()));
return blockSize;
}

Expand Down

0 comments on commit 4db20db

Please sign in to comment.