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_IBUFFER_H 18 #define ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_IBUFFER_H 19 20 #include "nnapi/Result.h" 21 #include "nnapi/Types.h" 22 23 namespace android::nn { 24 25 /** 26 * This interface represents a device memory buffer. 27 * 28 * This interface is thread-safe, and any class that implements this interface must be thread-safe. 29 */ 30 class IBuffer { 31 public: 32 /** 33 * Retrieves the token corresponding to this buffer. 34 * 35 * @return MemoryDomainToken corresponding to this buffer. 36 */ 37 virtual Request::MemoryDomainToken getToken() const = 0; 38 39 /** 40 * Retrieves the content of this buffer to a shared memory region. 41 * 42 * The IBuffer object must have been initialized before the call to IBuffer::copyTo. 43 * For more information on the state of the IBuffer object, refer to IDevice::allocate. 44 * 45 * @param dst The destination shared memory region. 46 * @return Nothing on success, otherwise GeneralError. 47 */ 48 virtual GeneralResult<void> copyTo(const SharedMemory& dst) const = 0; 49 50 /** 51 * Sets the content of this buffer from a shared memory region. 52 * 53 * @param src The source shared memory region. 54 * @param dimensions Updated dimensional information. If the dimensions of the IBuffer object 55 * are not fully specified, then the dimensions must be fully specified here. If the 56 * dimensions of the IBuffer object are fully specified, then the dimensions may be empty 57 * here. If dimensions.size() > 0, then all dimensions must be specified here, and any 58 * dimension that was specified in the IBuffer object must have the same value here. 59 * @return Nothing on success, otherwise GeneralError. 60 */ 61 virtual GeneralResult<void> copyFrom(const SharedMemory& src, 62 const Dimensions& dimensions) const = 0; 63 64 // Public virtual destructor to allow objects to be stored (and destroyed) as smart pointers. 65 // E.g., std::unique_ptr<IBuffer>. 66 virtual ~IBuffer() = default; 67 68 protected: 69 // Protect the non-destructor special member functions to prevent object slicing. 70 IBuffer() = default; 71 IBuffer(const IBuffer&) = default; 72 IBuffer(IBuffer&&) noexcept = default; 73 IBuffer& operator=(const IBuffer&) = default; 74 IBuffer& operator=(IBuffer&&) noexcept = default; 75 }; 76 77 } // namespace android::nn 78 79 #endif // ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_IBUFFER_H 80