open class FakeImage : MutableImageWrapper


Fake implementation of MutableImageWrapper for testing.

This class supports optional injection of both a ByteBuffer and a HardwareBuffer.

HardwareBuffer & Backing Behavior

If a hardwareBuffer is provided, its width, height, and format metadata will be verified and accessible. However, accessing pixel data directly from within the HardwareBuffer is not supported on pure JVM. Accessing the buffer fields from imagePlanes will use data backed by the byteBuffer parameter and/or byteBuffer field on this image.

If provided, the HardwareBuffer will be closed when this FakeImage is closed.

Summary

Public constructors

FakeImage(
    width: Int,
    height: Int,
    format: Int,
    timestamp: Long,
    byteBuffer: ByteBuffer?,
    hardwareBuffer: HardwareBuffer?,
    cropRect: Rect,
    imagePlanes: List<ImagePlane>
)

Public functions

open Unit

Closes the image.

open String
open T?
<T : Any> unwrapAs(type: Class<T>)

Unwraps this fake image to retrieve the underlying type.

Public properties

Int

The number of times close has been called on this image.

open Rect

The crop rectangle of the image.

open Int

The dataspace associated with this image.

open Int

The format of the image.

open HardwareBuffer?

The hardware buffer for this image.

open Int

The height of the image in pixels.

open List<ImagePlane>

The image planes for this image.

open Boolean

Returns true if this image has been closed, false otherwise.

open SyncFence?

The sync fence associated with this image, or null if there is no fence.

open Long

The timestamp associated with this image, in nanoseconds.

open Int

The width of the image in pixels.

Public constructors

FakeImage

FakeImage(
    width: Int,
    height: Int,
    format: Int,
    timestamp: Long,
    byteBuffer: ByteBuffer? = null,
    hardwareBuffer: HardwareBuffer? = null,
    cropRect: Rect = Rect(0, 0, width, height),
    imagePlanes: List<ImagePlane> = emptyList()
)

Public functions

close

Added in 1.7.0-alpha03
open fun close(): Unit

Closes the image.

Increments closeCount. If this is the first call to close, it will also close the underlying HardwareBuffer if it was provided or created.

toString

open fun toString(): String

unwrapAs

Added in 1.7.0-alpha03
open fun <T : Any> unwrapAs(type: Class<T>): T?

Unwraps this fake image to retrieve the underlying type.

Supports unwrapping to:

  • The FakeImage class itself (or subclasses).

  • ByteBuffer, which returns the backing buffer.

  • HardwareBuffer, which returns the associated hardware buffer (on API level 26+).

Parameters
type: Class<T>

The class of the object to return.

Returns
T?

The unwrapped object, or null if the type is not supported.

Public properties

closeCount

Added in 1.7.0-alpha03
val closeCountInt

The number of times close has been called on this image.

Useful for verifying lifecycle management in tests.

cropRect

Added in 1.7.0-alpha03
open var cropRectRect

The crop rectangle of the image.

See also
setCropRect

dataSpace

open var dataSpaceInt

The dataspace associated with this image.

Defaults to android.hardware.DataSpace.DATASPACE_UNKNOWN.

format

Added in 1.7.0-alpha03
open var formatInt

The format of the image.

The value returned is one of the constants defined in ImageFormat.

hardwareBuffer

open val hardwareBufferHardwareBuffer?

The hardware buffer for this image.

If a HardwareBuffer was provided in the constructor, that buffer is returned. Otherwise, a mock HardwareBuffer is generated dynamically based on the image format, width, and height.

Throws IllegalStateException if this image is already closed.

height

Added in 1.7.0-alpha03
open var heightInt

The height of the image in pixels.

See also
getHeight

imagePlanes

Added in 1.7.0-alpha03
open var imagePlanesList<ImagePlane>

The image planes for this image.

If no image planes were provided in the constructor, they are generated automatically based on the image format and the backing ByteBuffer.

Supported formats for automatic plane generation:

Throws IllegalStateException if this image is already closed. Throws UnsupportedOperationException if automatic generation is attempted on an unsupported format.

isClosed

Added in 1.7.0-alpha03
open val isClosedBoolean

Returns true if this image has been closed, false otherwise.

syncFence

open var syncFenceSyncFence?

The sync fence associated with this image, or null if there is no fence.

See also
setFence

timestamp

Added in 1.7.0-alpha03
open var timestampLong

The timestamp associated with this image, in nanoseconds.

See also
setTimestamp

width

Added in 1.7.0-alpha03
open var widthInt

The width of the image in pixels.

See also
getWidth