cuda.inl.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  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.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  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.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  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.
  27. //
  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.
  30. //
  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.
  41. //
  42. //M*/
  43. #ifndef OPENCV_CORE_CUDAINL_HPP
  44. #define OPENCV_CORE_CUDAINL_HPP
  45. #include "opencv2/core/cuda.hpp"
  46. //! @cond IGNORED
  47. namespace cv { namespace cuda {
  48. //===================================================================================
  49. // GpuMat
  50. //===================================================================================
  51. inline
  52. GpuMat::GpuMat(Allocator* allocator_)
  53. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  54. {}
  55. inline
  56. GpuMat::GpuMat(int rows_, int cols_, int type_, Allocator* allocator_)
  57. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  58. {
  59. if (rows_ > 0 && cols_ > 0)
  60. create(rows_, cols_, type_);
  61. }
  62. inline
  63. GpuMat::GpuMat(Size size_, int type_, Allocator* allocator_)
  64. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  65. {
  66. if (size_.height > 0 && size_.width > 0)
  67. create(size_.height, size_.width, type_);
  68. }
  69. inline
  70. GpuMat::GpuMat(int rows_, int cols_, int type_, Scalar s_, Allocator* allocator_)
  71. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  72. {
  73. if (rows_ > 0 && cols_ > 0)
  74. {
  75. create(rows_, cols_, type_);
  76. setTo(s_);
  77. }
  78. }
  79. inline
  80. GpuMat::GpuMat(Size size_, int type_, Scalar s_, Allocator* allocator_)
  81. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  82. {
  83. if (size_.height > 0 && size_.width > 0)
  84. {
  85. create(size_.height, size_.width, type_);
  86. setTo(s_);
  87. }
  88. }
  89. inline
  90. GpuMat::GpuMat(const GpuMat& m)
  91. : 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)
  92. {
  93. if (refcount)
  94. CV_XADD(refcount, 1);
  95. }
  96. inline
  97. GpuMat::GpuMat(InputArray arr, Allocator* allocator_) :
  98. flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), allocator(allocator_)
  99. {
  100. upload(arr);
  101. }
  102. inline
  103. GpuMat::~GpuMat()
  104. {
  105. release();
  106. }
  107. inline
  108. GpuMat& GpuMat::operator =(const GpuMat& m)
  109. {
  110. if (this != &m)
  111. {
  112. GpuMat temp(m);
  113. swap(temp);
  114. }
  115. return *this;
  116. }
  117. inline
  118. void GpuMat::create(Size size_, int type_)
  119. {
  120. create(size_.height, size_.width, type_);
  121. }
  122. inline
  123. void GpuMat::swap(GpuMat& b)
  124. {
  125. std::swap(flags, b.flags);
  126. std::swap(rows, b.rows);
  127. std::swap(cols, b.cols);
  128. std::swap(step, b.step);
  129. std::swap(data, b.data);
  130. std::swap(datastart, b.datastart);
  131. std::swap(dataend, b.dataend);
  132. std::swap(refcount, b.refcount);
  133. std::swap(allocator, b.allocator);
  134. }
  135. inline
  136. GpuMat GpuMat::clone() const
  137. {
  138. GpuMat m;
  139. copyTo(m);
  140. return m;
  141. }
  142. inline
  143. void GpuMat::copyTo(OutputArray dst, InputArray mask) const
  144. {
  145. copyTo(dst, mask, Stream::Null());
  146. }
  147. inline
  148. GpuMat& GpuMat::setTo(Scalar s)
  149. {
  150. return setTo(s, Stream::Null());
  151. }
  152. inline
  153. GpuMat& GpuMat::setTo(Scalar s, InputArray mask)
  154. {
  155. return setTo(s, mask, Stream::Null());
  156. }
  157. inline
  158. void GpuMat::convertTo(OutputArray dst, int rtype) const
  159. {
  160. convertTo(dst, rtype, Stream::Null());
  161. }
  162. inline
  163. void GpuMat::convertTo(OutputArray dst, int rtype, double alpha, double beta) const
  164. {
  165. convertTo(dst, rtype, alpha, beta, Stream::Null());
  166. }
  167. inline
  168. void GpuMat::convertTo(OutputArray dst, int rtype, double alpha, Stream& stream) const
  169. {
  170. convertTo(dst, rtype, alpha, 0.0, stream);
  171. }
  172. inline
  173. void GpuMat::assignTo(GpuMat& m, int _type) const
  174. {
  175. if (_type < 0)
  176. m = *this;
  177. else
  178. convertTo(m, _type);
  179. }
  180. inline
  181. uchar* GpuMat::ptr(int y)
  182. {
  183. CV_DbgAssert( (unsigned)y < (unsigned)rows );
  184. return data + step * y;
  185. }
  186. inline
  187. const uchar* GpuMat::ptr(int y) const
  188. {
  189. CV_DbgAssert( (unsigned)y < (unsigned)rows );
  190. return data + step * y;
  191. }
  192. template<typename _Tp> inline
  193. _Tp* GpuMat::ptr(int y)
  194. {
  195. return (_Tp*)ptr(y);
  196. }
  197. template<typename _Tp> inline
  198. const _Tp* GpuMat::ptr(int y) const
  199. {
  200. return (const _Tp*)ptr(y);
  201. }
  202. template <class T> inline
  203. GpuMat::operator PtrStepSz<T>() const
  204. {
  205. return PtrStepSz<T>(rows, cols, (T*)data, step);
  206. }
  207. template <class T> inline
  208. GpuMat::operator PtrStep<T>() const
  209. {
  210. return PtrStep<T>((T*)data, step);
  211. }
  212. inline
  213. GpuMat GpuMat::row(int y) const
  214. {
  215. return GpuMat(*this, Range(y, y+1), Range::all());
  216. }
  217. inline
  218. GpuMat GpuMat::col(int x) const
  219. {
  220. return GpuMat(*this, Range::all(), Range(x, x+1));
  221. }
  222. inline
  223. GpuMat GpuMat::rowRange(int startrow, int endrow) const
  224. {
  225. return GpuMat(*this, Range(startrow, endrow), Range::all());
  226. }
  227. inline
  228. GpuMat GpuMat::rowRange(Range r) const
  229. {
  230. return GpuMat(*this, r, Range::all());
  231. }
  232. inline
  233. GpuMat GpuMat::colRange(int startcol, int endcol) const
  234. {
  235. return GpuMat(*this, Range::all(), Range(startcol, endcol));
  236. }
  237. inline
  238. GpuMat GpuMat::colRange(Range r) const
  239. {
  240. return GpuMat(*this, Range::all(), r);
  241. }
  242. inline
  243. GpuMat GpuMat::operator ()(Range rowRange_, Range colRange_) const
  244. {
  245. return GpuMat(*this, rowRange_, colRange_);
  246. }
  247. inline
  248. GpuMat GpuMat::operator ()(Rect roi) const
  249. {
  250. return GpuMat(*this, roi);
  251. }
  252. inline
  253. bool GpuMat::isContinuous() const
  254. {
  255. return (flags & Mat::CONTINUOUS_FLAG) != 0;
  256. }
  257. inline
  258. size_t GpuMat::elemSize() const
  259. {
  260. return CV_ELEM_SIZE(flags);
  261. }
  262. inline
  263. size_t GpuMat::elemSize1() const
  264. {
  265. return CV_ELEM_SIZE1(flags);
  266. }
  267. inline
  268. int GpuMat::type() const
  269. {
  270. return CV_MAT_TYPE(flags);
  271. }
  272. inline
  273. int GpuMat::depth() const
  274. {
  275. return CV_MAT_DEPTH(flags);
  276. }
  277. inline
  278. int GpuMat::channels() const
  279. {
  280. return CV_MAT_CN(flags);
  281. }
  282. inline
  283. size_t GpuMat::step1() const
  284. {
  285. return step / elemSize1();
  286. }
  287. inline
  288. Size GpuMat::size() const
  289. {
  290. return Size(cols, rows);
  291. }
  292. inline
  293. bool GpuMat::empty() const
  294. {
  295. return data == 0;
  296. }
  297. static inline
  298. GpuMat createContinuous(int rows, int cols, int type)
  299. {
  300. GpuMat m;
  301. createContinuous(rows, cols, type, m);
  302. return m;
  303. }
  304. static inline
  305. void createContinuous(Size size, int type, OutputArray arr)
  306. {
  307. createContinuous(size.height, size.width, type, arr);
  308. }
  309. static inline
  310. GpuMat createContinuous(Size size, int type)
  311. {
  312. GpuMat m;
  313. createContinuous(size, type, m);
  314. return m;
  315. }
  316. static inline
  317. void ensureSizeIsEnough(Size size, int type, OutputArray arr)
  318. {
  319. ensureSizeIsEnough(size.height, size.width, type, arr);
  320. }
  321. static inline
  322. void swap(GpuMat& a, GpuMat& b)
  323. {
  324. a.swap(b);
  325. }
  326. //===================================================================================
  327. // HostMem
  328. //===================================================================================
  329. inline
  330. HostMem::HostMem(AllocType alloc_type_)
  331. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
  332. {
  333. }
  334. inline
  335. HostMem::HostMem(const HostMem& m)
  336. : 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)
  337. {
  338. if( refcount )
  339. CV_XADD(refcount, 1);
  340. }
  341. inline
  342. HostMem::HostMem(int rows_, int cols_, int type_, AllocType alloc_type_)
  343. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
  344. {
  345. if (rows_ > 0 && cols_ > 0)
  346. create(rows_, cols_, type_);
  347. }
  348. inline
  349. HostMem::HostMem(Size size_, int type_, AllocType alloc_type_)
  350. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
  351. {
  352. if (size_.height > 0 && size_.width > 0)
  353. create(size_.height, size_.width, type_);
  354. }
  355. inline
  356. HostMem::HostMem(InputArray arr, AllocType alloc_type_)
  357. : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), alloc_type(alloc_type_)
  358. {
  359. arr.getMat().copyTo(*this);
  360. }
  361. inline
  362. HostMem::~HostMem()
  363. {
  364. release();
  365. }
  366. inline
  367. HostMem& HostMem::operator =(const HostMem& m)
  368. {
  369. if (this != &m)
  370. {
  371. HostMem temp(m);
  372. swap(temp);
  373. }
  374. return *this;
  375. }
  376. inline
  377. void HostMem::swap(HostMem& b)
  378. {
  379. std::swap(flags, b.flags);
  380. std::swap(rows, b.rows);
  381. std::swap(cols, b.cols);
  382. std::swap(step, b.step);
  383. std::swap(data, b.data);
  384. std::swap(datastart, b.datastart);
  385. std::swap(dataend, b.dataend);
  386. std::swap(refcount, b.refcount);
  387. std::swap(alloc_type, b.alloc_type);
  388. }
  389. inline
  390. HostMem HostMem::clone() const
  391. {
  392. HostMem m(size(), type(), alloc_type);
  393. createMatHeader().copyTo(m);
  394. return m;
  395. }
  396. inline
  397. void HostMem::create(Size size_, int type_)
  398. {
  399. create(size_.height, size_.width, type_);
  400. }
  401. inline
  402. Mat HostMem::createMatHeader() const
  403. {
  404. return Mat(size(), type(), data, step);
  405. }
  406. inline
  407. bool HostMem::isContinuous() const
  408. {
  409. return (flags & Mat::CONTINUOUS_FLAG) != 0;
  410. }
  411. inline
  412. size_t HostMem::elemSize() const
  413. {
  414. return CV_ELEM_SIZE(flags);
  415. }
  416. inline
  417. size_t HostMem::elemSize1() const
  418. {
  419. return CV_ELEM_SIZE1(flags);
  420. }
  421. inline
  422. int HostMem::type() const
  423. {
  424. return CV_MAT_TYPE(flags);
  425. }
  426. inline
  427. int HostMem::depth() const
  428. {
  429. return CV_MAT_DEPTH(flags);
  430. }
  431. inline
  432. int HostMem::channels() const
  433. {
  434. return CV_MAT_CN(flags);
  435. }
  436. inline
  437. size_t HostMem::step1() const
  438. {
  439. return step / elemSize1();
  440. }
  441. inline
  442. Size HostMem::size() const
  443. {
  444. return Size(cols, rows);
  445. }
  446. inline
  447. bool HostMem::empty() const
  448. {
  449. return data == 0;
  450. }
  451. static inline
  452. void swap(HostMem& a, HostMem& b)
  453. {
  454. a.swap(b);
  455. }
  456. //===================================================================================
  457. // Stream
  458. //===================================================================================
  459. inline
  460. Stream::Stream(const Ptr<Impl>& impl)
  461. : impl_(impl)
  462. {
  463. }
  464. //===================================================================================
  465. // Event
  466. //===================================================================================
  467. inline
  468. Event::Event(const Ptr<Impl>& impl)
  469. : impl_(impl)
  470. {
  471. }
  472. //===================================================================================
  473. // Initialization & Info
  474. //===================================================================================
  475. inline
  476. bool TargetArchs::has(int major, int minor)
  477. {
  478. return hasPtx(major, minor) || hasBin(major, minor);
  479. }
  480. inline
  481. bool TargetArchs::hasEqualOrGreater(int major, int minor)
  482. {
  483. return hasEqualOrGreaterPtx(major, minor) || hasEqualOrGreaterBin(major, minor);
  484. }
  485. inline
  486. DeviceInfo::DeviceInfo()
  487. {
  488. device_id_ = getDevice();
  489. }
  490. inline
  491. DeviceInfo::DeviceInfo(int device_id)
  492. {
  493. CV_Assert( device_id >= 0 && device_id < getCudaEnabledDeviceCount() );
  494. device_id_ = device_id;
  495. }
  496. inline
  497. int DeviceInfo::deviceID() const
  498. {
  499. return device_id_;
  500. }
  501. inline
  502. size_t DeviceInfo::freeMemory() const
  503. {
  504. size_t _totalMemory = 0, _freeMemory = 0;
  505. queryMemory(_totalMemory, _freeMemory);
  506. return _freeMemory;
  507. }
  508. inline
  509. size_t DeviceInfo::totalMemory() const
  510. {
  511. size_t _totalMemory = 0, _freeMemory = 0;
  512. queryMemory(_totalMemory, _freeMemory);
  513. return _totalMemory;
  514. }
  515. inline
  516. bool DeviceInfo::supports(FeatureSet feature_set) const
  517. {
  518. int version = majorVersion() * 10 + minorVersion();
  519. return version >= feature_set;
  520. }
  521. }} // namespace cv { namespace cuda {
  522. //===================================================================================
  523. // Mat
  524. //===================================================================================
  525. namespace cv {
  526. inline
  527. Mat::Mat(const cuda::GpuMat& m)
  528. : flags(0), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows)
  529. {
  530. m.download(*this);
  531. }
  532. }
  533. //! @endcond
  534. #endif // OPENCV_CORE_CUDAINL_HPP