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_COMMON_NNAPI_SHARED_MEMORY_H
18 #define ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_SHARED_MEMORY_H
19
20 #include <android-base/unique_fd.h>
21
22 #include <any>
23 #include <memory>
24 #include <optional>
25 #include <string>
26 #include <variant>
27 #include <vector>
28
29 #include "nnapi/Result.h"
30 #include "nnapi/Types.h"
31
32 namespace android::nn {
33
34 class MutableMemoryBuilder {
35 public:
36 explicit MutableMemoryBuilder(uint32_t poolIndex);
37
38 DataLocation append(size_t length, size_t alignment = kMinMemoryAlignment,
39 size_t padding = kMinMemoryPadding);
40 bool empty() const;
41
42 GeneralResult<SharedMemory> finish();
43
44 private:
45 uint32_t mPoolIndex;
46 size_t mSize = 0;
47 };
48
49 class ConstantMemoryBuilder {
50 public:
51 explicit ConstantMemoryBuilder(uint32_t poolIndex);
52
53 DataLocation append(const void* data, size_t length);
54 bool empty() const;
55
56 GeneralResult<SharedMemory> finish();
57
58 private:
59 struct LazyCopy {
60 const void* data;
61 size_t length;
62 size_t offset;
63 };
64
65 MutableMemoryBuilder mBuilder;
66 std::vector<LazyCopy> mSlices;
67 };
68
69 GeneralResult<base::unique_fd> dupFd(int fd);
70
71 // Precondition: `*ForwardFdIt` must be convertible to `int`
72 template <typename ForwardFdIt>
dupFds(ForwardFdIt first,ForwardFdIt last)73 GeneralResult<std::vector<base::unique_fd>> dupFds(ForwardFdIt first, ForwardFdIt last) {
74 std::vector<base::unique_fd> fds;
75 fds.reserve(std::distance(first, last));
76 for (; first != last; ++first) {
77 const int fd = *first;
78 fds.push_back(NN_TRY(dupFd(fd)));
79 }
80 return fds;
81 }
82
83 // Precondition: size > 0
84 GeneralResult<SharedMemory> createSharedMemory(size_t size);
85
86 // Duplicates `fd` and takes ownership of the duplicate.
87 // Precondition: size > 0
88 GeneralResult<SharedMemory> createSharedMemoryFromFd(size_t size, int prot, int fd, size_t offset);
89
90 // Precondition: ahwb != nullptr
91 GeneralResult<SharedMemory> createSharedMemoryFromAHWB(AHardwareBuffer* ahwb, bool takeOwnership);
92
93 // Precondition: memory != nullptr
94 size_t getSize(const SharedMemory& memory);
95
96 bool isAhwbBlob(const Memory::HardwareBuffer& memory);
97
98 // Precondition: memory != nullptr
99 bool isAhwbBlob(const SharedMemory& memory);
100
101 struct Mapping {
102 std::variant<const void*, void*> pointer;
103 size_t size;
104 std::any context;
105 };
106
107 GeneralResult<Mapping> map(const SharedMemory& memory);
108
109 bool flush(const Mapping& mapping);
110
111 } // namespace android::nn
112
113 #endif // ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_SHARED_MEMORY_H
114