Create an explicit GlRuntimeException class

PiperOrigin-RevId: 590035213
This commit is contained in:
MediaPipe Team 2023-12-11 19:56:17 -08:00 committed by Copybara-Service
parent bd946db5a6
commit 9a20d6b3e4
2 changed files with 34 additions and 18 deletions

View File

@ -18,6 +18,7 @@ import android.opengl.GLES20;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.util.Log; import android.util.Log;
import com.google.mediapipe.glutil.ShaderUtil.GlRuntimeException;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.microedition.khronos.egl.EGLContext; import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLSurface; import javax.microedition.khronos.egl.EGLSurface;
@ -111,7 +112,7 @@ public class GlThread extends Thread {
0); 0);
int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER); int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) { if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
throw new RuntimeException("Framebuffer not complete, status=" + status); throw new GlRuntimeException("Framebuffer not complete, status=" + status);
} }
GLES20.glViewport(0, 0, width, height); GLES20.glViewport(0, 0, width, height);
ShaderUtil.checkGlError("glViewport"); ShaderUtil.checkGlError("glViewport");

View File

@ -24,16 +24,15 @@ import java.nio.FloatBuffer;
import java.util.Map; import java.util.Map;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/** /** Utility class for managing GLSL shaders. */
* Utility class for managing GLSL shaders.
*/
public class ShaderUtil { public class ShaderUtil {
private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private static final FluentLogger logger = FluentLogger.forEnclosingClass();
/** /**
* Loads a shader from source. * Loads a shader from source.
* @param shaderType a valid GL shader type, e.g. {@link GLES20#GL_VERTEX_SHADER} or *
* {@link GLES20#GL_FRAGMENT_SHADER}. * @param shaderType a valid GL shader type, e.g. {@link GLES20#GL_VERTEX_SHADER} or {@link
* GLES20#GL_FRAGMENT_SHADER}.
* @param source the shader's source in text form. * @param source the shader's source in text form.
* @return a handle to the created shader, or 0 in case of error. * @return a handle to the created shader, or 0 in case of error.
*/ */
@ -44,8 +43,8 @@ public class ShaderUtil {
int[] compiled = new int[1]; int[] compiled = new int[1];
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0) { if (compiled[0] == 0) {
logger.atSevere().log("Could not compile shader %d: %s", shaderType, logger.atSevere().log(
GLES20.glGetShaderInfoLog(shader)); "Could not compile shader %d: %s", shaderType, GLES20.glGetShaderInfoLog(shader));
GLES20.glDeleteShader(shader); GLES20.glDeleteShader(shader);
shader = 0; shader = 0;
} }
@ -99,6 +98,7 @@ public class ShaderUtil {
/** /**
* Creates a texture. Binds it to texture unit 0 to perform setup. * Creates a texture. Binds it to texture unit 0 to perform setup.
*
* @return the name of the new texture. * @return the name of the new texture.
*/ */
public static int createRgbaTexture(int width, int height) { public static int createRgbaTexture(int width, int height) {
@ -111,7 +111,8 @@ public class ShaderUtil {
GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_2D,
0, 0,
GLES20.GL_RGBA, GLES20.GL_RGBA,
width, height, width,
height,
0, 0,
GLES20.GL_RGBA, GLES20.GL_RGBA,
GLES20.GL_UNSIGNED_BYTE, GLES20.GL_UNSIGNED_BYTE,
@ -147,13 +148,11 @@ public class ShaderUtil {
} }
/** /**
* Creates a {@link FloatBuffer} with the given arguments as contents. * Creates a {@link FloatBuffer} with the given arguments as contents. The buffer is created in
* The buffer is created in native format for efficient use with OpenGL. * native format for efficient use with OpenGL.
*/ */
public static FloatBuffer floatBuffer(float... values) { public static FloatBuffer floatBuffer(float... values) {
ByteBuffer byteBuffer = ByteBuffer byteBuffer = ByteBuffer.allocateDirect(values.length * 4 /* sizeof(float) */);
ByteBuffer.allocateDirect(
values.length * 4 /* sizeof(float) */);
// use the device hardware's native byte order // use the device hardware's native byte order
byteBuffer.order(ByteOrder.nativeOrder()); byteBuffer.order(ByteOrder.nativeOrder());
@ -166,13 +165,29 @@ public class ShaderUtil {
return floatBuffer; return floatBuffer;
} }
/** /** Calls {@link GLES20#glGetError} and raises an exception if there was an error. */
* Calls {@link GLES20#glGetError} and raises an exception if there was an error.
*/
public static void checkGlError(String msg) { public static void checkGlError(String msg) {
int error = GLES20.glGetError(); int error = GLES20.glGetError();
if (error != GLES20.GL_NO_ERROR) { if (error != GLES20.GL_NO_ERROR) {
throw new RuntimeException(msg + ": GL error: 0x" + Integer.toHexString(error)); throw new GlRuntimeException(msg + ": GL error: 0x" + Integer.toHexString(error), error);
}
}
/** A custom {@link RuntimeException} indicating an OpenGl error. */
public static class GlRuntimeException extends RuntimeException {
private final int errorCode;
public GlRuntimeException(String message, int errorCode) {
super(message);
this.errorCode = errorCode;
}
public GlRuntimeException(String message) {
this(message, GLES20.GL_NO_ERROR);
}
public int getErrorCode() {
return errorCode;
} }
} }
} }