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 OHOS_RENDER_3D_GRAPHICS_MANAGER_COMMON_H
17 #define OHOS_RENDER_3D_GRAPHICS_MANAGER_COMMON_H
18 
19 #include <unordered_set>
20 
21 #include <EGL/egl.h>
22 
23 #include "engine_factory.h"
24 #if defined(MULTI_ECS_UPDATE_AT_ONCE) && (MULTI_ECS_UPDATE_AT_ONCE == 1)
25 #include "frameworks/core/pipeline/pipeline_context.h"
26 #endif
27 #include "i_engine.h"
28 #include "offscreen_context_helper.h"
29 
30 namespace OHOS::Render3D {
31 struct TextureInfo;
32 struct PlatformData;
33 
34 enum class RenderBackend {
35     UNDEFINE,
36     GLES,
37     GL,
38     VULKAN,
39     METAL,
40 };
41 
42 class __attribute__((visibility("default"))) GraphicsManagerCommon {
43 public:
44     explicit GraphicsManagerCommon(const GraphicsManagerCommon&) = delete;
45     GraphicsManagerCommon& operator=(const GraphicsManagerCommon&) = delete;
46 
47     void Register(int32_t key, RenderBackend backend = RenderBackend::GLES);
48     void UnRegister(int32_t key);
49     std::unique_ptr<IEngine> GetEngine(EngineFactory::EngineType type, int32_t key);
50     std::unique_ptr<IEngine> GetEngine(EngineFactory::EngineType type, int32_t key, const HapInfo& hapInfo);
51     EGLContext GetOrCreateOffScreenContext(EGLContext eglContext);
52     void BindOffScreenContext();
53     virtual PlatformData GetPlatformData() const = 0;
54     virtual PlatformData GetPlatformData(const HapInfo& hapInfo) const = 0;
55     const HapInfo& GetHapInfo() const;
56     bool HasMultiEcs();
57     RenderBackend GetRenderBackendType(int32_t key);
58 #if defined(MULTI_ECS_UPDATE_AT_ONCE) && (MULTI_ECS_UPDATE_AT_ONCE == 1)
59     void AttachContext(const OHOS::Ace::WeakPtr<OHOS::Ace::PipelineContext> context);
60     void DrawFrame(void* ecs, void* handles);
61     void UnloadEcs(void* ecs);
62 #endif
63 
64 protected:
65     GraphicsManagerCommon() = default;
66     virtual ~GraphicsManagerCommon();
67     bool LoadEngineLib();
68     bool InitEngine(EGLContext eglContext, PlatformData data);
69     void DeInitEngine();
70     void UnloadEngineLib();
71 #if defined(MULTI_ECS_UPDATE_AT_ONCE) && (MULTI_ECS_UPDATE_AT_ONCE == 1)
72     void PerformDraw();
73 #endif
74 
75 private:
76     std::unordered_set<int32_t> viewTextures_;
77     OffScreenContextHelper offScreenContextHelper_;
78     std::unique_ptr<IEngine> engine_ = nullptr;
79     bool engineLoaded_ = false;
80     bool engineInited_ = false;
81     std::unordered_map<int32_t, RenderBackend> backends_;
82     HapInfo hapInfo_;
83 #if defined(MULTI_ECS_UPDATE_AT_ONCE) && (MULTI_ECS_UPDATE_AT_ONCE == 1)
84     std::unordered_map<void*, void*> ecss_;
85 #endif
86 };
87 } // namespace OHOS::Render3D
88 #endif // OHOS_RENDER_3D_GRAPHICS_MANAGER_COMMON_H
89