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_PRIVATE_CUDA_HPP__
45 #define __OPENCV_CORE_PRIVATE_CUDA_HPP__
46 
47 #ifndef __OPENCV_BUILD
48 #  error this is a private header which should not be used from outside of the OpenCV library
49 #endif
50 
51 #include "cvconfig.h"
52 
53 #include "opencv2/core/cvdef.h"
54 #include "opencv2/core/base.hpp"
55 
56 #include "opencv2/core/cuda.hpp"
57 
58 #ifdef HAVE_CUDA
59 #  include <cuda.h>
60 #  include <cuda_runtime.h>
61 #  include <npp.h>
62 #  include "opencv2/core/cuda_stream_accessor.hpp"
63 #  include "opencv2/core/cuda/common.hpp"
64 
65 #  define NPP_VERSION (NPP_VERSION_MAJOR * 1000 + NPP_VERSION_MINOR * 100 + NPP_VERSION_BUILD)
66 
67 #  define CUDART_MINIMUM_REQUIRED_VERSION 4020
68 
69 #  if (CUDART_VERSION < CUDART_MINIMUM_REQUIRED_VERSION)
70 #    error "Insufficient Cuda Runtime library version, please update it."
71 #  endif
72 
73 #  if defined(CUDA_ARCH_BIN_OR_PTX_10)
74 #    error "OpenCV CUDA module doesn't support NVIDIA compute capability 1.0"
75 #  endif
76 #endif
77 
78 //! @cond IGNORED
79 
80 namespace cv { namespace cuda {
81     CV_EXPORTS cv::String getNppErrorMessage(int code);
82     CV_EXPORTS cv::String getCudaDriverApiErrorMessage(int code);
83 
84     CV_EXPORTS GpuMat getInputMat(InputArray _src, Stream& stream);
85 
86     CV_EXPORTS GpuMat getOutputMat(OutputArray _dst, int rows, int cols, int type, Stream& stream);
getOutputMat(OutputArray _dst,Size size,int type,Stream & stream)87     static inline GpuMat getOutputMat(OutputArray _dst, Size size, int type, Stream& stream)
88     {
89         return getOutputMat(_dst, size.height, size.width, type, stream);
90     }
91 
92     CV_EXPORTS void syncOutput(const GpuMat& dst, OutputArray _dst, Stream& stream);
93 }}
94 
95 #ifndef HAVE_CUDA
96 
throw_no_cuda()97 static inline void throw_no_cuda() { CV_Error(cv::Error::GpuNotSupported, "The library is compiled without CUDA support"); }
98 
99 #else // HAVE_CUDA
100 
throw_no_cuda()101 static inline void throw_no_cuda() { CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform"); }
102 
103 namespace cv { namespace cuda
104 {
105     class CV_EXPORTS BufferPool
106     {
107     public:
108         explicit BufferPool(Stream& stream);
109 
110         GpuMat getBuffer(int rows, int cols, int type);
getBuffer(Size size,int type)111         GpuMat getBuffer(Size size, int type) { return getBuffer(size.height, size.width, type); }
112 
getAllocator() const113         GpuMat::Allocator* getAllocator() const { return allocator_; }
114 
115     private:
116         GpuMat::Allocator* allocator_;
117     };
118 
checkNppError(int code,const char * file,const int line,const char * func)119     static inline void checkNppError(int code, const char* file, const int line, const char* func)
120     {
121         if (code < 0)
122             cv::error(cv::Error::GpuApiCallError, getNppErrorMessage(code), func, file, line);
123     }
124 
checkCudaDriverApiError(int code,const char * file,const int line,const char * func)125     static inline void checkCudaDriverApiError(int code, const char* file, const int line, const char* func)
126     {
127         if (code != CUDA_SUCCESS)
128             cv::error(cv::Error::GpuApiCallError, getCudaDriverApiErrorMessage(code), func, file, line);
129     }
130 
131     template<int n> struct NPPTypeTraits;
132     template<> struct NPPTypeTraits<CV_8U>  { typedef Npp8u npp_type; };
133     template<> struct NPPTypeTraits<CV_8S>  { typedef Npp8s npp_type; };
134     template<> struct NPPTypeTraits<CV_16U> { typedef Npp16u npp_type; };
135     template<> struct NPPTypeTraits<CV_16S> { typedef Npp16s npp_type; };
136     template<> struct NPPTypeTraits<CV_32S> { typedef Npp32s npp_type; };
137     template<> struct NPPTypeTraits<CV_32F> { typedef Npp32f npp_type; };
138     template<> struct NPPTypeTraits<CV_64F> { typedef Npp64f npp_type; };
139 
140     class NppStreamHandler
141     {
142     public:
NppStreamHandler(Stream & newStream)143         inline explicit NppStreamHandler(Stream& newStream)
144         {
145             oldStream = nppGetStream();
146             nppSetStream(StreamAccessor::getStream(newStream));
147         }
148 
NppStreamHandler(cudaStream_t newStream)149         inline explicit NppStreamHandler(cudaStream_t newStream)
150         {
151             oldStream = nppGetStream();
152             nppSetStream(newStream);
153         }
154 
~NppStreamHandler()155         inline ~NppStreamHandler()
156         {
157             nppSetStream(oldStream);
158         }
159 
160     private:
161         cudaStream_t oldStream;
162     };
163 }}
164 
165 #define nppSafeCall(expr)  cv::cuda::checkNppError(expr, __FILE__, __LINE__, CV_Func)
166 #define cuSafeCall(expr)  cv::cuda::checkCudaDriverApiError(expr, __FILE__, __LINE__, CV_Func)
167 
168 #endif // HAVE_CUDA
169 
170 //! @endcond
171 
172 #endif // __OPENCV_CORE_CUDA_PRIVATE_HPP__
173