# CanvasBufferedRenderer

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

*** ** * ** ***


```
@RequiresApi(value = 29)
public final class CanvasBufferedRenderer implements AutoCloseable
```

<br />

*** ** * ** ***

Creates an instance of a hardware-accelerated renderer. This is used to render a scene built from `https://developer.android.com/reference/android/graphics/RenderNode.html`s to an output `https://developer.android.com/reference/android/hardware/HardwareBuffer.html`. There can be as many `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` instances as desired.

Resources \& lifecycle

All `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` instances share a common render thread. Therefore `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` will share common resources and GPU utilization with hardware accelerated rendering initiated by the UI thread of an application. The render thread contains the GPU context \& resources necessary to do GPU-accelerated rendering. As such, the first `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` created comes with the cost of also creating the associated GPU contexts, however each incremental `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` thereafter is fairly cheap.

This is useful in situations where a scene built with `https://developer.android.com/reference/androidx/graphics/surface/SurfaceControlCompat.Transaction#setBuffer(androidx.graphics.surface.SurfaceControlCompat,android.hardware.HardwareBuffer,androidx.hardware.SyncFenceCompat,kotlin.Function1)`.

`https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` can optionally persist contents before each draw invocation so previous contents in the `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` target will be preserved across renders. This is determined by the argument provided to `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer.RenderRequest#preserveContents(kotlin.Boolean)` which is set to `false` by default.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer.Builder` Builder used to construct a `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` instance. |
| `public final inner class https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer.RenderRequest` Sets the parameters that can be used to control a render request for a `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer`. |
| `public final class https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer.RenderResult` Class that contains data regarding the result of the render request. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#close()()` Releases the resources associated with this `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` instance. |
| `final int` | `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#getBufferFormat()()` Returns the `https://developer.android.com/reference/androidx/hardware/HardwareBufferFormat` of the buffers that are being rendered into by this `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` |
| `final int` | `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#getMaxBuffers()()` Returns the number of buffers within the swap chain used for rendering with this `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` |
| `final long` | `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#getUsageFlags()()` Returns the current usage flag hints of the buffers that are being rendered into by this `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` |
| `final boolean` | `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#isClosed()()` Returns if the `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` has already been closed. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer.RenderRequest` | `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#obtainRenderRequest()()` Returns a `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer.RenderRequest` that can be used to render into the provided HardwareBuffer. |
| `final void` | `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#releaseBuffer(android.hardware.HardwareBuffer,androidx.hardware.SyncFenceCompat)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/hardware/HardwareBuffer.html hardwareBuffer, https://developer.android.com/reference/androidx/hardware/SyncFenceCompat fence )` Releases the `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` back into the allocation pool to be reused in subsequent renders. |
| `final void` | `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#setContentRoot(android.graphics.RenderNode)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/RenderNode.html renderNode)` Sets the content root to render. |
| `final void` | `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#setLightSourceAlpha(kotlin.Float,kotlin.Float)(float ambientShadowAlpha, float spotShadowAlpha)` Configures the ambient \& spot shadow alphas. |
| `final void` | `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#setLightSourceGeometry(kotlin.Float,kotlin.Float,kotlin.Float,kotlin.Float)( float lightX, float lightY, float lightZ, float lightRadius )` Sets the center of the light source. |

## Public methods

### close

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

```
public void close()
```

Releases the resources associated with this `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` instance. **Note** this does not call `https://developer.android.com/reference/android/hardware/HardwareBuffer.html#close()` on the provided `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` instance.

### getBufferFormat

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

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

Returns the `https://developer.android.com/reference/androidx/hardware/HardwareBufferFormat` of the buffers that are being rendered into by this `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer`

### getMaxBuffers

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

```
public final int getMaxBuffers()
```

Returns the number of buffers within the swap chain used for rendering with this `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer`

### getUsageFlags

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

```
public final long getUsageFlags()
```

Returns the current usage flag hints of the buffers that are being rendered into by this `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer`

### isClosed

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

```
public final boolean isClosed()
```

Returns if the `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` has already been closed. That is `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#close()` has been invoked.

### obtainRenderRequest

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

```
public final @NonNull CanvasBufferedRenderer.RenderRequest obtainRenderRequest()
```

Returns a `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer.RenderRequest` that can be used to render into the provided HardwareBuffer. This is used to synchronize the RenderNode content provided by `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#setContentRoot(android.graphics.RenderNode)`.

### releaseBuffer

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

```
public final void releaseBuffer(
    @NonNull HardwareBuffer hardwareBuffer,
    SyncFenceCompat fence
)
```

Releases the `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` back into the allocation pool to be reused in subsequent renders. The `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` instance released here must be one that was originally obtained from this `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer` instance. This method also takes in an optional `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat` instance that will be internally waited upon before re-using the buffer. This is useful in conjunction with `https://developer.android.com/reference/androidx/graphics/surface/SurfaceControlCompat.Transaction#setBuffer(androidx.graphics.surface.SurfaceControlCompat,android.hardware.HardwareBuffer,androidx.hardware.SyncFenceCompat,kotlin.Function1)` where the system will return a release fence that should be waited upon before the corresponding buffer can be re-used.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/hardware/HardwareBuffer.html hardwareBuffer` | `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` to return back to the allocation pool |
| `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat fence` | Optional `https://developer.android.com/reference/androidx/hardware/SyncFenceCompat` that should be waited upon before the buffer is reused. |

### setContentRoot

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

```
public final void setContentRoot(@NonNull RenderNode renderNode)
```

Sets the content root to render. It is not necessary to call this whenever the content recording changes. Any mutations to the `https://developer.android.com/reference/android/graphics/RenderNode.html` content, or any of the `https://developer.android.com/reference/android/graphics/RenderNode.html`s contained within the content node, will be applied whenever a new `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer.RenderRequest` is issued via `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#obtainRenderRequest()` and `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer.RenderRequest#drawAsync(java.util.concurrent.Executor,androidx.core.util.Consumer)`.

### setLightSourceAlpha

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

```
public final void setLightSourceAlpha(float ambientShadowAlpha, float spotShadowAlpha)
```

Configures the ambient \& spot shadow alphas. This is the alpha used when the shadow has max alpha, and ramps down from the values provided to zero.

These values are typically provided by the current theme, see R.attr.spotShadowAlpha and R.attr.ambientShadowAlpha.

This must be set at least once along with `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#setLightSourceGeometry(kotlin.Float,kotlin.Float,kotlin.Float,kotlin.Float)` before shadows will work.

### setLightSourceGeometry

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

```
public final void setLightSourceGeometry(
    float lightX,
    float lightY,
    float lightZ,
    float lightRadius
)
```

Sets the center of the light source. The light source point controls the directionality and shape of shadows rendered by `https://developer.android.com/reference/android/graphics/RenderNode.html` Z \& elevation.

The light source should be setup both as part of initial configuration, and whenever the window moves to ensure the light source stays anchored in display space instead of in window space.

This must be set at least once along with `https://developer.android.com/reference/androidx/graphics/CanvasBufferedRenderer#setLightSourceAlpha(kotlin.Float,kotlin.Float)` before shadows will work.