diff --git a/modules/fastcv/include/opencv2/fastcv/allocator.hpp b/modules/fastcv/include/opencv2/fastcv/allocator.hpp index a70666723c..1b568bde33 100644 --- a/modules/fastcv/include/opencv2/fastcv/allocator.hpp +++ b/modules/fastcv/include/opencv2/fastcv/allocator.hpp @@ -36,12 +36,15 @@ class QcResourceManager { /** * @brief Qualcomm's custom allocator. * This allocator uses Qualcomm's memory management functions. + * + * Note: The userdata field of cv::UMatData is used to store the file descriptor (fd) of the allocated memory. + * */ class QcAllocator : public cv::MatAllocator { public: QcAllocator(); ~QcAllocator(); - + cv::UMatData* allocate(int dims, const int* sizes, int type, void* data0, size_t* step, cv::AccessFlag flags, cv::UMatUsageFlags usageFlags) const CV_OVERRIDE; bool allocate(cv::UMatData* u, cv::AccessFlag accessFlags, cv::UMatUsageFlags usageFlags) const CV_OVERRIDE; void deallocate(cv::UMatData* u) const CV_OVERRIDE; diff --git a/modules/fastcv/src/allocator.cpp b/modules/fastcv/src/allocator.cpp index 83147d2354..6658b00d4c 100644 --- a/modules/fastcv/src/allocator.cpp +++ b/modules/fastcv/src/allocator.cpp @@ -55,13 +55,19 @@ cv::UMatData* QcAllocator::allocate(int dims, const int* sizes, int type, } total *= sizes[i]; } - uchar* data = data0 ? (uchar*)data0 : (uchar*)fcvHwMemAlloc(total, 16); + + int fd = -1; + uchar* data = data0 ? (uchar*)data0 : (uchar*)fcvHwMemAlloc(total, 16, &fd); cv::UMatData* u = new cv::UMatData(this); u->data = u->origdata = data; u->size = total; if(data0) u->flags |= cv::UMatData::USER_ALLOCATED; + // Store FD in userdata (cast to void*) + if (fd >= 0) + u->userdata = reinterpret_cast(static_cast(fd)); + // Add to active allocations cv::fastcv::QcResourceManager::getInstance().addAllocation(data);