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 andglAssertionsEnabled
istrue
.
-
Field Summary
Fields Modifier and Type Field Description static boolean
glAssertionsEnabled
Whether to throw aGlUtil.GlException
in case of an OpenGL error.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
assertValidTextureSize(int width, int height)
Asserts the texture size is valid.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()
If there is an OpenGl error, logs the error and ifglAssertionsEnabled
is true throws aGlUtil.GlException
.static FloatBuffer
createBuffer(float[] data)
Allocates a FloatBuffer with the given data.static FloatBuffer
createBuffer(int capacity)
Allocates a FloatBuffer.static EGLContext
createEglContext(EGLDisplay eglDisplay)
Returns a newEGLContext
for the specifiedEGLDisplay
.static EGLContext
createEglContextEs3Rgba1010102(EGLDisplay eglDisplay)
Returns a newEGLContext
for the specifiedEGLDisplay
, requesting ES 3 and an RGBA 1010102 config.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
createPlaceholderEglSurface(EGLDisplay eglDisplay)
Returns a placeholderEGLSurface
to use when reading and writing to the surface is not required.static int
createTexture(int width, int height)
Returns the texture identifier for a newly-allocated texture with the specified dimensions.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
focusPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay)
Creates and focuses a newEGLSurface
wrapping a 1x1 pixel buffer.static void
focusPlaceholderEglSurfaceBt2020Pq(EGLContext eglContext, EGLDisplay eglDisplay)
Creates and focuses a newEGLSurface
wrapping a 1x1 pixel buffer, for HDR rendering with Rec.static EGLSurface
getEglSurface(EGLDisplay eglDisplay, Object surface)
Returns a newEGLSurface
wrapping the specifiedsurface
.static EGLSurface
getEglSurfaceBt2020Pq(EGLDisplay eglDisplay, Object surface)
Returns a newEGLSurface
wrapping the specifiedsurface
, for HDR rendering with Rec.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 String
loadAsset(Context context, String assetPath)
Loads a file from the assets folder.
-
-
-
Field Detail
-
glAssertionsEnabled
public static boolean glAssertionsEnabled
Whether to throw aGlUtil.GlException
in case of an OpenGL error.
-
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
-
-
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.
-
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)
.
-
createEglDisplay
@RequiresApi(17) public static EGLDisplay createEglDisplay()
Returns an initialized defaultEGLDisplay
.
-
createEglContext
@RequiresApi(17) public static EGLContext createEglContext(EGLDisplay eglDisplay)
Returns a newEGLContext
for the specifiedEGLDisplay
.
-
createEglContextEs3Rgba1010102
@RequiresApi(17) public static EGLContext createEglContextEs3Rgba1010102(EGLDisplay eglDisplay)
Returns a newEGLContext
for the specifiedEGLDisplay
, requesting ES 3 and an RGBA 1010102 config.
-
getEglSurface
@RequiresApi(17) public static EGLSurface getEglSurface(EGLDisplay eglDisplay, Object surface)
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.
-
getEglSurfaceBt2020Pq
@RequiresApi(17) public static EGLSurface getEglSurfaceBt2020Pq(EGLDisplay eglDisplay, Object surface)
Returns a newEGLSurface
wrapping the specifiedsurface
, for HDR rendering with Rec. 2020 color primaries and using the PQ transfer function.- Parameters:
eglDisplay
- TheEGLDisplay
to attach the surface to.surface
- The surface to wrap; must be a surface, surface texture or surface holder.
-
createPlaceholderEglSurface
@RequiresApi(17) public static EGLSurface createPlaceholderEglSurface(EGLDisplay eglDisplay)
Returns a placeholderEGLSurface
to use when reading and writing to the surface is not required.- Parameters:
eglDisplay
- TheEGLDisplay
to attach the surface to.- Returns:
EGL14.EGL_NO_SURFACE
if supported and a 1x1 pixel buffer surface otherwise.
-
focusPlaceholderEglSurface
@RequiresApi(17) public static void focusPlaceholderEglSurface(EGLContext eglContext, EGLDisplay eglDisplay)
Creates and focuses a newEGLSurface
wrapping a 1x1 pixel buffer.- Parameters:
eglContext
- TheEGLContext
to make current.eglDisplay
- TheEGLDisplay
to attach the surface to.
-
focusPlaceholderEglSurfaceBt2020Pq
@RequiresApi(17) public static void focusPlaceholderEglSurfaceBt2020Pq(EGLContext eglContext, EGLDisplay eglDisplay)
Creates and focuses a newEGLSurface
wrapping a 1x1 pixel buffer, for HDR rendering with Rec. 2020 color primaries and using the PQ transfer function.- Parameters:
eglContext
- TheEGLContext
to make current.eglDisplay
- TheEGLDisplay
to attach the surface to.
-
checkGlError
public static void checkGlError()
If there is an OpenGl error, logs the error and ifglAssertionsEnabled
is true throws aGlUtil.GlException
.
-
assertValidTextureSize
public static void assertValidTextureSize(int width, int height)
Asserts the texture size is valid.- Parameters:
width
- The width for a texture.height
- The height for a texture.- Throws:
GlUtil.GlException
- If the texture width or height is invalid.
-
focusEglSurface
@RequiresApi(17) public 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.
-
focusFramebuffer
@RequiresApi(17) public 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.
-
deleteTexture
public static void deleteTexture(int textureId)
Deletes a GL texture.- Parameters:
textureId
- The ID of the texture to delete.
-
destroyEglContext
@RequiresApi(17) public static void destroyEglContext(@Nullable EGLDisplay eglDisplay, @Nullable EGLContext eglContext)
-
createBuffer
public static FloatBuffer createBuffer(float[] data)
Allocates a FloatBuffer with the given data.- Parameters:
data
- Used to initialize the new buffer.
-
createBuffer
public static FloatBuffer createBuffer(int capacity)
Allocates a FloatBuffer.- Parameters:
capacity
- The new buffer's capacity, in floats.
-
loadAsset
public static String loadAsset(Context context, String assetPath) throws IOException
Loads a file from the assets folder.- Parameters:
context
- TheContext
.assetPath
- The path to the file to load, from the assets folder.- Returns:
- The content of the file to load.
- Throws:
IOException
- If the file couldn't be read.
-
createExternalTexture
public static int createExternalTexture()
Creates a GL_TEXTURE_EXTERNAL_OES with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.
-
createTexture
public static int createTexture(int width, int height)
Returns the texture identifier for a newly-allocated texture with the specified dimensions.- Parameters:
width
- of the new texture in pixelsheight
- of the new texture in pixels
-
bindTexture
public 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.- Parameters:
texId
- The texture identifier.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.
-
createFboForTexture
public static int createFboForTexture(int texId)
Returns a new framebuffer for the texture.- Parameters:
texId
- The identifier of the texture to attach to the framebuffer.
-
-