Commit dc668f2b by hschlueter Committed by Marc Baechinger

Make GlUtil.GlException checked and remove flag to disable it.

Transformer always enabled glAssertionsEnabled, so there should
be no functional change.

ExoPlayer previously disabled glAssertionsEnabled, so GlUtil logged
GlExceptions instead of throwing them. The GlExceptions are now
caught and logged by the callers so that there should also be no
functional change overall.

This change also replaces EGLSurfaceTexture#GlException with
GlUtil#GlException.

PiperOrigin-RevId: 453963741
parent 9f3c595e
Showing with 326 additions and 229 deletions
...@@ -29,6 +29,7 @@ import android.opengl.GLUtils; ...@@ -29,6 +29,7 @@ import android.opengl.GLUtils;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.GlProgram; import com.google.android.exoplayer2.util.GlProgram;
import com.google.android.exoplayer2.util.GlUtil; import com.google.android.exoplayer2.util.GlUtil;
import com.google.android.exoplayer2.util.Log;
import java.io.IOException; import java.io.IOException;
import java.util.Locale; import java.util.Locale;
import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL10;
...@@ -41,6 +42,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -41,6 +42,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/* package */ final class BitmapOverlayVideoProcessor /* package */ final class BitmapOverlayVideoProcessor
implements VideoProcessingGLSurfaceView.VideoProcessor { implements VideoProcessingGLSurfaceView.VideoProcessor {
private static final String TAG = "BitmapOverlayVP";
private static final int OVERLAY_WIDTH = 512; private static final int OVERLAY_WIDTH = 512;
private static final int OVERLAY_HEIGHT = 256; private static final int OVERLAY_HEIGHT = 256;
...@@ -85,6 +87,9 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -85,6 +87,9 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/* fragmentShaderFilePath= */ "bitmap_overlay_video_processor_fragment.glsl"); /* fragmentShaderFilePath= */ "bitmap_overlay_video_processor_fragment.glsl");
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to initialize the shader program", e);
return;
} }
program.setBufferAttribute( program.setBufferAttribute(
"aFramePosition", "aFramePosition",
...@@ -119,7 +124,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -119,7 +124,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
GLUtils.texSubImage2D( GLUtils.texSubImage2D(
GL10.GL_TEXTURE_2D, /* level= */ 0, /* xoffset= */ 0, /* yoffset= */ 0, overlayBitmap); GL10.GL_TEXTURE_2D, /* level= */ 0, /* xoffset= */ 0, /* yoffset= */ 0, overlayBitmap);
GlUtil.checkGlError(); try {
GlUtil.checkGlError();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to populate the texture", e);
}
// Run the shader program. // Run the shader program.
GlProgram program = checkNotNull(this.program); GlProgram program = checkNotNull(this.program);
...@@ -128,16 +137,28 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -128,16 +137,28 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
program.setFloatUniform("uScaleX", bitmapScaleX); program.setFloatUniform("uScaleX", bitmapScaleX);
program.setFloatUniform("uScaleY", bitmapScaleY); program.setFloatUniform("uScaleY", bitmapScaleY);
program.setFloatsUniform("uTexTransform", transformMatrix); program.setFloatsUniform("uTexTransform", transformMatrix);
program.bindAttributesAndUniforms(); try {
program.bindAttributesAndUniforms();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to update the shader program", e);
}
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4);
GlUtil.checkGlError(); try {
GlUtil.checkGlError();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to draw a frame", e);
}
} }
@Override @Override
public void release() { public void release() {
if (program != null) { if (program != null) {
program.delete(); try {
program.delete();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to delete the shader program", e);
}
} }
} }
} }
...@@ -29,6 +29,7 @@ import com.google.android.exoplayer2.ExoPlayer; ...@@ -29,6 +29,7 @@ import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.GlUtil; import com.google.android.exoplayer2.util.GlUtil;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.TimedValueQueue; import com.google.android.exoplayer2.util.TimedValueQueue;
import com.google.android.exoplayer2.video.VideoFrameMetadataListener; import com.google.android.exoplayer2.video.VideoFrameMetadataListener;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
...@@ -70,6 +71,7 @@ public final class VideoProcessingGLSurfaceView extends GLSurfaceView { ...@@ -70,6 +71,7 @@ public final class VideoProcessingGLSurfaceView extends GLSurfaceView {
} }
private static final int EGL_PROTECTED_CONTENT_EXT = 0x32C0; private static final int EGL_PROTECTED_CONTENT_EXT = 0x32C0;
private static final String TAG = "VPGlSurfaceView";
private final VideoRenderer renderer; private final VideoRenderer renderer;
private final Handler mainHandler; private final Handler mainHandler;
...@@ -239,7 +241,11 @@ public final class VideoProcessingGLSurfaceView extends GLSurfaceView { ...@@ -239,7 +241,11 @@ public final class VideoProcessingGLSurfaceView extends GLSurfaceView {
@Override @Override
public synchronized void onSurfaceCreated(GL10 gl, EGLConfig config) { public synchronized void onSurfaceCreated(GL10 gl, EGLConfig config) {
texture = GlUtil.createExternalTexture(); try {
texture = GlUtil.createExternalTexture();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to create an external texture", e);
}
surfaceTexture = new SurfaceTexture(texture); surfaceTexture = new SurfaceTexture(texture);
surfaceTexture.setOnFrameAvailableListener( surfaceTexture.setOnFrameAvailableListener(
surfaceTexture -> { surfaceTexture -> {
......
...@@ -45,9 +45,6 @@ import java.util.Locale; ...@@ -45,9 +45,6 @@ import java.util.Locale;
// TODO(b/227625365): Delete this class and use a texture processor from the Transformer library, // TODO(b/227625365): Delete this class and use a texture processor from the Transformer library,
// once overlaying a bitmap and text is supported in Transformer. // once overlaying a bitmap and text is supported in Transformer.
/* package */ final class BitmapOverlayProcessor extends SingleFrameGlTextureProcessor { /* package */ final class BitmapOverlayProcessor extends SingleFrameGlTextureProcessor {
static {
GlUtil.glAssertionsEnabled = true;
}
private static final String VERTEX_SHADER_PATH = "vertex_shader_copy_es2.glsl"; private static final String VERTEX_SHADER_PATH = "vertex_shader_copy_es2.glsl";
private static final String FRAGMENT_SHADER_PATH = "fragment_shader_bitmap_overlay_es2.glsl"; private static final String FRAGMENT_SHADER_PATH = "fragment_shader_bitmap_overlay_es2.glsl";
...@@ -67,9 +64,9 @@ import java.util.Locale; ...@@ -67,9 +64,9 @@ import java.util.Locale;
/** /**
* Creates a new instance. * Creates a new instance.
* *
* @throws IOException If a problem occurs while reading shader files. * @throws FrameProcessingException If a problem occurs while reading shader files.
*/ */
public BitmapOverlayProcessor(Context context) throws IOException { public BitmapOverlayProcessor(Context context) throws FrameProcessingException {
paint = new Paint(); paint = new Paint();
paint.setTextSize(64); paint.setTextSize(64);
paint.setAntiAlias(true); paint.setAntiAlias(true);
...@@ -87,10 +84,14 @@ import java.util.Locale; ...@@ -87,10 +84,14 @@ import java.util.Locale;
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
bitmapTexId = GlUtil.createTexture(BITMAP_WIDTH_HEIGHT, BITMAP_WIDTH_HEIGHT); try {
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, /* level= */ 0, overlayBitmap, /* border= */ 0); bitmapTexId = GlUtil.createTexture(BITMAP_WIDTH_HEIGHT, BITMAP_WIDTH_HEIGHT);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, /* level= */ 0, overlayBitmap, /* border= */ 0);
glProgram = new GlProgram(context, VERTEX_SHADER_PATH, FRAGMENT_SHADER_PATH); glProgram = new GlProgram(context, VERTEX_SHADER_PATH, FRAGMENT_SHADER_PATH);
} catch (GlUtil.GlException | IOException e) {
throw new FrameProcessingException(e);
}
// Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y. // Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y.
glProgram.setBufferAttribute( glProgram.setBufferAttribute(
"aFramePosition", "aFramePosition",
...@@ -141,15 +142,19 @@ import java.util.Locale; ...@@ -141,15 +142,19 @@ import java.util.Locale;
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4);
GlUtil.checkGlError(); GlUtil.checkGlError();
} catch (GlUtil.GlException e) { } catch (GlUtil.GlException e) {
throw new FrameProcessingException(e); throw new FrameProcessingException(e, presentationTimeUs);
} }
} }
@Override @Override
public void release() { public void release() throws FrameProcessingException {
super.release(); super.release();
if (glProgram != null) { if (glProgram != null) {
glProgram.delete(); try {
glProgram.delete();
} catch (GlUtil.GlException e) {
throw new FrameProcessingException(e);
}
} }
} }
......
...@@ -31,9 +31,6 @@ import java.io.IOException; ...@@ -31,9 +31,6 @@ import java.io.IOException;
* darker the further they are away from the frame center. * darker the further they are away from the frame center.
*/ */
/* package */ final class PeriodicVignetteProcessor extends SingleFrameGlTextureProcessor { /* package */ final class PeriodicVignetteProcessor extends SingleFrameGlTextureProcessor {
static {
GlUtil.glAssertionsEnabled = true;
}
private static final String VERTEX_SHADER_PATH = "vertex_shader_copy_es2.glsl"; private static final String VERTEX_SHADER_PATH = "vertex_shader_copy_es2.glsl";
private static final String FRAGMENT_SHADER_PATH = "fragment_shader_vignette_es2.glsl"; private static final String FRAGMENT_SHADER_PATH = "fragment_shader_vignette_es2.glsl";
...@@ -60,7 +57,7 @@ import java.io.IOException; ...@@ -60,7 +57,7 @@ import java.io.IOException;
* @param minInnerRadius The lower bound of the radius that is unaffected by the effect. * @param minInnerRadius The lower bound of the radius that is unaffected by the effect.
* @param maxInnerRadius The upper bound of the radius that is unaffected by the effect. * @param maxInnerRadius The upper bound of the radius that is unaffected by the effect.
* @param outerRadius The radius after which all pixels are black. * @param outerRadius The radius after which all pixels are black.
* @throws IOException If a problem occurs while reading shader files. * @throws FrameProcessingException If a problem occurs while reading shader files.
*/ */
public PeriodicVignetteProcessor( public PeriodicVignetteProcessor(
Context context, Context context,
...@@ -69,12 +66,16 @@ import java.io.IOException; ...@@ -69,12 +66,16 @@ import java.io.IOException;
float minInnerRadius, float minInnerRadius,
float maxInnerRadius, float maxInnerRadius,
float outerRadius) float outerRadius)
throws IOException { throws FrameProcessingException {
checkArgument(minInnerRadius <= maxInnerRadius); checkArgument(minInnerRadius <= maxInnerRadius);
checkArgument(maxInnerRadius <= outerRadius); checkArgument(maxInnerRadius <= outerRadius);
this.minInnerRadius = minInnerRadius; this.minInnerRadius = minInnerRadius;
this.deltaInnerRadius = maxInnerRadius - minInnerRadius; this.deltaInnerRadius = maxInnerRadius - minInnerRadius;
glProgram = new GlProgram(context, VERTEX_SHADER_PATH, FRAGMENT_SHADER_PATH); try {
glProgram = new GlProgram(context, VERTEX_SHADER_PATH, FRAGMENT_SHADER_PATH);
} catch (IOException | GlUtil.GlException e) {
throw new FrameProcessingException(e);
}
glProgram.setFloatsUniform("uCenter", new float[] {centerX, centerY}); glProgram.setFloatsUniform("uCenter", new float[] {centerX, centerY});
glProgram.setFloatsUniform("uOuterRadius", new float[] {outerRadius}); glProgram.setFloatsUniform("uOuterRadius", new float[] {outerRadius});
// Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y. // Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y.
...@@ -102,15 +103,19 @@ import java.io.IOException; ...@@ -102,15 +103,19 @@ import java.io.IOException;
// The four-vertex triangle strip forms a quad. // The four-vertex triangle strip forms a quad.
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4);
} catch (GlUtil.GlException e) { } catch (GlUtil.GlException e) {
throw new FrameProcessingException(e); throw new FrameProcessingException(e, presentationTimeUs);
} }
} }
@Override @Override
public void release() { public void release() throws FrameProcessingException {
super.release(); super.release();
if (glProgram != null) { if (glProgram != null) {
glProgram.delete(); try {
glProgram.delete();
} catch (GlUtil.GlException e) {
throw new FrameProcessingException(e);
}
} }
} }
} }
...@@ -79,12 +79,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -79,12 +79,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
* @param graphName Name of a MediaPipe graph asset to load. * @param graphName Name of a MediaPipe graph asset to load.
* @param inputStreamName Name of the input video stream in the graph. * @param inputStreamName Name of the input video stream in the graph.
* @param outputStreamName Name of the input video stream in the graph. * @param outputStreamName Name of the input video stream in the graph.
* @throws IOException If a problem occurs while reading shader files or initializing MediaPipe * @throws FrameProcessingException If a problem occurs while reading shader files or initializing
* resources. * MediaPipe resources.
*/ */
public MediaPipeProcessor( public MediaPipeProcessor(
Context context, String graphName, String inputStreamName, String outputStreamName) Context context, String graphName, String inputStreamName, String outputStreamName)
throws IOException { throws FrameProcessingException {
checkState(LOADER.isAvailable()); checkState(LOADER.isAvailable());
frameProcessorConditionVariable = new ConditionVariable(); frameProcessorConditionVariable = new ConditionVariable();
...@@ -104,7 +104,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -104,7 +104,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
frameProcessorPendingError = error; frameProcessorPendingError = error;
frameProcessorConditionVariable.open(); frameProcessorConditionVariable.open();
}); });
glProgram = new GlProgram(context, COPY_VERTEX_SHADER_NAME, COPY_FRAGMENT_SHADER_NAME); try {
glProgram = new GlProgram(context, COPY_VERTEX_SHADER_NAME, COPY_FRAGMENT_SHADER_NAME);
} catch (IOException | GlUtil.GlException e) {
throw new FrameProcessingException(e);
}
} }
@Override @Override
...@@ -152,14 +156,14 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -152,14 +156,14 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4);
GlUtil.checkGlError(); GlUtil.checkGlError();
} catch (GlUtil.GlException e) { } catch (GlUtil.GlException e) {
throw new FrameProcessingException(e); throw new FrameProcessingException(e, presentationTimeUs);
} finally { } finally {
checkStateNotNull(outputFrame).release(); checkStateNotNull(outputFrame).release();
} }
} }
@Override @Override
public void release() { public void release() throws FrameProcessingException {
super.release(); super.release();
checkStateNotNull(frameProcessor).close(); checkStateNotNull(frameProcessor).close();
} }
......
...@@ -78,13 +78,6 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL ...@@ -78,13 +78,6 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL
private static final int EGL_PROTECTED_CONTENT_EXT = 0x32C0; private static final int EGL_PROTECTED_CONTENT_EXT = 0x32C0;
/** A runtime exception to be thrown if some EGL operations failed. */
public static final class GlException extends RuntimeException {
private GlException(String msg) {
super(msg);
}
}
private final Handler handler; private final Handler handler;
private final int[] textureIdHolder; private final int[] textureIdHolder;
@Nullable private final TextureImageListener callback; @Nullable private final TextureImageListener callback;
...@@ -124,7 +117,7 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL ...@@ -124,7 +117,7 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL
* *
* @param secureMode The {@link SecureMode} to be used for EGL surface. * @param secureMode The {@link SecureMode} to be used for EGL surface.
*/ */
public void init(@SecureMode int secureMode) { public void init(@SecureMode int secureMode) throws GlUtil.GlException {
display = getDefaultDisplay(); display = getDefaultDisplay();
EGLConfig config = chooseEGLConfig(display); EGLConfig config = chooseEGLConfig(display);
context = createEGLContext(display, config, secureMode); context = createEGLContext(display, config, secureMode);
...@@ -205,22 +198,18 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL ...@@ -205,22 +198,18 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL
} }
} }
private static EGLDisplay getDefaultDisplay() { private static EGLDisplay getDefaultDisplay() throws GlUtil.GlException {
EGLDisplay display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); EGLDisplay display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
if (display == null) { GlUtil.checkGlException(display != null, "eglGetDisplay failed");
throw new GlException("eglGetDisplay failed");
}
int[] version = new int[2]; int[] version = new int[2];
boolean eglInitialized = boolean eglInitialized =
EGL14.eglInitialize(display, version, /* majorOffset= */ 0, version, /* minorOffset= */ 1); EGL14.eglInitialize(display, version, /* majorOffset= */ 0, version, /* minorOffset= */ 1);
if (!eglInitialized) { GlUtil.checkGlException(eglInitialized, "eglInitialize failed");
throw new GlException("eglInitialize failed");
}
return display; return display;
} }
private static EGLConfig chooseEGLConfig(EGLDisplay display) { private static EGLConfig chooseEGLConfig(EGLDisplay display) throws GlUtil.GlException {
EGLConfig[] configs = new EGLConfig[1]; EGLConfig[] configs = new EGLConfig[1];
int[] numConfigs = new int[1]; int[] numConfigs = new int[1];
boolean success = boolean success =
...@@ -233,18 +222,17 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL ...@@ -233,18 +222,17 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL
/* config_size= */ 1, /* config_size= */ 1,
numConfigs, numConfigs,
/* num_configOffset= */ 0); /* num_configOffset= */ 0);
if (!success || numConfigs[0] <= 0 || configs[0] == null) { GlUtil.checkGlException(
throw new GlException( success && numConfigs[0] > 0 && configs[0] != null,
Util.formatInvariant( Util.formatInvariant(
/* format= */ "eglChooseConfig failed: success=%b, numConfigs[0]=%d, configs[0]=%s", /* format= */ "eglChooseConfig failed: success=%b, numConfigs[0]=%d, configs[0]=%s",
success, numConfigs[0], configs[0])); success, numConfigs[0], configs[0]));
}
return configs[0]; return configs[0];
} }
private static EGLContext createEGLContext( private static EGLContext createEGLContext(
EGLDisplay display, EGLConfig config, @SecureMode int secureMode) { EGLDisplay display, EGLConfig config, @SecureMode int secureMode) throws GlUtil.GlException {
int[] glAttributes; int[] glAttributes;
if (secureMode == SECURE_MODE_NONE) { if (secureMode == SECURE_MODE_NONE) {
glAttributes = new int[] {EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE}; glAttributes = new int[] {EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE};
...@@ -261,14 +249,13 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL ...@@ -261,14 +249,13 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL
EGLContext context = EGLContext context =
EGL14.eglCreateContext( EGL14.eglCreateContext(
display, config, android.opengl.EGL14.EGL_NO_CONTEXT, glAttributes, 0); display, config, android.opengl.EGL14.EGL_NO_CONTEXT, glAttributes, 0);
if (context == null) { GlUtil.checkGlException(context != null, "eglCreateContext failed");
throw new GlException("eglCreateContext failed");
}
return context; return context;
} }
private static EGLSurface createEGLSurface( private static EGLSurface createEGLSurface(
EGLDisplay display, EGLConfig config, EGLContext context, @SecureMode int secureMode) { EGLDisplay display, EGLConfig config, EGLContext context, @SecureMode int secureMode)
throws GlUtil.GlException {
EGLSurface surface; EGLSurface surface;
if (secureMode == SECURE_MODE_SURFACELESS_CONTEXT) { if (secureMode == SECURE_MODE_SURFACELESS_CONTEXT) {
surface = EGL14.EGL_NO_SURFACE; surface = EGL14.EGL_NO_SURFACE;
...@@ -296,20 +283,16 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL ...@@ -296,20 +283,16 @@ public final class EGLSurfaceTexture implements SurfaceTexture.OnFrameAvailableL
}; };
} }
surface = EGL14.eglCreatePbufferSurface(display, config, pbufferAttributes, /* offset= */ 0); surface = EGL14.eglCreatePbufferSurface(display, config, pbufferAttributes, /* offset= */ 0);
if (surface == null) { GlUtil.checkGlException(surface != null, "eglCreatePbufferSurface failed");
throw new GlException("eglCreatePbufferSurface failed");
}
} }
boolean eglMadeCurrent = boolean eglMadeCurrent =
EGL14.eglMakeCurrent(display, /* draw= */ surface, /* read= */ surface, context); EGL14.eglMakeCurrent(display, /* draw= */ surface, /* read= */ surface, context);
if (!eglMadeCurrent) { GlUtil.checkGlException(eglMadeCurrent, "eglMakeCurrent failed");
throw new GlException("eglMakeCurrent failed");
}
return surface; return surface;
} }
private static void generateTextureIds(int[] textureIdHolder) { private static void generateTextureIds(int[] textureIdHolder) throws GlUtil.GlException {
GLES20.glGenTextures(/* n= */ 1, textureIdHolder, /* offset= */ 0); GLES20.glGenTextures(/* n= */ 1, textureIdHolder, /* offset= */ 0);
GlUtil.checkGlError(); GlUtil.checkGlError();
} }
......
...@@ -53,7 +53,7 @@ public final class GlProgram { ...@@ -53,7 +53,7 @@ public final class GlProgram {
* @throws IOException When failing to read shader files. * @throws IOException When failing to read shader files.
*/ */
public GlProgram(Context context, String vertexShaderFilePath, String fragmentShaderFilePath) public GlProgram(Context context, String vertexShaderFilePath, String fragmentShaderFilePath)
throws IOException { throws IOException, GlUtil.GlException {
this( this(
GlUtil.loadAsset(context, vertexShaderFilePath), GlUtil.loadAsset(context, vertexShaderFilePath),
GlUtil.loadAsset(context, fragmentShaderFilePath)); GlUtil.loadAsset(context, fragmentShaderFilePath));
...@@ -68,7 +68,7 @@ public final class GlProgram { ...@@ -68,7 +68,7 @@ public final class GlProgram {
* @param vertexShaderGlsl The vertex shader program. * @param vertexShaderGlsl The vertex shader program.
* @param fragmentShaderGlsl The fragment shader program. * @param fragmentShaderGlsl The fragment shader program.
*/ */
public GlProgram(String vertexShaderGlsl, String fragmentShaderGlsl) { public GlProgram(String vertexShaderGlsl, String fragmentShaderGlsl) throws GlUtil.GlException {
programId = GLES20.glCreateProgram(); programId = GLES20.glCreateProgram();
GlUtil.checkGlError(); GlUtil.checkGlError();
...@@ -80,10 +80,9 @@ public final class GlProgram { ...@@ -80,10 +80,9 @@ public final class GlProgram {
GLES20.glLinkProgram(programId); GLES20.glLinkProgram(programId);
int[] linkStatus = new int[] {GLES20.GL_FALSE}; int[] linkStatus = new int[] {GLES20.GL_FALSE};
GLES20.glGetProgramiv(programId, GLES20.GL_LINK_STATUS, linkStatus, /* offset= */ 0); GLES20.glGetProgramiv(programId, GLES20.GL_LINK_STATUS, linkStatus, /* offset= */ 0);
if (linkStatus[0] != GLES20.GL_TRUE) { GlUtil.checkGlException(
GlUtil.throwGlException( linkStatus[0] == GLES20.GL_TRUE,
"Unable to link shader program: \n" + GLES20.glGetProgramInfoLog(programId)); "Unable to link shader program: \n" + GLES20.glGetProgramInfoLog(programId));
}
GLES20.glUseProgram(programId); GLES20.glUseProgram(programId);
attributeByName = new HashMap<>(); attributeByName = new HashMap<>();
int[] attributeCount = new int[1]; int[] attributeCount = new int[1];
...@@ -106,16 +105,15 @@ public final class GlProgram { ...@@ -106,16 +105,15 @@ public final class GlProgram {
GlUtil.checkGlError(); GlUtil.checkGlError();
} }
private static void addShader(int programId, int type, String glsl) { private static void addShader(int programId, int type, String glsl) throws GlUtil.GlException {
int shader = GLES20.glCreateShader(type); int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, glsl); GLES20.glShaderSource(shader, glsl);
GLES20.glCompileShader(shader); GLES20.glCompileShader(shader);
int[] result = new int[] {GLES20.GL_FALSE}; int[] result = new int[] {GLES20.GL_FALSE};
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, result, /* offset= */ 0); GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, result, /* offset= */ 0);
if (result[0] != GLES20.GL_TRUE) { GlUtil.checkGlException(
GlUtil.throwGlException(GLES20.glGetShaderInfoLog(shader) + ", source: " + glsl); result[0] == GLES20.GL_TRUE, GLES20.glGetShaderInfoLog(shader) + ", source: " + glsl);
}
GLES20.glAttachShader(programId, shader); GLES20.glAttachShader(programId, shader);
GLES20.glDeleteShader(shader); GLES20.glDeleteShader(shader);
...@@ -145,13 +143,13 @@ public final class GlProgram { ...@@ -145,13 +143,13 @@ public final class GlProgram {
* *
* <p>Call this in the rendering loop to switch between different programs. * <p>Call this in the rendering loop to switch between different programs.
*/ */
public void use() { public void use() throws GlUtil.GlException {
GLES20.glUseProgram(programId); GLES20.glUseProgram(programId);
GlUtil.checkGlError(); GlUtil.checkGlError();
} }
/** Deletes the program. Deleted programs cannot be used again. */ /** Deletes the program. Deleted programs cannot be used again. */
public void delete() { public void delete() throws GlUtil.GlException {
GLES20.glDeleteProgram(programId); GLES20.glDeleteProgram(programId);
GlUtil.checkGlError(); GlUtil.checkGlError();
} }
...@@ -160,7 +158,7 @@ public final class GlProgram { ...@@ -160,7 +158,7 @@ public final class GlProgram {
* Returns the location of an {@link Attribute}, which has been enabled as a vertex attribute * Returns the location of an {@link Attribute}, which has been enabled as a vertex attribute
* array. * array.
*/ */
public int getAttributeArrayLocationAndEnable(String attributeName) { public int getAttributeArrayLocationAndEnable(String attributeName) throws GlUtil.GlException {
int location = getAttributeLocation(attributeName); int location = getAttributeLocation(attributeName);
GLES20.glEnableVertexAttribArray(location); GLES20.glEnableVertexAttribArray(location);
GlUtil.checkGlError(); GlUtil.checkGlError();
...@@ -195,7 +193,7 @@ public final class GlProgram { ...@@ -195,7 +193,7 @@ public final class GlProgram {
} }
/** Binds all attributes and uniforms in the program. */ /** Binds all attributes and uniforms in the program. */
public void bindAttributesAndUniforms() { public void bindAttributesAndUniforms() throws GlUtil.GlException {
for (Attribute attribute : attributes) { for (Attribute attribute : attributes) {
attribute.bind(); attribute.bind();
} }
...@@ -276,7 +274,7 @@ public final class GlProgram { ...@@ -276,7 +274,7 @@ public final class GlProgram {
* *
* <p>Should be called before each drawing call. * <p>Should be called before each drawing call.
*/ */
public void bind() { public void bind() throws GlUtil.GlException {
Buffer buffer = checkNotNull(this.buffer, "call setBuffer before bind"); Buffer buffer = checkNotNull(this.buffer, "call setBuffer before bind");
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, /* buffer= */ 0); GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, /* buffer= */ 0);
GLES20.glVertexAttribPointer( GLES20.glVertexAttribPointer(
...@@ -362,7 +360,7 @@ public final class GlProgram { ...@@ -362,7 +360,7 @@ public final class GlProgram {
* *
* <p>Should be called before each drawing call. * <p>Should be called before each drawing call.
*/ */
public void bind() { public void bind() throws GlUtil.GlException {
switch (type) { switch (type) {
case GLES20.GL_FLOAT: case GLES20.GL_FLOAT:
GLES20.glUniform1fv(location, /* count= */ 1, value, /* offset= */ 0); GLES20.glUniform1fv(location, /* count= */ 1, value, /* offset= */ 0);
......
...@@ -177,6 +177,9 @@ public final class PlaceholderSurface extends Surface { ...@@ -177,6 +177,9 @@ public final class PlaceholderSurface extends Surface {
} catch (RuntimeException e) { } catch (RuntimeException e) {
Log.e(TAG, "Failed to initialize placeholder surface", e); Log.e(TAG, "Failed to initialize placeholder surface", e);
initException = e; initException = e;
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to initialize placeholder surface", e);
initException = new IllegalStateException(e);
} catch (Error e) { } catch (Error e) {
Log.e(TAG, "Failed to initialize placeholder surface", e); Log.e(TAG, "Failed to initialize placeholder surface", e);
initError = e; initError = e;
...@@ -200,7 +203,7 @@ public final class PlaceholderSurface extends Surface { ...@@ -200,7 +203,7 @@ public final class PlaceholderSurface extends Surface {
} }
} }
private void initInternal(@SecureMode int secureMode) { private void initInternal(@SecureMode int secureMode) throws GlUtil.GlException {
Assertions.checkNotNull(eglSurfaceTexture); Assertions.checkNotNull(eglSurfaceTexture);
eglSurfaceTexture.init(secureMode); eglSurfaceTexture.init(secureMode);
this.surface = this.surface =
......
...@@ -21,6 +21,7 @@ import android.content.Context; ...@@ -21,6 +21,7 @@ import android.content.Context;
import android.opengl.GLES20; import android.opengl.GLES20;
import android.opengl.GLSurfaceView; import android.opengl.GLSurfaceView;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.decoder.VideoDecoderOutputBuffer; import com.google.android.exoplayer2.decoder.VideoDecoderOutputBuffer;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
...@@ -46,6 +47,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -46,6 +47,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
public final class VideoDecoderGLSurfaceView extends GLSurfaceView public final class VideoDecoderGLSurfaceView extends GLSurfaceView
implements VideoDecoderOutputBufferRenderer { implements VideoDecoderOutputBufferRenderer {
private static final String TAG = "VideoDecoderGLSV";
private final Renderer renderer; private final Renderer renderer;
/** /**
...@@ -170,22 +173,26 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView ...@@ -170,22 +173,26 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView
@Override @Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) { public void onSurfaceCreated(GL10 unused, EGLConfig config) {
program = new GlProgram(VERTEX_SHADER, FRAGMENT_SHADER); try {
int posLocation = program.getAttributeArrayLocationAndEnable("in_pos"); program = new GlProgram(VERTEX_SHADER, FRAGMENT_SHADER);
GLES20.glVertexAttribPointer( int posLocation = program.getAttributeArrayLocationAndEnable("in_pos");
posLocation, GLES20.glVertexAttribPointer(
2, posLocation,
GLES20.GL_FLOAT, 2,
/* normalized= */ false, GLES20.GL_FLOAT,
/* stride= */ 0, /* normalized= */ false,
TEXTURE_VERTICES); /* stride= */ 0,
texLocations[0] = program.getAttributeArrayLocationAndEnable("in_tc_y"); TEXTURE_VERTICES);
texLocations[1] = program.getAttributeArrayLocationAndEnable("in_tc_u"); texLocations[0] = program.getAttributeArrayLocationAndEnable("in_tc_y");
texLocations[2] = program.getAttributeArrayLocationAndEnable("in_tc_v"); texLocations[1] = program.getAttributeArrayLocationAndEnable("in_tc_u");
colorMatrixLocation = program.getUniformLocation("mColorConversion"); texLocations[2] = program.getAttributeArrayLocationAndEnable("in_tc_v");
GlUtil.checkGlError(); colorMatrixLocation = program.getUniformLocation("mColorConversion");
setupTextures(); GlUtil.checkGlError();
GlUtil.checkGlError(); setupTextures();
GlUtil.checkGlError();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to set up the textures and program", e);
}
} }
@Override @Override
...@@ -282,7 +289,11 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView ...@@ -282,7 +289,11 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4);
GlUtil.checkGlError(); try {
GlUtil.checkGlError();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to draw a frame", e);
}
} }
public void setOutputBuffer(VideoDecoderOutputBuffer outputBuffer) { public void setOutputBuffer(VideoDecoderOutputBuffer outputBuffer) {
...@@ -298,13 +309,17 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView ...@@ -298,13 +309,17 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView
@RequiresNonNull("program") @RequiresNonNull("program")
private void setupTextures() { private void setupTextures() {
GLES20.glGenTextures(/* n= */ 3, yuvTextures, /* offset= */ 0); try {
for (int i = 0; i < 3; i++) { GLES20.glGenTextures(/* n= */ 3, yuvTextures, /* offset= */ 0);
GLES20.glUniform1i(program.getUniformLocation(TEXTURE_UNIFORMS[i]), i); for (int i = 0; i < 3; i++) {
GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i); GLES20.glUniform1i(program.getUniformLocation(TEXTURE_UNIFORMS[i]), i);
GlUtil.bindTexture(GLES20.GL_TEXTURE_2D, yuvTextures[i]); GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
GlUtil.bindTexture(GLES20.GL_TEXTURE_2D, yuvTextures[i]);
}
GlUtil.checkGlError();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to set up the textures", e);
} }
GlUtil.checkGlError();
} }
} }
} }
...@@ -19,6 +19,7 @@ import static com.google.android.exoplayer2.util.GlUtil.checkGlError; ...@@ -19,6 +19,7 @@ import static com.google.android.exoplayer2.util.GlUtil.checkGlError;
import android.opengl.GLES11Ext; import android.opengl.GLES11Ext;
import android.opengl.GLES20; import android.opengl.GLES20;
import android.util.Log;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.GlProgram; import com.google.android.exoplayer2.util.GlProgram;
...@@ -45,6 +46,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -45,6 +46,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
&& rightMesh.getSubMesh(0).textureId == Projection.SubMesh.VIDEO_TEXTURE_ID; && rightMesh.getSubMesh(0).textureId == Projection.SubMesh.VIDEO_TEXTURE_ID;
} }
private static final String TAG = "ProjectionRenderer";
// Basic vertex & fragment shaders to render a mesh with 3D position & 2D texture data. // Basic vertex & fragment shaders to render a mesh with 3D position & 2D texture data.
private static final String VERTEX_SHADER = private static final String VERTEX_SHADER =
"uniform mat4 uMvpMatrix;\n" "uniform mat4 uMvpMatrix;\n"
...@@ -115,12 +118,16 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -115,12 +118,16 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/** Initializes of the GL components. */ /** Initializes of the GL components. */
public void init() { public void init() {
program = new GlProgram(VERTEX_SHADER, FRAGMENT_SHADER); try {
mvpMatrixHandle = program.getUniformLocation("uMvpMatrix"); program = new GlProgram(VERTEX_SHADER, FRAGMENT_SHADER);
uTexMatrixHandle = program.getUniformLocation("uTexMatrix"); mvpMatrixHandle = program.getUniformLocation("uMvpMatrix");
positionHandle = program.getAttributeArrayLocationAndEnable("aPosition"); uTexMatrixHandle = program.getUniformLocation("uTexMatrix");
texCoordsHandle = program.getAttributeArrayLocationAndEnable("aTexCoords"); positionHandle = program.getAttributeArrayLocationAndEnable("aPosition");
textureHandle = program.getUniformLocation("uTexture"); texCoordsHandle = program.getAttributeArrayLocationAndEnable("aTexCoords");
textureHandle = program.getUniformLocation("uTexture");
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to initialize the program", e);
}
} }
/** /**
...@@ -154,7 +161,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -154,7 +161,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId);
GLES20.glUniform1i(textureHandle, 0); GLES20.glUniform1i(textureHandle, 0);
checkGlError(); try {
checkGlError();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to bind uniforms", e);
}
// Load position data. // Load position data.
GLES20.glVertexAttribPointer( GLES20.glVertexAttribPointer(
...@@ -164,7 +175,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -164,7 +175,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
false, false,
Projection.POSITION_COORDS_PER_VERTEX * C.BYTES_PER_FLOAT, Projection.POSITION_COORDS_PER_VERTEX * C.BYTES_PER_FLOAT,
meshData.vertexBuffer); meshData.vertexBuffer);
checkGlError(); try {
checkGlError();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to load position data", e);
}
// Load texture data. // Load texture data.
GLES20.glVertexAttribPointer( GLES20.glVertexAttribPointer(
...@@ -174,17 +189,29 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -174,17 +189,29 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
false, false,
Projection.TEXTURE_COORDS_PER_VERTEX * C.BYTES_PER_FLOAT, Projection.TEXTURE_COORDS_PER_VERTEX * C.BYTES_PER_FLOAT,
meshData.textureBuffer); meshData.textureBuffer);
checkGlError(); try {
checkGlError();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to load texture data", e);
}
// Render. // Render.
GLES20.glDrawArrays(meshData.drawMode, /* first= */ 0, meshData.vertexCount); GLES20.glDrawArrays(meshData.drawMode, /* first= */ 0, meshData.vertexCount);
checkGlError(); try {
checkGlError();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to render", e);
}
} }
/** Cleans up GL resources. */ /** Cleans up GL resources. */
public void shutdown() { public void shutdown() {
if (program != null) { if (program != null) {
program.delete(); try {
program.delete();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to delete the shader program", e);
}
} }
} }
......
...@@ -26,6 +26,7 @@ import com.google.android.exoplayer2.C; ...@@ -26,6 +26,7 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.GlUtil; import com.google.android.exoplayer2.util.GlUtil;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.TimedValueQueue; import com.google.android.exoplayer2.util.TimedValueQueue;
import com.google.android.exoplayer2.video.VideoFrameMetadataListener; import com.google.android.exoplayer2.video.VideoFrameMetadataListener;
import java.util.Arrays; import java.util.Arrays;
...@@ -36,6 +37,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -36,6 +37,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/* package */ final class SceneRenderer /* package */ final class SceneRenderer
implements VideoFrameMetadataListener, CameraMotionListener { implements VideoFrameMetadataListener, CameraMotionListener {
private static final String TAG = "SceneRenderer";
private final AtomicBoolean frameAvailable; private final AtomicBoolean frameAvailable;
private final AtomicBoolean resetRotationAtNextFrame; private final AtomicBoolean resetRotationAtNextFrame;
private final ProjectionRenderer projectionRenderer; private final ProjectionRenderer projectionRenderer;
...@@ -83,14 +86,19 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -83,14 +86,19 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/** Initializes the renderer. */ /** Initializes the renderer. */
public SurfaceTexture init() { public SurfaceTexture init() {
// Set the background frame color. This is only visible if the display mesh isn't a full sphere. try {
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); // Set the background frame color. This is only visible if the display mesh isn't a full
checkGlError(); // sphere.
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
checkGlError();
projectionRenderer.init(); projectionRenderer.init();
checkGlError(); checkGlError();
textureId = GlUtil.createExternalTexture(); textureId = GlUtil.createExternalTexture();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to initialize the renderer", e);
}
surfaceTexture = new SurfaceTexture(textureId); surfaceTexture = new SurfaceTexture(textureId);
surfaceTexture.setOnFrameAvailableListener(surfaceTexture -> frameAvailable.set(true)); surfaceTexture.setOnFrameAvailableListener(surfaceTexture -> frameAvailable.set(true));
return surfaceTexture; return surfaceTexture;
...@@ -107,11 +115,19 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -107,11 +115,19 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
// glClear isn't strictly necessary when rendering fully spherical panoramas, but it can improve // glClear isn't strictly necessary when rendering fully spherical panoramas, but it can improve
// performance on tiled renderers by causing the GPU to discard previous data. // performance on tiled renderers by causing the GPU to discard previous data.
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
checkGlError(); try {
checkGlError();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to draw a frame", e);
}
if (frameAvailable.compareAndSet(true, false)) { if (frameAvailable.compareAndSet(true, false)) {
Assertions.checkNotNull(surfaceTexture).updateTexImage(); Assertions.checkNotNull(surfaceTexture).updateTexImage();
checkGlError(); try {
checkGlError();
} catch (GlUtil.GlException e) {
Log.e(TAG, "Failed to draw a frame", e);
}
if (resetRotationAtNextFrame.compareAndSet(true, false)) { if (resetRotationAtNextFrame.compareAndSet(true, false)) {
Matrix.setIdentityM(rotationMatrix, 0); Matrix.setIdentityM(rotationMatrix, 0);
} }
......
...@@ -186,7 +186,8 @@ public class BitmapTestUtil { ...@@ -186,7 +186,8 @@ public class BitmapTestUtil {
* @param height The height of the pixel rectangle to read. * @param height The height of the pixel rectangle to read.
* @return A {@link Bitmap} with the framebuffer's values. * @return A {@link Bitmap} with the framebuffer's values.
*/ */
public static Bitmap createArgb8888BitmapFromCurrentGlFramebuffer(int width, int height) { public static Bitmap createArgb8888BitmapFromCurrentGlFramebuffer(int width, int height)
throws GlUtil.GlException {
ByteBuffer rgba8888Buffer = ByteBuffer.allocateDirect(width * height * 4); ByteBuffer rgba8888Buffer = ByteBuffer.allocateDirect(width * height * 4);
GLES20.glReadPixels( GLES20.glReadPixels(
0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, rgba8888Buffer); 0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, rgba8888Buffer);
...@@ -206,7 +207,7 @@ public class BitmapTestUtil { ...@@ -206,7 +207,7 @@ public class BitmapTestUtil {
* @param bitmap A {@link Bitmap}. * @param bitmap A {@link Bitmap}.
* @return The identifier of the newly created texture. * @return The identifier of the newly created texture.
*/ */
public static int createGlTextureFromBitmap(Bitmap bitmap) { public static int createGlTextureFromBitmap(Bitmap bitmap) throws GlUtil.GlException {
int texId = GlUtil.createTexture(bitmap.getWidth(), bitmap.getHeight()); int texId = GlUtil.createTexture(bitmap.getWidth(), bitmap.getHeight());
// Put the flipped bitmap in the OpenGL texture as the bitmap's positive y-axis points down // Put the flipped bitmap in the OpenGL texture as the bitmap's positive y-axis points down
// while OpenGL's positive y-axis points up. // while OpenGL's positive y-axis points up.
......
...@@ -52,13 +52,9 @@ public final class CropPixelTest { ...@@ -52,13 +52,9 @@ public final class CropPixelTest {
public static final String CROP_LARGER_PNG_ASSET_PATH = public static final String CROP_LARGER_PNG_ASSET_PATH =
"media/bitmap/sample_mp4_first_frame/crop_larger.png"; "media/bitmap/sample_mp4_first_frame/crop_larger.png";
static { private Context context = getApplicationContext();
GlUtil.glAssertionsEnabled = true; private @MonotonicNonNull EGLDisplay eglDisplay;
} private @MonotonicNonNull EGLContext eglContext;
private final Context context = getApplicationContext();
private final EGLDisplay eglDisplay = GlUtil.createEglDisplay();
private final EGLContext eglContext = GlUtil.createEglContext(eglDisplay);
private @MonotonicNonNull SingleFrameGlTextureProcessor cropTextureProcessor; private @MonotonicNonNull SingleFrameGlTextureProcessor cropTextureProcessor;
private @MonotonicNonNull EGLSurface placeholderEglSurface; private @MonotonicNonNull EGLSurface placeholderEglSurface;
private int inputTexId; private int inputTexId;
...@@ -67,7 +63,9 @@ public final class CropPixelTest { ...@@ -67,7 +63,9 @@ public final class CropPixelTest {
private int inputHeight; private int inputHeight;
@Before @Before
public void createTextures() throws IOException { public void createGlObjects() throws IOException, GlUtil.GlException {
eglDisplay = GlUtil.createEglDisplay();
eglContext = GlUtil.createEglContext(eglDisplay);
Bitmap inputBitmap = BitmapTestUtil.readBitmap(ORIGINAL_PNG_ASSET_PATH); Bitmap inputBitmap = BitmapTestUtil.readBitmap(ORIGINAL_PNG_ASSET_PATH);
inputWidth = inputBitmap.getWidth(); inputWidth = inputBitmap.getWidth();
inputHeight = inputBitmap.getHeight(); inputHeight = inputBitmap.getHeight();
...@@ -77,11 +75,13 @@ public final class CropPixelTest { ...@@ -77,11 +75,13 @@ public final class CropPixelTest {
} }
@After @After
public void release() { public void release() throws GlUtil.GlException, FrameProcessingException {
if (cropTextureProcessor != null) { if (cropTextureProcessor != null) {
cropTextureProcessor.release(); cropTextureProcessor.release();
} }
GlUtil.destroyEglContext(eglDisplay, eglContext); if (eglContext != null && eglDisplay != null) {
GlUtil.destroyEglContext(eglDisplay, eglContext);
}
} }
@Test @Test
...@@ -156,12 +156,12 @@ public final class CropPixelTest { ...@@ -156,12 +156,12 @@ public final class CropPixelTest {
assertThat(averagePixelAbsoluteDifference).isAtMost(MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE); assertThat(averagePixelAbsoluteDifference).isAtMost(MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE);
} }
private void setupOutputTexture(int outputWidth, int outputHeight) { private void setupOutputTexture(int outputWidth, int outputHeight) throws GlUtil.GlException {
outputTexId = GlUtil.createTexture(outputWidth, outputHeight); outputTexId = GlUtil.createTexture(outputWidth, outputHeight);
int frameBuffer = GlUtil.createFboForTexture(outputTexId); int frameBuffer = GlUtil.createFboForTexture(outputTexId);
GlUtil.focusFramebuffer( GlUtil.focusFramebuffer(
eglDisplay, checkNotNull(eglDisplay),
eglContext, checkNotNull(eglContext),
checkNotNull(placeholderEglSurface), checkNotNull(placeholderEglSurface),
frameBuffer, frameBuffer,
outputWidth, outputWidth,
......
...@@ -53,13 +53,9 @@ public final class MatrixTransformationProcessorPixelTest { ...@@ -53,13 +53,9 @@ public final class MatrixTransformationProcessorPixelTest {
public static final String ROTATE_90_PNG_ASSET_PATH = public static final String ROTATE_90_PNG_ASSET_PATH =
"media/bitmap/sample_mp4_first_frame/rotate90.png"; "media/bitmap/sample_mp4_first_frame/rotate90.png";
static {
GlUtil.glAssertionsEnabled = true;
}
private final Context context = getApplicationContext(); private final Context context = getApplicationContext();
private final EGLDisplay eglDisplay = GlUtil.createEglDisplay(); private @MonotonicNonNull EGLDisplay eglDisplay;
private final EGLContext eglContext = GlUtil.createEglContext(eglDisplay); private @MonotonicNonNull EGLContext eglContext;
private @MonotonicNonNull SingleFrameGlTextureProcessor matrixTransformationFrameProcessor; private @MonotonicNonNull SingleFrameGlTextureProcessor matrixTransformationFrameProcessor;
private int inputTexId; private int inputTexId;
private int outputTexId; private int outputTexId;
...@@ -67,7 +63,9 @@ public final class MatrixTransformationProcessorPixelTest { ...@@ -67,7 +63,9 @@ public final class MatrixTransformationProcessorPixelTest {
private int height; private int height;
@Before @Before
public void createTextures() throws IOException { public void createGlObjects() throws IOException, GlUtil.GlException {
eglDisplay = GlUtil.createEglDisplay();
eglContext = GlUtil.createEglContext(eglDisplay);
Bitmap inputBitmap = BitmapTestUtil.readBitmap(ORIGINAL_PNG_ASSET_PATH); Bitmap inputBitmap = BitmapTestUtil.readBitmap(ORIGINAL_PNG_ASSET_PATH);
width = inputBitmap.getWidth(); width = inputBitmap.getWidth();
height = inputBitmap.getHeight(); height = inputBitmap.getHeight();
...@@ -81,11 +79,13 @@ public final class MatrixTransformationProcessorPixelTest { ...@@ -81,11 +79,13 @@ public final class MatrixTransformationProcessorPixelTest {
} }
@After @After
public void release() { public void release() throws GlUtil.GlException, FrameProcessingException {
if (matrixTransformationFrameProcessor != null) { if (matrixTransformationFrameProcessor != null) {
matrixTransformationFrameProcessor.release(); matrixTransformationFrameProcessor.release();
} }
GlUtil.destroyEglContext(eglDisplay, eglContext); if (eglContext != null && eglDisplay != null) {
GlUtil.destroyEglContext(eglDisplay, eglContext);
}
} }
@Test @Test
......
...@@ -60,13 +60,9 @@ public final class PresentationPixelTest { ...@@ -60,13 +60,9 @@ public final class PresentationPixelTest {
public static final String ASPECT_RATIO_STRETCH_TO_FIT_WIDE_PNG_ASSET_PATH = public static final String ASPECT_RATIO_STRETCH_TO_FIT_WIDE_PNG_ASSET_PATH =
"media/bitmap/sample_mp4_first_frame/aspect_ratio_stretch_to_fit_wide.png"; "media/bitmap/sample_mp4_first_frame/aspect_ratio_stretch_to_fit_wide.png";
static { private Context context = getApplicationContext();
GlUtil.glAssertionsEnabled = true; private @MonotonicNonNull EGLDisplay eglDisplay;
} private @MonotonicNonNull EGLContext eglContext;
private final Context context = getApplicationContext();
private final EGLDisplay eglDisplay = GlUtil.createEglDisplay();
private final EGLContext eglContext = GlUtil.createEglContext(eglDisplay);
private @MonotonicNonNull SingleFrameGlTextureProcessor presentationTextureProcessor; private @MonotonicNonNull SingleFrameGlTextureProcessor presentationTextureProcessor;
private @MonotonicNonNull EGLSurface placeholderEglSurface; private @MonotonicNonNull EGLSurface placeholderEglSurface;
private int inputTexId; private int inputTexId;
...@@ -75,7 +71,9 @@ public final class PresentationPixelTest { ...@@ -75,7 +71,9 @@ public final class PresentationPixelTest {
private int inputHeight; private int inputHeight;
@Before @Before
public void createTextures() throws IOException { public void createGlObjects() throws IOException, GlUtil.GlException {
eglDisplay = GlUtil.createEglDisplay();
eglContext = GlUtil.createEglContext(eglDisplay);
Bitmap inputBitmap = BitmapTestUtil.readBitmap(ORIGINAL_PNG_ASSET_PATH); Bitmap inputBitmap = BitmapTestUtil.readBitmap(ORIGINAL_PNG_ASSET_PATH);
inputWidth = inputBitmap.getWidth(); inputWidth = inputBitmap.getWidth();
inputHeight = inputBitmap.getHeight(); inputHeight = inputBitmap.getHeight();
...@@ -85,11 +83,13 @@ public final class PresentationPixelTest { ...@@ -85,11 +83,13 @@ public final class PresentationPixelTest {
} }
@After @After
public void release() { public void release() throws GlUtil.GlException, FrameProcessingException {
if (presentationTextureProcessor != null) { if (presentationTextureProcessor != null) {
presentationTextureProcessor.release(); presentationTextureProcessor.release();
} }
GlUtil.destroyEglContext(eglDisplay, eglContext); if (eglContext != null && eglDisplay != null) {
GlUtil.destroyEglContext(eglDisplay, eglContext);
}
} }
@Test @Test
...@@ -282,12 +282,12 @@ public final class PresentationPixelTest { ...@@ -282,12 +282,12 @@ public final class PresentationPixelTest {
assertThat(averagePixelAbsoluteDifference).isAtMost(MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE); assertThat(averagePixelAbsoluteDifference).isAtMost(MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE);
} }
private void setupOutputTexture(int outputWidth, int outputHeight) { private void setupOutputTexture(int outputWidth, int outputHeight) throws GlUtil.GlException {
outputTexId = GlUtil.createTexture(outputWidth, outputHeight); outputTexId = GlUtil.createTexture(outputWidth, outputHeight);
int frameBuffer = GlUtil.createFboForTexture(outputTexId); int frameBuffer = GlUtil.createFboForTexture(outputTexId);
GlUtil.focusFramebuffer( GlUtil.focusFramebuffer(
eglDisplay, checkNotNull(eglDisplay),
eglContext, checkNotNull(eglContext),
checkNotNull(placeholderEglSurface), checkNotNull(placeholderEglSurface),
frameBuffer, frameBuffer,
outputWidth, outputWidth,
......
...@@ -30,10 +30,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -30,10 +30,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
*/ */
public final class Crop implements MatrixTransformation { public final class Crop implements MatrixTransformation {
static {
GlUtil.glAssertionsEnabled = true;
}
private final float left; private final float left;
private final float right; private final float right;
private final float bottom; private final float bottom;
......
...@@ -23,7 +23,6 @@ import android.graphics.Matrix; ...@@ -23,7 +23,6 @@ import android.graphics.Matrix;
import android.util.Size; import android.util.Size;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.util.GlUtil;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/** /**
...@@ -38,10 +37,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -38,10 +37,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
// TODO(b/218488308): Allow reconfiguration of the output size, as encoders may not support the // TODO(b/218488308): Allow reconfiguration of the output size, as encoders may not support the
// requested output resolution. // requested output resolution.
static {
GlUtil.glAssertionsEnabled = true;
}
private int outputRotationDegrees; private int outputRotationDegrees;
private @MonotonicNonNull Matrix transformationMatrix; private @MonotonicNonNull Matrix transformationMatrix;
......
...@@ -28,10 +28,6 @@ import java.io.IOException; ...@@ -28,10 +28,6 @@ import java.io.IOException;
/** Copies frames from an external texture and applies color transformations for HDR if needed. */ /** Copies frames from an external texture and applies color transformations for HDR if needed. */
/* package */ class ExternalTextureProcessor extends SingleFrameGlTextureProcessor { /* package */ class ExternalTextureProcessor extends SingleFrameGlTextureProcessor {
static {
GlUtil.glAssertionsEnabled = true;
}
private static final String VERTEX_SHADER_TEX_TRANSFORM_PATH = private static final String VERTEX_SHADER_TEX_TRANSFORM_PATH =
"shaders/vertex_shader_tex_transform_es2.glsl"; "shaders/vertex_shader_tex_transform_es2.glsl";
private static final String VERTEX_SHADER_TEX_TRANSFORM_ES3_PATH = private static final String VERTEX_SHADER_TEX_TRANSFORM_ES3_PATH =
...@@ -54,10 +50,10 @@ import java.io.IOException; ...@@ -54,10 +50,10 @@ import java.io.IOException;
* Creates a new instance. * Creates a new instance.
* *
* @param enableExperimentalHdrEditing Whether to attempt to process the input as an HDR signal. * @param enableExperimentalHdrEditing Whether to attempt to process the input as an HDR signal.
* @throws IOException If a problem occurs while reading shader files. * @throws FrameProcessingException If a problem occurs while reading shader files.
*/ */
public ExternalTextureProcessor(Context context, boolean enableExperimentalHdrEditing) public ExternalTextureProcessor(Context context, boolean enableExperimentalHdrEditing)
throws IOException { throws FrameProcessingException {
String vertexShaderFilePath = String vertexShaderFilePath =
enableExperimentalHdrEditing enableExperimentalHdrEditing
? VERTEX_SHADER_TEX_TRANSFORM_ES3_PATH ? VERTEX_SHADER_TEX_TRANSFORM_ES3_PATH
...@@ -66,7 +62,11 @@ import java.io.IOException; ...@@ -66,7 +62,11 @@ import java.io.IOException;
enableExperimentalHdrEditing enableExperimentalHdrEditing
? FRAGMENT_SHADER_COPY_EXTERNAL_YUV_ES3_PATH ? FRAGMENT_SHADER_COPY_EXTERNAL_YUV_ES3_PATH
: FRAGMENT_SHADER_COPY_EXTERNAL_PATH; : FRAGMENT_SHADER_COPY_EXTERNAL_PATH;
glProgram = new GlProgram(context, vertexShaderFilePath, fragmentShaderFilePath); try {
glProgram = new GlProgram(context, vertexShaderFilePath, fragmentShaderFilePath);
} catch (IOException | GlUtil.GlException e) {
throw new FrameProcessingException(e);
}
// Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y. // Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y.
glProgram.setBufferAttribute( glProgram.setBufferAttribute(
"aFramePosition", "aFramePosition",
...@@ -109,15 +109,19 @@ import java.io.IOException; ...@@ -109,15 +109,19 @@ import java.io.IOException;
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4);
GlUtil.checkGlError(); GlUtil.checkGlError();
} catch (GlUtil.GlException e) { } catch (GlUtil.GlException e) {
throw new FrameProcessingException(e); throw new FrameProcessingException(e, presentationTimeUs);
} }
} }
@Override @Override
public void release() { public void release() throws FrameProcessingException {
super.release(); super.release();
if (glProgram != null) { if (glProgram != null) {
glProgram.delete(); try {
glProgram.delete();
} catch (GlUtil.GlException e) {
throw new FrameProcessingException(e);
}
} }
} }
} }
...@@ -21,6 +21,26 @@ import com.google.android.exoplayer2.C; ...@@ -21,6 +21,26 @@ import com.google.android.exoplayer2.C;
public final class FrameProcessingException extends Exception { public final class FrameProcessingException extends Exception {
/** /**
* Wraps the given exception in a {@code FrameProcessingException} if it is not already a {@code
* FrameProcessingException} and returns the exception otherwise.
*/
public static FrameProcessingException from(Exception exception) {
return from(exception, /* presentationTimeUs= */ C.TIME_UNSET);
}
/**
* Wraps the given exception in a {@code FrameProcessingException} with the given timestamp if it
* is not already a {@code FrameProcessingException} and returns the exception otherwise.
*/
public static FrameProcessingException from(Exception exception, long presentationTimeUs) {
if (exception instanceof FrameProcessingException) {
return (FrameProcessingException) exception;
} else {
return new FrameProcessingException(exception, presentationTimeUs);
}
}
/**
* The microsecond timestamp of the frame being processed while the exception occurred or {@link * The microsecond timestamp of the frame being processed while the exception occurred or {@link
* C#TIME_UNSET} if unknown. * C#TIME_UNSET} if unknown.
*/ */
......
...@@ -41,7 +41,6 @@ import com.google.android.exoplayer2.util.GlUtil; ...@@ -41,7 +41,6 @@ import com.google.android.exoplayer2.util.GlUtil;
import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
...@@ -65,10 +64,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -65,10 +64,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
// TODO(b/227625423): Factor out FrameProcessor interface and rename this class to GlFrameProcessor. // TODO(b/227625423): Factor out FrameProcessor interface and rename this class to GlFrameProcessor.
/* package */ final class FrameProcessorChain { /* package */ final class FrameProcessorChain {
static {
GlUtil.glAssertionsEnabled = true;
}
/** /**
* Listener for asynchronous frame processing events. * Listener for asynchronous frame processing events.
* *
...@@ -150,7 +145,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -150,7 +145,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
List<GlEffect> effects, List<GlEffect> effects,
boolean enableExperimentalHdrEditing, boolean enableExperimentalHdrEditing,
ExecutorService singleThreadExecutorService) ExecutorService singleThreadExecutorService)
throws IOException { throws GlUtil.GlException, FrameProcessingException {
checkState(Thread.currentThread().getName().equals(THREAD_NAME)); checkState(Thread.currentThread().getName().equals(THREAD_NAME));
EGLDisplay eglDisplay = GlUtil.createEglDisplay(); EGLDisplay eglDisplay = GlUtil.createEglDisplay();
...@@ -204,7 +199,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -204,7 +199,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
ExternalTextureProcessor externalTextureProcessor, ExternalTextureProcessor externalTextureProcessor,
float pixelWidthHeightRatio, float pixelWidthHeightRatio,
List<GlEffect> effects) List<GlEffect> effects)
throws IOException { throws FrameProcessingException {
ImmutableList.Builder<SingleFrameGlTextureProcessor> textureProcessors = ImmutableList.Builder<SingleFrameGlTextureProcessor> textureProcessors =
new ImmutableList.Builder<SingleFrameGlTextureProcessor>().add(externalTextureProcessor); new ImmutableList.Builder<SingleFrameGlTextureProcessor>().add(externalTextureProcessor);
...@@ -532,22 +527,19 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -532,22 +527,19 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
int finalInputTexId = inputTexId; int finalInputTexId = inputTexId;
debugSurfaceViewWrapper.maybeRenderToSurfaceView( debugSurfaceViewWrapper.maybeRenderToSurfaceView(
() -> { () -> {
GlUtil.clearOutputFrame();
try { try {
GlUtil.clearOutputFrame();
getLast(textureProcessors).drawFrame(finalInputTexId, finalPresentationTimeUs); getLast(textureProcessors).drawFrame(finalInputTexId, finalPresentationTimeUs);
} catch (FrameProcessingException e) { } catch (GlUtil.GlException | FrameProcessingException e) {
Log.d(TAG, "Error rendering to debug preview", e); Log.d(TAG, "Error rendering to debug preview", e);
} }
}); });
} }
checkState(pendingFrameCount.getAndDecrement() > 0); checkState(pendingFrameCount.getAndDecrement() > 0);
} catch (FrameProcessingException | RuntimeException e) { } catch (FrameProcessingException | GlUtil.GlException | RuntimeException e) {
if (!stopProcessing.getAndSet(true)) { if (!stopProcessing.getAndSet(true)) {
listener.onFrameProcessingError( listener.onFrameProcessingError(FrameProcessingException.from(e, presentationTimeUs));
e instanceof FrameProcessingException
? (FrameProcessingException) e
: new FrameProcessingException(e, presentationTimeUs));
} }
} }
} }
...@@ -565,8 +557,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -565,8 +557,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
textureProcessors.get(i).release(); textureProcessors.get(i).release();
} }
GlUtil.destroyEglContext(eglDisplay, eglContext); GlUtil.destroyEglContext(eglDisplay, eglContext);
} catch (RuntimeException e) { } catch (FrameProcessingException | GlUtil.GlException | RuntimeException e) {
listener.onFrameProcessingError(new FrameProcessingException(e)); listener.onFrameProcessingError(FrameProcessingException.from(e));
} }
} }
...@@ -600,7 +592,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -600,7 +592,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
* otherwise. * otherwise.
*/ */
@WorkerThread @WorkerThread
public synchronized void maybeRenderToSurfaceView(Runnable renderRunnable) { public synchronized void maybeRenderToSurfaceView(Runnable renderRunnable)
throws GlUtil.GlException {
if (surface == null) { if (surface == null) {
return; return;
} }
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package com.google.android.exoplayer2.transformer; package com.google.android.exoplayer2.transformer;
import android.content.Context; import android.content.Context;
import java.io.IOException;
/** /**
* Interface for a video frame effect with a {@link SingleFrameGlTextureProcessor} implementation. * Interface for a video frame effect with a {@link SingleFrameGlTextureProcessor} implementation.
...@@ -29,5 +28,6 @@ public interface GlEffect { ...@@ -29,5 +28,6 @@ public interface GlEffect {
/** Returns a {@link SingleFrameGlTextureProcessor} that applies the effect. */ /** Returns a {@link SingleFrameGlTextureProcessor} that applies the effect. */
// TODO(b/227625423): use GlTextureProcessor here once this interface exists. // TODO(b/227625423): use GlTextureProcessor here once this interface exists.
SingleFrameGlTextureProcessor toGlTextureProcessor(Context context) throws IOException; SingleFrameGlTextureProcessor toGlTextureProcessor(Context context)
throws FrameProcessingException;
} }
...@@ -18,7 +18,6 @@ package com.google.android.exoplayer2.transformer; ...@@ -18,7 +18,6 @@ package com.google.android.exoplayer2.transformer;
import android.content.Context; import android.content.Context;
import android.opengl.Matrix; import android.opengl.Matrix;
import android.util.Size; import android.util.Size;
import java.io.IOException;
/** /**
* Specifies a 4x4 transformation {@link Matrix} to apply in the vertex shader for each frame. * Specifies a 4x4 transformation {@link Matrix} to apply in the vertex shader for each frame.
...@@ -50,7 +49,8 @@ public interface GlMatrixTransformation extends GlEffect { ...@@ -50,7 +49,8 @@ public interface GlMatrixTransformation extends GlEffect {
float[] getGlMatrixArray(long presentationTimeUs); float[] getGlMatrixArray(long presentationTimeUs);
@Override @Override
default SingleFrameGlTextureProcessor toGlTextureProcessor(Context context) throws IOException { default SingleFrameGlTextureProcessor toGlTextureProcessor(Context context)
throws FrameProcessingException {
return new MatrixTransformationProcessor(context, this); return new MatrixTransformationProcessor(context, this);
} }
} }
...@@ -110,6 +110,10 @@ public interface GlTextureProcessor { ...@@ -110,6 +110,10 @@ public interface GlTextureProcessor {
/** Notifies the texture processor that no further input frames will become available. */ /** Notifies the texture processor that no further input frames will become available. */
void signalEndOfInputStream(); void signalEndOfInputStream();
/** Releases all resources. */ /**
void release(); * Releases all resources.
*
* @throws FrameProcessingException If an error occurs while releasing resources.
*/
void release() throws FrameProcessingException;
} }
...@@ -41,10 +41,6 @@ import java.util.Arrays; ...@@ -41,10 +41,6 @@ import java.util.Arrays;
@SuppressWarnings("FunctionalInterfaceClash") // b/228192298 @SuppressWarnings("FunctionalInterfaceClash") // b/228192298
/* package */ final class MatrixTransformationProcessor extends SingleFrameGlTextureProcessor { /* package */ final class MatrixTransformationProcessor extends SingleFrameGlTextureProcessor {
static {
GlUtil.glAssertionsEnabled = true;
}
private static final String VERTEX_SHADER_TRANSFORMATION_PATH = private static final String VERTEX_SHADER_TRANSFORMATION_PATH =
"shaders/vertex_shader_transformation_es2.glsl"; "shaders/vertex_shader_transformation_es2.glsl";
private static final String FRAGMENT_SHADER_PATH = "shaders/fragment_shader_copy_es2.glsl"; private static final String FRAGMENT_SHADER_PATH = "shaders/fragment_shader_copy_es2.glsl";
...@@ -88,10 +84,10 @@ import java.util.Arrays; ...@@ -88,10 +84,10 @@ import java.util.Arrays;
* @param context The {@link Context}. * @param context The {@link Context}.
* @param matrixTransformation A {@link MatrixTransformation} that specifies the transformation * @param matrixTransformation A {@link MatrixTransformation} that specifies the transformation
* matrix to use for each frame. * matrix to use for each frame.
* @throws IOException If a problem occurs while reading shader files. * @throws FrameProcessingException If a problem occurs while reading shader files.
*/ */
public MatrixTransformationProcessor(Context context, MatrixTransformation matrixTransformation) public MatrixTransformationProcessor(Context context, MatrixTransformation matrixTransformation)
throws IOException { throws FrameProcessingException {
this(context, ImmutableList.of(matrixTransformation)); this(context, ImmutableList.of(matrixTransformation));
} }
...@@ -101,10 +97,10 @@ import java.util.Arrays; ...@@ -101,10 +97,10 @@ import java.util.Arrays;
* @param context The {@link Context}. * @param context The {@link Context}.
* @param matrixTransformation A {@link GlMatrixTransformation} that specifies the transformation * @param matrixTransformation A {@link GlMatrixTransformation} that specifies the transformation
* matrix to use for each frame. * matrix to use for each frame.
* @throws IOException If a problem occurs while reading shader files. * @throws FrameProcessingException If a problem occurs while reading shader files.
*/ */
public MatrixTransformationProcessor(Context context, GlMatrixTransformation matrixTransformation) public MatrixTransformationProcessor(Context context, GlMatrixTransformation matrixTransformation)
throws IOException { throws FrameProcessingException {
this(context, ImmutableList.of(matrixTransformation)); this(context, ImmutableList.of(matrixTransformation));
} }
...@@ -114,11 +110,11 @@ import java.util.Arrays; ...@@ -114,11 +110,11 @@ import java.util.Arrays;
* @param context The {@link Context}. * @param context The {@link Context}.
* @param matrixTransformations The {@link GlMatrixTransformation GlMatrixTransformations} to * @param matrixTransformations The {@link GlMatrixTransformation GlMatrixTransformations} to
* apply to each frame in order. * apply to each frame in order.
* @throws IOException If a problem occurs while reading shader files. * @throws FrameProcessingException If a problem occurs while reading shader files.
*/ */
public MatrixTransformationProcessor( public MatrixTransformationProcessor(
Context context, ImmutableList<GlMatrixTransformation> matrixTransformations) Context context, ImmutableList<GlMatrixTransformation> matrixTransformations)
throws IOException { throws FrameProcessingException {
this.matrixTransformations = matrixTransformations; this.matrixTransformations = matrixTransformations;
transformationMatrixCache = new float[matrixTransformations.size()][16]; transformationMatrixCache = new float[matrixTransformations.size()][16];
...@@ -126,7 +122,11 @@ import java.util.Arrays; ...@@ -126,7 +122,11 @@ import java.util.Arrays;
tempResultMatrix = new float[16]; tempResultMatrix = new float[16];
Matrix.setIdentityM(compositeTransformationMatrix, /* smOffset= */ 0); Matrix.setIdentityM(compositeTransformationMatrix, /* smOffset= */ 0);
visiblePolygon = NDC_SQUARE; visiblePolygon = NDC_SQUARE;
glProgram = new GlProgram(context, VERTEX_SHADER_TRANSFORMATION_PATH, FRAGMENT_SHADER_PATH); try {
glProgram = new GlProgram(context, VERTEX_SHADER_TRANSFORMATION_PATH, FRAGMENT_SHADER_PATH);
} catch (IOException | GlUtil.GlException e) {
throw new FrameProcessingException(e);
}
} }
@Override @Override
...@@ -168,9 +168,15 @@ import java.util.Arrays; ...@@ -168,9 +168,15 @@ import java.util.Arrays;
} }
@Override @Override
public void release() { public void release() throws FrameProcessingException {
super.release(); super.release();
glProgram.delete(); if (glProgram != null) {
try {
glProgram.delete();
} catch (GlUtil.GlException e) {
throw new FrameProcessingException(e);
}
}
} }
/** /**
......
...@@ -24,7 +24,6 @@ import android.graphics.Matrix; ...@@ -24,7 +24,6 @@ import android.graphics.Matrix;
import android.util.Size; import android.util.Size;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.GlUtil;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.Target; import java.lang.annotation.Target;
...@@ -162,10 +161,6 @@ public final class Presentation implements MatrixTransformation { ...@@ -162,10 +161,6 @@ public final class Presentation implements MatrixTransformation {
} }
} }
static {
GlUtil.glAssertionsEnabled = true;
}
private final int requestedHeightPixels; private final int requestedHeightPixels;
private final float requestedAspectRatio; private final float requestedAspectRatio;
private final @Layout int layout; private final @Layout int layout;
......
...@@ -83,10 +83,6 @@ public final class ScaleToFitTransformation implements MatrixTransformation { ...@@ -83,10 +83,6 @@ public final class ScaleToFitTransformation implements MatrixTransformation {
} }
} }
static {
GlUtil.glAssertionsEnabled = true;
}
private final Matrix transformationMatrix; private final Matrix transformationMatrix;
private @MonotonicNonNull Matrix adjustedTransformationMatrix; private @MonotonicNonNull Matrix adjustedTransformationMatrix;
......
...@@ -94,7 +94,7 @@ public abstract class SingleFrameGlTextureProcessor implements GlTextureProcesso ...@@ -94,7 +94,7 @@ public abstract class SingleFrameGlTextureProcessor implements GlTextureProcesso
listener.onInputFrameProcessed(inputTexture); listener.onInputFrameProcessed(inputTexture);
listener.onOutputFrameAvailable(outputTexture, presentationTimeUs); listener.onOutputFrameAvailable(outputTexture, presentationTimeUs);
} }
} catch (FrameProcessingException | RuntimeException e) { } catch (FrameProcessingException | GlUtil.GlException | RuntimeException e) {
if (listener != null) { if (listener != null) {
listener.onFrameProcessingError( listener.onFrameProcessingError(
e instanceof FrameProcessingException e instanceof FrameProcessingException
...@@ -106,7 +106,7 @@ public abstract class SingleFrameGlTextureProcessor implements GlTextureProcesso ...@@ -106,7 +106,7 @@ public abstract class SingleFrameGlTextureProcessor implements GlTextureProcesso
} }
@EnsuresNonNull("outputTexture") @EnsuresNonNull("outputTexture")
private void configureOutputTexture(int inputWidth, int inputHeight) { private void configureOutputTexture(int inputWidth, int inputHeight) throws GlUtil.GlException {
this.inputWidth = inputWidth; this.inputWidth = inputWidth;
this.inputHeight = inputHeight; this.inputHeight = inputHeight;
Size outputSize = configure(inputWidth, inputHeight); Size outputSize = configure(inputWidth, inputHeight);
...@@ -137,9 +137,13 @@ public abstract class SingleFrameGlTextureProcessor implements GlTextureProcesso ...@@ -137,9 +137,13 @@ public abstract class SingleFrameGlTextureProcessor implements GlTextureProcesso
@Override @Override
@CallSuper @CallSuper
public void release() { public void release() throws FrameProcessingException {
if (outputTexture != null) { if (outputTexture != null) {
GlUtil.deleteTexture(outputTexture.texId); try {
GlUtil.deleteTexture(outputTexture.texId);
} catch (GlUtil.GlException e) {
throw new FrameProcessingException(e);
}
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment