cuda.inl.hpp 15 KB

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