From a404fde4fa9d9d16b0be3c6e08dabd059c07f471 Mon Sep 17 00:00:00 2001 From: huangdarwin Date: Tue, 18 Oct 2022 14:10:39 +0000 Subject: [PATCH] GL: Move loadAsset to GlProgram, where it's used. (Also, make some public methods private) PiperOrigin-RevId: 481912071 --- .../android/exoplayer2/util/GlProgram.java | 23 +++++++++++++--- .../android/exoplayer2/util/GlUtil.java | 26 ++----------------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/GlProgram.java b/library/common/src/main/java/com/google/android/exoplayer2/util/GlProgram.java index a3fda298e7c..18a1cc60bc3 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/GlProgram.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/GlProgram.java @@ -22,6 +22,7 @@ import android.opengl.GLES20; import androidx.annotation.Nullable; import java.io.IOException; +import java.io.InputStream; import java.nio.Buffer; import java.util.HashMap; import java.util.Map; @@ -54,9 +55,25 @@ public final class GlProgram { */ public GlProgram(Context context, String vertexShaderFilePath, String fragmentShaderFilePath) throws IOException, GlUtil.GlException { - this( - GlUtil.loadAsset(context, vertexShaderFilePath), - GlUtil.loadAsset(context, fragmentShaderFilePath)); + this(loadAsset(context, vertexShaderFilePath), loadAsset(context, fragmentShaderFilePath)); + } + + /** + * 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); + } } /** 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 b0fc5feed67..438157576b4 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 @@ -32,8 +32,6 @@ 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.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; @@ -58,8 +56,6 @@ public GlException(String message) { /** Length of the normalized device coordinate (NDC) space, which spans from -1 to 1. */ public static final float LENGTH_NDC = 2f; - private static final String TAG = "GlUtil"; - // https://www.khronos.org/registry/EGL/extensions/EXT/EGL_EXT_protected_content.txt private static final String EXTENSION_PROTECTED_CONTENT = "EGL_EXT_protected_content"; // https://www.khronos.org/registry/EGL/extensions/KHR/EGL_KHR_surfaceless_context.txt @@ -351,7 +347,7 @@ public static void checkGlError() throws GlException { * @param height The height for a texture. * @throws GlException If the texture width or height is invalid. */ - public static void assertValidTextureSize(int width, int height) throws GlException { + private static void assertValidTextureSize(int width, int height) throws GlException { // TODO(b/201293185): Consider handling adjustments for sizes > GL_MAX_TEXTURE_SIZE // (ex. downscaling appropriately) in a texture processor instead of asserting incorrect // values. @@ -459,29 +455,11 @@ public static FloatBuffer createBuffer(float[] data) { * * @param capacity The new buffer's capacity, in floats. */ - public static FloatBuffer createBuffer(int capacity) { + private static FloatBuffer createBuffer(int capacity) { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(capacity * C.BYTES_PER_FLOAT); 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.