Loading neuralnetworks/aidl/vts/functional/GeneratedTestHarness.cpp +2 −2 Original line number Diff line number Diff line Loading @@ -435,8 +435,8 @@ std::optional<Request> ExecutionContext::createRequest(const TestModel& testMode mInputMemory = TestBlobAHWB::create(std::max<size_t>(inputSize, 1)); mOutputMemory = TestBlobAHWB::create(std::max<size_t>(outputSize, 1)); } else { mInputMemory = TestAshmem::create(std::max<size_t>(inputSize, 1)); mOutputMemory = TestAshmem::create(std::max<size_t>(outputSize, 1)); mInputMemory = TestAshmem::create(std::max<size_t>(inputSize, 1), /*aidlReadonly=*/true); mOutputMemory = TestAshmem::create(std::max<size_t>(outputSize, 1), /*aidlReadonly=*/false); } CHECK_NE(mInputMemory, nullptr); CHECK_NE(mOutputMemory, nullptr); Loading neuralnetworks/aidl/vts/functional/Utils.cpp +25 −4 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ #include <android/binder_status.h> #include <android/hardware_buffer.h> #include <sys/mman.h> #include <iostream> #include <limits> #include <numeric> Loading Loading @@ -98,19 +99,39 @@ uint32_t sizeOfData(const Operand& operand) { std::multiplies<>{}); } std::unique_ptr<TestAshmem> TestAshmem::create(uint32_t size) { auto ashmem = std::make_unique<TestAshmem>(size); std::unique_ptr<TestAshmem> TestAshmem::create(uint32_t size, bool aidlReadonly) { auto ashmem = std::make_unique<TestAshmem>(size, aidlReadonly); return ashmem->mIsValid ? std::move(ashmem) : nullptr; } void TestAshmem::initialize(uint32_t size) { // This function will create a readonly shared memory with PROT_READ only. // The input shared memory must be either Ashmem or mapped-FD. static nn::SharedMemory convertSharedMemoryToReadonly(const nn::SharedMemory& sharedMemory) { if (std::holds_alternative<nn::Memory::Ashmem>(sharedMemory->handle)) { const auto& memory = std::get<nn::Memory::Ashmem>(sharedMemory->handle); return nn::createSharedMemoryFromFd(memory.size, PROT_READ, memory.fd.get(), /*offset=*/0) .value(); } else if (std::holds_alternative<nn::Memory::Fd>(sharedMemory->handle)) { const auto& memory = std::get<nn::Memory::Fd>(sharedMemory->handle); return nn::createSharedMemoryFromFd(memory.size, PROT_READ, memory.fd.get(), memory.offset) .value(); } CHECK(false) << "Unexpected shared memory type"; return sharedMemory; } void TestAshmem::initialize(uint32_t size, bool aidlReadonly) { mIsValid = false; ASSERT_GT(size, 0); const auto sharedMemory = nn::createSharedMemory(size).value(); mMappedMemory = nn::map(sharedMemory).value(); mPtr = static_cast<uint8_t*>(std::get<void*>(mMappedMemory.pointer)); CHECK_NE(mPtr, nullptr); if (aidlReadonly) { mAidlMemory = utils::convert(convertSharedMemoryToReadonly(sharedMemory)).value(); } else { mAidlMemory = utils::convert(sharedMemory).value(); } mIsValid = true; } Loading neuralnetworks/aidl/vts/functional/Utils.h +6 −3 Original line number Diff line number Diff line Loading @@ -79,15 +79,18 @@ class TestMemoryBase { class TestAshmem : public TestMemoryBase { public: static std::unique_ptr<TestAshmem> create(uint32_t size); // If aidlReadonly is true, getAidlMemory will return a sAIDL memory with readonly access; // otherwise, the sAIDL memory has read-write access. This only affects the sAIDL memory. // getPointer will always return a valid address with read-write access. static std::unique_ptr<TestAshmem> create(uint32_t size, bool aidlReadonly = false); // Prefer TestAshmem::create. // The constructor calls initialize, which constructs the memory resources. This is a workaround // that gtest macros cannot be used directly in a constructor. TestAshmem(uint32_t size) { initialize(size); } TestAshmem(uint32_t size, bool aidlReadonly) { initialize(size, aidlReadonly); } private: void initialize(uint32_t size); void initialize(uint32_t size, bool aidlReadonly); nn::Mapping mMappedMemory; }; Loading Loading
neuralnetworks/aidl/vts/functional/GeneratedTestHarness.cpp +2 −2 Original line number Diff line number Diff line Loading @@ -435,8 +435,8 @@ std::optional<Request> ExecutionContext::createRequest(const TestModel& testMode mInputMemory = TestBlobAHWB::create(std::max<size_t>(inputSize, 1)); mOutputMemory = TestBlobAHWB::create(std::max<size_t>(outputSize, 1)); } else { mInputMemory = TestAshmem::create(std::max<size_t>(inputSize, 1)); mOutputMemory = TestAshmem::create(std::max<size_t>(outputSize, 1)); mInputMemory = TestAshmem::create(std::max<size_t>(inputSize, 1), /*aidlReadonly=*/true); mOutputMemory = TestAshmem::create(std::max<size_t>(outputSize, 1), /*aidlReadonly=*/false); } CHECK_NE(mInputMemory, nullptr); CHECK_NE(mOutputMemory, nullptr); Loading
neuralnetworks/aidl/vts/functional/Utils.cpp +25 −4 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ #include <android/binder_status.h> #include <android/hardware_buffer.h> #include <sys/mman.h> #include <iostream> #include <limits> #include <numeric> Loading Loading @@ -98,19 +99,39 @@ uint32_t sizeOfData(const Operand& operand) { std::multiplies<>{}); } std::unique_ptr<TestAshmem> TestAshmem::create(uint32_t size) { auto ashmem = std::make_unique<TestAshmem>(size); std::unique_ptr<TestAshmem> TestAshmem::create(uint32_t size, bool aidlReadonly) { auto ashmem = std::make_unique<TestAshmem>(size, aidlReadonly); return ashmem->mIsValid ? std::move(ashmem) : nullptr; } void TestAshmem::initialize(uint32_t size) { // This function will create a readonly shared memory with PROT_READ only. // The input shared memory must be either Ashmem or mapped-FD. static nn::SharedMemory convertSharedMemoryToReadonly(const nn::SharedMemory& sharedMemory) { if (std::holds_alternative<nn::Memory::Ashmem>(sharedMemory->handle)) { const auto& memory = std::get<nn::Memory::Ashmem>(sharedMemory->handle); return nn::createSharedMemoryFromFd(memory.size, PROT_READ, memory.fd.get(), /*offset=*/0) .value(); } else if (std::holds_alternative<nn::Memory::Fd>(sharedMemory->handle)) { const auto& memory = std::get<nn::Memory::Fd>(sharedMemory->handle); return nn::createSharedMemoryFromFd(memory.size, PROT_READ, memory.fd.get(), memory.offset) .value(); } CHECK(false) << "Unexpected shared memory type"; return sharedMemory; } void TestAshmem::initialize(uint32_t size, bool aidlReadonly) { mIsValid = false; ASSERT_GT(size, 0); const auto sharedMemory = nn::createSharedMemory(size).value(); mMappedMemory = nn::map(sharedMemory).value(); mPtr = static_cast<uint8_t*>(std::get<void*>(mMappedMemory.pointer)); CHECK_NE(mPtr, nullptr); if (aidlReadonly) { mAidlMemory = utils::convert(convertSharedMemoryToReadonly(sharedMemory)).value(); } else { mAidlMemory = utils::convert(sharedMemory).value(); } mIsValid = true; } Loading
neuralnetworks/aidl/vts/functional/Utils.h +6 −3 Original line number Diff line number Diff line Loading @@ -79,15 +79,18 @@ class TestMemoryBase { class TestAshmem : public TestMemoryBase { public: static std::unique_ptr<TestAshmem> create(uint32_t size); // If aidlReadonly is true, getAidlMemory will return a sAIDL memory with readonly access; // otherwise, the sAIDL memory has read-write access. This only affects the sAIDL memory. // getPointer will always return a valid address with read-write access. static std::unique_ptr<TestAshmem> create(uint32_t size, bool aidlReadonly = false); // Prefer TestAshmem::create. // The constructor calls initialize, which constructs the memory resources. This is a workaround // that gtest macros cannot be used directly in a constructor. TestAshmem(uint32_t size) { initialize(size); } TestAshmem(uint32_t size, bool aidlReadonly) { initialize(size, aidlReadonly); } private: void initialize(uint32_t size); void initialize(uint32_t size, bool aidlReadonly); nn::Mapping mMappedMemory; }; Loading