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_0_UTILS_CALLBACKS_H 18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_CALLBACKS_H 19 20 #include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h> 21 #include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h> 22 #include <android/hardware/neuralnetworks/1.0/types.h> 23 #include <nnapi/IPreparedModel.h> 24 #include <nnapi/Result.h> 25 #include <nnapi/Types.h> 26 #include <nnapi/hal/CommonUtils.h> 27 #include <nnapi/hal/ProtectCallback.h> 28 #include <nnapi/hal/TransferValue.h> 29 30 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface 31 // lifetimes across processes and for protecting asynchronous calls across HIDL. 32 33 namespace android::hardware::neuralnetworks::V1_0::utils { 34 35 // Converts the results of IDevice::getSupportedOperations* to the NN canonical format. On success, 36 // this function returns with the supported operations as indicated by a driver. On failure, this 37 // function returns with the appropriate nn::GeneralError. 38 nn::GeneralResult<std::vector<bool>> supportedOperationsCallback( 39 ErrorStatus status, const hidl_vec<bool>& supportedOperations); 40 41 // Converts the results of IDevice::prepareModel* to the NN canonical format. On success, this 42 // function returns with a non-null nn::SharedPreparedModel with a feature level of 43 // nn::Version::ANDROID_OC_MR1. On failure, this function returns with the appropriate 44 // nn::GeneralError. 45 nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback( 46 ErrorStatus status, const sp<IPreparedModel>& preparedModel); 47 48 // Converts the results of IDevice::execute* to the NN canonical format. On success, this function 49 // returns with an empty output shape vector and no timing information. On failure, this function 50 // returns with the appropriate nn::ExecutionError. 51 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> executionCallback( 52 ErrorStatus status); 53 54 // A HIDL callback class to receive the results of IDevice::prepareModel asynchronously. 55 class PreparedModelCallback final : public IPreparedModelCallback, 56 public hal::utils::IProtectedCallback { 57 public: 58 using Data = nn::GeneralResult<nn::SharedPreparedModel>; 59 60 Return<void> notify(ErrorStatus status, const sp<IPreparedModel>& preparedModel) override; 61 62 void notifyAsDeadObject() override; 63 64 Data get(); 65 66 private: 67 hal::utils::TransferValue<Data> mData; 68 }; 69 70 // A HIDL callback class to receive the results of IDevice::execute asynchronously. 71 class ExecutionCallback final : public IExecutionCallback, public hal::utils::IProtectedCallback { 72 public: 73 using Data = nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>; 74 75 Return<void> notify(ErrorStatus status) override; 76 77 void notifyAsDeadObject() override; 78 79 Data get(); 80 81 private: 82 hal::utils::TransferValue<Data> mData; 83 }; 84 85 } // namespace android::hardware::neuralnetworks::V1_0::utils 86 87 #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_CALLBACKS_H 88