package com.google.android.gms.internal.ads;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import android.os.HandlerThread;
import androidx.annotation.Nullable;
import androidx.media3.common.C;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.concurrent.CountDownLatch;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;

/* JADX INFO: loaded from: classes2.dex */
public final class a40 extends Thread implements SurfaceTexture.OnFrameAvailableListener, y30 {

    /* JADX INFO: renamed from: e0, reason: collision with root package name */
    public static final float[] f8476e0 = {-1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f};
    public SurfaceTexture Q;
    public SurfaceTexture R;
    public int S;
    public int T;
    public int U;
    public final FloatBuffer V;
    public final CountDownLatch W;
    public final Object X;
    public EGL10 Y;
    public EGLDisplay Z;

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

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    public EGLContext f8478a0;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final float[] f8479b;

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    public EGLSurface f8480b0;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final float[] f8481c;

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    public volatile boolean f8482c0;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final float[] f8483d;

    /* JADX INFO: renamed from: d0, reason: collision with root package name */
    public volatile boolean f8484d0;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final float[] f8485e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final float[] f8486f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final float[] f8487g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final float[] f8488h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public float f8489i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public float f8490j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public float f8491k;

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public int f8493m;

    public a40(Context context) {
        super("SphericalVideoProcessor");
        float[] fArr = f8476e0;
        FloatBuffer floatBufferAsFloatBuffer = ByteBuffer.allocateDirect(48).order(ByteOrder.nativeOrder()).asFloatBuffer();
        this.V = floatBufferAsFloatBuffer;
        floatBufferAsFloatBuffer.put(fArr).position(0);
        this.f8479b = new float[9];
        this.f8481c = new float[9];
        this.f8483d = new float[9];
        this.f8485e = new float[9];
        this.f8486f = new float[9];
        this.f8487g = new float[9];
        this.f8488h = new float[9];
        this.f8489i = Float.NaN;
        z30 z30Var = new z30(context);
        this.f8477a = z30Var;
        z30Var.f19079h = this;
        this.W = new CountDownLatch(1);
        this.X = new Object();
    }

    public static final void e(String str) {
        int iGlGetError = GLES20.glGetError();
        if (iGlGetError != 0) {
            new StringBuilder(str.length() + 10 + String.valueOf(iGlGetError).length());
        }
    }

    public static final void f(float[] fArr, float[] fArr2, float[] fArr3) {
        float f10 = fArr2[0] * fArr3[0];
        float f11 = fArr2[1];
        float f12 = fArr3[3];
        float f13 = fArr2[2];
        float f14 = fArr3[6];
        fArr[0] = f10 + (f11 * f12) + (f13 * f14);
        float f15 = fArr2[0];
        float f16 = fArr3[1] * f15;
        float f17 = fArr3[4];
        float f18 = fArr3[7];
        fArr[1] = f16 + (f11 * f17) + (f13 * f18);
        float f19 = f15 * fArr3[2];
        float f20 = fArr2[1];
        float f21 = fArr3[5];
        float f22 = fArr3[8];
        fArr[2] = f19 + (f20 * f21) + (f13 * f22);
        float f23 = fArr2[3];
        float f24 = fArr3[0];
        float f25 = fArr2[4];
        float f26 = fArr2[5];
        fArr[3] = (f23 * f24) + (f12 * f25) + (f26 * f14);
        float f27 = fArr2[3];
        float f28 = fArr3[1];
        fArr[4] = (f27 * f28) + (f25 * f17) + (f26 * f18);
        float f29 = fArr3[2];
        fArr[5] = (f27 * f29) + (fArr2[4] * f21) + (f26 * f22);
        float f30 = fArr2[6] * f24;
        float f31 = fArr2[7];
        float f32 = fArr3[3] * f31;
        float f33 = fArr2[8];
        fArr[6] = f30 + f32 + (f14 * f33);
        float f34 = fArr2[6];
        float f35 = f18 * f33;
        fArr[7] = f35 + (f31 * fArr3[4]) + (f28 * f34);
        fArr[8] = (f34 * f29) + (fArr2[7] * fArr3[5]) + (f33 * f22);
    }

