From 373db56a52e9943df79ff3540da061264d6dc4b9 Mon Sep 17 00:00:00 2001 From: kimvde Date: Thu, 2 Sep 2021 11:15:47 +0100 Subject: [PATCH] Add method to compile program from shader paths in GlUtil This method will be useful for adding Open GL to the Transformer. PiperOrigin-RevId: 394420744 --- .../gldemo/BitmapOverlayVideoProcessor.java | 27 +++++++------------ .../android/exoplayer2/util/GlUtil.java | 20 ++++++++++++++ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/BitmapOverlayVideoProcessor.java b/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/BitmapOverlayVideoProcessor.java index 38f5b7ff35..13e2a60684 100644 --- a/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/BitmapOverlayVideoProcessor.java +++ b/demos/gl/src/main/java/com/google/android/exoplayer2/gldemo/BitmapOverlayVideoProcessor.java @@ -28,9 +28,7 @@ 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 com.google.android.exoplayer2.util.Util; import java.io.IOException; -import java.io.InputStream; import java.util.Locale; import javax.microedition.khronos.opengles.GL10; @@ -79,10 +77,15 @@ import javax.microedition.khronos.opengles.GL10; @Override public void initialize() { - String vertexShaderCode = - loadAssetAsString(context, "bitmap_overlay_video_processor_vertex.glsl"); - String fragmentShaderCode = - loadAssetAsString(context, "bitmap_overlay_video_processor_fragment.glsl"); + String vertexShaderCode; + String fragmentShaderCode; + try { + 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 = GlUtil.compileProgram(vertexShaderCode, fragmentShaderCode); GlUtil.Attribute[] attributes = GlUtil.getAttributes(program); GlUtil.Uniform[] uniforms = GlUtil.getUniforms(program); @@ -156,16 +159,4 @@ import javax.microedition.khronos.opengles.GL10; GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); GlUtil.checkGlError(); } - - private static String loadAssetAsString(Context context, String assetFileName) { - @Nullable InputStream inputStream = null; - try { - inputStream = context.getAssets().open(assetFileName); - return Util.fromUtf8Bytes(Util.toByteArray(inputStream)); - } catch (IOException e) { - throw new IllegalStateException(e); - } finally { - Util.closeQuietly(inputStream); - } - } } diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java b/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java index 09fc24add7..245c827f9b 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/GlUtil.java @@ -31,6 +31,8 @@ import androidx.annotation.DoNotInline; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; +import java.io.IOException; +import java.io.InputStream; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -435,6 +437,24 @@ public final class GlUtil { return byteBuffer.order(ByteOrder.nativeOrder()).asFloatBuffer(); } + /** + * Loads a file from the assets folder. + * + * @param context The {@link Context}. + * @param assetPath The path to the file to load, from the assets folder. + * @return The content of the file to load. + * @throws IOException If the file couldn't be read. + */ + public static String loadAsset(Context context, String assetPath) throws IOException { + @Nullable InputStream inputStream = null; + try { + inputStream = context.getAssets().open(assetPath); + return Util.fromUtf8Bytes(Util.toByteArray(inputStream)); + } finally { + Util.closeQuietly(inputStream); + } + } + /** * Creates a GL_TEXTURE_EXTERNAL_OES with default configuration of GL_LINEAR filtering and * GL_CLAMP_TO_EDGE wrapping.