Merge changes Iae9f692f,Ib43f1cb6,I9d4f0c30,I7d06ea35 am: 5f14bad22c am: b9aff55ad9 am: 6109e931d9 am: ebab2cdef5

Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/1863342

Change-Id: I149729d4573cd6b520f1d464285fc1420bc5158c
This commit is contained in:
Michael Butler
2021-10-27 01:24:58 +00:00
committed by Automerger Merge Worker
62 changed files with 392 additions and 422 deletions

View File

@@ -31,6 +31,7 @@ cc_library_static {
export_include_dirs: ["include"],
cflags: ["-Wthread-safety"],
static_libs: [
"libarect",
"neuralnetworks_types",
"neuralnetworks_utils_hal_common",
],
@@ -40,6 +41,11 @@ cc_library_static {
export_static_lib_headers: [
"neuralnetworks_utils_hal_common",
],
target: {
android: {
shared_libs: ["libnativewindow"],
},
},
}
cc_test {

View File

@@ -24,9 +24,10 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/TransferValue.h>
#include "nnapi/hal/1.0/ProtectCallback.h"
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
// lifetimes across processes and for protecting asynchronous calls across HIDL.

View File

@@ -36,6 +36,7 @@ GeneralResult<Operand> unvalidatedConvert(const hal::V1_0::Operand& operand);
GeneralResult<Operation> unvalidatedConvert(const hal::V1_0::Operation& operation);
GeneralResult<Model::OperandValues> unvalidatedConvert(
const hardware::hidl_vec<uint8_t>& operandValues);
GeneralResult<SharedHandle> unvalidatedConvert(const hardware::hidl_handle& handle);
GeneralResult<SharedMemory> unvalidatedConvert(const hardware::hidl_memory& memory);
GeneralResult<Model> unvalidatedConvert(const hal::V1_0::Model& model);
GeneralResult<Request::Argument> unvalidatedConvert(
@@ -65,6 +66,7 @@ nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand);
nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation);
nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert(
const nn::Model::OperandValues& operandValues);
nn::GeneralResult<hidl_handle> unvalidatedConvert(const nn::SharedHandle& handle);
nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::SharedMemory& memory);
nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model);
nn::GeneralResult<RequestArgument> unvalidatedConvert(const nn::Request::Argument& requestArgument);

View File

@@ -24,7 +24,8 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include "nnapi/hal/1.0/ProtectCallback.h"
#include <functional>
#include <memory>

View File

@@ -22,9 +22,9 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include "PreparedModel.h"
#include "nnapi/hal/1.0/ProtectCallback.h"
#include <memory>
#include <utility>

View File

