From 900c28a250297964cf9aa65653b9adce4e6db27a Mon Sep 17 00:00:00 2001 From: Lev Proleev Date: Tue, 26 Jan 2021 19:40:20 +0000 Subject: 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 3b93b0b56a4f5128eaa942d804dd490317c0abcb) --- neuralnetworks/aidl/utils/src/Buffer.cpp | 78 ++++ neuralnetworks/aidl/utils/src/Callbacks.cpp | 61 ++++ neuralnetworks/aidl/utils/src/Conversions.cpp | 419 ++++++++++++++++++++-- neuralnetworks/aidl/utils/src/Device.cpp | 294 +++++++++++++++ neuralnetworks/aidl/utils/src/PreparedModel.cpp | 172 +++++++++ neuralnetworks/aidl/utils/src/ProtectCallback.cpp | 112 ++++++ neuralnetworks/aidl/utils/src/Service.cpp | 50 +++ neuralnetworks/aidl/utils/src/Utils.cpp | 31 +- 8 files changed, 1176 insertions(+), 41 deletions(-) create mode 100644 neuralnetworks/aidl/utils/src/Buffer.cpp create mode 100644 neuralnetworks/aidl/utils/src/Callbacks.cpp create mode 100644 neuralnetworks/aidl/utils/src/Device.cpp create mode 100644 neuralnetworks/aidl/utils/src/PreparedModel.cpp create mode 100644 neuralnetworks/aidl/utils/src/ProtectCallback.cpp create mode 100644 neuralnetworks/aidl/utils/src/Service.cpp (limited to 'neuralnetworks/aidl/utils/src') diff --git a/neuralnetworks/aidl/utils/src/Buffer.cpp b/neuralnetworks/aidl/utils/src/Buffer.cpp new file mode 100644 index 0000000000..c729a688d1 --- /dev/null +++ b/neuralnetworks/aidl/utils/src/Buffer.cpp @@ -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 +#include +#include + +#include "Conversions.h" +#include "Utils.h" +#include "nnapi/hal/aidl/Conversions.h" + +#include +#include + +// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface +// lifetimes across processes. + +namespace aidl::android::hardware::neuralnetworks::utils { + +nn::GeneralResult> Buffer::create( + std::shared_ptr 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(0)) { + return NN_ERROR() << "aidl_hal::utils::Buffer::create must have non-zero token"; + } + + return std::make_shared(PrivateConstructorTag{}, std::move(buffer), token); +} + +Buffer::Buffer(PrivateConstructorTag /*tag*/, std::shared_ptr buffer, + nn::Request::MemoryDomainToken token) + : kBuffer(std::move(buffer)), kToken(token) { + CHECK(kBuffer != nullptr); + CHECK(kToken != static_cast(0)); +} + +nn::Request::MemoryDomainToken Buffer::getToken() const { + return kToken; +} + +nn::GeneralResult 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 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 diff --git a/neuralnetworks/aidl/utils/src/Callbacks.cpp b/neuralnetworks/aidl/utils/src/Callbacks.cpp new file mode 100644 index 0000000000..8055665228 --- /dev/null +++ b/neuralnetworks/aidl/utils/src/Callbacks.cpp @@ -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 +#include +#include + +#include + +// 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 prepareModelCallback( + ErrorStatus status, const std::shared_ptr& 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& 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 diff --git a/neuralnetworks/aidl/utils/src/Conversions.cpp b/neuralnetworks/aidl/utils/src/Conversions.cpp index db3504bb74..5d9c55b1ba 100644 --- a/neuralnetworks/aidl/utils/src/Conversions.cpp +++ b/neuralnetworks/aidl/utils/src/Conversions.cpp @@ -18,6 +18,8 @@ #include #include +#include +#include #include #include #include @@ -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::max())) return NN_ERROR() +namespace { template constexpr std::underlying_type_t underlyingType(Type value) { return static_cast>(value); } constexpr auto kVersion = android::nn::Version::ANDROID_S; +constexpr int64_t kNoTiming = -1; } // namespace @@ -134,13 +139,8 @@ GeneralResult unvalidatedConvertHelper(const NativeHandle& aidlNativeHan std::vector 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; -static nn::GeneralResult nativeHandleFromAidlHandle( - const NativeHandle& handle) { +static GeneralResult nativeHandleFromAidlHandle(const NativeHandle& handle) { std::vector 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::max(); @@ -382,14 +378,14 @@ static uint32_t roundUpToMultiple(uint32_t value, uint32_t multiple) { GeneralResult unvalidatedConvert(const aidl_hal::Memory& memory) { VERIFY_NON_NEGATIVE(memory.size) << "Memory size must not be negative"; - if (memory.size > std::numeric_limits::max()) { + if (memory.size > std::numeric_limits::max()) { return NN_ERROR() << "Memory: size must be <= std::numeric_limits::max()"; } if (memory.name != "hardware_buffer_blob") { return std::make_shared(Memory{ .handle = NN_TRY(unvalidatedConvertHelper(memory.handle)), - .size = static_cast(memory.size), + .size = static_cast(memory.size), .name = memory.name, }); } @@ -434,11 +430,28 @@ GeneralResult unvalidatedConvert(const aidl_hal::Memory& memory) { return std::make_shared(Memory{ .handle = HardwareBufferHandle(hardwareBuffer, /*takeOwnership=*/true), - .size = static_cast(memory.size), + .size = static_cast(memory.size), .name = memory.name, }); } +GeneralResult 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(halTiming)); + }; + return Timing{.timeOnDevice = convertTiming(timing.timeOnDevice), + .timeInDriver = convertTiming(timing.timeInDriver)}; +} + GeneralResult unvalidatedConvert(const std::vector& operandValues) { return Model::OperandValues(operandValues.data(), operandValues.size()); } @@ -515,6 +528,23 @@ GeneralResult unvalidatedConvert(const NativeHandle& aidlNativeHan return std::make_shared(NN_TRY(unvalidatedConvertHelper(aidlNativeHandle))); } +GeneralResult unvalidatedConvert(const ndk::ScopedFileDescriptor& syncFence) { + auto duplicatedFd = NN_TRY(dupFd(syncFence.get())); + return SyncFence::create(std::move(duplicatedFd)); +} + +GeneralResult convert(const aidl_hal::Capabilities& capabilities) { + return validatedConvert(capabilities); +} + +GeneralResult convert(const aidl_hal::DeviceType& deviceType) { + return validatedConvert(deviceType); +} + +GeneralResult convert(const aidl_hal::ErrorStatus& errorStatus) { + return validatedConvert(errorStatus); +} + GeneralResult convert( const aidl_hal::ExecutionPreference& executionPreference) { return validatedConvert(executionPreference); @@ -548,6 +578,18 @@ GeneralResult convert(const aidl_hal::Request& request) { return validatedConvert(request); } +GeneralResult convert(const aidl_hal::Timing& timing) { + return validatedConvert(timing); +} + +GeneralResult convert(const ndk::ScopedFileDescriptor& syncFence) { + return unvalidatedConvert(syncFence); +} + +GeneralResult> convert(const std::vector& extension) { + return validatedConvert(extension); +} + GeneralResult> convert(const std::vector& operations) { return unvalidatedConvert(operations); } @@ -556,6 +598,11 @@ GeneralResult> convert(const std::vector> convert( + const std::vector& outputShapes) { + return validatedConvert(outputShapes); +} + GeneralResult> toUnsigned(const std::vector& 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 nn::GeneralResult>> unvalidatedConvertVec( const std::vector& arguments) { - std::vector> halObject(arguments.size()); - for (size_t i = 0; i < arguments.size(); ++i) { - halObject[i] = NN_TRY(unvalidatedConvert(arguments[i])); + std::vector> halObject; + halObject.reserve(arguments.size()); + for (const auto& argument : arguments) { + halObject.push_back(NN_TRY(unvalidatedConvert(argument))); } return halObject; } +template +nn::GeneralResult>> unvalidatedConvert( + const std::vector& arguments) { + return unvalidatedConvertVec(arguments); +} + template nn::GeneralResult> validatedConvert(const Type& canonical) { const auto maybeVersion = nn::validate(canonical); @@ -609,29 +663,29 @@ nn::GeneralResult 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 +struct overloaded : Ts... { + using Ts::operator()...; +}; +template +overloaded(Ts...)->overloaded; + static nn::GeneralResult 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(&handle.data[handle.numFds], @@ -642,6 +696,30 @@ static nn::GeneralResult aidlHandleFromNativeHandle( } // namespace +nn::GeneralResult> unvalidatedConvert(const nn::CacheToken& cacheToken) { + return std::vector(cacheToken.begin(), cacheToken.end()); +} + +nn::GeneralResult unvalidatedConvert(const nn::BufferDesc& bufferDesc) { + return BufferDesc{.dimensions = NN_TRY(toSigned(bufferDesc.dimensions))}; +} + +nn::GeneralResult unvalidatedConvert(const nn::BufferRole& bufferRole) { + VERIFY_LE_INT32_MAX(bufferRole.modelIndex) + << "BufferRole: modelIndex must be <= std::numeric_limits::max()"; + VERIFY_LE_INT32_MAX(bufferRole.ioIndex) + << "BufferRole: ioIndex must be <= std::numeric_limits::max()"; + return BufferRole{ + .modelIndex = static_cast(bufferRole.modelIndex), + .ioIndex = static_cast(bufferRole.ioIndex), + .frequency = bufferRole.frequency, + }; +} + +nn::GeneralResult unvalidatedConvert(const nn::MeasureTiming& measureTiming) { + return measureTiming == nn::MeasureTiming::YES; +} + nn::GeneralResult unvalidatedConvert(const nn::SharedHandle& sharedHandle) { CHECK(sharedHandle != nullptr); return unvalidatedConvert(*sharedHandle); @@ -707,6 +785,230 @@ nn::GeneralResult unvalidatedConvert(const nn::OutputShape& outputS .isSufficient = outputShape.isSufficient}; } +nn::GeneralResult unvalidatedConvert( + const nn::ExecutionPreference& executionPreference) { + return static_cast(executionPreference); +} + +nn::GeneralResult unvalidatedConvert(const nn::OperandType& operandType) { + return static_cast(operandType); +} + +nn::GeneralResult unvalidatedConvert( + const nn::Operand::LifeTime& operandLifeTime) { + return static_cast(operandLifeTime); +} + +nn::GeneralResult unvalidatedConvert(const nn::DataLocation& location) { + VERIFY_LE_INT32_MAX(location.poolIndex) + << "DataLocation: pool index must be <= std::numeric_limits::max()"; + return DataLocation{ + .poolIndex = static_cast(location.poolIndex), + .offset = static_cast(location.offset), + .length = static_cast(location.length), + }; +} + +nn::GeneralResult> unvalidatedConvert( + const nn::Operand::ExtraParams& extraParams) { + return std::visit( + overloaded{ + [](const nn::Operand::NoParams&) + -> nn::GeneralResult> { + return std::nullopt; + }, + [](const nn::Operand::SymmPerChannelQuantParams& symmPerChannelQuantParams) + -> nn::GeneralResult> { + if (symmPerChannelQuantParams.channelDim > + std::numeric_limits::max()) { + // Using explicit type conversion because std::optional in successful + // result confuses the compiler. + return (NN_ERROR() << "symmPerChannelQuantParams.channelDim must be <= " + "std::numeric_limits::max(), received: " + << symmPerChannelQuantParams.channelDim) + . + operator nn::GeneralResult>(); + } + return OperandExtraParams::make( + SymmPerChannelQuantParams{ + .scales = symmPerChannelQuantParams.scales, + .channelDim = static_cast( + symmPerChannelQuantParams.channelDim), + }); + }, + [](const nn::Operand::ExtensionParams& extensionParams) + -> nn::GeneralResult> { + return OperandExtraParams::make( + extensionParams); + }, + }, + extraParams); +} + +nn::GeneralResult 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 unvalidatedConvert(const nn::OperationType& operationType) { + return static_cast(operationType); +} + +nn::GeneralResult 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 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> unvalidatedConvert( + const nn::Model::OperandValues& operandValues) { + return std::vector(operandValues.data(), operandValues.data() + operandValues.size()); +} + +nn::GeneralResult unvalidatedConvert( + const nn::Model::ExtensionNameAndPrefix& extensionNameToPrefix) { + return ExtensionNameAndPrefix{ + .name = extensionNameToPrefix.name, + .prefix = extensionNameToPrefix.prefix, + }; +} + +nn::GeneralResult 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 unvalidatedConvert(const nn::Priority& priority) { + return static_cast(priority); +} + +nn::GeneralResult 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 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 unvalidatedConvert(const nn::Request::MemoryPool& memoryPool) { + return std::visit( + overloaded{ + [](const nn::SharedMemory& memory) -> nn::GeneralResult { + return RequestMemoryPool::make( + NN_TRY(unvalidatedConvert(memory))); + }, + [](const nn::Request::MemoryDomainToken& token) + -> nn::GeneralResult { + return RequestMemoryPool::make( + underlyingType(token)); + }, + [](const nn::SharedBuffer& /*buffer*/) { + return (NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) + << "Unable to make memory pool from IBuffer") + . + operator nn::GeneralResult(); + }, + }, + memoryPool); +} + +nn::GeneralResult unvalidatedConvert(const nn::Timing& timing) { + return Timing{ + .timeOnDevice = NN_TRY(unvalidatedConvert(timing.timeOnDevice)), + .timeInDriver = NN_TRY(unvalidatedConvert(timing.timeInDriver)), + }; +} + +nn::GeneralResult unvalidatedConvert(const nn::Duration& duration) { + const uint64_t nanoseconds = duration.count(); + if (nanoseconds > std::numeric_limits::max()) { + return std::numeric_limits::max(); + } + return static_cast(nanoseconds); +} + +nn::GeneralResult unvalidatedConvert(const nn::OptionalDuration& optionalDuration) { + if (!optionalDuration.has_value()) { + return kNoTiming; + } + return unvalidatedConvert(optionalDuration.value()); +} + +nn::GeneralResult unvalidatedConvert(const nn::OptionalTimePoint& optionalTimePoint) { + if (!optionalTimePoint.has_value()) { + return kNoTiming; + } + return unvalidatedConvert(optionalTimePoint->time_since_epoch()); +} + +nn::GeneralResult unvalidatedConvert(const nn::SyncFence& syncFence) { + auto duplicatedFd = NN_TRY(nn::dupFd(syncFence.getFd())); + return ndk::ScopedFileDescriptor(duplicatedFd.release()); +} + +nn::GeneralResult 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> convert(const nn::CacheToken& cacheToken) { + return unvalidatedConvert(cacheToken); +} + +nn::GeneralResult convert(const nn::BufferDesc& bufferDesc) { + return validatedConvert(bufferDesc); +} + +nn::GeneralResult convert(const nn::MeasureTiming& measureTiming) { + return validatedConvert(measureTiming); +} + nn::GeneralResult convert(const nn::SharedMemory& memory) { return validatedConvert(memory); } @@ -715,11 +1017,62 @@ nn::GeneralResult convert(const nn::ErrorStatus& errorStatus) { return validatedConvert(errorStatus); } +nn::GeneralResult convert(const nn::ExecutionPreference& executionPreference) { + return validatedConvert(executionPreference); +} + +nn::GeneralResult convert(const nn::Model& model) { + return validatedConvert(model); +} + +nn::GeneralResult convert(const nn::Priority& priority) { + return validatedConvert(priority); +} + +nn::GeneralResult convert(const nn::Request& request) { + return validatedConvert(request); +} + +nn::GeneralResult convert(const nn::Timing& timing) { + return validatedConvert(timing); +} + +nn::GeneralResult convert(const nn::OptionalDuration& optionalDuration) { + return validatedConvert(optionalDuration); +} + +nn::GeneralResult convert(const nn::OptionalTimePoint& outputShapes) { + return validatedConvert(outputShapes); +} + +nn::GeneralResult> convert(const std::vector& bufferRoles) { + return validatedConvert(bufferRoles); +} + nn::GeneralResult> convert( const std::vector& outputShapes) { return validatedConvert(outputShapes); } +nn::GeneralResult> convert( + const std::vector& 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 cacheFds; + cacheFds.reserve(cacheHandles.size()); + for (const auto& cacheHandle : cacheHandles) { + cacheFds.push_back(NN_TRY(unvalidatedConvertCache(cacheHandle))); + } + return cacheFds; +} + +nn::GeneralResult> convert( + const std::vector& syncFences) { + return unvalidatedConvert(syncFences); +} + nn::GeneralResult> toSigned(const std::vector& vec) { if (!std::all_of(vec.begin(), vec.end(), [](uint32_t v) { return v <= std::numeric_limits::max(); })) { diff --git a/neuralnetworks/aidl/utils/src/Device.cpp b/neuralnetworks/aidl/utils/src/Device.cpp new file mode 100644 index 0000000000..02ca861b6b --- /dev/null +++ b/neuralnetworks/aidl/utils/src/Device.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +// 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>> convert( + const std::vector& preparedModels) { + std::vector> 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>(&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 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 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 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> getSupportedExtensionsFrom( + aidl_hal::IDevice* device) { + CHECK(device != nullptr); + std::vector supportedExtensions; + const auto ret = device->getSupportedExtensions(&supportedExtensions); + HANDLE_ASTATUS(ret) << "getExtensions failed"; + return nn::convert(supportedExtensions); +} + +nn::GeneralResult> 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(numberOfCacheFiles.numModelCache) > nn::kMaxNumberOfCacheFiles) { + return NN_ERROR() << "getNumberOfCacheFilesNeeded returned numModelCache files greater " + "than allowed max (" + << numberOfCacheFiles.numModelCache << " vs " + << nn::kMaxNumberOfCacheFiles << ")"; + } + if (static_cast(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> Device::create( + std::string name, std::shared_ptr 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( + 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 extensions, + nn::Capabilities capabilities, + std::pair numberOfCacheFilesNeeded, + std::shared_ptr 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& Device::getSupportedExtensions() const { + return kExtensions; +} + +const nn::Capabilities& Device::getCapabilities() const { + return kCapabilities; +} + +std::pair Device::getNumberOfCacheFilesNeeded() const { + return kNumberOfCacheFilesNeeded; +} + +nn::GeneralResult Device::wait() const { + const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_ping(kDevice->asBinder().get())); + HANDLE_ASTATUS(ret) << "ping failed"; + return {}; +} + +nn::GeneralResult> Device::getSupportedOperations(const nn::Model& model) const { + // Ensure that model is ready for IPC. + std::optional maybeModelInShared; + const nn::Model& modelInShared = + NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared)); + + const auto aidlModel = NN_TRY(convert(modelInShared)); + + std::vector supportedOperations; + const auto ret = kDevice->getSupportedOperations(aidlModel, &supportedOperations); + HANDLE_ASTATUS(ret) << "getSupportedOperations failed"; + + return supportedOperations; +} + +nn::GeneralResult Device::prepareModel( + const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority, + nn::OptionalTimePoint deadline, const std::vector& modelCache, + const std::vector& dataCache, const nn::CacheToken& token) const { + // Ensure that model is ready for IPC. + std::optional 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(); + 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 Device::prepareModelFromCache( + nn::OptionalTimePoint deadline, const std::vector& modelCache, + const std::vector& 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(); + 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 Device::allocate( + const nn::BufferDesc& desc, const std::vector& preparedModels, + const std::vector& inputRoles, + const std::vector& 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 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(buffer.token)); +} + +DeathMonitor* Device::getDeathMonitor() const { + return kDeathHandler.getDeathMonitor().get(); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/src/PreparedModel.cpp b/neuralnetworks/aidl/utils/src/PreparedModel.cpp new file mode 100644 index 0000000000..aee4d90dab --- /dev/null +++ b/neuralnetworks/aidl/utils/src/PreparedModel.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +// 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, nn::Timing>> convertExecutionResults( + const std::vector& outputShapes, const Timing& timing) { + return std::make_pair(NN_TRY(nn::convert(outputShapes)), NN_TRY(nn::convert(timing))); +} + +nn::GeneralResult> 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> PreparedModel::create( + std::shared_ptr preparedModel) { + if (preparedModel == nullptr) { + return NN_ERROR() + << "aidl_hal::utils::PreparedModel::create must have non-null preparedModel"; + } + + return std::make_shared(PrivateConstructorTag{}, std::move(preparedModel)); +} + +PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, + std::shared_ptr preparedModel) + : kPreparedModel(std::move(preparedModel)) {} + +nn::ExecutionResult, 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 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{}); + 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> +PreparedModel::executeFenced(const nn::Request& request, const std::vector& 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 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> { + 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 PreparedModel::configureExecutionBurst() const { + return hal::V1_0::utils::Burst::create(shared_from_this()); +} + +std::any PreparedModel::getUnderlyingResource() const { + std::shared_ptr resource = kPreparedModel; + return resource; +} + +} // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/src/ProtectCallback.cpp b/neuralnetworks/aidl/utils/src/ProtectCallback.cpp new file mode 100644 index 0000000000..124641cbb8 --- /dev/null +++ b/neuralnetworks/aidl/utils/src/ProtectCallback.cpp @@ -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 +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#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(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::create(std::shared_ptr object) { + if (object == nullptr) { + return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) + << "utils::DeathHandler::create must have non-null object"; + } + auto deathMonitor = std::make_shared(); + 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 object, + ndk::ScopedAIBinder_DeathRecipient deathRecipient, + std::shared_ptr 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::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 diff --git a/neuralnetworks/aidl/utils/src/Service.cpp b/neuralnetworks/aidl/utils/src/Service.cpp new file mode 100644 index 0000000000..5ec6ded5e5 --- /dev/null +++ b/neuralnetworks/aidl/utils/src/Service.cpp @@ -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 +#include + +#include +#include +#include +#include +#include + +#include "Device.h" + +namespace aidl::android::hardware::neuralnetworks::utils { + +nn::GeneralResult getDevice(const std::string& name) { + hal::utils::ResilientDevice::Factory makeDevice = + [name](bool blocking) -> nn::GeneralResult { + 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 diff --git a/neuralnetworks/aidl/utils/src/Utils.cpp b/neuralnetworks/aidl/utils/src/Utils.cpp index 8d00e5926a..95516c854b 100644 --- a/neuralnetworks/aidl/utils/src/Utils.cpp +++ b/neuralnetworks/aidl/utils/src/Utils.cpp @@ -16,13 +16,12 @@ #include "Utils.h" +#include #include namespace aidl::android::hardware::neuralnetworks::utils { namespace { -using ::android::nn::GeneralResult; - template nn::GeneralResult> cloneVec(const std::vector& arguments) { std::vector clonedObjects; @@ -34,13 +33,13 @@ nn::GeneralResult> cloneVec(const std::vector& arguments } template -GeneralResult> clone(const std::vector& arguments) { +nn::GeneralResult> clone(const std::vector& arguments) { return cloneVec(arguments); } } // namespace -GeneralResult clone(const Memory& memory) { +nn::GeneralResult clone(const Memory& memory) { common::NativeHandle nativeHandle; nativeHandle.ints = memory.handle.ints; nativeHandle.fds.reserve(memory.handle.fds.size()); @@ -58,7 +57,7 @@ GeneralResult clone(const Memory& memory) { }; } -GeneralResult clone(const RequestMemoryPool& requestPool) { +nn::GeneralResult clone(const RequestMemoryPool& requestPool) { using Tag = RequestMemoryPool::Tag; switch (requestPool.getTag()) { case Tag::pool: @@ -70,10 +69,10 @@ GeneralResult clone(const RequestMemoryPool& requestPool) { // compiler. return (NN_ERROR() << "Unrecognized request pool tag: " << requestPool.getTag()) . - operator GeneralResult(); + operator nn::GeneralResult(); } -GeneralResult clone(const Request& request) { +nn::GeneralResult clone(const Request& request) { return Request{ .inputs = request.inputs, .outputs = request.outputs, @@ -81,7 +80,7 @@ GeneralResult clone(const Request& request) { }; } -GeneralResult clone(const Model& model) { +nn::GeneralResult clone(const Model& model) { return Model{ .main = model.main, .referenced = model.referenced, @@ -92,4 +91,20 @@ GeneralResult clone(const Model& model) { }; } +nn::GeneralResult 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(ret.getServiceSpecificError())) + << ret.getMessage(); +} + } // namespace aidl::android::hardware::neuralnetworks::utils -- cgit v1.2.3