1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
24 // * Redistribution's in binary form must reproduce the above copyright notice,
25 // this list of conditions and the following disclaimer in the documentation
26 // and/or other materials provided with the distribution.
28 // * The name of the copyright holders may not be used to endorse or promote products
29 // derived from this software without specific prior written permission.
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
44 #ifndef __OPENCV_CORE_CUDAINL_HPP__
45 #define __OPENCV_CORE_CUDAINL_HPP__
47 #include "opencv2/core/cuda.hpp"
49 namespace cv { namespace cuda {
51 //////////////////////////////// GpuMat ///////////////////////////////
54 GpuMat::GpuMat(Allocator* allocator_)
55 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
59 GpuMat::GpuMat(int rows_, int cols_, int type_, Allocator* allocator_)
60 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
62 if (rows_ > 0 && cols_ > 0)
63 create(rows_, cols_, type_);
67 GpuMat::GpuMat(Size size_, int type_, Allocator* allocator_)
68 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
70 if (size_.height > 0 && size_.width > 0)
71 create(size_.height, size_.width, type_);
75 GpuMat::GpuMat(int rows_, int cols_, int type_, Scalar s_, Allocator* allocator_)
76 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
78 if (rows_ > 0 && cols_ > 0)
80 create(rows_, cols_, type_);
86 GpuMat::GpuMat(Size size_, int type_, Scalar s_, Allocator* allocator_)
87 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
89 if (size_.height > 0 && size_.width > 0)
91 create(size_.height, size_.width, type_);
97 GpuMat::GpuMat(const GpuMat& m)
98 : flags(m.flags), rows(m.rows), cols(m.cols), step(m.step), data(m.data), refcount(m.refcount), datastart(m.datastart), dataend(m.dataend), allocator(m.allocator)
101 CV_XADD(refcount, 1);
105 GpuMat::GpuMat(InputArray arr, Allocator* allocator_) :
106 flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
118 GpuMat& GpuMat::operator =(const GpuMat& m)
130 void GpuMat::create(Size size_, int type_)
132 create(size_.height, size_.width, type_);
136 void GpuMat::swap(GpuMat& b)
138 std::swap(flags, b.flags);
139 std::swap(rows, b.rows);
140 std::swap(cols, b.cols);
141 std::swap(step, b.step);
142 std::swap(data, b.data);
143 std::swap(datastart, b.datastart);
144 std::swap(dataend, b.dataend);
145 std::swap(refcount, b.refcount);
149 GpuMat GpuMat::clone() const
157 void GpuMat::copyTo(OutputArray dst, InputArray mask) const
159 copyTo(dst, mask, Stream::Null());
163 GpuMat& GpuMat::setTo(Scalar s)
165 return setTo(s, Stream::Null());
169 GpuMat& GpuMat::setTo(Scalar s, InputArray mask)
171 return setTo(s, mask, Stream::Null());
175 void GpuMat::convertTo(OutputArray dst, int rtype) const
177 convertTo(dst, rtype, Stream::Null());
181 void GpuMat::convertTo(OutputArray dst, int rtype, double alpha, double beta) const
183 convertTo(dst, rtype, alpha, beta, Stream::Null());
187 void GpuMat::convertTo(OutputArray dst, int rtype, double alpha, Stream& stream) const
189 convertTo(dst, rtype, alpha, 0.0, stream);
193 void GpuMat::assignTo(GpuMat& m, int _type) const
202 uchar* GpuMat::ptr(int y)
204 CV_DbgAssert( (unsigned)y < (unsigned)rows );
205 return data + step * y;
209 const uchar* GpuMat::ptr(int y) const
211 CV_DbgAssert( (unsigned)y < (unsigned)rows );
212 return data + step * y;
215 template<typename _Tp> inline
216 _Tp* GpuMat::ptr(int y)
221 template<typename _Tp> inline
222 const _Tp* GpuMat::ptr(int y) const
224 return (const _Tp*)ptr(y);
227 template <class T> inline
228 GpuMat::operator PtrStepSz<T>() const
230 return PtrStepSz<T>(rows, cols, (T*)data, step);
233 template <class T> inline
234 GpuMat::operator PtrStep<T>() const
236 return PtrStep<T>((T*)data, step);
240 GpuMat GpuMat::row(int y) const
242 return GpuMat(*this, Range(y, y+1), Range::all());
246 GpuMat GpuMat::col(int x) const
248 return GpuMat(*this, Range::all(), Range(x, x+1));
252 GpuMat GpuMat::rowRange(int startrow, int endrow) const
254 return GpuMat(*this, Range(startrow, endrow), Range::all());
258 GpuMat GpuMat::rowRange(Range r) const
260 return GpuMat(*this, r, Range::all());
264 GpuMat GpuMat::colRange(int startcol, int endcol) const
266 return GpuMat(*this, Range::all(), Range(startcol, endcol));
270 GpuMat GpuMat::colRange(Range r) const
272 return GpuMat(*this, Range::all(), r);
276 GpuMat GpuMat::operator ()(Range rowRange_, Range colRange_) const
278 return GpuMat(*this, rowRange_, colRange_);
282 GpuMat GpuMat::operator ()(Rect roi) const
284 return GpuMat(*this, roi);
288 bool GpuMat::isContinuous() const
290 return (flags & Mat::CONTINUOUS_FLAG) != 0;
294 size_t GpuMat::elemSize() const
296 return CV_ELEM_SIZE(flags);
300 size_t GpuMat::elemSize1() const
302 return CV_ELEM_SIZE1(flags);
306 int GpuMat::type() const
308 return CV_MAT_TYPE(flags);
312 int GpuMat::depth() const
314 return CV_MAT_DEPTH(flags);
318 int GpuMat::channels() const
320 return CV_MAT_CN(flags);
324 size_t GpuMat::step1() const
326 return step / elemSize1();
330 Size GpuMat::size() const
332 return Size(cols, rows);
336 bool GpuMat::empty() const
342 GpuMat createContinuous(int rows, int cols, int type)
345 createContinuous(rows, cols, type, m);
350 void createContinuous(Size size, int type, OutputArray arr)
352 createContinuous(size.height, size.width, type, arr);
356 GpuMat createContinuous(Size size, int type)
359 createContinuous(size, type, m);
364 void ensureSizeIsEnough(Size size, int type, OutputArray arr)
366 ensureSizeIsEnough(size.height, size.width, type, arr);
370 void swap(GpuMat& a, GpuMat& b)
375 //////////////////////////////// CudaMem ////////////////////////////////
378 CudaMem::CudaMem(AllocType alloc_type_)
379 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
384 CudaMem::CudaMem(const CudaMem& m)
385 : flags(m.flags), rows(m.rows), cols(m.cols), step(m.step), data(m.data), refcount(m.refcount), datastart(m.datastart), dataend(m.dataend), alloc_type(m.alloc_type)
388 CV_XADD(refcount, 1);
392 CudaMem::CudaMem(int rows_, int cols_, int type_, AllocType alloc_type_)
393 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
395 if (rows_ > 0 && cols_ > 0)
396 create(rows_, cols_, type_);
400 CudaMem::CudaMem(Size size_, int type_, AllocType alloc_type_)
401 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
403 if (size_.height > 0 && size_.width > 0)
404 create(size_.height, size_.width, type_);
408 CudaMem::CudaMem(InputArray arr, AllocType alloc_type_)
409 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
411 arr.getMat().copyTo(*this);
421 CudaMem& CudaMem::operator =(const CudaMem& m)
433 void CudaMem::swap(CudaMem& b)
435 std::swap(flags, b.flags);
436 std::swap(rows, b.rows);
437 std::swap(cols, b.cols);
438 std::swap(step, b.step);
439 std::swap(data, b.data);
440 std::swap(datastart, b.datastart);
441 std::swap(dataend, b.dataend);
442 std::swap(refcount, b.refcount);
443 std::swap(alloc_type, b.alloc_type);
447 CudaMem CudaMem::clone() const
449 CudaMem m(size(), type(), alloc_type);
450 createMatHeader().copyTo(m);
455 void CudaMem::create(Size size_, int type_)
457 create(size_.height, size_.width, type_);
461 Mat CudaMem::createMatHeader() const
463 return Mat(size(), type(), data, step);
467 bool CudaMem::isContinuous() const
469 return (flags & Mat::CONTINUOUS_FLAG) != 0;
473 size_t CudaMem::elemSize() const
475 return CV_ELEM_SIZE(flags);
479 size_t CudaMem::elemSize1() const
481 return CV_ELEM_SIZE1(flags);
485 int CudaMem::type() const
487 return CV_MAT_TYPE(flags);
491 int CudaMem::depth() const
493 return CV_MAT_DEPTH(flags);
497 int CudaMem::channels() const
499 return CV_MAT_CN(flags);
503 size_t CudaMem::step1() const
505 return step / elemSize1();
509 Size CudaMem::size() const
511 return Size(cols, rows);
515 bool CudaMem::empty() const
521 void swap(CudaMem& a, CudaMem& b)
526 //////////////////////////////// Stream ///////////////////////////////
529 Stream::Stream(const Ptr<Impl>& impl)
534 //////////////////////////////// Initialization & Info ////////////////////////
537 bool TargetArchs::has(int major, int minor)
539 return hasPtx(major, minor) || hasBin(major, minor);
543 bool TargetArchs::hasEqualOrGreater(int major, int minor)
545 return hasEqualOrGreaterPtx(major, minor) || hasEqualOrGreaterBin(major, minor);
549 DeviceInfo::DeviceInfo()
551 device_id_ = getDevice();
555 DeviceInfo::DeviceInfo(int device_id)
557 CV_Assert( device_id >= 0 && device_id < getCudaEnabledDeviceCount() );
558 device_id_ = device_id;
562 int DeviceInfo::deviceID() const
568 size_t DeviceInfo::freeMemory() const
570 size_t _totalMemory, _freeMemory;
571 queryMemory(_totalMemory, _freeMemory);
576 size_t DeviceInfo::totalMemory() const
578 size_t _totalMemory, _freeMemory;
579 queryMemory(_totalMemory, _freeMemory);
584 bool DeviceInfo::supports(FeatureSet feature_set) const
586 int version = majorVersion() * 10 + minorVersion();
587 return version >= feature_set;
590 }} // namespace cv { namespace cuda {
592 //////////////////////////////// Mat ////////////////////////////////
597 Mat::Mat(const cuda::GpuMat& m)
598 : flags(0), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows)
605 #endif // __OPENCV_CORE_CUDAINL_HPP__