From 4db20db0486f97ec9c5ca67b5d08c6b3b700581b Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Tue, 21 Nov 2017 18:49:01 -0800 Subject: [PATCH] Remove intermediate work buffer. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../glide/gifdecoder/StandardGifDecoder.java | 49 +------------------ 1 file changed, 2 insertions(+), 47 deletions(-) diff --git a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/StandardGifDecoder.java b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/StandardGifDecoder.java index e50d64398b..681c7adc58 100644 --- a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/StandardGifDecoder.java +++ b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/StandardGifDecoder.java @@ -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. @@ -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; @@ -344,9 +332,6 @@ public void clear() { if (block != null) { bitmapProvider.release(block); } - if (workBuffer != null) { - bitmapProvider.release(workBuffer); - } } @Override @@ -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); @@ -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; } /** @@ -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; }