• 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 #include "SupportLibrary.h"
18 
19 #include <android-base/logging.h>
20 
21 #include <dlfcn.h>
22 
23 #include <cstring>
24 #include <memory>
25 #include <string>
26 
NnApiSupportLibrary(const NnApiSLDriverImplFL5 & impl,void * libHandle)27 NnApiSupportLibrary::NnApiSupportLibrary(const NnApiSLDriverImplFL5& impl, void* libHandle)
28     : NnApiSLDriverImplFL5(impl), libHandle(libHandle) {
29     base.implFeatureLevel = ANEURALNETWORKS_FEATURE_LEVEL_5;
30 }
31 
~NnApiSupportLibrary()32 NnApiSupportLibrary::~NnApiSupportLibrary() {
33     if (libHandle != nullptr) {
34         dlclose(libHandle);
35         libHandle = nullptr;
36     }
37 }
38 
loadNnApiSupportLibrary(const std::string & libName)39 std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(const std::string& libName) {
40     void* libHandle = dlopen(libName.c_str(), RTLD_LAZY | RTLD_LOCAL);
41     if (libHandle == nullptr) {
42         LOG(ERROR) << "nnapi error: unable to open library " << libName.c_str() << " " << dlerror();
43         return nullptr;
44     }
45 
46     auto result = loadNnApiSupportLibrary(libHandle);
47     if (!result) {
48         dlclose(libHandle);
49     }
50     return result;
51 }
52 
loadNnApiSupportLibrary(void * libHandle)53 std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(void* libHandle) {
54     NnApiSLDriverImpl* (*getSlDriverImpl)();
55     getSlDriverImpl = reinterpret_cast<decltype(getSlDriverImpl)>(
56             dlsym(libHandle, "ANeuralNetworks_getSLDriverImpl"));
57     if (getSlDriverImpl == nullptr) {
58         LOG(ERROR) << "Failed to find ANeuralNetworks_getSLDriverImpl symbol";
59         return nullptr;
60     }
61 
62     NnApiSLDriverImpl* impl = getSlDriverImpl();
63     if (impl == nullptr) {
64         LOG(ERROR) << "ANeuralNetworks_getSLDriverImpl returned nullptr";
65         return nullptr;
66     }
67 
68     if (impl->implFeatureLevel != ANEURALNETWORKS_FEATURE_LEVEL_5) {
69         LOG(ERROR) << "Unsupported NnApiSLDriverImpl->implFeatureLevel: " << impl->implFeatureLevel;
70         return nullptr;
71     }
72 
73     return std::make_unique<NnApiSupportLibrary>(*reinterpret_cast<NnApiSLDriverImplFL5*>(impl),
74                                                  libHandle);
75 }
76