Add canonical types adapters for NNAPI AIDL interface

Also:
* Add missing AIDL<->CT conversions
* Add AIDL-specific info to neuralnetworks/utils/README.md
* Add mock classes and tests AIDL adapters

Bug: 179015258
Test: neuralnetworks_utils_hal_test
Change-Id: Ifa98fadd46dca5dbf9b3ceb4da811aa8da45b6e4
Merged-In: Ifa98fadd46dca5dbf9b3ceb4da811aa8da45b6e4
(cherry picked from commit 3b93b0b56a)
This commit is contained in:
Lev Proleev
2021-01-26 19:40:20 +00:00
committed by Xusong Wang
parent 7b0b54697c
commit 900c28a250
28 changed files with 3257 additions and 43 deletions

View File

@@ -15,6 +15,9 @@
{
"name": "neuralnetworks_utils_hal_1_3_test"
},
{
"name": "neuralnetworks_utils_hal_aidl_test"
},
{
"name": "VtsHalNeuralnetworksV1_0TargetTest",
"options": [

View File

@@ -29,10 +29,12 @@ cc_library_static {
srcs: ["src/*"],
local_include_dirs: ["include/nnapi/hal/aidl/"],
export_include_dirs: ["include"],
cflags: ["-Wthread-safety"],
static_libs: [
"libarect",
"neuralnetworks_types",
"neuralnetworks_utils_hal_common",
"neuralnetworks_utils_hal_1_0",
],
shared_libs: [
"android.hardware.neuralnetworks-V1-ndk_platform",
@@ -41,3 +43,38 @@ cc_library_static {
"libnativewindow",
],
}
cc_test {
name: "neuralnetworks_utils_hal_aidl_test",
defaults: ["neuralnetworks_utils_defaults"],
srcs: [
"test/*.cpp",
],
static_libs: [
"android.hardware.common-V2-ndk_platform",
"android.hardware.neuralnetworks-V1-ndk_platform",
"libgmock",
"libneuralnetworks_common",
"neuralnetworks_types",
"neuralnetworks_utils_hal_aidl",
"neuralnetworks_utils_hal_common",
],
shared_libs: [
"android.hidl.allocator@1.0",
"libbase",
"libbinder_ndk",
"libcutils",
"libhidlbase",
"libhidlmemory",
"liblog",
"libnativewindow",
"libutils",
],
cflags: [
/* GMOCK defines functions for printing all MOCK_DEVICE arguments and
* MockDevice contains a string pointer which triggers a warning in the
* base logging library. */
"-Wno-user-defined-warnings",
],
test_suites: ["general-tests"],
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_BUFFER_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_BUFFER_H
#include <aidl/android/hardware/neuralnetworks/IBuffer.h>
#include <nnapi/IBuffer.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <memory>
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
// lifetimes across processes.
namespace aidl::android::hardware::neuralnetworks::utils {
// Class that adapts aidl_hal::IBuffer to nn::IBuffer.
class Buffer final : public nn::IBuffer {
struct PrivateConstructorTag {};
public:
static nn::GeneralResult<std::shared_ptr<const Buffer>> create(
std::shared_ptr<aidl_hal::IBuffer> buffer, nn::Request::MemoryDomainToken token);
Buffer(PrivateConstructorTag tag, std::shared_ptr<aidl_hal::IBuffer> buffer,
nn::Request::MemoryDomainToken token);
nn::Request::MemoryDomainToken getToken() const override;
nn::GeneralResult<void> copyTo(const nn::SharedMemory& dst) const override;
nn::GeneralResult<void> copyFrom(const nn::SharedMemory& src,
const nn::Dimensions& dimensions) const override;
private:
const std::shared_ptr<aidl_hal::IBuffer> kBuffer;
const nn::Request::MemoryDomainToken kToken;
};
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_BUFFER_H

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_CALLBACKS_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_CALLBACKS_H
#include <aidl/android/hardware/neuralnetworks/BnPreparedModelCallback.h>
#include <aidl/android/hardware/neuralnetworks/IDevice.h>
#include <nnapi/IPreparedModel.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/TransferValue.h>
#include <nnapi/hal/aidl/ProtectCallback.h>
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
// lifetimes across processes and for protecting asynchronous calls across AIDL.
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 {
public:
using Data = nn::GeneralResult<nn::SharedPreparedModel>;
ndk::ScopedAStatus notify(ErrorStatus status,
const std::shared_ptr<IPreparedModel>& preparedModel) override;
void notifyAsDeadObject() override;
Data get();
private:
hal::utils::TransferValue<Data> mData;
};
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_CALLBACKS_H

View File

@@ -46,6 +46,7 @@
#include <aidl/android/hardware/neuralnetworks/SymmPerChannelQuantParams.h>
#include <aidl/android/hardware/neuralnetworks/Timing.h>
#include <android/binder_auto_utils.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
@@ -96,7 +97,11 @@ GeneralResult<Extension::OperandTypeInformation> unvalidatedConvert(
const aidl_hal::ExtensionOperandTypeInformation& operandTypeInformation);
GeneralResult<SharedHandle> unvalidatedConvert(
const ::aidl::android::hardware::common::NativeHandle& handle);
GeneralResult<SyncFence> unvalidatedConvert(const ndk::ScopedFileDescriptor& syncFence);
GeneralResult<Capabilities> convert(const aidl_hal::Capabilities& capabilities);
GeneralResult<DeviceType> convert(const aidl_hal::DeviceType& deviceType);
GeneralResult<ErrorStatus> convert(const aidl_hal::ErrorStatus& errorStatus);
GeneralResult<ExecutionPreference> convert(
const aidl_hal::ExecutionPreference& executionPreference);
GeneralResult<SharedMemory> convert(const aidl_hal::Memory& memory);
@@ -106,9 +111,14 @@ GeneralResult<OperandType> convert(const aidl_hal::OperandType& operandType);
GeneralResult<Priority> convert(const aidl_hal::Priority& priority);
GeneralResult<Request::MemoryPool> convert(const aidl_hal::RequestMemoryPool& memoryPool);
GeneralResult<Request> convert(const aidl_hal::Request& request);
GeneralResult<Timing> convert(const aidl_hal::Timing& timing);
GeneralResult<SyncFence> convert(const ndk::ScopedFileDescriptor& syncFence);
GeneralResult<std::vector<Extension>> convert(const std::vector<aidl_hal::Extension>& extension);
GeneralResult<std::vector<Operation>> convert(const std::vector<aidl_hal::Operation>& outputShapes);
GeneralResult<std::vector<SharedMemory>> convert(const std::vector<aidl_hal::Memory>& memories);
GeneralResult<std::vector<OutputShape>> convert(
const std::vector<aidl_hal::OutputShape>& outputShapes);
GeneralResult<std::vector<uint32_t>> toUnsigned(const std::vector<int32_t>& vec);
@@ -118,14 +128,62 @@ namespace aidl::android::hardware::neuralnetworks::utils {
namespace nn = ::android::nn;
nn::GeneralResult<std::vector<uint8_t>> unvalidatedConvert(const nn::CacheToken& cacheToken);
nn::GeneralResult<BufferDesc> unvalidatedConvert(const nn::BufferDesc& bufferDesc);
nn::GeneralResult<BufferRole> unvalidatedConvert(const nn::BufferRole& bufferRole);
nn::GeneralResult<bool> unvalidatedConvert(const nn::MeasureTiming& measureTiming);
nn::GeneralResult<Memory> unvalidatedConvert(const nn::SharedMemory& memory);
nn::GeneralResult<OutputShape> unvalidatedConvert(const nn::OutputShape& outputShape);
nn::GeneralResult<ErrorStatus> unvalidatedConvert(const nn::ErrorStatus& errorStatus);
nn::GeneralResult<ExecutionPreference> unvalidatedConvert(
const nn::ExecutionPreference& executionPreference);
nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType);
nn::GeneralResult<OperandLifeTime> unvalidatedConvert(const nn::Operand::LifeTime& operandLifeTime);
nn::GeneralResult<DataLocation> unvalidatedConvert(const nn::DataLocation& location);
nn::GeneralResult<std::optional<OperandExtraParams>> unvalidatedConvert(
const nn::Operand::ExtraParams& extraParams);
nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand);
nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType);
nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation);
nn::GeneralResult<Subgraph> unvalidatedConvert(const nn::Model::Subgraph& subgraph);
nn::GeneralResult<std::vector<uint8_t>> unvalidatedConvert(
const nn::Model::OperandValues& operandValues);
nn::GeneralResult<ExtensionNameAndPrefix> unvalidatedConvert(
const nn::Model::ExtensionNameAndPrefix& extensionNameToPrefix);
nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model);
nn::GeneralResult<Priority> unvalidatedConvert(const nn::Priority& priority);
nn::GeneralResult<Request> unvalidatedConvert(const nn::Request& request);
nn::GeneralResult<RequestArgument> unvalidatedConvert(const nn::Request::Argument& requestArgument);
nn::GeneralResult<RequestMemoryPool> unvalidatedConvert(const nn::Request::MemoryPool& memoryPool);
nn::GeneralResult<Timing> unvalidatedConvert(const nn::Timing& timing);
nn::GeneralResult<int64_t> unvalidatedConvert(const nn::Duration& duration);
nn::GeneralResult<int64_t> unvalidatedConvert(const nn::OptionalDuration& optionalDuration);
nn::GeneralResult<int64_t> unvalidatedConvert(const nn::OptionalTimePoint& optionalTimePoint);
nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SyncFence& syncFence);
nn::GeneralResult<common::NativeHandle> unvalidatedConvert(const nn::SharedHandle& sharedHandle);
nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvertCache(
const nn::SharedHandle& handle);
nn::GeneralResult<std::vector<uint8_t>> convert(const nn::CacheToken& cacheToken);
nn::GeneralResult<BufferDesc> convert(const nn::BufferDesc& bufferDesc);
nn::GeneralResult<bool> convert(const nn::MeasureTiming& measureTiming);
nn::GeneralResult<Memory> convert(const nn::SharedMemory& memory);
nn::GeneralResult<ErrorStatus> convert(const nn::ErrorStatus& errorStatus);
nn::GeneralResult<ExecutionPreference> convert(const nn::ExecutionPreference& executionPreference);
nn::GeneralResult<Model> convert(const nn::Model& model);
nn::GeneralResult<Priority> convert(const nn::Priority& priority);
nn::GeneralResult<Request> convert(const nn::Request& request);
nn::GeneralResult<Timing> convert(const nn::Timing& timing);
nn::GeneralResult<int64_t> convert(const nn::OptionalDuration& optionalDuration);
nn::GeneralResult<int64_t> convert(const nn::OptionalTimePoint& optionalTimePoint);
nn::GeneralResult<std::vector<BufferRole>> convert(const std::vector<nn::BufferRole>& bufferRoles);
nn::GeneralResult<std::vector<OutputShape>> convert(
const std::vector<nn::OutputShape>& outputShapes);
nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert(
const std::vector<nn::SharedHandle>& handles);
nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert(
const std::vector<nn::SyncFence>& syncFences);
nn::GeneralResult<std::vector<int32_t>> toSigned(const std::vector<uint32_t>& vec);

View File

