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_FRAMEWORKS_ML_NN_RUNTIME_TEST_TEST_UTILS_H
18 #define ANDROID_FRAMEWORKS_ML_NN_RUNTIME_TEST_TEST_UTILS_H
19 
20 #include <android-base/mapped_file.h>
21 #include <android-base/unique_fd.h>
22 #include <android/sharedmem.h>
23 
24 #include <memory>
25 #include <utility>
26 
27 #include "TestHarness.h"
28 #include "TestNeuralNetworksWrapper.h"
29 
30 namespace android::nn {
31 
32 // Convenience class to manage the file, mapping, and memory object.
33 class TestAshmem {
34    public:
TestAshmem(::android::base::unique_fd fd,std::unique_ptr<::android::base::MappedFile> mapped,test_wrapper::Memory memory)35     TestAshmem(::android::base::unique_fd fd, std::unique_ptr<::android::base::MappedFile> mapped,
36                test_wrapper::Memory memory)
37         : mFd(std::move(fd)), mMapped(std::move(mapped)), mMemory(std::move(memory)) {}
38 
39     // Factory function for TestAshmem; prefer this over the raw constructor
createFrom(const test_helper::TestBuffer & buffer)40     static std::unique_ptr<TestAshmem> createFrom(const test_helper::TestBuffer& buffer) {
41         return createFrom(buffer.get<void>(), buffer.size());
42     }
43 
44     // Factory function for TestAshmem; prefer this over the raw constructor
createFrom(const void * data,uint32_t length)45     static std::unique_ptr<TestAshmem> createFrom(const void* data, uint32_t length) {
46         // Create ashmem-based fd.
47         int fd = ASharedMemory_create(nullptr, length);
48         if (fd <= 0) return nullptr;
49         ::android::base::unique_fd managedFd(fd);
50 
51         // Map and populate ashmem.
52         auto mappedFile =
53                 ::android::base::MappedFile::FromFd(fd, 0, length, PROT_READ | PROT_WRITE);
54         if (!mappedFile) return nullptr;
55         memcpy(mappedFile->data(), data, length);
56 
57         // Create NNAPI memory object.
58         test_wrapper::Memory memory(length, PROT_READ | PROT_WRITE, fd, 0);
59         if (!memory.isValid()) return nullptr;
60 
61         // Store everything in managing class.
62         return std::make_unique<TestAshmem>(std::move(managedFd), std::move(mappedFile),
63                                             std::move(memory));
64     }
65 
size()66     size_t size() { return mMapped->size(); }
get()67     test_wrapper::Memory* get() { return &mMemory; }
68 
69     template <typename Type>
dataAs()70     Type* dataAs() {
71         return static_cast<Type*>(static_cast<void*>(mMapped->data()));
72     }
73 
74    private:
75     ::android::base::unique_fd mFd;
76     std::unique_ptr<::android::base::MappedFile> mMapped;
77     test_wrapper::Memory mMemory;
78 };
79 
80 }  // namespace android::nn
81 
82 #endif  // ANDROID_FRAMEWORKS_ML_NN_RUNTIME_TEST_TEST_UTILS_H
83