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_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_BUFFER_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_BUFFER_H
19 
20 #include <android/hardware/neuralnetworks/1.3/IBuffer.h>
21 #include <android/hardware/neuralnetworks/1.3/types.h>
22 #include <nnapi/IBuffer.h>
23 #include <nnapi/Result.h>
24 #include <nnapi/Types.h>
25 #include <memory>
26 
27 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
28 // lifetimes across processes.
29 
30 namespace android::hardware::neuralnetworks::V1_3::utils {
31 
32 // Class that adapts V1_3::IBuffer to nn::IBuffer.
33 class Buffer final : public nn::IBuffer {
34     struct PrivateConstructorTag {};
35 
36   public:
37     static nn::GeneralResult<std::shared_ptr<const Buffer>> create(
38             sp<V1_3::IBuffer> buffer, nn::Request::MemoryDomainToken token);
39 
40     Buffer(PrivateConstructorTag tag, sp<V1_3::IBuffer> buffer,
41            nn::Request::MemoryDomainToken token);
42 
43     nn::Request::MemoryDomainToken getToken() const override;
44 
45     nn::GeneralResult<void> copyTo(const nn::SharedMemory& dst) const override;
46     nn::GeneralResult<void> copyFrom(const nn::SharedMemory& src,
47                                      const nn::Dimensions& dimensions) const override;
48 
49   private:
50     const sp<V1_3::IBuffer> kBuffer;
51     const nn::Request::MemoryDomainToken kToken;
52 };
53 
54 }  // namespace android::hardware::neuralnetworks::V1_3::utils
55 
56 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_BUFFER_H
57