@@ -0,0 +1,98 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_DEVICE_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_DEVICE_H
#include <aidl/android/hardware/neuralnetworks/IDevice.h>
#include <nnapi/IBuffer.h>
#include <nnapi/IDevice.h>
#include <nnapi/OperandTypes.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/aidl/ProtectCallback.h>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
// lifetimes across processes and for protecting asynchronous calls across AIDL.
namespace aidl::android::hardware::neuralnetworks::utils {
// Class that adapts aidl_hal::IDevice to nn::IDevice.
class Device final : public nn::IDevice {
struct PrivateConstructorTag {};
public:
static nn::GeneralResult<std::shared_ptr<const Device>> create(
std::string name, std::shared_ptr<aidl_hal::IDevice> device);
Device(PrivateConstructorTag tag, std::string name, std::string versionString,
nn::DeviceType deviceType, std::vector<nn::Extension> extensions,
nn::Capabilities capabilities, std::pair<uint32_t, uint32_t> numberOfCacheFilesNeeded,
std::shared_ptr<aidl_hal::IDevice> device, DeathHandler deathHandler);
const std::string& getName() const override;
const std::string& getVersionString() const override;
nn::Version getFeatureLevel() const override;
nn::DeviceType getType() const override;
bool isUpdatable() const override;
const std::vector<nn::Extension>& getSupportedExtensions() const override;
const nn::Capabilities& getCapabilities() const override;
std::pair<uint32_t, uint32_t> getNumberOfCacheFilesNeeded() const override;
nn::GeneralResult<void> wait() const override;
nn::GeneralResult<std::vector<bool>> getSupportedOperations(
const nn::Model& model) const override;
nn::GeneralResult<nn::SharedPreparedModel> prepareModel(
const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
const std::vector<nn::SharedHandle>& dataCache,
const nn::CacheToken& token) const override;
nn::GeneralResult<nn::SharedPreparedModel> prepareModelFromCache(
nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
const std::vector<nn::SharedHandle>& dataCache,
const nn::CacheToken& token) const override;
nn::GeneralResult<nn::SharedBuffer> allocate(
const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
const std::vector<nn::BufferRole>& inputRoles,
const std::vector<nn::BufferRole>& outputRoles) const override;
DeathMonitor* getDeathMonitor() const;
private:
const std::string kName;
const std::string kVersionString;
const nn::DeviceType kDeviceType;
const std::vector<nn::Extension> kExtensions;
const nn::Capabilities kCapabilities;
const std::pair<uint32_t, uint32_t> kNumberOfCacheFilesNeeded;
const std::shared_ptr<aidl_hal::IDevice> kDevice;
const DeathHandler kDeathHandler;
};
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_DEVICE_H

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PREPARED_MODEL_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PREPARED_MODEL_H
#include <aidl/android/hardware/neuralnetworks/IPreparedModel.h>
#include <nnapi/IPreparedModel.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/aidl/ProtectCallback.h>
#include <memory>
#include <tuple>
#include <utility>
#include <vector>
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
// lifetimes across processes and for protecting asynchronous calls across AIDL.
namespace aidl::android::hardware::neuralnetworks::utils {
// Class that adapts aidl_hal::IPreparedModel to nn::IPreparedModel.
class PreparedModel final : public nn::IPreparedModel,
public std::enable_shared_from_this<PreparedModel> {
struct PrivateConstructorTag {};
public:
static nn::GeneralResult<std::shared_ptr<const PreparedModel>> create(
std::shared_ptr<aidl_hal::IPreparedModel> preparedModel);
PreparedModel(PrivateConstructorTag tag,
std::shared_ptr<aidl_hal::IPreparedModel> preparedModel);
nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> execute(
const nn::Request& request, nn::MeasureTiming measure,
const nn::OptionalTimePoint& deadline,
const nn::OptionalDuration& loopTimeoutDuration) const override;
nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> executeFenced(
const nn::Request& request, const std::vector<nn::SyncFence>& waitFor,
nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline,
const nn::OptionalDuration& loopTimeoutDuration,
const nn::OptionalDuration& timeoutDurationAfterFence) const override;
nn::GeneralResult<nn::SharedBurst> configureExecutionBurst() const override;
std::any getUnderlyingResource() const override;
private:
const std::shared_ptr<aidl_hal::IPreparedModel> kPreparedModel;
};
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PREPARED_MODEL_H

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PROTECT_CALLBACK_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PROTECT_CALLBACK_H
#include <android-base/scopeguard.h>
#include <android-base/thread_annotations.h>
#include <android/binder_interface_utils.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/ProtectCallback.h>
#include <functional>
#include <mutex>
#include <vector>
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
// lifetimes across processes and for protecting asynchronous calls across AIDL.
namespace aidl::android::hardware::neuralnetworks::utils {
// 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;
// Precondition: `killable` must be non-null.
void remove(hal::utils::IProtectedCallback* killable) const;
private:
mutable std::mutex mMutex;
mutable std::vector<hal::utils::IProtectedCallback*> mObjects GUARDED_BY(mMutex);
};
class DeathHandler final {
public:
static nn::GeneralResult<DeathHandler> create(std::shared_ptr<ndk::ICInterface> object);
DeathHandler(const DeathHandler&) = delete;
DeathHandler(DeathHandler&&) noexcept = default;
DeathHandler& operator=(const DeathHandler&) = delete;
DeathHandler& operator=(DeathHandler&&) noexcept = delete;
~DeathHandler();
using Cleanup = std::function<void()>;
// Precondition: `killable` must be non-null.
[[nodiscard]] ::android::base::ScopeGuard<Cleanup> protectCallback(
hal::utils::IProtectedCallback* killable) const;
std::shared_ptr<DeathMonitor> getDeathMonitor() const { return kDeathMonitor; }
private:
DeathHandler(std::shared_ptr<ndk::ICInterface> object,
ndk::ScopedAIBinder_DeathRecipient deathRecipient,
std::shared_ptr<DeathMonitor> deathMonitor);
std::shared_ptr<ndk::ICInterface> kObject;
ndk::ScopedAIBinder_DeathRecipient kDeathRecipient;
std::shared_ptr<DeathMonitor> kDeathMonitor;
};
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PROTECT_CALLBACK_H

View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_SERVICE_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_SERVICE_H
#include <nnapi/IDevice.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <string>
namespace aidl::android::hardware::neuralnetworks::utils {
nn::GeneralResult<nn::SharedDevice> getDevice(const std::string& name);
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_SERVICE_H

View File

@@ -23,6 +23,7 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/Validation.h>
#include <nnapi/hal/HandleError.h>
namespace aidl::android::hardware::neuralnetworks::utils {
@@ -52,6 +53,12 @@ nn::GeneralResult<Request> clone(const Request& request);
nn::GeneralResult<RequestMemoryPool> clone(const RequestMemoryPool& requestPool);
nn::GeneralResult<Model> clone(const Model& model);
nn::GeneralResult<void> handleTransportError(const ndk::ScopedAStatus& ret);
#define HANDLE_ASTATUS(ret) \
for (const auto status = handleTransportError(ret); !status.ok();) \
return NN_ERROR(status.error().code) << status.error().message << ": "
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_H

View File

@@ -0,0 +1,78 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Buffer.h"
#include <nnapi/IPreparedModel.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include "Conversions.h"
#include "Utils.h"
#include "nnapi/hal/aidl/Conversions.h"
#include <memory>
#include <utility>
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
// lifetimes across processes.
namespace aidl::android::hardware::neuralnetworks::utils {
nn::GeneralResult<std::shared_ptr<const Buffer>> Buffer::create(
std::shared_ptr<aidl_hal::IBuffer> buffer, nn::Request::MemoryDomainToken token) {
if (buffer == nullptr) {
return NN_ERROR() << "aidl_hal::utils::Buffer::create must have non-null buffer";
}
if (token == static_cast<nn::Request::MemoryDomainToken>(0)) {
return NN_ERROR() << "aidl_hal::utils::Buffer::create must have non-zero token";
}
return std::make_shared<const Buffer>(PrivateConstructorTag{}, std::move(buffer), token);
}
Buffer::Buffer(PrivateConstructorTag /*tag*/, std::shared_ptr<aidl_hal::IBuffer> buffer,
nn::Request::MemoryDomainToken token)
: kBuffer(std::move(buffer)), kToken(token) {
CHECK(kBuffer != nullptr);
CHECK(kToken != static_cast<nn::Request::MemoryDomainToken>(0));
}
nn::Request::MemoryDomainToken Buffer::getToken() const {
return kToken;
}
nn::GeneralResult<void> Buffer::copyTo(const nn::SharedMemory& dst) const {
const auto aidlDst = NN_TRY(convert(dst));
const auto ret = kBuffer->copyTo(aidlDst);
HANDLE_ASTATUS(ret) << "IBuffer::copyTo failed";
return {};
}
nn::GeneralResult<void> Buffer::copyFrom(const nn::SharedMemory& src,
const nn::Dimensions& dimensions) const {
const auto aidlSrc = NN_TRY(convert(src));
const auto aidlDimensions = NN_TRY(toSigned(dimensions));
const auto ret = kBuffer->copyFrom(aidlSrc, aidlDimensions);
HANDLE_ASTATUS(ret) << "IBuffer::copyFrom failed";
return {};
}
} // namespace aidl::android::hardware::neuralnetworks::utils

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Callbacks.h"
#include "Conversions.h"
#include "PreparedModel.h"
#include "ProtectCallback.h"
#include "Utils.h"
#include <nnapi/IPreparedModel.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <utility>
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
// lifetimes across processes and for protecting asynchronous calls across AIDL.
namespace aidl::android::hardware::neuralnetworks::utils {
namespace {
// Converts the results of IDevice::prepareModel* to the NN canonical format. On success, this
// function returns with a non-null nn::SharedPreparedModel with a feature level of
// 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);
return NN_TRY(PreparedModel::create(preparedModel));
}
} // namespace
ndk::ScopedAStatus PreparedModelCallback::notify(
ErrorStatus status, const std::shared_ptr<IPreparedModel>& preparedModel) {
mData.put(prepareModelCallback(status, preparedModel));
return ndk::ScopedAStatus::ok();
}
void PreparedModelCallback::notifyAsDeadObject() {
mData.put(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object");
}
PreparedModelCallback::Data PreparedModelCallback::get() {
return mData.take();
}
} // namespace aidl::android::hardware::neuralnetworks::utils

View File

@@ -18,6 +18,8 @@
#include <aidl/android/hardware/common/NativeHandle.h>
#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <android/binder_auto_utils.h>
#include <android/hardware_buffer.h>
#include <cutils/native_handle.h>
#include <nnapi/OperandTypes.h>
@@ -42,14 +44,17 @@
#define VERIFY_NON_NEGATIVE(value) \
while (UNLIKELY(value < 0)) return NN_ERROR()
namespace {
#define VERIFY_LE_INT32_MAX(value) \
while (UNLIKELY(value > std::numeric_limits<int32_t>::max())) return NN_ERROR()
namespace {
template <typename Type>
constexpr std::underlying_type_t<Type> underlyingType(Type value) {
return static_cast<std::underlying_type_t<Type>>(value);
}
constexpr auto kVersion = android::nn::Version::ANDROID_S;
constexpr int64_t kNoTiming = -1;
} // namespace
@@ -134,13 +139,8 @@ GeneralResult<Handle> unvalidatedConvertHelper(const NativeHandle& aidlNativeHan
std::vector<base::unique_fd> fds;
fds.reserve(aidlNativeHandle.fds.size());
for (const auto& fd : aidlNativeHandle.fds) {
const int dupFd = dup(fd.get());
if (dupFd == -1) {
// TODO(b/120417090): is ANEURALNETWORKS_UNEXPECTED_NULL the correct error to return
// here?
return NN_ERROR() << "Failed to dup the fd";
}
fds.emplace_back(dupFd);
auto duplicatedFd = NN_TRY(dupFd(fd.get()));
fds.emplace_back(duplicatedFd.release());
}
return Handle{.fds = std::move(fds), .ints = aidlNativeHandle.ints};
@@ -157,16 +157,12 @@ struct NativeHandleDeleter {
using UniqueNativeHandle = std::unique_ptr<native_handle_t, NativeHandleDeleter>;
static nn::GeneralResult<UniqueNativeHandle> nativeHandleFromAidlHandle(
const NativeHandle& handle) {
static GeneralResult<UniqueNativeHandle> nativeHandleFromAidlHandle(const NativeHandle& handle) {
std::vector<base::unique_fd> fds;
fds.reserve(handle.fds.size());
for (const auto& fd : handle.fds) {
const int dupFd = dup(fd.get());
if (dupFd == -1) {
return NN_ERROR() << "Failed to dup the fd";
}
fds.emplace_back(dupFd);
auto duplicatedFd = NN_TRY(dupFd(fd.get()));
fds.emplace_back(duplicatedFd.release());
}
constexpr size_t kIntMax = std::numeric_limits<int>::max();
@@ -382,14 +378,14 @@ static uint32_t roundUpToMultiple(uint32_t value, uint32_t multiple) {
GeneralResult<SharedMemory> unvalidatedConvert(const aidl_hal::Memory& memory) {
VERIFY_NON_NEGATIVE(memory.size) << "Memory size must not be negative";
if (memory.size > std::numeric_limits<uint32_t>::max()) {
if (memory.size > std::numeric_limits<size_t>::max()) {
return NN_ERROR() << "Memory: size must be <= std::numeric_limits<size_t>::max()";
}
if (memory.name != "hardware_buffer_blob") {
return std::make_shared<const Memory>(Memory{
.handle = NN_TRY(unvalidatedConvertHelper(memory.handle)),
.size = static_cast<uint32_t>(memory.size),
.size = static_cast<size_t>(memory.size),
.name = memory.name,
});
}
@@ -434,11 +430,28 @@ GeneralResult<SharedMemory> unvalidatedConvert(const aidl_hal::Memory& memory) {
return std::make_shared<const Memory>(Memory{
.handle = HardwareBufferHandle(hardwareBuffer, /*takeOwnership=*/true),
.size = static_cast<uint32_t>(memory.size),
.size = static_cast<size_t>(memory.size),
.name = memory.name,
});
}
GeneralResult<Timing> unvalidatedConvert(const aidl_hal::Timing& timing) {
if (timing.timeInDriver < -1) {
return NN_ERROR() << "Timing: timeInDriver must not be less than -1";
}
if (timing.timeOnDevice < -1) {
return NN_ERROR() << "Timing: timeOnDevice must not be less than -1";
}
constexpr auto convertTiming = [](int64_t halTiming) -> OptionalDuration {
if (halTiming == kNoTiming) {
return {};
}
return nn::Duration(static_cast<uint64_t>(halTiming));
};
return Timing{.timeOnDevice = convertTiming(timing.timeOnDevice),
.timeInDriver = convertTiming(timing.timeInDriver)};
}
GeneralResult<Model::OperandValues> unvalidatedConvert(const std::vector<uint8_t>& operandValues) {
return Model::OperandValues(operandValues.data(), operandValues.size());
}
@@ -515,6 +528,23 @@ GeneralResult<SharedHandle> unvalidatedConvert(const NativeHandle& aidlNativeHan
return std::make_shared<const Handle>(NN_TRY(unvalidatedConvertHelper(aidlNativeHandle)));
}
GeneralResult<SyncFence> unvalidatedConvert(const ndk::ScopedFileDescriptor& syncFence) {
auto duplicatedFd = NN_TRY(dupFd(syncFence.get()));
return SyncFence::create(std::move(duplicatedFd));
}
GeneralResult<Capabilities> convert(const aidl_hal::Capabilities& capabilities) {
return validatedConvert(capabilities);
}
GeneralResult<DeviceType> convert(const aidl_hal::DeviceType& deviceType) {
return validatedConvert(deviceType);
}
GeneralResult<ErrorStatus> convert(const aidl_hal::ErrorStatus& errorStatus) {
return validatedConvert(errorStatus);
}
GeneralResult<ExecutionPreference> convert(
const aidl_hal::ExecutionPreference& executionPreference) {
return validatedConvert(executionPreference);
@@ -548,6 +578,18 @@ GeneralResult<Request> convert(const aidl_hal::Request& request) {
return validatedConvert(request);
}
GeneralResult<Timing> convert(const aidl_hal::Timing& timing) {
return validatedConvert(timing);
}
GeneralResult<SyncFence> convert(const ndk::ScopedFileDescriptor& syncFence) {
return unvalidatedConvert(syncFence);
}
GeneralResult<std::vector<Extension>> convert(const std::vector<aidl_hal::Extension>& extension) {
return validatedConvert(extension);
}
GeneralResult<std::vector<Operation>> convert(const std::vector<aidl_hal::Operation>& operations) {
return unvalidatedConvert(operations);
}
@@ -556,6 +598,11 @@ GeneralResult<std::vector<SharedMemory>> convert(const std::vector<aidl_hal::Mem
return validatedConvert(memories);
}
GeneralResult<std::vector<OutputShape>> convert(
const std::vector<aidl_hal::OutputShape>& outputShapes) {
return validatedConvert(outputShapes);
}
GeneralResult<std::vector<uint32_t>> toUnsigned(const std::vector<int32_t>& vec) {
if (!std::all_of(vec.begin(), vec.end(), [](int32_t v) { return v >= 0; })) {
return NN_ERROR() << "Negative value passed to conversion from signed to unsigned";
@@ -575,13 +622,20 @@ using UnvalidatedConvertOutput =
template <typename Type>
nn::GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> unvalidatedConvertVec(
const std::vector<Type>& arguments) {
std::vector<UnvalidatedConvertOutput<Type>> halObject(arguments.size());
for (size_t i = 0; i < arguments.size(); ++i) {
halObject[i] = NN_TRY(unvalidatedConvert(arguments[i]));
std::vector<UnvalidatedConvertOutput<Type>> halObject;
halObject.reserve(arguments.size());
for (const auto& argument : arguments) {
halObject.push_back(NN_TRY(unvalidatedConvert(argument)));
}
return halObject;
}
template <typename Type>
nn::GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> unvalidatedConvert(
const std::vector<Type>& arguments) {
return unvalidatedConvertVec(arguments);
}
template <typename Type>
nn::GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& canonical) {
const auto maybeVersion = nn::validate(canonical);
@@ -609,29 +663,29 @@ nn::GeneralResult<common::NativeHandle> unvalidatedConvert(const nn::Handle& han
common::NativeHandle aidlNativeHandle;
aidlNativeHandle.fds.reserve(handle.fds.size());
for (const auto& fd : handle.fds) {
const int dupFd = dup(fd.get());
if (dupFd == -1) {
// TODO(b/120417090): is ANEURALNETWORKS_UNEXPECTED_NULL the correct error to return
// here?
return NN_ERROR() << "Failed to dup the fd";
}
aidlNativeHandle.fds.emplace_back(dupFd);
auto duplicatedFd = NN_TRY(nn::dupFd(fd.get()));
aidlNativeHandle.fds.emplace_back(duplicatedFd.release());
}
aidlNativeHandle.ints = handle.ints;
return aidlNativeHandle;
}
// Helper template for std::visit
template <class... Ts>
struct overloaded : Ts... {
using Ts::operator()...;
};
template <class... Ts>
overloaded(Ts...)->overloaded<Ts...>;
static nn::GeneralResult<common::NativeHandle> aidlHandleFromNativeHandle(
const native_handle_t& handle) {
common::NativeHandle aidlNativeHandle;
aidlNativeHandle.fds.reserve(handle.numFds);
for (int i = 0; i < handle.numFds; ++i) {
const int dupFd = dup(handle.data[i]);
if (dupFd == -1) {
return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to dup the fd";
}
aidlNativeHandle.fds.emplace_back(dupFd);
auto duplicatedFd = NN_TRY(nn::dupFd(handle.data[i]));
aidlNativeHandle.fds.emplace_back(duplicatedFd.release());
}
aidlNativeHandle.ints = std::vector<int>(&handle.data[handle.numFds],
@@ -642,6 +696,30 @@ static nn::GeneralResult<common::NativeHandle> aidlHandleFromNativeHandle(
} // namespace
nn::GeneralResult<std::vector<uint8_t>> unvalidatedConvert(const nn::CacheToken& cacheToken) {
return std::vector<uint8_t>(cacheToken.begin(), cacheToken.end());
}
nn::GeneralResult<BufferDesc> unvalidatedConvert(const nn::BufferDesc& bufferDesc) {
return BufferDesc{.dimensions = NN_TRY(toSigned(bufferDesc.dimensions))};
}
nn::GeneralResult<BufferRole> unvalidatedConvert(const nn::BufferRole& bufferRole) {
VERIFY_LE_INT32_MAX(bufferRole.modelIndex)
<< "BufferRole: modelIndex must be <= std::numeric_limits<int32_t>::max()";
VERIFY_LE_INT32_MAX(bufferRole.ioIndex)
<< "BufferRole: ioIndex must be <= std::numeric_limits<int32_t>::max()";
return BufferRole{
.modelIndex = static_cast<int32_t>(bufferRole.modelIndex),
.ioIndex = static_cast<int32_t>(bufferRole.ioIndex),
.frequency = bufferRole.frequency,
};
}
nn::GeneralResult<bool> unvalidatedConvert(const nn::MeasureTiming& measureTiming) {
return measureTiming == nn::MeasureTiming::YES;
}
nn::GeneralResult<common::NativeHandle> unvalidatedConvert(const nn::SharedHandle& sharedHandle) {
CHECK(sharedHandle != nullptr);
return unvalidatedConvert(*sharedHandle);
@@ -707,6 +785,230 @@ nn::GeneralResult<OutputShape> unvalidatedConvert(const nn::OutputShape& outputS
.isSufficient = outputShape.isSufficient};
}
nn::GeneralResult<ExecutionPreference> unvalidatedConvert(
const nn::ExecutionPreference& executionPreference) {
return static_cast<ExecutionPreference>(executionPreference);
}
nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType) {
return static_cast<OperandType>(operandType);
}
nn::GeneralResult<OperandLifeTime> unvalidatedConvert(
const nn::Operand::LifeTime& operandLifeTime) {
return static_cast<OperandLifeTime>(operandLifeTime);
}
nn::GeneralResult<DataLocation> unvalidatedConvert(const nn::DataLocation& location) {
VERIFY_LE_INT32_MAX(location.poolIndex)
<< "DataLocation: pool index must be <= std::numeric_limits<int32_t>::max()";
return DataLocation{
.poolIndex = static_cast<int32_t>(location.poolIndex),
.offset = static_cast<int64_t>(location.offset),
.length = static_cast<int64_t>(location.length),
};
}
nn::GeneralResult<std::optional<OperandExtraParams>> unvalidatedConvert(
const nn::Operand::ExtraParams& extraParams) {
return std::visit(
overloaded{
[](const nn::Operand::NoParams&)
-> nn::GeneralResult<std::optional<OperandExtraParams>> {
return std::nullopt;
},
[](const nn::Operand::SymmPerChannelQuantParams& symmPerChannelQuantParams)
-> nn::GeneralResult<std::optional<OperandExtraParams>> {
if (symmPerChannelQuantParams.channelDim >
std::numeric_limits<int32_t>::max()) {
// Using explicit type conversion because std::optional in successful
// result confuses the compiler.
return (NN_ERROR() << "symmPerChannelQuantParams.channelDim must be <= "
"std::numeric_limits<int32_t>::max(), received: "
<< symmPerChannelQuantParams.channelDim)
.
operator nn::GeneralResult<std::optional<OperandExtraParams>>();
}
return OperandExtraParams::make<OperandExtraParams::Tag::channelQuant>(
SymmPerChannelQuantParams{
.scales = symmPerChannelQuantParams.scales,
.channelDim = static_cast<int32_t>(
symmPerChannelQuantParams.channelDim),
});
},
[](const nn::Operand::ExtensionParams& extensionParams)
-> nn::GeneralResult<std::optional<OperandExtraParams>> {
return OperandExtraParams::make<OperandExtraParams::Tag::extension>(
extensionParams);
},
},
extraParams);
}
nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand) {
return Operand{
.type = NN_TRY(unvalidatedConvert(operand.type)),
.dimensions = NN_TRY(toSigned(operand.dimensions)),
.scale = operand.scale,
.zeroPoint = operand.zeroPoint,
.lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)),
.location = NN_TRY(unvalidatedConvert(operand.location)),
.extraParams = NN_TRY(unvalidatedConvert(operand.extraParams)),
};
}
nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType) {
return static_cast<OperationType>(operationType);
}
nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation) {
return Operation{
.type = NN_TRY(unvalidatedConvert(operation.type)),
.inputs = NN_TRY(toSigned(operation.inputs)),
.outputs = NN_TRY(toSigned(operation.outputs)),
};
}
nn::GeneralResult<Subgraph> unvalidatedConvert(const nn::Model::Subgraph& subgraph) {
return Subgraph{
.operands = NN_TRY(unvalidatedConvert(subgraph.operands)),
.operations = NN_TRY(unvalidatedConvert(subgraph.operations)),
.inputIndexes = NN_TRY(toSigned(subgraph.inputIndexes)),
.outputIndexes = NN_TRY(toSigned(subgraph.outputIndexes)),
};
}
nn::GeneralResult<std::vector<uint8_t>> unvalidatedConvert(
const nn::Model::OperandValues& operandValues) {
return std::vector<uint8_t>(operandValues.data(), operandValues.data() + operandValues.size());
}
nn::GeneralResult<ExtensionNameAndPrefix> unvalidatedConvert(
const nn::Model::ExtensionNameAndPrefix& extensionNameToPrefix) {
return ExtensionNameAndPrefix{
.name = extensionNameToPrefix.name,
.prefix = extensionNameToPrefix.prefix,
};
}
nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) {
return Model{
.main = NN_TRY(unvalidatedConvert(model.main)),
.referenced = NN_TRY(unvalidatedConvert(model.referenced)),
.operandValues = NN_TRY(unvalidatedConvert(model.operandValues)),
.pools = NN_TRY(unvalidatedConvert(model.pools)),
.relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16,
.extensionNameToPrefix = NN_TRY(unvalidatedConvert(model.extensionNameToPrefix)),
};
}
nn::GeneralResult<Priority> unvalidatedConvert(const nn::Priority& priority) {
return static_cast<Priority>(priority);
}
nn::GeneralResult<Request> unvalidatedConvert(const nn::Request& request) {
return Request{
.inputs = NN_TRY(unvalidatedConvert(request.inputs)),
.outputs = NN_TRY(unvalidatedConvert(request.outputs)),
.pools = NN_TRY(unvalidatedConvert(request.pools)),
};
}
nn::GeneralResult<RequestArgument> unvalidatedConvert(
const nn::Request::Argument& requestArgument) {
if (requestArgument.lifetime == nn::Request::Argument::LifeTime::POINTER) {
return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
<< "Request cannot be unvalidatedConverted because it contains pointer-based memory";
}
const bool hasNoValue = requestArgument.lifetime == nn::Request::Argument::LifeTime::NO_VALUE;
return RequestArgument{
.hasNoValue = hasNoValue,
.location = NN_TRY(unvalidatedConvert(requestArgument.location)),
.dimensions = NN_TRY(toSigned(requestArgument.dimensions)),
};
}
nn::GeneralResult<RequestMemoryPool> unvalidatedConvert(const nn::Request::MemoryPool& memoryPool) {
return std::visit(
overloaded{
[](const nn::SharedMemory& memory) -> nn::GeneralResult<RequestMemoryPool> {
return RequestMemoryPool::make<RequestMemoryPool::Tag::pool>(
NN_TRY(unvalidatedConvert(memory)));
},
[](const nn::Request::MemoryDomainToken& token)
-> nn::GeneralResult<RequestMemoryPool> {
return RequestMemoryPool::make<RequestMemoryPool::Tag::token>(
underlyingType(token));
},
[](const nn::SharedBuffer& /*buffer*/) {
return (NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
<< "Unable to make memory pool from IBuffer")
.
operator nn::GeneralResult<RequestMemoryPool>();
},
},
memoryPool);
}
nn::GeneralResult<Timing> unvalidatedConvert(const nn::Timing& timing) {
return Timing{
.timeOnDevice = NN_TRY(unvalidatedConvert(timing.timeOnDevice)),
.timeInDriver = NN_TRY(unvalidatedConvert(timing.timeInDriver)),
};
}
nn::GeneralResult<int64_t> unvalidatedConvert(const nn::Duration& duration) {
const uint64_t nanoseconds = duration.count();
if (nanoseconds > std::numeric_limits<int64_t>::max()) {
return std::numeric_limits<int64_t>::max();
}
return static_cast<int64_t>(nanoseconds);
}
nn::GeneralResult<int64_t> unvalidatedConvert(const nn::OptionalDuration& optionalDuration) {
if (!optionalDuration.has_value()) {
return kNoTiming;
}
return unvalidatedConvert(optionalDuration.value());
}
nn::GeneralResult<int64_t> unvalidatedConvert(const nn::OptionalTimePoint& optionalTimePoint) {
if (!optionalTimePoint.has_value()) {
return kNoTiming;
}
return unvalidatedConvert(optionalTimePoint->time_since_epoch());
}
nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SyncFence& syncFence) {
auto duplicatedFd = NN_TRY(nn::dupFd(syncFence.getFd()));
return ndk::ScopedFileDescriptor(duplicatedFd.release());
}
nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvertCache(
const nn::SharedHandle& handle) {
if (handle->ints.size() != 0) {
NN_ERROR() << "Cache handle must not contain ints";
}
if (handle->fds.size() != 1) {
NN_ERROR() << "Cache handle must contain exactly one fd but contains "
<< handle->fds.size();
}
auto duplicatedFd = NN_TRY(nn::dupFd(handle->fds.front().get()));
return ndk::ScopedFileDescriptor(duplicatedFd.release());
}
nn::GeneralResult<std::vector<uint8_t>> convert(const nn::CacheToken& cacheToken) {
return unvalidatedConvert(cacheToken);
}
nn::GeneralResult<BufferDesc> convert(const nn::BufferDesc& bufferDesc) {
return validatedConvert(bufferDesc);
}
nn::GeneralResult<bool> convert(const nn::MeasureTiming& measureTiming) {
return validatedConvert(measureTiming);
}
nn::GeneralResult<Memory> convert(const nn::SharedMemory& memory) {
return validatedConvert(memory);
}
@@ -715,11 +1017,62 @@ nn::GeneralResult<ErrorStatus> convert(const nn::ErrorStatus& errorStatus) {
return validatedConvert(errorStatus);
}
nn::GeneralResult<ExecutionPreference> convert(const nn::ExecutionPreference& executionPreference) {
return validatedConvert(executionPreference);
}
nn::GeneralResult<Model> convert(const nn::Model& model) {
return validatedConvert(model);
}
nn::GeneralResult<Priority> convert(const nn::Priority& priority) {
return validatedConvert(priority);
}
nn::GeneralResult<Request> convert(const nn::Request& request) {
return validatedConvert(request);
}
nn::GeneralResult<Timing> convert(const nn::Timing& timing) {
return validatedConvert(timing);
}
nn::GeneralResult<int64_t> convert(const nn::OptionalDuration& optionalDuration) {
return validatedConvert(optionalDuration);
}
nn::GeneralResult<int64_t> convert(const nn::OptionalTimePoint& outputShapes) {
return validatedConvert(outputShapes);
}
nn::GeneralResult<std::vector<BufferRole>> convert(const std::vector<nn::BufferRole>& bufferRoles) {
return validatedConvert(bufferRoles);
}
nn::GeneralResult<std::vector<OutputShape>> convert(
const std::vector<nn::OutputShape>& outputShapes) {
return validatedConvert(outputShapes);
}
nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert(
const std::vector<nn::SharedHandle>& cacheHandles) {
const auto version = NN_TRY(hal::utils::makeGeneralFailure(nn::validate(cacheHandles)));
if (version > kVersion) {
return NN_ERROR() << "Insufficient version: " << version << " vs required " << kVersion;
}
std::vector<ndk::ScopedFileDescriptor> cacheFds;
cacheFds.reserve(cacheHandles.size());
for (const auto& cacheHandle : cacheHandles) {
cacheFds.push_back(NN_TRY(unvalidatedConvertCache(cacheHandle)));
}
return cacheFds;
}
nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert(
const std::vector<nn::SyncFence>& syncFences) {
return unvalidatedConvert(syncFences);
}
nn::GeneralResult<std::vector<int32_t>> toSigned(const std::vector<uint32_t>& vec) {
if (!std::all_of(vec.begin(), vec.end(),
[](uint32_t v) { return v <= std::numeric_limits<int32_t>::max(); })) {

View File

@@ -0,0 +1,294 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Device.h"
#include "Buffer.h"
#include "Callbacks.h"
#include "Conversions.h"
#include "PreparedModel.h"
#include "ProtectCallback.h"
#include "Utils.h"
#include <aidl/android/hardware/neuralnetworks/IDevice.h>
#include <android/binder_auto_utils.h>
#include <android/binder_interface_utils.h>
#include <nnapi/IBuffer.h>
#include <nnapi/IDevice.h>
#include <nnapi/IPreparedModel.h>
#include <nnapi/OperandTypes.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <any>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
// lifetimes across processes and for protecting asynchronous calls across AIDL.
namespace aidl::android::hardware::neuralnetworks::utils {
namespace {
nn::GeneralResult<std::vector<std::shared_ptr<IPreparedModel>>> convert(
const std::vector<nn::SharedPreparedModel>& preparedModels) {
std::vector<std::shared_ptr<IPreparedModel>> aidlPreparedModels(preparedModels.size());
for (size_t i = 0; i < preparedModels.size(); ++i) {
std::any underlyingResource = preparedModels[i]->getUnderlyingResource();
if (const auto* aidlPreparedModel =
std::any_cast<std::shared_ptr<aidl_hal::IPreparedModel>>(&underlyingResource)) {
aidlPreparedModels[i] = *aidlPreparedModel;
} else {
return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
<< "Unable to convert from nn::IPreparedModel to aidl_hal::IPreparedModel";
}
}
return aidlPreparedModels;
}
nn::GeneralResult<nn::Capabilities> getCapabilitiesFrom(IDevice* device) {
CHECK(device != nullptr);
Capabilities capabilities;
const auto ret = device->getCapabilities(&capabilities);
HANDLE_ASTATUS(ret) << "getCapabilities failed";
return nn::convert(capabilities);
}
nn::GeneralResult<std::string> getVersionStringFrom(aidl_hal::IDevice* device) {
CHECK(device != nullptr);
std::string version;
const auto ret = device->getVersionString(&version);
HANDLE_ASTATUS(ret) << "getVersionString failed";
return version;
}
nn::GeneralResult<nn::DeviceType> getDeviceTypeFrom(aidl_hal::IDevice* device) {
CHECK(device != nullptr);
DeviceType deviceType;
const auto ret = device->getType(&deviceType);
HANDLE_ASTATUS(ret) << "getDeviceType failed";
return nn::convert(deviceType);
}
nn::GeneralResult<std::vector<nn::Extension>> getSupportedExtensionsFrom(
aidl_hal::IDevice* device) {
CHECK(device != nullptr);
std::vector<Extension> supportedExtensions;
const auto ret = device->getSupportedExtensions(&supportedExtensions);
HANDLE_ASTATUS(ret) << "getExtensions failed";
return nn::convert(supportedExtensions);
}
nn::GeneralResult<std::pair<uint32_t, uint32_t>> getNumberOfCacheFilesNeededFrom(
aidl_hal::IDevice* device) {
CHECK(device != nullptr);
NumberOfCacheFiles numberOfCacheFiles;
const auto ret = device->getNumberOfCacheFilesNeeded(&numberOfCacheFiles);
HANDLE_ASTATUS(ret) << "getNumberOfCacheFilesNeeded failed";
if (numberOfCacheFiles.numDataCache < 0 || numberOfCacheFiles.numModelCache < 0) {
return NN_ERROR() << "Driver reported negative numer of cache files needed";
}
if (static_cast<uint32_t>(numberOfCacheFiles.numModelCache) > nn::kMaxNumberOfCacheFiles) {
return NN_ERROR() << "getNumberOfCacheFilesNeeded returned numModelCache files greater "
"than allowed max ("
<< numberOfCacheFiles.numModelCache << " vs "
<< nn::kMaxNumberOfCacheFiles << ")";
}
if (static_cast<uint32_t>(numberOfCacheFiles.numDataCache) > nn::kMaxNumberOfCacheFiles) {
return NN_ERROR() << "getNumberOfCacheFilesNeeded returned numDataCache files greater "
"than allowed max ("
<< numberOfCacheFiles.numDataCache << " vs " << nn::kMaxNumberOfCacheFiles
<< ")";
}
return std::make_pair(numberOfCacheFiles.numDataCache, numberOfCacheFiles.numModelCache);
}
} // namespace
nn::GeneralResult<std::shared_ptr<const Device>> Device::create(
std::string name, std::shared_ptr<aidl_hal::IDevice> device) {
if (name.empty()) {
return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
<< "aidl_hal::utils::Device::create must have non-empty name";
}
if (device == nullptr) {
return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
<< "aidl_hal::utils::Device::create must have non-null device";
}
auto versionString = NN_TRY(getVersionStringFrom(device.get()));
const auto deviceType = NN_TRY(getDeviceTypeFrom(device.get()));
auto extensions = NN_TRY(getSupportedExtensionsFrom(device.get()));
auto capabilities = NN_TRY(getCapabilitiesFrom(device.get()));
const auto numberOfCacheFilesNeeded = NN_TRY(getNumberOfCacheFilesNeededFrom(device.get()));
auto deathHandler = NN_TRY(DeathHandler::create(device));
return std::make_shared<const Device>(
PrivateConstructorTag{}, std::move(name), std::move(versionString), deviceType,
std::move(extensions), std::move(capabilities), numberOfCacheFilesNeeded,
std::move(device), std::move(deathHandler));
}
Device::Device(PrivateConstructorTag /*tag*/, std::string name, std::string versionString,
nn::DeviceType deviceType, std::vector<nn::Extension> extensions,
nn::Capabilities capabilities,
std::pair<uint32_t, uint32_t> numberOfCacheFilesNeeded,
std::shared_ptr<aidl_hal::IDevice> device, DeathHandler deathHandler)
: kName(std::move(name)),
kVersionString(std::move(versionString)),
kDeviceType(deviceType),
kExtensions(std::move(extensions)),
kCapabilities(std::move(capabilities)),
kNumberOfCacheFilesNeeded(numberOfCacheFilesNeeded),
kDevice(std::move(device)),
kDeathHandler(std::move(deathHandler)) {}
const std::string& Device::getName() const {
return kName;
}
const std::string& Device::getVersionString() const {
return kVersionString;
}
nn::Version Device::getFeatureLevel() const {
return nn::Version::ANDROID_S;
}
nn::DeviceType Device::getType() const {
return kDeviceType;
}
bool Device::isUpdatable() const {
return false;
}
const std::vector<nn::Extension>& Device::getSupportedExtensions() const {
return kExtensions;
}
const nn::Capabilities& Device::getCapabilities() const {
return kCapabilities;
}
std::pair<uint32_t, uint32_t> Device::getNumberOfCacheFilesNeeded() const {
return kNumberOfCacheFilesNeeded;
}
nn::GeneralResult<void> Device::wait() const {
const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_ping(kDevice->asBinder().get()));
HANDLE_ASTATUS(ret) << "ping failed";
return {};
}
nn::GeneralResult<std::vector<bool>> Device::getSupportedOperations(const nn::Model& model) const {
// Ensure that model is ready for IPC.
std::optional<nn::Model> maybeModelInShared;
const nn::Model& modelInShared =
NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
const auto aidlModel = NN_TRY(convert(modelInShared));
std::vector<bool> supportedOperations;
const auto ret = kDevice->getSupportedOperations(aidlModel, &supportedOperations);
HANDLE_ASTATUS(ret) << "getSupportedOperations failed";
return supportedOperations;
}
nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
// Ensure that model is ready for IPC.
std::optional<nn::Model> maybeModelInShared;
const nn::Model& modelInShared =
NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
const auto aidlModel = NN_TRY(convert(modelInShared));
const auto aidlPreference = NN_TRY(convert(preference));
const auto aidlPriority = NN_TRY(convert(priority));
const auto aidlDeadline = NN_TRY(convert(deadline));
const auto aidlModelCache = NN_TRY(convert(modelCache));
const auto aidlDataCache = NN_TRY(convert(dataCache));
const auto aidlToken = NN_TRY(convert(token));
const auto cb = ndk::SharedRefBase::make<PreparedModelCallback>();
const auto scoped = kDeathHandler.protectCallback(cb.get());
const auto ret = kDevice->prepareModel(aidlModel, aidlPreference, aidlPriority, aidlDeadline,
aidlModelCache, aidlDataCache, aidlToken, cb);
HANDLE_ASTATUS(ret) << "prepareModel failed";
return cb->get();
}
nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
const auto aidlDeadline = NN_TRY(convert(deadline));
const auto aidlModelCache = NN_TRY(convert(modelCache));
const auto aidlDataCache = NN_TRY(convert(dataCache));
const auto aidlToken = NN_TRY(convert(token));
const auto cb = ndk::SharedRefBase::make<PreparedModelCallback>();
const auto scoped = kDeathHandler.protectCallback(cb.get());
const auto ret = kDevice->prepareModelFromCache(aidlDeadline, aidlModelCache, aidlDataCache,
aidlToken, cb);
HANDLE_ASTATUS(ret) << "prepareModelFromCache failed";
return cb->get();
}
nn::GeneralResult<nn::SharedBuffer> Device::allocate(
const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
const std::vector<nn::BufferRole>& inputRoles,
const std::vector<nn::BufferRole>& outputRoles) const {
const auto aidlDesc = NN_TRY(convert(desc));
const auto aidlPreparedModels = NN_TRY(convert(preparedModels));
const auto aidlInputRoles = NN_TRY(convert(inputRoles));
const auto aidlOutputRoles = NN_TRY(convert(outputRoles));
std::vector<IPreparedModelParcel> aidlPreparedModelParcels;
aidlPreparedModelParcels.reserve(aidlPreparedModels.size());
for (const auto& preparedModel : aidlPreparedModels) {
aidlPreparedModelParcels.push_back({.preparedModel = preparedModel});
}
DeviceBuffer buffer;
const auto ret = kDevice->allocate(aidlDesc, aidlPreparedModelParcels, aidlInputRoles,
aidlOutputRoles, &buffer);
HANDLE_ASTATUS(ret) << "IDevice::allocate failed";
if (buffer.token < 0) {
return NN_ERROR() << "IDevice::allocate returned negative token";
}
return Buffer::create(buffer.buffer, static_cast<nn::Request::MemoryDomainToken>(buffer.token));
}
DeathMonitor* Device::getDeathMonitor() const {
return kDeathHandler.getDeathMonitor().get();
}
} // namespace aidl::android::hardware::neuralnetworks::utils

View File

@@ -0,0 +1,172 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "PreparedModel.h"
#include "Callbacks.h"
#include "Conversions.h"
#include "ProtectCallback.h"
#include "Utils.h"
#include <android/binder_auto_utils.h>
#include <nnapi/IPreparedModel.h>
#include <nnapi/Result.h>
#include <nnapi/TypeUtils.h>
#include <nnapi/Types.h>
#include <nnapi/hal/1.0/Burst.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/HandleError.h>
#include <memory>
#include <tuple>
#include <utility>
#include <vector>
// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
// lifetimes across processes and for protecting asynchronous calls across AIDL.
namespace aidl::android::hardware::neuralnetworks::utils {
namespace {
nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> convertExecutionResults(
const std::vector<OutputShape>& outputShapes, const Timing& timing) {
return std::make_pair(NN_TRY(nn::convert(outputShapes)), NN_TRY(nn::convert(timing)));
}
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);
return std::make_pair(NN_TRY(nn::convert(timingLaunched)), NN_TRY(nn::convert(timingFenced)));
}
} // namespace
nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
std::shared_ptr<aidl_hal::IPreparedModel> preparedModel) {
if (preparedModel == nullptr) {
return NN_ERROR()
<< "aidl_hal::utils::PreparedModel::create must have non-null preparedModel";
}
return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, std::move(preparedModel));
}
PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/,
std::shared_ptr<aidl_hal::IPreparedModel> preparedModel)
: kPreparedModel(std::move(preparedModel)) {}
nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
const nn::Request& request, nn::MeasureTiming measure,
const nn::OptionalTimePoint& deadline,
const nn::OptionalDuration& loopTimeoutDuration) const {
// Ensure that request is ready for IPC.
std::optional<nn::Request> maybeRequestInShared;
const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure(
hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)));
const auto aidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
const auto aidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure)));
const auto aidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline)));
const auto aidlLoopTimeoutDuration =
NN_TRY(hal::utils::makeExecutionFailure(convert(loopTimeoutDuration)));
ExecutionResult executionResult;
const auto ret = kPreparedModel->executeSynchronously(
aidlRequest, aidlMeasure, aidlDeadline, aidlLoopTimeoutDuration, &executionResult);
HANDLE_ASTATUS(ret) << "executeSynchronously failed";
if (!executionResult.outputSufficientSize) {
auto canonicalOutputShapes =
nn::convert(executionResult.outputShapes).value_or(std::vector<nn::OutputShape>{});
return NN_ERROR(nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, std::move(canonicalOutputShapes))
<< "execution failed with " << nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE;
}
auto [outputShapes, timing] = NN_TRY(hal::utils::makeExecutionFailure(
convertExecutionResults(executionResult.outputShapes, executionResult.timing)));
NN_TRY(hal::utils::makeExecutionFailure(
hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)));
return std::make_pair(std::move(outputShapes), timing);
}
nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
PreparedModel::executeFenced(const nn::Request& request, const std::vector<nn::SyncFence>& waitFor,
nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline,
const nn::OptionalDuration& loopTimeoutDuration,
const nn::OptionalDuration& timeoutDurationAfterFence) const {
// Ensure that request is ready for IPC.
std::optional<nn::Request> maybeRequestInShared;
const nn::Request& requestInShared =
NN_TRY(hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared));
const auto aidlRequest = NN_TRY(convert(requestInShared));
const auto aidlWaitFor = NN_TRY(convert(waitFor));
const auto aidlMeasure = NN_TRY(convert(measure));
const auto aidlDeadline = NN_TRY(convert(deadline));
const auto aidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
const auto aidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
FencedExecutionResult result;
const auto ret = kPreparedModel->executeFenced(aidlRequest, aidlWaitFor, aidlMeasure,
aidlDeadline, aidlLoopTimeoutDuration,
aidlTimeoutDurationAfterFence, &result);
HANDLE_ASTATUS(ret) << "executeFenced failed";
auto resultSyncFence = nn::SyncFence::createAsSignaled();
if (result.syncFence.get() != -1) {
resultSyncFence = NN_TRY(nn::convert(result.syncFence));
}
auto callback = result.callback;
if (callback == nullptr) {
return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "callback is null";
}
// If executeFenced required the request memory to be moved into shared memory, block here until
// the fenced execution has completed and flush the memory back.
if (maybeRequestInShared.has_value()) {
const auto state = resultSyncFence.syncWait({});
if (state != nn::SyncFence::FenceState::SIGNALED) {
return NN_ERROR() << "syncWait failed with " << state;
}
NN_TRY(hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared));
}
// Create callback which can be used to retrieve the execution error status and timings.
nn::ExecuteFencedInfoCallback resultCallback =
[callback]() -> nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> {
ErrorStatus errorStatus;
Timing timingLaunched;
Timing timingFenced;
const auto ret = callback->getExecutionInfo(&timingLaunched, &timingFenced, &errorStatus);
HANDLE_ASTATUS(ret) << "fenced execution callback getExecutionInfo failed";
return convertFencedExecutionResults(errorStatus, timingLaunched, timingFenced);
};
return std::make_pair(std::move(resultSyncFence), std::move(resultCallback));
}
nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
return hal::V1_0::utils::Burst::create(shared_from_this());
}
std::any PreparedModel::getUnderlyingResource() const {
std::shared_ptr<aidl_hal::IPreparedModel> resource = kPreparedModel;
return resource;
}
} // namespace aidl::android::hardware::neuralnetworks::utils

