From e589718414d147a2c7d5c52d684715b575795cb9 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 (cherry picked from commit 93ee1f48ad5c9240d3d7b5f26039a98f4dc24b63) --- .../media3/common/util/GlProgram.java | 23 +++++++++++++--- .../androidx/media3/common/util/GlUtil.java | 26 ++----------------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/libraries/common/src/main/java/androidx/media3/common/util/GlProgram.java b/libraries/common/src/main/java/androidx/media3/common/util/GlProgram.java index a167c18541..579ce6fe5b 100644 --- a/libraries/common/src/main/java/androidx/media3/common/util/GlProgram.java +++ b/libraries/common/src/main/java/androidx/media3/common/util/GlProgram.java @@ -22,6 +22,7 @@ import android.opengl.GLES11Ext; 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; @@ -55,9 +56,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/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java b/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java index 67c39c576a..2e2c149ef3 100644 --- a/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java +++ b/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java @@ -32,8 +32,6 @@ import androidx.annotation.DoNotInline; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.media3.common.C; -import java.io.IOException; -import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; @@ -59,8 +57,6 @@ public final class GlUtil { /** 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 @@ -352,7 +348,7 @@ public final class GlUtil { * @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. @@ -460,29 +456,11 @@ public final class GlUtil { * * @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.