Class GlUtil
- java.lang.Object
-
- com.google.android.exoplayer2.util.GlUtil
-
public final class GlUtil extends Object
OpenGL ES utilities.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
GlUtil.GlException
Thrown when an OpenGL error occurs.
-
Field Summary
Fields Modifier and Type Field Description static int[]
EGL_CONFIG_ATTRIBUTES_RGBA_1010102
static int[]
EGL_CONFIG_ATTRIBUTES_RGBA_8888
static int
HOMOGENEOUS_COORDINATE_VECTOR_SIZE
Number of elements in a 3d homogeneous coordinate vector describing a vertex.static float
LENGTH_NDC
Length of the normalized device coordinate (NDC) space, which spans from -1 to 1.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
bindTexture(int textureTarget, int texId)
Binds the texture of the given type with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.static void
checkGlError()
Collects all OpenGL errors that occurred since this method was last called and throws aGlUtil.GlException
with the combined error message.static void
checkGlException(boolean expression, String errorMessage)
static void
clearOutputFrame()
Fills the pixels in the current output render target with (r=0, g=0, b=0, a=0).static float[]
create4x4IdentityMatrix()
Creates a 4x4 identity matrix.static FloatBuffer
createBuffer(float[] data)
Allocates a FloatBuffer with the given data.static EGLContext
createEglContext(EGLDisplay eglDisplay)
Creates a newEGLContext
for the specifiedEGLDisplay
.static EGLContext
createEglContext(EGLDisplay eglDisplay, int[] configAttributes)
Creates a newEGLContext
for the specifiedEGLDisplay
.static EGLDisplay
createEglDisplay()
Returns an initialized defaultEGLDisplay
.static int
createExternalTexture()
Creates a GL_TEXTURE_EXTERNAL_OES with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.static int
createFboForTexture(int texId)
Returns a new framebuffer for the texture.static EGLSurface
createFocusedPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay, int[] configAttributes)
Creates and focuses a placeholderEGLSurface
.static int
createTexture(int width, int height, boolean useHighPrecisionColorComponents)
Allocates a new RGBA texture with the specified dimensions and color component precision.static float[]
createVertexBuffer(List<float[]> vertexList)
Flattens the list of 4 element NDC coordinate vectors into a buffer.static void
deleteTexture(int textureId)
Deletes a GL texture.static void
destroyEglContext(EGLDisplay eglDisplay, EGLContext eglContext)
static void
focusEglSurface(EGLDisplay eglDisplay, EGLContext eglContext, EGLSurface eglSurface, int width, int height)
Makes the specifiedeglSurface
the render target, using a viewport ofwidth
byheight
pixels.static void
focusFramebuffer(EGLDisplay eglDisplay, EGLContext eglContext, EGLSurface eglSurface, int framebuffer, int width, int height)
Makes the specifiedframebuffer
the render target, using a viewport ofwidth
byheight
pixels.static void
focusFramebufferUsingCurrentContext(int framebuffer, int width, int height)
Makes the specifiedframebuffer
the render target, using a viewport ofwidth
byheight
pixels.static EGLSurface
focusPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay)
Creates and focuses a placeholderEGLSurface
.static EGLSurface
getEglSurface(EGLDisplay eglDisplay, Object surface)
Returns a newEGLSurface
wrapping the specifiedsurface
.static EGLSurface
getEglSurface(EGLDisplay eglDisplay, Object surface, int[] configAttributes)
Returns a newEGLSurface
wrapping the specifiedsurface
.static float[]
getNormalizedCoordinateBounds()
Bounds of normalized device coordinates, commonly used for defining viewport boundaries.static float[]
getTextureCoordinateBounds()
Typical bounds used for sampling from textures.static boolean
isProtectedContentExtensionSupported(Context context)
Returns whether creating a GL context with "EGL_EXT_protected_content" is possible.static boolean
isSurfacelessContextExtensionSupported()
Returns whether the "EGL_KHR_surfaceless_context" extension is supported.static boolean
isYuvTargetExtensionSupported()
Returns whether the "GL_EXT_YUV_target" extension is supported.static void
setToIdentity(float[] matrix)
Sets the inputmatrix
to an identity matrix.
-
-
-
Field Detail
-
HOMOGENEOUS_COORDINATE_VECTOR_SIZE
public static final int HOMOGENEOUS_COORDINATE_VECTOR_SIZE
Number of elements in a 3d homogeneous coordinate vector describing a vertex.- See Also:
- Constant Field Values
-
LENGTH_NDC
public static final float LENGTH_NDC
Length of the normalized device coordinate (NDC) space, which spans from -1 to 1.- See Also:
- Constant Field Values
-
EGL_CONFIG_ATTRIBUTES_RGBA_8888
public static final int[] EGL_CONFIG_ATTRIBUTES_RGBA_8888
-
EGL_CONFIG_ATTRIBUTES_RGBA_1010102
public static final int[] EGL_CONFIG_ATTRIBUTES_RGBA_1010102
-
-
Method Detail
-
getNormalizedCoordinateBounds
public static float[] getNormalizedCoordinateBounds()
Bounds of normalized device coordinates, commonly used for defining viewport boundaries.
-
getTextureCoordinateBounds
public static float[] getTextureCoordinateBounds()
Typical bounds used for sampling from textures.
-
create4x4IdentityMatrix
public static float[] create4x4IdentityMatrix()
Creates a 4x4 identity matrix.
-
setToIdentity
public static void setToIdentity(float[] matrix)
Sets the inputmatrix
to an identity matrix.
-
createVertexBuffer
public static float[] createVertexBuffer(List<float[]> vertexList)
Flattens the list of 4 element NDC coordinate vectors into a buffer.
-
isProtectedContentExtensionSupported
public static boolean isProtectedContentExtensionSupported(Context context)
Returns whether creating a GL context with "EGL_EXT_protected_content" is possible.If
true
, the device supports a protected output path for DRM content when using GL.
-
isSurfacelessContextExtensionSupported
public static boolean isSurfacelessContextExtensionSupported()
Returns whether the "EGL_KHR_surfaceless_context" extension is supported.This extension allows passing
EGL14.EGL_NO_SURFACE
for both the write and read surfaces in a call toEGL14.eglMakeCurrent(EGLDisplay, EGLSurface, EGLSurface, EGLContext)
.
-
isYuvTargetExtensionSupported
public static boolean isYuvTargetExtensionSupported()
Returns whether the "GL_EXT_YUV_target" extension is supported.This extension allows sampling raw YUV values from an external texture, which is required for HDR.
-
createEglDisplay
@RequiresApi(17) public static EGLDisplay createEglDisplay() throws GlUtil.GlException
Returns an initialized defaultEGLDisplay
.- Throws:
GlUtil.GlException
-
createEglContext
@RequiresApi(17) public static EGLContext createEglContext(EGLDisplay eglDisplay) throws GlUtil.GlException
Creates a newEGLContext
for the specifiedEGLDisplay
.Configures the
EGLContext
withEGL_CONFIG_ATTRIBUTES_RGBA_8888
and OpenGL ES 2.0.- Parameters:
eglDisplay
- TheEGLDisplay
to create anEGLContext
for.- Throws:
GlUtil.GlException
-
createEglContext
@RequiresApi(17) public static EGLContext createEglContext(EGLDisplay eglDisplay, int[] configAttributes) throws GlUtil.GlException
Creates a newEGLContext
for the specifiedEGLDisplay
.- Parameters:
eglDisplay
- TheEGLDisplay
to create anEGLContext
for.configAttributes
- The attributes to configure EGL with. Accepts eitherEGL_CONFIG_ATTRIBUTES_RGBA_1010102
, which will request OpenGL ES 3.0, orEGL_CONFIG_ATTRIBUTES_RGBA_8888
, which will request OpenGL ES 2.0.- Throws:
GlUtil.GlException
-
getEglSurface
@RequiresApi(17) public static EGLSurface getEglSurface(EGLDisplay eglDisplay, Object surface) throws GlUtil.GlException
Returns a newEGLSurface
wrapping the specifiedsurface
.The
EGLSurface
will configure withEGL_CONFIG_ATTRIBUTES_RGBA_8888
and OpenGL ES 2.0.- Parameters:
eglDisplay
- TheEGLDisplay
to attach the surface to.surface
- The surface to wrap; must be a surface, surface texture or surface holder.- Throws:
GlUtil.GlException
-
getEglSurface
@RequiresApi(17) public static EGLSurface getEglSurface(EGLDisplay eglDisplay, Object surface, int[] configAttributes) throws GlUtil.GlException
Returns a newEGLSurface
wrapping the specifiedsurface
.- Parameters:
eglDisplay
- TheEGLDisplay
to attach the surface to.surface
- The surface to wrap; must be a surface, surface texture or surface holder.configAttributes
- The attributes to configure EGL with. AcceptsEGL_CONFIG_ATTRIBUTES_RGBA_1010102
andEGL_CONFIG_ATTRIBUTES_RGBA_8888
.- Throws:
GlUtil.GlException
-
focusPlaceholderEglSurface
@RequiresApi(17) public static EGLSurface focusPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay) throws GlUtil.GlException
Creates and focuses a placeholderEGLSurface
.This makes a
EGLContext
current when reading and writing to a surface is not required, configured withEGL_CONFIG_ATTRIBUTES_RGBA_8888
.- Parameters:
eglContext
- TheEGLContext
to make current.eglDisplay
- TheEGLDisplay
to attach the surface to.- Returns:
EGL14.EGL_NO_SURFACE
if supported and a 1x1 pixel buffer surface otherwise.- Throws:
GlUtil.GlException
-
createFocusedPlaceholderEglSurface
@RequiresApi(17) public static EGLSurface createFocusedPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay, int[] configAttributes) throws GlUtil.GlException
Creates and focuses a placeholderEGLSurface
.This makes a
EGLContext
current when reading and writing to a surface is not required.- Parameters:
eglContext
- TheEGLContext
to make current.eglDisplay
- TheEGLDisplay
to attach the surface to.configAttributes
- The attributes to configure EGL with. AcceptsEGL_CONFIG_ATTRIBUTES_RGBA_1010102
andEGL_CONFIG_ATTRIBUTES_RGBA_8888
.- Returns:
- A placeholder
EGLSurface
that has been focused to allow rendering to take place, orEGL14.EGL_NO_SURFACE
if the current context supports rendering without a surface. - Throws:
GlUtil.GlException
-
checkGlError
public static void checkGlError() throws GlUtil.GlException
Collects all OpenGL errors that occurred since this method was last called and throws aGlUtil.GlException
with the combined error message.- Throws:
GlUtil.GlException
-
clearOutputFrame
public static void clearOutputFrame() throws GlUtil.GlException
Fills the pixels in the current output render target with (r=0, g=0, b=0, a=0).- Throws:
GlUtil.GlException
-
focusEglSurface
@RequiresApi(17) public static void focusEglSurface(EGLDisplay eglDisplay, EGLContext eglContext, EGLSurface eglSurface, int width, int height) throws GlUtil.GlException
Makes the specifiedeglSurface
the render target, using a viewport ofwidth
byheight
pixels.- Throws:
GlUtil.GlException
-
focusFramebuffer
@RequiresApi(17) public static void focusFramebuffer(EGLDisplay eglDisplay, EGLContext eglContext, EGLSurface eglSurface, int framebuffer, int width, int height) throws GlUtil.GlException
Makes the specifiedframebuffer
the render target, using a viewport ofwidth
byheight
pixels.- Throws:
GlUtil.GlException
-
focusFramebufferUsingCurrentContext
@RequiresApi(17) public static void focusFramebufferUsingCurrentContext(int framebuffer, int width, int height) throws GlUtil.GlException
Makes the specifiedframebuffer
the render target, using a viewport ofwidth
byheight
pixels.The caller must ensure that there is a current OpenGL context before calling this method.
- Parameters:
framebuffer
- The identifier of the framebuffer object to bind as the output render target.width
- The viewport width, in pixels.height
- The viewport height, in pixels.- Throws:
GlUtil.GlException
-
deleteTexture
public static void deleteTexture(int textureId) throws GlUtil.GlException
Deletes a GL texture.- Parameters:
textureId
- The ID of the texture to delete.- Throws:
GlUtil.GlException
-
destroyEglContext
@RequiresApi(17) public static void destroyEglContext(@Nullable EGLDisplay eglDisplay, @Nullable EGLContext eglContext) throws GlUtil.GlException
- Throws:
GlUtil.GlException
-
createBuffer
public static FloatBuffer createBuffer(float[] data)
Allocates a FloatBuffer with the given data.- Parameters:
data
- Used to initialize the new buffer.
-
createExternalTexture
public static int createExternalTexture() throws GlUtil.GlException
Creates a GL_TEXTURE_EXTERNAL_OES with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.- Throws:
GlUtil.GlException
-
createTexture
public static int createTexture(int width, int height, boolean useHighPrecisionColorComponents) throws GlUtil.GlException
Allocates a new RGBA texture with the specified dimensions and color component precision.- Parameters:
width
- The width of the new texture in pixels.height
- The height of the new texture in pixels.useHighPrecisionColorComponents
- Iffalse
, uses 8-bit unsigned bytes. Iftrue
, use 16-bit (half-precision) floating-point.- Returns:
- The texture identifier for the newly-allocated texture.
- Throws:
GlUtil.GlException
- If the texture allocation fails.
-
bindTexture
public static void bindTexture(int textureTarget, int texId) throws GlUtil.GlException
Binds the texture of the given type with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.- Parameters:
textureTarget
- The target to which the texture is bound, e.g.GLES20.GL_TEXTURE_2D
for a two-dimensional texture orGLES11Ext.GL_TEXTURE_EXTERNAL_OES
for an external texture.texId
- The texture identifier.- Throws:
GlUtil.GlException
-
createFboForTexture
public static int createFboForTexture(int texId) throws GlUtil.GlException
Returns a new framebuffer for the texture.- Parameters:
texId
- The identifier of the texture to attach to the framebuffer.- Throws:
GlUtil.GlException
-
checkGlException
public static void checkGlException(boolean expression, String errorMessage) throws GlUtil.GlException
- Throws:
GlUtil.GlException
-
-