    public static final void g(float[] fArr, float f10) {
        fArr[0] = 1.0f;
        fArr[1] = 0.0f;
        fArr[2] = 0.0f;
        fArr[3] = 0.0f;
        double d10 = f10;
        fArr[4] = (float) Math.cos(d10);
        fArr[5] = (float) (-Math.sin(d10));
        fArr[6] = 0.0f;
        fArr[7] = (float) Math.sin(d10);
        fArr[8] = (float) Math.cos(d10);
    }

    public static final void h(float[] fArr, float f10) {
        double d10 = f10;
        fArr[0] = (float) Math.cos(d10);
        fArr[1] = (float) (-Math.sin(d10));
        fArr[2] = 0.0f;
        fArr[3] = (float) Math.sin(d10);
        fArr[4] = (float) Math.cos(d10);
        fArr[5] = 0.0f;
        fArr[6] = 0.0f;
        fArr[7] = 0.0f;
        fArr[8] = 1.0f;
    }

    public static final int i(int i10, String str) {
        int iGlCreateShader = GLES20.glCreateShader(i10);
        e("createShader");
        if (iGlCreateShader != 0) {
            GLES20.glShaderSource(iGlCreateShader, str);
            e("shaderSource");
            GLES20.glCompileShader(iGlCreateShader);
            e("compileShader");
            int[] iArr = new int[1];
            GLES20.glGetShaderiv(iGlCreateShader, 35713, iArr, 0);
            e("getShaderiv");
            if (iArr[0] == 0) {
                new StringBuilder(String.valueOf(i10).length() + 26);
                GLES20.glGetShaderInfoLog(iGlCreateShader);
                GLES20.glDeleteShader(iGlCreateShader);
                e("deleteShader");
                return 0;
            }
        }
        return iGlCreateShader;
    }

    public final void a(int i10, int i11) {
        Object obj = this.X;
        synchronized (obj) {
            this.f8493m = i10;
            this.f8492l = i11;
            this.f8482c0 = true;
            obj.notifyAll();
        }
    }

    public final void b() {
        Object obj = this.X;
        synchronized (obj) {
            this.f8484d0 = true;
            this.R = null;
            obj.notifyAll();
        }
    }

    public final void c(float f10, float f11) {
        int i10 = this.f8493m;
        int i11 = this.f8492l;
        if (i10 <= i11) {
            i10 = i11;
        }
        float f12 = i10;
        this.f8490j -= (f10 * 1.7453293f) / f12;
        float f13 = this.f8491k - ((f11 * 1.7453293f) / f12);
        this.f8491k = f13;
        if (f13 < -1.5707964f) {
            this.f8491k = -1.5707964f;
            f13 = -1.5707964f;
        }
        if (f13 > 1.5707964f) {
            this.f8491k = 1.5707964f;
        }
    }

    public final void d() {
        EGLSurface eGLSurface;
        EGLSurface eGLSurface2 = this.f8480b0;
        if (eGLSurface2 != null && eGLSurface2 != (eGLSurface = EGL10.EGL_NO_SURFACE)) {
            this.Y.eglMakeCurrent(this.Z, eGLSurface, eGLSurface, EGL10.EGL_NO_CONTEXT);
            this.Y.eglDestroySurface(this.Z, this.f8480b0);
            this.f8480b0 = null;
        }
        EGLContext eGLContext = this.f8478a0;
        if (eGLContext != null) {
            this.Y.eglDestroyContext(this.Z, eGLContext);
            this.f8478a0 = null;
        }
        EGLDisplay eGLDisplay = this.Z;
        if (eGLDisplay != null) {
            this.Y.eglTerminate(eGLDisplay);
            this.Z = null;
        }
    }

