package com.applovin.impl; 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; /* loaded from: classes.dex */ public final class n7 implements SurfaceTexture.OnFrameAvailableListener, Runnable { private static final int[] h = {12352, 4, 12324, 8, 12323, 8, 12322, 8, 12321, 8, 12325, 0, 12327, 12344, 12339, 4, 12344}; private final Handler a; private final int[] b; private EGLDisplay c; private EGLContext d; private EGLSurface f; private SurfaceTexture g; public interface c { } private void a() { } public static final class b extends RuntimeException { private b(String str) { super(str); } } public n7(Handler handler) { this(handler, null); } /* JADX WARN: Multi-variable type inference failed */ public void d() { this.a.removeCallbacks(this); try { SurfaceTexture surfaceTexture = this.g; if (surfaceTexture != null) { surfaceTexture.release(); GLES20.glDeleteTextures(1, this.b, 0); } } finally { EGLDisplay eGLDisplay = this.c; if (eGLDisplay != null && !eGLDisplay.equals(EGL14.EGL_NO_DISPLAY)) { EGLDisplay eGLDisplay2 = this.c; EGLSurface eGLSurface = EGL14.EGL_NO_SURFACE; EGL14.eglMakeCurrent(eGLDisplay2, eGLSurface, eGLSurface, EGL14.EGL_NO_CONTEXT); } EGLSurface eGLSurface2 = this.f; if (eGLSurface2 != null && !eGLSurface2.equals(EGL14.EGL_NO_SURFACE)) { EGL14.eglDestroySurface(this.c, this.f); } EGLContext eGLContext = this.d; if (eGLContext != null) { EGL14.eglDestroyContext(this.c, eGLContext); } if (xp.a >= 19) { EGL14.eglReleaseThread(); } EGLDisplay eGLDisplay3 = this.c; if (eGLDisplay3 != null && !eGLDisplay3.equals(EGL14.EGL_NO_DISPLAY)) { EGL14.eglTerminate(this.c); } this.c = null; this.d = null; this.f = null; this.g = null; } } public SurfaceTexture c() { return (SurfaceTexture) b1.a(this.g); } @Override // android.graphics.SurfaceTexture.OnFrameAvailableListener public void onFrameAvailable(SurfaceTexture surfaceTexture) { this.a.post(this); } @Override // java.lang.Runnable public void run() { a(); SurfaceTexture surfaceTexture = this.g; if (surfaceTexture != null) { try { surfaceTexture.updateTexImage(); } catch (RuntimeException unused) { } } } private static EGLDisplay b() { EGLDisplay eglGetDisplay = EGL14.eglGetDisplay(0); if (eglGetDisplay != null) { int[] iArr = new int[2]; if (EGL14.eglInitialize(eglGetDisplay, iArr, 0, iArr, 1)) { return eglGetDisplay; } throw new b("eglInitialize failed"); } throw new b("eglGetDisplay failed"); } public n7(Handler handler, c cVar) { this.a = handler; this.b = new int[1]; } private static EGLConfig a(EGLDisplay eGLDisplay) { EGLConfig eGLConfig; EGLConfig[] eGLConfigArr = new EGLConfig[1]; int[] iArr = new int[1]; boolean eglChooseConfig = EGL14.eglChooseConfig(eGLDisplay, h, 0, eGLConfigArr, 0, 1, iArr, 0); if (!eglChooseConfig || iArr[0] <= 0 || (eGLConfig = eGLConfigArr[0]) == null) { throw new b(xp.a("eglChooseConfig failed: success=%b, numConfigs[0]=%d, configs[0]=%s", Boolean.valueOf(eglChooseConfig), Integer.valueOf(iArr[0]), eGLConfigArr[0])); } return eGLConfig; } private static EGLContext a(EGLDisplay eGLDisplay, EGLConfig eGLConfig, int i) { EGLContext eglCreateContext = EGL14.eglCreateContext(eGLDisplay, eGLConfig, EGL14.EGL_NO_CONTEXT, i == 0 ? new int[]{12440, 2, 12344} : new int[]{12440, 2, 12992, 1, 12344}, 0); if (eglCreateContext != null) { return eglCreateContext; } throw new b("eglCreateContext failed"); } private static EGLSurface a(EGLDisplay eGLDisplay, EGLConfig eGLConfig, EGLContext eGLContext, int i) { EGLSurface eglCreatePbufferSurface; if (i == 1) { eglCreatePbufferSurface = EGL14.EGL_NO_SURFACE; } else { eglCreatePbufferSurface = EGL14.eglCreatePbufferSurface(eGLDisplay, eGLConfig, i == 2 ? new int[]{12375, 1, 12374, 1, 12992, 1, 12344} : new int[]{12375, 1, 12374, 1, 12344}, 0); if (eglCreatePbufferSurface == null) { throw new b("eglCreatePbufferSurface failed"); } } if (EGL14.eglMakeCurrent(eGLDisplay, eglCreatePbufferSurface, eglCreatePbufferSurface, eGLContext)) { return eglCreatePbufferSurface; } throw new b("eglMakeCurrent failed"); } private static void a(int[] iArr) { GLES20.glGenTextures(1, iArr, 0); ba.a(); } public void a(int i) { EGLDisplay b2 = b(); this.c = b2; EGLConfig a2 = a(b2); EGLContext a3 = a(this.c, a2, i); this.d = a3; this.f = a(this.c, a2, a3, i); a(this.b); SurfaceTexture surfaceTexture = new SurfaceTexture(this.b[0]); this.g = surfaceTexture; surfaceTexture.setOnFrameAvailableListener(this); } }