1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_H_ 17 #define TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_H_ 18 19 #include <dlfcn.h> 20 21 #include <cstdlib> 22 #include <memory> 23 #include <string> 24 #include <variant> 25 26 // Changed when importing from AOSP 27 #include "tensorflow/lite/kernels/internal/compatibility.h" 28 #include "tensorflow/lite/nnapi/NeuralNetworksTypes.h" 29 #include "tensorflow/lite/nnapi/sl/public/NeuralNetworksSupportLibraryImpl.h" 30 31 namespace tflite { 32 namespace nnapi { 33 34 #ifndef __NNAPI_FL5_MIN_ANDROID_API__ 35 #define __NNAPI_FL5_MIN_ANDROID_API__ __ANDROID_API_S__ 36 #endif 37 38 /** 39 * Helper struct, wraps different versions of NnApiSLDriverImpl. 40 * 41 * Owns the .so handle, and will close it in destructor. 42 * Sets proper implStructFeatureLevel in constructor. 43 * 44 * There's expectation that for M>N, NnApiSLDriverImplFL(M) is 45 * a strict superset of NnApiSLDriverImplFL(N), and *NnApiSLDriverImplFL(M) can 46 * be reinterpret_cast to *NnApiSLDriverImplFL(N) safely. 47 * 48 * The base->implFeatureLevel is set to the actual Feature Level 49 * implemented by the SLDriverImpl, 50 */ 51 struct NnApiSupportLibrary { NnApiSupportLibraryNnApiSupportLibrary52 NnApiSupportLibrary(const NnApiSLDriverImplFL5* impl, void* libHandle) 53 : libHandle(libHandle), fl5(impl) {} 54 // No need for ctor below since FL6&7 are typedefs of FL5 55 // NnApiSupportLibrary(const NnApiSLDriverImplFL6& impl, void* libHandle): 56 // impl(impl), NnApiSupportLibrary(const NnApiSLDriverImplFL7& impl, void* 57 // libHandle): impl(impl), libHandle(libHandle) {} ~NnApiSupportLibraryNnApiSupportLibrary58 ~NnApiSupportLibrary() { 59 if (libHandle != nullptr) { 60 dlclose(libHandle); 61 libHandle = nullptr; 62 } 63 } 64 NnApiSupportLibrary(const NnApiSupportLibrary&) = delete; 65 NnApiSupportLibrary& operator=(const NnApiSupportLibrary&) = delete; 66 getFeatureLevelNnApiSupportLibrary67 int64_t getFeatureLevel() const { return fl5->base.implFeatureLevel; } 68 getFL5NnApiSupportLibrary69 const NnApiSLDriverImplFL5* getFL5() const { return fl5; } getFL6NnApiSupportLibrary70 const NnApiSLDriverImplFL6* getFL6() const { 71 TFLITE_CHECK_GE(getFeatureLevel(), ANEURALNETWORKS_FEATURE_LEVEL_6); 72 return reinterpret_cast<const NnApiSLDriverImplFL6*>(&fl5); 73 } getFL7NnApiSupportLibrary74 const NnApiSLDriverImplFL7* getFL7() const { 75 TFLITE_CHECK_GE(getFeatureLevel(), ANEURALNETWORKS_FEATURE_LEVEL_7); 76 return reinterpret_cast<const NnApiSLDriverImplFL6*>(&fl5); 77 } 78 79 void* libHandle = nullptr; 80 const NnApiSLDriverImplFL5* fl5; 81 }; 82 83 /** 84 * Loads the NNAPI support library. 85 * The NnApiSupportLibrary structure is filled with all the pointers. If one 86 * function doesn't exist, a null pointer is stored. 87 */ 88 std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary( 89 const std::string& libName); 90 std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary( 91 void* libHandle); 92 93 } // namespace nnapi 94 } // namespace tflite 95 96 #endif // TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_H_ 97