AHardwareBuffer

AHardwareBuffer is a type of shared memory that wraps a gralloc buffer. In Android 10, the Neural Networks API (NNAPI) supports using AHardwareBuffer, allowing the driver to perform executions without copying data, which improves performance and power consumption for apps. For example, a camera HAL stack can pass AHardwareBuffer objects to the NNAPI for machine learning workloads using AHardwareBuffer handles generated by camera NDK and media NDK APIs. For more information, see ANeuralNetworksMemory_createFromAHardwareBuffer.

AHardwareBuffer objects used in NNAPI are passed to the driver through a hidl_memory struct named either hardware_buffer or hardware_buffer_blob. The hidl_memory struct hardware_buffer_blob only represents AHardwareBuffer objects with the AHARDWAREBUFFER_FORMAT_BLOB format.

The information required by the framework is encoded in the hidl_handle field of the hidl_memory struct. The hidl_handle field wraps native_handle, which encodes all of the required metadata about AHardwareBuffer or Gralloc buffer.

The driver must properly decode the provided hidl_handle field and access the memory described by hidl_handle. When the getSupportedOperations_1_2, getSupportedOperations_1_1, or getSupportedOperations method is called, the driver should detect whether it can decode the provided hidl_handle and access the memory described by hidl_handle. The model preparation must fail if the hidl_handle field used for a constant operand isn't supported. The execution must fail if the hidl_handle field used for an input or output operand of the execution isn't supported. It's recommended for the driver to return a GENERAL_FAILURE error code if the model preparation or execution fails.