package io.flutter.embedding.android;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorSpace;
import android.graphics.Paint;
import android.hardware.HardwareBuffer;
import android.media.Image;
import android.media.ImageReader;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Surface;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.nio.ByteBuffer;
import java.util.Locale;

/* JADX INFO: loaded from: classes.dex */
public class FlutterImageView extends View implements io.flutter.embedding.engine.renderer.l {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public ImageReader f27844a;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public boolean f27846c;

    @Nullable
    private Bitmap currentBitmap;

    @Nullable
    private Image currentImage;

    @Nullable
    private io.flutter.embedding.engine.renderer.j flutterRenderer;

    /* JADX WARN: Failed to restore enum class, 'enum' modifier and super class removed */
    /* JADX WARN: Unknown enum class pattern. Please report as an issue! */
    public static final class SurfaceKind {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public static final SurfaceKind f27847a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public static final SurfaceKind f27848b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public static final /* synthetic */ SurfaceKind[] f27849c;

        static {
            SurfaceKind surfaceKind = new SurfaceKind("background", 0);
            f27847a = surfaceKind;
            SurfaceKind surfaceKind2 = new SurfaceKind("overlay", 1);
            f27848b = surfaceKind2;
            f27849c = new SurfaceKind[]{surfaceKind, surfaceKind2};
        }

        public static SurfaceKind valueOf(String str) {
            return (SurfaceKind) Enum.valueOf(SurfaceKind.class, str);
        }

        public static SurfaceKind[] values() {
            return (SurfaceKind[]) f27849c.clone();
        }
    }

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public FlutterImageView(Context context, int i10, int i11, SurfaceKind surfaceKind) {
        super(context, null);
        ImageReader imageReaderD = d(i10, i11);
        this.f27846c = false;
        this.f27844a = imageReaderD;
        this.f27845b = surfaceKind;
        setAlpha(0.0f);
    }

    public static ImageReader d(int i10, int i11) {
        if (i10 <= 0) {
            String.format(Locale.US, "ImageReader width must be greater than 0, but given width=%d, set width=1", Integer.valueOf(i10));
            i10 = 1;
        }
        if (i11 <= 0) {
            String.format(Locale.US, "ImageReader height must be greater than 0, but given height=%d, set height=1", Integer.valueOf(i11));
            i11 = 1;
        }
        return Build.VERSION.SDK_INT >= 29 ? ImageReader.newInstance(i10, i11, 1, 3, 768L) : ImageReader.newInstance(i10, i11, 1, 3);
    }

    @Override // io.flutter.embedding.engine.renderer.l
    public final void a(io.flutter.embedding.engine.renderer.j jVar) {
        if (this.f27845b.ordinal() == 0) {
            jVar.i(this.f27844a.getSurface());
        }
        setAlpha(1.0f);
        this.flutterRenderer = jVar;
        this.f27846c = true;
    }

    @Override // io.flutter.embedding.engine.renderer.l
    public final void b() {
        if (this.f27846c) {
            setAlpha(0.0f);
            c();
            this.currentBitmap = null;
            Image image = this.currentImage;
            if (image != null) {
                image.close();
                this.currentImage = null;
            }
            invalidate();
            this.f27846c = false;
        }
    }

    public final boolean c() {
        if (!this.f27846c) {
            return false;
        }
        Image imageAcquireLatestImage = this.f27844a.acquireLatestImage();
        if (imageAcquireLatestImage != null) {
            Image image = this.currentImage;
            if (image != null) {
                image.close();
                this.currentImage = null;
            }
            this.currentImage = imageAcquireLatestImage;
            invalidate();
        }
        return imageAcquireLatestImage != null;
    }

    public final void e(int i10, int i11) {
        if (this.flutterRenderer == null) {
            return;
        }
        if (i10 == this.f27844a.getWidth() && i11 == this.f27844a.getHeight()) {
            return;
        }
        Image image = this.currentImage;
        if (image != null) {
            image.close();
            this.currentImage = null;
        }
        this.f27844a.close();
        this.f27844a = d(i10, i11);
    }

    @Override // io.flutter.embedding.engine.renderer.l
    @Nullable
    public io.flutter.embedding.engine.renderer.j getAttachedRenderer() {
        return this.flutterRenderer;
    }

    public ImageReader getImageReader() {
        return this.f27844a;
    }

    @NonNull
    public Surface getSurface() {
        return this.f27844a.getSurface();
    }

    @Override // android.view.View
    public final void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Image image = this.currentImage;
        if (image != null) {
            if (Build.VERSION.SDK_INT >= 29) {
                HardwareBuffer hardwareBuffer = image.getHardwareBuffer();
                this.currentBitmap = Bitmap.wrapHardwareBuffer(hardwareBuffer, ColorSpace.get(ColorSpace.Named.SRGB));
                hardwareBuffer.close();
            } else {
                Image.Plane[] planes = image.getPlanes();
                if (planes.length == 1) {
                    Image.Plane plane = planes[0];
                    int rowStride = plane.getRowStride() / plane.getPixelStride();
                    int height = this.currentImage.getHeight();
                    Bitmap bitmap = this.currentBitmap;
                    if (bitmap == null || bitmap.getWidth() != rowStride || this.currentBitmap.getHeight() != height) {
                        this.currentBitmap = Bitmap.createBitmap(rowStride, height, Bitmap.Config.ARGB_8888);
                    }
                    ByteBuffer buffer = plane.getBuffer();
                    buffer.rewind();
                    this.currentBitmap.copyPixelsFromBuffer(buffer);
                }
            }
        }
        Bitmap bitmap2 = this.currentBitmap;
        if (bitmap2 != null) {
            canvas.drawBitmap(bitmap2, 0.0f, 0.0f, (Paint) null);
        }
    }

    @Override // android.view.View
    public final void onSizeChanged(int i10, int i11, int i12, int i13) {
        if (i10 == this.f27844a.getWidth() && i11 == this.f27844a.getHeight()) {
            return;
        }
        if (this.f27845b == SurfaceKind.f27847a && this.f27846c) {
            e(i10, i11);
            this.flutterRenderer.i(this.f27844a.getSurface());
        }
    }

    @Override // io.flutter.embedding.engine.renderer.l
    public final void pause() {
    }

    @Override // io.flutter.embedding.engine.renderer.l
    public final void resume() {
    }

    public FlutterImageView(@NonNull Context context) {
        this(context, 1, 1, SurfaceKind.f27847a);
    }

    public FlutterImageView(@NonNull Context context, @NonNull AttributeSet attributeSet) {
        this(context, 1, 1, SurfaceKind.f27847a);
    }
}
