diff --git a/modules/core/include/opencv2/core/cuda.hpp b/modules/core/include/opencv2/core/cuda.hpp index 4c5c522e24f4c5213b1d01cdf694b2f89f3e7a6b..8856520c1ad9e3bb47bc1f5035ed5541d3643ef1 100644 --- a/modules/core/include/opencv2/core/cuda.hpp +++ b/modules/core/include/opencv2/core/cuda.hpp @@ -507,6 +507,9 @@ public: //! creates a new asynchronous stream Stream(); + //! creates a new asynchronous stream with custom allocator + Stream(const Ptr& allocator); + /** @brief Returns true if the current stream queue is finished. Otherwise, it returns false. */ bool queryIfComplete() const; diff --git a/modules/core/src/cuda_stream.cpp b/modules/core/src/cuda_stream.cpp index 24ecbd4ab0673950af940a8cc4f722e2423cf46e..696771404ccb60625b8d20a0073403731c3e7f2b 100644 --- a/modules/core/src/cuda_stream.cpp +++ b/modules/core/src/cuda_stream.cpp @@ -282,9 +282,10 @@ public: cudaStream_t stream; bool ownStream; - Ptr stackAllocator; + Ptr allocator; Impl(); + Impl(const Ptr& allocator); explicit Impl(cudaStream_t stream); ~Impl(); @@ -295,17 +296,23 @@ cv::cuda::Stream::Impl::Impl() : stream(0), ownStream(false) cudaSafeCall( cudaStreamCreate(&stream) ); ownStream = true; - stackAllocator = makePtr(stream); + allocator = makePtr(stream); +} + +cv::cuda::Stream::Impl::Impl(const Ptr& allocator) : stream(0), ownStream(false), allocator(allocator) +{ + cudaSafeCall( cudaStreamCreate(&stream) ); + ownStream = true; } cv::cuda::Stream::Impl::Impl(cudaStream_t stream_) : stream(stream_), ownStream(false) { - stackAllocator = makePtr(stream); + allocator = makePtr(stream); } cv::cuda::Stream::Impl::~Impl() { - stackAllocator.release(); + allocator.release(); if (stream && ownStream) { @@ -417,6 +424,16 @@ cv::cuda::Stream::Stream() #endif } +cv::cuda::Stream::Stream(const Ptr& allocator) +{ +#ifndef HAVE_CUDA + (void) allocator; + throw_no_cuda(); +#else + impl_ = makePtr(allocator); +#endif +} + bool cv::cuda::Stream::queryIfComplete() const { #ifndef HAVE_CUDA @@ -675,7 +692,7 @@ cv::cuda::BufferPool::BufferPool(Stream& stream) throw_no_cuda(); } #else -cv::cuda::BufferPool::BufferPool(Stream& stream) : allocator_(stream.impl_->stackAllocator) +cv::cuda::BufferPool::BufferPool(Stream& stream) : allocator_(stream.impl_->allocator) { } #endif