GlUtil
@UnstableApi
class GlUtil
OpenGL ES utilities.
Summary
Nested types |
|---|
class GlUtil.GlException : ExceptionThrown 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.0fLength 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, sampleFilter: Int)Binds the texture of the given type with the specified MIN and MAG sampling filter and GL_CLAMP_TO_EDGE wrapping. |
java-static Unit |
blitFrameBuffer(Copies the pixels from |
java-static Unit |
Collects all OpenGL errors that occurred since this method was last called and throws a |
java-static Unit |
checkGlException(expression: Boolean, errorMessage: String!)Throws a |
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! |
createBuffer(data: FloatArray!)Allocates a FloatBuffer with the given data. |
java-static EGLContext! |
createEglContext(eglDisplay: EGLDisplay!)Creates a new |
java-static EGLContext! |
createEglContext(Creates a new |
java-static EGLSurface! |
createEglSurface(Creates a new |
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 |
createFboForTexture(texId: Int)Returns a new framebuffer for the texture. |
java-static EGLSurface! |
createFocusedPlaceholderEglSurface(Creates and focuses a placeholder |
java-static Long |
Returns a newly created sync object and inserts it into the GL command stream. |
java-static Int |
createPixelBufferObject(size: Int)Creates a pixel buffer object with a data store of the given size and usage |
java-static Int |
createRgb10A2Texture(width: Int, height: Int)Allocates a new |
java-static Int |
createTexture(bitmap: Bitmap!)Allocates a new texture, initialized with the |
java-static Int |
createTexture(Allocates a new RGBA texture with the specified dimensions and color component precision. |
java-static FloatArray<Float>! |
createVertexBuffer(vertexList: (Mutable)List<FloatArray!>!)Flattens the list of 4 element NDC coordinate vectors into a buffer. |
java-static Unit |
deleteBuffer(bufferId: Int)Deletes a buffer object, or silently ignores the method call if |
java-static Unit |
Deletes a framebuffer, or silently ignores the method call if |
java-static Unit |
Deletes a renderbuffer, or silently ignores the method call if |
java-static Unit |
deleteSyncObject(syncObject: Long)Deletes the underlying native object. |
java-static Unit |
deleteSyncObjectQuietly(syncObject: Long)Releases the GL sync object if set, suppressing any error. |
java-static Unit |
deleteTexture(textureId: Int)Deletes a GL texture. |
java-static Unit |
destroyEglContext(eglDisplay: EGLDisplay?, eglContext: EGLContext?)Destroys the |
java-static Unit |
destroyEglSurface(eglDisplay: EGLDisplay?, eglSurface: EGLSurface?)Destroys the |
java-static Unit |
focusEglSurface(Makes the specified |
java-static Unit |
focusFramebuffer(Makes the specified |
java-static Unit |
focusFramebufferUsingCurrentContext(Makes the specified |
java-static Int |
Returns a new, unbound GL texture identifier. |
java-static Long |
Returns the |
java-static EGLContext! |
Gets the current |
java-static EGLDisplay! |
Returns an initialized default |
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 |
java-static Boolean |
Returns whether |
java-static Boolean |
isColorTransferSupported(@C.ColorTransfer colorTransfer: Int)Returns whether the given |
java-static Boolean |
isProtectedContentExtensionSupported(context: Context!)Returns whether creating a GL context with |
java-static Boolean |
Returns whether the |
java-static Boolean |
Returns whether the |
java-static ByteBuffer! |
@RequiresApi(value = 24)Maps the pixel buffer object's data store of a given size and returns a |
java-static Unit |
@RequiresApi(value = 24)Reads pixel data from the |
java-static Unit |
setTexture(texId: Int, bitmap: Bitmap!)Sets the |
java-static Unit |
setToIdentity(matrix: FloatArray!)Sets the input |
java-static Unit |
terminate(eglDisplay: EGLDisplay!)Terminates the |
java-static Unit |
@RequiresApi(value = 24)Unmaps the pixel buffer object |
Constants
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, sampleFilter: Int): Unit
Binds the texture of the given type with the specified MIN and MAG sampling filter and GL_CLAMP_TO_EDGE wrapping.
| Parameters | |
|---|---|
textureTarget: Int |
The target to which the texture is bound, e.g. |
texId: Int |
The texture identifier. |
sampleFilter: Int |
The texture sample filter for both |
blitFrameBuffer
java-static fun blitFrameBuffer(
readFboId: Int,
readRect: GlRect!,
drawFboId: Int,
drawRect: GlRect!
): Unit
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
java-static fun checkGlError(): Unit
Collects all OpenGL errors that occurred since this method was last called and throws a GlException 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
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 |
createEglContext
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 |
eglDisplay: EGLDisplay! |
The |
openGlVersion: @IntRange(from = 2, to = 3) Int |
The version of OpenGL ES to configure. Accepts either |
configAttributes: IntArray! |
The attributes to configure EGL with. Accepts either |
createEglSurface
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 |
surface: Any! |
The surface to wrap; must be a surface, surface texture or surface holder. |
@C.ColorTransfer colorTransfer: Int |
The |
isEncoderInputSurface: Boolean |
Whether the |
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
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 |
eglDisplay: EGLDisplay! |
The |
| Returns | |
|---|---|
EGLSurface! |
|
createGlSyncFence
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.
createPixelBufferObject
java-static fun createPixelBufferObject(size: Int): Int
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 | |
|---|---|
size: Int |
The size of the buffer object's data store. |
| Returns | |
|---|---|
Int |
The pixel buffer object. |
createRgb10A2Texture
java-static fun createRgb10A2Texture(width: Int, height: Int): Int
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 | |
|---|---|
width: Int |
The width of the new texture in pixels. |
height: Int |
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
java-static fun createTexture(bitmap: Bitmap!): Int
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
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 |
| 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.
deleteBuffer
java-static fun deleteBuffer(bufferId: Int): Unit
Deletes a buffer object, or silently ignores the method call if bufferId is unused.
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
java-static fun destroyEglContext(eglDisplay: EGLDisplay?, eglContext: EGLContext?): Unit
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
java-static fun destroyEglSurface(eglDisplay: EGLDisplay?, eglSurface: EGLSurface?): Unit
Destroys the EGLSurface identified by the provided EGLDisplay and EGLSurface.
focusEglSurface
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
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.
generateTexture
java-static fun generateTexture(): Int
Returns a new, unbound GL texture identifier.
getContextMajorVersion
java-static fun getContextMajorVersion(): Long
Returns the EGL_CONTEXT_CLIENT_VERSION of the current context.
Returns 0 if no EGLContextis focused.
getDefaultEglDisplay
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.
isBt2020HlgExtensionSupported
java-static fun isBt2020HlgExtensionSupported(): Boolean
Returns whether EXTENSION_COLORSPACE_BT2020_HLG is supported.
isBt2020PqExtensionSupported
java-static fun isBt2020PqExtensionSupported(): Boolean
Returns whether EXTENSION_COLORSPACE_BT2020_PQ is supported.
isColorTransferSupported
java-static fun isColorTransferSupported(@C.ColorTransfer colorTransfer: Int): Boolean
Returns whether the given C.ColorTransfer 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.
mapPixelBufferObject
@RequiresApi(value = 24)
java-static fun mapPixelBufferObject(bufferId: Int, size: Int): ByteBuffer!
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 | |
|---|---|
bufferId: Int |
The pixel buffer object. |
size: Int |
The size of the pixel buffer object's data store to be mapped. |
| Returns | |
|---|---|
ByteBuffer! |
The |
schedulePixelBufferRead
@RequiresApi(value = 24)
java-static fun schedulePixelBufferRead(
readFboId: Int,
width: Int,
height: Int,
bufferId: Int
): Unit
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.
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.
terminate
java-static fun terminate(eglDisplay: EGLDisplay!): Unit
Terminates the EGLDisplay connection.
unmapPixelBufferObject
@RequiresApi(value = 24)
java-static fun unmapPixelBufferObject(bufferId: Int): Unit
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 | |
|---|---|
bufferId: Int |
The pixel buffer object. |