• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "ShimServiceSample"
18 
19 #include <android-base/logging.h>
20 #include <android-base/scopeguard.h>
21 #include <dlfcn.h>
22 
23 #include "NeuralNetworksShim.h"
24 #include "SupportLibrarySymbols.h"
25 
main()26 int main() {
27     NnApiSLDriverImpl* impl = ANeuralNetworks_getSLDriverImpl();
28     if (impl == nullptr) {
29         LOG(ERROR) << "ANeuralNetworks_getSLDriverImpl returned nullptr";
30         return EXIT_FAILURE;
31     }
32 
33     ANeuralNetworksShimDeviceInfo* deviceInfo;
34     ANeuralNetworksShimDeviceInfo_create(&deviceInfo,
35                                          /*deviceName=*/"nnapi-sample_sl",
36                                          /*serviceName=*/"nnapi-sample_sl_shim");
37     const auto guardDeviceInfo = android::base::make_scope_guard(
38             [deviceInfo] { ANeuralNetworksShimDeviceInfo_free(deviceInfo); });
39 
40     ANeuralNetworksShimRegistrationParams* params;
41     ANeuralNetworksShimRegistrationParams_create(impl, &params);
42     const auto guardParams = android::base::make_scope_guard(
43             [params] { ANeuralNetworksShimRegistrationParams_free(params); });
44     ANeuralNetworksShimRegistrationParams_addDeviceInfo(params, deviceInfo);
45     // The default is 15, use more only if there's more devices exposed.
46     ANeuralNetworksShimRegistrationParams_setNumberOfListenerThreads(params, 15);
47     ANeuralNetworksShimRegistrationParams_registerAsLazyService(params, /*asLazy=*/false);
48     ANeuralNetworksShimRegistrationParams_fallbackToMinimumSupportDevice(params,
49                                                                          /*fallback=*/false);
50 
51     auto result = ANeuralNetworksShim_registerSupportLibraryService(params);
52 
53     LOG(ERROR) << "ANeuralNetworksShim_registerSupportLibraryService returned with error status: "
54                << result;
55 
56     return EXIT_FAILURE;
57 }
58