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_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TEST_HAL_UTILS_H
18 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TEST_HAL_UTILS_H
19
20 #include <HalInterfaces.h>
21 #include <Utils.h>
22 #include <nnapi/hal/1.0/Device.h>
23 #include <nnapi/hal/1.1/Device.h>
24 #include <nnapi/hal/1.2/Device.h>
25 #include <nnapi/hal/1.3/Device.h>
26 #include <utils/StrongPointer.h>
27
28 #include <string>
29 #include <utility>
30
31 namespace android::nn {
32
33 // Creates valid V1_3::Capabilities.
makeCapabilities(float perf)34 inline V1_3::Capabilities makeCapabilities(float perf) {
35 const V1_0::PerformanceInfo perfInfo = {.execTime = perf, .powerUsage = perf};
36 return {.relaxedFloat32toFloat16PerformanceScalar = perfInfo,
37 .relaxedFloat32toFloat16PerformanceTensor = perfInfo,
38 .operandPerformance = nonExtensionOperandPerformance<HalVersion::V1_3>(perfInfo),
39 .ifPerformance = perfInfo,
40 .whilePerformance = perfInfo};
41 }
42
makeSharedDevice(std::string name,sp<V1_0::IDevice> driver)43 inline SharedDevice makeSharedDevice(std::string name, sp<V1_0::IDevice> driver) {
44 auto handleError = [](GeneralResult<SharedDevice> result) -> SharedDevice {
45 if (!result.has_value()) {
46 LOG(ERROR) << "Failed to create Device (" << result.error().code
47 << "): " << result.error().message;
48 return nullptr;
49 }
50 return std::move(result).value();
51 };
52 if (auto driver13 = V1_3::IDevice::castFrom(driver).withDefault(nullptr); driver13 != nullptr) {
53 return handleError(V1_3::utils::Device::create(std::move(name), std::move(driver13)));
54 }
55 if (auto driver12 = V1_2::IDevice::castFrom(driver).withDefault(nullptr); driver12 != nullptr) {
56 return handleError(V1_2::utils::Device::create(std::move(name), std::move(driver12)));
57 }
58 if (auto driver11 = V1_1::IDevice::castFrom(driver).withDefault(nullptr); driver11 != nullptr) {
59 return handleError(V1_1::utils::Device::create(std::move(name), std::move(driver11)));
60 }
61 return handleError(V1_0::utils::Device::create(std::move(name), std::move(driver)));
62 }
63
64 } // namespace android::nn
65
66 #endif // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TEST_HAL_UTILS_H
67