Merge "Making NN hardware interface libs "host supported""

This commit is contained in:
Treehugger Robot 2021-08-04 22:27:11 +00:00 committed by Gerrit Code Review
commit 7519960237
11 changed files with 133 additions and 30 deletions

View file

@ -44,6 +44,7 @@ cc_library_static {
cc_test { cc_test {
name: "neuralnetworks_utils_hal_1_0_test", name: "neuralnetworks_utils_hal_1_0_test",
host_supported: true,
srcs: ["test/*.cpp"], srcs: ["test/*.cpp"],
static_libs: [ static_libs: [
"android.hardware.neuralnetworks@1.0", "android.hardware.neuralnetworks@1.0",
@ -62,8 +63,12 @@ cc_test {
"libhidlbase", "libhidlbase",
"libhidlmemory", "libhidlmemory",
"liblog", "liblog",
"libnativewindow",
"libutils", "libutils",
], ],
target: {
android: {
shared_libs: ["libnativewindow"],
},
},
test_suites: ["general-tests"], test_suites: ["general-tests"],
} }

View file

@ -25,19 +25,10 @@ package {
cc_defaults { cc_defaults {
name: "neuralnetworks_vts_functional_defaults", name: "neuralnetworks_vts_functional_defaults",
defaults: ["VtsHalTargetTestDefaults"], defaults: [
arch: { "VtsHalTargetTestDefaults",
x86: { "neuralnetworks_float16",
cflags: [ "-D_Float16=__fp16", ],
"-Xclang", "-fnative-half-type",
"-Xclang", "-fallow-half-arguments-and-returns" ],
},
x86_64: {
cflags: [ "-D_Float16=__fp16",
"-Xclang", "-fnative-half-type",
"-Xclang", "-fallow-half-arguments-and-returns" ],
},
},
} }
cc_library_static { cc_library_static {
@ -83,6 +74,7 @@ cc_test {
"libnativewindow", "libnativewindow",
], ],
static_libs: [ static_libs: [
"VtsHalNeuralNetworksV1_0_utils",
"android.hardware.neuralnetworks@1.0", "android.hardware.neuralnetworks@1.0",
"android.hidl.allocator@1.0", "android.hidl.allocator@1.0",
"android.hidl.memory@1.0", "android.hidl.memory@1.0",
@ -90,7 +82,6 @@ cc_test {
"libhidlmemory", "libhidlmemory",
"libneuralnetworks_generated_test_harness", "libneuralnetworks_generated_test_harness",
"libneuralnetworks_utils", "libneuralnetworks_utils",
"VtsHalNeuralNetworksV1_0_utils",
], ],
whole_static_libs: [ whole_static_libs: [
"neuralnetworks_generated_V1_0_example", "neuralnetworks_generated_V1_0_example",
@ -98,5 +89,8 @@ cc_test {
header_libs: [ header_libs: [
"libneuralnetworks_headers", "libneuralnetworks_headers",
], ],
test_suites: ["general-tests", "vts"], test_suites: [
"general-tests",
"vts",
],
} }

View file

@ -46,6 +46,7 @@ cc_library_static {
cc_test { cc_test {
name: "neuralnetworks_utils_hal_1_1_test", name: "neuralnetworks_utils_hal_1_1_test",
host_supported: true,
srcs: ["test/*.cpp"], srcs: ["test/*.cpp"],
static_libs: [ static_libs: [
"android.hardware.neuralnetworks@1.0", "android.hardware.neuralnetworks@1.0",
@ -66,8 +67,12 @@ cc_test {
"libhidlbase", "libhidlbase",
"libhidlmemory", "libhidlmemory",
"liblog", "liblog",
"libnativewindow",
"libutils", "libutils",
], ],
target: {
android: {
shared_libs: ["libnativewindow"],
},
},
test_suites: ["general-tests"], test_suites: ["general-tests"],
} }

View file

@ -28,18 +28,19 @@ cc_test {
defaults: ["neuralnetworks_vts_functional_defaults"], defaults: ["neuralnetworks_vts_functional_defaults"],
srcs: [ srcs: [
"BasicTests.cpp", "BasicTests.cpp",
"GeneratedTestHarness.cpp",
"TestAssertions.cpp", "TestAssertions.cpp",
"TestMain.cpp", "TestMain.cpp",
"ValidateModel.cpp", "ValidateModel.cpp",
"ValidateRequest.cpp", "ValidateRequest.cpp",
"VtsHalNeuralnetworks.cpp", "VtsHalNeuralnetworks.cpp",
"GeneratedTestHarness.cpp",
], ],
shared_libs: [ shared_libs: [
"libfmq", "libfmq",
"libnativewindow", "libnativewindow",
], ],
static_libs: [ static_libs: [
"VtsHalNeuralNetworksV1_0_utils",
"android.hardware.neuralnetworks@1.0", "android.hardware.neuralnetworks@1.0",
"android.hardware.neuralnetworks@1.1", "android.hardware.neuralnetworks@1.1",
"android.hidl.allocator@1.0", "android.hidl.allocator@1.0",
@ -48,7 +49,6 @@ cc_test {
"libhidlmemory", "libhidlmemory",
"libneuralnetworks_generated_test_harness", "libneuralnetworks_generated_test_harness",
"libneuralnetworks_utils", "libneuralnetworks_utils",
"VtsHalNeuralNetworksV1_0_utils",
], ],
whole_static_libs: [ whole_static_libs: [
"neuralnetworks_generated_V1_0_example", "neuralnetworks_generated_V1_0_example",
@ -57,5 +57,8 @@ cc_test {
header_libs: [ header_libs: [
"libneuralnetworks_headers", "libneuralnetworks_headers",
], ],
test_suites: ["general-tests", "vts"], test_suites: [
"general-tests",
"vts",
],
} }

View file

@ -50,10 +50,21 @@ cc_library_static {
cflags: ["-DNN_DEBUGGABLE"], cflags: ["-DNN_DEBUGGABLE"],
}, },
}, },
target: {
host: {
cflags: [
"-D__INTRODUCED_IN(x)=",
"-D__assert(a,b,c)=",
// We want all the APIs to be available on the host.
"-D__ANDROID_API__=10000",
],
},
},
} }
cc_test { cc_test {
name: "neuralnetworks_utils_hal_1_2_test", name: "neuralnetworks_utils_hal_1_2_test",
host_supported: true,
srcs: ["test/*.cpp"], srcs: ["test/*.cpp"],
static_libs: [ static_libs: [
"android.hardware.neuralnetworks@1.0", "android.hardware.neuralnetworks@1.0",
@ -76,8 +87,12 @@ cc_test {
"libhidlbase", "libhidlbase",
"libhidlmemory", "libhidlmemory",
"liblog", "liblog",
"libnativewindow",
"libutils", "libutils",
], ],
target: {
android: {
shared_libs: ["libnativewindow"],
},
},
test_suites: ["general-tests"], test_suites: ["general-tests"],
} }

View file

@ -47,10 +47,21 @@ cc_library_static {
export_static_lib_headers: [ export_static_lib_headers: [
"neuralnetworks_utils_hal_common", "neuralnetworks_utils_hal_common",
], ],
target: {
host: {
cflags: [
"-D__INTRODUCED_IN(x)=",
"-D__assert(a,b,c)=",
// We want all the APIs to be available on the host.
"-D__ANDROID_API__=10000",
],
},
},
} }
cc_test { cc_test {
name: "neuralnetworks_utils_hal_1_3_test", name: "neuralnetworks_utils_hal_1_3_test",
host_supported: true,
srcs: ["test/*.cpp"], srcs: ["test/*.cpp"],
static_libs: [ static_libs: [
"android.hardware.neuralnetworks@1.0", "android.hardware.neuralnetworks@1.0",
@ -75,8 +86,12 @@ cc_test {
"libhidlbase", "libhidlbase",
"libhidlmemory", "libhidlmemory",
"liblog", "liblog",
"libnativewindow",
"libutils", "libutils",
], ],
target: {
android: {
shared_libs: ["libnativewindow"],
},
},
test_suites: ["general-tests"], test_suites: ["general-tests"],
} }

View file

@ -9,6 +9,7 @@ package {
aidl_interface { aidl_interface {
name: "android.hardware.neuralnetworks", name: "android.hardware.neuralnetworks",
host_supported: true,
vendor_available: true, vendor_available: true,
srcs: [ srcs: [
"android/hardware/neuralnetworks/*.aidl", "android/hardware/neuralnetworks/*.aidl",

View file

@ -41,8 +41,12 @@ cc_library_static {
"android.hardware.neuralnetworks-V1-ndk", "android.hardware.neuralnetworks-V1-ndk",
"libbinder_ndk", "libbinder_ndk",
"libhidlbase", "libhidlbase",
"libnativewindow",
], ],
target: {
android: {
shared_libs: ["libnativewindow"],
},
},
} }
cc_test { cc_test {
@ -70,9 +74,21 @@ cc_test {
"libhidlbase", "libhidlbase",
"libhidlmemory", "libhidlmemory",
"liblog", "liblog",
"libnativewindow",
"libutils", "libutils",
], ],
target: {
android: {
shared_libs: ["libnativewindow"],
},
host: {
cflags: [
"-D__INTRODUCED_IN(x)=",
"-D__assert(a,b,c)=",
// We want all the APIs to be available on the host.
"-D__ANDROID_API__=10000",
],
},
},
cflags: [ cflags: [
/* GMOCK defines functions for printing all MOCK_DEVICE arguments and /* GMOCK defines functions for printing all MOCK_DEVICE arguments and
* MockDevice contains a string pointer which triggers a warning in the * MockDevice contains a string pointer which triggers a warning in the

View file

@ -25,7 +25,6 @@
#include <android-base/mapped_file.h> #include <android-base/mapped_file.h>
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#include <android/binder_auto_utils.h> #include <android/binder_auto_utils.h>
#include <android/hardware_buffer.h>
#include <cutils/native_handle.h> #include <cutils/native_handle.h>
#include <nnapi/OperandTypes.h> #include <nnapi/OperandTypes.h>
#include <nnapi/OperationTypes.h> #include <nnapi/OperationTypes.h>
@ -36,7 +35,6 @@
#include <nnapi/Validation.h> #include <nnapi/Validation.h>
#include <nnapi/hal/CommonUtils.h> #include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h> #include <nnapi/hal/HandleError.h>
#include <vndk/hardware_buffer.h>
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
@ -48,6 +46,11 @@
#include "Utils.h" #include "Utils.h"
#ifdef __ANDROID__
#include <android/hardware_buffer.h>
#include <vndk/hardware_buffer.h>
#endif // __ANDROID__
#define VERIFY_NON_NEGATIVE(value) \ #define VERIFY_NON_NEGATIVE(value) \
while (UNLIKELY(value < 0)) return NN_ERROR() while (UNLIKELY(value < 0)) return NN_ERROR()
@ -119,6 +122,7 @@ struct NativeHandleDeleter {
using UniqueNativeHandle = std::unique_ptr<native_handle_t, NativeHandleDeleter>; using UniqueNativeHandle = std::unique_ptr<native_handle_t, NativeHandleDeleter>;
#ifdef __ANDROID__
GeneralResult<UniqueNativeHandle> nativeHandleFromAidlHandle(const NativeHandle& handle) { GeneralResult<UniqueNativeHandle> nativeHandleFromAidlHandle(const NativeHandle& handle) {
auto nativeHandle = UniqueNativeHandle(dupFromAidl(handle)); auto nativeHandle = UniqueNativeHandle(dupFromAidl(handle));
if (nativeHandle.get() == nullptr) { if (nativeHandle.get() == nullptr) {
@ -131,6 +135,7 @@ GeneralResult<UniqueNativeHandle> nativeHandleFromAidlHandle(const NativeHandle&
} }
return nativeHandle; return nativeHandle;
} }
#endif // __ANDROID__
} // anonymous namespace } // anonymous namespace
@ -371,6 +376,7 @@ GeneralResult<SharedMemory> unvalidatedConvert(const aidl_hal::Memory& memory) {
return createSharedMemoryFromFd(size, prot, fd, offset); return createSharedMemoryFromFd(size, prot, fd, offset);
} }
case Tag::hardwareBuffer: { case Tag::hardwareBuffer: {
#ifdef __ANDROID__
const auto& hardwareBuffer = memory.get<Tag::hardwareBuffer>(); const auto& hardwareBuffer = memory.get<Tag::hardwareBuffer>();
const UniqueNativeHandle handle = const UniqueNativeHandle handle =
@ -393,6 +399,11 @@ GeneralResult<SharedMemory> unvalidatedConvert(const aidl_hal::Memory& memory) {
} }
return createSharedMemoryFromAHWB(ahwb, /*takeOwnership=*/true); return createSharedMemoryFromAHWB(ahwb, /*takeOwnership=*/true);
#else // __ANDROID__
LOG(FATAL) << "GeneralResult<SharedMemory> unvalidatedConvert(const aidl_hal::Memory& "
"memory): Not Available on Host Build";
return NN_ERROR() << "createFromHandle failed";
#endif // __ANDROID__
} }
} }
return NN_ERROR() << "Unrecognized Memory::Tag: " << memory.getTag(); return NN_ERROR() << "Unrecognized Memory::Tag: " << memory.getTag();
@ -612,6 +623,7 @@ struct overloaded : Ts... {
template <class... Ts> template <class... Ts>
overloaded(Ts...)->overloaded<Ts...>; overloaded(Ts...)->overloaded<Ts...>;
#ifdef __ANDROID__
nn::GeneralResult<common::NativeHandle> aidlHandleFromNativeHandle( nn::GeneralResult<common::NativeHandle> aidlHandleFromNativeHandle(
const native_handle_t& nativeHandle) { const native_handle_t& nativeHandle) {
auto handle = ::android::dupToAidl(&nativeHandle); auto handle = ::android::dupToAidl(&nativeHandle);
@ -621,6 +633,7 @@ nn::GeneralResult<common::NativeHandle> aidlHandleFromNativeHandle(
} }
return handle; return handle;
} }
#endif // __ANDROID__
nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::Ashmem& memory) { nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::Ashmem& memory) {
if constexpr (std::numeric_limits<size_t>::max() > std::numeric_limits<int64_t>::max()) { if constexpr (std::numeric_limits<size_t>::max() > std::numeric_limits<int64_t>::max()) {
@ -668,6 +681,7 @@ nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::Fd& memory) {
} }
nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::HardwareBuffer& memory) { nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::HardwareBuffer& memory) {
#ifdef __ANDROID__
const native_handle_t* nativeHandle = AHardwareBuffer_getNativeHandle(memory.handle.get()); const native_handle_t* nativeHandle = AHardwareBuffer_getNativeHandle(memory.handle.get());
if (nativeHandle == nullptr) { if (nativeHandle == nullptr) {
return (NN_ERROR() << "unvalidatedConvert failed because AHardwareBuffer_getNativeHandle " return (NN_ERROR() << "unvalidatedConvert failed because AHardwareBuffer_getNativeHandle "
@ -695,6 +709,12 @@ nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::HardwareBuffer& m
.handle = std::move(handle), .handle = std::move(handle),
}; };
return Memory::make<Memory::Tag::hardwareBuffer>(std::move(hardwareBuffer)); return Memory::make<Memory::Tag::hardwareBuffer>(std::move(hardwareBuffer));
#else // __ANDROID__
LOG(FATAL) << "nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::HardwareBuffer& "
"memory): Not Available on Host Build";
(void)memory;
return (NN_ERROR() << "unvalidatedConvert failed").operator nn::GeneralResult<Memory>();
#endif // __ANDROID__
} }
nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::Unknown& /*memory*/) { nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::Unknown& /*memory*/) {

View file

@ -37,13 +37,18 @@ cc_library_static {
shared_libs: [ shared_libs: [
"android.hardware.neuralnetworks-V1-ndk", "android.hardware.neuralnetworks-V1-ndk",
"libhidlbase", "libhidlbase",
"libnativewindow",
"libbinder_ndk", "libbinder_ndk",
], ],
target: {
android: {
shared_libs: ["libnativewindow"],
},
},
} }
cc_test { cc_test {
name: "neuralnetworks_utils_hal_common_test", name: "neuralnetworks_utils_hal_common_test",
host_supported: true,
srcs: ["test/*.cpp"], srcs: ["test/*.cpp"],
static_libs: [ static_libs: [
"android.hardware.neuralnetworks@1.0", "android.hardware.neuralnetworks@1.0",
@ -61,8 +66,12 @@ cc_test {
"libhidlbase", "libhidlbase",
"libhidlmemory", "libhidlmemory",
"liblog", "liblog",
"libnativewindow",
"libutils", "libutils",
], ],
target: {
android: {
shared_libs: ["libnativewindow"],
},
},
test_suites: ["general-tests"], test_suites: ["general-tests"],
} }

View file

@ -20,14 +20,12 @@
#include <android-base/logging.h> #include <android-base/logging.h>
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#include <android/hardware_buffer.h>
#include <hidl/HidlSupport.h> #include <hidl/HidlSupport.h>
#include <nnapi/Result.h> #include <nnapi/Result.h>
#include <nnapi/SharedMemory.h> #include <nnapi/SharedMemory.h>
#include <nnapi/TypeUtils.h> #include <nnapi/TypeUtils.h>
#include <nnapi/Types.h> #include <nnapi/Types.h>
#include <nnapi/Validation.h> #include <nnapi/Validation.h>
#include <vndk/hardware_buffer.h>
#include <algorithm> #include <algorithm>
#include <any> #include <any>
@ -36,6 +34,11 @@
#include <variant> #include <variant>
#include <vector> #include <vector>
#ifdef __ANDROID__
#include <android/hardware_buffer.h>
#include <vndk/hardware_buffer.h>
#endif // __ANDROID__
namespace android::hardware::neuralnetworks::utils { namespace android::hardware::neuralnetworks::utils {
namespace { namespace {
@ -139,6 +142,7 @@ nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Fd& memory
} }
nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::HardwareBuffer& memory) { nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::HardwareBuffer& memory) {
#ifdef __ANDROID__
const auto* ahwb = memory.handle.get(); const auto* ahwb = memory.handle.get();
AHardwareBuffer_Desc bufferDesc; AHardwareBuffer_Desc bufferDesc;
AHardwareBuffer_describe(ahwb, &bufferDesc); AHardwareBuffer_describe(ahwb, &bufferDesc);
@ -152,6 +156,12 @@ nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::HardwareBu
hidl_handle copiedHandle(hidlHandle); hidl_handle copiedHandle(hidlHandle);
return hidl_memory(name, std::move(copiedHandle), size); 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) { nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Unknown& memory) {
@ -333,9 +343,11 @@ nn::GeneralResult<hidl_memory> createHidlMemoryFromSharedMemory(const nn::Shared
return std::visit([](const auto& x) { return createHidlMemoryFrom(x); }, memory->handle); return std::visit([](const auto& x) { return createHidlMemoryFrom(x); }, memory->handle);
} }
#ifdef __ANDROID__
static uint32_t roundUpToMultiple(uint32_t value, uint32_t multiple) { static uint32_t roundUpToMultiple(uint32_t value, uint32_t multiple) {
return (value + multiple - 1) / multiple * multiple; return (value + multiple - 1) / multiple * multiple;
} }
#endif // __ANDROID__
nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_memory& memory) { nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_memory& memory) {
CHECK_LE(memory.size(), std::numeric_limits<size_t>::max()); CHECK_LE(memory.size(), std::numeric_limits<size_t>::max());
@ -387,6 +399,7 @@ nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_
return std::make_shared<const nn::Memory>(nn::Memory{.handle = std::move(handle)}); return std::make_shared<const nn::Memory>(nn::Memory{.handle = std::move(handle)});
} }
#ifdef __ANDROID__
const auto size = memory.size(); const auto size = memory.size();
const auto format = AHARDWAREBUFFER_FORMAT_BLOB; const auto format = AHARDWAREBUFFER_FORMAT_BLOB;
const auto usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN; const auto usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
@ -423,6 +436,13 @@ nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_
} }
return nn::createSharedMemoryFromAHWB(hardwareBuffer, /*takeOwnership=*/true); 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) { nn::GeneralResult<hidl_handle> hidlHandleFromSharedHandle(const nn::Handle& handle) {