Compute Graph Framework SDK Reference  5.6
IChannelPacket.hpp
Go to the documentation of this file.
1
2//
3// Notice
4// ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES
5// NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
6// THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT,
7// MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8//
9// NVIDIA CORPORATION & AFFILIATES assumes no responsibility for the consequences of use of such
10// information or for any infringement of patents or other rights of third parties that may
11// result from its use. No license is granted by implication or otherwise under any patent
12// or patent rights of NVIDIA CORPORATION & AFFILIATES. No third party distribution is allowed unless
13// expressly authorized by NVIDIA. Details are subject to change without notice.
14// This code supersedes and replaces all information previously supplied.
15// NVIDIA CORPORATION & AFFILIATES products are not authorized for use as critical
16// components in life support devices or systems without express written approval of
17// NVIDIA CORPORATION & AFFILIATES.
18//
19// SPDX-FileCopyrightText: Copyright (c) 2020-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
20// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
21//
22// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
23// property and proprietary rights in and to this material, related
24// documentation and any modifications thereto. Any use, reproduction,
25// disclosure or distribution of this material and related documentation
26// without an express license agreement from NVIDIA CORPORATION or
27// its affiliates is strictly prohibited.
28//
30
31#ifndef DW_FRAMEWORK_ICHANNEL_PACKET_HPP_
32#define DW_FRAMEWORK_ICHANNEL_PACKET_HPP_
33
34#include <typeinfo>
35#include <cstddef>
36#include <dwcgf/Types.hpp>
39#include <nvscibuf.h>
40#include <nvscisync.h>
41#include <dw/core/language/Function.hpp>
42
43namespace dw
44{
45namespace framework
46{
47
48// Forward-declare
49class ChannelLocalShmemRegistry;
50
51// Virtual functions for the channel to interact with packets opaquely
53{
54public:
55 // Enable ownership via this interface
56 virtual ~IChannelPacket() = default;
57 // Deserialize metadata at provided pointer
59
60 // Callbacks needed for socket channel
62 {
63 public:
64 // Get size of serialized data buffer
65 virtual uint8_t* getBuffer() = 0;
66 // Get size of serialized data buffer
67 virtual size_t getBufferSize() = 0;
68 // Serialize the packet to internal buffer
69 virtual size_t serialize() = 0;
70 // Deserialize the packet from internal buffer
71 virtual void deserialize(size_t) = 0;
72 };
73
74 // Callbacks needed for nvsci channel
76 {
77 public:
78 // Init time callbacks
79
80 // Get size of the metadata
81 virtual uint32_t getNumBuffers() const = 0;
82 // Get size of serialized data buffer
83 virtual void fillNvSciBufAttributes(uint32_t bufferIndex, NvSciBufAttrList& attrList) const = 0;
84 // Initialize the packet from the given NvSciBufObjs
85 virtual void initializeFromNvSciBufObjs(dw::core::span<NvSciBufObj> bufs) = 0;
86
87 // Runtime callbacks
88
89 // Pack the API data type into the NvSciBufObjs
90 virtual void pack() = 0;
91 // Unpack the API data type from the NvSciBufObjs
92 virtual void unpack() = 0;
93 };
94
95 // Get interface for channel socket, throws if not enabled or supported
97 {
98 if (auto* ptr = dynamic_cast<SocketCallbacks*>(this))
99 {
100 return *ptr;
101 }
102 else
103 {
104 throw Exception(DW_NOT_SUPPORTED, "This packet interface does not implement socket callbacks");
105 }
106 }
107
109 {
110 if (auto* ptr = dynamic_cast<NvSciCallbacks*>(this))
111 {
112 return *ptr;
113 }
114 else
115 {
116 throw Exception(DW_NOT_SUPPORTED, "This packet interface does not implement nvsci callbacks");
117 }
118 }
119};
120
121// TODO(chale): this should be made private to the framework once external entities no longer create
122// Channel packets
124{
125public:
127 : m_typeSize(typeSize)
128 , m_buffer(new uint8_t[m_typeSize]())
129 , m_data{m_buffer.get(), m_typeSize}
130 {
131 }
132
134 {
135 return m_data;
136 }
137
138protected:
140 std::unique_ptr<uint8_t[]> m_buffer;
142};
143
145{
146public:
147 ChannelPacketDefault(size_t typeSize)
148 : ChannelPacketDefaultBase(typeSize)
149 {
150 }
151
152 uint8_t* getBuffer() final
153 {
154 return m_buffer.get();
155 }
156
157 size_t getBufferSize() final
158 {
159 return m_typeSize;
160 }
161
162 size_t serialize() final
163 {
164 return m_typeSize;
165 }
166
167 void deserialize(size_t) final
168 {
169 }
170};
171
173{
174 static constexpr char LOG_TAG[] = "ChannelNvSciPacketDefault";
175
176public:
178 : m_typeSize{typeSize}
179 {
180 }
181
182 uint32_t getNumBuffers() const final
183 {
184 return 1U;
185 }
186
187 void fillNvSciBufAttributes(uint32_t bufferIndex, NvSciBufAttrList& attrList) const final
188 {
189 if (bufferIndex != 0U)
190 {
191 throw Exception(DW_INVALID_ARGUMENT, "ChannelNvSciPacketDefault: invalid buffer index");
192 }
193
194 fillCpuPacketDataAttributes(attrList);
195 }
196
197 void initializeFromNvSciBufObjs(dw::core::span<NvSciBufObj> bufs)
198 {
199 if (bufs.size() != getNumBuffers())
200 {
201 throw Exception(DW_INVALID_ARGUMENT, "ChannelNvSciPacketDefault: wrong number of buffers passed");
202 }
203
204 FRWK_CHECK_NVSCI_ERROR(NvSciBufObjGetCpuPtr(bufs[0], &m_data));
205 }
206
207 void pack() final
208 {
209 // noop
210 }
211
212 void unpack() final
213 {
214 // noop
215 }
216
218 {
219 return GenericData(m_data, m_typeSize);
220 }
221
222private:
223 void fillCpuPacketDataAttributes(NvSciBufAttrList& output) const
224 {
225 const NvSciBufType bufferType = NvSciBufType_RawBuffer;
226 const bool cpuAccessFlag = true;
227 const uint64_t rawAlign = 4;
228 const NvSciBufAttrValAccessPerm permissions = NvSciBufAccessPerm_ReadWrite;
229
230 dw::core::Array<NvSciBufAttrKeyValuePair, 5> rawBufferAttributes =
231 {{{NvSciBufGeneralAttrKey_Types, &bufferType, sizeof(bufferType)},
232 {NvSciBufGeneralAttrKey_NeedCpuAccess, &cpuAccessFlag, sizeof(cpuAccessFlag)},
233 {NvSciBufGeneralAttrKey_RequiredPerm, &permissions, sizeof(permissions)},
234 {NvSciBufRawBufferAttrKey_Align, &rawAlign, sizeof(rawAlign)},
235 {NvSciBufRawBufferAttrKey_Size, &m_typeSize, sizeof(m_typeSize)}}};
236
237 FRWK_CHECK_NVSCI_ERROR(NvSciBufAttrListSetAttrs(output,
238 rawBufferAttributes.data(),
239 rawBufferAttributes.size()));
240 }
241
242 size_t m_typeSize{};
243 void* m_data{};
244};
245
247{
248public:
249 virtual std::unique_ptr<IChannelPacket> makePacket(ChannelPacketTypeID id, ChannelType channelType, GenericData ref) = 0;
250};
251using ChannelPacketFactoryPtr = std::shared_ptr<IChannelPacketFactory>;
252
253} // namespace framework
254} // namespace dw
255
256#endif // DW_FRAMEWORK_ICHANNEL_PACKET_HPP_
#define FRWK_CHECK_NVSCI_ERROR(e)
Definition: Exception.hpp:355
void initializeFromNvSciBufObjs(dw::core::span< NvSciBufObj > bufs)
void fillNvSciBufAttributes(uint32_t bufferIndex, NvSciBufAttrList &attrList) const final
std::unique_ptr< uint8_t[]> m_buffer
virtual std::unique_ptr< IChannelPacket > makePacket(ChannelPacketTypeID id, ChannelType channelType, GenericData ref)=0
virtual void initializeFromNvSciBufObjs(dw::core::span< NvSciBufObj > bufs)=0
virtual uint32_t getNumBuffers() const =0
virtual void fillNvSciBufAttributes(uint32_t bufferIndex, NvSciBufAttrList &attrList) const =0
SocketCallbacks & getSocketCallbacks()
virtual GenericData getGenericData()=0
virtual ~IChannelPacket()=default
NvSciCallbacks & getNvSciCallbacks()
std::shared_ptr< IChannelPacketFactory > ChannelPacketFactoryPtr
enum ChannelType :uint8_t { DW_CHANNEL_TYPE_SHMEM_LOCAL=0, DW_CHANNEL_TYPE_SHMEM_REMOTE=1, DW_CHANNEL_TYPE_EGLSTREAM=2, DW_CHANNEL_TYPE_SOCKET=3, DW_CHANNEL_TYPE_DDS=4, DW_CHANNEL_TYPE_NVSCI=5, } ChannelType
uint32_t ChannelPacketTypeID
Definition: Exception.hpp:47