# 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)
public final class FrameBufferRenderer implements GLRenderer.RenderCallback
```

<br />

*** ** * ** ***

`https://developer.android.com/reference/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 |
|---|
| `public interface https://developer.android.com/reference/androidx/graphics/opengl/FrameBufferRenderer.RenderCallback` Callbacks invoked to render content leveraging a `https://developer.android.com/reference/androidx/graphics/opengl/FrameBufferRenderer` |

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

| ### Public methods |
|---|---|
| `final void` | `https://developer.android.com/reference/androidx/graphics/opengl/FrameBufferRenderer#clear()()` |
| `void` | `https://developer.android.com/reference/androidx/graphics/opengl/FrameBufferRenderer#onDrawFrame(androidx.graphics.opengl.egl.EGLManager)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/opengl/egl/EGLManager 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/androidx/graphics/opengl/GLRenderer.RenderCallback#onSurfaceCreated(androidx.graphics.opengl.egl.EGLSpec,android.opengl.EGLConfig,android.view.Surface,kotlin.Int,kotlin.Int)`. |
| `https://developer.android.com/reference/android/opengl/EGLSurface.html` | `https://developer.android.com/reference/androidx/graphics/opengl/FrameBufferRenderer#onSurfaceCreated(androidx.graphics.opengl.egl.EGLSpec,android.opengl.EGLConfig,android.view.Surface,kotlin.Int,kotlin.Int)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/opengl/egl/EGLSpec spec, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/opengl/EGLConfig.html config, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/Surface.html surface, int width, int height )` 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)

```
public FrameBufferRenderer(
    @NonNull FrameBufferRenderer.RenderCallback frameBufferRendererCallbacks,
    @NonNull SyncStrategy syncStrategy
)
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/opengl/FrameBufferRenderer.RenderCallback frameBufferRendererCallbacks` | Callbacks to provide a `https://developer.android.com/reference/androidx/graphics/opengl/FrameBuffer` instance to render into, draw method to render into the `https://developer.android.com/reference/androidx/graphics/opengl/FrameBuffer` as well as a subsequent callback to consume the contents of the `https://developer.android.com/reference/androidx/graphics/opengl/FrameBuffer` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/opengl/SyncStrategy syncStrategy` | `https://developer.android.com/reference/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/androidx/graphics/opengl/FrameBuffer` instance. This determines if a `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat` instance is provided in the `https://developer.android.com/reference/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/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: ``` 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 methods

### clear

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

```
public final void clear()
```

### onDrawFrame

```
public void onDrawFrame(@NonNull EGLManager 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/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/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/opengl/egl/EGLManager eglManager` | Handle to EGL dependencies |

### onSurfaceCreated

```
public EGLSurface onSurfaceCreated(
    @NonNull EGLSpec spec,
    @NonNull EGLConfig config,
    @NonNull Surface surface,
    int width,
    int height
)
```

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/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/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/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/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/opengl/egl/EGLSpec spec` | EGLSpec used to create the corresponding EGLSurface |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/opengl/EGLConfig.html config` | EGLConfig used to create the corresponding EGLSurface |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/Surface.html surface` | `https://developer.android.com/reference/android/view/Surface.html` used to create an EGLSurface from |
| `int width` | Desired width of the surface to create |
| `int height` | Desired height of the surface to create |