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_IRENDER_NODE_GRAPH_SHARE_MANAGER_H
17 #define API_RENDER_IRENDER_NODE_GRAPH_SHARE_MANAGER_H
18 
19 #include <base/containers/string_view.h>
20 #include <base/util/uid.h>
21 #include <core/plugin/intf_interface.h>
22 #include <render/namespace.h>
23 #include <render/render_data_structures.h>
24 #include <render/resource_handle.h>
25 
RENDER_BEGIN_NAMESPACE()26 RENDER_BEGIN_NAMESPACE()
27 /** @ingroup group_render_IRenderNodeGraphShareManager */
28 /**
29  * Share input / output and other resources with current render node graph.
30  * Resources might change during the run and new RenderNodes might come in between.
31  * Render node graph can access the previous render node graph outputs.
32  *
33  * All the registering needs to be happen in RenderNode::PreExecuteFrame().
34  * Not internally synchronized for data modifications.
35  */
36 class IRenderNodeGraphShareManager : public CORE_NS::IInterface {
37 public:
38     static constexpr auto UID = BASE_NS::Uid("eef71d88-ddc0-41b8-b593-ac23c44ac086");
39 
40     /** Use simple resource names like: color, depth, velocity, alpha... */
41     static constexpr uint32_t MAX_NAME_LENGTH { 32u };
42     using RenderNodeGraphShareName = BASE_NS::fixed_string<MAX_NAME_LENGTH>;
43     /** Named resources struct */
44     struct NamedResource {
45         /** Name of the resource (simple unique within these few resources) */
46         RenderNodeGraphShareName name;
47         /** Resource handle */
48         RenderHandle handle;
49     };
50 
51     /** Get render node graph input resource handles.
52      * Set globally through render node graph manager or in render node graph description.
53      */
54     virtual BASE_NS::array_view<const RenderHandle> GetRenderNodeGraphInputs() const = 0;
55 
56     /** Get named render node graph input resource handles. Empty names if not set.
57      * Set globally through render node graph manager or in render node graph description.
58      */
59     virtual BASE_NS::array_view<const NamedResource> GetNamedRenderNodeGraphInputs() const = 0;
60 
61     /** Get render node graph output resource handles.
62      * Set globally through render node graph manager or in render node graph description.
63      */
64     virtual BASE_NS::array_view<const RenderHandle> GetRenderNodeGraphOutputs() const = 0;
65 
66     /** Get named render node graph output resource handles. Empty names if not set.
67      * Set globally through render node graph manager or in render node graph description.
68      */
69     virtual BASE_NS::array_view<const NamedResource> GetNamedRenderNodeGraphOutputs() const = 0;
70 
71     /** Get render node graph input resource handle with index.
72      * Set globally through render node graph manager or in render node graph description.
73      */
74     virtual RenderHandle GetRenderNodeGraphInput(const uint32_t index) const = 0;
75 
76     /** Get render node graph output resource handle with index.
77      * Set globally through render node graph manager or in render node graph description.
78      */
79     virtual RenderHandle GetRenderNodeGraphOutput(const uint32_t index) const = 0;
80 
81     /** Get outputs from previous render node graph.
82      * @return Array view of resource handles.
83      */
84     virtual BASE_NS::array_view<const RenderHandle> GetPrevRenderNodeGraphOutputs() const = 0;
85 
86     /** Get named outputs from previous render node graph.
87      * @return Array view of named resources.
88      */
89     virtual BASE_NS::array_view<const NamedResource> GetNamedPrevRenderNodeGraphOutputs() const = 0;
90 
91     /** Get resource render handle with index from previous render node graph.
92      * @param index Index of the registered resource.
93      * @return Resource handle, invalid if not found.
94      */
95     virtual RenderHandle GetPrevRenderNodeGraphOutput(const uint32_t index) const = 0;
96 
97     /** Get resource render handle with name from previous render node graph.
98      * @param resourceName Simple name of the registered resource.
99      * @return Resource handle, invalid if not found.
100      */
101     virtual RenderHandle GetNamedPrevRenderNodeGraphOutput(const BASE_NS::string_view resourceName) const = 0;
102 
103     /** Register render node output resource handles. (I.e. the output of this render node)
104      * Should be called every frame in PreExecuteFrame() (and initially in InitNode()).
105      * @param outputs Output handles to be registered.
106      */
107     virtual void RegisterRenderNodeOutputs(const BASE_NS::array_view<const RenderHandle> outputs) = 0;
108 
109     /** Register render node output one-by-one.
110      * Should be called every frame in PreExecuteFrame() (and initially in InitNode()).
111      * @param name Named output handle to be registered.
112      * @param handle Output handle to be registered.
113      */
114     virtual void RegisterRenderNodeOutput(const BASE_NS::string_view name, const RenderHandle& handle) = 0;
115 
116     /** Get registered output from a named render node with index.
117      */
118     virtual RenderHandle GetRegisteredRenderNodeOutput(
119         const BASE_NS::string_view renderNodeName, const uint32_t index) const = 0;
120 
121     /** Get registered output from a named render node with name.
122      * @param renderNodeName Render node name of the output.
123      * @param resourceName Name of the output resource.
124      */
125     virtual RenderHandle GetRegisteredRenderNodeOutput(
126         const BASE_NS::string_view renderNodeName, const BASE_NS::string_view resourceName) const = 0;
127 
128     /** Get registered output from previous render node with index.
129      * @param index Index of the output.
130      */
131     virtual RenderHandle GetRegisteredPrevRenderNodeOutput(const uint32_t index) const = 0;
132 
133     /** Register global render node output one-by-one. One should only register specific outputs globally.
134      * Should be called every frame in PreExecuteFrame() (and initially in InitNode()).
135      * @param name Named output handle to be registered.
136      * @param handle Output handle to be registered.
137      */
138     virtual void RegisterGlobalRenderNodeOutput(const BASE_NS::string_view name, const RenderHandle& handle) = 0;
139 
140     /** Get globally registered render node outputs. The render node name is the unique / full render node name.
141      * Should be called every frame in PreExecuteFrame() (and initially in InitNode()).
142      * @param nodeName Global (and unique) render node name.
143      * @return Globally registered render node outputs.
144      */
145     virtual BASE_NS::array_view<const NamedResource> GetRegisteredGlobalRenderNodeOutputs(
146         const BASE_NS::string_view nodeName) const = 0;
147 
148 protected:
149     IRenderNodeGraphShareManager() = default;
150     virtual ~IRenderNodeGraphShareManager() = default;
151 
152     IRenderNodeGraphShareManager(const IRenderNodeGraphShareManager&) = delete;
153     IRenderNodeGraphShareManager& operator=(const IRenderNodeGraphShareManager&) = delete;
154     IRenderNodeGraphShareManager(IRenderNodeGraphShareManager&&) = delete;
155     IRenderNodeGraphShareManager& operator=(IRenderNodeGraphShareManager&&) = delete;
156 };
157 RENDER_END_NAMESPACE()
158 
159 #endif // API_RENDER_IRENDER_NODE_GRAPH_SHARE_H
160