Skip to content

Commit

Permalink
Create local references of instance variables used in GIF decoder loops.
Browse files Browse the repository at this point in the history
Cuts ~40% off of frame rendering times on API 23 emulators and devices.

Progress towards #2471.
  • Loading branch information
sjudd committed Nov 22, 2017
1 parent 6837543 commit e7a4942
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,12 @@ private Bitmap setPixels(GifFrame currentFrame, GifFrame previousFrame) {
int inc = 8;
int iline = 0;
boolean isFirstFrame = framePointer == 0;
int sampleSize = this.sampleSize;
int downsampledWidth = this.downsampledWidth;
int downsampledHeight = this.downsampledHeight;
byte[] mainPixels = this.mainPixels;
int[] act = this.act;
boolean isFirstFrameTransparent = false;
for (int i = 0; i < downsampledIH; i++) {
int line = i;
if (currentFrame.interlace) {
Expand Down Expand Up @@ -538,7 +544,7 @@ private Bitmap setPixels(GifFrame currentFrame, GifFrame previousFrame) {
}
if (averageColor != COLOR_TRANSPARENT_BLACK) {
dest[dx] = averageColor;
} else if (!isFirstFrameTransparent && isFirstFrame) {
} else if (isFirstFrame && !isFirstFrameTransparent) {
isFirstFrameTransparent = true;
}
sx += sampleSize;
Expand All @@ -547,6 +553,8 @@ private Bitmap setPixels(GifFrame currentFrame, GifFrame previousFrame) {
}
}

this.isFirstFrameTransparent = isFirstFrameTransparent;

// Copy pixels into previous image
if (savePrevious && (currentFrame.dispose == DISPOSAL_UNSPECIFIED
|| currentFrame.dispose == DISPOSAL_NONE)) {
Expand Down Expand Up @@ -629,15 +637,19 @@ private void decodeBitmapData(GifFrame frame) {
// Allocate new pixel array.
mainPixels = bitmapProvider.obtainByteArray(npix);
}
byte[] mainPixels = this.mainPixels;
if (prefix == null) {
prefix = new short[MAX_STACK_SIZE];
}
short[] prefix = this.prefix;
if (suffix == null) {
suffix = new byte[MAX_STACK_SIZE];
}
byte[] suffix = this.suffix;
if (pixelStack == null) {
pixelStack = new byte[MAX_STACK_SIZE + 1];
}
byte[] pixelStack = this.pixelStack;

// Initialize GIF data stream decoder.
dataSize = readByte();
Expand All @@ -653,6 +665,7 @@ private void decodeBitmapData(GifFrame frame) {
suffix[code] = (byte) code;
}

byte[] block = this.block;
// Decode GIF pixel stream.
datum = bits = count = first = top = pi = bi = 0;
for (i = 0; i < npix; ) {
Expand All @@ -665,6 +678,7 @@ private void decodeBitmapData(GifFrame frame) {
break;
}
bi = 0;
block = this.block;
}

datum += (((int) block[bi]) & MASK_INT_LOWEST_BYTE) << bits;
Expand Down

0 comments on commit e7a4942

Please sign in to comment.