@UnstableApi
public final class GlUtil


OpenGL ES utilities.

Summary

Nested types

public final class GlUtil.GlException extends Exception

Thrown when an OpenGL error occurs.

Constants

static final int[]
static final int[]
static final int

Number of elements in a 3d homogeneous coordinate vector describing a vertex.

static final float
LENGTH_NDC = 2.0f

Length of the normalized device coordinate (NDC) space, which spans from -1 to 1.

Public methods

static void
awaitSyncObject(long syncObject)

Ensures that following commands on the current OpenGL context will not be executed until the sync point has been reached.

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

Collects all OpenGL errors that occurred since this method was last called and throws a with the combined error message.

static void
checkGlException(boolean expression, String errorMessage)

Throws a GlException with the given message if expression evaluates to false.

static void

Fills the pixels in the current output render target buffers with (r=0, g=0, b=0, a=0).

static float[]

Creates a 4x4 identity matrix.

static FloatBuffer
createBuffer(float[] data)

Allocates a FloatBuffer with the given data.

static EGLContext
@RequiresApi(value = 17)
createEglContext(EGLDisplay eglDisplay)

Creates a new EGLContext for the specified EGLDisplay.

static EGLContext
@RequiresApi(value = 17)
createEglContext(
    EGLContext sharedContext,
    EGLDisplay eglDisplay,
    @IntRange(from = 2, to = 3) int openGlVersion,
    int[] configAttributes
)

Creates a new EGLContext for the specified EGLDisplay.

static EGLSurface
@RequiresApi(value = 17)
createEglSurface(
    EGLDisplay eglDisplay,
    Object surface,
    @C.ColorTransfer int colorTransfer,
    boolean isEncoderInputSurface
)

Creates a new EGLSurface wrapping the specified surface.

static int

Creates a GL_TEXTURE_EXTERNAL_OES with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.

static int

Returns a new framebuffer for the texture.

static EGLSurface
@RequiresApi(value = 17)
createFocusedPlaceholderEglSurface(
    EGLContext eglContext,
    EGLDisplay eglDisplay
)

Creates and focuses a placeholder EGLSurface.

static long

Returns a newly created sync object and inserts it into the GL command stream.

static int

Allocates a new texture, initialized with the bitmap data and size.

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
deleteFbo(int fboId)

Deletes a framebuffer, or silently ignores the method call if fboId is unused.

static void
deleteRbo(int rboId)

Deletes a renderbuffer, or silently ignores the method call if rboId is unused.

static void
deleteSyncObject(long syncObject)

Deletes the underlying native object.

static void
deleteSyncObjectQuietly(long syncObject)

Releases the GL sync object if set, suppressing any error.

static void
deleteTexture(int textureId)

Deletes a GL texture.

static void
@RequiresApi(value = 17)
destroyEglContext(
    @Nullable EGLDisplay eglDisplay,
    @Nullable EGLContext eglContext
)

Destroys the EGLContext identified by the provided EGLDisplay and .

static void
@RequiresApi(value = 17)
destroyEglSurface(
    @Nullable EGLDisplay eglDisplay,
    @Nullable EGLSurface eglSurface
)

Destroys the EGLSurface identified by the provided EGLDisplay and .

static void
@RequiresApi(value = 17)
focusEglSurface(
    EGLDisplay eglDisplay,
    EGLContext eglContext,
    EGLSurface eglSurface,
    int width,
    int height
)

Makes the specified eglSurface the render target, using a viewport of width by height pixels.

static void
@RequiresApi(value = 17)
focusFramebuffer(
    EGLDisplay eglDisplay,
    EGLContext eglContext,
    EGLSurface eglSurface,
    int framebuffer,
    int width,
    int height
)

Makes the specified framebuffer the render target, using a viewport of width by height pixels.

static void
focusFramebufferUsingCurrentContext(
    int framebuffer,
    int width,
    int height
)

Makes the specified framebuffer the render target, using a viewport of width by height pixels.

static int

Returns a new, unbound GL texture identifier.

static long

Returns the EGL_CONTEXT_CLIENT_VERSION of the current context.

static EGLContext

Gets the current context.

static EGLDisplay

Returns an initialized default EGLDisplay.

static float[]

Bounds of normalized device coordinates, commonly used for defining viewport boundaries.

static float[]

Typical bounds used for sampling from textures.

static boolean

Returns whether EXTENSION_COLORSPACE_BT2020_PQ is supported.

static boolean

Returns whether creating a GL context with EXTENSION_PROTECTED_CONTENT is possible.

static boolean

Returns whether the EXTENSION_SURFACELESS_CONTEXT extension is supported.

static boolean

Returns whether the EXTENSION_YUV_TARGET extension is supported.

static void
setTexture(int texId, Bitmap bitmap)

Sets the texId to contain the bitmap data and size.

static void
setToIdentity(float[] matrix)

Sets the input matrix to an identity matrix.

Constants

EGL_CONFIG_ATTRIBUTES_RGBA_1010102

public static final int[] EGL_CONFIG_ATTRIBUTES_RGBA_1010102

EGL_CONFIG_ATTRIBUTES_RGBA_8888

