HardwareBufferPool


@ExperimentalApi
@RequiresApi(value = 26)
class HardwareBufferPool


A pool of HardwareBuffers.

This class manages a fixed-capacity pool of HardwareBuffers that can be dequeued with a specific Format and usage flags. Dequeued buffers should be returned to the pool using recycle for reuse.

Methods can be called from any thread.

Summary

Nested types

Wrapper around a HardwareBuffer and an acquireFence.

Public constructors

Creates a new instance.

Public functions

HardwareBufferPool.HardwareBufferWithFence?
get(format: Format!, @Frame.Usage usageFlags: Long, wakeupListener: Runnable!)

Attempts to fetch a HardwareBufferWithFence matching the specified format and usageFlags.

Unit

Returns a HardwareBuffer to the pool.

Unit

Releases the pool and all its buffers.

Public constructors

HardwareBufferPool

HardwareBufferPool(capacity: Int)

Creates a new instance.

Parameters
capacity: Int

The maximum number of buffers allowed to exist (pooled + in-flight).

Public functions

get

fun get(format: Format!, @Frame.Usage usageFlags: Long, wakeupListener: Runnable!): HardwareBufferPool.HardwareBufferWithFence?

Attempts to fetch a HardwareBufferWithFence matching the specified format and usageFlags.

If the pool has reached its capacity and no buffers are available for reuse, this method returns null. In this case, the wakeupListener will be invoked when a buffer is eventually returned to the pool.

If this method is called multiple times without returning a buffer, only the most recent wakeupListener is guaranteed to be invoked.

Parameters
format: Format!

The required Format for the dequeued buffer.

@Frame.Usage usageFlags: Long

The requested usage flags for the buffer.

wakeupListener: Runnable!

A callback to notify the caller when a buffer becomes available.

Returns
HardwareBufferPool.HardwareBufferWithFence?

A HardwareBufferWithFence, or null if the pool is currently full.

recycle

fun recycle(buffer: HardwareBuffer!, fence: SyncFenceWrapper?): Unit

Returns a HardwareBuffer to the pool.

Parameters
buffer: HardwareBuffer!

The HardwareBuffer to return.

fence: SyncFenceWrapper?

An optional SyncFenceWrapper that must be reached before the buffer can be reused.

Throws
java.lang.IllegalArgumentException

if the buffer is closed.

release

fun release(): Unit

Releases the pool and all its buffers.

This should be called when the pool is no longer needed to ensure all resources are cleaned up.