1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TEST_TEST_UTILS_H 18 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TEST_TEST_UTILS_H 19 20 #include <android-base/file.h> 21 #include <android-base/logging.h> 22 #include <android-base/mapped_file.h> 23 #include <android-base/unique_fd.h> 24 #include <android/sharedmem.h> 25 26 #include <memory> 27 #include <utility> 28 29 #include "TestHarness.h" 30 #include "TestNeuralNetworksWrapper.h" 31 32 namespace android::nn { 33 34 // Convenience class to manage the file, mapping, and memory object. 35 class TestAshmem { 36 public: TestAshmem(::android::base::unique_fd fd,std::unique_ptr<::android::base::MappedFile> mapped,test_wrapper::Memory memory)37 TestAshmem(::android::base::unique_fd fd, std::unique_ptr<::android::base::MappedFile> mapped, 38 test_wrapper::Memory memory) 39 : mFd(std::move(fd)), mMapped(std::move(mapped)), mMemory(std::move(memory)) {} 40 41 // Factory function for TestAshmem; prefer this over the raw constructor createFrom(const test_helper::TestBuffer & buffer)42 static std::unique_ptr<TestAshmem> createFrom(const test_helper::TestBuffer& buffer) { 43 return createFrom(buffer.get<void>(), buffer.size()); 44 } 45 46 // Factory function for TestAshmem; prefer this over the raw constructor createFrom(const void * data,uint32_t length)47 static std::unique_ptr<TestAshmem> createFrom(const void* data, uint32_t length) { 48 // Create ashmem-based fd. 49 #ifdef __ANDROID__ 50 int fd = ASharedMemory_create(nullptr, length); 51 #else // __ANDROID__ 52 TemporaryFile tmpFile; 53 int fd = tmpFile.release(); 54 CHECK_EQ(ftruncate(fd, length), 0); 55 #endif // __ANDROID__ 56 if (fd <= 0) return nullptr; 57 ::android::base::unique_fd managedFd(fd); 58 59 // Map and populate ashmem. 60 auto mappedFile = 61 ::android::base::MappedFile::FromFd(fd, 0, length, PROT_READ | PROT_WRITE); 62 if (!mappedFile) return nullptr; 63 memcpy(mappedFile->data(), data, length); 64 65 // Create NNAPI memory object. 66 test_wrapper::Memory memory(length, PROT_READ | PROT_WRITE, fd, 0); 67 if (!memory.isValid()) return nullptr; 68 69 // Store everything in managing class. 70 return std::make_unique<TestAshmem>(std::move(managedFd), std::move(mappedFile), 71 std::move(memory)); 72 } 73 size()74 size_t size() { return mMapped->size(); } get()75 test_wrapper::Memory* get() { return &mMemory; } 76 77 template <typename Type> dataAs()78 Type* dataAs() { 79 return static_cast<Type*>(static_cast<void*>(mMapped->data())); 80 } 81 82 private: 83 ::android::base::unique_fd mFd; 84 std::unique_ptr<::android::base::MappedFile> mMapped; 85 test_wrapper::Memory mMemory; 86 }; 87 88 } // namespace android::nn 89 90 #endif // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TEST_TEST_UTILS_H 91