Rabbit-R1/original r1/java/sources/androidx/media3/common/util/EGLSurfaceTexture.java
2024-05-21 17:08:36 -04:00

172 lines
7.3 KiB
Java

package androidx.media3.common.util;
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 androidx.media3.common.util.GlUtil;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/* loaded from: classes2.dex */
public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableListener, Runnable {
private static final int[] EGL_CONFIG_ATTRIBUTES = {12352, 4, 12324, 8, 12323, 8, 12322, 8, 12321, 8, 12325, 0, 12327, 12344, 12339, 4, 12344};
private static final int EGL_PROTECTED_CONTENT_EXT = 12992;
private static final int EGL_SURFACE_HEIGHT = 1;
private static final int EGL_SURFACE_WIDTH = 1;
public static final int SECURE_MODE_NONE = 0;
public static final int SECURE_MODE_PROTECTED_PBUFFER = 2;
public static final int SECURE_MODE_SURFACELESS_CONTEXT = 1;
private final TextureImageListener callback;
private EGLContext context;
private EGLDisplay display;
private final Handler handler;
private EGLSurface surface;
private SurfaceTexture texture;
private final int[] textureIdHolder;
@Target({ElementType.TYPE_USE})
@Documented
@Retention(RetentionPolicy.SOURCE)
/* loaded from: classes2.dex */
public @interface SecureMode {
}
/* loaded from: classes2.dex */
public interface TextureImageListener {
void onFrameAvailable();
}
public EGLSurfaceTexture(Handler handler) {
this(handler, null);
}
public EGLSurfaceTexture(Handler handler, TextureImageListener textureImageListener) {
this.handler = handler;
this.callback = textureImageListener;
this.textureIdHolder = new int[1];
}
public void init(int i) throws GlUtil.GlException {
EGLDisplay defaultDisplay = getDefaultDisplay();
this.display = defaultDisplay;
EGLConfig chooseEGLConfig = chooseEGLConfig(defaultDisplay);
EGLContext createEGLContext = createEGLContext(this.display, chooseEGLConfig, i);
this.context = createEGLContext;
this.surface = createEGLSurface(this.display, chooseEGLConfig, createEGLContext, i);
generateTextureIds(this.textureIdHolder);
SurfaceTexture surfaceTexture = new SurfaceTexture(this.textureIdHolder[0]);
this.texture = surfaceTexture;
surfaceTexture.setOnFrameAvailableListener(this);
}
/* JADX WARN: Multi-variable type inference failed */
public void release() {
this.handler.removeCallbacks(this);
try {
SurfaceTexture surfaceTexture = this.texture;
if (surfaceTexture != null) {
surfaceTexture.release();
GLES20.glDeleteTextures(1, this.textureIdHolder, 0);
}
} finally {
EGLDisplay eGLDisplay = this.display;
if (eGLDisplay != null && !eGLDisplay.equals(EGL14.EGL_NO_DISPLAY)) {
EGL14.eglMakeCurrent(this.display, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT);
}
EGLSurface eGLSurface = this.surface;
if (eGLSurface != null && !eGLSurface.equals(EGL14.EGL_NO_SURFACE)) {
EGL14.eglDestroySurface(this.display, this.surface);
}
EGLContext eGLContext = this.context;
if (eGLContext != null) {
EGL14.eglDestroyContext(this.display, eGLContext);
}
if (Util.SDK_INT >= 19) {
EGL14.eglReleaseThread();
}
EGLDisplay eGLDisplay2 = this.display;
if (eGLDisplay2 != null && !eGLDisplay2.equals(EGL14.EGL_NO_DISPLAY)) {
EGL14.eglTerminate(this.display);
}
this.display = null;
this.context = null;
this.surface = null;
this.texture = null;
}
}
public SurfaceTexture getSurfaceTexture() {
return (SurfaceTexture) Assertions.checkNotNull(this.texture);
}
@Override // android.graphics.SurfaceTexture.OnFrameAvailableListener
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
this.handler.post(this);
}
@Override // java.lang.Runnable
public void run() {
dispatchOnFrameAvailable();
SurfaceTexture surfaceTexture = this.texture;
if (surfaceTexture != null) {
try {
surfaceTexture.updateTexImage();
} catch (RuntimeException unused) {
}
}
}
private void dispatchOnFrameAvailable() {
TextureImageListener textureImageListener = this.callback;
if (textureImageListener != null) {
textureImageListener.onFrameAvailable();
}
}
private static EGLDisplay getDefaultDisplay() throws GlUtil.GlException {
EGLDisplay eglGetDisplay = EGL14.eglGetDisplay(0);
GlUtil.checkGlException(eglGetDisplay != null, "eglGetDisplay failed");
int[] iArr = new int[2];
GlUtil.checkGlException(EGL14.eglInitialize(eglGetDisplay, iArr, 0, iArr, 1), "eglInitialize failed");
return eglGetDisplay;
}
private static EGLConfig chooseEGLConfig(EGLDisplay eGLDisplay) throws GlUtil.GlException {
EGLConfig[] eGLConfigArr = new EGLConfig[1];
int[] iArr = new int[1];
boolean eglChooseConfig = EGL14.eglChooseConfig(eGLDisplay, EGL_CONFIG_ATTRIBUTES, 0, eGLConfigArr, 0, 1, iArr, 0);
GlUtil.checkGlException(eglChooseConfig && iArr[0] > 0 && eGLConfigArr[0] != null, Util.formatInvariant("eglChooseConfig failed: success=%b, numConfigs[0]=%d, configs[0]=%s", Boolean.valueOf(eglChooseConfig), Integer.valueOf(iArr[0]), eGLConfigArr[0]));
return eGLConfigArr[0];
}
private static EGLContext createEGLContext(EGLDisplay eGLDisplay, EGLConfig eGLConfig, int i) throws GlUtil.GlException {
EGLContext eglCreateContext = EGL14.eglCreateContext(eGLDisplay, eGLConfig, EGL14.EGL_NO_CONTEXT, i == 0 ? new int[]{12440, 2, 12344} : new int[]{12440, 2, EGL_PROTECTED_CONTENT_EXT, 1, 12344}, 0);
GlUtil.checkGlException(eglCreateContext != null, "eglCreateContext failed");
return eglCreateContext;
}
private static EGLSurface createEGLSurface(EGLDisplay eGLDisplay, EGLConfig eGLConfig, EGLContext eGLContext, int i) throws GlUtil.GlException {
EGLSurface eglCreatePbufferSurface;
if (i == 1) {
eglCreatePbufferSurface = EGL14.EGL_NO_SURFACE;
} else {
eglCreatePbufferSurface = EGL14.eglCreatePbufferSurface(eGLDisplay, eGLConfig, i == 2 ? new int[]{12375, 1, 12374, 1, EGL_PROTECTED_CONTENT_EXT, 1, 12344} : new int[]{12375, 1, 12374, 1, 12344}, 0);
GlUtil.checkGlException(eglCreatePbufferSurface != null, "eglCreatePbufferSurface failed");
}
GlUtil.checkGlException(EGL14.eglMakeCurrent(eGLDisplay, eglCreatePbufferSurface, eglCreatePbufferSurface, eGLContext), "eglMakeCurrent failed");
return eglCreatePbufferSurface;
}
private static void generateTextureIds(int[] iArr) throws GlUtil.GlException {
GLES20.glGenTextures(1, iArr, 0);
GlUtil.checkGlError();
}
}