HardwareBufferPool


@ExperimentalApi
@RequiresApi(value = 26)
public final 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

HardwareBufferPool(int capacity)

Creates a new instance.

Public methods

@Nullable HardwareBufferPool.HardwareBufferWithFence
get(Format format, @Frame.Usage long usageFlags, Runnable wakeupListener)

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

void

Returns a HardwareBuffer to the pool.

void

Releases the pool and all its buffers.

Public constructors

HardwareBufferPool

public HardwareBufferPool(int capacity)

Creates a new instance.

Parameters
int capacity

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

Public methods

get

public @Nullable HardwareBufferPool.HardwareBufferWithFence get(Format format, @Frame.Usage long usageFlags, Runnable wakeupListener)

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 long usageFlags

The requested usage flags for the buffer.

Runnable wakeupListener

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

Returns
@Nullable HardwareBufferPool.HardwareBufferWithFence

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

recycle

public void recycle(HardwareBuffer buffer, @Nullable SyncFenceWrapper fence)

Returns a HardwareBuffer to the pool.

Parameters
HardwareBuffer buffer

The HardwareBuffer to return.

@Nullable SyncFenceWrapper fence

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

Throws
java.lang.IllegalArgumentException

if the buffer is closed.

release

public void release()

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.