GL: Move loadAsset to GlProgram, where it's used.

(Also, make some public methods private)

PiperOrigin-RevId: 481912071
This commit is contained in:
huangdarwin 2022-10-18 14:10:39 +00:00 committed by Marc Baechinger
parent bd9181e6ba
commit a404fde4fa
2 changed files with 22 additions and 27 deletions

View File

@ -22,6 +22,7 @@ import android.opengl.GLES11Ext;
import android.opengl.GLES20; import android.opengl.GLES20;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.Buffer; import java.nio.Buffer;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -54,9 +55,25 @@ public final class GlProgram {
*/ */
public GlProgram(Context context, String vertexShaderFilePath, String fragmentShaderFilePath) public GlProgram(Context context, String vertexShaderFilePath, String fragmentShaderFilePath)
throws IOException, GlUtil.GlException { throws IOException, GlUtil.GlException {
this( this(loadAsset(context, vertexShaderFilePath), loadAsset(context, fragmentShaderFilePath));
GlUtil.loadAsset(context, vertexShaderFilePath), }
GlUtil.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);
}
} }
/** /**

View File

@ -32,8 +32,6 @@ import androidx.annotation.DoNotInline;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
@ -58,8 +56,6 @@ public final class GlUtil {
/** Length of the normalized device coordinate (NDC) space, which spans from -1 to 1. */ /** Length of the normalized device coordinate (NDC) space, which spans from -1 to 1. */
public static final float LENGTH_NDC = 2f; 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 // https://www.khronos.org/registry/EGL/extensions/EXT/EGL_EXT_protected_content.txt
private static final String EXTENSION_PROTECTED_CONTENT = "EGL_EXT_protected_content"; private static final String EXTENSION_PROTECTED_CONTENT = "EGL_EXT_protected_content";
// https://www.khronos.org/registry/EGL/extensions/KHR/EGL_KHR_surfaceless_context.txt // https://www.khronos.org/registry/EGL/extensions/KHR/EGL_KHR_surfaceless_context.txt
@ -351,7 +347,7 @@ public final class GlUtil {
* @param height The height for a texture. * @param height The height for a texture.
* @throws GlException If the texture width or height is invalid. * @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 // TODO(b/201293185): Consider handling adjustments for sizes > GL_MAX_TEXTURE_SIZE
// (ex. downscaling appropriately) in a texture processor instead of asserting incorrect // (ex. downscaling appropriately) in a texture processor instead of asserting incorrect
// values. // values.
@ -459,29 +455,11 @@ public final class GlUtil {
* *
* @param capacity The new buffer's capacity, in floats. * @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); ByteBuffer byteBuffer = ByteBuffer.allocateDirect(capacity * C.BYTES_PER_FLOAT);
return byteBuffer.order(ByteOrder.nativeOrder()).asFloatBuffer(); 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 * Creates a GL_TEXTURE_EXTERNAL_OES with default configuration of GL_LINEAR filtering and
* GL_CLAMP_TO_EDGE wrapping. * GL_CLAMP_TO_EDGE wrapping.