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_ENGINE_H
17 #define CORE_ENGINE_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/array_view.h>
22 #include <base/containers/string.h>
23 #include <base/containers/string_view.h>
24 #include <base/containers/unique_ptr.h>
25 #include <base/containers/vector.h>
26 #include <base/namespace.h>
27 #include <core/ecs/intf_ecs.h>
28 #include <core/engine_info.h>
29 #include <core/intf_engine.h>
30 #include <core/io/intf_file_manager.h>
31 #include <core/namespace.h>
32 #include <core/plugin/intf_class_register.h>
33 #include <core/plugin/intf_interface.h>
34 #include <core/plugin/intf_plugin.h>
35 #include <core/plugin/intf_plugin_register.h>
36 
37 BASE_BEGIN_NAMESPACE()
38 struct Uid;
39 template<class T1, class T2>
40 struct pair;
41 BASE_END_NAMESPACE()
42 
43 CORE_BEGIN_NAMESPACE()
44 class IImageLoaderManager;
45 class IPlatform;
46 class IThreadPool;
47 
48 class Engine final : public IEngine, virtual public IClassRegister, IPluginRegister::ITypeInfoListener {
49 public:
50     explicit Engine(EngineCreateInfo const& createInfo);
51     ~Engine() override;
52 
53     void Init() override;
54     bool TickFrame() override;
55     bool TickFrame(const BASE_NS::array_view<IEcs*>& ecsInputs) override;
56     IFileManager& GetFileManager() override;
57 
58     IImageLoaderManager& GetImageLoaderManager() override;
59 
60     const IPlatform& GetPlatform() const override;
61 
62     EngineTime GetEngineTime() const override;
63 
64     IEcs::Ptr CreateEcs() override;
65     IEcs::Ptr CreateEcs(IThreadPool& threadPool) override;
66 
67     BASE_NS::string_view GetVersion() override;
68     bool IsDebugBuild() override;
69 
70     // IInterface
71     const IInterface* GetInterface(const BASE_NS::Uid& uid) const override;
72     IInterface* GetInterface(const BASE_NS::Uid& uid) override;
73     void Ref() override;
74     void Unref() override;
75 
76     // IClassFactory
77     IInterface::Ptr CreateInstance(const BASE_NS::Uid& uid) override;
78 
79     // IClassRegister
80     void RegisterInterfaceType(const InterfaceTypeInfo& interfaceInfo) override;
81     void UnregisterInterfaceType(const InterfaceTypeInfo& interfaceInfo) override;
82     BASE_NS::array_view<const InterfaceTypeInfo* const> GetInterfaceMetadata() const override;
83     const InterfaceTypeInfo& GetInterfaceMetadata(const BASE_NS::Uid& uid) const override;
84     IInterface* GetInstance(const BASE_NS::Uid& uid) const override;
85 
86     // IPluginRegister::ITypeInfoListener
87     void OnTypeInfoEvent(EventType type, BASE_NS::array_view<const ITypeInfo* const> typeInfos) override;
88 
89 private:
90     void RegisterDefaultPaths();
91     void LoadPlugins();
92     void UnloadPlugins();
93     static bool TickFrame(IEcs& ecs, uint64_t totalTime, uint64_t deltaTime);
94 
95     uint64_t firstTime_ { ~0u };
96     uint64_t previousFrameTime_ { ~0u };
97     uint64_t deltaTime_ { 1 };
98 
99     BASE_NS::unique_ptr<IPlatform> platform_;
100     ContextInfo applicationContext_;
101 
102     IFileManager::Ptr fileManager_;
103 
104     BASE_NS::unique_ptr<class ImageLoaderManager> imageManager_;
105     uint32_t refCount_ { 0 };
106 
107     BASE_NS::vector<BASE_NS::pair<PluginToken, const IEnginePlugin*>> plugins_;
108     BASE_NS::vector<const InterfaceTypeInfo*> interfaceTypeInfos_;
109 };
110 CORE_END_NAMESPACE()
111 
112 #endif // CORE_ENGINE_H
113