Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame^] | 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 | #include "Service.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android/hardware/neuralnetworks/1.0/IDevice.h> |
| 21 | #include <android/hardware/neuralnetworks/1.1/IDevice.h> |
| 22 | #include <android/hardware/neuralnetworks/1.2/IDevice.h> |
| 23 | #include <android/hardware/neuralnetworks/1.3/IDevice.h> |
| 24 | #include <android/hidl/manager/1.2/IServiceManager.h> |
| 25 | #include <hidl/ServiceManagement.h> |
| 26 | #include <nnapi/IDevice.h> |
| 27 | #include <nnapi/Result.h> |
| 28 | #include <nnapi/TypeUtils.h> |
| 29 | #include <nnapi/Types.h> |
| 30 | #include <nnapi/hal/1.0/Service.h> |
| 31 | #include <nnapi/hal/1.1/Service.h> |
| 32 | #include <nnapi/hal/1.2/Service.h> |
| 33 | #include <nnapi/hal/1.3/Service.h> |
| 34 | |
| 35 | #include <functional> |
| 36 | #include <memory> |
| 37 | #include <string> |
| 38 | #include <type_traits> |
| 39 | #include <unordered_set> |
| 40 | #include <vector> |
| 41 | |
| 42 | namespace android::hardware::neuralnetworks::service { |
| 43 | namespace { |
| 44 | |
| 45 | using getDeviceFn = std::add_pointer_t<nn::GeneralResult<nn::SharedDevice>(const std::string&)>; |
| 46 | |
| 47 | void getDevicesForVersion(const std::string& descriptor, getDeviceFn getDevice, |
| 48 | std::vector<nn::SharedDevice>* devices, |
| 49 | std::unordered_set<std::string>* registeredDevices) { |
| 50 | CHECK(devices != nullptr); |
| 51 | CHECK(registeredDevices != nullptr); |
| 52 | |
| 53 | const auto names = getAllHalInstanceNames(descriptor); |
| 54 | for (const auto& name : names) { |
| 55 | if (const auto [it, unregistered] = registeredDevices->insert(name); unregistered) { |
| 56 | auto maybeDevice = getDevice(name); |
| 57 | if (maybeDevice.has_value()) { |
| 58 | auto device = std::move(maybeDevice).value(); |
| 59 | CHECK(device != nullptr); |
| 60 | devices->push_back(std::move(device)); |
| 61 | } else { |
| 62 | LOG(ERROR) << "getDevice(" << name << ") failed with " << maybeDevice.error().code |
| 63 | << ": " << maybeDevice.error().message; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | std::vector<nn::SharedDevice> getDevices() { |
| 70 | std::vector<nn::SharedDevice> devices; |
| 71 | std::unordered_set<std::string> registeredDevices; |
| 72 | |
| 73 | getDevicesForVersion(V1_3::IDevice::descriptor, &V1_3::utils::getDevice, &devices, |
| 74 | ®isteredDevices); |
| 75 | getDevicesForVersion(V1_2::IDevice::descriptor, &V1_2::utils::getDevice, &devices, |
| 76 | ®isteredDevices); |
| 77 | getDevicesForVersion(V1_1::IDevice::descriptor, &V1_1::utils::getDevice, &devices, |
| 78 | ®isteredDevices); |
| 79 | getDevicesForVersion(V1_0::IDevice::descriptor, &V1_0::utils::getDevice, &devices, |
| 80 | ®isteredDevices); |
| 81 | |
| 82 | return devices; |
| 83 | } |
| 84 | |
| 85 | } // namespace |
| 86 | } // namespace android::hardware::neuralnetworks::service |
| 87 | |
| 88 | namespace android::nn::hal { |
| 89 | |
| 90 | std::vector<nn::SharedDevice> getDevices() { |
| 91 | return hardware::neuralnetworks::service::getDevices(); |
| 92 | } |
| 93 | |
| 94 | } // namespace android::nn::hal |