# CanvasFrontBufferedRenderer

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

*** ** * ** ***


```
@RequiresApi(value = 29)
public final class CanvasFrontBufferedRenderer<T extends Object>
```

<br />

*** ** * ** ***

Class responsible for supporting a "front buffered" rendering system. This allows for lower latency graphics by leveraging a combination of front buffered and multi buffered content layers. Active content is rendered first into the front buffered layer which is simultaneously being presented to the display. Periodically content is rendered into the multi buffered layer which will have more traditional latency guarantees, however, minimizes the impact of visual artifacts due to graphical tearing.

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback<T extends https://developer.android.com/reference/java/lang/Object.html>` Provides callbacks for consumers to draw into the front and multi buffered layers as well as provide opportunities to synchronize `https://developer.android.com/reference/androidx/graphics/surface/SurfaceControlCompat.Transaction`s to submit the layers to the hardware compositor. |

| ### Public constructors |
|---|
| `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#CanvasFrontBufferedRenderer(android.view.SurfaceView,androidx.graphics.lowlatency.CanvasFrontBufferedRenderer.Callback,kotlin.Int)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/SurfaceView.html surfaceView, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback<@https://developer.android.com/reference/androidx/annotation/NonNull T> callback, int bufferFormat )` |

| ### Public methods |
|---|---|
| `final void` | `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#cancel()()` Requests to cancel rendering and hides the front buffered layer. |
| `final void` | `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#clear()()` Clears the contents of both the front and multi buffered layers. |
| `final void` | `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#commit()()` Requests to render the entire scene to the multi buffered layer and schedules a call to `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback#onDrawMultiBufferedLayer(android.graphics.Canvas,kotlin.Int,kotlin.Int,kotlin.collections.Collection)`. |
| `final void` | `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#execute(java.lang.Runnable)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Runnable.html runnable)` Queue a `https://developer.android.com/reference/java/lang/Runnable.html` to be executed on the internal rendering thread. |
| `final int` | `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#getBufferFormat()()` format of the underlying buffers being rendered into by `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/ColorSpace.html` | `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#getColorSpace()()` Configures the `https://developer.android.com/reference/android/graphics/ColorSpace.html` that the content should be rendered with for the front and multi buffered layers. |
| `final boolean` | `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#isValid()()` Determines whether or not the `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer` is in a valid state. |
| `final void` | `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#release(kotlin.Boolean,kotlin.Function0)(boolean cancelPending, Function0<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onReleaseComplete)` Releases the `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer`. |
| `final void` | `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#renderFrontBufferedLayer(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull T param)` Render content to the front buffered layer providing optional parameters to be consumed in `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback#onDrawFrontBufferedLayer(android.graphics.Canvas,kotlin.Int,kotlin.Int,kotlin.Any)`. |
| `final void` | `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#renderMultiBufferedLayer(kotlin.collections.Collection)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> params)` Requests to render to the multi buffered layer. |
| `final void` | `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#setColorSpace(android.graphics.ColorSpace)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/ColorSpace.html value)` Configures the `https://developer.android.com/reference/android/graphics/ColorSpace.html` that the content should be rendered with for the front and multi buffered layers. |

## Public constructors

### CanvasFrontBufferedRenderer

