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