Skip to content

Commit 8bce760

Browse files
committed
Merge pull request opencv#8424 from khnaba:expose-buffer-pool
2 parents b3d128b + cdcf44b commit 8bce760

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

modules/core/include/opencv2/core/cuda.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,34 @@ The function does not reallocate memory if the matrix has proper attributes alre
327327
*/
328328
CV_EXPORTS void ensureSizeIsEnough(int rows, int cols, int type, OutputArray arr);
329329

330+
/** @brief BufferPool for use with CUDA streams
331+
332+
* BufferPool utilizes cuda::Stream's allocator to create new buffers. It is
333+
* particularly useful when BufferPoolUsage is set to true, or a custom
334+
* allocator is specified for the cuda::Stream, and you want to implement your
335+
* own stream based functions utilizing the same underlying GPU memory
336+
* management.
337+
*/
338+
class CV_EXPORTS BufferPool
339+
{
340+
public:
341+
342+
//! Gets the BufferPool for the given stream.
343+
explicit BufferPool(Stream& stream);
344+
345+
//! Allocates a new GpuMat of given size and type.
346+
GpuMat getBuffer(int rows, int cols, int type);
347+
348+
//! Allocates a new GpuMat of given size and type.
349+
GpuMat getBuffer(Size size, int type) { return getBuffer(size.height, size.width, type); }
350+
351+
//! Returns the allocator associated with the stream.
352+
Ptr<GpuMat::Allocator> getAllocator() const { return allocator_; }
353+
354+
private:
355+
Ptr<GpuMat::Allocator> allocator_;
356+
};
357+
330358
//! BufferPool management (must be called before Stream creation)
331359
CV_EXPORTS void setBufferPoolUsage(bool on);
332360
CV_EXPORTS void setBufferPoolConfig(int deviceId, size_t stackSize, int stackCount);

modules/core/include/opencv2/core/private.cuda.hpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,6 @@ static inline void throw_no_cuda() { CV_Error(cv::Error::StsNotImplemented, "The
102102

103103
namespace cv { namespace cuda
104104
{
105-
class CV_EXPORTS BufferPool
106-
{
107-
public:
108-
explicit BufferPool(Stream& stream);
109-
110-
GpuMat getBuffer(int rows, int cols, int type);
111-
GpuMat getBuffer(Size size, int type) { return getBuffer(size.height, size.width, type); }
112-
113-
GpuMat::Allocator* getAllocator() const { return allocator_; }
114-
115-
private:
116-
GpuMat::Allocator* allocator_;
117-
};
118-
119105
static inline void checkNppError(int code, const char* file, const int line, const char* func)
120106
{
121107
if (code < 0)

modules/core/src/cuda_stream.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,20 +668,33 @@ void cv::cuda::setBufferPoolConfig(int deviceId, size_t stackSize, int stackCoun
668668
#endif
669669
}
670670

671-
#ifdef HAVE_CUDA
672-
673-
cv::cuda::BufferPool::BufferPool(Stream& stream) : allocator_(stream.impl_->stackAllocator.get())
671+
#ifndef HAVE_CUDA
672+
cv::cuda::BufferPool::BufferPool(Stream& stream)
673+
{
674+
(void) stream;
675+
throw_no_cuda();
676+
}
677+
#else
678+
cv::cuda::BufferPool::BufferPool(Stream& stream) : allocator_(stream.impl_->stackAllocator)
674679
{
675680
}
681+
#endif
676682

677683
GpuMat cv::cuda::BufferPool::getBuffer(int rows, int cols, int type)
678684
{
685+
#ifndef HAVE_CUDA
686+
(void) rows;
687+
(void) cols;
688+
(void) type;
689+
throw_no_cuda();
690+
return GpuMat();
691+
#else
679692
GpuMat buf(allocator_);
680693
buf.create(rows, cols, type);
681694
return buf;
695+
#endif
682696
}
683697

684-
#endif
685698

686699
////////////////////////////////////////////////////////////////
687700
// Event

0 commit comments

Comments
 (0)