# FrameBufferRenderer

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/FrameBufferRenderer.kt+class:androidx.graphics.opengl.FrameBufferRenderer) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.0.0)

*** ** * ** ***


```
@RequiresApi(value = 26)
class FrameBufferRenderer : GLRenderer.RenderCallback
```

<br />

*** ** * ** ***

`https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer.RenderCallback` implementation that renders content into a frame buffer object backed by a `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` object

## Summary

| ### Nested types |
|---|
| `interface https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBufferRenderer.RenderCallback` Callbacks invoked to render content leveraging a `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBufferRenderer` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBufferRenderer#FrameBufferRenderer(androidx.graphics.opengl.FrameBufferRenderer.RenderCallback,androidx.graphics.opengl.SyncStrategy)( frameBufferRendererCallbacks: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBufferRenderer.RenderCallback, syncStrategy: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/SyncStrategy )` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBufferRenderer#clear()()` |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBufferRenderer#onDrawFrame(androidx.graphics.opengl.egl.EGLManager)(eglManager: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/egl/EGLManager)` Callback used to issue OpenGL drawing commands into the `https://developer.android.com/reference/android/opengl/EGLSurface.html` created in `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer.RenderCallback#onSurfaceCreated(androidx.graphics.opengl.egl.EGLSpec,android.opengl.EGLConfig,android.view.Surface,kotlin.Int,kotlin.Int)`. |
| `open https://developer.android.com/reference/android/opengl/EGLSurface.html?` | `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBufferRenderer#onSurfaceCreated(androidx.graphics.opengl.egl.EGLSpec,android.opengl.EGLConfig,android.view.Surface,kotlin.Int,kotlin.Int)( spec: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/egl/EGLSpec, config: https://developer.android.com/reference/android/opengl/EGLConfig.html, surface: https://developer.android.com/reference/android/view/Surface.html, width: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, height: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Used to create a corresponding `https://developer.android.com/reference/android/opengl/EGLSurface.html` from the provided `https://developer.android.com/reference/android/view/Surface.html` instance. |

## Public constructors

### FrameBufferRenderer

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

```
FrameBufferRenderer(
    frameBufferRendererCallbacks: FrameBufferRenderer.RenderCallback,
    syncStrategy: SyncStrategy = SyncStrategy.ALWAYS
)
```

| Parameters |
|---|---|
| `frameBufferRendererCallbacks: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBufferRenderer.RenderCallback` | Callbacks to provide a `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBuffer` instance to render into, draw method to render into the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBuffer` as well as a subsequent callback to consume the contents of the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBuffer` |
| `syncStrategy: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/SyncStrategy = SyncStrategy.ALWAYS` | `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/SyncStrategy` used to determine when a fence is to be created to gate on consumption of the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBuffer` instance. This determines if a `https://developer.android.com/reference/kotlin/androidx/hardware/SyncFenceCompat` instance is provided in the `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/FrameBufferRenderer.RenderCallback#onDrawComplete(androidx.graphics.opengl.FrameBuffer,androidx.hardware.SyncFenceCompat)` depending on the use case. For example for front buffered rendering scenarios, it is possible that no `https://developer.android.com/reference/kotlin/androidx/hardware/SyncFenceCompat` is provided in order to reduce latency within the rendering pipeline. This API can be used to render content into a `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` directly and convert that to a bitmap with the following code snippet: ```kotlin val glRenderer = GLRenderer().apply { start() } val callbacks = object : FrameBufferRenderer.RenderCallback { override fun obtainFrameBuffer(egl: EGLSpec): FrameBuffer = FrameBuffer( egl, HardwareBuffer.create( width, height, HardwareBuffer.RGBA_8888, 1, HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE ) ) override fun onDraw(eglManager: EGLManager) { // GL code } override fun onDrawComplete(frameBuffer: FrameBuffer, syncFenceCompat: SyncFenceCompat?) { syncFenceCompat?.awaitForever() val bitmap = Bitmap.wrapHardwareBuffer(frameBuffer.hardwareBuffer, ColorSpace.get(ColorSpace.Named.LINEAR_SRGB)) // bitmap operations } } glRenderer.createRenderTarget(width,height, FrameBufferRenderer(callbacks)).requestRender() ``` |

