GlUtil
@UnstableApi
public final class GlUtil
OpenGL ES utilities.
Summary
Nested types |
|---|
public final class GlUtil.GlException extends ExceptionThrown 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.0fLength 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(Copies the pixels from |
static void |
Collects all OpenGL errors that occurred since this method was last called and throws a |
static void |
checkGlException(boolean expression, String errorMessage)Throws a |
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 |
createEglContext(EGLDisplay eglDisplay)Creates a new |
static EGLContext |
createEglContext(Creates a new |
static EGLSurface |
createEglSurface(Creates a new |
static int |
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 |
createFocusedPlaceholderEglSurface(Creates and focuses a placeholder |
static long |
Returns a newly created sync object and inserts it into the GL command stream. |
static int |
createPixelBufferObject(int size)Creates a pixel buffer object with a data store of the given size and usage |
static int |
createRgb10A2Texture(int width, int height)Allocates a new |
static int |
createTexture(Bitmap bitmap)Allocates a new texture, initialized with the |
static int |
createTexture(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 |
static void |
deleteFbo(int fboId)Deletes a framebuffer, or silently ignores the method call if |
static void |
deleteRbo(int rboId)Deletes a renderbuffer, or silently ignores the method call if |
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(Destroys the |
static void |
destroyEglSurface(Destroys the |
static void |
focusEglSurface(Makes the specified |
static void |
focusFramebuffer(Makes the specified |
static void |
focusFramebufferUsingCurrentContext(Makes the specified |
static int |
Returns a new, unbound GL texture identifier. |
static long |
Returns the |
static EGLContext |
Gets the current |
static EGLDisplay |
Returns an initialized default |
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 |
static boolean |
Returns whether |
static boolean |
isColorTransferSupported(@C.ColorTransfer int colorTransfer)Returns whether the given |
static boolean |
Returns whether creating a GL context with |
static boolean |
Returns whether the |
static boolean |
Returns whether the |
static ByteBuffer |
@RequiresApi(value = 24)Maps the pixel buffer object's data store of a given size and returns a |
static void |
@RequiresApi(value = 24)Reads pixel data from the |
static void |
setTexture(int texId, Bitmap bitmap)Sets the |
static void |
setToIdentity(float[] matrix)Sets the input |
static void |
terminate(EGLDisplay eglDisplay)Terminates the |
static void |
@RequiresApi(value = 24)Unmaps the pixel buffer object |
Constants
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. |
int texId |
The texture identifier. |
int sampleFilter |
The texture sample filter for both |
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.
checkGlError
public static void checkGlError()
Collects all OpenGL errors that occurred since this method was last called and throws a GlException 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 |
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 |
EGLDisplay eglDisplay |
The |
@IntRange(from = 2, to = 3) int openGlVersion |
The version of OpenGL ES to configure. Accepts either |
int[] configAttributes |
The attributes to configure EGL with. Accepts either |
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 |
Object surface |
The surface to wrap; must be a surface, surface texture or surface holder. |
@C.ColorTransfer int colorTransfer |
The |
boolean isEncoderInputSurface |
Whether the |
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 |
EGLDisplay eglDisplay |
The |
| Returns | |
|---|---|
EGLSurface |
|
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. |
createRgb10A2Texture
public static int createRgb10A2Texture(int width, int height)
Allocates a new normalized integerGL_RGB10_A2 texture with the specified dimensions.
Normalized integers in textures are automatically converted for floating point numbers https://www.khronos.org/opengl/wiki/Normalized_Integer
The only supported pixel data type for the GL_RGB10_A2 sized internal format is GL_UNSIGNED_INT_2_10_10_10_REV. See https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml
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. |
| 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(Bitmap bitmap)
Allocates a new texture, initialized with the bitmap data and size.
| 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 |
| 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 EGLContext.
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 EGLSurface.
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. |
getContextMajorVersion
public static long getContextMajorVersion()
Returns the EGL_CONTEXT_CLIENT_VERSION of the current context.
Returns 0 if no EGLContextis focused.
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.
isColorTransferSupported
public static boolean isColorTransferSupported(@C.ColorTransfer int colorTransfer)
Returns whether the given C.ColorTransfer 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 |
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. |