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 classGlUtil.GlExceptionThrown when an OpenGL error occurs.
-
Field Summary
Fields Modifier and Type Field Description static int[]EGL_CONFIG_ATTRIBUTES_RGBA_1010102static int[]EGL_CONFIG_ATTRIBUTES_RGBA_8888static intHOMOGENEOUS_COORDINATE_VECTOR_SIZENumber of elements in a 3d homogeneous coordinate vector describing a vertex.static floatLENGTH_NDCLength 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 voidbindTexture(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 voidcheckGlError()Collects all OpenGL errors that occurred since this method was last called and throws aGlUtil.GlExceptionwith the combined error message.static voidcheckGlException(boolean expression, String errorMessage)static voidclearOutputFrame()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 FloatBuffercreateBuffer(float[] data)Allocates a FloatBuffer with the given data.static EGLContextcreateEglContext(EGLDisplay eglDisplay)Creates a newEGLContextfor the specifiedEGLDisplay.static EGLContextcreateEglContext(EGLDisplay eglDisplay, int[] configAttributes)Creates a newEGLContextfor the specifiedEGLDisplay.static EGLDisplaycreateEglDisplay()Returns an initialized defaultEGLDisplay.static intcreateExternalTexture()Creates a GL_TEXTURE_EXTERNAL_OES with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.static intcreateFboForTexture(int texId)Returns a new framebuffer for the texture.static EGLSurfacecreateFocusedPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay, int[] configAttributes)Creates and focuses a placeholderEGLSurface.static intcreateTexture(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 voiddeleteTexture(int textureId)Deletes a GL texture.static voiddestroyEglContext(EGLDisplay eglDisplay, EGLContext eglContext)static voidfocusEglSurface(EGLDisplay eglDisplay, EGLContext eglContext, EGLSurface eglSurface, int width, int height)Makes the specifiedeglSurfacethe render target, using a viewport ofwidthbyheightpixels.static voidfocusFramebuffer(EGLDisplay eglDisplay, EGLContext eglContext, EGLSurface eglSurface, int framebuffer, int width, int height)Makes the specifiedframebufferthe render target, using a viewport ofwidthbyheightpixels.static voidfocusFramebufferUsingCurrentContext(int framebuffer, int width, int height)Makes the specifiedframebufferthe render target, using a viewport ofwidthbyheightpixels.static EGLSurfacefocusPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay)Creates and focuses a placeholderEGLSurface.static EGLSurfacegetEglSurface(EGLDisplay eglDisplay, Object surface)Returns a newEGLSurfacewrapping the specifiedsurface.static EGLSurfacegetEglSurface(EGLDisplay eglDisplay, Object surface, int[] configAttributes)Returns a newEGLSurfacewrapping 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 booleanisProtectedContentExtensionSupported(Context context)Returns whether creating a GL context with "EGL_EXT_protected_content" is possible.static booleanisSurfacelessContextExtensionSupported()Returns whether the "EGL_KHR_surfaceless_context" extension is supported.static booleanisYuvTargetExtensionSupported()Returns whether the "GL_EXT_YUV_target" extension is supported.static voidsetToIdentity(float[] matrix)Sets the inputmatrixto 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 inputmatrixto 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_SURFACEfor 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 newEGLContextfor the specifiedEGLDisplay.Configures the
EGLContextwithEGL_CONFIG_ATTRIBUTES_RGBA_8888and OpenGL ES 2.0.- Parameters:
eglDisplay- TheEGLDisplayto create anEGLContextfor.- Throws:
GlUtil.GlException
-
createEglContext
@RequiresApi(17) public static EGLContext createEglContext(EGLDisplay eglDisplay, int[] configAttributes) throws GlUtil.GlException
Creates a newEGLContextfor the specifiedEGLDisplay.- Parameters:
eglDisplay- TheEGLDisplayto create anEGLContextfor.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 newEGLSurfacewrapping the specifiedsurface.The
EGLSurfacewill configure withEGL_CONFIG_ATTRIBUTES_RGBA_8888and OpenGL ES 2.0.- Parameters:
eglDisplay- TheEGLDisplayto 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 newEGLSurfacewrapping the specifiedsurface.- Parameters:
eglDisplay- TheEGLDisplayto 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_1010102andEGL_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
EGLContextcurrent when reading and writing to a surface is not required, configured withEGL_CONFIG_ATTRIBUTES_RGBA_8888.- Parameters:
eglContext- TheEGLContextto make current.eglDisplay- TheEGLDisplayto attach the surface to.- Returns:
EGL14.EGL_NO_SURFACEif 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
EGLContextcurrent when reading and writing to a surface is not required.- Parameters:
eglContext- TheEGLContextto make current.eglDisplay- TheEGLDisplayto attach the surface to.configAttributes- The attributes to configure EGL with. AcceptsEGL_CONFIG_ATTRIBUTES_RGBA_1010102andEGL_CONFIG_ATTRIBUTES_RGBA_8888.- Returns:
- A placeholder
EGLSurfacethat has been focused to allow rendering to take place, orEGL14.EGL_NO_SURFACEif the current context supports rendering without a surface. - Throws:
GlUtil.GlException
-
checkGlError
public static void checkGlError() throws GlUtil.GlExceptionCollects all OpenGL errors that occurred since this method was last called and throws aGlUtil.GlExceptionwith the combined error message.- Throws:
GlUtil.GlException
-
clearOutputFrame
public static void clearOutputFrame() throws GlUtil.GlExceptionFills 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 specifiedeglSurfacethe render target, using a viewport ofwidthbyheightpixels.- 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 specifiedframebufferthe render target, using a viewport ofwidthbyheightpixels.- Throws:
GlUtil.GlException
-
focusFramebufferUsingCurrentContext
@RequiresApi(17) public static void focusFramebufferUsingCurrentContext(int framebuffer, int width, int height) throws GlUtil.GlExceptionMakes the specifiedframebufferthe render target, using a viewport ofwidthbyheightpixels.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.GlExceptionDeletes 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.GlExceptionCreates 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.GlExceptionAllocates 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.GlExceptionBinds 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_2Dfor a two-dimensional texture orGLES11Ext.GL_TEXTURE_EXTERNAL_OESfor an external texture.texId- The texture identifier.- Throws:
GlUtil.GlException
-
createFboForTexture
public static int createFboForTexture(int texId) throws GlUtil.GlExceptionReturns 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
-
-