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 API_CORE_IENGINE_H
17 #define API_CORE_IENGINE_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/refcnt_ptr.h>
22 #include <base/containers/string_view.h>
23 #include <base/namespace.h>
24 #include <base/util/uid.h>
25 #include <core/ecs/intf_ecs.h>
26 #include <core/namespace.h>
27 #include <core/plugin/intf_class_factory.h>
28 #include <core/plugin/intf_interface.h>
29 
30 BASE_BEGIN_NAMESPACE()
31 template<class T>
32 class array_view;
33 BASE_END_NAMESPACE()
34 
35 CORE_BEGIN_NAMESPACE()
36 struct EngineCreateInfo;
37 class IFileManager;
38 class IImageLoaderManager;
39 class IPlatform;
40 class IThreadPool;
41 
42 /** \addtogroup group_iengine
43  *  @{
44  */
45 /** Engine time (totaltime and deltatime in microseconds) */
46 struct EngineTime {
47     /** Total time */
48     uint64_t totalTimeUs { 0 };
49     /** Delta time */
50     uint64_t deltaTimeUs { 0 };
51 };
52 
53 /** Engine interface.
54     Engine needs to be created with IEngineFactory::Create.
55 */
56 class IEngine : public IClassFactory {
57 public:
58     static constexpr BASE_NS::Uid UID { "760877f7-0baf-422b-a1a7-35834683ddd3" };
59 
60     using Ptr = BASE_NS::refcnt_ptr<IEngine>;
61 
62     /** Init engine. Create needed managers and data.
63      *  Needs to be called before accessing managers.
64      */
65     virtual void Init() = 0;
66 
67     /** Tick frame. Update ECS scene.
68      *  Needs to be called once per frame before RenderFrame().
69      *  @return True if ECS updated render data stores and rendering is required.
70      */
71     virtual bool TickFrame() = 0;
72 
73     /** Tick frame. Update internal and given ECS scenes.
74      *  Needs to be called once per frame before RenderFrame().
75      *  @param ecsInputs ECS instances that need to be updated.
76      *  @return True if rendering is required.
77      */
78     virtual bool TickFrame(const BASE_NS::array_view<IEcs*>& ecsInputs) = 0;
79 
80     /** Get file manager */
81     virtual IFileManager& GetFileManager() = 0;
82 
83     /** Return platform specific information */
84     virtual const IPlatform& GetPlatform() const = 0;
85 
86     /** Get engine time.
87      *  @return EngineTime struct.
88      */
89     virtual EngineTime GetEngineTime() const = 0;
90 
91     /** Creates a new ECS instance.
92      * @return ECS instance.
93      */
94     virtual IEcs::Ptr CreateEcs() = 0;
95 
96     /** Creates a new ECS instance with a shared thread pool.
97      * @param threadPool Thread pool which the ECS instance and it's managers and systems can use.
98      * @return ECS instance.
99      */
100     virtual IEcs::Ptr CreateEcs(IThreadPool& threadPool) = 0;
101 
102     virtual IImageLoaderManager& GetImageLoaderManager() = 0;
103 
104     /** Get version */
105     virtual BASE_NS::string_view GetVersion() = 0;
106 
107     /** Get whether engine is build in debug mode */
108     virtual bool IsDebugBuild() = 0;
109 
110 protected:
111     IEngine() = default;
112     virtual ~IEngine() = default;
113 };
114 
GetName(const IEngine *)115 inline constexpr BASE_NS::string_view GetName(const IEngine*)
116 {
117     return "IEngine";
118 }
119 
120 // factory inteface.
121 class IEngineFactory : public IInterface {
122 public:
123     static constexpr BASE_NS::Uid UID { "f2ce87a4-5a3d-4327-bc90-ff29efb398b0" };
124 
125     using Ptr = BASE_NS::refcnt_ptr<IEngineFactory>;
126 
127     virtual IEngine::Ptr Create(const EngineCreateInfo& engineCreateInfo) = 0;
128 
129 protected:
130     IEngineFactory() = default;
131     virtual ~IEngineFactory() = default;
132 };
133 
GetName(const IEngineFactory *)134 inline constexpr BASE_NS::string_view GetName(const IEngineFactory*)
135 {
136     return "IEngineFactory";
137 }
138 
139 #if defined(CORE_DYNAMIC) && (CORE_DYNAMIC == 1)
140 /** Get version */
141 extern BASE_NS::string_view (*GetVersion)();
142 /** Get whether engine is build in debug mode */
143 extern bool (*IsDebugBuild)();
144 #else
145 /** Get version */
146 CORE_PUBLIC BASE_NS::string_view GetVersion();
147 /** Get whether engine is build in debug mode */
148 CORE_PUBLIC bool IsDebugBuild();
149 #endif
150 /** @} */
151 CORE_END_NAMESPACE()
152 
153 #endif // API_CORE_IENGINE_H
154