# GLFrameBufferRenderer.Callback

Artifact: [androidx.graphics:graphics-core](https://developer.android.com/jetpack/androidx/releases/graphics) [View Source](https://cs.android.com/search?q=file:androidx/graphics/opengl/GLFrameBufferRenderer.kt+class:androidx.graphics.opengl.GLFrameBufferRenderer.Callback) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

*** ** * ** ***


```
interface GLFrameBufferRenderer.Callback
```

<br />

*** ** * ** ***

`https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLFrameBufferRenderer` callbacks that are invoked to render OpenGL content within the underlying buffers. This includes an optional callback to be used to configure the underlying `https://developer.android.com/reference/kotlin/androidx/graphics/surface/SurfaceControlCompat.Transaction` used to present content to the display

## Summary

| ### Public functions |
|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLFrameBufferRenderer.Callback#onBufferReleased(androidx.graphics.opengl.FrameBuffer,androidx.hardware.SyncFenceCompat)(frameBuffer: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBuffer, releaseFence: https://developer.android.com/reference/kotlin/androidx/hardware/SyncFenceCompat?)` Optional callback invoked the thread backed by the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer` when the provided framebuffer is released. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLFrameBufferRenderer.Callback#onDrawComplete(androidx.graphics.surface.SurfaceControlCompat,androidx.graphics.surface.SurfaceControlCompat.Transaction,androidx.graphics.opengl.FrameBuffer,androidx.hardware.SyncFenceCompat)( targetSurfaceControl: https://developer.android.com/reference/kotlin/androidx/graphics/surface/SurfaceControlCompat, transaction: https://developer.android.com/reference/kotlin/androidx/graphics/surface/SurfaceControlCompat.Transaction, frameBuffer: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBuffer, syncFence: https://developer.android.com/reference/kotlin/androidx/hardware/SyncFenceCompat? )` Optional callback invoked the thread backed by the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer` when rendering to a buffer is complete but before the buffer is submitted to the hardware compositor. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLFrameBufferRenderer.Callback#onDrawFrame(androidx.graphics.opengl.egl.EGLManager,kotlin.Int,kotlin.Int,androidx.graphics.lowlatency.BufferInfo,kotlin.FloatArray)( eglManager: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/egl/EGLManager, width: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, height: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, bufferInfo: https://developer.android.com/reference/kotlin/androidx/graphics/lowlatency/BufferInfo, transform: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html )` Callback invoked on the thread backed by the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer` to render content into a buffer with the specified parameters. |

## Public functions

### onBufferReleased

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
@WorkerThread
open fun onBufferReleased(frameBuffer: FrameBuffer, releaseFence: SyncFenceCompat?): Unit
```

Optional callback invoked the thread backed by the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer` when the provided framebuffer is released. That is the given `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBuffer` instance is no longer being presented and is not visible.

| Parameters |
|---|---|
| `frameBuffer: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBuffer` | The buffer that is no longer being presented and has returned to the buffer allocation pool |
| `releaseFence: https://developer.android.com/reference/kotlin/androidx/hardware/SyncFenceCompat?` | Optional fence that must be waited upon before the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBuffer` can be reused. The framework will invoke this callback early to improve performance and signal the fence when it is ready to be re-used. |

### onDrawComplete

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
@WorkerThread
open fun onDrawComplete(
    targetSurfaceControl: SurfaceControlCompat,
    transaction: SurfaceControlCompat.Transaction,
    frameBuffer: FrameBuffer,
    syncFence: SyncFenceCompat?
): Unit
```

Optional callback invoked the thread backed by the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer` when rendering to a buffer is complete but before the buffer is submitted to the hardware compositor. This provides consumers a mechanism for synchronizing the transaction with other `https://developer.android.com/reference/kotlin/androidx/graphics/surface/SurfaceControlCompat` objects that maybe rendered within the scene.

| Parameters |
|---|---|
| `targetSurfaceControl: https://developer.android.com/reference/kotlin/androidx/graphics/surface/SurfaceControlCompat` | Handle to the `https://developer.android.com/reference/kotlin/androidx/graphics/surface/SurfaceControlCompat` where the buffer is presented. This can be used to configure various properties of the `https://developer.android.com/reference/kotlin/androidx/graphics/surface/SurfaceControlCompat` like z-ordering or visibility with the corresponding `https://developer.android.com/reference/kotlin/androidx/graphics/surface/SurfaceControlCompat.Transaction`. |
| `transaction: https://developer.android.com/reference/kotlin/androidx/graphics/surface/SurfaceControlCompat.Transaction` | Current `https://developer.android.com/reference/kotlin/androidx/graphics/surface/SurfaceControlCompat.Transaction` to apply updated buffered content to the front buffered layer. |
| `frameBuffer: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBuffer` | The buffer that has been rendered into and is ready to be displayed. The `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` backing this `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBuffer` is already configured to be presented for the targetSurfaceControl. That is `https://developer.android.com/reference/kotlin/androidx/graphics/surface/SurfaceControlCompat.Transaction#setBuffer(androidx.graphics.surface.SurfaceControlCompat,android.hardware.HardwareBuffer,androidx.hardware.SyncFenceCompat,kotlin.Function1)` is already invoked with the given `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` and optional `https://developer.android.com/reference/kotlin/androidx/hardware/SyncFenceCompat` instance before this method is invoked. |
| `syncFence: https://developer.android.com/reference/kotlin/androidx/hardware/SyncFenceCompat?` | Optional `https://developer.android.com/reference/kotlin/androidx/hardware/SyncFenceCompat` is used to determine when rendering is done and reflected within the given frameBuffer. |

### onDrawFrame

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

```
@WorkerThread
fun onDrawFrame(
    eglManager: EGLManager,
    width: Int,
    height: Int,
    bufferInfo: BufferInfo,
    transform: FloatArray
): Unit
```

Callback invoked on the thread backed by the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer` to render content into a buffer with the specified parameters.

```kotlin
import androidx.graphics.lowlatency.BufferInfo
import androidx.graphics.opengl.GLFrameBufferRenderer
import androidx.graphics.opengl.egl.EGLManager

val surfaceView = SurfaceView(context)
val renderer =
    GLFrameBufferRenderer.Builder(
            surfaceView,
            object : GLFrameBufferRenderer.Callback {

                val myMatrix = FloatArray(16)
                val result = FloatArray(16)

                override fun onDrawFrame(
                    eglManager: EGLManager,
                    width: Int,
                    height: Int,
                    bufferInfo: BufferInfo,
                    transform: FloatArray,
                ) {
                    Matrix.orthoM(
                        myMatrix, // matrix
                        0, // offset starting index into myMatrix
                        0f, // left
                        bufferInfo.width.toFloat(), // right
                        0f, // bottom
                        bufferInfo.width.toFloat(), // top
                        -1f, // near
                        1f, // far
                    )

                    Matrix.multiplyMM(result, 0, myMatrix, 0, transform, 0)

                    // pass result matrix as uniform to shader logic
                }
            },
        )
        .build()
renderer.render()
```

| Parameters |
|---|---|
| `eglManager: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/egl/EGLManager` | `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/egl/EGLManager` useful in configuring EGL objects to be used when issuing OpenGL commands to render into the front buffered layer |
| `width: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Logical width of the content to render. This dimension matches what is provided from `https://developer.android.com/reference/android/view/SurfaceHolder.Callback.html#surfaceChanged(android.view.SurfaceHolder, int, int, int)` |
| `height: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Logical height of the content to render. This dimension matches what is provided from `https://developer.android.com/reference/android/view/SurfaceHolder.Callback.html#surfaceChanged(android.view.SurfaceHolder, int, int, int)` |
| `bufferInfo: https://developer.android.com/reference/kotlin/androidx/graphics/lowlatency/BufferInfo` | `https://developer.android.com/reference/kotlin/androidx/graphics/lowlatency/BufferInfo` about the buffer that is being rendered into. This includes the width and height of the buffer which can be different than the corresponding dimensions of the `https://developer.android.com/reference/android/view/SurfaceView.html` provided to the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLFrameBufferRenderer` as pre-rotation can occasionally swap width and height parameters in order to avoid GPU composition to rotate content. This should be used as input to `https://developer.android.com/reference/android/opengl/GLES20.html#glViewport(int, int, int, int)`. Additionally this also contains a frame buffer identifier that can be used to retarget rendering operations to the original destination after rendering into intermediate scratch buffers. |
| `transform: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html` | Matrix that should be applied to the rendering in this callback. This should be consumed as input to any vertex shader implementations. Buffers are pre-rotated in advance in order to avoid unnecessary overhead of GPU composition to rotate content in the same install orientation of the display. This is a 4 x 4 matrix is represented as a flattened array of 16 floating point values. Consumers are expected to leverage `https://developer.android.com/reference/android/opengl/Matrix.html#multiplyMM(float[], int, float[], int, float[], int)` with this parameter alongside any additional transformations that are to be applied. For example: ```kotlin val myMatrix = FloatArray(16) Matrix.orthoM( myMatrix, // matrix 0, // offset starting index into myMatrix 0f, // left bufferInfo.width.toFloat(), // right 0f, // bottom bufferInfo.height.toFloat(), // top -1f, // near 1f, // far ) val result = FloatArray(16) Matrix.multiplyMM(result, 0, myMatrix, 0, transform, 0) ``` |