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 <base/containers/fixed_string.h>
17 #include <core/intf_engine.h>
18 #include <core/io/intf_file_manager.h>
19 #include <core/plugin/intf_plugin.h>
20 #include <render/namespace.h>
21 
22 #include "render_context.h"
23 
24 // Include the declarations directly from engine.
25 // NOTE: macro defined by cmake as CORE_STATIC_PLUGIN_HEADER="${CORE_ROOT_DIRECTORY}/src/static_plugin_decl.h"
26 // this is so that the core include directories are not leaked here, but we want this one header in this case.
27 #ifndef __APPLE__
28 #include CORE_STATIC_PLUGIN_HEADER
29 #include "registry_data.cpp"
30 #endif
31 
32 // Rofs Data.
33 extern "C" {
34 extern const void* const BINARYDATAFORRENDER[];
35 extern const uint64_t SIZEOFDATAFORRENDER;
36 }
37 
38 RENDER_BEGIN_NAMESPACE()
39 using namespace CORE_NS;
40 using BASE_NS::fixed_string;
41 using BASE_NS::string_view;
42 namespace {
43 constexpr string_view ROFS = "rofsRndr";
44 constexpr string_view SHADERS = "render_shaders";
45 constexpr string_view VIDS = "render_vertexinputdeclarations";
46 constexpr string_view PIDS = "render_pipelinelayouts";
47 constexpr string_view SSTATES = "render_shaderstates";
48 constexpr string_view RENDERDATA = "render_renderdataconfigurations";
49 
50 constexpr string_view SHADER_PATH = "rofsRndr://shaders/";
51 constexpr string_view VID_PATH = "rofsRndr://vertexinputdeclarations/";
52 constexpr string_view PID_PATH = "rofsRndr://pipelinelayouts/";
53 constexpr string_view SHADER_STATE_PATH = "rofsRndr://shaderstates/";
54 constexpr string_view RENDERDATA_PATH = "rofsRndr://renderdataconfigurations/";
55 
CreatePlugin(IEngine & engine)56 PluginToken CreatePlugin(IEngine& engine)
57 {
58     RenderPluginState* token = new RenderPluginState { engine, {} };
59     auto& registry = *engine.GetInterface<IClassRegister>();
60 
61     registry.RegisterInterfaceType(token->interfaceInfo_);
62 
63     IFileManager& fileManager = engine.GetFileManager();
64 #if (RENDER_EMBEDDED_ASSETS_ENABLED == 1)
65     // Create engine:// protocol that points to embedded asset files.
66     fileManager.RegisterFilesystem(ROFS, fileManager.CreateROFilesystem(BINARYDATAFORRENDER, SIZEOFDATAFORRENDER));
67     fileManager.RegisterPath(SHADERS, SHADER_PATH, false);
68     fileManager.RegisterPath(VIDS, VID_PATH, false);
69     fileManager.RegisterPath(PIDS, PID_PATH, false);
70     fileManager.RegisterPath(SSTATES, SHADER_STATE_PATH, false);
71     fileManager.RegisterPath(RENDERDATA, RENDERDATA_PATH, false);
72 #endif
73 
74     return token;
75 }
76 
DestroyPlugin(PluginToken token)77 void DestroyPlugin(PluginToken token)
78 {
79     RenderPluginState* state = static_cast<RenderPluginState*>(token);
80     IFileManager& fileManager = state->engine_.GetFileManager();
81 #if (RENDER_EMBEDDED_ASSETS_ENABLED == 1)
82     fileManager.UnregisterPath(SHADERS, SHADER_PATH);
83     fileManager.UnregisterPath(VIDS, VID_PATH);
84     fileManager.UnregisterPath(PIDS, PID_PATH);
85     fileManager.UnregisterPath(SSTATES, SHADER_STATE_PATH);
86     fileManager.UnregisterPath(RENDERDATA, RENDERDATA_PATH);
87     fileManager.UnregisterFilesystem(ROFS);
88 #endif
89 
90     auto& registry = *state->engine_.GetInterface<IClassRegister>();
91     registry.UnregisterInterfaceType(state->interfaceInfo_);
92 
93     delete state;
94 }
95 
96 constexpr IEnginePlugin ENGINE_PLUGIN(CreatePlugin, DestroyPlugin);
97 } // namespace
98 
99 extern "C" void InitRegistry(CORE_NS::IPluginRegister& pluginRegistry);
100 
RegisterInterfaces(IPluginRegister & pluginRegistry)101 PluginToken RegisterInterfaces(IPluginRegister& pluginRegistry)
102 {
103     InitRegistry(pluginRegistry);
104     pluginRegistry.RegisterTypeInfo(ENGINE_PLUGIN);
105 
106     return &pluginRegistry;
107 }
108 
UnregisterInterfaces(PluginToken token)109 void UnregisterInterfaces(PluginToken token)
110 {
111     IPluginRegister* pluginRegistry = static_cast<IPluginRegister*>(token);
112     pluginRegistry->UnregisterTypeInfo(ENGINE_PLUGIN);
113 }
114 RENDER_END_NAMESPACE()
115