xref: /aosp_15_r20/external/executorch/backends/qualcomm/runtime/backends/QnnImplementation.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Qualcomm Innovation Center, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 #pragma once
9 
10 #include <executorch/backends/qualcomm/runtime/Logging.h>
11 #include <executorch/backends/qualcomm/runtime/backends/QnnFunctionInterface.h>
12 
13 #include <mutex>
14 #include <string>
15 #include <unordered_map>
16 namespace executorch {
17 namespace backends {
18 namespace qnn {
19 class QnnImplementation {
20  public:
21   using BackendIdType = decltype(QnnInterface_t{}.backendId);
22 
QnnImplementation(std::string lib_path)23   explicit QnnImplementation(std::string lib_path)
24       : lib_path_(std::move(lib_path)){};
25 
26   executorch::runtime::Error Load(const QnnSaver_Config_t** saver_config);
27 
28   const QnnInterface& GetQnnInterface() const;
29 
30   executorch::runtime::Error TerminateAllBackends();
31 
32  private:
33   static constexpr const int required_num_providers_{1};
34 
35   static executorch::runtime::Error StartBackend(
36       const std::string& lib_path,
37       const QnnSaver_Config_t** saver_config);
38 
39   static executorch::runtime::Error InitBackend(
40       void* const lib_handle,
41       const QnnSaver_Config_t** saver_config);
42 
43   std::string lib_path_;
44   QnnInterface qnn_interface_;
45 
46   static std::unordered_map<std::string, BackendIdType> lib_path_to_backend_id_;
47   static std::unordered_map<BackendIdType, const QnnInterface_t*>
48       loaded_backend_;
49   static std::unordered_map<BackendIdType, void*> loaded_lib_handle_;
50   static std::mutex be_init_mutex_;
51 };
52 } // namespace qnn
53 } // namespace backends
54 } // namespace executorch
55