Loading neuralnetworks/1.3/utils/src/Conversions.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -239,7 +239,7 @@ GeneralResult<Request::MemoryPool> unvalidatedConvert( using Discriminator = hal::V1_3::Request::MemoryPool::hidl_discriminator; switch (memoryPool.getDiscriminator()) { case Discriminator::hidlMemory: return hal::utils::createSharedMemoryFromHidlMemory(memoryPool.hidlMemory()); return unvalidatedConvert(memoryPool.hidlMemory()); case Discriminator::token: return static_cast<Request::MemoryDomainToken>(memoryPool.token()); } Loading neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Conversions.h +3 −7 Original line number Diff line number Diff line Loading @@ -95,9 +95,7 @@ GeneralResult<ExecutionPreference> unvalidatedConvert( GeneralResult<Extension> unvalidatedConvert(const aidl_hal::Extension& extension); 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<SharedHandle> unvalidatedConvert(const ndk::ScopedFileDescriptor& handle); GeneralResult<std::vector<Operation>> unvalidatedConvert( const std::vector<aidl_hal::Operation>& operations); Loading @@ -113,7 +111,7 @@ GeneralResult<OperandType> convert(const aidl_hal::OperandType& operandType); GeneralResult<Priority> convert(const aidl_hal::Priority& priority); GeneralResult<Request> convert(const aidl_hal::Request& request); GeneralResult<Timing> convert(const aidl_hal::Timing& timing); GeneralResult<SyncFence> convert(const ndk::ScopedFileDescriptor& syncFence); GeneralResult<SharedHandle> convert(const ndk::ScopedFileDescriptor& handle); GeneralResult<std::vector<Extension>> convert(const std::vector<aidl_hal::Extension>& extension); GeneralResult<std::vector<SharedMemory>> convert(const std::vector<aidl_hal::Memory>& memories); Loading Loading @@ -160,9 +158,7 @@ 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<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SharedHandle& handle); nn::GeneralResult<std::vector<uint8_t>> convert(const nn::CacheToken& cacheToken); nn::GeneralResult<BufferDesc> convert(const nn::BufferDesc& bufferDesc); Loading neuralnetworks/aidl/utils/src/Conversions.cpp +8 −56 Original line number Diff line number Diff line Loading @@ -108,17 +108,6 @@ GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> validatedConvert( return canonical; } GeneralResult<Handle> unvalidatedConvertHelper(const NativeHandle& aidlNativeHandle) { std::vector<base::unique_fd> fds; fds.reserve(aidlNativeHandle.fds.size()); for (const auto& fd : aidlNativeHandle.fds) { auto duplicatedFd = NN_TRY(dupFd(fd.get())); fds.emplace_back(duplicatedFd.release()); } return Handle{.fds = std::move(fds), .ints = aidlNativeHandle.ints}; } struct NativeHandleDeleter { void operator()(native_handle_t* handle) const { if (handle) { Loading Loading @@ -498,18 +487,14 @@ GeneralResult<ExecutionPreference> unvalidatedConvert( return static_cast<ExecutionPreference>(executionPreference); } GeneralResult<SharedHandle> unvalidatedConvert(const NativeHandle& aidlNativeHandle) { return std::make_shared<const Handle>(NN_TRY(unvalidatedConvertHelper(aidlNativeHandle))); } GeneralResult<std::vector<Operation>> unvalidatedConvert( const std::vector<aidl_hal::Operation>& operations) { return unvalidatedConvertVec(operations); } GeneralResult<SyncFence> unvalidatedConvert(const ndk::ScopedFileDescriptor& syncFence) { auto duplicatedFd = NN_TRY(dupFd(syncFence.get())); return SyncFence::create(std::move(duplicatedFd)); GeneralResult<SharedHandle> unvalidatedConvert(const ndk::ScopedFileDescriptor& handle) { auto duplicatedFd = NN_TRY(dupFd(handle.get())); return std::make_shared<const Handle>(std::move(duplicatedFd)); } GeneralResult<Capabilities> convert(const aidl_hal::Capabilities& capabilities) { Loading Loading @@ -553,8 +538,8 @@ GeneralResult<Timing> convert(const aidl_hal::Timing& timing) { return validatedConvert(timing); } GeneralResult<SyncFence> convert(const ndk::ScopedFileDescriptor& syncFence) { return validatedConvert(syncFence); GeneralResult<SharedHandle> convert(const ndk::ScopedFileDescriptor& handle) { return validatedConvert(handle); } GeneralResult<std::vector<Extension>> convert(const std::vector<aidl_hal::Extension>& extension) { Loading Loading @@ -619,17 +604,6 @@ nn::GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> validatedConvert( return halObject; } nn::GeneralResult<common::NativeHandle> unvalidatedConvert(const nn::Handle& handle) { common::NativeHandle aidlNativeHandle; aidlNativeHandle.fds.reserve(handle.fds.size()); for (const auto& fd : handle.fds) { 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... { Loading Loading @@ -755,11 +729,6 @@ nn::GeneralResult<bool> unvalidatedConvert(const nn::MeasureTiming& measureTimin return measureTiming == nn::MeasureTiming::YES; } nn::GeneralResult<common::NativeHandle> unvalidatedConvert(const nn::SharedHandle& sharedHandle) { CHECK(sharedHandle != nullptr); return unvalidatedConvert(*sharedHandle); } nn::GeneralResult<Memory> unvalidatedConvert(const nn::SharedMemory& memory) { if (memory == nullptr) { return (NN_ERROR() << "Unable to convert nullptr memory") Loading Loading @@ -997,16 +966,8 @@ nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SyncFe 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())); nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SharedHandle& handle) { auto duplicatedFd = NN_TRY(nn::dupFd(handle->get())); return ndk::ScopedFileDescriptor(duplicatedFd.release()); } Loading Loading @@ -1069,16 +1030,7 @@ nn::GeneralResult<std::vector<OutputShape>> convert( 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; return validatedConvert(cacheHandles); } nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert( Loading neuralnetworks/aidl/utils/src/PreparedModel.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -160,7 +160,7 @@ PreparedModel::executeFencedInternal(const Request& request, auto resultSyncFence = nn::SyncFence::createAsSignaled(); if (result.syncFence.get() != -1) { resultSyncFence = NN_TRY(nn::convert(result.syncFence)); resultSyncFence = nn::SyncFence::create(NN_TRY(nn::convert(result.syncFence))).value(); } auto callback = result.callback; Loading neuralnetworks/utils/common/src/CommonUtils.cpp +43 −46 Original line number Diff line number Diff line Loading @@ -89,23 +89,39 @@ void copyPointersToSharedMemory(nn::Model::Subgraph* subgraph, }); } nn::GeneralResult<hidl_handle> createNativeHandleFrom(base::unique_fd fd, nn::GeneralResult<hidl_handle> createNativeHandleFrom(std::vector<base::unique_fd> fds, const std::vector<int32_t>& ints) { constexpr size_t kIntMax = std::numeric_limits<int>::max(); CHECK_LE(fds.size(), kIntMax); CHECK_LE(ints.size(), kIntMax); native_handle_t* nativeHandle = native_handle_create(1, static_cast<int>(ints.size())); native_handle_t* nativeHandle = native_handle_create(static_cast<int>(fds.size()), static_cast<int>(ints.size())); if (nativeHandle == nullptr) { return NN_ERROR() << "Failed to create native_handle"; } nativeHandle->data[0] = fd.release(); std::copy(ints.begin(), ints.end(), nativeHandle->data + 1); for (size_t i = 0; i < fds.size(); ++i) { nativeHandle->data[i] = fds[i].release(); } std::copy(ints.begin(), ints.end(), nativeHandle->data + nativeHandle->numFds); hidl_handle handle; handle.setTo(nativeHandle, /*shouldOwn=*/true); return handle; } nn::GeneralResult<hidl_handle> createNativeHandleFrom(base::unique_fd fd, const std::vector<int32_t>& ints) { std::vector<base::unique_fd> fds; fds.push_back(std::move(fd)); return createNativeHandleFrom(std::move(fds), ints); } nn::GeneralResult<hidl_handle> createNativeHandleFrom(const nn::Memory::Unknown::Handle& handle) { std::vector<base::unique_fd> fds = NN_TRY(nn::dupFds(handle.fds.begin(), handle.fds.end())); return createNativeHandleFrom(std::move(fds), handle.ints); } nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Ashmem& memory) { auto fd = NN_TRY(nn::dupFd(memory.fd)); auto handle = NN_TRY(createNativeHandleFrom(std::move(fd), {})); Loading Loading @@ -139,7 +155,22 @@ nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::HardwareBu } nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Unknown& memory) { return hidl_memory(memory.name, NN_TRY(hidlHandleFromSharedHandle(memory.handle)), memory.size); return hidl_memory(memory.name, NN_TRY(createNativeHandleFrom(memory.handle)), memory.size); } nn::GeneralResult<nn::Memory::Unknown::Handle> unknownHandleFromNativeHandle( const native_handle_t* handle) { if (handle == nullptr) { return NN_ERROR() << "unknownHandleFromNativeHandle failed because handle is nullptr"; } std::vector<base::unique_fd> fds = NN_TRY(nn::dupFds(handle->data + 0, handle->data + handle->numFds)); std::vector<int> ints(handle->data + handle->numFds, handle->data + handle->numFds + handle->numInts); return nn::Memory::Unknown::Handle{.fds = std::move(fds), .ints = std::move(ints)}; } } // anonymous namespace Loading Loading @@ -349,7 +380,7 @@ nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_ if (memory.name() != "hardware_buffer_blob") { auto handle = nn::Memory::Unknown{ .handle = NN_TRY(sharedHandleFromNativeHandle(memory.handle())), .handle = NN_TRY(unknownHandleFromNativeHandle(memory.handle())), .size = static_cast<size_t>(memory.size()), .name = memory.name(), }; Loading Loading @@ -395,53 +426,19 @@ nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_ } nn::GeneralResult<hidl_handle> hidlHandleFromSharedHandle(const nn::Handle& handle) { std::vector<base::unique_fd> fds; fds.reserve(handle.fds.size()); for (const auto& fd : handle.fds) { const int dupFd = dup(fd); if (dupFd == -1) { return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to dup the fd"; } fds.emplace_back(dupFd); } constexpr size_t kIntMax = std::numeric_limits<int>::max(); CHECK_LE(handle.fds.size(), kIntMax); CHECK_LE(handle.ints.size(), kIntMax); native_handle_t* nativeHandle = native_handle_create(static_cast<int>(handle.fds.size()), static_cast<int>(handle.ints.size())); if (nativeHandle == nullptr) { return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to create native_handle"; } for (size_t i = 0; i < fds.size(); ++i) { nativeHandle->data[i] = fds[i].release(); } std::copy(handle.ints.begin(), handle.ints.end(), &nativeHandle->data[nativeHandle->numFds]); hidl_handle hidlHandle; hidlHandle.setTo(nativeHandle, /*shouldOwn=*/true); return hidlHandle; base::unique_fd fd = NN_TRY(nn::dupFd(handle.get())); return createNativeHandleFrom(std::move(fd), {}); } nn::GeneralResult<nn::Handle> sharedHandleFromNativeHandle(const native_handle_t* handle) { if (handle == nullptr) { return NN_ERROR() << "sharedHandleFromNativeHandle failed because handle is nullptr"; } std::vector<base::unique_fd> fds; 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"; } fds.emplace_back(dupFd); if (handle->numFds != 1 || handle->numInts != 0) { return NN_ERROR() << "sharedHandleFromNativeHandle failed because handle does not only " "hold a single fd"; } std::vector<int> ints(&handle->data[handle->numFds], &handle->data[handle->numFds + handle->numInts]); return nn::Handle{.fds = std::move(fds), .ints = std::move(ints)}; return nn::dupFd(handle->data[0]); } nn::GeneralResult<hidl_vec<hidl_handle>> convertSyncFences( Loading Loading
neuralnetworks/1.3/utils/src/Conversions.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -239,7 +239,7 @@ GeneralResult<Request::MemoryPool> unvalidatedConvert( using Discriminator = hal::V1_3::Request::MemoryPool::hidl_discriminator; switch (memoryPool.getDiscriminator()) { case Discriminator::hidlMemory: return hal::utils::createSharedMemoryFromHidlMemory(memoryPool.hidlMemory()); return unvalidatedConvert(memoryPool.hidlMemory()); case Discriminator::token: return static_cast<Request::MemoryDomainToken>(memoryPool.token()); } Loading
neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Conversions.h +3 −7 Original line number Diff line number Diff line Loading @@ -95,9 +95,7 @@ GeneralResult<ExecutionPreference> unvalidatedConvert( GeneralResult<Extension> unvalidatedConvert(const aidl_hal::Extension& extension); 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<SharedHandle> unvalidatedConvert(const ndk::ScopedFileDescriptor& handle); GeneralResult<std::vector<Operation>> unvalidatedConvert( const std::vector<aidl_hal::Operation>& operations); Loading @@ -113,7 +111,7 @@ GeneralResult<OperandType> convert(const aidl_hal::OperandType& operandType); GeneralResult<Priority> convert(const aidl_hal::Priority& priority); GeneralResult<Request> convert(const aidl_hal::Request& request); GeneralResult<Timing> convert(const aidl_hal::Timing& timing); GeneralResult<SyncFence> convert(const ndk::ScopedFileDescriptor& syncFence); GeneralResult<SharedHandle> convert(const ndk::ScopedFileDescriptor& handle); GeneralResult<std::vector<Extension>> convert(const std::vector<aidl_hal::Extension>& extension); GeneralResult<std::vector<SharedMemory>> convert(const std::vector<aidl_hal::Memory>& memories); Loading Loading @@ -160,9 +158,7 @@ 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<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SharedHandle& handle); nn::GeneralResult<std::vector<uint8_t>> convert(const nn::CacheToken& cacheToken); nn::GeneralResult<BufferDesc> convert(const nn::BufferDesc& bufferDesc); Loading
neuralnetworks/aidl/utils/src/Conversions.cpp +8 −56 Original line number Diff line number Diff line Loading @@ -108,17 +108,6 @@ GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> validatedConvert( return canonical; } GeneralResult<Handle> unvalidatedConvertHelper(const NativeHandle& aidlNativeHandle) { std::vector<base::unique_fd> fds; fds.reserve(aidlNativeHandle.fds.size()); for (const auto& fd : aidlNativeHandle.fds) { auto duplicatedFd = NN_TRY(dupFd(fd.get())); fds.emplace_back(duplicatedFd.release()); } return Handle{.fds = std::move(fds), .ints = aidlNativeHandle.ints}; } struct NativeHandleDeleter { void operator()(native_handle_t* handle) const { if (handle) { Loading Loading @@ -498,18 +487,14 @@ GeneralResult<ExecutionPreference> unvalidatedConvert( return static_cast<ExecutionPreference>(executionPreference); } GeneralResult<SharedHandle> unvalidatedConvert(const NativeHandle& aidlNativeHandle) { return std::make_shared<const Handle>(NN_TRY(unvalidatedConvertHelper(aidlNativeHandle))); } GeneralResult<std::vector<Operation>> unvalidatedConvert( const std::vector<aidl_hal::Operation>& operations) { return unvalidatedConvertVec(operations); } GeneralResult<SyncFence> unvalidatedConvert(const ndk::ScopedFileDescriptor& syncFence) { auto duplicatedFd = NN_TRY(dupFd(syncFence.get())); return SyncFence::create(std::move(duplicatedFd)); GeneralResult<SharedHandle> unvalidatedConvert(const ndk::ScopedFileDescriptor& handle) { auto duplicatedFd = NN_TRY(dupFd(handle.get())); return std::make_shared<const Handle>(std::move(duplicatedFd)); } GeneralResult<Capabilities> convert(const aidl_hal::Capabilities& capabilities) { Loading Loading @@ -553,8 +538,8 @@ GeneralResult<Timing> convert(const aidl_hal::Timing& timing) { return validatedConvert(timing); } GeneralResult<SyncFence> convert(const ndk::ScopedFileDescriptor& syncFence) { return validatedConvert(syncFence); GeneralResult<SharedHandle> convert(const ndk::ScopedFileDescriptor& handle) { return validatedConvert(handle); } GeneralResult<std::vector<Extension>> convert(const std::vector<aidl_hal::Extension>& extension) { Loading Loading @@ -619,17 +604,6 @@ nn::GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> validatedConvert( return halObject; } nn::GeneralResult<common::NativeHandle> unvalidatedConvert(const nn::Handle& handle) { common::NativeHandle aidlNativeHandle; aidlNativeHandle.fds.reserve(handle.fds.size()); for (const auto& fd : handle.fds) { 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... { Loading Loading @@ -755,11 +729,6 @@ nn::GeneralResult<bool> unvalidatedConvert(const nn::MeasureTiming& measureTimin return measureTiming == nn::MeasureTiming::YES; } nn::GeneralResult<common::NativeHandle> unvalidatedConvert(const nn::SharedHandle& sharedHandle) { CHECK(sharedHandle != nullptr); return unvalidatedConvert(*sharedHandle); } nn::GeneralResult<Memory> unvalidatedConvert(const nn::SharedMemory& memory) { if (memory == nullptr) { return (NN_ERROR() << "Unable to convert nullptr memory") Loading Loading @@ -997,16 +966,8 @@ nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SyncFe 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())); nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SharedHandle& handle) { auto duplicatedFd = NN_TRY(nn::dupFd(handle->get())); return ndk::ScopedFileDescriptor(duplicatedFd.release()); } Loading Loading @@ -1069,16 +1030,7 @@ nn::GeneralResult<std::vector<OutputShape>> convert( 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; return validatedConvert(cacheHandles); } nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert( Loading
neuralnetworks/aidl/utils/src/PreparedModel.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -160,7 +160,7 @@ PreparedModel::executeFencedInternal(const Request& request, auto resultSyncFence = nn::SyncFence::createAsSignaled(); if (result.syncFence.get() != -1) { resultSyncFence = NN_TRY(nn::convert(result.syncFence)); resultSyncFence = nn::SyncFence::create(NN_TRY(nn::convert(result.syncFence))).value(); } auto callback = result.callback; Loading
neuralnetworks/utils/common/src/CommonUtils.cpp +43 −46 Original line number Diff line number Diff line Loading @@ -89,23 +89,39 @@ void copyPointersToSharedMemory(nn::Model::Subgraph* subgraph, }); } nn::GeneralResult<hidl_handle> createNativeHandleFrom(base::unique_fd fd, nn::GeneralResult<hidl_handle> createNativeHandleFrom(std::vector<base::unique_fd> fds, const std::vector<int32_t>& ints) { constexpr size_t kIntMax = std::numeric_limits<int>::max(); CHECK_LE(fds.size(), kIntMax); CHECK_LE(ints.size(), kIntMax); native_handle_t* nativeHandle = native_handle_create(1, static_cast<int>(ints.size())); native_handle_t* nativeHandle = native_handle_create(static_cast<int>(fds.size()), static_cast<int>(ints.size())); if (nativeHandle == nullptr) { return NN_ERROR() << "Failed to create native_handle"; } nativeHandle->data[0] = fd.release(); std::copy(ints.begin(), ints.end(), nativeHandle->data + 1); for (size_t i = 0; i < fds.size(); ++i) { nativeHandle->data[i] = fds[i].release(); } std::copy(ints.begin(), ints.end(), nativeHandle->data + nativeHandle->numFds); hidl_handle handle; handle.setTo(nativeHandle, /*shouldOwn=*/true); return handle; } nn::GeneralResult<hidl_handle> createNativeHandleFrom(base::unique_fd fd, const std::vector<int32_t>& ints) { std::vector<base::unique_fd> fds; fds.push_back(std::move(fd)); return createNativeHandleFrom(std::move(fds), ints); } nn::GeneralResult<hidl_handle> createNativeHandleFrom(const nn::Memory::Unknown::Handle& handle) { std::vector<base::unique_fd> fds = NN_TRY(nn::dupFds(handle.fds.begin(), handle.fds.end())); return createNativeHandleFrom(std::move(fds), handle.ints); } nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Ashmem& memory) { auto fd = NN_TRY(nn::dupFd(memory.fd)); auto handle = NN_TRY(createNativeHandleFrom(std::move(fd), {})); Loading Loading @@ -139,7 +155,22 @@ nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::HardwareBu } nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Unknown& memory) { return hidl_memory(memory.name, NN_TRY(hidlHandleFromSharedHandle(memory.handle)), memory.size); return hidl_memory(memory.name, NN_TRY(createNativeHandleFrom(memory.handle)), memory.size); } nn::GeneralResult<nn::Memory::Unknown::Handle> unknownHandleFromNativeHandle( const native_handle_t* handle) { if (handle == nullptr) { return NN_ERROR() << "unknownHandleFromNativeHandle failed because handle is nullptr"; } std::vector<base::unique_fd> fds = NN_TRY(nn::dupFds(handle->data + 0, handle->data + handle->numFds)); std::vector<int> ints(handle->data + handle->numFds, handle->data + handle->numFds + handle->numInts); return nn::Memory::Unknown::Handle{.fds = std::move(fds), .ints = std::move(ints)}; } } // anonymous namespace Loading Loading @@ -349,7 +380,7 @@ nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_ if (memory.name() != "hardware_buffer_blob") { auto handle = nn::Memory::Unknown{ .handle = NN_TRY(sharedHandleFromNativeHandle(memory.handle())), .handle = NN_TRY(unknownHandleFromNativeHandle(memory.handle())), .size = static_cast<size_t>(memory.size()), .name = memory.name(), }; Loading Loading @@ -395,53 +426,19 @@ nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_ } nn::GeneralResult<hidl_handle> hidlHandleFromSharedHandle(const nn::Handle& handle) { std::vector<base::unique_fd> fds; fds.reserve(handle.fds.size()); for (const auto& fd : handle.fds) { const int dupFd = dup(fd); if (dupFd == -1) { return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to dup the fd"; } fds.emplace_back(dupFd); } constexpr size_t kIntMax = std::numeric_limits<int>::max(); CHECK_LE(handle.fds.size(), kIntMax); CHECK_LE(handle.ints.size(), kIntMax); native_handle_t* nativeHandle = native_handle_create(static_cast<int>(handle.fds.size()), static_cast<int>(handle.ints.size())); if (nativeHandle == nullptr) { return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to create native_handle"; } for (size_t i = 0; i < fds.size(); ++i) { nativeHandle->data[i] = fds[i].release(); } std::copy(handle.ints.begin(), handle.ints.end(), &nativeHandle->data[nativeHandle->numFds]); hidl_handle hidlHandle; hidlHandle.setTo(nativeHandle, /*shouldOwn=*/true); return hidlHandle; base::unique_fd fd = NN_TRY(nn::dupFd(handle.get())); return createNativeHandleFrom(std::move(fd), {}); } nn::GeneralResult<nn::Handle> sharedHandleFromNativeHandle(const native_handle_t* handle) { if (handle == nullptr) { return NN_ERROR() << "sharedHandleFromNativeHandle failed because handle is nullptr"; } std::vector<base::unique_fd> fds; 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"; } fds.emplace_back(dupFd); if (handle->numFds != 1 || handle->numInts != 0) { return NN_ERROR() << "sharedHandleFromNativeHandle failed because handle does not only " "hold a single fd"; } std::vector<int> ints(&handle->data[handle->numFds], &handle->data[handle->numFds + handle->numInts]); return nn::Handle{.fds = std::move(fds), .ints = std::move(ints)}; return nn::dupFd(handle->data[0]); } nn::GeneralResult<hidl_vec<hidl_handle>> convertSyncFences( Loading