public static final int[] EGL_CONFIG_ATTRIBUTES_RGBA_8888

HOMOGENEOUS_COORDINATE_VECTOR_SIZE

public static final int HOMOGENEOUS_COORDINATE_VECTOR_SIZE = 4

Number of elements in a 3d homogeneous coordinate vector describing a vertex.

LENGTH_NDC

public static final float LENGTH_NDC = 2.0f

Length of the normalized device coordinate (NDC) space, which spans from -1 to 1.

Public methods

awaitSyncObject

public static void awaitSyncObject(long syncObject)

Ensures that following commands on the current OpenGL context will not be executed until the sync point has been reached. If syncObject equals 0, this does not block the CPU, and only affects the current OpenGL context. Otherwise, this will block the CPU.

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
int textureTarget

The target to which the texture is bound, e.g. GL_TEXTURE_2D for a two-dimensional texture or GL_TEXTURE_EXTERNAL_OES for an external texture.

int texId

The texture identifier.

checkGlError

public static void checkGlError()

Collects all OpenGL errors that occurred since this method was last called and throws a with the combined error message.

checkGlException

public static void checkGlException(boolean expression, String errorMessage)

Throws a GlException with the given message if expression evaluates to false.

clearFocusedBuffers

public static void clearFocusedBuffers()

Fills the pixels in the current output render target buffers with (r=0, g=0, b=0, a=0).

Buffers can be focused using focusEglSurface and focusFramebufferUsingCurrentContext, focusFramebuffer, and createFocusedPlaceholderEglSurface.

create4x4IdentityMatrix

public static float[] create4x4IdentityMatrix()

Creates a 4x4 identity matrix.

createBuffer

public static FloatBuffer createBuffer(float[] data)

Allocates a FloatBuffer with the given data.

Parameters
float[] data

Used to initialize the new buffer.

createEglContext

@RequiresApi(value = 17)
public static EGLContext createEglContext(EGLDisplay eglDisplay)

Creates a new EGLContext for the specified EGLDisplay.

Configures the EGLContext with EGL_CONFIG_ATTRIBUTES_RGBA_8888 and OpenGL ES 2.0.

Parameters
EGLDisplay eglDisplay

The EGLDisplay to create an EGLContext for.

createEglContext

@RequiresApi(value = 17)
public static EGLContext createEglContext(
    EGLContext sharedContext,
    EGLDisplay eglDisplay,
    @IntRange(from = 2, to = 3) int openGlVersion,
    int[] configAttributes
)

Creates a new EGLContext for the specified EGLDisplay.

Parameters
EGLContext sharedContext

The EGLContext with which to share data.

EGLDisplay eglDisplay

The EGLDisplay to create an EGLContext for.

@IntRange(from = 2, to = 3) int openGlVersion

The version of OpenGL ES to configure. Accepts either 2, for OpenGL ES 2.0, or 3, for OpenGL ES 3.0.

int[] configAttributes

The attributes to configure EGL with. Accepts either EGL_CONFIG_ATTRIBUTES_RGBA_1010102, or EGL_CONFIG_ATTRIBUTES_RGBA_8888.

createEglSurface

@RequiresApi(value = 17)
public static EGLSurface createEglSurface(
    EGLDisplay eglDisplay,
    Object surface,
    @C.ColorTransfer int colorTransfer,
    boolean isEncoderInputSurface
)

Creates a new EGLSurface wrapping the specified surface.

The EGLSurface will configure with OpenGL ES 2.0.

Parameters
EGLDisplay eglDisplay

The EGLDisplay to attach the surface to.

Object surface

The surface to wrap; must be a surface, surface texture or surface holder.

@C.ColorTransfer int colorTransfer

The color transfer characteristics to which the surface is configured. The only accepted values are COLOR_TRANSFER_SDR, COLOR_TRANSFER_HLG and COLOR_TRANSFER_ST2084.

boolean isEncoderInputSurface

Whether the surface is the input surface of an encoder.

createExternalTexture

public static int createExternalTexture()

Creates a GL_TEXTURE_EXTERNAL_OES with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.

createFboForTexture

public static int createFboForTexture(int texId)

Returns a new framebuffer for the texture.

Parameters
int texId

The identifier of the texture to attach to the framebuffer.

createFocusedPlaceholderEglSurface

@RequiresApi(value = 17)
public static EGLSurface createFocusedPlaceholderEglSurface(
    EGLContext eglContext,
    EGLDisplay eglDisplay
)

Creates and focuses a placeholder EGLSurface.

This makes a EGLContext current when reading and writing to a surface is not required, configured with EGL_CONFIG_ATTRIBUTES_RGBA_8888.

Parameters
EGLContext eglContext

The EGLContext to make current.

EGLDisplay eglDisplay

The EGLDisplay to attach the surface to.

Returns
EGLSurface

EGL_NO_SURFACE if supported and a 1x1 pixel buffer surface otherwise.

createGlSyncFence

@RequiresApi(value = 17)
public static long createGlSyncFence()

Returns a newly created sync object and inserts it into the GL command stream.

