package org.webrtc; import android.graphics.SurfaceTexture; import android.opengl.EGL14; import android.opengl.EGLConfig; import android.opengl.EGLContext; import android.opengl.EGLDisplay; import android.opengl.EGLExt; import android.opengl.EGLSurface; import android.os.Build; import android.view.Surface; import io.sentry.protocol.ViewHierarchyNode; import org.webrtc.EglBase14; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes3.dex */ public class EglBase14Impl implements EglBase14 { private static final int CURRENT_SDK_VERSION = Build.VERSION.SDK_INT; private static final int EGLExt_SDK_VERSION = 18; private static final String TAG = "EglBase14Impl"; private EGLConfig eglConfig; private EGLContext eglContext; private EGLDisplay eglDisplay; private EGLSurface eglSurface = EGL14.EGL_NO_SURFACE; public static boolean isEGL14Supported() { StringBuilder sb = new StringBuilder("SDK version: "); int i = CURRENT_SDK_VERSION; Logging.d(TAG, sb.append(i).append(". isEGL14Supported: ").append(i >= 18).toString()); return i >= 18; } /* loaded from: classes3.dex */ public static class Context implements EglBase14.Context { private final EGLContext egl14Context; @Override // org.webrtc.EglBase14.Context public EGLContext getRawContext() { return this.egl14Context; } @Override // org.webrtc.EglBase.Context public long getNativeEglContext() { return EglBase14Impl.CURRENT_SDK_VERSION >= 21 ? this.egl14Context.getNativeHandle() : this.egl14Context.getHandle(); } public Context(EGLContext eGLContext) { this.egl14Context = eGLContext; } } public EglBase14Impl(EGLContext eGLContext, int[] iArr) { EGLDisplay eglDisplay = getEglDisplay(); this.eglDisplay = eglDisplay; this.eglConfig = getEglConfig(eglDisplay, iArr); int openGlesVersionFromConfig = EglBase.getOpenGlesVersionFromConfig(iArr); Logging.d(TAG, "Using OpenGL ES version " + openGlesVersionFromConfig); this.eglContext = createEglContext(eGLContext, this.eglDisplay, this.eglConfig, openGlesVersionFromConfig); } @Override // org.webrtc.EglBase public void createSurface(Surface surface) { createSurfaceInternal(surface); } @Override // org.webrtc.EglBase public void createSurface(SurfaceTexture surfaceTexture) { createSurfaceInternal(surfaceTexture); } private void createSurfaceInternal(Object obj) { if (!(obj instanceof Surface) && !(obj instanceof SurfaceTexture)) { throw new IllegalStateException("Input must be either a Surface or SurfaceTexture"); } checkIsNotReleased(); if (this.eglSurface != EGL14.EGL_NO_SURFACE) { throw new RuntimeException("Already has an EGLSurface"); } EGLSurface eglCreateWindowSurface = EGL14.eglCreateWindowSurface(this.eglDisplay, this.eglConfig, obj, new int[]{12344}, 0); this.eglSurface = eglCreateWindowSurface; if (eglCreateWindowSurface == EGL14.EGL_NO_SURFACE) { throw new RuntimeException("Failed to create window surface: 0x" + Integer.toHexString(EGL14.eglGetError())); } } @Override // org.webrtc.EglBase public void createDummyPbufferSurface() { createPbufferSurface(1, 1); } @Override // org.webrtc.EglBase public void createPbufferSurface(int i, int i2) { checkIsNotReleased(); if (this.eglSurface != EGL14.EGL_NO_SURFACE) { throw new RuntimeException("Already has an EGLSurface"); } EGLSurface eglCreatePbufferSurface = EGL14.eglCreatePbufferSurface(this.eglDisplay, this.eglConfig, new int[]{12375, i, 12374, i2, 12344}, 0); this.eglSurface = eglCreatePbufferSurface; if (eglCreatePbufferSurface == EGL14.EGL_NO_SURFACE) { throw new RuntimeException("Failed to create pixel buffer surface with size " + i + ViewHierarchyNode.JsonKeys.X + i2 + ": 0x" + Integer.toHexString(EGL14.eglGetError())); } } @Override // org.webrtc.EglBase public Context getEglBaseContext() { return new Context(this.eglContext); } @Override // org.webrtc.EglBase public boolean hasSurface() { return this.eglSurface != EGL14.EGL_NO_SURFACE; } @Override // org.webrtc.EglBase public int surfaceWidth() { int[] iArr = new int[1]; EGL14.eglQuerySurface(this.eglDisplay, this.eglSurface, 12375, iArr, 0); return iArr[0]; } @Override // org.webrtc.EglBase public int surfaceHeight() { int[] iArr = new int[1]; EGL14.eglQuerySurface(this.eglDisplay, this.eglSurface, 12374, iArr, 0); return iArr[0]; } @Override // org.webrtc.EglBase public void releaseSurface() { if (this.eglSurface != EGL14.EGL_NO_SURFACE) { EGL14.eglDestroySurface(this.eglDisplay, this.eglSurface); this.eglSurface = EGL14.EGL_NO_SURFACE; } } private void checkIsNotReleased() { if (this.eglDisplay == EGL14.EGL_NO_DISPLAY || this.eglContext == EGL14.EGL_NO_CONTEXT || this.eglConfig == null) { throw new RuntimeException("This object has been released"); } } @Override // org.webrtc.EglBase public void release() { checkIsNotReleased(); releaseSurface(); detachCurrent(); synchronized (EglBase.lock) { EGL14.eglDestroyContext(this.eglDisplay, this.eglContext); } EGL14.eglReleaseThread(); EGL14.eglTerminate(this.eglDisplay); this.eglContext = EGL14.EGL_NO_CONTEXT; this.eglDisplay = EGL14.EGL_NO_DISPLAY; this.eglConfig = null; } @Override // org.webrtc.EglBase public void makeCurrent() { checkIsNotReleased(); if (this.eglSurface == EGL14.EGL_NO_SURFACE) { throw new RuntimeException("No EGLSurface - can't make current"); } synchronized (EglBase.lock) { EGLDisplay eGLDisplay = this.eglDisplay; EGLSurface eGLSurface = this.eglSurface; if (!EGL14.eglMakeCurrent(eGLDisplay, eGLSurface, eGLSurface, this.eglContext)) { throw new RuntimeException("eglMakeCurrent failed: 0x" + Integer.toHexString(EGL14.eglGetError())); } } } @Override // org.webrtc.EglBase public void detachCurrent() { synchronized (EglBase.lock) { if (!EGL14.eglMakeCurrent(this.eglDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT)) { throw new RuntimeException("eglDetachCurrent failed: 0x" + Integer.toHexString(EGL14.eglGetError())); } } } @Override // org.webrtc.EglBase public void swapBuffers() { checkIsNotReleased(); if (this.eglSurface == EGL14.EGL_NO_SURFACE) { throw new RuntimeException("No EGLSurface - can't swap buffers"); } synchronized (EglBase.lock) { EGL14.eglSwapBuffers(this.eglDisplay, this.eglSurface); } } @Override // org.webrtc.EglBase public void swapBuffers(long j) { checkIsNotReleased(); if (this.eglSurface == EGL14.EGL_NO_SURFACE) { throw new RuntimeException("No EGLSurface - can't swap buffers"); } synchronized (EglBase.lock) { EGLExt.eglPresentationTimeANDROID(this.eglDisplay, this.eglSurface, j); EGL14.eglSwapBuffers(this.eglDisplay, this.eglSurface); } } private static EGLDisplay getEglDisplay() { EGLDisplay eglGetDisplay = EGL14.eglGetDisplay(0); if (eglGetDisplay == EGL14.EGL_NO_DISPLAY) { throw new RuntimeException("Unable to get EGL14 display: 0x" + Integer.toHexString(EGL14.eglGetError())); } int[] iArr = new int[2]; if (EGL14.eglInitialize(eglGetDisplay, iArr, 0, iArr, 1)) { return eglGetDisplay; } throw new RuntimeException("Unable to initialize EGL14: 0x" + Integer.toHexString(EGL14.eglGetError())); } private static EGLConfig getEglConfig(EGLDisplay eGLDisplay, int[] iArr) { EGLConfig[] eGLConfigArr = new EGLConfig[1]; int[] iArr2 = new int[1]; if (!EGL14.eglChooseConfig(eGLDisplay, iArr, 0, eGLConfigArr, 0, 1, iArr2, 0)) { throw new RuntimeException("eglChooseConfig failed: 0x" + Integer.toHexString(EGL14.eglGetError())); } if (iArr2[0] <= 0) { throw new RuntimeException("Unable to find any matching EGL config"); } EGLConfig eGLConfig = eGLConfigArr[0]; if (eGLConfig != null) { return eGLConfig; } throw new RuntimeException("eglChooseConfig returned null"); } private static EGLContext createEglContext(EGLContext eGLContext, EGLDisplay eGLDisplay, EGLConfig eGLConfig, int i) { EGLContext eglCreateContext; if (eGLContext != null && eGLContext == EGL14.EGL_NO_CONTEXT) { throw new RuntimeException("Invalid sharedContext"); } int[] iArr = {12440, i, 12344}; if (eGLContext == null) { eGLContext = EGL14.EGL_NO_CONTEXT; } synchronized (EglBase.lock) { eglCreateContext = EGL14.eglCreateContext(eGLDisplay, eGLConfig, eGLContext, iArr, 0); } if (eglCreateContext != EGL14.EGL_NO_CONTEXT) { return eglCreateContext; } throw new RuntimeException("Failed to create EGL context: 0x" + Integer.toHexString(EGL14.eglGetError())); } }