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