package com.bumptech.glide.load.resource.gif; import android.content.Context; import android.graphics.Bitmap; import android.util.Log; import com.bumptech.glide.gifdecoder.GifDecoder; import com.bumptech.glide.gifdecoder.GifHeader; import com.bumptech.glide.gifdecoder.GifHeaderParser; import com.bumptech.glide.gifdecoder.StandardGifDecoder; import com.bumptech.glide.load.DecodeFormat; import com.bumptech.glide.load.ImageHeaderParser; import com.bumptech.glide.load.ImageHeaderParserUtils; import com.bumptech.glide.load.Options; import com.bumptech.glide.load.ResourceDecoder; import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.UnitTransformation; import com.bumptech.glide.util.LogTime; import com.bumptech.glide.util.Util; import java.io.IOException; import java.nio.ByteBuffer; import java.util.List; import java.util.Queue; /* loaded from: classes.dex */ public class ByteBufferGifDecoder implements ResourceDecoder { private static final GifDecoderFactory f = new GifDecoderFactory(); private static final GifHeaderParserPool g = new GifHeaderParserPool(); private final Context a; private final List b; private final GifHeaderParserPool c; private final GifDecoderFactory d; private final GifBitmapProvider e; static class GifDecoderFactory { GifDecoderFactory() { } GifDecoder a(GifDecoder.BitmapProvider bitmapProvider, GifHeader gifHeader, ByteBuffer byteBuffer, int i) { return new StandardGifDecoder(bitmapProvider, gifHeader, byteBuffer, i); } } public ByteBufferGifDecoder(Context context, List list, BitmapPool bitmapPool, ArrayPool arrayPool) { this(context, list, bitmapPool, arrayPool, g, f); } ByteBufferGifDecoder(Context context, List list, BitmapPool bitmapPool, ArrayPool arrayPool, GifHeaderParserPool gifHeaderParserPool, GifDecoderFactory gifDecoderFactory) { this.a = context.getApplicationContext(); this.b = list; this.d = gifDecoderFactory; this.e = new GifBitmapProvider(bitmapPool, arrayPool); this.c = gifHeaderParserPool; } static class GifHeaderParserPool { private final Queue a = Util.a(0); GifHeaderParserPool() { } synchronized GifHeaderParser a(ByteBuffer byteBuffer) { GifHeaderParser poll; poll = this.a.poll(); if (poll == null) { poll = new GifHeaderParser(); } poll.a(byteBuffer); return poll; } synchronized void a(GifHeaderParser gifHeaderParser) { gifHeaderParser.a(); this.a.offer(gifHeaderParser); } } @Override // com.bumptech.glide.load.ResourceDecoder public boolean a(ByteBuffer byteBuffer, Options options) throws IOException { return !((Boolean) options.a(GifOptions.b)).booleanValue() && ImageHeaderParserUtils.a(this.b, byteBuffer) == ImageHeaderParser.ImageType.GIF; } @Override // com.bumptech.glide.load.ResourceDecoder public GifDrawableResource a(ByteBuffer byteBuffer, int i, int i2, Options options) { GifHeaderParser a = this.c.a(byteBuffer); try { return a(byteBuffer, i, i2, a, options); } finally { this.c.a(a); } } private GifDrawableResource a(ByteBuffer byteBuffer, int i, int i2, GifHeaderParser gifHeaderParser, Options options) { long a = LogTime.a(); try { GifHeader b = gifHeaderParser.b(); if (b.b() > 0 && b.c() == 0) { Bitmap.Config config = options.a(GifOptions.a) == DecodeFormat.PREFER_RGB_565 ? Bitmap.Config.RGB_565 : Bitmap.Config.ARGB_8888; GifDecoder a2 = this.d.a(this.e, b, byteBuffer, a(b, i, i2)); a2.a(config); a2.b(); Bitmap a3 = a2.a(); if (a3 == null) { return null; } GifDrawableResource gifDrawableResource = new GifDrawableResource(new GifDrawable(this.a, a2, UnitTransformation.a(), i, i2, a3)); if (Log.isLoggable("BufferGifDecoder", 2)) { Log.v("BufferGifDecoder", "Decoded GIF from stream in " + LogTime.a(a)); } return gifDrawableResource; } if (Log.isLoggable("BufferGifDecoder", 2)) { Log.v("BufferGifDecoder", "Decoded GIF from stream in " + LogTime.a(a)); } return null; } finally { if (Log.isLoggable("BufferGifDecoder", 2)) { Log.v("BufferGifDecoder", "Decoded GIF from stream in " + LogTime.a(a)); } } } private static int a(GifHeader gifHeader, int i, int i2) { int min = Math.min(gifHeader.a() / i2, gifHeader.d() / i); int max = Math.max(1, min == 0 ? 0 : Integer.highestOneBit(min)); if (Log.isLoggable("BufferGifDecoder", 2) && max > 1) { Log.v("BufferGifDecoder", "Downsampling GIF, sampleSize: " + max + ", target dimens: [" + i + "x" + i2 + "], actual dimens: [" + gifHeader.d() + "x" + gifHeader.a() + "]"); } return max; } }