View File

@@ -0,0 +1,112 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ProtectCallback.h"
#include <android-base/logging.h>
#include <android-base/scopeguard.h>
#include <android-base/thread_annotations.h>
#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>
#include <memory>
#include <mutex>
#include <vector>
#include "Utils.h"
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(); });
}
void DeathMonitor::serviceDied(void* cookie) {
auto deathMonitor = static_cast<DeathMonitor*>(cookie);
deathMonitor->serviceDied();
}
void DeathMonitor::add(hal::utils::IProtectedCallback* killable) const {
CHECK(killable != nullptr);
std::lock_guard guard(mMutex);
mObjects.push_back(killable);
}
void DeathMonitor::remove(hal::utils::IProtectedCallback* killable) const {
CHECK(killable != nullptr);
std::lock_guard guard(mMutex);
const auto removedIter = std::remove(mObjects.begin(), mObjects.end(), killable);
mObjects.erase(removedIter);
}
nn::GeneralResult<DeathHandler> DeathHandler::create(std::shared_ptr<ndk::ICInterface> object) {
if (object == nullptr) {
return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
<< "utils::DeathHandler::create must have non-null object";
}
auto deathMonitor = std::make_shared<DeathMonitor>();
auto deathRecipient = ndk::ScopedAIBinder_DeathRecipient(
AIBinder_DeathRecipient_new(DeathMonitor::serviceDied));
// If passed a local binder, AIBinder_linkToDeath will do nothing and return
// STATUS_INVALID_OPERATION. We ignore this case because we only use local binders in tests
// where this is not an error.
if (object->isRemote()) {
const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_linkToDeath(
object->asBinder().get(), deathRecipient.get(), deathMonitor.get()));
HANDLE_ASTATUS(ret) << "AIBinder_linkToDeath failed";
}
return DeathHandler(std::move(object), std::move(deathRecipient), std::move(deathMonitor));
}
DeathHandler::DeathHandler(std::shared_ptr<ndk::ICInterface> object,
ndk::ScopedAIBinder_DeathRecipient deathRecipient,
std::shared_ptr<DeathMonitor> deathMonitor)
: kObject(std::move(object)),
kDeathRecipient(std::move(deathRecipient)),
kDeathMonitor(std::move(deathMonitor)) {
CHECK(kObject != nullptr);
CHECK(kDeathRecipient.get() != nullptr);
CHECK(kDeathMonitor != nullptr);
}
DeathHandler::~DeathHandler() {
if (kObject != nullptr && kDeathRecipient.get() != nullptr && kDeathMonitor != nullptr) {
const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_unlinkToDeath(
kObject->asBinder().get(), kDeathRecipient.get(), kDeathMonitor.get()));
const auto maybeSuccess = handleTransportError(ret);
if (!maybeSuccess.ok()) {
LOG(ERROR) << maybeSuccess.error().message;
}
}
}
[[nodiscard]] ::android::base::ScopeGuard<DeathHandler::Cleanup> DeathHandler::protectCallback(
hal::utils::IProtectedCallback* killable) const {
CHECK(killable != nullptr);
kDeathMonitor->add(killable);
return ::android::base::make_scope_guard(
[deathMonitor = kDeathMonitor, killable] { deathMonitor->remove(killable); });
}
} // namespace aidl::android::hardware::neuralnetworks::utils

