@UnstableApi
class GlUtil


OpenGL ES utilities.

Summary

Nested types

Thrown when an OpenGL error occurs.

Constants

const IntArray<Int>!
const IntArray<Int>!
const Int

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

const Float
LENGTH_NDC = 2.0f

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

Public functions

java-static Unit
awaitSyncObject(syncObject: Long)

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

java-static Unit
bindTexture(textureTarget: Int, texId: Int)

Binds the texture of the given type with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.

java-static Unit

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

java-static Unit
checkGlException(expression: Boolean, errorMessage: String!)

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

java-static Unit

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

java-static FloatArray<Float>!

Creates a 4x4 identity matrix.

java-static FloatBuffer!

Allocates a FloatBuffer with the given data.

java-static EGLContext!
@RequiresApi(value = 17)
createEglContext(eglDisplay: EGLDisplay!)

Creates a new EGLContext for the specified EGLDisplay.

java-static EGLContext!
@RequiresApi(value = 17)
createEglContext(
    sharedContext: EGLContext!,
    eglDisplay: EGLDisplay!,
    openGlVersion: @IntRange(from = 2, to = 3) Int,
    configAttributes: IntArray!
)

Creates a new EGLContext for the specified EGLDisplay.

java-static EGLSurface!
@RequiresApi(value = 17)
createEglSurface(
    eglDisplay: EGLDisplay!,
    surface: Any!,
    @C.ColorTransfer colorTransfer: Int,
    isEncoderInputSurface: Boolean
)

Creates a new EGLSurface wrapping the specified surface.

java-static Int

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

java-static Int

Returns a new framebuffer for the texture.

java-static EGLSurface!
@RequiresApi(value = 17)
createFocusedPlaceholderEglSurface(
    eglContext: EGLContext!,
    eglDisplay: EGLDisplay!
)

Creates and focuses a placeholder EGLSurface.

java-static Long

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

java-static Int

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

java-static Int
createTexture(
    width: Int,
    height: Int,
    useHighPrecisionColorComponents: Boolean
)

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

java-static FloatArray<Float>!

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

java-static Unit
deleteFbo(fboId: Int)

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

java-static Unit
deleteRbo(rboId: Int)

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

java-static Unit
deleteSyncObject(syncObject: Long)

Deletes the underlying native object.

java-static Unit

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

java-static Unit
deleteTexture(textureId: Int)

Deletes a GL texture.

java-static Unit
@RequiresApi(value = 17)
destroyEglContext(eglDisplay: EGLDisplay?, eglContext: EGLContext?)

Destroys the EGLContext identified by the provided EGLDisplay and .

java-static Unit
@RequiresApi(value = 17)
destroyEglSurface(eglDisplay: EGLDisplay?, eglSurface: EGLSurface?)

Destroys the EGLSurface identified by the provided EGLDisplay and .

java-static Unit
@RequiresApi(value = 17)
focusEglSurface(
    eglDisplay: EGLDisplay!,
    eglContext: EGLContext!,
    eglSurface: EGLSurface!,
    width: Int,
    height: Int
)

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

java-static Unit
@RequiresApi(value = 17)
focusFramebuffer(
    eglDisplay: EGLDisplay!,
    eglContext: EGLContext!,
    eglSurface: EGLSurface!,
    framebuffer: Int,
    width: Int,
    height: Int
)

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

java-static Unit
focusFramebufferUsingCurrentContext(
    framebuffer: Int,
    width: Int,
    height: Int
)

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

java-static Int

Returns a new, unbound GL texture identifier.

java-static Long

Returns the EGL_CONTEXT_CLIENT_VERSION of the current context.

java-static EGLContext!

Gets the current context.

java-static EGLDisplay!

Returns an initialized default EGLDisplay.

java-static FloatArray<Float>!

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

java-static FloatArray<Float>!

Typical bounds used for sampling from textures.

java-static Boolean

Returns whether EXTENSION_COLORSPACE_BT2020_PQ is supported.

java-static Boolean

Returns whether creating a GL context with EXTENSION_PROTECTED_CONTENT is possible.

java-static Boolean

Returns whether the EXTENSION_SURFACELESS_CONTEXT extension is supported.

java-static Boolean

Returns whether the EXTENSION_YUV_TARGET extension is supported.

java-static Unit
setTexture(texId: Int, bitmap: Bitmap!)

Sets the texId to contain the bitmap data and size.

java-static Unit

Sets the input matrix to an identity matrix.

Constants

EGL_CONFIG_ATTRIBUTES_RGBA_1010102

const val EGL_CONFIG_ATTRIBUTES_RGBA_1010102IntArray<Int>!

EGL_CONFIG_ATTRIBUTES_RGBA_8888

const val EGL_CONFIG_ATTRIBUTES_RGBA_8888IntArray<Int>!

HOMOGENEOUS_COORDINATE_VECTOR_SIZE

const val HOMOGENEOUS_COORDINATE_VECTOR_SIZE = 4: Int

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

LENGTH_NDC

const val LENGTH_NDC = 2.0f: Float

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

Public functions

awaitSyncObject

java-static fun awaitSyncObject(syncObject: Long): Unit

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

java-static fun bindTexture(textureTarget: Int, texId: Int): Unit

Binds the texture of the given type with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.

Parameters
textureTarget: Int

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.

texId: Int

The texture identifier.

checkGlError

java-static fun checkGlError(): Unit

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

checkGlException

java-static fun checkGlException(expression: Boolean, errorMessage: String!): Unit

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

clearFocusedBuffers

java-static fun clearFocusedBuffers(): Unit

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

java-static fun create4x4IdentityMatrix(): FloatArray<Float>!

Creates a 4x4 identity matrix.

createBuffer