    @Override // android.graphics.SurfaceTexture.OnFrameAvailableListener
    public final void onFrameAvailable(SurfaceTexture surfaceTexture) {
        this.U++;
        Object obj = this.X;
        synchronized (obj) {
            obj.notifyAll();
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:37:0x00b1  */
    /* JADX WARN: Removed duplicated region for block: B:6:0x001e  */
    @Override // java.lang.Thread, java.lang.Runnable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void run() {
        boolean z10;
        int iGlCreateProgram;
        if (this.R == null) {
            q7.j.b("SphericalVideoProcessor started with no output texture.");
            this.W.countDown();
            return;
        }
        EGL10 egl10 = (EGL10) EGLContext.getEGL();
        this.Y = egl10;
        EGLDisplay eGLDisplayEglGetDisplay = egl10.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
        this.Z = eGLDisplayEglGetDisplay;
        if (eGLDisplayEglGetDisplay == EGL10.EGL_NO_DISPLAY) {
            z10 = false;
        } else {
            if (this.Y.eglInitialize(eGLDisplayEglGetDisplay, new int[2])) {
                int[] iArr = new int[1];
                EGLConfig[] eGLConfigArr = new EGLConfig[1];
                EGLConfig eGLConfig = (this.Y.eglChooseConfig(this.Z, new int[]{12352, 4, 12324, 8, 12323, 8, 12322, 8, 12325, 16, 12344}, eGLConfigArr, 1, iArr) && iArr[0] > 0) ? eGLConfigArr[0] : null;
                if (eGLConfig != null) {
                    EGL10 egl102 = this.Y;
                    EGLDisplay eGLDisplay = this.Z;
                    EGLContext eGLContext = EGL10.EGL_NO_CONTEXT;
                    EGLContext eGLContextEglCreateContext = egl102.eglCreateContext(eGLDisplay, eGLConfig, eGLContext, new int[]{12440, 2, 12344});
                    this.f8478a0 = eGLContextEglCreateContext;
                    if (eGLContextEglCreateContext != null && eGLContextEglCreateContext != eGLContext) {
                        EGLSurface eGLSurfaceEglCreateWindowSurface = this.Y.eglCreateWindowSurface(this.Z, eGLConfig, this.R, null);
                        this.f8480b0 = eGLSurfaceEglCreateWindowSurface;
                        if (eGLSurfaceEglCreateWindowSurface != null && eGLSurfaceEglCreateWindowSurface != EGL10.EGL_NO_SURFACE && this.Y.eglMakeCurrent(this.Z, eGLSurfaceEglCreateWindowSurface, eGLSurfaceEglCreateWindowSurface, this.f8478a0)) {
                            z10 = true;
                        }
                    }
                }
            }
        }
        tn tnVar = ao.H1;
        n7.y yVar = n7.y.f31093e;
        int i10 = i(35633, !((String) yVar.f31096c.b(tnVar)).equals(tnVar.b()) ? (String) yVar.f31096c.b(tnVar) : "attribute highp vec3 aPosition;varying vec3 pos;void main() {  gl_Position = vec4(aPosition, 1.0);  pos = aPosition;}");
        if (i10 == 0) {
            iGlCreateProgram = 0;
        } else {
            tn tnVar2 = ao.I1;
            int i11 = i(35632, !((String) yVar.f31096c.b(tnVar2)).equals(tnVar2.b()) ? (String) yVar.f31096c.b(tnVar2) : "#extension GL_OES_EGL_image_external : require\n#define INV_PI 0.3183\nprecision highp float;varying vec3 pos;uniform samplerExternalOES uSplr;uniform mat3 uVMat;uniform float uFOVx;uniform float uFOVy;void main() {  vec3 ray = vec3(pos.x * tan(uFOVx), pos.y * tan(uFOVy), -1);  ray = (uVMat * ray).xyz;  ray = normalize(ray);  vec2 texCrd = vec2(    0.5 + atan(ray.x, - ray.z) * INV_PI * 0.5, acos(ray.y) * INV_PI);  gl_FragColor = vec4(texture2D(uSplr, texCrd).xyz, 1.0);}");
            if (i11 != 0) {
                iGlCreateProgram = GLES20.glCreateProgram();
                e("createProgram");
                if (iGlCreateProgram != 0) {
                    GLES20.glAttachShader(iGlCreateProgram, i10);
                    e("attachShader");
                    GLES20.glAttachShader(iGlCreateProgram, i11);
                    e("attachShader");
                    GLES20.glLinkProgram(iGlCreateProgram);
                    e("linkProgram");
                    int[] iArr2 = new int[1];
                    GLES20.glGetProgramiv(iGlCreateProgram, 35714, iArr2, 0);
                    e("getProgramiv");
                    if (iArr2[0] != 1) {
                        GLES20.glGetProgramInfoLog(iGlCreateProgram);
                        GLES20.glDeleteProgram(iGlCreateProgram);
                        e("deleteProgram");
                        iGlCreateProgram = 0;
                    } else {
                        GLES20.glValidateProgram(iGlCreateProgram);
                        e("validateProgram");
                    }
                }
            }
        }
        this.S = iGlCreateProgram;
        GLES20.glUseProgram(iGlCreateProgram);
        e("useProgram");
        int iGlGetAttribLocation = GLES20.glGetAttribLocation(this.S, "aPosition");
        GLES20.glVertexAttribPointer(iGlGetAttribLocation, 3, 5126, false, 12, (Buffer) this.V);
        e("vertexAttribPointer");
        GLES20.glEnableVertexAttribArray(iGlGetAttribLocation);
        e("enableVertexAttribArray");
        int[] iArr3 = new int[1];
        GLES20.glGenTextures(1, iArr3, 0);
        e("genTextures");
        int i12 = iArr3[0];
        GLES20.glBindTexture(36197, i12);
        e("bindTextures");
        GLES20.glTexParameteri(36197, 10240, C.TEXTURE_MIN_FILTER_LINEAR);
        e("texParameteri");
        GLES20.glTexParameteri(36197, 10241, C.TEXTURE_MIN_FILTER_LINEAR);
        e("texParameteri");
        GLES20.glTexParameteri(36197, 10242, 33071);
        e("texParameteri");
        GLES20.glTexParameteri(36197, 10243, 33071);
        e("texParameteri");
        int iGlGetUniformLocation = GLES20.glGetUniformLocation(this.S, "uVMat");
        this.T = iGlGetUniformLocation;
        GLES20.glUniformMatrix3fv(iGlGetUniformLocation, 1, false, new float[]{1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}, 0);
        int i13 = this.S;
        if (!z10 || i13 == 0) {
            String strConcat = "EGL initialization failed: ".concat(String.valueOf(GLUtils.getEGLErrorString(this.Y.eglGetError())));
            q7.j.b(strConcat);
            m7.n.C.f30469h.d("SphericalVideoProcessor.run.1", new Throwable(strConcat));
            d();
            this.W.countDown();
            return;
        }
        SurfaceTexture surfaceTexture = new SurfaceTexture(i12);
        this.Q = surfaceTexture;
        surfaceTexture.setOnFrameAvailableListener(this);
        this.W.countDown();
        z30 z30Var = this.f8477a;
        if (z30Var.f19078g == null) {
            SensorManager sensorManager = z30Var.f19072a;
            Sensor defaultSensor = sensorManager.getDefaultSensor(11);
            if (defaultSensor == null) {
                q7.j.b("No Sensor of TYPE_ROTATION_VECTOR");
            } else {
                HandlerThread handlerThread = new HandlerThread("OrientationMonitor");
                handlerThread.start();
                vi1 vi1Var = new vi1(handlerThread.getLooper(), 0);
                z30Var.f19078g = vi1Var;
                if (!sensorManager.registerListener(z30Var, defaultSensor, 0, vi1Var)) {
                    q7.j.b("SensorManager.registerListener failed.");
                    z30Var.b();
                }
            }
        }
        try {
            try {
                try {
                    this.f8482c0 = true;
                    while (!this.f8484d0) {
                        while (this.U > 0) {
                            this.Q.updateTexImage();
                            this.U--;
                        }
                        float[] fArr = this.f8479b;
                        if (z30Var.c(fArr)) {
                            if (Float.isNaN(this.f8489i)) {
                                float[] fArr2 = {0.0f, 1.0f, 0.0f};
                                float f10 = fArr[0];
                                float f11 = fArr2[0];
                                float f12 = fArr[1];
                                float f13 = fArr2[1];
                                float f14 = (fArr[2] * 0.0f) + (f12 * f13) + (f10 * f11);
                                float f15 = (fArr[5] * 0.0f) + (fArr[3] * f11) + (fArr[4] * f13);
                                float f16 = fArr[6];
                                float f17 = fArr[7];
                                float f18 = fArr[8];
                                this.f8489i = -(((float) Math.atan2(f15, f14)) - 1.5707964f);
                            }
                            h(this.f8487g, this.f8489i + this.f8490j);
                        } else {
                            g(fArr, -1.5707964f);
                            h(this.f8487g, this.f8490j);
                        }
                        float[] fArr3 = this.f8481c;
                        g(fArr3, 1.5707964f);
                        float[] fArr4 = this.f8483d;
                        f(fArr4, this.f8487g, fArr3);
                        float[] fArr5 = this.f8485e;
                        f(fArr5, fArr, fArr4);
                        float[] fArr6 = this.f8486f;
                        g(fArr6, this.f8491k);
                        float[] fArr7 = this.f8488h;
                        f(fArr7, fArr6, fArr5);
                        GLES20.glUniformMatrix3fv(this.T, 1, false, fArr7, 0);
                        GLES20.glDrawArrays(5, 0, 4);
                        e("drawArrays");
                        GLES20.glFinish();
                        this.Y.eglSwapBuffers(this.Z, this.f8480b0);
                        if (this.f8482c0) {
                            GLES20.glViewport(0, 0, this.f8493m, this.f8492l);
                            e("viewport");
                            int iGlGetUniformLocation2 = GLES20.glGetUniformLocation(this.S, "uFOVx");
                            int iGlGetUniformLocation3 = GLES20.glGetUniformLocation(this.S, "uFOVy");
                            int i14 = this.f8493m;
                            int i15 = this.f8492l;
                            if (i14 > i15) {
                                GLES20.glUniform1f(iGlGetUniformLocation2, 0.87266463f);
                                GLES20.glUniform1f(iGlGetUniformLocation3, (this.f8492l * 0.87266463f) / this.f8493m);
                            } else {
                                GLES20.glUniform1f(iGlGetUniformLocation2, (i14 * 0.87266463f) / i15);
                                GLES20.glUniform1f(iGlGetUniformLocation3, 0.87266463f);
                            }
                            this.f8482c0 = false;
                        }
                        try {
                            Object obj = this.X;
                            synchronized (obj) {
                                try {
                                    if (!this.f8484d0 && !this.f8482c0 && this.U == 0) {
                                        obj.wait();
                                    }
                                } catch (Throwable th) {
                                    throw th;
                                }
                            }
                        } catch (InterruptedException unused) {
                        }
                    }
                } catch (IllegalStateException unused2) {
                    q7.j.d("SphericalVideoProcessor halted unexpectedly.");
                }
            } catch (Throwable th2) {
                m7.n.C.f30469h.d("SphericalVideoProcessor.run.2", th2);
            }
            this.f8477a.b();
            this.Q.setOnFrameAvailableListener(null);
            this.Q = null;
            d();
        } catch (Throwable th3) {
            this.f8477a.b();
            this.Q.setOnFrameAvailableListener(null);
            this.Q = null;
            d();
            throw th3;
        }
    }

    @Nullable
    public final SurfaceTexture zze() {
        if (this.R == null) {
            return null;
        }
        try {
            this.W.await();
        } catch (InterruptedException unused) {
        }
        return this.Q;
    }
}
