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