## Public functions

### clear

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

```
fun clear(): Unit
```

### onDrawFrame

```
open fun onDrawFrame(eglManager: EGLManager): Unit
```

Callback used to issue OpenGL drawing commands into the `https://developer.android.com/reference/android/opengl/EGLSurface.html` created in `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer.RenderCallback#onSurfaceCreated(androidx.graphics.opengl.egl.EGLSpec,android.opengl.EGLConfig,android.view.Surface,kotlin.Int,kotlin.Int)`. This `https://developer.android.com/reference/android/opengl/EGLSurface.html` is guaranteed to be current before this callback is invoked and `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/egl/EGLManager#swapAndFlushBuffers()` will be invoked afterwards. If additional scratch `https://developer.android.com/reference/android/opengl/EGLSurface.html`s are used here it is up to the implementation of this method to ensure that the proper surfaces are made current and the appropriate swap buffers call is made

This method is invoked on the backing thread

| Parameters |
|---|---|
| `eglManager: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/egl/EGLManager` | Handle to EGL dependencies |

### onSurfaceCreated

```
open fun onSurfaceCreated(
    spec: EGLSpec,
    config: EGLConfig,
    surface: Surface,
    width: Int,
    height: Int
): EGLSurface?
```

Used to create a corresponding `https://developer.android.com/reference/android/opengl/EGLSurface.html` from the provided `https://developer.android.com/reference/android/view/Surface.html` instance. This enables consumers to configure the corresponding `https://developer.android.com/reference/android/opengl/EGLSurface.html` they wish to render into. The `https://developer.android.com/reference/android/opengl/EGLSurface.html` created here is guaranteed to be the current surface before `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer.RenderCallback#onDrawFrame(androidx.graphics.opengl.egl.EGLManager)` is called. That is, implementations of onDrawFrame do not need to call eglMakeCurrent on this `https://developer.android.com/reference/android/opengl/EGLSurface.html`.

This method is invoked on the GL thread.

The default implementation will create a window surface with EGL_WIDTH and EGL_HEIGHT set to `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer.RenderCallback#onSurfaceCreated(androidx.graphics.opengl.egl.EGLSpec,android.opengl.EGLConfig,android.view.Surface,kotlin.Int,kotlin.Int)` and `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/GLRenderer.RenderCallback#onSurfaceCreated(androidx.graphics.opengl.egl.EGLSpec,android.opengl.EGLConfig,android.view.Surface,kotlin.Int,kotlin.Int)` respectively. Implementations can override this method to provide additional `https://developer.android.com/reference/kotlin/androidx/graphics/opengl/egl/EGLConfigAttributes` for this surface (ex. `https://developer.android.com/reference/android/opengl/EGL14.html#EGL_SINGLE_BUFFER`.

Implementations can return null to indicate the default surface should be used. This is helpful in situations where content is to be rendered within a frame buffer object instead of to an `https://developer.android.com/reference/android/opengl/EGLSurface.html`

| Parameters |
|---|---|
| `spec: https://developer.android.com/reference/kotlin/androidx/graphics/opengl/egl/EGLSpec` | EGLSpec used to create the corresponding EGLSurface |
| `config: https://developer.android.com/reference/android/opengl/EGLConfig.html` | EGLConfig used to create the corresponding EGLSurface |
| `surface: https://developer.android.com/reference/android/view/Surface.html` | `https://developer.android.com/reference/android/view/Surface.html` used to create an EGLSurface from |
| `width: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Desired width of the surface to create |
| `height: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Desired height of the surface to create |