• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_helper.h>
34 #include <core/plugin/intf_plugin.h>
35 #include <core/plugin/intf_plugin_register.h>
36 
37 BASE_BEGIN_NAMESPACE()
38 template<class T1, class T2>
39 struct pair;
40 BASE_END_NAMESPACE()
41 
42 CORE_BEGIN_NAMESPACE()
43 class IImageLoaderManager;
44 class IPlatform;
45 class IThreadPool;
46 
47 class Engine final : public IInterfaceHelper<IEngine, IClassRegister, IClassFactory>,
48                      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     const IInterface* GetInterface(const BASE_NS::Uid& uid) const override;
71     IInterface* GetInterface(const BASE_NS::Uid& uid) override;
72 
73     // IClassFactory
74     IInterface::Ptr CreateInstance(const BASE_NS::Uid& uid) override;
75 
76     // IClassRegister
77     void RegisterInterfaceType(const InterfaceTypeInfo& interfaceInfo) override;
78     void UnregisterInterfaceType(const InterfaceTypeInfo& interfaceInfo) override;
79     BASE_NS::array_view<const InterfaceTypeInfo* const> GetInterfaceMetadata() const override;
80     const InterfaceTypeInfo& GetInterfaceMetadata(const BASE_NS::Uid& uid) const override;
81     IInterface* GetInstance(const BASE_NS::Uid& uid) const override;
82 
83     // IPluginRegister::ITypeInfoListener
84     void OnTypeInfoEvent(EventType type, BASE_NS::array_view<const ITypeInfo* const> typeInfos) override;
85 
86 private:
87     void RegisterDefaultPaths();
88     void LoadPlugins();
89     void UnloadPlugins();
90     static bool TickFrame(IEcs& ecs, uint64_t totalTime, uint64_t deltaTime);
91 
92     uint64_t firstTime_ { ~0u };
93     uint64_t previousFrameTime_ { ~0u };
94     uint64_t deltaTime_ { 1 };
95 
96     BASE_NS::unique_ptr<IPlatform> platform_;
97 
98     IFileManager::Ptr fileManager_;
99 
100     BASE_NS::unique_ptr<class ImageLoaderManager> imageManager_;
101 
102     BASE_NS::vector<BASE_NS::pair<PluginToken, const IEnginePlugin*>> plugins_;
103     BASE_NS::vector<const InterfaceTypeInfo*> interfaceTypeInfos_;
104 
105     // unique counter for ECS objects
106     int32_t ecsCounter_ { 0 };
107 };
108 CORE_END_NAMESPACE()
109 
110 #endif // CORE_ENGINE_H
111