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_RENDER_IPLUGIN_H
17 #define API_RENDER_IPLUGIN_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/array_view.h>
22 #include <base/util/compile_time_hashes.h>
23 #include <base/util/uid.h>
24 #include <core/plugin/intf_plugin.h>
25 #include <render/namespace.h>
26 
27 RENDER_BEGIN_NAMESPACE()
28 class IRenderContext;
29 class IRenderDataStore;
30 class IRenderNode;
31 
32 /** \addtogroup group_plugin_iplugin
33  *  @{
34  */
35 /** Information needed from the plugin for managing RenderDataStores. */
36 struct RenderDataStoreTypeInfo : public CORE_NS::ITypeInfo {
37     static constexpr BASE_NS::Uid UID { "79dd23ac-db4f-476e-85cd-a285a3aa4fb0" };
38 
39     using CreateRenderDataStoreFn = IRenderDataStore* (*)(IRenderContext& renderContext, char const* instanceName);
40     using DestroyRenderDataStoreFn = void (*)(IRenderDataStore* instance);
41 
42     /** Unique ID of the rander data store. */
43     const BASE_NS::Uid uid;
44     /** Name used during data store creation to identify the type of the data store. */
45     char const* const typeName { "" };
46     /** Pointer to function which is used to create data store instances. */
47     const CreateRenderDataStoreFn createDataStore;
48     /** Pointer to function which is used to destory data store instances. */
49     const DestroyRenderDataStoreFn destroyDataStore;
50 };
51 
52 /** Information needed from the plugin for managing RenderNodes. */
53 struct RenderNodeTypeInfo : public CORE_NS::ITypeInfo {
54     /** TypeInfo UID for render node. */
55     static constexpr BASE_NS::Uid UID { "92085439-2cf7-4762-8769-28b552f4c5a4" };
56 
57     using CreateRenderNodeFn = IRenderNode* (*)();
58     using DestroyRenderNodeFn = void (*)(IRenderNode* instance);
59     using PluginRenderNodeClassType = uint32_t;
60     using PluginRenderNodeBackendFlags = uint32_t;
61 
62     /** Unique ID of the rander node. */
63     const BASE_NS::Uid uid;
64     /** Name used during node creation to identify the type of the node. */
65     char const* const typeName { "" };
66     /** Pointer to function which is used to create node instances. */
67     const CreateRenderNodeFn createNode;
68     /** Pointer to function which is used to destroy node instances. */
69     const DestroyRenderNodeFn destroyNode;
70 
71     /** Render node backend flags (see IRenderNode) */
72     PluginRenderNodeBackendFlags renderNodeBackendFlags { 0u };
73     /** Render node class type (see IRenderNode) */
74     PluginRenderNodeClassType renderNodeClassType { 0u };
75 };
76 
77 /** A plugin which adds new render data store and render node types. */
78 struct IRenderPlugin : public CORE_NS::ITypeInfo {
79     /** TypeInfo UID for render plugin. */
80     static constexpr BASE_NS::Uid UID { "303e3ffe-36fd-4e1b-82f3-349844fab2eb" };
81 
82     /*
83     Plugin lifecycle.
84     1. createPlugin  (*as many times as contexts instantiated)   (initialize IRenderContext specific state here.)
85     2. destroyPlugin  (*as many times as contexts instantiated)  (deinitialize IRenderContext specific state here.)
86     */
87 
88     using CreatePluginFn = CORE_NS::PluginToken (*)(IRenderContext&);
89     using DestroyPluginFn = void (*)(CORE_NS::PluginToken);
90 
IRenderPluginIRenderPlugin91     constexpr IRenderPlugin(CreatePluginFn create, DestroyPluginFn destroy)
92         : ITypeInfo { UID }, createPlugin { create }, destroyPlugin { destroy }
93     {}
94 
95     /** Initialize function for render plugin.
96      * Called when plugin is initially loaded by context. Used to register paths etc.
97      * Is expected to register its own named interfaces (IInterface) which are tied to the context instance.
98      * Called when attaching to engine.
99      */
100     const CreatePluginFn createPlugin { nullptr };
101 
102     /** Deinitialize function for render plugin.
103      * Called when plugin is about to be unloaded by context.
104      * Called when detaching from context.
105      */
106     const DestroyPluginFn destroyPlugin { nullptr };
107 };
108 /** @} */
109 RENDER_END_NAMESPACE()
110 
111 #endif // API_RENDER_IPLUGIN_H
112