Package com.google.android.exoplayer2.util

## Class GlUtil

- java.lang.Object
- - com.google.android.exoplayer2.util.GlUtil

-     public final class GlUtil
      extends Object

  OpenGL ES utilities.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class | Description
    --- | --- | ---
    `static class` | `GlUtil.GlException` | Thrown when an OpenGL error occurs.

  - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `static int[]` | `EGL_CONFIG_ATTRIBUTES_RGBA_1010102` |
    `static int[]` | `EGL_CONFIG_ATTRIBUTES_RGBA_8888` |
    `static int` | `HOMOGENEOUS_COORDINATE_VECTOR_SIZE` | Number of elements in a 3d homogeneous coordinate vector describing a vertex.
    `static float` | `LENGTH_NDC` | Length of the normalized device coordinate (NDC) space, which spans from -1 to 1.

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `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` | `checkGlError()` | Collects all OpenGL errors that occurred since this method was last called and throws a `GlUtil.GlException` with the combined error message.
    `static void` | `checkGlException​(boolean expression, String errorMessage)` | Throws a `GlUtil.GlException` with the given message if `expression` evaluates to `false`.
    `static void` | `clearOutputFrame()` | Fills the pixels in the current output render target with (r=0, g=0, b=0, a=0).
    `static float[]` | `create4x4IdentityMatrix()` | 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 `EGLContext` for the specified `EGLDisplay`.
    `static EGLContext` | `createEglContext​(EGLDisplay eglDisplay, int[] configAttributes)` | Creates a new `EGLContext` for the specified `EGLDisplay`.
    `static EGLDisplay` | `createEglDisplay()` | Returns an initialized default `EGLDisplay`.
    `static int` | `createExternalTexture()` | 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​(EGLContext eglContext, EGLDisplay eglDisplay, int[] configAttributes)` | Creates and focuses a placeholder `EGLSurface`.
    `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` | `deleteTexture​(int textureId)` | Deletes a GL texture.
    `static void` | `destroyEglContext​(EGLDisplay eglDisplay, EGLContext eglContext)` | Destroys the `EGLContext` identified by the provided `EGLDisplay` and `EGLContext`.
    `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 EGLSurface` | `focusPlaceholderEglSurface​(EGLContext eglContext, EGLDisplay eglDisplay)` | Creates and focuses a placeholder `EGLSurface`.
    `static EGLSurface` | `getEglSurface​(EGLDisplay eglDisplay, Object surface)` | Returns a new `EGLSurface` wrapping the specified `surface`.
    `static EGLSurface` | `getEglSurface​(EGLDisplay eglDisplay, Object surface, int[] configAttributes)` | Returns a new `EGLSurface` wrapping the specified `surface`.
    `static float[]` | `getNormalizedCoordinateBounds()` | Bounds of normalized device coordinates, commonly used for defining viewport boundaries.
    `static float[]` | `getTextureCoordinateBounds()` | Typical bounds used for sampling from textures.
    `static boolean` | `isProtectedContentExtensionSupported​(Context context)` | Returns whether creating a GL context with "EGL_EXT_protected_content" is possible.
    `static boolean` | `isSurfacelessContextExtensionSupported()` | Returns whether the "EGL_KHR_surfaceless_context" extension is supported.
    `static boolean` | `isYuvTargetExtensionSupported()` | Returns whether the "GL_EXT_YUV_target" extension is supported.
    `static void` | `setToIdentity​(float[] matrix)` | Sets the input `matrix` to an identity matrix.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### HOMOGENEOUS_COORDINATE_VECTOR_SIZE

          public static final int HOMOGENEOUS_COORDINATE_VECTOR_SIZE

      Number of elements in a 3d homogeneous coordinate vector describing a vertex.
      See Also:
        Constant Field Values

    - #### LENGTH_NDC

          public static final float LENGTH_NDC

      Length of the normalized device coordinate (NDC) space, which spans from -1 to 1.
      See Also:
        Constant Field Values

    - #### EGL_CONFIG_ATTRIBUTES_RGBA_8888

          public static final int[] EGL_CONFIG_ATTRIBUTES_RGBA_8888

    - #### EGL_CONFIG_ATTRIBUTES_RGBA_1010102

          public static final int[] EGL_CONFIG_ATTRIBUTES_RGBA_1010102

  - ### Method Detail

    - #### 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.

    - #### create4x4IdentityMatrix

          public static float[] create4x4IdentityMatrix()

      Creates a 4x4 identity matrix.

    - #### setToIdentity

          public static void setToIdentity​(float[] matrix)

      Sets the input `matrix` to an identity matrix.

    - #### createVertexBuffer

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

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

    - #### isProtectedContentExtensionSupported

          public static boolean isProtectedContentExtensionSupported​(Context context)

      Returns whether creating a GL context with "EGL_EXT_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 "EGL_KHR_surfaceless_context" extension is supported.

      This extension allows passing `EGL14.EGL_NO_SURFACE` for both the write and read surfaces in a call to `EGL14.eglMakeCurrent(EGLDisplay, EGLSurface, EGLSurface, EGLContext)`.

    - #### isYuvTargetExtensionSupported

          public static boolean isYuvTargetExtensionSupported()

      Returns whether the "GL_EXT_YUV_target" extension is supported.

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

    - #### createEglDisplay

          @RequiresApi(17)
          public static EGLDisplay createEglDisplay()
                                             throws GlUtil.GlException

      Returns an initialized default `EGLDisplay`.
      Throws:
        `GlUtil.GlException`

    - #### createEglContext

          @RequiresApi(17)
          public static EGLContext createEglContext​(EGLDisplay eglDisplay)
                                             throws GlUtil.GlException

      Creates a new `EGLContext` for the specified `EGLDisplay`.

      Configures the `EGLContext` with `EGL_CONFIG_ATTRIBUTES_RGBA_8888` and OpenGL ES 2.0.

      Parameters:
        `eglDisplay` - The `EGLDisplay` to create an `EGLContext` for.
      Throws:
        `GlUtil.GlException`

    - #### createEglContext

          @RequiresApi(17)
          public static EGLContext createEglContext​(EGLDisplay eglDisplay,
                                                    int[] configAttributes)
                                             throws GlUtil.GlException

      Creates a new `EGLContext` for the specified `EGLDisplay`.
      Parameters:
        `eglDisplay` - The `EGLDisplay` to create an `EGLContext` for.
        `configAttributes` - The attributes to configure EGL with. Accepts either `EGL_CONFIG_ATTRIBUTES_RGBA_1010102`, which will request OpenGL ES 3.0, or `EGL_CONFIG_ATTRIBUTES_RGBA_8888`, which will request OpenGL ES 2.0.
      Throws:
        `GlUtil.GlException`

    - #### getEglSurface

          @RequiresApi(17)
          public static EGLSurface getEglSurface​(EGLDisplay eglDisplay,
                                                 Object surface)
                                          throws GlUtil.GlException

      Returns a new `EGLSurface` wrapping the specified `surface`.

      The `EGLSurface` will configure with `EGL_CONFIG_ATTRIBUTES_RGBA_8888` and OpenGL ES 2.0.

      Parameters:
        `eglDisplay` - The `EGLDisplay` to attach the surface to.
        `surface` - The surface to wrap; must be a surface, surface texture or surface holder.
      Throws:
        `GlUtil.GlException`

    - #### getEglSurface

          @RequiresApi(17)
          public static EGLSurface getEglSurface​(EGLDisplay eglDisplay,
                                                 Object surface,
                                                 int[] configAttributes)
                                          throws GlUtil.GlException

      Returns a new `EGLSurface` wrapping the specified `surface`.
      Parameters:
        `eglDisplay` - The `EGLDisplay` to attach the surface to.
        `surface` - The surface to wrap; must be a surface, surface texture or surface holder.
        `configAttributes` - The attributes to configure EGL with. Accepts `EGL_CONFIG_ATTRIBUTES_RGBA_1010102` and `EGL_CONFIG_ATTRIBUTES_RGBA_8888`.
      Throws:
        `GlUtil.GlException`

    - #### focusPlaceholderEglSurface

          @RequiresApi(17)
          public static EGLSurface focusPlaceholderEglSurface​(EGLContext eglContext,
                                                              EGLDisplay eglDisplay)
                                                       throws GlUtil.GlException

      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` - The `EGLContext` to make current.
        `eglDisplay` - The `EGLDisplay` to attach the surface to.
      Returns:
        `EGL14.EGL_NO_SURFACE` if supported and a 1x1 pixel buffer surface otherwise.
      Throws:
        `GlUtil.GlException`

    - #### createFocusedPlaceholderEglSurface

          @RequiresApi(17)
          public static EGLSurface createFocusedPlaceholderEglSurface​(EGLContext eglContext,
                                                                      EGLDisplay eglDisplay,
                                                                      int[] configAttributes)
                                                               throws GlUtil.GlException

      Creates and focuses a placeholder `EGLSurface`.

      This makes a `EGLContext` current when reading and writing to a surface is not required.

      Parameters:
        `eglContext` - The `EGLContext` to make current.
        `eglDisplay` - The `EGLDisplay` to attach the surface to.
        `configAttributes` - The attributes to configure EGL with. Accepts `EGL_CONFIG_ATTRIBUTES_RGBA_1010102` and `EGL_CONFIG_ATTRIBUTES_RGBA_8888`.
      Returns:
        A placeholder `EGLSurface` that has been focused to allow rendering to take place, or `EGL14.EGL_NO_SURFACE` if the current context supports rendering without a surface.
      Throws:
        `GlUtil.GlException`

    - #### checkGlError

          public static void checkGlError()
                                   throws GlUtil.GlException

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

    - #### clearOutputFrame

          public static void clearOutputFrame()
                                       throws GlUtil.GlException

      Fills the pixels in the current output render target with (r=0, g=0, b=0, a=0).
      Throws:
        `GlUtil.GlException`

    - #### focusEglSurface

          @RequiresApi(17)
          public static void focusEglSurface​(EGLDisplay eglDisplay,
                                             EGLContext eglContext,
                                             EGLSurface eglSurface,
                                             int width,
                                             int height)
                                      throws GlUtil.GlException

      Makes the specified `eglSurface` the render target, using a viewport of `width` by `height` pixels.
      Throws:
        `GlUtil.GlException`

    - #### focusFramebuffer

          @RequiresApi(17)
          public static void focusFramebuffer​(EGLDisplay eglDisplay,
                                              EGLContext eglContext,
                                              EGLSurface eglSurface,
                                              int framebuffer,
                                              int width,
                                              int height)
                                       throws GlUtil.GlException

      Makes the specified `framebuffer` the render target, using a viewport of `width` by `height` pixels.
      Throws:
        `GlUtil.GlException`

    - #### focusFramebufferUsingCurrentContext

          @RequiresApi(17)
          public static void focusFramebufferUsingCurrentContext​(int framebuffer,
                                                                 int width,
                                                                 int height)
                                                          throws GlUtil.GlException

      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` - The identifier of the framebuffer object to bind as the output render target.
        `width` - The viewport width, in pixels.
        `height` - The viewport height, in pixels.
      Throws:
        `GlUtil.GlException`

    - #### deleteTexture

          public static void deleteTexture​(int textureId)
                                    throws GlUtil.GlException

      Deletes a GL texture.
      Parameters:
        `textureId` - The ID of the texture to delete.
      Throws:
        `GlUtil.GlException`

    - #### destroyEglContext

          @RequiresApi(17)
          public static void destroyEglContext​(@Nullable
                                               EGLDisplay eglDisplay,
                                               @Nullable
                                               EGLContext eglContext)
                                        throws GlUtil.GlException

      Destroys the `EGLContext` identified by the provided `EGLDisplay` and `EGLContext`.
      Throws:
        `GlUtil.GlException`

    - #### createBuffer

          public static FloatBuffer createBuffer​(float[] data)

      Allocates a FloatBuffer with the given data.
      Parameters:
        `data` - Used to initialize the new buffer.

    - #### createExternalTexture

          public static int createExternalTexture()
                                           throws GlUtil.GlException

      Creates a GL_TEXTURE_EXTERNAL_OES with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.
      Throws:
        `GlUtil.GlException`

    - #### createTexture

          public static int createTexture​(int width,
                                          int height,
                                          boolean useHighPrecisionColorComponents)
                                   throws GlUtil.GlException

      Allocates a new RGBA texture with the specified dimensions and color component precision.
      Parameters:
        `width` - The width of the new texture in pixels.
        `height` - The height of the new texture in pixels.
        `useHighPrecisionColorComponents` - If `false`, uses 8-bit unsigned bytes. If `true`, use 16-bit (half-precision) floating-point.
      Returns:
        The texture identifier for the newly-allocated texture.
      Throws:
        `GlUtil.GlException` - If the texture allocation fails.

    - #### bindTexture

          public static void bindTexture​(int textureTarget,
                                         int texId)
                                  throws GlUtil.GlException

      Binds the texture of the given type with default configuration of GL_LINEAR filtering and GL_CLAMP_TO_EDGE wrapping.
      Parameters:
        `textureTarget` - The target to which the texture is bound, e.g. `GLES20.GL_TEXTURE_2D` for a two-dimensional texture or `GLES11Ext.GL_TEXTURE_EXTERNAL_OES` for an external texture.
        `texId` - The texture identifier.
      Throws:
        `GlUtil.GlException`

    - #### createFboForTexture

          public static int createFboForTexture​(int texId)
                                         throws GlUtil.GlException

      Returns a new framebuffer for the texture.
      Parameters:
        `texId` - The identifier of the texture to attach to the framebuffer.
      Throws:
        `GlUtil.GlException`

    - #### checkGlException

          public static void checkGlException​(boolean expression,
                                              String errorMessage)
                                       throws GlUtil.GlException

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