package c5;

import android.graphics.Bitmap;
import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/* JADX INFO: loaded from: classes.dex */
public final class l implements e {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Bitmap.Config f4891f = Bitmap.Config.ARGB_8888;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final m f4892a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Set f4893b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final androidx.window.core.h f4894c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final long f4895d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public long f4896e;

    public l(long j10) {
        q qVar = new q();
        HashSet hashSet = new HashSet(Arrays.asList(Bitmap.Config.values()));
        int i10 = Build.VERSION.SDK_INT;
        hashSet.add(null);
        if (i10 >= 26) {
            hashSet.remove(Bitmap.Config.HARDWARE);
        }
        Set setUnmodifiableSet = Collections.unmodifiableSet(hashSet);
        this.f4895d = j10;
        this.f4892a = qVar;
        this.f4893b = setUnmodifiableSet;
        this.f4894c = new androidx.window.core.h(8);
    }

    @NonNull
    private static Bitmap createBitmap(int i10, int i11, @Nullable Bitmap.Config config) {
        if (config == null) {
            config = f4891f;
        }
        return Bitmap.createBitmap(i10, i11, config);
    }

    @Nullable
    private synchronized Bitmap getDirtyOrNull(int i10, int i11, @Nullable Bitmap.Config config) {
        Bitmap bitmap;
        try {
            if (Build.VERSION.SDK_INT >= 26 && config == Bitmap.Config.HARDWARE) {
                throw new IllegalArgumentException("Cannot create a mutable Bitmap with config: " + config + ". Consider setting Downsampler#ALLOW_HARDWARE_CONFIG to false in your RequestOptions and/or in GlideBuilder.setDefaultRequestOptions");
            }
            bitmap = this.f4892a.get(i10, i11, config != null ? config : f4891f);
            if (bitmap != null) {
                this.f4896e -= (long) this.f4892a.h(bitmap);
                this.f4894c.getClass();
                bitmap.setHasAlpha(true);
                bitmap.setPremultiplied(true);
            } else if (Log.isLoggable("LruBitmapPool", 3)) {
                this.f4892a.f(i10, i11, config);
            }
            if (Log.isLoggable("LruBitmapPool", 2)) {
                this.f4892a.f(i10, i11, config);
            }
            if (Log.isLoggable("LruBitmapPool", 2)) {
                Objects.toString(this.f4892a);
            }
        } catch (Throwable th) {
            throw th;
        }
        return bitmap;
    }

    @Override // c5.e
    public final synchronized void a(Bitmap bitmap) {
        try {
            if (bitmap == null) {
                throw new NullPointerException("Bitmap must not be null");
            }
            if (bitmap.isRecycled()) {
                throw new IllegalStateException("Cannot pool recycled bitmap");
            }
            if (bitmap.isMutable() && this.f4892a.h(bitmap) <= this.f4895d && this.f4893b.contains(bitmap.getConfig())) {
                int iH = this.f4892a.h(bitmap);
                this.f4892a.a(bitmap);
                this.f4894c.getClass();
                this.f4896e += (long) iH;
                if (Log.isLoggable("LruBitmapPool", 2)) {
                    this.f4892a.j(bitmap);
                }
                if (Log.isLoggable("LruBitmapPool", 2)) {
                    Objects.toString(this.f4892a);
                }
                e(this.f4895d);
                return;
            }
            if (Log.isLoggable("LruBitmapPool", 2)) {
                this.f4892a.j(bitmap);
                bitmap.isMutable();
                this.f4893b.contains(bitmap.getConfig());
            }
            bitmap.recycle();
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // c5.e
    public final Bitmap b(int i10, int i11, Bitmap.Config config) {
        Bitmap dirtyOrNull = getDirtyOrNull(i10, i11, config);
        return dirtyOrNull == null ? createBitmap(i10, i11, config) : dirtyOrNull;
    }

    @Override // c5.e
    public final void c(int i10) {
        if (i10 >= 40 || i10 >= 20) {
            d();
        } else if (i10 >= 20 || i10 == 15) {
            e(this.f4895d / 2);
        }
    }

    @Override // c5.e
    public final void d() {
        e(0L);
    }

    public final synchronized void e(long j10) {
        while (this.f4896e > j10) {
            Bitmap bitmapRemoveLast = this.f4892a.removeLast();
            if (bitmapRemoveLast == null) {
                if (Log.isLoggable("LruBitmapPool", 5)) {
                    Objects.toString(this.f4892a);
                }
                this.f4896e = 0L;
                return;
            } else {
                this.f4894c.getClass();
                this.f4896e -= (long) this.f4892a.h(bitmapRemoveLast);
                if (Log.isLoggable("LruBitmapPool", 3)) {
                    this.f4892a.j(bitmapRemoveLast);
                }
                if (Log.isLoggable("LruBitmapPool", 2)) {
                    Objects.toString(this.f4892a);
                }
                bitmapRemoveLast.recycle();
            }
        }
    }

    @Override // c5.e
    public final Bitmap get(int i10, int i11, Bitmap.Config config) {
        Bitmap dirtyOrNull = getDirtyOrNull(i10, i11, config);
        if (dirtyOrNull == null) {
            return createBitmap(i10, i11, config);
        }
        dirtyOrNull.eraseColor(0);
        return dirtyOrNull;
    }
}