@@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_HANDLE_ERROR_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_HANDLE_ERROR_H
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_HANDLE_ERROR_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_HANDLE_ERROR_H
#include <android/hidl/base/1.0/IBase.h>
#include <hidl/HidlSupport.h>
@@ -27,7 +27,7 @@
namespace android::hardware::neuralnetworks::utils {
template <typename Type>
nn::GeneralResult<Type> handleTransportError(const hardware::Return<Type>& ret) {
nn::GeneralResult<Type> handleTransportError(const Return<Type>& ret) {
if (ret.isDeadObject()) {
return nn::error(nn::ErrorStatus::DEAD_OBJECT)
<< "Return<>::isDeadObject returned true: " << ret.description();
@@ -52,13 +52,13 @@ nn::GeneralResult<Type> handleTransportError(const hardware::Return<Type>& ret)
std::move(result).value(); \
})
#define HANDLE_HAL_STATUS(status) \
if (const auto canonical = ::android::nn::convert(status).value_or( \
::android::nn::ErrorStatus::GENERAL_FAILURE); \
canonical == ::android::nn::ErrorStatus::NONE) { \
} else \
#define HANDLE_STATUS_HIDL(status) \
if (const ::android::nn::ErrorStatus canonical = ::android::nn::convert(status).value_or( \
::android::nn::ErrorStatus::GENERAL_FAILURE); \
canonical == ::android::nn::ErrorStatus::NONE) { \
} else \
return NN_ERROR(canonical)
} // namespace android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_HANDLE_ERROR_H
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_HANDLE_ERROR_H

View File

@@ -22,7 +22,8 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include "nnapi/hal/1.0/ProtectCallback.h"
#include <memory>
#include <tuple>

View File

@@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_PROTECT_CALLBACK_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_PROTECT_CALLBACK_H
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_PROTECT_CALLBACK_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_PROTECT_CALLBACK_H
#include <android-base/scopeguard.h>
#include <android-base/thread_annotations.h>
@@ -98,4 +98,4 @@ class DeathHandler final {
} // namespace android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_PROTECT_CALLBACK_H
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_PROTECT_CALLBACK_H

View File

@@ -25,7 +25,6 @@
#include <nnapi/TypeUtils.h>
#include <nnapi/Types.h>
#include <nnapi/Validation.h>
#include <nnapi/hal/HandleError.h>
namespace android::hardware::neuralnetworks::V1_0::utils {

View File

@@ -17,7 +17,9 @@
#include "Callbacks.h"
#include "Conversions.h"
#include "HandleError.h"
#include "PreparedModel.h"
#include "ProtectCallback.h"
#include "Utils.h"
#include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h>
@@ -27,8 +29,6 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/TransferValue.h>
#include <utility>
@@ -40,19 +40,19 @@ namespace android::hardware::neuralnetworks::V1_0::utils {
nn::GeneralResult<std::vector<bool>> supportedOperationsCallback(
ErrorStatus status, const hidl_vec<bool>& supportedOperations) {
HANDLE_HAL_STATUS(status) << "get supported operations failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "get supported operations failed with " << toString(status);
return supportedOperations;
}
nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback(
ErrorStatus status, const sp<IPreparedModel>& preparedModel) {
HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "model preparation failed with " << toString(status);
return NN_TRY(PreparedModel::create(preparedModel));
}
nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> executionCallback(
ErrorStatus status) {
HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "execution failed with " << toString(status);
return {};
}

View File

@@ -37,6 +37,11 @@
#include "Utils.h"
#ifdef __ANDROID__
#include <android/hardware_buffer.h>
#include <vndk/hardware_buffer.h>
#endif // __ANDROID__
namespace {
template <typename Type>
@@ -49,6 +54,7 @@ constexpr std::underlying_type_t<Type> underlyingType(Type value) {
namespace android::nn {
namespace {
using hardware::hidl_handle;
using hardware::hidl_memory;
using hardware::hidl_vec;
@@ -74,6 +80,121 @@ GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& halOb
return canonical;
}
nn::GeneralResult<nn::Memory::Unknown::Handle> unknownHandleFromNativeHandle(
const native_handle_t* handle) {
if (handle == nullptr) {
return NN_ERROR() << "unknownHandleFromNativeHandle failed because handle is nullptr";
}
std::vector<base::unique_fd> fds =
NN_TRY(nn::dupFds(handle->data + 0, handle->data + handle->numFds));
std::vector<int> ints(handle->data + handle->numFds,
handle->data + handle->numFds + handle->numInts);
return nn::Memory::Unknown::Handle{.fds = std::move(fds), .ints = std::move(ints)};
}
nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_memory& memory) {
CHECK_LE(memory.size(), std::numeric_limits<size_t>::max());
if (!memory.valid()) {
return NN_ERROR() << "Unable to convert invalid hidl_memory";
}
if (memory.name() == "ashmem") {
if (memory.handle()->numFds != 1) {
return NN_ERROR() << "Unable to convert invalid ashmem memory object with "
<< memory.handle()->numFds << " numFds, but expected 1";
}
if (memory.handle()->numInts != 0) {
return NN_ERROR() << "Unable to convert invalid ashmem memory object with "
<< memory.handle()->numInts << " numInts, but expected 0";
}
auto handle = nn::Memory::Ashmem{
.fd = NN_TRY(nn::dupFd(memory.handle()->data[0])),
.size = static_cast<size_t>(memory.size()),
};
return std::make_shared<const nn::Memory>(nn::Memory{.handle = std::move(handle)});
}
if (memory.name() == "mmap_fd") {
if (memory.handle()->numFds != 1) {
return NN_ERROR() << "Unable to convert invalid mmap_fd memory object with "
<< memory.handle()->numFds << " numFds, but expected 1";
}
if (memory.handle()->numInts != 3) {
return NN_ERROR() << "Unable to convert invalid mmap_fd memory object with "
<< memory.handle()->numInts << " numInts, but expected 3";
}
const int fd = memory.handle()->data[0];
const int prot = memory.handle()->data[1];
const int lower = memory.handle()->data[2];
const int higher = memory.handle()->data[3];
const size_t offset = nn::getOffsetFromInts(lower, higher);
return nn::createSharedMemoryFromFd(static_cast<size_t>(memory.size()), prot, fd, offset);
}
if (memory.name() != "hardware_buffer_blob") {
auto handle = nn::Memory::Unknown{
.handle = NN_TRY(unknownHandleFromNativeHandle(memory.handle())),
.size = static_cast<size_t>(memory.size()),
.name = memory.name(),
};
return std::make_shared<const nn::Memory>(nn::Memory{.handle = std::move(handle)});
}
#ifdef __ANDROID__
constexpr auto roundUpToMultiple = [](uint32_t value, uint32_t multiple) -> uint32_t {
return (value + multiple - 1) / multiple * multiple;
};
const auto size = memory.size();
const auto format = AHARDWAREBUFFER_FORMAT_BLOB;
const auto usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
const uint32_t width = size;
const uint32_t height = 1; // height is always 1 for BLOB mode AHardwareBuffer.
const uint32_t layers = 1; // layers is always 1 for BLOB mode AHardwareBuffer.
// AHardwareBuffer_createFromHandle() might fail because an allocator
// expects a specific stride value. In that case, we try to guess it by
// aligning the width to small powers of 2.
// TODO(b/174120849): Avoid stride assumptions.
AHardwareBuffer* hardwareBuffer = nullptr;
status_t status = UNKNOWN_ERROR;
for (uint32_t alignment : {1, 4, 32, 64, 128, 2, 8, 16}) {
const uint32_t stride = roundUpToMultiple(width, alignment);
AHardwareBuffer_Desc desc{
.width = width,
.height = height,
.layers = layers,
.format = format,
.usage = usage,
.stride = stride,
};
status = AHardwareBuffer_createFromHandle(&desc, memory.handle(),
AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE,
&hardwareBuffer);
if (status == NO_ERROR) {
break;
}
}
if (status != NO_ERROR) {
return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
<< "Can't create AHardwareBuffer from handle. Error: " << status;
}
return nn::createSharedMemoryFromAHWB(hardwareBuffer, /*takeOwnership=*/true);
#else // __ANDROID__
LOG(FATAL) << "nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const "
"hidl_memory& memory): Not Available on Host Build";
return (NN_ERROR() << "createSharedMemoryFromHidlMemory failed")
.
operator nn::GeneralResult<nn::SharedMemory>();
#endif // __ANDROID__
}
} // anonymous namespace
GeneralResult<OperandType> unvalidatedConvert(const hal::V1_0::OperandType& operandType) {
@@ -146,8 +267,20 @@ GeneralResult<Model::OperandValues> unvalidatedConvert(const hidl_vec<uint8_t>&
return Model::OperandValues(operandValues.data(), operandValues.size());
}
GeneralResult<SharedHandle> unvalidatedConvert(const hidl_handle& handle) {
if (handle.getNativeHandle() == nullptr) {
return nullptr;
}
if (handle->numFds != 1 || handle->numInts != 0) {
return NN_ERROR()
<< "unvalidatedConvert failed because handle does not only hold a single fd";
}
auto duplicatedFd = NN_TRY(nn::dupFd(handle->data[0]));
return std::make_shared<const Handle>(std::move(duplicatedFd));
}
GeneralResult<SharedMemory> unvalidatedConvert(const hidl_memory& memory) {
return hal::utils::createSharedMemoryFromHidlMemory(memory);
return createSharedMemoryFromHidlMemory(memory);
}
GeneralResult<Model> unvalidatedConvert(const hal::V1_0::Model& model) {
@@ -155,7 +288,7 @@ GeneralResult<Model> unvalidatedConvert(const hal::V1_0::Model& model) {
// Verify number of consumers.
const auto numberOfConsumers =
NN_TRY(hal::utils::countNumberOfConsumers(model.operands.size(), operations));
NN_TRY(countNumberOfConsumers(model.operands.size(), operations));
CHECK(model.operands.size() == numberOfConsumers.size());
for (size_t i = 0; i < model.operands.size(); ++i) {
if (model.operands[i].numberOfConsumers != numberOfConsumers[i]) {
@@ -260,6 +393,82 @@ nn::GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& c
return utils::unvalidatedConvert(canonical);
}
nn::GeneralResult<hidl_handle> createNativeHandleFrom(std::vector<base::unique_fd> fds,
const std::vector<int32_t>& ints) {
constexpr size_t kIntMax = std::numeric_limits<int>::max();
CHECK_LE(fds.size(), kIntMax);
CHECK_LE(ints.size(), kIntMax);
native_handle_t* nativeHandle =
native_handle_create(static_cast<int>(fds.size()), static_cast<int>(ints.size()));
if (nativeHandle == nullptr) {
return NN_ERROR() << "Failed to create native_handle";
}
for (size_t i = 0; i < fds.size(); ++i) {
nativeHandle->data[i] = fds[i].release();
}
std::copy(ints.begin(), ints.end(), nativeHandle->data + nativeHandle->numFds);
hidl_handle handle;
handle.setTo(nativeHandle, /*shouldOwn=*/true);
return handle;
}
nn::GeneralResult<hidl_handle> createNativeHandleFrom(base::unique_fd fd,
const std::vector<int32_t>& ints) {
std::vector<base::unique_fd> fds;
fds.push_back(std::move(fd));
return createNativeHandleFrom(std::move(fds), ints);
}
nn::GeneralResult<hidl_handle> createNativeHandleFrom(const nn::Memory::Unknown::Handle& handle) {
std::vector<base::unique_fd> fds = NN_TRY(nn::dupFds(handle.fds.begin(), handle.fds.end()));
return createNativeHandleFrom(std::move(fds), handle.ints);
}
nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Ashmem& memory) {
auto fd = NN_TRY(nn::dupFd(memory.fd));
auto handle = NN_TRY(createNativeHandleFrom(std::move(fd), {}));
return hidl_memory("ashmem", std::move(handle), memory.size);
}
nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Fd& memory) {
auto fd = NN_TRY(nn::dupFd(memory.fd));
const auto [lowOffsetBits, highOffsetBits] = nn::getIntsFromOffset(memory.offset);
const std::vector<int> ints = {memory.prot, lowOffsetBits, highOffsetBits};
auto handle = NN_TRY(createNativeHandleFrom(std::move(fd), ints));
return hidl_memory("mmap_fd", std::move(handle), memory.size);
}
nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::HardwareBuffer& memory) {
#ifdef __ANDROID__
const auto* ahwb = memory.handle.get();
AHardwareBuffer_Desc bufferDesc;
AHardwareBuffer_describe(ahwb, &bufferDesc);
const bool isBlob = bufferDesc.format == AHARDWAREBUFFER_FORMAT_BLOB;
const size_t size = isBlob ? bufferDesc.width : 0;
const char* const name = isBlob ? "hardware_buffer_blob" : "hardware_buffer";
const native_handle_t* nativeHandle = AHardwareBuffer_getNativeHandle(ahwb);
const hidl_handle hidlHandle(nativeHandle);
hidl_handle copiedHandle(hidlHandle);
return hidl_memory(name, std::move(copiedHandle), size);
#else // __ANDROID__
LOG(FATAL) << "nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const "
"nn::Memory::HardwareBuffer& memory): Not Available on Host Build";
(void)memory;
return (NN_ERROR() << "createHidlMemoryFrom failed").operator nn::GeneralResult<hidl_memory>();
#endif // __ANDROID__
}
nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Unknown& memory) {
return hidl_memory(memory.name, NN_TRY(createNativeHandleFrom(memory.handle)), memory.size);
}
} // anonymous namespace
nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType) {
@@ -332,8 +541,19 @@ nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert(
return hidl_vec<uint8_t>(operandValues.data(), operandValues.data() + operandValues.size());
}
nn::GeneralResult<hidl_handle> unvalidatedConvert(const nn::SharedHandle& handle) {
if (handle == nullptr) {
return {};
}
base::unique_fd fd = NN_TRY(nn::dupFd(handle->get()));
return createNativeHandleFrom(std::move(fd), {});
}
nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::SharedMemory& memory) {
return hal::utils::createHidlMemoryFromSharedMemory(memory);
if (memory == nullptr) {
return NN_ERROR() << "Memory must be non-empty";
}
return std::visit([](const auto& x) { return createHidlMemoryFrom(x); }, memory->handle);
}
nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) {
@@ -346,7 +566,7 @@ nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) {
// Update number of consumers.
const auto numberOfConsumers =
NN_TRY(hal::utils::countNumberOfConsumers(operands.size(), model.main.operations));
NN_TRY(countNumberOfConsumers(operands.size(), model.main.operations));
CHECK(operands.size() == numberOfConsumers.size());
for (size_t i = 0; i < operands.size(); ++i) {
operands[i].numberOfConsumers = numberOfConsumers[i];

View File

@@ -18,6 +18,8 @@
#include "Callbacks.h"
#include "Conversions.h"
#include "HandleError.h"
#include "ProtectCallback.h"
#include "Utils.h"
#include <android/hardware/neuralnetworks/1.0/IDevice.h>
@@ -29,8 +31,6 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/TransferValue.h>
#include <functional>
@@ -47,7 +47,7 @@ namespace {
nn::GeneralResult<nn::Capabilities> capabilitiesCallback(ErrorStatus status,
const Capabilities& capabilities) {
HANDLE_HAL_STATUS(status) << "getting capabilities failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "getting capabilities failed with " << toString(status);
return nn::convert(capabilities);
}
@@ -156,7 +156,7 @@ nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
const auto ret = kDevice->prepareModel(hidlModel, cb);
const auto status = HANDLE_TRANSPORT_FAILURE(ret);
HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "model preparation failed with " << toString(status);
return cb->get();
}

View File

@@ -18,6 +18,8 @@
#include "Callbacks.h"
#include "Conversions.h"
#include "HandleError.h"
#include "ProtectCallback.h"
#include "Utils.h"
#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
@@ -27,8 +29,6 @@
#include <nnapi/TypeUtils.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <memory>
#include <utility>

View File

@@ -20,6 +20,8 @@
#include "Callbacks.h"
#include "Conversions.h"
#include "Execution.h"
#include "HandleError.h"
#include "ProtectCallback.h"
#include "Utils.h"
#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
@@ -28,8 +30,6 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <memory>
#include <tuple>
@@ -84,7 +84,7 @@ PreparedModel::executeInternal(const V1_0::Request& request,
const auto ret = kPreparedModel->execute(request, cb);
const auto status = HANDLE_TRANSPORT_FAILURE(ret);
HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "execution failed with " << toString(status);
auto result = NN_TRY(cb->get());
if (relocation.output) {

View File

@@ -22,7 +22,8 @@
#include <android/hidl/base/1.0/IBase.h>
#include <hidl/HidlSupport.h>
#include <nnapi/Result.h>
#include <nnapi/hal/HandleError.h>
#include "HandleError.h"
#include <algorithm>
#include <functional>

View File

@@ -23,8 +23,8 @@
#include <nnapi/OperandTypes.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include <functional>
#include <memory>

View File

@@ -26,7 +26,6 @@
#include <nnapi/Types.h>
#include <nnapi/Validation.h>
#include <nnapi/hal/1.0/Conversions.h>
#include <nnapi/hal/HandleError.h>
namespace android::hardware::neuralnetworks::V1_1::utils {

View File

@@ -100,7 +100,7 @@ GeneralResult<Model> unvalidatedConvert(const hal::V1_1::Model& model) {
// Verify number of consumers.
const auto numberOfConsumers =
NN_TRY(hal::utils::countNumberOfConsumers(model.operands.size(), operations));
NN_TRY(countNumberOfConsumers(model.operands.size(), operations));
CHECK(model.operands.size() == numberOfConsumers.size());
for (size_t i = 0; i < model.operands.size(); ++i) {
if (model.operands[i].numberOfConsumers != numberOfConsumers[i]) {
@@ -223,7 +223,7 @@ nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) {
// Update number of consumers.
const auto numberOfConsumers =
NN_TRY(hal::utils::countNumberOfConsumers(operands.size(), model.main.operations));
NN_TRY(countNumberOfConsumers(operands.size(), model.main.operations));
CHECK(operands.size() == numberOfConsumers.size());
for (size_t i = 0; i < operands.size(); ++i) {
operands[i].numberOfConsumers = numberOfConsumers[i];

View File

@@ -29,9 +29,9 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/Callbacks.h>
#include <nnapi/hal/1.0/HandleError.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <functional>
#include <memory>
@@ -47,7 +47,7 @@ namespace {
nn::GeneralResult<nn::Capabilities> capabilitiesCallback(V1_0::ErrorStatus status,
const Capabilities& capabilities) {
HANDLE_HAL_STATUS(status) << "getting capabilities failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "getting capabilities failed with " << toString(status);
return nn::convert(capabilities);
}
@@ -157,7 +157,7 @@ nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
const auto ret = kDevice->prepareModel_1_1(hidlModel, hidlPreference, cb);
const auto status = HANDLE_TRANSPORT_FAILURE(ret);
HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "model preparation failed with " << toString(status);
return cb->get();
}

View File

@@ -27,8 +27,8 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/Callbacks.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/TransferValue.h>
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface

View File

@@ -45,7 +45,6 @@ GeneralResult<Timing> unvalidatedConvert(const hal::V1_2::Timing& timing);
GeneralResult<Extension> unvalidatedConvert(const hal::V1_2::Extension& extension);
GeneralResult<Extension::OperandTypeInformation> unvalidatedConvert(
const hal::V1_2::Extension::OperandTypeInformation& operandTypeInformation);
GeneralResult<SharedHandle> unvalidatedConvert(const hardware::hidl_handle& handle);
GeneralResult<DeviceType> convert(const hal::V1_2::DeviceType& deviceType);
GeneralResult<Capabilities> convert(const hal::V1_2::Capabilities& capabilities);
@@ -86,7 +85,6 @@ nn::GeneralResult<Timing> unvalidatedConvert(const nn::Timing& timing);
nn::GeneralResult<Extension> unvalidatedConvert(const nn::Extension& extension);
nn::GeneralResult<Extension::OperandTypeInformation> unvalidatedConvert(
const nn::Extension::OperandTypeInformation& operandTypeInformation);
nn::GeneralResult<hidl_handle> unvalidatedConvert(const nn::SharedHandle& handle);
nn::GeneralResult<DeviceType> convert(const nn::DeviceType& deviceType);
nn::GeneralResult<Capabilities> convert(const nn::Capabilities& capabilities);

View File

@@ -23,8 +23,8 @@
#include <nnapi/OperandTypes.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include <functional>
#include <memory>

View File

@@ -21,8 +21,8 @@
#include <nnapi/IExecution.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include "PreparedModel.h"

View File

@@ -32,8 +32,8 @@
#include <nnapi/IPreparedModel.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include <atomic>
#include <chrono>

View File

@@ -29,7 +29,7 @@
#include <nnapi/IBurst.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <atomic>
#include <chrono>

View File

@@ -23,7 +23,7 @@
#include <hidl/MQDescriptor.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <atomic>
#include <chrono>

View File

@@ -22,8 +22,8 @@
#include <nnapi/IPreparedModel.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include <memory>
#include <tuple>

View File

@@ -28,7 +28,6 @@
#include <nnapi/hal/1.0/Conversions.h>
#include <nnapi/hal/1.1/Conversions.h>
#include <nnapi/hal/1.1/Utils.h>
#include <nnapi/hal/HandleError.h>
#include <limits>

View File

@@ -29,10 +29,10 @@
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/Callbacks.h>
#include <nnapi/hal/1.0/Conversions.h>
#include <nnapi/hal/1.0/HandleError.h>
#include <nnapi/hal/1.0/PreparedModel.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/TransferValue.h>
#include <utility>
@@ -62,7 +62,7 @@ convertExecutionGeneralResultsHelper(const hidl_vec<OutputShape>& outputShapes,
nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback(
V1_0::ErrorStatus status, const sp<IPreparedModel>& preparedModel) {
HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "model preparation failed with " << toString(status);
return NN_TRY(PreparedModel::create(preparedModel, /*executeSynchronously=*/true));
}
@@ -74,7 +74,7 @@ nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> executi
return NN_ERROR(nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, std::move(canonicalOutputShapes))
<< "execution failed with " << toString(status);
}
HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "execution failed with " << toString(status);
return convertExecutionGeneralResultsHelper(outputShapes, timing);
}

View File

@@ -28,7 +28,6 @@
#include <nnapi/hal/1.0/Conversions.h>
#include <nnapi/hal/1.1/Conversions.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <algorithm>
#include <functional>
@@ -187,7 +186,7 @@ GeneralResult<Model> unvalidatedConvert(const hal::V1_2::Model& model) {
// Verify number of consumers.
const auto numberOfConsumers =
NN_TRY(hal::utils::countNumberOfConsumers(model.operands.size(), operations));
NN_TRY(countNumberOfConsumers(model.operands.size(), operations));
CHECK(model.operands.size() == numberOfConsumers.size());
for (size_t i = 0; i < model.operands.size(); ++i) {
if (model.operands[i].numberOfConsumers != numberOfConsumers[i]) {
@@ -264,14 +263,6 @@ GeneralResult<Extension::OperandTypeInformation> unvalidatedConvert(
};
}
GeneralResult<SharedHandle> unvalidatedConvert(const hidl_handle& hidlHandle) {
if (hidlHandle.getNativeHandle() == nullptr) {
return nullptr;
}
auto handle = NN_TRY(hal::utils::sharedHandleFromNativeHandle(hidlHandle.getNativeHandle()));
return std::make_shared<const Handle>(std::move(handle));
}
GeneralResult<DeviceType> convert(const hal::V1_2::DeviceType& deviceType) {
return validatedConvert(deviceType);
}
@@ -334,6 +325,10 @@ nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert(
return V1_0::utils::unvalidatedConvert(operandValues);
}
nn::GeneralResult<hidl_handle> unvalidatedConvert(const nn::SharedHandle& handle) {
return V1_0::utils::unvalidatedConvert(handle);
}
nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::SharedMemory& memory) {
return V1_0::utils::unvalidatedConvert(memory);
}
@@ -481,7 +476,7 @@ nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) {
// Update number of consumers.
const auto numberOfConsumers =
NN_TRY(hal::utils::countNumberOfConsumers(operands.size(), model.main.operations));
NN_TRY(countNumberOfConsumers(operands.size(), model.main.operations));
CHECK(operands.size() == numberOfConsumers.size());
for (size_t i = 0; i < operands.size(); ++i) {
operands[i].numberOfConsumers = numberOfConsumers[i];
@@ -544,13 +539,6 @@ nn::GeneralResult<Extension::OperandTypeInformation> unvalidatedConvert(
};
}
nn::GeneralResult<hidl_handle> unvalidatedConvert(const nn::SharedHandle& handle) {
if (handle == nullptr) {
return {};
}
return hal::utils::hidlHandleFromSharedHandle(*handle);
}
nn::GeneralResult<DeviceType> convert(const nn::DeviceType& deviceType) {
return validatedConvert(deviceType);
}

View File

@@ -30,10 +30,10 @@
#include <nnapi/OperandTypes.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/HandleError.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/1.1/Conversions.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <functional>
#include <memory>
@@ -49,31 +49,31 @@ namespace {
nn::GeneralResult<nn::Capabilities> capabilitiesCallback(V1_0::ErrorStatus status,
const Capabilities& capabilities) {
HANDLE_HAL_STATUS(status) << "getting capabilities failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "getting capabilities failed with " << toString(status);
return nn::convert(capabilities);
}
nn::GeneralResult<std::string> versionStringCallback(V1_0::ErrorStatus status,
const hidl_string& versionString) {
HANDLE_HAL_STATUS(status) << "getVersionString failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "getVersionString failed with " << toString(status);
return versionString;
}
nn::GeneralResult<nn::DeviceType> deviceTypeCallback(V1_0::ErrorStatus status,
DeviceType deviceType) {
HANDLE_HAL_STATUS(status) << "getDeviceType failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "getDeviceType failed with " << toString(status);
return nn::convert(deviceType);
}
nn::GeneralResult<std::vector<nn::Extension>> supportedExtensionsCallback(
V1_0::ErrorStatus status, const hidl_vec<Extension>& extensions) {
HANDLE_HAL_STATUS(status) << "getExtensions failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "getExtensions failed with " << toString(status);
return nn::convert(extensions);
}
nn::GeneralResult<std::pair<uint32_t, uint32_t>> numberOfCacheFilesNeededCallback(
V1_0::ErrorStatus status, uint32_t numModelCache, uint32_t numDataCache) {
HANDLE_HAL_STATUS(status) << "getNumberOfCacheFilesNeeded failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "getNumberOfCacheFilesNeeded failed with " << toString(status);
if (numModelCache > nn::kMaxNumberOfCacheFiles) {
return NN_ERROR() << "getNumberOfCacheFilesNeeded returned numModelCache files greater "
"than allowed max ("
@@ -254,7 +254,7 @@ nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
const auto ret = kDevice->prepareModel_1_2(hidlModel, hidlPreference, hidlModelCache,
hidlDataCache, hidlToken, cb);
const auto status = HANDLE_TRANSPORT_FAILURE(ret);
HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "model preparation failed with " << toString(status);
return cb->get();
}
@@ -271,7 +271,7 @@ nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
const auto ret = kDevice->prepareModelFromCache(hidlModelCache, hidlDataCache, hidlToken, cb);
const auto status = HANDLE_TRANSPORT_FAILURE(ret);
HANDLE_HAL_STATUS(status) << "model preparation from cache failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "model preparation from cache failed with " << toString(status);
return cb->get();
}

View File

@@ -29,7 +29,6 @@
#include <nnapi/TypeUtils.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <memory>
#include <utility>

View File

@@ -28,9 +28,9 @@
#include <nnapi/Types.h>
#include <nnapi/Validation.h>
#include <nnapi/hal/1.0/Conversions.h>
#include <nnapi/hal/1.0/HandleError.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/TransferValue.h>
#include <algorithm>
@@ -82,8 +82,8 @@ class BurstExecution final : public nn::IExecution,
nn::GeneralResult<sp<IBurstContext>> executionBurstResultCallback(
V1_0::ErrorStatus status, const sp<IBurstContext>& burstContext) {
HANDLE_HAL_STATUS(status) << "IPreparedModel::configureExecutionBurst failed with status "
<< toString(status);
HANDLE_STATUS_HIDL(status) << "IPreparedModel::configureExecutionBurst failed with status "
<< toString(status);
if (burstContext == nullptr) {
return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
<< "IPreparedModel::configureExecutionBurst returned nullptr for burst";

View File

@@ -27,8 +27,8 @@
#include <nnapi/Types.h>
#include <nnapi/Validation.h>
#include <nnapi/hal/1.0/Conversions.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/1.0/HandleError.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/TransferValue.h>
#include <algorithm>
@@ -50,7 +50,7 @@ constexpr V1_2::Timing kNoTiming = {std::numeric_limits<uint64_t>::max(),
nn::GeneralResult<std::vector<nn::SharedMemory>> getMemoriesCallback(
V1_0::ErrorStatus status, const hidl_vec<hidl_memory>& memories) {
HANDLE_HAL_STATUS(status) << "getting burst memories failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "getting burst memories failed with " << toString(status);
std::vector<nn::SharedMemory> canonicalMemories;
canonicalMemories.reserve(memories.size());
for (const auto& memory : memories) {

View File

@@ -27,7 +27,7 @@
#include <hidl/MQDescriptor.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <atomic>
#include <chrono>

View File

@@ -31,9 +31,9 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/Conversions.h>
#include <nnapi/hal/1.0/HandleError.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <chrono>
#include <memory>
@@ -82,7 +82,7 @@ PreparedModel::executeAsynchronously(const V1_0::Request& request, MeasureTiming
const auto ret = kPreparedModel->execute_1_2(request, measure, cb);
const auto status = HANDLE_TRANSPORT_FAILURE(ret);
if (status != V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "execution failed with " << toString(status);
}
return cb->get();

View File

@@ -30,8 +30,8 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/Callbacks.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/TransferValue.h>
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface

View File

@@ -113,6 +113,9 @@ nn::GeneralResult<V1_2::DeviceType> convert(const nn::DeviceType& deviceType);
nn::GeneralResult<V1_2::MeasureTiming> convert(const nn::MeasureTiming& measureTiming);
nn::GeneralResult<V1_2::Timing> convert(const nn::Timing& timing);
nn::GeneralResult<hidl_vec<hidl_handle>> convertSyncFences(
const std::vector<nn::SyncFence>& fences);
} // namespace android::hardware::neuralnetworks::V1_3::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_CONVERSIONS_H

View File

@@ -23,8 +23,8 @@
#include <nnapi/OperandTypes.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include <functional>
#include <memory>

View File

@@ -21,8 +21,8 @@
#include <nnapi/IPreparedModel.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include <memory>
#include <tuple>

View File

@@ -30,7 +30,6 @@
#include <nnapi/hal/1.1/Utils.h>
#include <nnapi/hal/1.2/Conversions.h>
#include <nnapi/hal/1.2/Utils.h>
#include <nnapi/hal/HandleError.h>
namespace android::hardware::neuralnetworks::V1_3::utils {

View File

@@ -25,7 +25,7 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/Conversions.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/1.0/HandleError.h>
#include "Conversions.h"
#include "Utils.h"
@@ -66,7 +66,7 @@ nn::GeneralResult<void> Buffer::copyTo(const nn::SharedMemory& dst) const {
const auto ret = kBuffer->copyTo(hidlDst);
const auto status = HANDLE_TRANSPORT_FAILURE(ret);
HANDLE_HAL_STATUS(status) << "IBuffer::copyTo failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "IBuffer::copyTo failed with " << toString(status);
return {};
}
@@ -78,7 +78,7 @@ nn::GeneralResult<void> Buffer::copyFrom(const nn::SharedMemory& src,
const auto ret = kBuffer->copyFrom(hidlSrc, hidlDimensions);
const auto status = HANDLE_TRANSPORT_FAILURE(ret);
HANDLE_HAL_STATUS(status) << "IBuffer::copyFrom failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "IBuffer::copyFrom failed with " << toString(status);
return {};
}

View File

@@ -30,13 +30,13 @@
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/Callbacks.h>
#include <nnapi/hal/1.0/Conversions.h>
#include <nnapi/hal/1.0/HandleError.h>
#include <nnapi/hal/1.0/PreparedModel.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/1.2/Callbacks.h>
#include <nnapi/hal/1.2/Conversions.h>
#include <nnapi/hal/1.2/PreparedModel.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <nnapi/hal/TransferValue.h>
#include <utility>
@@ -71,13 +71,13 @@ convertExecutionGeneralResultsHelper(const hidl_vec<V1_2::OutputShape>& outputSh
nn::GeneralResult<std::vector<bool>> supportedOperationsCallback(
ErrorStatus status, const hidl_vec<bool>& supportedOperations) {
HANDLE_HAL_STATUS(status) << "get supported operations failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "get supported operations failed with " << toString(status);
return supportedOperations;
}
nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback(
ErrorStatus status, const sp<IPreparedModel>& preparedModel) {
HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "model preparation failed with " << toString(status);
return NN_TRY(PreparedModel::create(preparedModel, /*executeSynchronously=*/true));
}
@@ -90,7 +90,7 @@ nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> executi
return NN_ERROR(nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, std::move(canonicalOutputShapes))
<< "execution failed with " << toString(status);
}
HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "execution failed with " << toString(status);
return convertExecutionGeneralResultsHelper(outputShapes, timing);
}

View File

@@ -28,7 +28,6 @@
#include <nnapi/hal/1.0/Conversions.h>
#include <nnapi/hal/1.2/Conversions.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <algorithm>
#include <chrono>
@@ -194,7 +193,7 @@ GeneralResult<Model::Subgraph> unvalidatedConvert(const hal::V1_3::Subgraph& sub
// Verify number of consumers.
const auto numberOfConsumers =
NN_TRY(hal::utils::countNumberOfConsumers(subgraph.operands.size(), operations));
NN_TRY(countNumberOfConsumers(subgraph.operands.size(), operations));
CHECK(subgraph.operands.size() == numberOfConsumers.size());
for (size_t i = 0; i < subgraph.operands.size(); ++i) {
if (subgraph.operands[i].numberOfConsumers != numberOfConsumers[i]) {
@@ -380,7 +379,7 @@ nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert(
}
nn::GeneralResult<hidl_handle> unvalidatedConvert(const nn::SharedHandle& handle) {
return V1_2::utils::unvalidatedConvert(handle);
return V1_0::utils::unvalidatedConvert(handle);
}
nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::SharedMemory& memory) {
@@ -543,7 +542,7 @@ nn::GeneralResult<Subgraph> unvalidatedConvert(const nn::Model::Subgraph& subgra
// Update number of consumers.
const auto numberOfConsumers =
NN_TRY(hal::utils::countNumberOfConsumers(operands.size(), subgraph.operations));
NN_TRY(countNumberOfConsumers(operands.size(), subgraph.operations));
CHECK(operands.size() == numberOfConsumers.size());
for (size_t i = 0; i < operands.size(); ++i) {
operands[i].numberOfConsumers = numberOfConsumers[i];
@@ -727,4 +726,13 @@ nn::GeneralResult<V1_2::Timing> convert(const nn::Timing& timing) {
return V1_2::utils::convert(timing);
}
nn::GeneralResult<hidl_vec<hidl_handle>> convertSyncFences(
const std::vector<nn::SyncFence>& syncFences) {
std::vector<nn::SharedHandle> handles;
handles.reserve(syncFences.size());
std::transform(syncFences.begin(), syncFences.end(), std::back_inserter(handles),
[](const nn::SyncFence& syncFence) { return syncFence.getSharedHandle(); });
return convert(handles);
}
} // namespace android::hardware::neuralnetworks::V1_3::utils

View File

@@ -33,13 +33,13 @@
#include <nnapi/OperandTypes.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/HandleError.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/1.1/Conversions.h>
#include <nnapi/hal/1.2/Conversions.h>
#include <nnapi/hal/1.2/Device.h>
#include <nnapi/hal/1.2/Utils.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <any>
#include <functional>
@@ -72,7 +72,7 @@ nn::GeneralResult<hidl_vec<sp<IPreparedModel>>> convert(
nn::GeneralResult<nn::Capabilities> capabilitiesCallback(ErrorStatus status,
const Capabilities& capabilities) {
HANDLE_HAL_STATUS(status) << "getting capabilities failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "getting capabilities failed with " << toString(status);
return nn::convert(capabilities);
}
@@ -89,7 +89,7 @@ nn::GeneralResult<nn::Capabilities> getCapabilitiesFrom(V1_3::IDevice* device) {
nn::GeneralResult<nn::SharedBuffer> allocationCallback(ErrorStatus status,
const sp<IBuffer>& buffer, uint32_t token) {
HANDLE_HAL_STATUS(status) << "IDevice::allocate failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "IDevice::allocate failed with " << toString(status);
return Buffer::create(buffer, static_cast<nn::Request::MemoryDomainToken>(token));
}
@@ -208,7 +208,7 @@ nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
kDevice->prepareModel_1_3(hidlModel, hidlPreference, hidlPriority, hidlDeadline,
hidlModelCache, hidlDataCache, hidlToken, cb);
const auto status = HANDLE_TRANSPORT_FAILURE(ret);
HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "model preparation failed with " << toString(status);
return cb->get();
}
@@ -227,7 +227,7 @@ nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
const auto ret = kDevice->prepareModelFromCache_1_3(hidlDeadline, hidlModelCache, hidlDataCache,
hidlToken, cb);
const auto status = HANDLE_TRANSPORT_FAILURE(ret);
HANDLE_HAL_STATUS(status) << "model preparation from cache failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "model preparation from cache failed with " << toString(status);
return cb->get();
}

View File

@@ -29,7 +29,6 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <memory>
#include <utility>
@@ -73,7 +72,7 @@ nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Executi
nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> Execution::computeFenced(
const std::vector<nn::SyncFence>& waitFor, const nn::OptionalTimePoint& deadline,
const nn::OptionalDuration& timeoutDurationAfterFence) const {
const auto hidlWaitFor = NN_TRY(hal::utils::convertSyncFences(waitFor));
const auto hidlWaitFor = NN_TRY(convertSyncFences(waitFor));
const auto hidlDeadline = NN_TRY(convert(deadline));
const auto hidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
return kPreparedModel->executeFencedInternal(kRequest, hidlWaitFor, kMeasure, hidlDeadline,

View File

@@ -30,12 +30,12 @@
#include <nnapi/Result.h>
#include <nnapi/TypeUtils.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/HandleError.h>
#include <nnapi/hal/1.0/ProtectCallback.h>
#include <nnapi/hal/1.2/Conversions.h>
#include <nnapi/hal/1.2/ExecutionBurstController.h>
#include <nnapi/hal/1.2/ExecutionBurstUtils.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <nnapi/hal/ProtectCallback.h>
#include <memory>
#include <tuple>
@@ -50,14 +50,14 @@ namespace {
nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> convertFencedExecutionCallbackResults(
ErrorStatus status, const V1_2::Timing& timingLaunched, const V1_2::Timing& timingFenced) {
HANDLE_HAL_STATUS(status) << "fenced execution callback info failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "fenced execution callback info failed with " << toString(status);
return std::make_pair(NN_TRY(nn::convert(timingLaunched)), NN_TRY(nn::convert(timingFenced)));
}
nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> fencedExecutionCallback(
ErrorStatus status, const hidl_handle& syncFence,
const sp<IFencedExecutionCallback>& callback) {
HANDLE_HAL_STATUS(status) << "fenced execution failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "fenced execution failed with " << toString(status);
auto resultSyncFence = nn::SyncFence::createAsSignaled();
if (syncFence.getNativeHandle() != nullptr) {
@@ -127,7 +127,7 @@ PreparedModel::executeAsynchronously(const Request& request, V1_2::MeasureTiming
kPreparedModel->execute_1_3(request, measure, deadline, loopTimeoutDuration, cb);
const auto status = HANDLE_TRANSPORT_FAILURE(ret);
if (status != ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
HANDLE_STATUS_HIDL(status) << "execution failed with " << toString(status);
}
return cb->get();
@@ -186,7 +186,7 @@ PreparedModel::executeFenced(const nn::Request& request, const std::vector<nn::S
&maybeRequestInShared, &relocation));
const auto hidlRequest = NN_TRY(convert(requestInShared));
const auto hidlWaitFor = NN_TRY(hal::utils::convertSyncFences(waitFor));
const auto hidlWaitFor = NN_TRY(convertSyncFences(waitFor));
const auto hidlMeasure = NN_TRY(convert(measure));
const auto hidlDeadline = NN_TRY(convert(deadline));
const auto hidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));

View File

@@ -40,7 +40,6 @@ cc_library_static {
shared_libs: [
"android.hardware.neuralnetworks-V2-ndk",
"libbinder_ndk",
"libhidlbase",
],
target: {
android: {

View File

@@ -32,8 +32,7 @@
namespace aidl::android::hardware::neuralnetworks::utils {
// An AIDL callback class to receive the results of IDevice::prepareModel* asynchronously.
class PreparedModelCallback final : public BnPreparedModelCallback,
public hal::utils::IProtectedCallback {
class PreparedModelCallback final : public BnPreparedModelCallback, public IProtectedCallback {
public:
using Data = nn::GeneralResult<nn::SharedPreparedModel>;

View File

@@ -23,7 +23,6 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include <functional>
#include <mutex>
@@ -34,19 +33,39 @@
namespace aidl::android::hardware::neuralnetworks::utils {
class IProtectedCallback {
public:
/**
* Marks this object as a dead object.
*/
virtual void notifyAsDeadObject() = 0;
// Public virtual destructor to allow objects to be stored (and destroyed) as smart pointers.
// E.g., std::unique_ptr<IProtectedCallback>.
virtual ~IProtectedCallback() = default;
protected:
// Protect the non-destructor special member functions to prevent object slicing.
IProtectedCallback() = default;
IProtectedCallback(const IProtectedCallback&) = default;
IProtectedCallback(IProtectedCallback&&) noexcept = default;
IProtectedCallback& operator=(const IProtectedCallback&) = default;
IProtectedCallback& operator=(IProtectedCallback&&) noexcept = default;
};
// Thread safe class
class DeathMonitor final {
public:
static void serviceDied(void* cookie);
void serviceDied();
// Precondition: `killable` must be non-null.
void add(hal::utils::IProtectedCallback* killable) const;
void add(IProtectedCallback* killable) const;
// Precondition: `killable` must be non-null.
void remove(hal::utils::IProtectedCallback* killable) const;
void remove(IProtectedCallback* killable) const;
private:
mutable std::mutex mMutex;
mutable std::vector<hal::utils::IProtectedCallback*> mObjects GUARDED_BY(mMutex);
mutable std::vector<IProtectedCallback*> mObjects GUARDED_BY(mMutex);
};
class DeathHandler final {
@@ -62,7 +81,7 @@ class DeathHandler final {
using Cleanup = std::function<void()>;
// Precondition: `killable` must be non-null.
[[nodiscard]] ::android::base::ScopeGuard<Cleanup> protectCallback(
hal::utils::IProtectedCallback* killable) const;
IProtectedCallback* killable) const;
std::shared_ptr<DeathMonitor> getDeathMonitor() const { return kDeathMonitor; }

View File

@@ -24,7 +24,6 @@
#include <nnapi/TypeUtils.h>
#include <nnapi/Types.h>
#include <nnapi/Validation.h>
#include <nnapi/hal/HandleError.h>
namespace aidl::android::hardware::neuralnetworks::utils {
@@ -75,6 +74,13 @@ nn::GeneralResult<void> handleTransportError(const ndk::ScopedAStatus& ret);
for (const auto status = handleTransportError(ret); !status.ok();) \
return NN_ERROR(status.error().code) << status.error().message << ": "
#define HANDLE_STATUS_AIDL(status) \
if (const ::android::nn::ErrorStatus canonical = ::android::nn::convert(status).value_or( \
::android::nn::ErrorStatus::GENERAL_FAILURE); \
canonical == ::android::nn::ErrorStatus::NONE) { \
} else \
return NN_ERROR(canonical)
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_H

View File

@@ -26,7 +26,6 @@
#include <nnapi/Result.h>
#include <nnapi/TypeUtils.h>
#include <nnapi/Types.h>
#include <nnapi/hal/HandleError.h>
#include <memory>
#include <mutex>

View File

@@ -38,7 +38,7 @@ namespace {
// nn::Version::ANDROID_S. On failure, this function returns with the appropriate nn::GeneralError.
nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback(
ErrorStatus status, const std::shared_ptr<IPreparedModel>& preparedModel) {
HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
HANDLE_STATUS_AIDL(status) << "model preparation failed with " << toString(status);
return NN_TRY(PreparedModel::create(preparedModel));
}

View File

@@ -34,7 +34,6 @@
#include <nnapi/Types.h>
#include <nnapi/Validation.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <algorithm>
#include <chrono>

View File

@@ -25,7 +25,6 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <memory>
#include <utility>

View File

@@ -30,7 +30,6 @@
#include <nnapi/TypeUtils.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <memory>
#include <tuple>
@@ -51,7 +50,7 @@ nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> convertEx
nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> convertFencedExecutionResults(
ErrorStatus status, const aidl_hal::Timing& timingLaunched,
const aidl_hal::Timing& timingFenced) {
HANDLE_HAL_STATUS(status) << "fenced execution callback info failed with " << toString(status);
HANDLE_STATUS_AIDL(status) << "fenced execution callback info failed with " << toString(status);
return std::make_pair(NN_TRY(nn::convert(timingLaunched)), NN_TRY(nn::convert(timingFenced)));
}

View File

@@ -22,7 +22,6 @@
#include <android/binder_auto_utils.h>
#include <android/binder_interface_utils.h>
#include <nnapi/Result.h>
#include <nnapi/hal/ProtectCallback.h>
#include <algorithm>
#include <functional>
@@ -37,7 +36,7 @@ namespace aidl::android::hardware::neuralnetworks::utils {
void DeathMonitor::serviceDied() {
std::lock_guard guard(mMutex);
std::for_each(mObjects.begin(), mObjects.end(),
[](hal::utils::IProtectedCallback* killable) { killable->notifyAsDeadObject(); });
[](IProtectedCallback* killable) { killable->notifyAsDeadObject(); });
}
void DeathMonitor::serviceDied(void* cookie) {
@@ -45,13 +44,13 @@ void DeathMonitor::serviceDied(void* cookie) {
deathMonitor->serviceDied();
}
void DeathMonitor::add(hal::utils::IProtectedCallback* killable) const {
void DeathMonitor::add(IProtectedCallback* killable) const {
CHECK(killable != nullptr);
std::lock_guard guard(mMutex);
mObjects.push_back(killable);
}
void DeathMonitor::remove(hal::utils::IProtectedCallback* killable) const {
void DeathMonitor::remove(IProtectedCallback* killable) const {
CHECK(killable != nullptr);
std::lock_guard guard(mMutex);
const auto removedIter = std::remove(mObjects.begin(), mObjects.end(), killable);
@@ -102,7 +101,7 @@ DeathHandler::~DeathHandler() {
}
[[nodiscard]] ::android::base::ScopeGuard<DeathHandler::Cleanup> DeathHandler::protectCallback(
hal::utils::IProtectedCallback* killable) const {
IProtectedCallback* killable) const {
CHECK(killable != nullptr);
kDeathMonitor->add(killable);
return ::android::base::make_scope_guard(

View File

@@ -1315,8 +1315,8 @@ static void mutateExecutionPriorityTest(const std::shared_ptr<IDevice>& device,
void validateModel(const std::shared_ptr<IDevice>& device, const Model& model) {
const auto numberOfConsumers =
nn::countNumberOfConsumers(model.main.operands.size(),
nn::unvalidatedConvert(model.main.operations).value())
countNumberOfConsumers(model.main.operands.size(),
nn::unvalidatedConvert(model.main.operations).value())
.value();
mutateExecutionOrderTest(device, model, numberOfConsumers);
mutateOperandTypeTest(device, model);

View File

@@ -36,7 +36,6 @@
#include <nnapi/hal/1.2/Utils.h>
#include <nnapi/hal/1.3/Conversions.h>
#include <nnapi/hal/1.3/Utils.h>
#include <nnapi/hal/HandleError.h>
#include <sys/types.h>
#include <memory>

View File

@@ -30,20 +30,8 @@ cc_library_static {
local_include_dirs: ["include/nnapi/hal"],
export_include_dirs: ["include"],
cflags: ["-Wthread-safety"],
static_libs: [
"libarect",
"neuralnetworks_types",
],
shared_libs: [
"android.hardware.neuralnetworks-V2-ndk",
"libhidlbase",
"libbinder_ndk",
],
target: {
android: {
shared_libs: ["libnativewindow"],
},
},
static_libs: ["neuralnetworks_types"],
shared_libs: ["libbinder_ndk"],
}
cc_test {
@@ -51,7 +39,6 @@ cc_test {
host_supported: true,
srcs: ["test/*.cpp"],
static_libs: [
"android.hardware.neuralnetworks@1.0",
"libgmock",
"libneuralnetworks_common",
"neuralnetworks_types",

View File

@@ -17,8 +17,6 @@
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_COMMON_UTILS_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_COMMON_UTILS_H
#include <cutils/native_handle.h>
#include <hidl/HidlSupport.h>
#include <nnapi/Result.h>
#include <nnapi/SharedMemory.h>
#include <nnapi/Types.h>
@@ -125,18 +123,6 @@ nn::GeneralResult<std::reference_wrapper<const nn::Request>> convertRequestFromP
const nn::Request* request, uint32_t alignment, uint32_t padding,
std::optional<nn::Request>* maybeRequestInSharedOut, RequestRelocation* relocationOut);
nn::GeneralResult<std::vector<uint32_t>> countNumberOfConsumers(
size_t numberOfOperands, const std::vector<nn::Operation>& operations);
nn::GeneralResult<hidl_memory> createHidlMemoryFromSharedMemory(const nn::SharedMemory& memory);
nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_memory& memory);
nn::GeneralResult<hidl_handle> hidlHandleFromSharedHandle(const nn::Handle& handle);
nn::GeneralResult<nn::Handle> sharedHandleFromNativeHandle(const native_handle_t* handle);
nn::GeneralResult<hidl_vec<hidl_handle>> convertSyncFences(
const std::vector<nn::SyncFence>& fences);
} // namespace android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_COMMON_UTILS_H

View File

@@ -16,11 +16,7 @@
#include "CommonUtils.h"
#include "HandleError.h"
#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <hidl/HidlSupport.h>
#include <nnapi/Result.h>
#include <nnapi/SharedMemory.h>
#include <nnapi/TypeUtils.h>
@@ -34,11 +30,6 @@
#include <variant>
#include <vector>
#ifdef __ANDROID__
#include <android/hardware_buffer.h>
#include <vndk/hardware_buffer.h>
#endif // __ANDROID__
namespace android::hardware::neuralnetworks::utils {
namespace {
@@ -92,97 +83,6 @@ void copyPointersToSharedMemory(nn::Model::Subgraph* subgraph,
});
}
nn::GeneralResult<hidl_handle> createNativeHandleFrom(std::vector<base::unique_fd> fds,
const std::vector<int32_t>& ints) {
constexpr size_t kIntMax = std::numeric_limits<int>::max();
CHECK_LE(fds.size(), kIntMax);
CHECK_LE(ints.size(), kIntMax);
native_handle_t* nativeHandle =
native_handle_create(static_cast<int>(fds.size()), static_cast<int>(ints.size()));
if (nativeHandle == nullptr) {
return NN_ERROR() << "Failed to create native_handle";
}
for (size_t i = 0; i < fds.size(); ++i) {
nativeHandle->data[i] = fds[i].release();
}
std::copy(ints.begin(), ints.end(), nativeHandle->data + nativeHandle->numFds);
hidl_handle handle;
handle.setTo(nativeHandle, /*shouldOwn=*/true);
return handle;
}
nn::GeneralResult<hidl_handle> createNativeHandleFrom(base::unique_fd fd,
const std::vector<int32_t>& ints) {
std::vector<base::unique_fd> fds;
fds.push_back(std::move(fd));
return createNativeHandleFrom(std::move(fds), ints);
}
nn::GeneralResult<hidl_handle> createNativeHandleFrom(const nn::Memory::Unknown::Handle& handle) {
std::vector<base::unique_fd> fds = NN_TRY(nn::dupFds(handle.fds.begin(), handle.fds.end()));
return createNativeHandleFrom(std::move(fds), handle.ints);
}
nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Ashmem& memory) {
auto fd = NN_TRY(nn::dupFd(memory.fd));
auto handle = NN_TRY(createNativeHandleFrom(std::move(fd), {}));
return hidl_memory("ashmem", std::move(handle), memory.size);
}
nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Fd& memory) {
auto fd = NN_TRY(nn::dupFd(memory.fd));
const auto [lowOffsetBits, highOffsetBits] = nn::getIntsFromOffset(memory.offset);
const std::vector<int> ints = {memory.prot, lowOffsetBits, highOffsetBits};
auto handle = NN_TRY(createNativeHandleFrom(std::move(fd), ints));
return hidl_memory("mmap_fd", std::move(handle), memory.size);
}
nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::HardwareBuffer& memory) {
#ifdef __ANDROID__
const auto* ahwb = memory.handle.get();
AHardwareBuffer_Desc bufferDesc;
AHardwareBuffer_describe(ahwb, &bufferDesc);
const bool isBlob = bufferDesc.format == AHARDWAREBUFFER_FORMAT_BLOB;
const size_t size = isBlob ? bufferDesc.width : 0;
const char* const name = isBlob ? "hardware_buffer_blob" : "hardware_buffer";
const native_handle_t* nativeHandle = AHardwareBuffer_getNativeHandle(ahwb);
const hidl_handle hidlHandle(nativeHandle);
hidl_handle copiedHandle(hidlHandle);
return hidl_memory(name, std::move(copiedHandle), size);
#else // __ANDROID__
LOG(FATAL) << "nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const "
"nn::Memory::HardwareBuffer& memory): Not Available on Host Build";
(void)memory;
return (NN_ERROR() << "createHidlMemoryFrom failed").operator nn::GeneralResult<hidl_memory>();
#endif // __ANDROID__
}
nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Unknown& memory) {
return hidl_memory(memory.name, NN_TRY(createNativeHandleFrom(memory.handle)), memory.size);
}
nn::GeneralResult<nn::Memory::Unknown::Handle> unknownHandleFromNativeHandle(
const native_handle_t* handle) {
if (handle == nullptr) {
return NN_ERROR() << "unknownHandleFromNativeHandle failed because handle is nullptr";
}
std::vector<base::unique_fd> fds =
NN_TRY(nn::dupFds(handle->data + 0, handle->data + handle->numFds));
std::vector<int> ints(handle->data + handle->numFds,
handle->data + handle->numFds + handle->numInts);
return nn::Memory::Unknown::Handle{.fds = std::move(fds), .ints = std::move(ints)};
}
} // anonymous namespace
nn::Capabilities::OperandPerformanceTable makeQuantized8PerformanceConsistentWithP(
@@ -331,147 +231,4 @@ nn::GeneralResult<std::reference_wrapper<const nn::Request>> convertRequestFromP
return **maybeRequestInSharedOut;
}
nn::GeneralResult<std::vector<uint32_t>> countNumberOfConsumers(
size_t numberOfOperands, const std::vector<nn::Operation>& operations) {
return nn::countNumberOfConsumers(numberOfOperands, operations);
}
nn::GeneralResult<hidl_memory> createHidlMemoryFromSharedMemory(const nn::SharedMemory& memory) {
if (memory == nullptr) {
return NN_ERROR() << "Memory must be non-empty";
}
return std::visit([](const auto& x) { return createHidlMemoryFrom(x); }, memory->handle);
}
#ifdef __ANDROID__
static uint32_t roundUpToMultiple(uint32_t value, uint32_t multiple) {
return (value + multiple - 1) / multiple * multiple;
}
#endif // __ANDROID__
nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_memory& memory) {
CHECK_LE(memory.size(), std::numeric_limits<size_t>::max());
if (!memory.valid()) {
return NN_ERROR() << "Unable to convert invalid hidl_memory";
}
if (memory.name() == "ashmem") {
if (memory.handle()->numFds != 1) {
return NN_ERROR() << "Unable to convert invalid ashmem memory object with "
<< memory.handle()->numFds << " numFds, but expected 1";
}
if (memory.handle()->numInts != 0) {
return NN_ERROR() << "Unable to convert invalid ashmem memory object with "
<< memory.handle()->numInts << " numInts, but expected 0";
}
auto handle = nn::Memory::Ashmem{
.fd = NN_TRY(nn::dupFd(memory.handle()->data[0])),
.size = static_cast<size_t>(memory.size()),
};
return std::make_shared<const nn::Memory>(nn::Memory{.handle = std::move(handle)});
}
if (memory.name() == "mmap_fd") {
if (memory.handle()->numFds != 1) {
return NN_ERROR() << "Unable to convert invalid mmap_fd memory object with "
<< memory.handle()->numFds << " numFds, but expected 1";
}
if (memory.handle()->numInts != 3) {
return NN_ERROR() << "Unable to convert invalid mmap_fd memory object with "
<< memory.handle()->numInts << " numInts, but expected 3";
}
const int fd = memory.handle()->data[0];
const int prot = memory.handle()->data[1];
const int lower = memory.handle()->data[2];
const int higher = memory.handle()->data[3];
const size_t offset = nn::getOffsetFromInts(lower, higher);
return nn::createSharedMemoryFromFd(static_cast<size_t>(memory.size()), prot, fd, offset);
}
if (memory.name() != "hardware_buffer_blob") {
auto handle = nn::Memory::Unknown{
.handle = NN_TRY(unknownHandleFromNativeHandle(memory.handle())),
.size = static_cast<size_t>(memory.size()),
.name = memory.name(),
};
return std::make_shared<const nn::Memory>(nn::Memory{.handle = std::move(handle)});
}
#ifdef __ANDROID__
const auto size = memory.size();
const auto format = AHARDWAREBUFFER_FORMAT_BLOB;
const auto usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
const uint32_t width = size;
const uint32_t height = 1; // height is always 1 for BLOB mode AHardwareBuffer.
const uint32_t layers = 1; // layers is always 1 for BLOB mode AHardwareBuffer.
// AHardwareBuffer_createFromHandle() might fail because an allocator
// expects a specific stride value. In that case, we try to guess it by
// aligning the width to small powers of 2.
// TODO(b/174120849): Avoid stride assumptions.
AHardwareBuffer* hardwareBuffer = nullptr;
status_t status = UNKNOWN_ERROR;
for (uint32_t alignment : {1, 4, 32, 64, 128, 2, 8, 16}) {
const uint32_t stride = roundUpToMultiple(width, alignment);
AHardwareBuffer_Desc desc{
.width = width,
.height = height,
.layers = layers,
.format = format,
.usage = usage,
.stride = stride,
};
status = AHardwareBuffer_createFromHandle(&desc, memory.handle(),
AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE,
&hardwareBuffer);
if (status == NO_ERROR) {
break;
}
}
if (status != NO_ERROR) {
return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
<< "Can't create AHardwareBuffer from handle. Error: " << status;
}
return nn::createSharedMemoryFromAHWB(hardwareBuffer, /*takeOwnership=*/true);
#else // __ANDROID__
LOG(FATAL) << "nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const "
"hidl_memory& memory): Not Available on Host Build";
return (NN_ERROR() << "createSharedMemoryFromHidlMemory failed")
.
operator nn::GeneralResult<nn::SharedMemory>();
#endif // __ANDROID__
}
nn::GeneralResult<hidl_handle> hidlHandleFromSharedHandle(const nn::Handle& handle) {
base::unique_fd fd = NN_TRY(nn::dupFd(handle.get()));
return createNativeHandleFrom(std::move(fd), {});
}
nn::GeneralResult<nn::Handle> sharedHandleFromNativeHandle(const native_handle_t* handle) {
if (handle == nullptr) {
return NN_ERROR() << "sharedHandleFromNativeHandle failed because handle is nullptr";
}
if (handle->numFds != 1 || handle->numInts != 0) {
return NN_ERROR() << "sharedHandleFromNativeHandle failed because handle does not only "
"hold a single fd";
}
return nn::dupFd(handle->data[0]);
}
nn::GeneralResult<hidl_vec<hidl_handle>> convertSyncFences(
const std::vector<nn::SyncFence>& syncFences) {
hidl_vec<hidl_handle> handles(syncFences.size());
for (size_t i = 0; i < syncFences.size(); ++i) {
const auto& handle = syncFences[i].getSharedHandle();
if (handle == nullptr) {
return NN_ERROR() << "convertSyncFences failed because sync fence is empty";
}
handles[i] = NN_TRY(hidlHandleFromSharedHandle(*handle));
}
return handles;
}
} // namespace android::hardware::neuralnetworks::utils