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 GRAPHICS_CONTEXT_H
17 #define GRAPHICS_CONTEXT_H
18 
19 #include <3d/intf_graphics_context.h>
20 #include <base/containers/unique_ptr.h>
21 #include <base/containers/unordered_map.h>
22 #include <base/containers/vector.h>
23 #include <core/namespace.h>
24 #include <core/plugin/intf_class_factory.h>
25 #include <core/plugin/intf_class_register.h>
26 #include <core/plugin/intf_plugin.h>
27 #include <render/resource_handle.h>
28 
29 CORE_BEGIN_NAMESPACE()
30 class IEcs;
31 CORE_END_NAMESPACE()
32 
33 RENDER_BEGIN_NAMESPACE()
34 class IRenderContext;
35 class IRenderDataStore;
36 RENDER_END_NAMESPACE()
37 
38 CORE3D_BEGIN_NAMESPACE()
39 class IMeshUtil;
40 class MeshUtil;
41 class IGltf2;
42 class Gltf2;
43 class ISceneUtil;
44 class SceneUtil;
45 class IRenderUtil;
46 class RenderUtil;
47 struct I3DPlugin;
48 
49 class GraphicsContext final : public IGraphicsContext,
50                               virtual CORE_NS::IClassRegister,
51                               virtual CORE_NS::IClassFactory,
52                               CORE_NS::IPluginRegister::ITypeInfoListener {
53 public:
54     explicit GraphicsContext(struct Agp3DPluginState&, RENDER_NS::IRenderContext& context);
55     ~GraphicsContext() override;
56 
57     GraphicsContext(const GraphicsContext&) = delete;
58     GraphicsContext& operator=(const GraphicsContext&) = delete;
59 
60     void Init() override;
61 
62     RENDER_NS::IRenderContext& GetRenderContext() const override;
63 
64     BASE_NS::array_view<const RENDER_NS::RenderHandleReference> GetRenderNodeGraphs(
65         const CORE_NS::IEcs& ecs) const override;
66 
67     ISceneUtil& GetSceneUtil() const override;
68 
69     IMeshUtil& GetMeshUtil() const override;
70 
71     IGltf2& GetGltf() const override;
72 
73     IRenderUtil& GetRenderUtil() const override;
74 
75     // IInterface
76     const CORE_NS::IInterface* GetInterface(const BASE_NS::Uid& uid) const override;
77     CORE_NS::IInterface* GetInterface(const BASE_NS::Uid& uid) override;
78     void Ref() override;
79     void Unref() override;
80 
81 private:
82     // IClassRegister
83     void RegisterInterfaceType(const CORE_NS::InterfaceTypeInfo& interfaceInfo) override;
84     void UnregisterInterfaceType(const CORE_NS::InterfaceTypeInfo& interfaceInfo) override;
85     BASE_NS::array_view<const CORE_NS::InterfaceTypeInfo* const> GetInterfaceMetadata() const override;
86     const CORE_NS::InterfaceTypeInfo& GetInterfaceMetadata(const BASE_NS::Uid& uid) const override;
87     IInterface* GetInstance(const BASE_NS::Uid& uid) const override;
88 
89     // IClassFactory
90     IInterface::Ptr CreateInstance(const BASE_NS::Uid& uid) override;
91 
92     // IPluginRegister::ITypeInfoListener
93     void OnTypeInfoEvent(EventType type, BASE_NS::array_view<const CORE_NS::ITypeInfo* const> typeInfos) override;
94 
95     struct Agp3DPluginState& factory_;
96     RENDER_NS::IRenderContext& context_;
97 
98     BASE_NS::vector<BASE_NS::pair<CORE_NS::PluginToken, const I3DPlugin*>> plugins_;
99     BASE_NS::vector<const CORE_NS::InterfaceTypeInfo*> interfaceTypeInfos_;
100 
101     BASE_NS::unique_ptr<SceneUtil> sceneUtil_;
102     BASE_NS::unique_ptr<MeshUtil> meshUtil_;
103     BASE_NS::unique_ptr<Gltf2> gltf2_;
104     BASE_NS::unique_ptr<RenderUtil> renderUtil_;
105     bool initialized_ { false };
106     uint32_t refcnt_ { 0 };
107 };
108 
GetName(const IGraphicsContext *)109 inline constexpr BASE_NS::string_view GetName(const IGraphicsContext*)
110 {
111     return "IGraphicsContext3D";
112 }
113 CORE3D_END_NAMESPACE()
114 
115 #endif // GRAPHICS_CONTEXT_H
116