```
public <T extends Object> CanvasFrontBufferedRenderer(
    @NonNull SurfaceView surfaceView,
    @NonNull CanvasFrontBufferedRenderer.Callback<@NonNull T> callback,
    int bufferFormat
)
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/SurfaceView.html surfaceView` | Target SurfaceView to act as the parent rendering layer for multi buffered content |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback<@https://developer.android.com/reference/androidx/annotation/NonNull T> callback` | Callbacks used to render into front and multi buffered layers as well as configuring `https://developer.android.com/reference/androidx/graphics/surface/SurfaceControlCompat.Transaction`s for controlling these layers in addition to other `https://developer.android.com/reference/androidx/graphics/surface/SurfaceControlCompat` instances that must be updated atomically within the user interface. These callbacks are invoked on an internal rendering thread. The templated type here is consumer defined to represent the data structures to be consumed for rendering within `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback#onDrawFrontBufferedLayer(android.graphics.Canvas,kotlin.Int,kotlin.Int,kotlin.Any)` and `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback#onDrawMultiBufferedLayer(android.graphics.Canvas,kotlin.Int,kotlin.Int,kotlin.collections.Collection)` and are provided by the `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#renderFrontBufferedLayer(kotlin.Any)` and `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#renderMultiBufferedLayer(kotlin.collections.Collection)` methods. |
| `int bufferFormat` | format of the underlying buffers being rendered into by `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer`. The particular valid combinations for a given Android version and implementation should be documented by that version. `https://developer.android.com/reference/android/hardware/HardwareBuffer.html#RGBA_8888` and `https://developer.android.com/reference/android/hardware/HardwareBuffer.html#RGBX_8888` are guaranteed to be supported. However, consumers are recommended to query the desired HardwareBuffer configuration using `https://developer.android.com/reference/android/hardware/HardwareBuffer.html#isSupported(int, int, int, int, long)`. The default is `https://developer.android.com/reference/android/hardware/HardwareBuffer.html#RGBA_8888`. Value is one of the following: - `1` - `22` - `43` - `2` - `3` - `4` - `33` - `35` - `48` - `49` - `50` - `51` - `52` - `53` - `54` <br /> |

## Public methods

### cancel

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

```
public final void cancel()
```

Requests to cancel rendering and hides the front buffered layer. Unlike `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#commit()`, this does not schedule a call to render into the multi buffered layer. This is useful in palm rejection use cases, where some initial touch events might be processed before a corresponding cancel event is received indicating the touch gesture is coming from a palm rather than intentional user input. In the case where MotionEvent#getAction returns ACTION_CANCEL, this is to be invoked.

If this `https://developer.android.com/reference/androidx/graphics/lowlatency/GLFrontBufferedRenderer` has been released, that is `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#isValid()` returns `false`, this call is ignored.

### clear

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

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

Clears the contents of both the front and multi buffered layers. This triggers a call to `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback#onMultiBufferedLayerRenderComplete(androidx.graphics.surface.SurfaceControlCompat,androidx.graphics.surface.SurfaceControlCompat,androidx.graphics.surface.SurfaceControlCompat.Transaction)` and hides the front buffered layer.

### commit

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

```
public final void commit()
```

Requests to render the entire scene to the multi buffered layer and schedules a call to `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback#onDrawMultiBufferedLayer(android.graphics.Canvas,kotlin.Int,kotlin.Int,kotlin.collections.Collection)`. The parameters provided to `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback#onDrawMultiBufferedLayer(android.graphics.Canvas,kotlin.Int,kotlin.Int,kotlin.collections.Collection)` will include each argument provided to every `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#renderFrontBufferedLayer(kotlin.Any)` call since the last call to `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#commit()` has been made. When rendering to the multi-buffered layer is complete, this synchronously hides the front buffer and updates the multi buffered layer.

If this `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer` has been released, that is `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#isValid()` returns `false`, this call is ignored.

### execute

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

```
public final void execute(@NonNull Runnable runnable)
```

Queue a `https://developer.android.com/reference/java/lang/Runnable.html` to be executed on the internal rendering thread. Note it is important this `https://developer.android.com/reference/java/lang/Runnable.html` does not block otherwise it can stall the rendering thread.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Runnable.html runnable` | to be executed |

### getBufferFormat

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

```
public final int getBufferFormat()
```

format of the underlying buffers being rendered into by `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer`. The particular valid combinations for a given Android version and implementation should be documented by that version. `https://developer.android.com/reference/android/hardware/HardwareBuffer.html#RGBA_8888` and `https://developer.android.com/reference/android/hardware/HardwareBuffer.html#RGBX_8888` are guaranteed to be supported. However, consumers are recommended to query the desired HardwareBuffer configuration using `https://developer.android.com/reference/android/hardware/HardwareBuffer.html#isSupported(int, int, int, int, long)`. The default is `https://developer.android.com/reference/android/hardware/HardwareBuffer.html#RGBA_8888`.

