1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
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 CORE_PLUGIN_REGISTRY_H
17 #define CORE_PLUGIN_REGISTRY_H
18
19 #include <base/containers/array_view.h>
20 #include <base/containers/unordered_map.h>
21 #include <base/containers/vector.h>
22 #include <core/plugin/intf_class_factory.h>
23 #include <core/plugin/intf_plugin.h>
24 #include <core/plugin/intf_plugin_register.h>
25
26 #include "engine_factory.h"
27 #include "io/file_manager.h"
28 #include "loader/system_graph_loader.h"
29 #include "log/logger.h"
30 #include "os/intf_library.h"
31 #include "os/platform.h"
32 #if (CORE_PERF_ENABLED == 1)
33 #include "perf/performance_data_manager.h"
34 #endif
35 #include "threading/task_queue_factory.h"
36 #include "util/frustum_util.h"
37
CORE_BEGIN_NAMESPACE()38 CORE_BEGIN_NAMESPACE()
39 /**
40 Registry for interfaces that are engine independent.
41 */
42 class PluginRegistry final : public IPluginRegister, public IClassRegister, public IClassFactory {
43 public:
44 PluginRegistry();
45 ~PluginRegistry() override;
46
47 // IPluginRegister
48 BASE_NS::array_view<const IPlugin* const> GetPlugins() const override;
49 bool LoadPlugins(BASE_NS::array_view<const BASE_NS::Uid> pluginUids) override;
50 void UnloadPlugins(BASE_NS::array_view<const BASE_NS::Uid> pluginUids) override;
51 IClassRegister& GetClassRegister() const override;
52 void RegisterTypeInfo(const ITypeInfo& type) override;
53 void UnregisterTypeInfo(const ITypeInfo& type) override;
54 BASE_NS::array_view<const ITypeInfo* const> GetTypeInfos(const BASE_NS::Uid& typeUid) const override;
55 void AddListener(ITypeInfoListener& listener) override;
56 void RemoveListener(const ITypeInfoListener& listener) override;
57
58 // IClassRegister
59 void RegisterInterfaceType(const InterfaceTypeInfo& interfaceInfo) override;
60 void UnregisterInterfaceType(const InterfaceTypeInfo& interfaceInfo) override;
61 BASE_NS::array_view<const InterfaceTypeInfo* const> GetInterfaceMetadata() const override;
62 const InterfaceTypeInfo& GetInterfaceMetadata(const BASE_NS::Uid& uid) const override;
63 IInterface* GetInstance(const BASE_NS::Uid& uid) const override;
64
65 // IClassFactory
66 IInterface::Ptr CreateInstance(const BASE_NS::Uid& uid) override;
67
68 // IInterface
69 const IInterface* GetInterface(const BASE_NS::Uid& uid) const override;
70 IInterface* GetInterface(const BASE_NS::Uid& uid) override;
71 void Ref() override;
72 void Unref() override;
73
74 void RegisterPluginPath(BASE_NS::string_view path) override;
75 IFileManager& GetFileManager() override;
76
77 void HandlePerfTracePlugin(const PlatformCreateInfo& platformCreateInfo);
78
79 protected:
80 static BASE_NS::vector<InterfaceTypeInfo> RegisterGlobalInterfaces(PluginRegistry& registry);
81 void UnregisterGlobalInterfaces();
82
83 struct PluginData {
84 ILibrary::Ptr library;
85 PluginToken token;
86 int32_t refcnt;
87 };
88
89 void RegisterPlugin(ILibrary::Ptr lib, const IPlugin& plugin, bool asDependency);
90 static void UnregisterPlugin(const IPlugin& plugin, PluginToken token);
91
92 BASE_NS::vector<PluginData> pluginDatas_;
93 BASE_NS::vector<const IPlugin*> plugins_;
94
95 BASE_NS::unordered_map<BASE_NS::Uid, BASE_NS::vector<const ITypeInfo*>> typeInfos_;
96 BASE_NS::vector<const ITypeInfo*> newTypeInfos_;
97 BASE_NS::vector<const ITypeInfo*> oldTypeInfos_;
98
99 BASE_NS::vector<const InterfaceTypeInfo*> interfaceTypeInfos_;
100
101 BASE_NS::vector<ITypeInfoListener*> typeInfoListeners_;
102
103 Logger logger_ { true };
104 EngineFactory engineFactory_;
105 SystemGraphLoaderFactory systemGraphLoadeFactory;
106 FrustumUtil frustumUtil_;
107 TaskQueueFactory taskQueueFactory_;
108 #if (CORE_PERF_ENABLED == 1)
109 PerformanceDataManagerFactory perfManFactory_;
110 BASE_NS::Uid perfTracePlugin_;
111 uint64_t perfLoggerId_ {};
112 #endif
113 BASE_NS::vector<InterfaceTypeInfo> ownInterfaceInfos_;
114 FileManager fileManager_;
115 bool fileProtocolRegistered_ { false };
116 bool loading_ { false };
117 };
118 CORE_END_NAMESPACE()
119
120 #endif // CORE_PLUGIN_REGISTRY_H
121