Commit 3eda590c by claincly Committed by bachinger

Rollback of https://github.com/google/ExoPlayer/commit/9788750ddb23b2064dddf99d6e1ea491b2e45cea

*** Original commit ***

Simplify GL program handling.

***

PiperOrigin-RevId: 400970170
parent 80d36516
......@@ -15,8 +15,6 @@
*/
package com.google.android.exoplayer2.gldemo;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
......@@ -28,6 +26,7 @@ import android.opengl.GLES20;
import android.opengl.GLUtils;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.GlUtil;
import java.io.IOException;
import java.util.Locale;
......@@ -50,7 +49,7 @@ import javax.microedition.khronos.opengles.GL10;
private final Bitmap logoBitmap;
private final Canvas overlayCanvas;
@Nullable private GlUtil.Program program;
private int program;
@Nullable private GlUtil.Attribute[] attributes;
@Nullable private GlUtil.Uniform[] uniforms;
......@@ -78,40 +77,27 @@ import javax.microedition.khronos.opengles.GL10;
@Override
public void initialize() {
String vertexShaderCode;
String fragmentShaderCode;
try {
program =
new GlUtil.Program(
context,
/* vertexShaderFilePath= */ "bitmap_overlay_video_processor_vertex.glsl",
/* fragmentShaderFilePath= */ "bitmap_overlay_video_processor_fragment.glsl");
vertexShaderCode = GlUtil.loadAsset(context, "bitmap_overlay_video_processor_vertex.glsl");
fragmentShaderCode =
GlUtil.loadAsset(context, "bitmap_overlay_video_processor_fragment.glsl");
} catch (IOException e) {
throw new IllegalStateException(e);
}
program.use();
GlUtil.Attribute[] attributes = program.getAttributes();
program = GlUtil.compileProgram(vertexShaderCode, fragmentShaderCode);
GlUtil.Attribute[] attributes = GlUtil.getAttributes(program);
GlUtil.Uniform[] uniforms = GlUtil.getUniforms(program);
for (GlUtil.Attribute attribute : attributes) {
if (attribute.name.equals("a_position")) {
attribute.setBuffer(
new float[] {
-1, -1, 0, 1,
1, -1, 0, 1,
-1, 1, 0, 1,
1, 1, 0, 1
},
4);
attribute.setBuffer(new float[] {-1, -1, 0, 1, 1, -1, 0, 1, -1, 1, 0, 1, 1, 1, 0, 1}, 4);
} else if (attribute.name.equals("a_texcoord")) {
attribute.setBuffer(
new float[] {
0, 0, 0, 1,
1, 0, 0, 1,
0, 1, 0, 1,
1, 1, 0, 1
},
4);
attribute.setBuffer(new float[] {0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1}, 4);
}
}
this.attributes = attributes;
this.uniforms = checkNotNull(program).getUniforms();
this.uniforms = uniforms;
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
......@@ -140,8 +126,9 @@ import javax.microedition.khronos.opengles.GL10;
GlUtil.checkGlError();
// Run the shader program.
GlUtil.Uniform[] uniforms = checkNotNull(this.uniforms);
GlUtil.Attribute[] attributes = checkNotNull(this.attributes);
GlUtil.Uniform[] uniforms = Assertions.checkNotNull(this.uniforms);
GlUtil.Attribute[] attributes = Assertions.checkNotNull(this.attributes);
GLES20.glUseProgram(program);
for (GlUtil.Uniform uniform : uniforms) {
switch (uniform.name) {
case "tex_sampler_0":
......
......@@ -15,8 +15,6 @@
*/
package com.google.android.exoplayer2.video;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
......@@ -142,7 +140,7 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView
// glDrawArrays uses it.
private final FloatBuffer[] textureCoords;
@Nullable private GlUtil.Program program;
private int program;
private int colorMatrixLocation;
// Accessed only from the GL thread.
......@@ -163,9 +161,9 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView
@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
program = new GlUtil.Program(VERTEX_SHADER, FRAGMENT_SHADER);
program.use();
int posLocation = program.glGetAttribLocation("in_pos");
program = GlUtil.compileProgram(VERTEX_SHADER, FRAGMENT_SHADER);
GLES20.glUseProgram(program);
int posLocation = GLES20.glGetAttribLocation(program, "in_pos");
GLES20.glEnableVertexAttribArray(posLocation);
GLES20.glVertexAttribPointer(
posLocation,
......@@ -174,14 +172,14 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView
/* normalized= */ false,
/* stride= */ 0,
TEXTURE_VERTICES);
texLocations[0] = checkNotNull(program).glGetAttribLocation("in_tc_y");
texLocations[0] = GLES20.glGetAttribLocation(program, "in_tc_y");
GLES20.glEnableVertexAttribArray(texLocations[0]);
texLocations[1] = checkNotNull(program).glGetAttribLocation("in_tc_u");
texLocations[1] = GLES20.glGetAttribLocation(program, "in_tc_u");
GLES20.glEnableVertexAttribArray(texLocations[1]);
texLocations[2] = checkNotNull(program).glGetAttribLocation("in_tc_v");
texLocations[2] = GLES20.glGetAttribLocation(program, "in_tc_v");
GLES20.glEnableVertexAttribArray(texLocations[2]);
GlUtil.checkGlError();
colorMatrixLocation = checkNotNull(program).glGetUniformLocation("mColorConversion");
colorMatrixLocation = GLES20.glGetUniformLocation(program, "mColorConversion");
GlUtil.checkGlError();
setupTextures();
GlUtil.checkGlError();
......@@ -298,7 +296,7 @@ public final class VideoDecoderGLSurfaceView extends GLSurfaceView
private void setupTextures() {
GLES20.glGenTextures(3, yuvTextures, /* offset= */ 0);
for (int i = 0; i < 3; i++) {
GLES20.glUniform1i(checkNotNull(program).glGetUniformLocation(TEXTURE_UNIFORMS[i]), i);
GLES20.glUniform1i(GLES20.glGetUniformLocation(program, TEXTURE_UNIFORMS[i]), i);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, yuvTextures[i]);
GLES20.glTexParameterf(
......
......@@ -15,7 +15,6 @@
*/
package com.google.android.exoplayer2.video.spherical;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.GlUtil.checkGlError;
import android.opengl.GLES11Ext;
......@@ -91,12 +90,11 @@ import java.nio.FloatBuffer;
};
private int stereoMode;
@Nullable private GlUtil.Program program;
@Nullable private MeshData leftMeshData;
@Nullable private MeshData rightMeshData;
// Program related GL items. These are only valid if program != 0.
private int program;
private int mvpMatrixHandle;
private int uTexMatrixHandle;
private int positionHandle;
......@@ -121,12 +119,12 @@ import java.nio.FloatBuffer;
/** Initializes of the GL components. */
/* package */ void init() {
program = new GlUtil.Program(VERTEX_SHADER_CODE, FRAGMENT_SHADER_CODE);
mvpMatrixHandle = program.glGetUniformLocation("uMvpMatrix");
uTexMatrixHandle = program.glGetUniformLocation("uTexMatrix");
positionHandle = program.glGetAttribLocation("aPosition");
texCoordsHandle = program.glGetAttribLocation("aTexCoords");
textureHandle = program.glGetUniformLocation("uTexture");
program = GlUtil.compileProgram(VERTEX_SHADER_CODE, FRAGMENT_SHADER_CODE);
mvpMatrixHandle = GLES20.glGetUniformLocation(program, "uMvpMatrix");
uTexMatrixHandle = GLES20.glGetUniformLocation(program, "uTexMatrix");
positionHandle = GLES20.glGetAttribLocation(program, "aPosition");
texCoordsHandle = GLES20.glGetAttribLocation(program, "aTexCoords");
textureHandle = GLES20.glGetUniformLocation(program, "uTexture");
}
/**
......@@ -145,7 +143,7 @@ import java.nio.FloatBuffer;
}
// Configure shader.
checkNotNull(program).use();
GLES20.glUseProgram(program);
checkGlError();
GLES20.glEnableVertexAttribArray(positionHandle);
......@@ -198,9 +196,8 @@ import java.nio.FloatBuffer;
/** Cleans up the GL resources. */
/* package */ void shutdown() {
if (program != null) {
program.delete();
program = null;
if (program != 0) {
GLES20.glDeleteProgram(program);
}
}
......
......@@ -186,7 +186,6 @@ import java.nio.ByteBuffer;
}
eglSurface =
GlUtil.getEglSurface(eglDisplay, checkNotNull(checkNotNull(encoder).getInputSurface()));
GlUtil.focusSurface(
eglDisplay,
eglContext,
......@@ -194,20 +193,17 @@ import java.nio.ByteBuffer;
encoderConfigurationOutputFormat.width,
encoderConfigurationOutputFormat.height);
decoderTextureId = GlUtil.createExternalTexture();
GlUtil.Program copyProgram;
String vertexShaderCode;
String fragmentShaderCode;
try {
copyProgram =
new GlUtil.Program(
context,
/* vertexShaderFilePath= */ "shaders/blit_vertex_shader.glsl",
/* fragmentShaderFilePath= */ "shaders/copy_external_fragment_shader.glsl");
vertexShaderCode = GlUtil.loadAsset(context, "shaders/blit_vertex_shader.glsl");
fragmentShaderCode = GlUtil.loadAsset(context, "shaders/copy_external_fragment_shader.glsl");
} catch (IOException e) {
throw new IllegalStateException(e);
}
copyProgram.use();
GlUtil.Attribute[] copyAttributes = copyProgram.getAttributes();
int copyProgram = GlUtil.compileProgram(vertexShaderCode, fragmentShaderCode);
GLES20.glUseProgram(copyProgram);
GlUtil.Attribute[] copyAttributes = GlUtil.getAttributes(copyProgram);
checkState(copyAttributes.length == 2, "Expected program to have two vertex attributes.");
for (GlUtil.Attribute copyAttribute : copyAttributes) {
if (copyAttribute.name.equals("a_position")) {
......@@ -233,7 +229,7 @@ import java.nio.ByteBuffer;
}
copyAttribute.bind();
}
GlUtil.Uniform[] copyUniforms = copyProgram.getUniforms();
GlUtil.Uniform[] copyUniforms = GlUtil.getUniforms(copyProgram);
checkState(copyUniforms.length == 2, "Expected program to have two uniforms.");
for (GlUtil.Uniform copyUniform : copyUniforms) {
if (copyUniform.name.equals("tex_sampler")) {
......
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