diff options
author | Haamed Gheibi <haamed@google.com> | 2021-09-30 03:31:03 +0000 |
---|---|---|
committer | Haamed Gheibi <haamed@google.com> | 2021-09-30 03:32:37 +0000 |
commit | 0d25a36c4a05b7dc1fe833d311cba0abd1979db7 (patch) | |
tree | b9bb668d79bdb511337c63d71f724fd7c5d16cad /neuralnetworks/utils/common/src/CommonUtils.cpp | |
parent | 41b783ac627a1562c9959d2a309339c43e89ee95 (diff) | |
parent | e58dbcf0de859ade425f22b62a45259449ae538d (diff) |
Merge TP1A.210812.002
Bug: 198367246
Change-Id: I64df13aefdc06c8bb9f9b3a4256c41879e15e515
Diffstat (limited to 'neuralnetworks/utils/common/src/CommonUtils.cpp')
-rw-r--r-- | neuralnetworks/utils/common/src/CommonUtils.cpp | 89 |
1 files changed, 43 insertions, 46 deletions
diff --git a/neuralnetworks/utils/common/src/CommonUtils.cpp b/neuralnetworks/utils/common/src/CommonUtils.cpp index 8e55bf0253..906b0cf32b 100644 --- a/neuralnetworks/utils/common/src/CommonUtils.cpp +++ b/neuralnetworks/utils/common/src/CommonUtils.cpp @@ -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), {})); @@ -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 @@ -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(), }; @@ -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( |