1 /*
2  * Copyright (C) 2021 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_AIDL_UTILS_BUFFER_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_BUFFER_H
19 
20 #include <aidl/android/hardware/neuralnetworks/IBuffer.h>
21 #include <nnapi/IBuffer.h>
22 #include <nnapi/Result.h>
23 #include <nnapi/Types.h>
24 #include <nnapi/hal/CommonUtils.h>
25 #include <memory>
26 
27 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
28 // lifetimes across processes.
29 
30 namespace aidl::android::hardware::neuralnetworks::utils {
31 
32 // Class that adapts aidl_hal::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             std::shared_ptr<aidl_hal::IBuffer> buffer, nn::Request::MemoryDomainToken token);
39 
40     Buffer(PrivateConstructorTag tag, std::shared_ptr<aidl_hal::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 std::shared_ptr<aidl_hal::IBuffer> kBuffer;
51     const nn::Request::MemoryDomainToken kToken;
52 };
53 
54 }  // namespace aidl::android::hardware::neuralnetworks::utils
55 
56 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_BUFFER_H
57