# HardwareBufferRenderer

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Nested Classes](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#nestedclasses)\|[Ctors](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#pubctors)\|[Methods](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#inhmethods)  

# HardwareBufferRenderer

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/graphics/HardwareBufferRenderer "View this page in Kotlin")\|Java

`
public


class
HardwareBufferRenderer
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


implements

`[AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable)`


`

|---|-----------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.graphics.HardwareBufferRenderer |

<br />

*** ** * ** ***

<br />

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

### Resources \& lifecycle

All HardwareBufferRenderer and[HardwareRenderer](https://developer.android.com/reference/android/graphics/HardwareRenderer)instances share a common render thread. Therefore HardwareBufferRenderer 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 HardwareBufferRenderer created comes with the cost of also creating the associated GPU contexts, however each incremental HardwareBufferRenderer thereafter is fairly cheap. The expected usage is to have a HardwareBufferRenderer instance for every active[HardwareBuffer](https://developer.android.com/reference/android/hardware/HardwareBuffer).
This is useful in situations where a scene built with[RenderNode](https://developer.android.com/reference/android/graphics/RenderNode)s can be consumed directly by the system compositor through[SurfaceControl.Transaction.setBuffer(SurfaceControl, HardwareBuffer)](https://developer.android.com/reference/android/view/SurfaceControl.Transaction#setBuffer(android.view.SurfaceControl,%20android.hardware.HardwareBuffer)). HardwareBufferRenderer will never clear contents before each draw invocation so previous contents in the[HardwareBuffer](https://developer.android.com/reference/android/hardware/HardwareBuffer)target will be preserved across renders.

<br />

## Summary

|                                                                                                                                                        ### Nested classes                                                                                                                                                        ||
|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` class` | [HardwareBufferRenderer.RenderRequest](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer.RenderRequest) Sets the parameters that can be used to control a render request for a[HardwareBufferRenderer](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer). |
| ` class` | [HardwareBufferRenderer.RenderResult](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer.RenderResult) Class that contains data regarding the result of the render request.                                                                                                               |

|                                                                                                                                                                                                                                                                     ### Public constructors                                                                                                                                                                                                                                                                     ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[HardwareBufferRenderer](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#HardwareBufferRenderer(android.hardware.HardwareBuffer))`(`[HardwareBuffer](https://developer.android.com/reference/android/hardware/HardwareBuffer)` buffer) ` Creates a new instance of[HardwareBufferRenderer](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer)with the provided[HardwareBuffer](https://developer.android.com/reference/android/hardware/HardwareBuffer)as the output of the rendered scene. |

|                                                                                                                                                                                                                                                              ### Public methods                                                                                                                                                                                                                                                              ||
|------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                                                                                                  | ` `[close](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#close())`() ` Releases the resources associated with this[HardwareBufferRenderer](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer)instance.                                                                                                                             |
| ` boolean`                                                                                                                               | ` `[isClosed](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#isClosed())`() ` Returns if the[HardwareBufferRenderer](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer)has already been closed.                                                                                                                                     |
| ` `[HardwareBufferRenderer.RenderRequest](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer.RenderRequest) | ` `[obtainRenderRequest](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#obtainRenderRequest())`() ` Returns a[RenderRequest](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer.RenderRequest)that can be used to render into the provided[HardwareBuffer](https://developer.android.com/reference/android/hardware/HardwareBuffer). |
| ` void`                                                                                                                                  | ` `[setContentRoot](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#setContentRoot(android.graphics.RenderNode))`(`[RenderNode](https://developer.android.com/reference/android/graphics/RenderNode)` content) ` Sets the content root to render.                                                                                                                  |
| ` void`                                                                                                                                  | ` `[setLightSourceAlpha](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#setLightSourceAlpha(float,%20float))`(float ambientShadowAlpha, float spotShadowAlpha) ` Configures the ambient \& spot shadow alphas.                                                                                                                                                    |
| ` void`                                                                                                                                  | ` `[setLightSourceGeometry](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#setLightSourceGeometry(float,%20float,%20float,%20float))`(float lightX, float lightY, float lightZ, float lightRadius) ` Sets the center of the light source.                                                                                                                         |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||
| From interface` `[java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable)` ` |------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/lang/AutoCloseable#close())`() ` Closes this resource, relinquishing any underlying resources. | ||

## Public constructors

### HardwareBufferRenderer

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public HardwareBufferRenderer (HardwareBuffer buffer)
```

Creates a new instance of[HardwareBufferRenderer](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer)with the provided[HardwareBuffer](https://developer.android.com/reference/android/hardware/HardwareBuffer)as the output of the rendered scene.

<br />

|                           Parameters                           ||
|----------|------------------------------------------------------|
| `buffer` | `HardwareBuffer`: This value cannot be`null`. <br /> |

## Public methods

### close

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void close ()
```

Releases the resources associated with this[HardwareBufferRenderer](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer)instance. \*\*Note\*\* this does not call[HardwareBuffer.close()](https://developer.android.com/reference/android/hardware/HardwareBuffer#close())on the provided[HardwareBuffer](https://developer.android.com/reference/android/hardware/HardwareBuffer)instance

<br />

### isClosed

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean isClosed ()
```

Returns if the[HardwareBufferRenderer](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer)has already been closed. That is[HardwareBufferRenderer.close()](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#close())has been invoked.

<br />

|                                                                                 Returns                                                                                 ||
|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `boolean` | True if the[HardwareBufferRenderer](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer)has been closed, false otherwise. <br /> |

### obtainRenderRequest

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public HardwareBufferRenderer.RenderRequest obtainRenderRequest ()
```

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

<br />

|                                                                                                                                                                                                              Returns                                                                                                                                                                                                               ||
|---------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [HardwareBufferRenderer.RenderRequest](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer.RenderRequest) | An instance of[RenderRequest](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer.RenderRequest). The instance may be reused for every frame, so the caller should not hold onto it for longer than a single render request. This value cannot be`null`. <br /> |

### setContentRoot

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setContentRoot (RenderNode content)
```

Sets the content root to render. It is not necessary to call this whenever the content recording changes. Any mutations to the RenderNode content, or any of the RenderNodes contained within the content node, will be applied whenever a new[RenderRequest](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer.RenderRequest)is issued via[obtainRenderRequest()](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#obtainRenderRequest())and[RenderRequest.draw(Executor,
Consumer)](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer.RenderRequest#draw(java.util.concurrent.Executor,%20java.util.function.Consumer<android.graphics.HardwareBufferRenderer.RenderResult>)).

<br />

|                                                                     Parameters                                                                     ||
|-----------|-----------------------------------------------------------------------------------------------------------------------------------------|
| `content` | `RenderNode`: The content to set as the root RenderNode. If null the content root is removed and the renderer will draw nothing. <br /> |

### setLightSourceAlpha

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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](https://developer.android.com/reference/android/R.attr#spotShadowAlpha)and[R.attr.ambientShadowAlpha](https://developer.android.com/reference/android/R.attr#ambientShadowAlpha).

This must be set at least once along with[setLightSourceGeometry(float, float, float, float)](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#setLightSourceGeometry(float,%20float,%20float,%20float))before shadows will work.

<br />

|                                                                         Parameters                                                                          ||
|----------------------|---------------------------------------------------------------------------------------------------------------------------------------|
| `ambientShadowAlpha` | `float`: The alpha for the ambient shadow. If unsure, a reasonable default is 0.039f. Value is between 0.0f and 1.0f inclusive <br /> |
| `spotShadowAlpha`    | `float`: The alpha for the spot shadow. If unsure, a reasonable default is 0.19f. Value is between 0.0f and 1.0f inclusive <br />     |

### setLightSourceGeometry

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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 RenderNode 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[setLightSourceAlpha(float, float)](https://developer.android.com/reference/android/graphics/HardwareBufferRenderer#setLightSourceAlpha(float,%20float))before shadows will work.

<br />

|                                                                                                     Parameters                                                                                                     ||
|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `lightX`      | `float`: The X position of the light source. If unsure, a reasonable default is 'displayWidth / 2f - windowLeft'. <br />                                                                            |
| `lightY`      | `float`: The Y position of the light source. If unsure, a reasonable default is '0 - windowTop' <br />                                                                                              |
| `lightZ`      | `float`: The Z position of the light source. Must be \>= 0. If unsure, a reasonable default is 600dp. Value is 0.0f or greater <br />                                                               |
| `lightRadius` | `float`: The radius of the light source. Smaller radius will have sharper edges, larger radius will have softer shadows. If unsure, a reasonable default is 800 dp. Value is 0.0f or greater <br /> |