View File

@@ -0,0 +1,50 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Service.h"
#include <android/binder_auto_utils.h>
#include <android/binder_manager.h>
#include <nnapi/IDevice.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/ResilientDevice.h>
#include <string>
#include "Device.h"
namespace aidl::android::hardware::neuralnetworks::utils {
nn::GeneralResult<nn::SharedDevice> getDevice(const std::string& name) {
hal::utils::ResilientDevice::Factory makeDevice =
[name](bool blocking) -> nn::GeneralResult<nn::SharedDevice> {
auto service = blocking ? IDevice::fromBinder(
ndk::SpAIBinder(AServiceManager_getService(name.c_str())))
: IDevice::fromBinder(ndk::SpAIBinder(
AServiceManager_checkService(name.c_str())));
if (service == nullptr) {
return NN_ERROR() << (blocking ? "AServiceManager_getService"
: "AServiceManager_checkService")
<< " returned nullptr";
}
return Device::create(name, std::move(service));
};
return hal::utils::ResilientDevice::create(std::move(makeDevice));
}
} // namespace aidl::android::hardware::neuralnetworks::utils

View File

@@ -16,13 +16,12 @@
#include "Utils.h"
#include <android/binder_status.h>
#include <nnapi/Result.h>
namespace aidl::android::hardware::neuralnetworks::utils {
namespace {
using ::android::nn::GeneralResult;
template <typename Type>
nn::GeneralResult<std::vector<Type>> cloneVec(const std::vector<Type>& arguments) {
std::vector<Type> clonedObjects;
@@ -34,13 +33,13 @@ nn::GeneralResult<std::vector<Type>> cloneVec(const std::vector<Type>& arguments
}
template <typename Type>
GeneralResult<std::vector<Type>> clone(const std::vector<Type>& arguments) {
nn::GeneralResult<std::vector<Type>> clone(const std::vector<Type>& arguments) {
return cloneVec(arguments);
}
} // namespace
GeneralResult<Memory> clone(const Memory& memory) {
nn::GeneralResult<Memory> clone(const Memory& memory) {
common::NativeHandle nativeHandle;
nativeHandle.ints = memory.handle.ints;
nativeHandle.fds.reserve(memory.handle.fds.size());
@@ -58,7 +57,7 @@ GeneralResult<Memory> clone(const Memory& memory) {
};
}
GeneralResult<RequestMemoryPool> clone(const RequestMemoryPool& requestPool) {
nn::GeneralResult<RequestMemoryPool> clone(const RequestMemoryPool& requestPool) {
using Tag = RequestMemoryPool::Tag;
switch (requestPool.getTag()) {
case Tag::pool:
@@ -70,10 +69,10 @@ GeneralResult<RequestMemoryPool> clone(const RequestMemoryPool& requestPool) {
// compiler.
return (NN_ERROR() << "Unrecognized request pool tag: " << requestPool.getTag())
.
operator GeneralResult<RequestMemoryPool>();
operator nn::GeneralResult<RequestMemoryPool>();
}
GeneralResult<Request> clone(const Request& request) {
nn::GeneralResult<Request> clone(const Request& request) {
return Request{
.inputs = request.inputs,
.outputs = request.outputs,
@@ -81,7 +80,7 @@ GeneralResult<Request> clone(const Request& request) {
};
}
GeneralResult<Model> clone(const Model& model) {
nn::GeneralResult<Model> clone(const Model& model) {
return Model{
.main = model.main,
.referenced = model.referenced,
@@ -92,4 +91,20 @@ GeneralResult<Model> clone(const Model& model) {
};
}
nn::GeneralResult<void> handleTransportError(const ndk::ScopedAStatus& ret) {
if (ret.getStatus() == STATUS_DEAD_OBJECT) {
return nn::error(nn::ErrorStatus::DEAD_OBJECT)
<< "Binder transaction returned STATUS_DEAD_OBJECT: " << ret.getDescription();
}
if (ret.isOk()) {
return {};
}
if (ret.getExceptionCode() != EX_SERVICE_SPECIFIC) {
return nn::error(nn::ErrorStatus::GENERAL_FAILURE)
<< "Binder transaction returned exception: " << ret.getDescription();
}
return nn::error(static_cast<nn::ErrorStatus>(ret.getServiceSpecificError()))
<< ret.getMessage();
}
} // namespace aidl::android::hardware::neuralnetworks::utils

View File

@@ -0,0 +1,212 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MockBuffer.h"
#include <aidl/android/hardware/neuralnetworks/ErrorStatus.h>
#include <aidl/android/hardware/neuralnetworks/IBuffer.h>
#include <android/binder_auto_utils.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <nnapi/IBuffer.h>
#include <nnapi/SharedMemory.h>
#include <nnapi/TypeUtils.h>
#include <nnapi/Types.h>
#include <nnapi/hal/aidl/Buffer.h>
#include <functional>
#include <memory>
namespace aidl::android::hardware::neuralnetworks::utils {
namespace {
using ::testing::_;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::Return;
const auto kMemory = nn::createSharedMemory(4).value();
const std::shared_ptr<IBuffer> kInvalidBuffer;
constexpr auto kInvalidToken = nn::Request::MemoryDomainToken{0};
constexpr auto kToken = nn::Request::MemoryDomainToken{1};
constexpr auto makeStatusOk = [] { return ndk::ScopedAStatus::ok(); };
constexpr auto makeGeneralFailure = [] {
return ndk::ScopedAStatus::fromServiceSpecificError(
static_cast<int32_t>(ErrorStatus::GENERAL_FAILURE));
};
constexpr auto makeGeneralTransportFailure = [] {
return ndk::ScopedAStatus::fromStatus(STATUS_NO_MEMORY);
};
constexpr auto makeDeadObjectFailure = [] {
return ndk::ScopedAStatus::fromStatus(STATUS_DEAD_OBJECT);
};
} // namespace
TEST(BufferTest, invalidBuffer) {
// run test
const auto result = Buffer::create(kInvalidBuffer, kToken);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(BufferTest, invalidToken) {
// setup call
const auto mockBuffer = MockBuffer::create();
// run test
const auto result = Buffer::create(mockBuffer, kInvalidToken);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(BufferTest, create) {
// setup call
const auto mockBuffer = MockBuffer::create();
const auto buffer = Buffer::create(mockBuffer, kToken).value();
// run test
const auto token = buffer->getToken();
// verify result
EXPECT_EQ(token, kToken);
}
TEST(BufferTest, copyTo) {
// setup call
const auto mockBuffer = MockBuffer::create();
const auto buffer = Buffer::create(mockBuffer, kToken).value();
EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeStatusOk));
// run test
const auto result = buffer->copyTo(kMemory);
// verify result
EXPECT_TRUE(result.has_value()) << result.error().message;
}
TEST(BufferTest, copyToError) {
// setup test
const auto mockBuffer = MockBuffer::create();
const auto buffer = Buffer::create(mockBuffer, kToken).value();
EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralFailure));
// run test
const auto result = buffer->copyTo(kMemory);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(BufferTest, copyToTransportFailure) {
// setup test
const auto mockBuffer = MockBuffer::create();
const auto buffer = Buffer::create(mockBuffer, kToken).value();
EXPECT_CALL(*mockBuffer, copyTo(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = buffer->copyTo(kMemory);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(BufferTest, copyToDeadObject) {
// setup test
const auto mockBuffer = MockBuffer::create();
const auto buffer = Buffer::create(mockBuffer, kToken).value();
EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = buffer->copyTo(kMemory);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(BufferTest, copyFrom) {
// setup call
const auto mockBuffer = MockBuffer::create();
const auto buffer = Buffer::create(mockBuffer, kToken).value();
EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(InvokeWithoutArgs(makeStatusOk));
// run test
const auto result = buffer->copyFrom(kMemory, {});
// verify result
EXPECT_TRUE(result.has_value());
}
TEST(BufferTest, copyFromError) {
// setup test
const auto mockBuffer = MockBuffer::create();
const auto buffer = Buffer::create(mockBuffer, kToken).value();
EXPECT_CALL(*mockBuffer, copyFrom(_, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralFailure));
// run test
const auto result = buffer->copyFrom(kMemory, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(BufferTest, copyFromTransportFailure) {
// setup test
const auto mockBuffer = MockBuffer::create();
const auto buffer = Buffer::create(mockBuffer, kToken).value();
EXPECT_CALL(*mockBuffer, copyFrom(_, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = buffer->copyFrom(kMemory, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(BufferTest, copyFromDeadObject) {
// setup test
const auto mockBuffer = MockBuffer::create();
const auto buffer = Buffer::create(mockBuffer, kToken).value();
EXPECT_CALL(*mockBuffer, copyFrom(_, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = buffer->copyFrom(kMemory, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
} // namespace aidl::android::hardware::neuralnetworks::utils

View File

@@ -0,0 +1,861 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MockBuffer.h"
#include "MockDevice.h"
#include "MockPreparedModel.h"
#include <aidl/android/hardware/neuralnetworks/BnDevice.h>
#include <android/binder_auto_utils.h>
#include <android/binder_status.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <nnapi/IDevice.h>
#include <nnapi/TypeUtils.h>
#include <nnapi/Types.h>
#include <nnapi/hal/aidl/Device.h>
#include <functional>
#include <memory>
#include <string>
namespace aidl::android::hardware::neuralnetworks::utils {
namespace {
namespace nn = ::android::nn;
using ::testing::_;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::SetArgPointee;
const nn::Model kSimpleModel = {
.main = {.operands = {{.type = nn::OperandType::TENSOR_FLOAT32,
.dimensions = {1},
.lifetime = nn::Operand::LifeTime::SUBGRAPH_INPUT},
{.type = nn::OperandType::TENSOR_FLOAT32,
.dimensions = {1},
.lifetime = nn::Operand::LifeTime::SUBGRAPH_OUTPUT}},
.operations = {{.type = nn::OperationType::RELU, .inputs = {0}, .outputs = {1}}},
.inputIndexes = {0},
.outputIndexes = {1}}};
const std::string kName = "Google-MockV1";
const std::string kInvalidName = "";
const std::shared_ptr<BnDevice> kInvalidDevice;
constexpr PerformanceInfo kNoPerformanceInfo = {.execTime = std::numeric_limits<float>::max(),
.powerUsage = std::numeric_limits<float>::max()};
constexpr NumberOfCacheFiles kNumberOfCacheFiles = {.numModelCache = nn::kMaxNumberOfCacheFiles,
.numDataCache = nn::kMaxNumberOfCacheFiles};
constexpr auto makeStatusOk = [] { return ndk::ScopedAStatus::ok(); };
std::shared_ptr<MockDevice> createMockDevice() {
const auto mockDevice = MockDevice::create();
// Setup default actions for each relevant call.
ON_CALL(*mockDevice, getVersionString(_))
.WillByDefault(DoAll(SetArgPointee<0>(kName), InvokeWithoutArgs(makeStatusOk)));
ON_CALL(*mockDevice, getType(_))
.WillByDefault(
DoAll(SetArgPointee<0>(DeviceType::OTHER), InvokeWithoutArgs(makeStatusOk)));
ON_CALL(*mockDevice, getSupportedExtensions(_))
.WillByDefault(DoAll(SetArgPointee<0>(std::vector<Extension>{}),
InvokeWithoutArgs(makeStatusOk)));
ON_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
.WillByDefault(
DoAll(SetArgPointee<0>(kNumberOfCacheFiles), InvokeWithoutArgs(makeStatusOk)));
ON_CALL(*mockDevice, getCapabilities(_))
.WillByDefault(
DoAll(SetArgPointee<0>(Capabilities{
.relaxedFloat32toFloat16PerformanceScalar = kNoPerformanceInfo,
.relaxedFloat32toFloat16PerformanceTensor = kNoPerformanceInfo,
.ifPerformance = kNoPerformanceInfo,
.whilePerformance = kNoPerformanceInfo,
}),
InvokeWithoutArgs(makeStatusOk)));
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
// uninteresting methods calls.
EXPECT_CALL(*mockDevice, getVersionString(_)).Times(testing::AnyNumber());
EXPECT_CALL(*mockDevice, getType(_)).Times(testing::AnyNumber());
EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(testing::AnyNumber());
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(testing::AnyNumber());
EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(testing::AnyNumber());
return mockDevice;
}
constexpr auto makePreparedModelReturnImpl =
[](ErrorStatus launchStatus, ErrorStatus returnStatus,
const std::shared_ptr<MockPreparedModel>& preparedModel,
const std::shared_ptr<IPreparedModelCallback>& cb) {
cb->notify(returnStatus, preparedModel);
if (launchStatus == ErrorStatus::NONE) {
return ndk::ScopedAStatus::ok();
}
return ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(launchStatus));
};
auto makePreparedModelReturn(ErrorStatus launchStatus, ErrorStatus returnStatus,
const std::shared_ptr<MockPreparedModel>& preparedModel) {
return [launchStatus, returnStatus, preparedModel](
const Model& /*model*/, ExecutionPreference /*preference*/,
Priority /*priority*/, const int64_t& /*deadline*/,
const std::vector<ndk::ScopedFileDescriptor>& /*modelCache*/,
const std::vector<ndk::ScopedFileDescriptor>& /*dataCache*/,
const std::vector<uint8_t>& /*token*/,
const std::shared_ptr<IPreparedModelCallback>& cb) -> ndk::ScopedAStatus {
return makePreparedModelReturnImpl(launchStatus, returnStatus, preparedModel, cb);
};
}
auto makePreparedModelFromCacheReturn(ErrorStatus launchStatus, ErrorStatus returnStatus,
const std::shared_ptr<MockPreparedModel>& preparedModel) {
return [launchStatus, returnStatus, preparedModel](
const int64_t& /*deadline*/,
const std::vector<ndk::ScopedFileDescriptor>& /*modelCache*/,
const std::vector<ndk::ScopedFileDescriptor>& /*dataCache*/,
const std::vector<uint8_t>& /*token*/,
const std::shared_ptr<IPreparedModelCallback>& cb) {
return makePreparedModelReturnImpl(launchStatus, returnStatus, preparedModel, cb);
};
}
constexpr auto makeGeneralFailure = [] {
return ndk::ScopedAStatus::fromServiceSpecificError(
static_cast<int32_t>(ErrorStatus::GENERAL_FAILURE));
};
constexpr auto makeGeneralTransportFailure = [] {
return ndk::ScopedAStatus::fromStatus(STATUS_NO_MEMORY);
};
constexpr auto makeDeadObjectFailure = [] {
return ndk::ScopedAStatus::fromStatus(STATUS_DEAD_OBJECT);
};
} // namespace
TEST(DeviceTest, invalidName) {
// run test
const auto device = MockDevice::create();
const auto result = Device::create(kInvalidName, device);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
}
TEST(DeviceTest, invalidDevice) {
// run test
const auto result = Device::create(kName, kInvalidDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
}
TEST(DeviceTest, getVersionStringError) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getVersionString(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getVersionStringTransportFailure) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getVersionString(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getVersionStringDeadObject) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getVersionString(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(DeviceTest, getTypeError) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getType(_)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getTypeTransportFailure) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getType(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getTypeDeadObject) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getType(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(DeviceTest, getSupportedExtensionsError) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getSupportedExtensionsTransportFailure) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getSupportedExtensionsDeadObject) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(DeviceTest, getNumberOfCacheFilesNeededError) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, dataCacheFilesExceedsSpecifiedMax) {
// setup test
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
.Times(1)
.WillOnce(DoAll(SetArgPointee<0>(NumberOfCacheFiles{
.numModelCache = nn::kMaxNumberOfCacheFiles + 1,
.numDataCache = nn::kMaxNumberOfCacheFiles}),
InvokeWithoutArgs(makeStatusOk)));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, modelCacheFilesExceedsSpecifiedMax) {
// setup test
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
.Times(1)
.WillOnce(DoAll(SetArgPointee<0>(NumberOfCacheFiles{
.numModelCache = nn::kMaxNumberOfCacheFiles,
.numDataCache = nn::kMaxNumberOfCacheFiles + 1}),
InvokeWithoutArgs(makeStatusOk)));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getNumberOfCacheFilesNeededTransportFailure) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getNumberOfCacheFilesNeededDeadObject) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(DeviceTest, getCapabilitiesError) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getCapabilities(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getCapabilitiesTransportFailure) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getCapabilities(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getCapabilitiesDeadObject) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getCapabilities(_))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = Device::create(kName, mockDevice);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(DeviceTest, getName) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
// run test
const auto& name = device->getName();
// verify result
EXPECT_EQ(name, kName);
}
TEST(DeviceTest, getFeatureLevel) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
// run test
const auto featureLevel = device->getFeatureLevel();
// verify result
EXPECT_EQ(featureLevel, nn::Version::ANDROID_S);
}
TEST(DeviceTest, getCachedData) {
// setup call
const auto mockDevice = createMockDevice();
EXPECT_CALL(*mockDevice, getVersionString(_)).Times(1);
EXPECT_CALL(*mockDevice, getType(_)).Times(1);
EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(1);
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1);
EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(1);
const auto result = Device::create(kName, mockDevice);
ASSERT_TRUE(result.has_value())
<< "Failed with " << result.error().code << ": " << result.error().message;
const auto& device = result.value();
// run test and verify results
EXPECT_EQ(device->getVersionString(), device->getVersionString());
EXPECT_EQ(device->getType(), device->getType());
EXPECT_EQ(device->getSupportedExtensions(), device->getSupportedExtensions());
EXPECT_EQ(device->getNumberOfCacheFilesNeeded(), device->getNumberOfCacheFilesNeeded());
EXPECT_EQ(device->getCapabilities(), device->getCapabilities());
}
TEST(DeviceTest, getSupportedOperations) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
.Times(1)
.WillOnce(DoAll(
SetArgPointee<1>(std::vector<bool>(kSimpleModel.main.operations.size(), true)),
InvokeWithoutArgs(makeStatusOk)));
// run test
const auto result = device->getSupportedOperations(kSimpleModel);
// verify result
ASSERT_TRUE(result.has_value())
<< "Failed with " << result.error().code << ": " << result.error().message;
const auto& supportedOperations = result.value();
EXPECT_EQ(supportedOperations.size(), kSimpleModel.main.operations.size());
EXPECT_THAT(supportedOperations, Each(testing::IsTrue()));
}
TEST(DeviceTest, getSupportedOperationsError) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralFailure));
// run test
const auto result = device->getSupportedOperations(kSimpleModel);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getSupportedOperationsTransportFailure) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = device->getSupportedOperations(kSimpleModel);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, getSupportedOperationsDeadObject) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = device->getSupportedOperations(kSimpleModel);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(DeviceTest, prepareModel) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
const auto mockPreparedModel = MockPreparedModel::create();
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::NONE, ErrorStatus::NONE,
mockPreparedModel)));
// run test
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
nn::Priority::DEFAULT, {}, {}, {}, {});
// verify result
ASSERT_TRUE(result.has_value())
<< "Failed with " << result.error().code << ": " << result.error().message;
EXPECT_NE(result.value(), nullptr);
}
TEST(DeviceTest, prepareModelLaunchError) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::GENERAL_FAILURE,
ErrorStatus::GENERAL_FAILURE, nullptr)));
// run test
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
nn::Priority::DEFAULT, {}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, prepareModelReturnError) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::NONE,
ErrorStatus::GENERAL_FAILURE, nullptr)));
// run test
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
nn::Priority::DEFAULT, {}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, prepareModelNullptrError) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(
Invoke(makePreparedModelReturn(ErrorStatus::NONE, ErrorStatus::NONE, nullptr)));
// run test
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
nn::Priority::DEFAULT, {}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, prepareModelTransportFailure) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
nn::Priority::DEFAULT, {}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, prepareModelDeadObject) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
nn::Priority::DEFAULT, {}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(DeviceTest, prepareModelAsyncCrash) {
// setup test
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
const auto ret = [&device]() {
DeathMonitor::serviceDied(device->getDeathMonitor());
return ndk::ScopedAStatus::ok();
};
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(ret));
// run test
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
nn::Priority::DEFAULT, {}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(DeviceTest, prepareModelFromCache) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
const auto mockPreparedModel = MockPreparedModel::create();
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
.Times(1)
.WillOnce(Invoke(makePreparedModelFromCacheReturn(ErrorStatus::NONE, ErrorStatus::NONE,
mockPreparedModel)));
// run test
const auto result = device->prepareModelFromCache({}, {}, {}, {});
// verify result
ASSERT_TRUE(result.has_value())
<< "Failed with " << result.error().code << ": " << result.error().message;
EXPECT_NE(result.value(), nullptr);
}
TEST(DeviceTest, prepareModelFromCacheLaunchError) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
.Times(1)
.WillOnce(Invoke(makePreparedModelFromCacheReturn(
ErrorStatus::GENERAL_FAILURE, ErrorStatus::GENERAL_FAILURE, nullptr)));
// run test
const auto result = device->prepareModelFromCache({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, prepareModelFromCacheReturnError) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
.Times(1)
.WillOnce(Invoke(makePreparedModelFromCacheReturn(
ErrorStatus::NONE, ErrorStatus::GENERAL_FAILURE, nullptr)));
// run test
const auto result = device->prepareModelFromCache({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, prepareModelFromCacheNullptrError) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
.Times(1)
.WillOnce(Invoke(makePreparedModelFromCacheReturn(ErrorStatus::NONE, ErrorStatus::NONE,
nullptr)));
// run test
const auto result = device->prepareModelFromCache({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, prepareModelFromCacheTransportFailure) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = device->prepareModelFromCache({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, prepareModelFromCacheDeadObject) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = device->prepareModelFromCache({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(DeviceTest, prepareModelFromCacheAsyncCrash) {
// setup test
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
const auto ret = [&device]() {
DeathMonitor::serviceDied(device->getDeathMonitor());
return ndk::ScopedAStatus::ok();
};
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(ret));
// run test
const auto result = device->prepareModelFromCache({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(DeviceTest, allocate) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
const auto mockBuffer = DeviceBuffer{.buffer = MockBuffer::create(), .token = 1};
EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
.Times(1)
.WillOnce(DoAll(SetArgPointee<4>(mockBuffer), InvokeWithoutArgs(makeStatusOk)));
// run test
const auto result = device->allocate({}, {}, {}, {});
// verify result
ASSERT_TRUE(result.has_value())
<< "Failed with " << result.error().code << ": " << result.error().message;
EXPECT_NE(result.value(), nullptr);
}
TEST(DeviceTest, allocateError) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralFailure));
// run test
const auto result = device->allocate({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, allocateTransportFailure) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = device->allocate({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(DeviceTest, allocateDeadObject) {
// setup call
const auto mockDevice = createMockDevice();
const auto device = Device::create(kName, mockDevice).value();
EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = device->allocate({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
} // namespace aidl::android::hardware::neuralnetworks::utils

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_BUFFER
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_BUFFER
#include <aidl/android/hardware/neuralnetworks/BnBuffer.h>
#include <android/binder_interface_utils.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <hidl/Status.h>
namespace aidl::android::hardware::neuralnetworks::utils {
class MockBuffer final : public BnBuffer {
public:
static std::shared_ptr<MockBuffer> create();
MOCK_METHOD(ndk::ScopedAStatus, copyTo, (const Memory& dst), (override));
MOCK_METHOD(ndk::ScopedAStatus, copyFrom,
(const Memory& src, const std::vector<int32_t>& dimensions), (override));
};
inline std::shared_ptr<MockBuffer> MockBuffer::create() {
return ndk::SharedRefBase::make<MockBuffer>();
}
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_BUFFER

View File

@@ -0,0 +1,67 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_DEVICE
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_DEVICE
#include <aidl/android/hardware/neuralnetworks/BnDevice.h>
#include <android/binder_auto_utils.h>
#include <android/binder_interface_utils.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace aidl::android::hardware::neuralnetworks::utils {
class MockDevice final : public BnDevice {
public:
static std::shared_ptr<MockDevice> create();
MOCK_METHOD(ndk::ScopedAStatus, allocate,
(const BufferDesc& desc, const std::vector<IPreparedModelParcel>& preparedModels,
const std::vector<BufferRole>& inputRoles,
const std::vector<BufferRole>& outputRoles, DeviceBuffer* deviceBuffer),
(override));
MOCK_METHOD(ndk::ScopedAStatus, getCapabilities, (Capabilities * capabilities), (override));
MOCK_METHOD(ndk::ScopedAStatus, getNumberOfCacheFilesNeeded,
(NumberOfCacheFiles * numberOfCacheFiles), (override));
MOCK_METHOD(ndk::ScopedAStatus, getSupportedExtensions, (std::vector<Extension> * extensions),
(override));
MOCK_METHOD(ndk::ScopedAStatus, getSupportedOperations,
(const Model& model, std::vector<bool>* supportedOperations), (override));
MOCK_METHOD(ndk::ScopedAStatus, getType, (DeviceType * deviceType), (override));
MOCK_METHOD(ndk::ScopedAStatus, getVersionString, (std::string * version), (override));
MOCK_METHOD(ndk::ScopedAStatus, prepareModel,
(const Model& model, ExecutionPreference preference, Priority priority,
int64_t deadline, const std::vector<ndk::ScopedFileDescriptor>& modelCache,
const std::vector<ndk::ScopedFileDescriptor>& dataCache,
const std::vector<uint8_t>& token,
const std::shared_ptr<IPreparedModelCallback>& callback),
(override));
MOCK_METHOD(ndk::ScopedAStatus, prepareModelFromCache,
(int64_t deadline, const std::vector<ndk::ScopedFileDescriptor>& modelCache,
const std::vector<ndk::ScopedFileDescriptor>& dataCache,
const std::vector<uint8_t>& token,
const std::shared_ptr<IPreparedModelCallback>& callback),
(override));
};
inline std::shared_ptr<MockDevice> MockDevice::create() {
return ndk::SharedRefBase::make<MockDevice>();
}
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_DEVICE

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_FENCED_EXECUTION_CALLBACK
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_FENCED_EXECUTION_CALLBACK
#include <aidl/android/hardware/neuralnetworks/BnFencedExecutionCallback.h>
#include <android/binder_auto_utils.h>
#include <android/binder_interface_utils.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <hidl/Status.h>
namespace aidl::android::hardware::neuralnetworks::utils {
class MockFencedExecutionCallback final : public BnFencedExecutionCallback {
public:
static std::shared_ptr<MockFencedExecutionCallback> create();
// V1_3 methods below.
MOCK_METHOD(ndk::ScopedAStatus, getExecutionInfo,
(Timing * timingLaunched, Timing* timingFenced, ErrorStatus* errorStatus),
(override));
};
inline std::shared_ptr<MockFencedExecutionCallback> MockFencedExecutionCallback::create() {
return ndk::SharedRefBase::make<MockFencedExecutionCallback>();
}
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_FENCED_EXECUTION_CALLBACK

View File

@@ -0,0 +1,50 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_PREPARED_MODEL
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_PREPARED_MODEL
#include <aidl/android/hardware/neuralnetworks/BnPreparedModel.h>
#include <android/binder_interface_utils.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <hidl/HidlSupport.h>
#include <hidl/Status.h>
namespace aidl::android::hardware::neuralnetworks::utils {
class MockPreparedModel final : public BnPreparedModel {
public:
static std::shared_ptr<MockPreparedModel> create();
MOCK_METHOD(ndk::ScopedAStatus, executeSynchronously,
(const Request& request, bool measureTiming, int64_t deadline,
int64_t loopTimeoutDuration, ExecutionResult* executionResult),
(override));
MOCK_METHOD(ndk::ScopedAStatus, executeFenced,
(const Request& request, const std::vector<ndk::ScopedFileDescriptor>& waitFor,
bool measureTiming, int64_t deadline, int64_t loopTimeoutDuration,
int64_t duration, FencedExecutionResult* fencedExecutionResult),
(override));
};
inline std::shared_ptr<MockPreparedModel> MockPreparedModel::create() {
return ndk::SharedRefBase::make<MockPreparedModel>();
}
} // namespace aidl::android::hardware::neuralnetworks::utils
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_PREPARED_MODEL

View File

@@ -0,0 +1,272 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MockFencedExecutionCallback.h"
#include "MockPreparedModel.h"
#include <aidl/android/hardware/neuralnetworks/IFencedExecutionCallback.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <nnapi/IPreparedModel.h>
#include <nnapi/TypeUtils.h>
#include <nnapi/Types.h>
#include <nnapi/hal/aidl/PreparedModel.h>
#include <functional>
#include <memory>
namespace aidl::android::hardware::neuralnetworks::utils {
namespace {
using ::testing::_;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::SetArgPointee;
const std::shared_ptr<IPreparedModel> kInvalidPreparedModel;
constexpr auto kNoTiming = Timing{.timeOnDevice = -1, .timeInDriver = -1};
constexpr auto makeStatusOk = [] { return ndk::ScopedAStatus::ok(); };
constexpr auto makeGeneralFailure = [] {
return ndk::ScopedAStatus::fromServiceSpecificError(
static_cast<int32_t>(ErrorStatus::GENERAL_FAILURE));
};
constexpr auto makeGeneralTransportFailure = [] {
return ndk::ScopedAStatus::fromStatus(STATUS_NO_MEMORY);
};
constexpr auto makeDeadObjectFailure = [] {
return ndk::ScopedAStatus::fromStatus(STATUS_DEAD_OBJECT);
};
auto makeFencedExecutionResult(const std::shared_ptr<MockFencedExecutionCallback>& callback) {
return [callback](const Request& /*request*/,
const std::vector<ndk::ScopedFileDescriptor>& /*waitFor*/,
bool /*measureTiming*/, int64_t /*deadline*/, int64_t /*loopTimeoutDuration*/,
int64_t /*duration*/, FencedExecutionResult* fencedExecutionResult) {
*fencedExecutionResult = FencedExecutionResult{.callback = callback,
.syncFence = ndk::ScopedFileDescriptor(-1)};
return ndk::ScopedAStatus::ok();
};
}
} // namespace
TEST(PreparedModelTest, invalidPreparedModel) {
// run test
const auto result = PreparedModel::create(kInvalidPreparedModel);
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(PreparedModelTest, executeSync) {
// setup call
const auto mockPreparedModel = MockPreparedModel::create();
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
const auto mockExecutionResult = ExecutionResult{
.outputSufficientSize = true,
.outputShapes = {},
.timing = kNoTiming,
};
EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _, _, _))
.Times(1)
.WillOnce(
DoAll(SetArgPointee<4>(mockExecutionResult), InvokeWithoutArgs(makeStatusOk)));
// run test
const auto result = preparedModel->execute({}, {}, {}, {});
// verify result
EXPECT_TRUE(result.has_value())
<< "Failed with " << result.error().code << ": " << result.error().message;
}
TEST(PreparedModelTest, executeSyncError) {
// setup test
const auto mockPreparedModel = MockPreparedModel::create();
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _, _, _))
.Times(1)
.WillOnce(Invoke(makeGeneralFailure));
// run test
const auto result = preparedModel->execute({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(PreparedModelTest, executeSyncTransportFailure) {
// setup test
const auto mockPreparedModel = MockPreparedModel::create();
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = preparedModel->execute({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(PreparedModelTest, executeSyncDeadObject) {
// setup test
const auto mockPreparedModel = MockPreparedModel::create();
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = preparedModel->execute({}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
TEST(PreparedModelTest, executeFenced) {
// setup call
const auto mockPreparedModel = MockPreparedModel::create();
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
const auto mockCallback = MockFencedExecutionCallback::create();
EXPECT_CALL(*mockCallback, getExecutionInfo(_, _, _))
.Times(1)
.WillOnce(DoAll(SetArgPointee<0>(kNoTiming), SetArgPointee<1>(kNoTiming),
SetArgPointee<2>(ErrorStatus::NONE), Invoke(makeStatusOk)));
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _))
.Times(1)
.WillOnce(Invoke(makeFencedExecutionResult(mockCallback)));
// run test
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
// verify result
ASSERT_TRUE(result.has_value())
<< "Failed with " << result.error().code << ": " << result.error().message;
const auto& [syncFence, callback] = result.value();
EXPECT_EQ(syncFence.syncWait({}), nn::SyncFence::FenceState::SIGNALED);
ASSERT_NE(callback, nullptr);
// get results from callback
const auto callbackResult = callback();
ASSERT_TRUE(callbackResult.has_value()) << "Failed with " << callbackResult.error().code << ": "
<< callbackResult.error().message;
}
TEST(PreparedModelTest, executeFencedCallbackError) {
// setup call
const auto mockPreparedModel = MockPreparedModel::create();
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
const auto mockCallback = MockFencedExecutionCallback::create();
EXPECT_CALL(*mockCallback, getExecutionInfo(_, _, _))
.Times(1)
.WillOnce(Invoke(DoAll(SetArgPointee<0>(kNoTiming), SetArgPointee<1>(kNoTiming),
SetArgPointee<2>(ErrorStatus::GENERAL_FAILURE),
Invoke(makeStatusOk))));
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _))
.Times(1)
.WillOnce(Invoke(makeFencedExecutionResult(mockCallback)));
// run test
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
// verify result
ASSERT_TRUE(result.has_value())
<< "Failed with " << result.error().code << ": " << result.error().message;
const auto& [syncFence, callback] = result.value();
EXPECT_NE(syncFence.syncWait({}), nn::SyncFence::FenceState::ACTIVE);
ASSERT_NE(callback, nullptr);
// verify callback failure
const auto callbackResult = callback();
ASSERT_FALSE(callbackResult.has_value());
EXPECT_EQ(callbackResult.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(PreparedModelTest, executeFencedError) {
// setup test
const auto mockPreparedModel = MockPreparedModel::create();
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralFailure));
// run test
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(PreparedModelTest, executeFencedTransportFailure) {
// setup test
const auto mockPreparedModel = MockPreparedModel::create();
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
// run test
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
}
TEST(PreparedModelTest, executeFencedDeadObject) {
// setup test
const auto mockPreparedModel = MockPreparedModel::create();
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _))
.Times(1)
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
// run test
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
// verify result
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
}
// TODO: test burst execution if/when it is added to nn::IPreparedModel.
TEST(PreparedModelTest, getUnderlyingResource) {
// setup test
const auto mockPreparedModel = MockPreparedModel::create();
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
// run test
const auto resource = preparedModel->getUnderlyingResource();
// verify resource
const std::shared_ptr<IPreparedModel>* maybeMock =
std::any_cast<std::shared_ptr<IPreparedModel>>(&resource);
ASSERT_NE(maybeMock, nullptr);
EXPECT_EQ(maybeMock->get(), mockPreparedModel.get());
}
} // namespace aidl::android::hardware::neuralnetworks::utils

View File

@@ -49,7 +49,9 @@ The `convert` functions operate only on types that used in a HIDL method call di
(i.e., not as a nested class) or used in a subsequent version of the NN HAL. Prefer using `convert`
over `unvalidatedConvert`.
# HIDL Interface Lifetimes across Processes
# Interface Lifetimes across Processes
## HIDL
Some notes about HIDL interface objects and lifetimes across processes:
@@ -68,7 +70,20 @@ behave strangely with `::android::wp` references.)
If the process which created the HIDL interface object dies, any call on this object from another
process will result in a HIDL transport error with the code `DEAD_OBJECT`.
# Protecting Asynchronous Calls across HIDL
## AIDL
We use NDK backend for AIDL interfaces. Handling of lifetimes is generally the same with the
following differences:
* Interfaces inherit from `ndk::ICInterface`, which inherits from `ndk::SharedRefBase`. The latter
is an analog of `::android::RefBase` using `std::shared_ptr` for reference counting.
* AIDL calls return `ndk::ScopedAStatus` which wraps fields of types `binder_status_t` and
`binder_exception_t`. In case the call is made on a dead object, the call will return
`ndk::ScopedAStatus` with exception `EX_TRANSACTION_FAILED` and binder status
`STATUS_DEAD_OBJECT`.
# Protecting Asynchronous Calls
## Across HIDL
Some notes about asynchronous calls across HIDL:
@@ -95,3 +110,17 @@ died. nnapi/hal/ProtectCallback.h's `DeathHandler` uses `hidl_death_recipient`s
driver process has died, and `DeathHandler` will unblock any thread waiting on the results of an
`IProtectedCallback` callback object that may otherwise not be signaled. In order for this to work,
the `IProtectedCallback` object must have been registered via `DeathHandler::protectCallback()`.
## Across AIDL
We use NDK backend for AIDL interfaces. Handling of asynchronous calls is generally the same with
the following differences:
* AIDL calls return `ndk::ScopedAStatus` which wraps fields of types `binder_status_t` and
`binder_exception_t`. In case the call is made on a dead object, the call will return
`ndk::ScopedAStatus` with exception `EX_TRANSACTION_FAILED` and binder status
`STATUS_DEAD_OBJECT`.
* AIDL interface doesn't contain asynchronous `IPreparedModel::execute`.
* Service death is handled using `AIBinder_DeathRecipient` object which is linked to an interface
object using `AIBinder_linkToDeath`. nnapi/hal/aidl/ProtectCallback.h provides `DeathHandler`
object that is a direct analog of HIDL `DeathHandler`, only using libbinder_ndk objects for
implementation.

View File

@@ -35,8 +35,10 @@ cc_library_static {
"neuralnetworks_types",
],
shared_libs: [
"android.hardware.neuralnetworks-V1-ndk_platform",
"libhidlbase",
"libnativewindow",
"libbinder_ndk",
],
}

View File

@@ -32,6 +32,8 @@ namespace hal = ::android::hardware::neuralnetworks;
// Shorthands
namespace aidl::android::hardware::neuralnetworks {
namespace aidl_hal = ::aidl::android::hardware::neuralnetworks;
namespace hal = ::android::hardware::neuralnetworks;
namespace nn = ::android::nn;
} // namespace aidl::android::hardware::neuralnetworks
// Shorthands