package defpackage;

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 androidx.media3.common.util.GlUtil$GlException;
import java.util.Locale;

/* JADX INFO: renamed from: ˏᵺˊⁱ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class HandlerThreadC1926 extends HandlerThread implements Handler.Callback {

    /* JADX INFO: renamed from: ˌᴵ, reason: contains not printable characters */
    public RunnableC1733 f7678;

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public Error f7679;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public Handler f7680;

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public RuntimeException f7681;

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public C2048 f7682;

    @Override // android.os.Handler.Callback
    public final boolean handleMessage(Message message) {
        int i = message.what;
        try {
            if (i == 1) {
                try {
                    m5165(message.arg1);
                    synchronized (this) {
                        notify();
                    }
                    return true;
                } catch (GlUtil$GlException e) {
                    ˋˀᵧ.ˋˈ("Failed to initialize placeholder surface", e);
                    this.f7681 = new IllegalStateException(e);
                    synchronized (this) {
                        notify();
                    }
                } catch (Error e2) {
                    ˋˀᵧ.ˋˈ("Failed to initialize placeholder surface", e2);
                    this.f7679 = e2;
                    synchronized (this) {
                        notify();
                    }
                } catch (RuntimeException e3) {
                    ˋˀᵧ.ˋˈ("Failed to initialize placeholder surface", e3);
                    this.f7681 = e3;
                    synchronized (this) {
                        notify();
                    }
                }
            } else if (i == 2) {
                try {
                    m5164();
                    return true;
                } catch (Throwable th) {
                    try {
                        ˋˀᵧ.ˋˈ("Failed to release placeholder surface", th);
                        return true;
                    } finally {
                        quit();
                    }
                }
            }
            return true;
        } catch (Throwable th2) {
            synchronized (this) {
                notify();
                throw th2;
            }
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final void m5164() {
        this.f7678.getClass();
        RunnableC1733 runnableC1733 = this.f7678;
        runnableC1733.f6938.removeCallbacks(runnableC1733);
        try {
            SurfaceTexture surfaceTexture = runnableC1733.f6943;
            if (surfaceTexture != null) {
                surfaceTexture.release();
                GLES20.glDeleteTextures(1, runnableC1733.f6940, 0);
            }
        } finally {
            EGLDisplay eGLDisplay = runnableC1733.f6939;
            if (eGLDisplay != null && !eGLDisplay.equals(EGL14.EGL_NO_DISPLAY)) {
                EGLDisplay eGLDisplay2 = runnableC1733.f6939;
                EGLSurface eGLSurface = EGL14.EGL_NO_SURFACE;
                EGL14.eglMakeCurrent(eGLDisplay2, eGLSurface, eGLSurface, EGL14.EGL_NO_CONTEXT);
            }
            EGLSurface eGLSurface2 = runnableC1733.f6942;
            if (eGLSurface2 != null && !eGLSurface2.equals(EGL14.EGL_NO_SURFACE)) {
                EGL14.eglDestroySurface(runnableC1733.f6939, runnableC1733.f6942);
            }
            EGLContext eGLContext = runnableC1733.f6941;
            if (eGLContext != null) {
                EGL14.eglDestroyContext(runnableC1733.f6939, eGLContext);
            }
            EGL14.eglReleaseThread();
            EGLDisplay eGLDisplay3 = runnableC1733.f6939;
            if (eGLDisplay3 != null && !eGLDisplay3.equals(EGL14.EGL_NO_DISPLAY)) {
                EGL14.eglTerminate(runnableC1733.f6939);
            }
            runnableC1733.f6939 = null;
            runnableC1733.f6941 = null;
            runnableC1733.f6942 = null;
            runnableC1733.f6943 = null;
        }
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final void m5165(int i) {
        EGLSurface eGLSurfaceEglCreatePbufferSurface;
        this.f7678.getClass();
        RunnableC1733 runnableC1733 = this.f7678;
        int[] iArr = runnableC1733.f6940;
        EGLDisplay eGLDisplayEglGetDisplay = EGL14.eglGetDisplay(0);
        ᵺᵘ.ᵻʽᵯ("eglGetDisplay failed", eGLDisplayEglGetDisplay != null);
        int[] iArr2 = new int[2];
        ᵺᵘ.ᵻʽᵯ("eglInitialize failed", EGL14.eglInitialize(eGLDisplayEglGetDisplay, iArr2, 0, iArr2, 1));
        runnableC1733.f6939 = eGLDisplayEglGetDisplay;
        EGLConfig[] eGLConfigArr = new EGLConfig[1];
        int[] iArr3 = new int[1];
        boolean zEglChooseConfig = EGL14.eglChooseConfig(eGLDisplayEglGetDisplay, RunnableC1733.f6937, 0, eGLConfigArr, 0, 1, iArr3, 0);
        boolean z = zEglChooseConfig && iArr3[0] > 0 && eGLConfigArr[0] != null;
        Object[] objArr = {Boolean.valueOf(zEglChooseConfig), Integer.valueOf(iArr3[0]), eGLConfigArr[0]};
        String str = AbstractC0956.f4222;
        ᵺᵘ.ᵻʽᵯ(String.format(Locale.US, "eglChooseConfig failed: success=%b, numConfigs[0]=%d, configs[0]=%s", objArr), z);
        EGLConfig eGLConfig = eGLConfigArr[0];
        EGLContext eGLContextEglCreateContext = EGL14.eglCreateContext(runnableC1733.f6939, eGLConfig, EGL14.EGL_NO_CONTEXT, i == 0 ? new int[]{12440, 2, 12344} : new int[]{12440, 2, 12992, 1, 12344}, 0);
        ᵺᵘ.ᵻʽᵯ("eglCreateContext failed", eGLContextEglCreateContext != null);
        runnableC1733.f6941 = eGLContextEglCreateContext;
        EGLDisplay eGLDisplay = runnableC1733.f6939;
        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);
            ᵺᵘ.ᵻʽᵯ("eglCreatePbufferSurface failed", eGLSurfaceEglCreatePbufferSurface != null);
        }
        ᵺᵘ.ᵻʽᵯ("eglMakeCurrent failed", EGL14.eglMakeCurrent(eGLDisplay, eGLSurfaceEglCreatePbufferSurface, eGLSurfaceEglCreatePbufferSurface, eGLContextEglCreateContext));
        runnableC1733.f6942 = eGLSurfaceEglCreatePbufferSurface;
        GLES20.glGenTextures(1, iArr, 0);
        ᵺᵘ.ˊᵫᵗ();
        SurfaceTexture surfaceTexture = new SurfaceTexture(iArr[0]);
        runnableC1733.f6943 = surfaceTexture;
        surfaceTexture.setOnFrameAvailableListener(runnableC1733);
        SurfaceTexture surfaceTexture2 = this.f7678.f6943;
        surfaceTexture2.getClass();
        this.f7682 = new C2048(this, surfaceTexture2, i != 0);
    }
}
