• 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 #ifndef ANDROID_PACKAGES_MODULES_NEURALNETWORKS_SL_SUPPORT_LIBRARY_H
18 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_SL_SUPPORT_LIBRARY_H
19 
20 #include <android-base/logging.h>
21 #include <dlfcn.h>
22 
23 #include <memory>
24 #include <string>
25 #include <variant>
26 
27 #include "NeuralNetworksSupportLibraryImpl.h"
28 #include "NeuralNetworksTypes.h"
29 
30 #ifndef __NNAPI_FL5_MIN_ANDROID_API__
31 #define __NNAPI_FL5_MIN_ANDROID_API__ __ANDROID_API_S__
32 #endif
33 
34 /**
35  * Helper struct, wraps different versions of NnApiSLDriverImpl.
36  *
37  * Owns the .so handle, and will close it in destructor.
38  * Sets proper implStructFeatureLevel in constructor.
39  *
40  * There's expectation that for M>N, NnApiSLDriverImplFL(M) is
41  * a strict superset of NnApiSLDriverImplFL(N), and *NnApiSLDriverImplFL(M) can
42  * be reinterpret_cast to *NnApiSLDriverImplFL(N) safely.
43  *
44  * The base->implFeatureLevel is set to the actual Feature Level
45  * implemented by the SLDriverImpl,
46  */
47 struct NnApiSupportLibrary {
NnApiSupportLibraryNnApiSupportLibrary48     NnApiSupportLibrary(const NnApiSLDriverImplFL5& impl, void* libHandle)
49         : libHandle(libHandle), impl(impl) {}
50     // No need for ctor below since FL6&7 are typedefs of FL5
51     // NnApiSupportLibrary(const NnApiSLDriverImplFL6& impl, void* libHandle): impl(impl),
52     // NnApiSupportLibrary(const NnApiSLDriverImplFL7& impl, void* libHandle): impl(impl),
53     // libHandle(libHandle) {}
NnApiSupportLibraryNnApiSupportLibrary54     NnApiSupportLibrary(const NnApiSLDriverImplFL8& impl, void* libHandle)
55         : libHandle(libHandle), impl(impl) {}
~NnApiSupportLibraryNnApiSupportLibrary56     ~NnApiSupportLibrary() {
57         if (libHandle != nullptr) {
58             dlclose(libHandle);
59             libHandle = nullptr;
60         }
61     }
62 
getFeatureLevelNnApiSupportLibrary63     int64_t getFeatureLevel() const { return getFL5()->base.implFeatureLevel; }
getFL5NnApiSupportLibrary64     const NnApiSLDriverImplFL5* getFL5() const {
65         return std::visit(
66                 [](auto&& impl) { return reinterpret_cast<const NnApiSLDriverImplFL5*>(&impl); },
67                 impl);
68     }
getFL6NnApiSupportLibrary69     const NnApiSLDriverImplFL6* getFL6() const {
70         CHECK_GE(getFeatureLevel(), ANEURALNETWORKS_FEATURE_LEVEL_6);
71         return std::visit(
72                 [](auto&& impl) { return reinterpret_cast<const NnApiSLDriverImplFL6*>(&impl); },
73                 impl);
74     }
getFL7NnApiSupportLibrary75     const NnApiSLDriverImplFL7* getFL7() const {
76         assert(getFeatureLevel() >= ANEURALNETWORKS_FEATURE_LEVEL_7);
77         return std::visit(
78                 [](auto&& impl) { return reinterpret_cast<const NnApiSLDriverImplFL7*>(&impl); },
79                 impl);
80     }
getFL8NnApiSupportLibrary81     const NnApiSLDriverImplFL8* getFL8() const {
82         assert(getFeatureLevel() >= ANEURALNETWORKS_FEATURE_LEVEL_8);
83         return std::visit(
84                 [](auto&& impl) { return reinterpret_cast<const NnApiSLDriverImplFL8*>(&impl); },
85                 impl);
86     }
87 
88     void* libHandle = nullptr;
89     // NnApiSLDriverImplFL[6-7] is a typedef of FL5, can't be explicitly specified.
90     std::variant<NnApiSLDriverImplFL5,
91                  /*NnApiSLDriverImplFL6, NnApiSLDriverImplFL7,*/ NnApiSLDriverImplFL8>
92             impl;
93 };
94 
95 /**
96  * Loads the NNAPI support library.
97  * The NnApiSupportLibrary structure is filled with all the pointers. If one
98  * function doesn't exist, a null pointer is stored.
99  */
100 std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(const std::string& libName);
101 std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(void* libHandle);
102 
103 #endif  // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_SL_SUPPORT_LIBRARY_H
104