HDR: Support RGBA_1010102 in GlUtil as a parameter.

Make it easier to support use of RGBA_101012 rather than RGBA_8888 for EGL
contexts, displays, and surfaces.

This tangentially supports adding HDR tests, by slightly simplifying the color
selection logic we'd have to add in HDR tests.

PiperOrigin-RevId: 482219428
(cherry picked from commit bb215857e5fd0768ce53c7c61ba91c48dab14bd2)
This commit is contained in:
huangdarwin 2022-10-19 16:04:41 +00:00 committed by microkatz
parent b0ad5c2681
commit ebc6b67e72
11 changed files with 103 additions and 97 deletions

View File

@ -16,6 +16,7 @@
package androidx.media3.common.util;
import static android.opengl.GLU.gluErrorString;
import static androidx.media3.common.util.Assertions.checkArgument;
import static androidx.media3.common.util.Assertions.checkState;
import android.content.Context;
@ -35,6 +36,7 @@ import androidx.media3.common.C;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Arrays;
import java.util.List;
import javax.microedition.khronos.egl.EGL10;
@ -57,15 +59,7 @@ 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;
// 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
private static final String EXTENSION_SURFACELESS_CONTEXT = "EGL_KHR_surfaceless_context";
// https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_YUV_target.txt
private static final String EXTENSION_YUV_TARGET = "GL_EXT_YUV_target";
private static final int[] EGL_WINDOW_SURFACE_ATTRIBUTES_NONE = new int[] {EGL14.EGL_NONE};
private static final int[] EGL_CONFIG_ATTRIBUTES_RGBA_8888 =
public static final int[] EGL_CONFIG_ATTRIBUTES_RGBA_8888 =
new int[] {
EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
EGL14.EGL_RED_SIZE, /* redSize= */ 8,
@ -76,7 +70,7 @@ public final class GlUtil {
EGL14.EGL_STENCIL_SIZE, /* stencilSize= */ 0,
EGL14.EGL_NONE
};
private static final int[] EGL_CONFIG_ATTRIBUTES_RGBA_1010102 =
public static final int[] EGL_CONFIG_ATTRIBUTES_RGBA_1010102 =
new int[] {
EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
EGL14.EGL_RED_SIZE, /* redSize= */ 10,
@ -88,6 +82,15 @@ public final class GlUtil {
EGL14.EGL_NONE
};
// 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
private static final String EXTENSION_SURFACELESS_CONTEXT = "EGL_KHR_surfaceless_context";
// https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_YUV_target.txt
private static final String EXTENSION_YUV_TARGET = "GL_EXT_YUV_target";
private static final int[] EGL_WINDOW_SURFACE_ATTRIBUTES_NONE = new int[] {EGL14.EGL_NONE};
/** Class only contains static methods. */
private GlUtil() {}
@ -187,12 +190,7 @@ public final class GlUtil {
try {
EGLDisplay eglDisplay = createEglDisplay();
EGLContext eglContext = createEglContext(eglDisplay);
if (GlUtil.isSurfacelessContextExtensionSupported()) {
focusEglSurface(
eglDisplay, eglContext, EGL14.EGL_NO_SURFACE, /* width= */ 1, /* height= */ 1);
} else {
focusPlaceholderEglSurface(eglContext, eglDisplay);
}
glExtensions = GLES20.glGetString(GLES20.GL_EXTENSIONS);
destroyEglContext(eglDisplay, eglContext);
} catch (GlException e) {
@ -211,25 +209,45 @@ public final class GlUtil {
return Api17.createEglDisplay();
}
/** Returns a new {@link EGLContext} for the specified {@link EGLDisplay}. */
/**
* Creates a new {@link EGLContext} for the specified {@link EGLDisplay}.
*
* <p>Configures the {@link EGLContext} with {@link #EGL_CONFIG_ATTRIBUTES_RGBA_8888} and OpenGL
* ES 2.0.
*
* @param eglDisplay The {@link EGLDisplay} to create an {@link EGLContext} for.
*/
@RequiresApi(17)
public static EGLContext createEglContext(EGLDisplay eglDisplay) throws GlException {
return Api17.createEglContext(eglDisplay, /* version= */ 2, EGL_CONFIG_ATTRIBUTES_RGBA_8888);
return createEglContext(eglDisplay, EGL_CONFIG_ATTRIBUTES_RGBA_8888);
}
/**
* Returns a new {@link EGLContext} for the specified {@link EGLDisplay}, requesting ES 3 and an
* RGBA 1010102 config.
* Creates a new {@link EGLContext} for the specified {@link EGLDisplay}.
*
* @param eglDisplay The {@link EGLDisplay} to create an {@link EGLContext} for.
* @param configAttributes The attributes to configure EGL with. Accepts either {@link
* #EGL_CONFIG_ATTRIBUTES_RGBA_1010102}, which will request OpenGL ES 3.0, or {@link
* #EGL_CONFIG_ATTRIBUTES_RGBA_8888}, which will request OpenGL ES 2.0.
*/
@RequiresApi(17)
public static EGLContext createEglContextEs3Rgba1010102(EGLDisplay eglDisplay)
public static EGLContext createEglContext(EGLDisplay eglDisplay, int[] configAttributes)
throws GlException {
return Api17.createEglContext(eglDisplay, /* version= */ 3, EGL_CONFIG_ATTRIBUTES_RGBA_1010102);
checkArgument(
Arrays.equals(configAttributes, EGL_CONFIG_ATTRIBUTES_RGBA_8888)
|| Arrays.equals(configAttributes, EGL_CONFIG_ATTRIBUTES_RGBA_1010102));
return Api17.createEglContext(
eglDisplay,
/* version= */ Arrays.equals(configAttributes, EGL_CONFIG_ATTRIBUTES_RGBA_1010102) ? 3 : 2,
configAttributes);
}
/**
* Returns a new {@link EGLSurface} wrapping the specified {@code surface}.
*
* <p>The {@link EGLSurface} will configure with {@link #EGL_CONFIG_ATTRIBUTES_RGBA_8888} and
* OpenGL ES 2.0.
*
* @param eglDisplay The {@link EGLDisplay} to attach the surface to.
* @param surface The surface to wrap; must be a surface, surface texture or surface holder.
*/
@ -240,19 +258,18 @@ public final class GlUtil {
}
/**
* Returns a new RGBA 1010102 {@link EGLSurface} wrapping the specified {@code surface}.
* Returns a new {@link EGLSurface} wrapping the specified {@code surface}.
*
* @param eglDisplay The {@link EGLDisplay} to attach the surface to.
* @param surface The surface to wrap; must be a surface, surface texture or surface holder.
* @param configAttributes The attributes to configure EGL with. Accepts {@link
* #EGL_CONFIG_ATTRIBUTES_RGBA_1010102} and {@link #EGL_CONFIG_ATTRIBUTES_RGBA_8888}.
*/
@RequiresApi(17)
public static EGLSurface getEglSurfaceRgba1010102(EGLDisplay eglDisplay, Object surface)
throws GlException {
public static EGLSurface getEglSurface(
EGLDisplay eglDisplay, Object surface, int[] configAttributes) throws GlException {
return Api17.getEglSurface(
eglDisplay,
surface,
EGL_CONFIG_ATTRIBUTES_RGBA_1010102,
EGL_WINDOW_SURFACE_ATTRIBUTES_NONE);
eglDisplay, surface, configAttributes, EGL_WINDOW_SURFACE_ATTRIBUTES_NONE);
}
/**
@ -277,48 +294,46 @@ public final class GlUtil {
}
/**
* Returns a placeholder {@link EGLSurface} to use when reading and writing to the surface is not
* required.
* Creates and focuses a placeholder {@link EGLSurface}.
*
* <p>This makes a {@link EGLContext} current when reading and writing to a surface is not
* required, configured with {@link #EGL_CONFIG_ATTRIBUTES_RGBA_8888}.
*
* @param eglContext The {@link EGLContext} to make current.
* @param eglDisplay The {@link EGLDisplay} to attach the surface to.
* @return {@link EGL14#EGL_NO_SURFACE} if supported and a 1x1 pixel buffer surface otherwise.
*/
@RequiresApi(17)
public static EGLSurface createPlaceholderEglSurface(EGLDisplay eglDisplay) throws GlException {
return isSurfacelessContextExtensionSupported()
? EGL14.EGL_NO_SURFACE
: createPbufferSurface(
eglDisplay, /* width= */ 1, /* height= */ 1, EGL_CONFIG_ATTRIBUTES_RGBA_8888);
}
/**
* Creates and focuses a new {@link EGLSurface} wrapping a 1x1 pixel buffer.
*
* @param eglContext The {@link EGLContext} to make current.
* @param eglDisplay The {@link EGLDisplay} to attach the surface to.
*/
@RequiresApi(17)
public static void focusPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay)
public static EGLSurface focusPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay)
throws GlException {
EGLSurface eglSurface =
createPbufferSurface(
eglDisplay, /* width= */ 1, /* height= */ 1, EGL_CONFIG_ATTRIBUTES_RGBA_8888);
focusEglSurface(eglDisplay, eglContext, eglSurface, /* width= */ 1, /* height= */ 1);
return createFocusedPlaceholderEglSurface(
eglContext, eglDisplay, EGL_CONFIG_ATTRIBUTES_RGBA_8888);
}
/**
* Creates and focuses a new RGBA 1010102 {@link EGLSurface} wrapping a 1x1 pixel buffer.
* Creates and focuses a placeholder {@link EGLSurface}.
*
* <p>This makes a {@link EGLContext} current when reading and writing to a surface is not
* required.
*
* @param eglContext The {@link EGLContext} to make current.
* @param eglDisplay The {@link EGLDisplay} to attach the surface to.
* @param configAttributes The attributes to configure EGL with. Accepts {@link
* #EGL_CONFIG_ATTRIBUTES_RGBA_1010102} and {@link #EGL_CONFIG_ATTRIBUTES_RGBA_8888}.
* @return A placeholder {@link EGLSurface} that has been focused to allow rendering to take
* place, or {@link EGL14#EGL_NO_SURFACE} if the current context supports rendering without a
* surface.
*/
@RequiresApi(17)
public static void focusPlaceholderEglSurfaceRgba1010102(
EGLContext eglContext, EGLDisplay eglDisplay) throws GlException {
public static EGLSurface createFocusedPlaceholderEglSurface(
EGLContext eglContext, EGLDisplay eglDisplay, int[] configAttributes) throws GlException {
EGLSurface eglSurface =
createPbufferSurface(
eglDisplay, /* width= */ 1, /* height= */ 1, EGL_CONFIG_ATTRIBUTES_RGBA_1010102);
isSurfacelessContextExtensionSupported()
? EGL14.EGL_NO_SURFACE
: createPbufferSurface(eglDisplay, /* width= */ 1, /* height= */ 1, configAttributes);
focusEglSurface(eglDisplay, eglContext, eglSurface, /* width= */ 1, /* height= */ 1);
return eglSurface;
}
/**

View File

@ -79,13 +79,11 @@ public class ContrastPixelTest {
public void createGlObjects() throws Exception {
eglDisplay = GlUtil.createEglDisplay();
eglContext = GlUtil.createEglContext(eglDisplay);
placeholderEglSurface = GlUtil.focusPlaceholderEglSurface(eglContext, eglDisplay);
Bitmap inputBitmap = readBitmap(ORIGINAL_PNG_ASSET_PATH);
inputWidth = inputBitmap.getWidth();
inputHeight = inputBitmap.getHeight();
placeholderEglSurface = GlUtil.createPlaceholderEglSurface(eglDisplay);
GlUtil.focusEglSurface(eglDisplay, eglContext, placeholderEglSurface, inputWidth, inputHeight);
inputTexId = createGlTextureFromBitmap(inputBitmap);
}

View File

@ -72,11 +72,11 @@ public final class CropPixelTest {
public void createGlObjects() throws IOException, GlUtil.GlException {
eglDisplay = GlUtil.createEglDisplay();
eglContext = GlUtil.createEglContext(eglDisplay);
placeholderEglSurface = GlUtil.focusPlaceholderEglSurface(eglContext, eglDisplay);
Bitmap inputBitmap = readBitmap(ORIGINAL_PNG_ASSET_PATH);
inputWidth = inputBitmap.getWidth();
inputHeight = inputBitmap.getHeight();
placeholderEglSurface = GlUtil.createPlaceholderEglSurface(eglDisplay);
GlUtil.focusEglSurface(eglDisplay, eglContext, placeholderEglSurface, inputWidth, inputHeight);
inputTexId = createGlTextureFromBitmap(inputBitmap);
}

View File

@ -84,11 +84,11 @@ public final class HslAdjustmentPixelTest {
public void createGlObjects() throws IOException, GlUtil.GlException {
eglDisplay = GlUtil.createEglDisplay();
eglContext = GlUtil.createEglContext(eglDisplay);
placeholderEglSurface = GlUtil.focusPlaceholderEglSurface(eglContext, eglDisplay);
Bitmap inputBitmap = readBitmap(ORIGINAL_PNG_ASSET_PATH);
inputWidth = inputBitmap.getWidth();
inputHeight = inputBitmap.getHeight();
placeholderEglSurface = GlUtil.createPlaceholderEglSurface(eglDisplay);
GlUtil.focusEglSurface(eglDisplay, eglContext, placeholderEglSurface, inputWidth, inputHeight);
inputTexId = createGlTextureFromBitmap(inputBitmap);
int outputTexId =

View File

@ -72,11 +72,11 @@ public final class MatrixTextureProcessorPixelTest {
public void createGlObjects() throws IOException, GlUtil.GlException {
eglDisplay = GlUtil.createEglDisplay();
eglContext = GlUtil.createEglContext(eglDisplay);
EGLSurface placeholderEglSurface = GlUtil.focusPlaceholderEglSurface(eglContext, eglDisplay);
Bitmap inputBitmap = readBitmap(ORIGINAL_PNG_ASSET_PATH);
inputWidth = inputBitmap.getWidth();
inputHeight = inputBitmap.getHeight();
EGLSurface placeholderEglSurface = GlUtil.createPlaceholderEglSurface(eglDisplay);
GlUtil.focusEglSurface(eglDisplay, eglContext, placeholderEglSurface, inputWidth, inputHeight);
inputTexId = createGlTextureFromBitmap(inputBitmap);
int outputTexId =
GlUtil.createTexture(inputWidth, inputHeight, /* useHighPrecisionColorComponents= */ false);

View File

@ -81,11 +81,11 @@ public final class PresentationPixelTest {
public void createGlObjects() throws IOException, GlUtil.GlException {
eglDisplay = GlUtil.createEglDisplay();
eglContext = GlUtil.createEglContext(eglDisplay);
placeholderEglSurface = GlUtil.focusPlaceholderEglSurface(eglContext, eglDisplay);
Bitmap inputBitmap = readBitmap(ORIGINAL_PNG_ASSET_PATH);
inputWidth = inputBitmap.getWidth();
inputHeight = inputBitmap.getHeight();
placeholderEglSurface = GlUtil.createPlaceholderEglSurface(eglDisplay);
GlUtil.focusEglSurface(eglDisplay, eglContext, placeholderEglSurface, inputWidth, inputHeight);
inputTexId = createGlTextureFromBitmap(inputBitmap);
}

View File

@ -78,11 +78,11 @@ public final class RgbAdjustmentPixelTest {
public void createGlObjects() throws IOException, GlUtil.GlException {
eglDisplay = GlUtil.createEglDisplay();
eglContext = GlUtil.createEglContext(eglDisplay);
placeholderEglSurface = GlUtil.focusPlaceholderEglSurface(eglContext, eglDisplay);
Bitmap inputBitmap = readBitmap(ORIGINAL_PNG_ASSET_PATH);
inputWidth = inputBitmap.getWidth();
inputHeight = inputBitmap.getHeight();
placeholderEglSurface = GlUtil.createPlaceholderEglSurface(eglDisplay);
GlUtil.focusEglSurface(eglDisplay, eglContext, placeholderEglSurface, inputWidth, inputHeight);
inputTexId = createGlTextureFromBitmap(inputBitmap);
int outputTexId =

View File

@ -73,11 +73,11 @@ public final class RgbFilterPixelTest {
public void createGlObjects() throws IOException, GlUtil.GlException {
eglDisplay = GlUtil.createEglDisplay();
eglContext = GlUtil.createEglContext(eglDisplay);
placeholderEglSurface = GlUtil.focusPlaceholderEglSurface(eglContext, eglDisplay);
Bitmap inputBitmap = readBitmap(ORIGINAL_PNG_ASSET_PATH);
inputWidth = inputBitmap.getWidth();
inputHeight = inputBitmap.getHeight();
placeholderEglSurface = GlUtil.createPlaceholderEglSurface(eglDisplay);
GlUtil.focusEglSurface(eglDisplay, eglContext, placeholderEglSurface, inputWidth, inputHeight);
inputTexId = createGlTextureFromBitmap(inputBitmap);
int outputTexId =

View File

@ -78,13 +78,11 @@ public class SingleColorLutPixelTest {
public void createGlObjects() throws Exception {
eglDisplay = GlUtil.createEglDisplay();
eglContext = GlUtil.createEglContext(eglDisplay);
placeholderEglSurface = GlUtil.focusPlaceholderEglSurface(eglContext, eglDisplay);
Bitmap inputBitmap = readBitmap(ORIGINAL_PNG_ASSET_PATH);
inputWidth = inputBitmap.getWidth();
inputHeight = inputBitmap.getHeight();
placeholderEglSurface = GlUtil.createPlaceholderEglSurface(eglDisplay);
GlUtil.focusEglSurface(eglDisplay, eglContext, placeholderEglSurface, inputWidth, inputHeight);
inputTexId = createGlTextureFromBitmap(inputBitmap);
}

View File

@ -317,11 +317,14 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@Nullable EGLSurface outputEglSurface = this.outputEglSurface;
if (outputEglSurface == null) {
boolean colorInfoIsHdr = ColorInfo.isTransferHdr(colorInfo);
if (colorInfoIsHdr) {
outputEglSurface = GlUtil.getEglSurfaceRgba1010102(eglDisplay, outputSurfaceInfo.surface);
} else {
outputEglSurface = GlUtil.getEglSurface(eglDisplay, outputSurfaceInfo.surface);
}
outputEglSurface =
GlUtil.getEglSurface(
eglDisplay,
outputSurfaceInfo.surface,
colorInfoIsHdr
? GlUtil.EGL_CONFIG_ATTRIBUTES_RGBA_1010102
: GlUtil.EGL_CONFIG_ATTRIBUTES_RGBA_8888);
@Nullable
SurfaceView debugSurfaceView =
@ -443,11 +446,13 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
}
if (eglSurface == null) {
if (useHdr) {
eglSurface = GlUtil.getEglSurfaceRgba1010102(eglDisplay, surface);
} else {
eglSurface = GlUtil.getEglSurface(eglDisplay, surface);
}
eglSurface =
GlUtil.getEglSurface(
eglDisplay,
surface,
useHdr
? GlUtil.EGL_CONFIG_ATTRIBUTES_RGBA_1010102
: GlUtil.EGL_CONFIG_ATTRIBUTES_RGBA_8888);
}
EGLSurface eglSurface = this.eglSurface;
GlUtil.focusEglSurface(eglDisplay, eglContext, eglSurface, width, height);

View File

@ -21,7 +21,6 @@ import static androidx.media3.common.util.Assertions.checkStateNotNull;
import static com.google.common.collect.Iterables.getLast;
import android.content.Context;
import android.opengl.EGL14;
import android.opengl.EGLContext;
import android.opengl.EGLDisplay;
import android.view.Surface;
@ -122,19 +121,10 @@ public final class GlEffectsFrameProcessor implements FrameProcessor {
// configure based on the color info from the decoder output media format instead.
boolean useHdr = ColorInfo.isTransferHdr(colorInfo);
EGLDisplay eglDisplay = GlUtil.createEglDisplay();
EGLContext eglContext =
useHdr
? GlUtil.createEglContextEs3Rgba1010102(eglDisplay)
: GlUtil.createEglContext(eglDisplay);
if (GlUtil.isSurfacelessContextExtensionSupported()) {
GlUtil.focusEglSurface(
eglDisplay, eglContext, EGL14.EGL_NO_SURFACE, /* width= */ 1, /* height= */ 1);
} else if (useHdr) {
GlUtil.focusPlaceholderEglSurfaceRgba1010102(eglContext, eglDisplay);
} else {
GlUtil.focusPlaceholderEglSurface(eglContext, eglDisplay);
}
int[] configAttributes =
useHdr ? GlUtil.EGL_CONFIG_ATTRIBUTES_RGBA_1010102 : GlUtil.EGL_CONFIG_ATTRIBUTES_RGBA_8888;
EGLContext eglContext = GlUtil.createEglContext(eglDisplay, configAttributes);
GlUtil.createFocusedPlaceholderEglSurface(eglContext, eglDisplay, configAttributes);
ImmutableList<GlTextureProcessor> textureProcessors =
getGlTextureProcessorsForGlEffects(