package R0;

import android.graphics.SurfaceTexture;
import android.opengl.EGL14;
import android.opengl.EGLConfig;
import android.opengl.EGLContext;
import android.opengl.EGLDisplay;
import android.opengl.EGLSurface;
import android.opengl.GLES20;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import java.util.Locale;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class k extends HandlerThread implements Handler.Callback {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public x0.e f5941q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public Handler f5942r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public Error f5943s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public RuntimeException f5944t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public l f5945u;

    public final void a(int i) throws x0.f {
        EGLSurface eGLSurfaceEglCreatePbufferSurface;
        this.f5941q.getClass();
        x0.e eVar = this.f5941q;
        eVar.getClass();
        EGLDisplay eGLDisplayEglGetDisplay = EGL14.eglGetDisplay(0);
        AbstractC1709a.f("eglGetDisplay failed", eGLDisplayEglGetDisplay != null);
        int[] iArr = new int[2];
        AbstractC1709a.f("eglInitialize failed", EGL14.eglInitialize(eGLDisplayEglGetDisplay, iArr, 0, iArr, 1));
        eVar.f18509s = eGLDisplayEglGetDisplay;
        EGLConfig[] eGLConfigArr = new EGLConfig[1];
        int[] iArr2 = new int[1];
        boolean zEglChooseConfig = EGL14.eglChooseConfig(eGLDisplayEglGetDisplay, x0.e.f18506w, 0, eGLConfigArr, 0, 1, iArr2, 0);
        boolean z8 = zEglChooseConfig && iArr2[0] > 0 && eGLConfigArr[0] != null;
        Object[] objArr = {Boolean.valueOf(zEglChooseConfig), Integer.valueOf(iArr2[0]), eGLConfigArr[0]};
        int i5 = x0.v.f18553a;
        AbstractC1709a.f(String.format(Locale.US, "eglChooseConfig failed: success=%b, numConfigs[0]=%d, configs[0]=%s", objArr), z8);
        EGLConfig eGLConfig = eGLConfigArr[0];
        EGLContext eGLContextEglCreateContext = EGL14.eglCreateContext(eVar.f18509s, eGLConfig, EGL14.EGL_NO_CONTEXT, i == 0 ? new int[]{12440, 2, 12344} : new int[]{12440, 2, 12992, 1, 12344}, 0);
        AbstractC1709a.f("eglCreateContext failed", eGLContextEglCreateContext != null);
        eVar.f18510t = eGLContextEglCreateContext;
        EGLDisplay eGLDisplay = eVar.f18509s;
        if (i == 1) {
            eGLSurfaceEglCreatePbufferSurface = EGL14.EGL_NO_SURFACE;
        } else {
            eGLSurfaceEglCreatePbufferSurface = EGL14.eglCreatePbufferSurface(eGLDisplay, eGLConfig, i == 2 ? new int[]{12375, 1, 12374, 1, 12992, 1, 12344} : new int[]{12375, 1, 12374, 1, 12344}, 0);
            AbstractC1709a.f("eglCreatePbufferSurface failed", eGLSurfaceEglCreatePbufferSurface != null);
        }
        AbstractC1709a.f("eglMakeCurrent failed", EGL14.eglMakeCurrent(eGLDisplay, eGLSurfaceEglCreatePbufferSurface, eGLSurfaceEglCreatePbufferSurface, eGLContextEglCreateContext));
        eVar.f18511u = eGLSurfaceEglCreatePbufferSurface;
        int[] iArr3 = eVar.f18508r;
        GLES20.glGenTextures(1, iArr3, 0);
        AbstractC1709a.e();
        SurfaceTexture surfaceTexture = new SurfaceTexture(iArr3[0]);
        eVar.f18512v = surfaceTexture;
        surfaceTexture.setOnFrameAvailableListener(eVar);
        SurfaceTexture surfaceTexture2 = this.f5941q.f18512v;
        surfaceTexture2.getClass();
        this.f5945u = new l(this, surfaceTexture2, i != 0);
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final void b() {
        this.f5941q.getClass();
        x0.e eVar = this.f5941q;
        eVar.f18507q.removeCallbacks(eVar);
        try {
            SurfaceTexture surfaceTexture = eVar.f18512v;
            if (surfaceTexture != null) {
                surfaceTexture.release();
                GLES20.glDeleteTextures(1, eVar.f18508r, 0);
            }
        } finally {
            EGLDisplay eGLDisplay = eVar.f18509s;
            if (eGLDisplay != null && !eGLDisplay.equals(EGL14.EGL_NO_DISPLAY)) {
                EGLDisplay eGLDisplay2 = eVar.f18509s;
                EGLSurface eGLSurface = EGL14.EGL_NO_SURFACE;
                EGL14.eglMakeCurrent(eGLDisplay2, eGLSurface, eGLSurface, EGL14.EGL_NO_CONTEXT);
            }
            EGLSurface eGLSurface2 = eVar.f18511u;
            if (eGLSurface2 != null && !eGLSurface2.equals(EGL14.EGL_NO_SURFACE)) {
                EGL14.eglDestroySurface(eVar.f18509s, eVar.f18511u);
            }
            EGLContext eGLContext = eVar.f18510t;
            if (eGLContext != null) {
                EGL14.eglDestroyContext(eVar.f18509s, eGLContext);
            }
            EGL14.eglReleaseThread();
            EGLDisplay eGLDisplay3 = eVar.f18509s;
            if (eGLDisplay3 != null && !eGLDisplay3.equals(EGL14.EGL_NO_DISPLAY)) {
                EGL14.eglTerminate(eVar.f18509s);
            }
            eVar.f18509s = null;
            eVar.f18510t = null;
            eVar.f18511u = null;
            eVar.f18512v = null;
        }
    }

    @Override // android.os.Handler.Callback
    public final boolean handleMessage(Message message) {
        int i = message.what;
        try {
            if (i == 1) {
                try {
                    a(message.arg1);
                    synchronized (this) {
                        notify();
                    }
                    return true;
                } catch (Error e8) {
                    AbstractC1709a.p("PlaceholderSurface", "Failed to initialize placeholder surface", e8);
                    this.f5943s = e8;
                    synchronized (this) {
                        notify();
                    }
                } catch (RuntimeException e9) {
                    AbstractC1709a.p("PlaceholderSurface", "Failed to initialize placeholder surface", e9);
                    this.f5944t = e9;
                    synchronized (this) {
                        notify();
                    }
                } catch (x0.f e10) {
                    AbstractC1709a.p("PlaceholderSurface", "Failed to initialize placeholder surface", e10);
                    this.f5944t = new IllegalStateException(e10);
                    synchronized (this) {
                        notify();
                    }
                }
            } else if (i == 2) {
                try {
                    b();
                    return true;
                } catch (Throwable th) {
                    try {
                        AbstractC1709a.p("PlaceholderSurface", "Failed to release placeholder surface", th);
                    } finally {
                        quit();
                    }
                }
            }
            return true;
        } catch (Throwable th2) {
            synchronized (this) {
                notify();
                throw th2;
            }
        }
    }
}
