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