### getColorSpace

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

```
public final @NonNull ColorSpace getColorSpace()
```

Configures the `https://developer.android.com/reference/android/graphics/ColorSpace.html` that the content should be rendered with for the front and multi buffered layers. This parameter is only consumed on Android U and above. For older API levels this is ignored.

### isValid

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

```
public final boolean isValid()
```

Determines whether or not the `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer` is in a valid state. That is the `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#release(kotlin.Boolean,kotlin.Function0)` method has not been called. If this returns false, then subsequent calls to `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#renderFrontBufferedLayer(kotlin.Any)`, `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#renderMultiBufferedLayer(kotlin.collections.Collection)`, `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#commit()`, and `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#release(kotlin.Boolean,kotlin.Function0)` are ignored

| Returns |
|---|---|
| `boolean` | `true` if this `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer` has been released, `false` otherwise |

### release

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

```
public final void release(boolean cancelPending, Function0<Unit> onReleaseComplete)
```

Releases the `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer`. In process requests are ignored. If the `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer` is already released, that is `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#isValid()` returns `false`, this method does nothing.

### renderFrontBufferedLayer

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

```
public final void renderFrontBufferedLayer(@NonNull T param)
```

Render content to the front buffered layer providing optional parameters to be consumed in `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback#onDrawFrontBufferedLayer(android.graphics.Canvas,kotlin.Int,kotlin.Int,kotlin.Any)`. Additionally the parameter provided here will also be consumed in `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback#onDrawMultiBufferedLayer(android.graphics.Canvas,kotlin.Int,kotlin.Int,kotlin.collections.Collection)` when the corresponding `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#commit()` method is invoked, which will include all `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#renderFrontBufferedLayer(kotlin.Any)`s in each call made to this method up to the corresponding `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#commit()` call.

If this `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer` has been released, that is `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#isValid()` returns `false`, this call is ignored.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T param` | Optional parameter to be consumed when rendering content into the commit layer |

### renderMultiBufferedLayer

```
public final void renderMultiBufferedLayer(@NonNull Collection<@NonNull T> params)
```

Requests to render to the multi buffered layer. This schedules a call to `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback#onDrawMultiBufferedLayer(android.graphics.Canvas,kotlin.Int,kotlin.Int,kotlin.collections.Collection)` with the parameters provided. If the front buffered layer is visible, this will hide this layer after rendering to the multi buffered layer is complete. This is equivalent to calling `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#renderFrontBufferedLayer(kotlin.Any)` for each parameter provided in the collection followed by a single call to `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#commit()`. This is useful for re-rendering the multi buffered scene when the corresponding Activity is being resumed from the background in which the contents should be re-drawn. Additionally this allows for applications to decide to dynamically render to either front or multi buffered layers.

If this `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer` has been released, that is `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer#isValid()` returns 'false', this call is ignored.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> params` | Parameters that to be consumed when rendering to the multi buffered layer. These parameters will be provided in the corresponding call to `https://developer.android.com/reference/androidx/graphics/lowlatency/CanvasFrontBufferedRenderer.Callback#onDrawMultiBufferedLayer(android.graphics.Canvas,kotlin.Int,kotlin.Int,kotlin.collections.Collection)` |

### setColorSpace

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

```
public final void setColorSpace(@NonNull ColorSpace value)
```

Configures the `https://developer.android.com/reference/android/graphics/ColorSpace.html` that the content should be rendered with for the front and multi buffered layers. This parameter is only consumed on Android U and above. For older API levels this is ignored.