@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, int sampleFilter)

Binds the texture of the given type with the specified MIN and MAG sampling filter and GL_CLAMP_TO_EDGE wrapping.

static void
blitFrameBuffer(
    int readFboId,
    GlRect readRect,
    int drawFboId,
    GlRect drawRect
)

Copies the pixels from readFboId into drawFboId.

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

Creates a new EGLContext for the specified EGLDisplay.

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

Creates a new EGLContext for the specified EGLDisplay.

static EGLSurface
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
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

Creates a pixel buffer object with a data store of the given size and usage GL_DYNAMIC_READ.

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
deleteBuffer(int bufferId)

Deletes a buffer object, or silently ignores the method call if bufferId is unused.

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
destroyEglContext(
    @Nullable EGLDisplay eglDisplay,
    @Nullable EGLContext eglContext
)

Destroys the EGLContext identified by the provided EGLDisplay and .

static void
destroyEglSurface(
    @Nullable EGLDisplay eglDisplay,
    @Nullable EGLSurface eglSurface
)

Destroys the EGLSurface identified by the provided EGLDisplay and .

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.

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.

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_HLG is supported.

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 ByteBuffer
@RequiresApi(value = 24)
mapPixelBufferObject(int bufferId, int size)

Maps the pixel buffer object's data store of a given size and returns a ByteBuffer of OpenGL managed memory.

static void
@RequiresApi(value = 24)
schedulePixelBufferRead(
    int readFboId,
    int width,
    int height,
    int bufferId
)

Reads pixel data from the GL_COLOR_ATTACHMENT0 attachment of a framebuffer into the data store of a pixel buffer object.

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.

static void
@RequiresApi(value = 24)
unmapPixelBufferObject(int bufferId)

Unmaps the pixel buffer object bufferId's data store.

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, int sampleFilter)

Binds the texture of the given type with the specified MIN and MAG sampling filter 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.

int sampleFilter

The texture sample filter for both GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MIN_FILTER.

blitFrameBuffer

public static void blitFrameBuffer(
    int readFboId,
    GlRect readRect,
    int drawFboId,
    GlRect drawRect
)

Copies the pixels from readFboId into drawFboId. Requires OpenGL ES 3.0.

When the input pixel region (given by readRect) doesn't have the same size as the output region (given by drawRect), this method uses GL_LINEAR filtering to scale the image contents.

Parameters
int readFboId

The framebuffer object to read from.

GlRect readRect

The rectangular region of readFboId to read from.

int drawFboId

The framebuffer object to draw into.

GlRect drawRect

The rectangular region of drawFboId to draw into.

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

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

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

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

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

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.

createPixelBufferObject

public static int createPixelBufferObject(int size)

Creates a pixel buffer object with a data store of the given size and usage GL_DYNAMIC_READ.

The buffer is suitable for repeated modification by OpenGL and reads by the application.

Parameters
int size

The size of the buffer object's data store.

Returns
int

The pixel buffer object.

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.

deleteBuffer

public static void deleteBuffer(int bufferId)

Deletes a buffer object, or silently ignores the method call if bufferId is unused.

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

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

public static void destroyEglSurface(
    @Nullable EGLDisplay eglDisplay,
    @Nullable EGLSurface eglSurface
)

Destroys the EGLSurface identified by the provided EGLDisplay and .

focusEglSurface

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

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

public static long getContextMajorVersion()

Returns the EGL_CONTEXT_CLIENT_VERSION of the current context.

Returns 0 if no EGLContextis focused.

getCurrentContext

public static EGLContext getCurrentContext()

Gets the current context.

getDefaultEglDisplay

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.

isBt2020HlgExtensionSupported

public static boolean isBt2020HlgExtensionSupported()

Returns whether EXTENSION_COLORSPACE_BT2020_HLG is supported.

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.

mapPixelBufferObject

@RequiresApi(value = 24)
public static ByteBuffer mapPixelBufferObject(int bufferId, int size)

Maps the pixel buffer object's data store of a given size and returns a ByteBuffer of OpenGL managed memory.

The application must not write into the returned ByteBuffer.

The pixel buffer object should have a previously scheduled pixel buffer read.

When the application no longer needs to access the returned buffer, call unmapPixelBufferObject.

This call blocks until the pixel buffer data from the last schedulePixelBufferRead call is available.

Requires API 24: see schedulePixelBufferRead.

Parameters
int bufferId

The pixel buffer object.

int size

The size of the pixel buffer object's data store to be mapped.

Returns
ByteBuffer

The ByteBuffer that holds pixel data.

schedulePixelBufferRead

@RequiresApi(value = 24)
public static void schedulePixelBufferRead(
    int readFboId,
    int width,
    int height,
    int bufferId
)

Reads pixel data from the GL_COLOR_ATTACHMENT0 attachment of a framebuffer into the data store of a pixel buffer object.

The texture backing the color attachment of readFboId and the buffer store of bufferId must hold an image of the given width and height with format GL_RGBA and type GL_UNSIGNED_BYTE.

This a non-blocking call which reads the data asynchronously.

Requires API 24: This method must call the version of glReadPixels which accepts an integer offset as the last parameter. This version of glReadPixels is not available in the Java GLES30 wrapper until API 24.

HDR support is not yet implemented.

Parameters
int readFboId

The framebuffer that holds pixel data.

int width

The image width.

int height

The image height.

int bufferId

The pixel buffer object to read into.

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.

unmapPixelBufferObject

@RequiresApi(value = 24)
public static void unmapPixelBufferObject(int bufferId)

Unmaps the pixel buffer object bufferId's data store.

The pixel buffer object should be previously mapped.

After this method returns, accessing data inside a previously mappedByteBuffer results in undefined behaviour.

When this method returns, the pixel buffer object bufferId can be reused by schedulePixelBufferRead.

Requires API 24: see schedulePixelBufferRead.

Parameters
int bufferId

The pixel buffer object.