• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <3d/ecs/components/material_component.h>
17 #include <3d/ecs/components/mesh_component.h>
18 #include <3d/ecs/components/render_handle_component.h>
19 #include <3d/ecs/components/render_mesh_component.h>
20 #include <3d/implementation_uids.h>
21 #include <3d/intf_graphics_context.h>
22 #include <3d/intf_plugin.h>
23 #include <base/util/uid.h>
24 #include <core/intf_engine.h>
25 #include <core/io/intf_file_manager.h>
26 #include <core/plugin/intf_plugin.h>
27 #include <core/plugin/intf_plugin_decl.h>
28 #include <font/implementation_uids.h>
29 #include <render/device/intf_shader_manager.h>
30 #include <render/intf_render_context.h>
31 #include <render/namespace.h>
32 #include <text_3d/ecs/components/text_component.h>
33 #include <text_3d/implementation_uids.h>
34 
35 #include "ecs/systems/text_system.h"
36 
37 // Rofs Data.
38 extern "C" const void* const BINARY_DATA_FOR_TEXT3D[];
39 extern "C" const uint64_t SIZE_OF_DATA_FOR_TEXT3D;
40 
41 TEXT3D_BEGIN_NAMESPACE()
42 CORE_NS::IComponentManager* ITextComponentManagerInstance(CORE_NS::IEcs&);
43 CORE_NS::ISystem* TextSystemInstance(CORE_NS::IEcs&);
44 
45 void ITextComponentManagerDestroy(CORE_NS::IComponentManager*);
46 void TextSystemDestroy(CORE_NS::ISystem*);
47 
48 namespace {
49 static CORE_NS::IPluginRegister* gPluginRegistry { nullptr };
50 
51 static constexpr RENDER_NS::IShaderManager::ShaderFilePathDesc SHADER_FILE_PATHS {
52     "text3dshaders://",
53     "",
54     "",
55     "",
56 };
57 constexpr BASE_NS::string_view ROFS = "text3drofs";
58 constexpr BASE_NS::string_view SHADERS = "text3dshaders";
59 constexpr BASE_NS::string_view SHADER_PATH = "text3drofs://shaders/";
60 
61 static constexpr BASE_NS::Uid PLUGIN_DEPENDENCIES[] = { CORE3D_NS::UID_3D_PLUGIN, FONT_NS::UID_FONT_PLUGIN };
62 
63 struct PluginState final {
64     CORE3D_NS::IGraphicsContext& context_;
65     /*
66     CORE_NS::InterfaceTypeInfo interfaceInfo_ = CORE_NS::InterfaceTypeInfo {
67         this,
68         UID_FONT_MANAGER,
69         "IFontManager",
70         nullptr,
71         [](CORE_NS::IClassRegister&, CORE_NS::PluginToken token) -> CORE_NS::IInterface* {
72             if (token) {
73                 RenderPluginState* state = static_cast<RenderPluginState*>(token);
74                 if (!state->fontManager_) {
75                     state->fontManager_.reset(new FontManager(state->context_));
76                 }
77                 return state->fontManager_.get();
78             }
79             return nullptr;
80         },
81     };
82     */
PluginState__anon4da04b350111::PluginState83     PluginState(CORE3D_NS::IGraphicsContext& context) : context_(context) {}
84 };
85 
86 constexpr CORE_NS::ComponentManagerTypeInfo TEXT_COMPONENT_TYPE_INFO = {
87     { CORE_NS::ComponentManagerTypeInfo::UID },
88     ITextComponentManager::UID,
89     CORE_NS::GetName<ITextComponentManager>().data(),
90     ITextComponentManagerInstance,
91     ITextComponentManagerDestroy,
92 };
93 
94 constexpr CORE_NS::ComponentManagerTypeInfo COMPONENT_TYPE_INFOS[] = {
95     TEXT_COMPONENT_TYPE_INFO,
96 };
97 
98 // Text system dependencies.
99 static constexpr BASE_NS::Uid TEXT_SYSTEM_RW_DEPS[] = {
100     CORE3D_NS::IRenderMeshComponentManager::UID,
101     CORE3D_NS::IMaterialComponentManager::UID,
102     CORE3D_NS::IMeshComponentManager::UID,
103     CORE3D_NS::IRenderHandleComponentManager::UID,
104 };
105 static constexpr BASE_NS::Uid TEXT_SYSTEM_R_DEPS[] = {
106     TEXT_COMPONENT_TYPE_INFO.uid,
107 };
108 
109 constexpr CORE_NS::SystemTypeInfo SYSTEM_TYPE_INFOS[] = {
110     {
111         { CORE_NS::SystemTypeInfo::UID },
112         TextSystem::UID,
113         CORE_NS::GetName<TextSystem>().data(),
114         TextSystemInstance,
115         TextSystemDestroy,
116         TEXT_SYSTEM_RW_DEPS,
117         TEXT_SYSTEM_R_DEPS,
118     },
119 };
120 
CreatePlugin(CORE3D_NS::IGraphicsContext & context)121 CORE_NS::PluginToken CreatePlugin(CORE3D_NS::IGraphicsContext& context)
122 {
123     PluginState* state = new PluginState { context };
124     auto& renderContext = context.GetRenderContext();
125 
126     auto& fm = renderContext.GetEngine().GetFileManager();
127     auto rofs = fm.CreateROFilesystem(BINARY_DATA_FOR_TEXT3D, SIZE_OF_DATA_FOR_TEXT3D);
128     fm.RegisterFilesystem(ROFS, BASE_NS::move(rofs));
129     fm.RegisterPath(SHADERS, SHADER_PATH, false);
130 
131     renderContext.GetDevice().GetShaderManager().LoadShaderFiles(SHADER_FILE_PATHS);
132 
133     return state;
134 }
135 
DestroyPlugin(CORE_NS::PluginToken token)136 void DestroyPlugin(CORE_NS::PluginToken token)
137 {
138     PluginState* state = static_cast<PluginState*>(token);
139     auto& renderContext = state->context_.GetRenderContext();
140 
141     renderContext.GetDevice().GetShaderManager().UnloadShaderFiles(SHADER_FILE_PATHS);
142 
143     auto& fm = renderContext.GetEngine().GetFileManager();
144     fm.UnregisterPath(SHADERS, SHADER_PATH);
145     fm.UnregisterFilesystem(ROFS);
146 
147     delete state;
148 }
149 
150 static constexpr CORE3D_NS::I3DPlugin PLUGIN(CreatePlugin, DestroyPlugin);
151 } // namespace
152 
153 // implementation in the generated version.cpp
154 const char* GetVersionInfo();
155 
RegisterInterfaces(CORE_NS::IPluginRegister & pluginRegistry)156 CORE_NS::PluginToken RegisterInterfaces(CORE_NS::IPluginRegister& pluginRegistry)
157 {
158     gPluginRegistry = &pluginRegistry;
159     for (const auto& info : COMPONENT_TYPE_INFOS) {
160         pluginRegistry.RegisterTypeInfo(info);
161     }
162     for (const auto& info : SYSTEM_TYPE_INFOS) {
163         pluginRegistry.RegisterTypeInfo(info);
164     }
165     pluginRegistry.RegisterTypeInfo(PLUGIN);
166     return &pluginRegistry;
167 }
168 
UnregisterInterfaces(CORE_NS::PluginToken token)169 void UnregisterInterfaces(CORE_NS::PluginToken token)
170 {
171     auto* pluginRegistry = static_cast<CORE_NS::IPluginRegister*>(token);
172     pluginRegistry->UnregisterTypeInfo(PLUGIN);
173     for (const auto& info : COMPONENT_TYPE_INFOS) {
174         pluginRegistry->UnregisterTypeInfo(info);
175     }
176     for (const auto& info : SYSTEM_TYPE_INFOS) {
177         pluginRegistry->UnregisterTypeInfo(info);
178     }
179 }
180 TEXT3D_END_NAMESPACE()
181 
CORE_BEGIN_NAMESPACE()182 CORE_BEGIN_NAMESPACE()
183 IPluginRegister& GetPluginRegister()
184 {
185     return *TEXT3D_NS::gPluginRegistry;
186 }
CORE_END_NAMESPACE()187 CORE_END_NAMESPACE()
188 
189 extern "C" {
190 PLUGIN_DATA(Text3DPlugin) {
191     { CORE_NS::IPlugin::UID },
192     // name of plugin.
193     "3D Text Plugin",
194     // Version information of the plugin.
195     { TEXT3D_NS::UID_TEXT3D_PLUGIN, TEXT3D_NS::GetVersionInfo },
196     TEXT3D_NS::RegisterInterfaces,
197     TEXT3D_NS::UnregisterInterfaces,
198     { TEXT3D_NS::PLUGIN_DEPENDENCIES },
199 };
200 }
201