java-static fun createBuffer(data: FloatArray!): FloatBuffer!

Allocates a FloatBuffer with the given data.

Parameters
data: FloatArray!

Used to initialize the new buffer.

createEglContext

@RequiresApi(value = 17)
java-static fun createEglContext(eglDisplay: EGLDisplay!): EGLContext!

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)
java-static fun createEglContext(
    sharedContext: EGLContext!,
    eglDisplay: EGLDisplay!,
    openGlVersion: @IntRange(from = 2, to = 3) Int,
    configAttributes: IntArray!
): EGLContext!

Creates a new EGLContext for the specified EGLDisplay.

Parameters
sharedContext: EGLContext!

The EGLContext with which to share data.

eglDisplay: EGLDisplay!

The EGLDisplay to create an EGLContext for.

openGlVersion: @IntRange(from = 2, to = 3) Int

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

configAttributes: IntArray!

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

createEglSurface

@RequiresApi(value = 17)
java-static fun createEglSurface(
    eglDisplay: EGLDisplay!,
    surface: Any!,
    @C.ColorTransfer colorTransfer: Int,
    isEncoderInputSurface: Boolean
): EGLSurface!

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.

surface: Any!

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

@C.ColorTransfer colorTransfer: Int

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.

isEncoderInputSurface: Boolean

Whether the surface is the input surface of an encoder.

createExternalTexture

java-static fun createExternalTexture(): Int

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

createFboForTexture

java-static fun createFboForTexture(texId: Int): Int

Returns a new framebuffer for the texture.

Parameters
texId: Int

The identifier of the texture to attach to the framebuffer.

createFocusedPlaceholderEglSurface

@RequiresApi(value = 17)
java-static fun createFocusedPlaceholderEglSurface(
    eglContext: EGLContext!,
    eglDisplay: EGLDisplay!
): EGLSurface!

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)
java-static fun createGlSyncFence(): Long

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

java-static fun createTexture(bitmap: Bitmap!): Int

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

java-static fun createTexture(
    width: Int,
    height: Int,
    useHighPrecisionColorComponents: Boolean
): Int

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
width: Int

The width of the new texture in pixels.

height: Int

The height of the new texture in pixels.

useHighPrecisionColorComponents: Boolean

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

java-static fun createVertexBuffer(vertexList: (Mutable)List<FloatArray!>!): FloatArray<Float>!

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

deleteFbo

java-static fun deleteFbo(fboId: Int): Unit

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

deleteRbo

java-static fun deleteRbo(rboId: Int): Unit

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

deleteSyncObject

java-static fun deleteSyncObject(syncObject: Long): Unit

Deletes the underlying native object.

The syncObject must not be used after deletion.

deleteSyncObjectQuietly

java-static fun deleteSyncObjectQuietly(syncObject: Long): Unit

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

deleteTexture

java-static fun deleteTexture(textureId: Int): Unit

Deletes a GL texture.

Parameters
textureId: Int

The ID of the texture to delete.

destroyEglContext

@RequiresApi(value = 17)
java-static fun destroyEglContext(eglDisplay: EGLDisplay?, eglContext: EGLContext?): Unit

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)
java-static fun destroyEglSurface(eglDisplay: EGLDisplay?, eglSurface: EGLSurface?): Unit

Destroys the EGLSurface identified by the provided EGLDisplay and .

focusEglSurface

@RequiresApi(value = 17)
java-static fun focusEglSurface(
    eglDisplay: EGLDisplay!,
    eglContext: EGLContext!,
    eglSurface: EGLSurface!,
    width: Int,
    height: Int
): Unit

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

focusFramebuffer

@RequiresApi(value = 17)
java-static fun focusFramebuffer(
    eglDisplay: EGLDisplay!,
    eglContext: EGLContext!,
    eglSurface: EGLSurface!,
    framebuffer: Int,
    width: Int,
    height: Int
): Unit

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

focusFramebufferUsingCurrentContext

java-static fun focusFramebufferUsingCurrentContext(
    framebuffer: Int,
    width: Int,
    height: Int
): Unit

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
framebuffer: Int

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

width: Int

The viewport width, in pixels.

height: Int

The viewport height, in pixels.

generateTexture

java-static fun generateTexture(): Int

Returns a new, unbound GL texture identifier.

getContextMajorVersion

@RequiresApi(value = 17)
java-static fun getContextMajorVersion(): Long

Returns the EGL_CONTEXT_CLIENT_VERSION of the current context.

Returns 0 if no EGLContextis focused.

getCurrentContext

@RequiresApi(value = 17)
java-static fun getCurrentContext(): EGLContext!

Gets the current context.

getDefaultEglDisplay

@RequiresApi(value = 17)
java-static fun getDefaultEglDisplay(): EGLDisplay!

Returns an initialized default EGLDisplay.

getNormalizedCoordinateBounds

java-static fun getNormalizedCoordinateBounds(): FloatArray<Float>!

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

getTextureCoordinateBounds

java-static fun getTextureCoordinateBounds(): FloatArray<Float>!

Typical bounds used for sampling from textures.

isBt2020PqExtensionSupported

java-static fun isBt2020PqExtensionSupported(): Boolean

Returns whether EXTENSION_COLORSPACE_BT2020_PQ is supported.

isProtectedContentExtensionSupported

java-static fun isProtectedContentExtensionSupported(context: Context!): Boolean

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

java-static fun isSurfacelessContextExtensionSupported(): Boolean

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

java-static fun isYuvTargetExtensionSupported(): Boolean

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

java-static fun setTexture(texId: Int, bitmap: Bitmap!): Unit

Sets the texId to contain the bitmap data and size.

setToIdentity

java-static fun setToIdentity(matrix: FloatArray!): Unit

Sets the input matrix to an identity matrix.