ContinuousGpuMat.hpp 1.28 KB
#pragma once

#include <cuda_runtime.h>

#include <opencv2/core/cuda.hpp>

class ContinuousGpuMat {
  public:
	ContinuousGpuMat(const ContinuousGpuMat &) = delete;
	ContinuousGpuMat &operator=(const ContinuousGpuMat &) = delete;
	ContinuousGpuMat &operator=(ContinuousGpuMat &&) noexcept = delete;
	ContinuousGpuMat(ContinuousGpuMat &&) noexcept = delete;

	ContinuousGpuMat(const int rows, const int cols, const int type) {
		allocate(rows, cols, type);
	}

	ContinuousGpuMat(const cv::Size &size, const int type)
	    : ContinuousGpuMat(size.height, size.width, type) {}

	explicit ContinuousGpuMat(cv::InputArray host)
	    : ContinuousGpuMat(host.rows(), host.cols(), host.type()) {
		continuousMat.upload(host);
	}

	void create(const cv::Size &size, const int type) {
		allocate(size.height, size.width, type);
	}

	cv::Size size() const { return continuousMat.size(); }

	int type() const { return continuousMat.type(); }

	cv::cuda::GpuMat &device() { return continuousMat; }

	~ContinuousGpuMat() { cudaFree(deviceMemory); }

  private:
	void *deviceMemory{nullptr};
	cv::cuda::GpuMat continuousMat;

	void allocate(const int rows, const int cols, const int type) {
		cudaMalloc(&deviceMemory, rows * cols * cv::getElemSize(type));
		continuousMat = cv::cuda::GpuMat(rows, cols, type, deviceMemory);
	}
};