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();
46 
47     // IPluginRegister
48     BASE_NS::array_view<const IPlugin* const> GetPlugins() const override;
49     bool LoadPlugins(const BASE_NS::array_view<const BASE_NS::Uid> pluginUids) override;
50     void UnloadPlugins(const 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(const BASE_NS::string_view path) override;
75     IFileManager& GetFileManager() override;
76 
77 protected:
78     static BASE_NS::vector<InterfaceTypeInfo> RegisterGlobalInterfaces(PluginRegistry& registry);
79     void UnregisterGlobalInterfaces();
80 
81     struct PluginData {
82         ILibrary::Ptr library;
83         PluginToken token;
84         int32_t refcnt;
85     };
86 
87     void RegisterPlugin(ILibrary::Ptr lib, const IPlugin& plugin, bool asDependency);
88     static void UnregisterPlugin(const IPlugin& plugin, PluginToken token);
89 
90     BASE_NS::vector<PluginData> pluginDatas_;
91     BASE_NS::vector<const IPlugin*> plugins_;
92 
93     BASE_NS::unordered_map<BASE_NS::Uid, BASE_NS::vector<const ITypeInfo*>> typeInfos_;
94     BASE_NS::vector<const ITypeInfo*> newTypeInfos_;
95     BASE_NS::vector<const ITypeInfo*> oldTypeInfos_;
96 
97     BASE_NS::vector<const InterfaceTypeInfo*> interfaceTypeInfos_;
98 
99     BASE_NS::vector<ITypeInfoListener*> typeInfoListeners_;
100 
101     Logger logger_ { true };
102     EngineFactory engineFactory_;
103     SystemGraphLoaderFactory systemGraphLoadeFactory;
104     FrustumUtil frustumUtil_;
105     TaskQueueFactory taskQueueFactory_;
106 #if (CORE_PERF_ENABLED == 1)
107     PerformanceDataManagerFactory perfManFactory_;
108 #endif
109     BASE_NS::vector<InterfaceTypeInfo> ownInterfaceInfos_;
110     FileManager fileManager_;
111     bool fileProtocolRegistered_ { false };
112     bool loading_ { false };
113 };
114 CORE_END_NAMESPACE()
115 
116 #endif // CORE_PLUGIN_REGISTRY_H
117