The Lock And Extract Interop Handles sample demonstrates how to create a VPI-owned image and then extract exactly one interop handle from it.
The sample creates a default VPI context, which enables all backends supported by the running platform, reads those backend flags with vpiContextGetFlags, and passes the backend flags to vpiImageCreate instead of wrapping external memory first. This is the preferred direction when a pipeline wants VPI to own the image and then needs to hand the image to CUDA, multimedia, or NvSci code afterwards.
For convenience, here's the code that is also installed in the samples directory.
29 #include <nvbufsurface.h>
41 #define CHECK_STATUS(STMT) \
44 VPIStatus status = (STMT); \
45 if (status != VPI_SUCCESS) \
47 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
48 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
49 std::ostringstream ss; \
50 ss << "line " << __LINE__ << " " << vpiStatusGetName(status) << ": " << buffer; \
51 throw std::runtime_error(ss.str()); \
64 ExportMode ParseExportMode(
int argc,
char *argv[])
68 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" <cuda_ptr|nvbufsurface|nvscibuf>");
71 std::string mode = argv[1];
73 if (mode ==
"cuda_ptr")
75 return ExportMode::CUDA_PTR;
77 if (mode ==
"nvbufsurface")
79 return ExportMode::NVBUFSURFACE;
81 if (mode ==
"nvscibuf")
83 return ExportMode::NVSCIBUF;
86 throw std::runtime_error(
"Export mode must be one of cuda_ptr, nvbufsurface or nvscibuf");
89 void PrintCudaPointer(
VPIImage image)
96 std::cout <<
"CUDA pointer export\n";
97 std::cout <<
" pointer : " <<
static_cast<void *
>(plane.
pBase) <<
"\n";
98 std::cout <<
" offsetBytes : " << plane.
offsetBytes <<
"\n";
99 std::cout <<
" pitchBytes : " << plane.
pitchBytes <<
"\n";
100 std::cout <<
" note : This export is zero-copy.\n";
105 void PrintNvBufSurface(
VPIImage image)
110 NvBufSurface *surface =
nullptr;
111 if (NvBufSurfaceFromFd(nvBufferData.buffer.fd,
reinterpret_cast<void **
>(&surface)) != 0 || surface ==
nullptr)
114 throw std::runtime_error(
"NvBufSurfaceFromFd failed");
117 std::cout <<
"NvBufSurface export\n";
118 std::cout <<
" dmabuf fd : " << nvBufferData.buffer.fd <<
"\n";
119 std::cout <<
" surface pointer : " << surface <<
"\n";
120 std::cout <<
" note : This export is zero-copy.\n";
125 void PrintNvSciBufObj(
VPIImage image)
130 std::cout <<
"NvSciBufObj export\n";
131 std::cout <<
" object handle : " << nvSciData.buffer.nvscibuf.buffer <<
"\n";
132 std::cout <<
" note : This export is zero-copy.\n";
139 int main(
int argc,
char *argv[])
141 ExportMode exportMode = ParseExportMode(argc, argv);
144 <<
"This sample shows how to extract either a CUDA pointer, an NvBufSurface, or an NvSciBufObj\n"
145 "from a VPI-owned image. These extraction paths are zero-copy.\n"
146 "The image is created with all platform-supported backend flags so VPI can choose compatible memory.\n\n";
152 uint64_t imageBackends = 0;
162 if (exportMode == ExportMode::CUDA_PTR)
164 PrintCudaPointer(image);
166 else if (exportMode == ExportMode::NVBUFSURFACE)
168 PrintNvBufSurface(image);
172 PrintNvSciBufObj(image);
177 if (image !=
nullptr)
181 if (context !=
nullptr)
Functions and structures for dealing with VPI contexts.
Functions and structures for dealing with VPI images.
#define VPI_BACKEND_ALL
All backends.
VPIStatus vpiContextCreate(uint64_t flags, VPIContext *ctx)
Create a context instance.
VPIStatus vpiContextSetCurrent(VPIContext ctx)
Sets the context for the calling thread.
void vpiContextDestroy(VPIContext ctx)
Destroy a context instance as well as all resources it owns.
VPIStatus vpiContextGetFlags(VPIContext ctx, uint64_t *flags)
Get the current context flags.
struct VPIContextImpl * VPIContext
A handle to a context.
int64_t offsetBytes
Offset in bytes from pBase to the first column of the first plane row.
VPIByte * pBase
Pointer to the memory buffer which contains the plane data.
int32_t pitchBytes
Difference in bytes of beginning of one row and the beginning of the previous.
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
struct VPIImageImpl * VPIImage
A handle to an image.
VPIStatus vpiImageLockData(VPIImage img, VPILockMode mode, VPIImageBufferType bufType, VPIImageData *data)
Acquires the lock on an image object and returns the image contents.
VPIStatus vpiImageCreate(int32_t width, int32_t height, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
VPIStatus vpiImageUnlock(VPIImage img)
Releases the lock on an image object.
@ VPI_IMAGE_BUFFER_CUDA_PITCH_LINEAR
CUDA-accessible with planes in pitch-linear memory layout.
@ VPI_IMAGE_BUFFER_NVSCIBUF
NvSciBuf Please consult NvSciBufObj for more information.
@ VPI_IMAGE_BUFFER_NVBUFFER
NvBuffer.
Stores information about image characteristics and content.
Represents one image plane in pitch-linear layout.
@ VPI_LOCK_READ
Lock memory only for reading.