Returns 0 if the operation failed, no EGLContext is focused, or the focused EGLContext version is less than 3.0.

createTexture

public static int createTexture(Bitmap bitmap)

Allocates a new texture, initialized with the bitmap data and size.

Parameters
Bitmap bitmap

The Bitmap for which the texture is created.

Returns
int

The texture identifier for the newly-allocated texture.

Throws
androidx.media3.common.util.GlUtil.GlException

If the texture allocation fails.

createTexture

public static int createTexture(
    int width,
    int height,
    boolean useHighPrecisionColorComponents
)

Allocates a new RGBA texture with the specified dimensions and color component precision.

The created texture is not zero-initialized. To clear the texture, focus on the texture and clear its content.

Parameters
int width

The width of the new texture in pixels.

int height

The height of the new texture in pixels.

boolean useHighPrecisionColorComponents

If false, uses colors with 8-bit unsigned bytes. If true, use 16-bit (half-precision) floating-point.

Returns
int

The texture identifier for the newly-allocated texture.

Throws
androidx.media3.common.util.GlUtil.GlException

If the texture allocation fails.

createVertexBuffer

public static float[] createVertexBuffer(List<float[]> vertexList)

Flattens the list of 4 element NDC coordinate vectors into a buffer.

deleteFbo

public static void deleteFbo(int fboId)

Deletes a framebuffer, or silently ignores the method call if fboId is unused.

deleteRbo

public static void deleteRbo(int rboId)

Deletes a renderbuffer, or silently ignores the method call if rboId is unused.

deleteSyncObject

public static void deleteSyncObject(long syncObject)

Deletes the underlying native object.

The syncObject must not be used after deletion.

deleteSyncObjectQuietly

public static void deleteSyncObjectQuietly(long syncObject)

Releases the GL sync object if set, suppressing any error.

deleteTexture

public static void deleteTexture(int textureId)

Deletes a GL texture.

Parameters
int textureId

The ID of the texture to delete.

destroyEglContext

@RequiresApi(value = 17)
public static void destroyEglContext(
    @Nullable EGLDisplay eglDisplay,
    @Nullable EGLContext eglContext
)

Destroys the EGLContext identified by the provided EGLDisplay and .

This is a no-op if called on already-destroyed EGLDisplay and EGLContext instances.

destroyEglSurface

@RequiresApi(value = 17)
public static void destroyEglSurface(
    @Nullable EGLDisplay eglDisplay,
    @Nullable EGLSurface eglSurface
)

Destroys the EGLSurface identified by the provided EGLDisplay and .

focusEglSurface

@RequiresApi(value = 17)
public static void focusEglSurface(
    EGLDisplay eglDisplay,
    EGLContext eglContext,
    EGLSurface eglSurface,
    int width,
    int height
)

Makes the specified eglSurface the render target, using a viewport of width by height pixels.

focusFramebuffer

@RequiresApi(value = 17)
public static void focusFramebuffer(
    EGLDisplay eglDisplay,
    EGLContext eglContext,
    EGLSurface eglSurface,
    int framebuffer,
    int width,
    int height
)

Makes the specified framebuffer the render target, using a viewport of width by height pixels.

focusFramebufferUsingCurrentContext

public static void focusFramebufferUsingCurrentContext(
    int framebuffer,
    int width,
    int height
)

Makes the specified framebuffer the render target, using a viewport of width by height pixels.

The caller must ensure that there is a current OpenGL context before calling this method.

Parameters
int framebuffer

The identifier of the framebuffer object to bind as the output render target.

int width

The viewport width, in pixels.

int height

The viewport height, in pixels.

generateTexture

public static int generateTexture()

Returns a new, unbound GL texture identifier.

getContextMajorVersion

@RequiresApi(value = 17)
public static long getContextMajorVersion()

Returns the EGL_CONTEXT_CLIENT_VERSION of the current context.

Returns 0 if no EGLContextis focused.

getCurrentContext

@RequiresApi(value = 17)
public static EGLContext getCurrentContext()

Gets the current context.

getDefaultEglDisplay

@RequiresApi(value = 17)
public static EGLDisplay getDefaultEglDisplay()

Returns an initialized default EGLDisplay.

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.

isBt2020PqExtensionSupported

public static boolean isBt2020PqExtensionSupported()

Returns whether EXTENSION_COLORSPACE_BT2020_PQ is supported.

isProtectedContentExtensionSupported

public static boolean isProtectedContentExtensionSupported(Context context)

Returns whether creating a GL context with EXTENSION_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 EXTENSION_SURFACELESS_CONTEXT extension is supported.

This extension allows passing EGL_NO_SURFACE for both the write and read surfaces in a call to eglMakeCurrent.

isYuvTargetExtensionSupported

public static boolean isYuvTargetExtensionSupported()

Returns whether the EXTENSION_YUV_TARGET extension is supported.

This extension allows sampling raw YUV values from an external texture, which is required for HDR input.

setTexture

public static void setTexture(int texId, Bitmap bitmap)

Sets the texId to contain the bitmap data and size.

setToIdentity

public static void setToIdentity(float[] matrix)

Sets the input matrix to an identity matrix.