Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / src / backends / gpu / ggpucore.cpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 //
5 // Copyright (C) 2018-2019 Intel Corporation
6
7
8 #include "precomp.hpp"
9
10 #include "opencv2/gapi/core.hpp"
11 #include "opencv2/gapi/gpu/core.hpp"
12 #include "backends/gpu/ggpucore.hpp"
13
14 GAPI_GPU_KERNEL(GGPUAdd, cv::gapi::core::GAdd)
15 {
16     static void run(const cv::UMat& a, const cv::UMat& b, int dtype, cv::UMat& out)
17     {
18         cv::add(a, b, out, cv::noArray(), dtype);
19     }
20 };
21
22 GAPI_GPU_KERNEL(GGPUAddC, cv::gapi::core::GAddC)
23 {
24     static void run(const cv::UMat& a, const cv::Scalar& b, int dtype, cv::UMat& out)
25     {
26         cv::add(a, b, out, cv::noArray(), dtype);
27     }
28 };
29
30 GAPI_GPU_KERNEL(GGPUSub, cv::gapi::core::GSub)
31 {
32     static void run(const cv::UMat& a, const cv::UMat& b, int dtype, cv::UMat& out)
33     {
34         cv::subtract(a, b, out, cv::noArray(), dtype);
35     }
36 };
37
38 GAPI_GPU_KERNEL(GGPUSubC, cv::gapi::core::GSubC)
39 {
40     static void run(const cv::UMat& a, const cv::Scalar& b, int dtype, cv::UMat& out)
41     {
42         cv::subtract(a, b, out, cv::noArray(), dtype);
43     }
44 };
45
46 GAPI_GPU_KERNEL(GGPUSubRC, cv::gapi::core::GSubRC)
47 {
48     static void run(const cv::Scalar& a, const cv::UMat& b, int dtype, cv::UMat& out)
49     {
50         cv::subtract(a, b, out, cv::noArray(), dtype);
51     }
52 };
53
54 GAPI_GPU_KERNEL(GGPUMul, cv::gapi::core::GMul)
55 {
56     static void run(const cv::UMat& a, const cv::UMat& b, double scale, int dtype, cv::UMat& out)
57     {
58         cv::multiply(a, b, out, scale, dtype);
59     }
60 };
61
62 GAPI_GPU_KERNEL(GGPUMulCOld, cv::gapi::core::GMulCOld)
63 {
64     static void run(const cv::UMat& a, double b, int dtype, cv::UMat& out)
65     {
66         cv::multiply(a, b, out, 1, dtype);
67     }
68 };
69
70 GAPI_GPU_KERNEL(GGPUMulC, cv::gapi::core::GMulC)
71 {
72     static void run(const cv::UMat& a, const cv::Scalar& b, int dtype, cv::UMat& out)
73     {
74         cv::multiply(a, b, out, 1, dtype);
75     }
76 };
77
78 GAPI_GPU_KERNEL(GGPUDiv, cv::gapi::core::GDiv)
79 {
80     static void run(const cv::UMat& a, const cv::UMat& b, double scale, int dtype, cv::UMat& out)
81     {
82         cv::divide(a, b, out, scale, dtype);
83     }
84 };
85
86 GAPI_GPU_KERNEL(GGPUDivC, cv::gapi::core::GDivC)
87 {
88     static void run(const cv::UMat& a, const cv::Scalar& b, double scale, int dtype, cv::UMat& out)
89     {
90         cv::divide(a, b, out, scale, dtype);
91     }
92 };
93
94 GAPI_GPU_KERNEL(GGPUDivRC, cv::gapi::core::GDivRC)
95 {
96     static void run(const cv::Scalar& a, const cv::UMat& b, double scale, int dtype, cv::UMat& out)
97     {
98         cv::divide(a, b, out, scale, dtype);
99     }
100 };
101
102 GAPI_GPU_KERNEL(GGPUMask, cv::gapi::core::GMask)
103 {
104     static void run(const cv::UMat& in, const cv::UMat& mask, cv::UMat& out)
105     {
106         out = cv::UMat::zeros(in.size(), in.type());
107         in.copyTo(out, mask);
108     }
109 };
110
111
112 GAPI_GPU_KERNEL(GGPUMean, cv::gapi::core::GMean)
113 {
114     static void run(const cv::UMat& in, cv::Scalar& out)
115     {
116         out = cv::mean(in);
117     }
118 };
119
120 GAPI_GPU_KERNEL(GGPUPolarToCart, cv::gapi::core::GPolarToCart)
121 {
122     static void run(const cv::UMat& magn, const cv::UMat& angle, bool angleInDegrees, cv::UMat& outx, cv::UMat& outy)
123     {
124         cv::polarToCart(magn, angle, outx, outy, angleInDegrees);
125     }
126 };
127
128 GAPI_GPU_KERNEL(GGPUCartToPolar, cv::gapi::core::GCartToPolar)
129 {
130     static void run(const cv::UMat& x, const cv::UMat& y, bool angleInDegrees, cv::UMat& outmagn, cv::UMat& outangle)
131     {
132         cv::cartToPolar(x, y, outmagn, outangle, angleInDegrees);
133     }
134 };
135
136 GAPI_GPU_KERNEL(GGPUCmpGT, cv::gapi::core::GCmpGT)
137 {
138     static void run(const cv::UMat& a, const cv::UMat& b, cv::UMat& out)
139     {
140         cv::compare(a, b, out, cv::CMP_GT);
141     }
142 };
143
144 GAPI_GPU_KERNEL(GGPUCmpGE, cv::gapi::core::GCmpGE)
145 {
146     static void run(const cv::UMat& a, const cv::UMat& b, cv::UMat& out)
147     {
148         cv::compare(a, b, out, cv::CMP_GE);
149     }
150 };
151
152 GAPI_GPU_KERNEL(GGPUCmpLE, cv::gapi::core::GCmpLE)
153 {
154     static void run(const cv::UMat& a, const cv::UMat& b, cv::UMat& out)
155     {
156         cv::compare(a, b, out, cv::CMP_LE);
157     }
158 };
159
160 GAPI_GPU_KERNEL(GGPUCmpLT, cv::gapi::core::GCmpLT)
161 {
162     static void run(const cv::UMat& a, const cv::UMat& b, cv::UMat& out)
163     {
164         cv::compare(a, b, out, cv::CMP_LT);
165     }
166 };
167
168 GAPI_GPU_KERNEL(GGPUCmpEQ, cv::gapi::core::GCmpEQ)
169 {
170     static void run(const cv::UMat& a, const cv::UMat& b, cv::UMat& out)
171     {
172         cv::compare(a, b, out, cv::CMP_EQ);
173     }
174 };
175
176 GAPI_GPU_KERNEL(GGPUCmpNE, cv::gapi::core::GCmpNE)
177 {
178     static void run(const cv::UMat& a, const cv::UMat& b, cv::UMat& out)
179     {
180         cv::compare(a, b, out, cv::CMP_NE);
181     }
182 };
183
184 GAPI_GPU_KERNEL(GGPUCmpGTScalar, cv::gapi::core::GCmpGTScalar)
185 {
186     static void run(const cv::UMat& a, const cv::Scalar& b, cv::UMat& out)
187     {
188         cv::compare(a, b, out, cv::CMP_GT);
189     }
190 };
191
192 GAPI_GPU_KERNEL(GGPUCmpGEScalar, cv::gapi::core::GCmpGEScalar)
193 {
194     static void run(const cv::UMat& a, const cv::Scalar& b, cv::UMat& out)
195     {
196         cv::compare(a, b, out, cv::CMP_GE);
197     }
198 };
199
200 GAPI_GPU_KERNEL(GGPUCmpLEScalar, cv::gapi::core::GCmpLEScalar)
201 {
202     static void run(const cv::UMat& a, const cv::Scalar& b, cv::UMat& out)
203     {
204         cv::compare(a, b, out, cv::CMP_LE);
205     }
206 };
207
208 GAPI_GPU_KERNEL(GGPUCmpLTScalar, cv::gapi::core::GCmpLTScalar)
209 {
210     static void run(const cv::UMat& a, const cv::Scalar& b, cv::UMat& out)
211     {
212         cv::compare(a, b, out, cv::CMP_LT);
213     }
214 };
215
216 GAPI_GPU_KERNEL(GGPUCmpEQScalar, cv::gapi::core::GCmpEQScalar)
217 {
218     static void run(const cv::UMat& a, const cv::Scalar& b, cv::UMat& out)
219     {
220         cv::compare(a, b, out, cv::CMP_EQ);
221     }
222 };
223
224 GAPI_GPU_KERNEL(GGPUCmpNEScalar, cv::gapi::core::GCmpNEScalar)
225 {
226     static void run(const cv::UMat& a, const cv::Scalar& b, cv::UMat& out)
227     {
228         cv::compare(a, b, out, cv::CMP_NE);
229     }
230 };
231
232 GAPI_GPU_KERNEL(GGPUAnd, cv::gapi::core::GAnd)
233 {
234     static void run(const cv::UMat& a, const cv::UMat& b, cv::UMat& out)
235     {
236         cv::bitwise_and(a, b, out);
237     }
238 };
239
240 GAPI_GPU_KERNEL(GGPUAndS, cv::gapi::core::GAndS)
241 {
242     static void run(const cv::UMat& a, const cv::Scalar& b, cv::UMat& out)
243     {
244         cv::bitwise_and(a, b, out);
245     }
246 };
247
248 GAPI_GPU_KERNEL(GGPUOr, cv::gapi::core::GOr)
249 {
250     static void run(const cv::UMat& a, const cv::UMat& b, cv::UMat& out)
251     {
252         cv::bitwise_or(a, b, out);
253     }
254 };
255
256 GAPI_GPU_KERNEL(GGPUOrS, cv::gapi::core::GOrS)
257 {
258     static void run(const cv::UMat& a, const cv::Scalar& b, cv::UMat& out)
259     {
260         cv::bitwise_or(a, b, out);
261     }
262 };
263
264 GAPI_GPU_KERNEL(GGPUXor, cv::gapi::core::GXor)
265 {
266     static void run(const cv::UMat& a, const cv::UMat& b, cv::UMat& out)
267     {
268         cv::bitwise_xor(a, b, out);
269     }
270 };
271
272 GAPI_GPU_KERNEL(GGPUXorS, cv::gapi::core::GXorS)
273 {
274     static void run(const cv::UMat& a, const cv::Scalar& b, cv::UMat& out)
275     {
276         cv::bitwise_xor(a, b, out);
277     }
278 };
279
280 GAPI_GPU_KERNEL(GGPUNot, cv::gapi::core::GNot)
281 {
282     static void run(const cv::UMat& a, cv::UMat& out)
283     {
284         cv::bitwise_not(a, out);
285     }
286 };
287
288 GAPI_GPU_KERNEL(GGPUSelect, cv::gapi::core::GSelect)
289 {
290     static void run(const cv::UMat& src1, const cv::UMat& src2, const cv::UMat& mask, cv::UMat& out)
291     {
292         src2.copyTo(out);
293         src1.copyTo(out, mask);
294     }
295 };
296
297 ////TODO: doesn't compiled with UMat
298 //GAPI_GPU_KERNEL(GGPUMin, cv::gapi::core::GMin)
299 //{
300 //    static void run(const cv::UMat& in1, const cv::UMat& in2, cv::UMat& out)
301 //    {
302 //        out = cv::min(in1, in2);
303 //    }
304 //};
305 //
306 ////TODO: doesn't compiled with UMat
307 //GAPI_GPU_KERNEL(GGPUMax, cv::gapi::core::GMax)
308 //{
309 //    static void run(const cv::UMat& in1, const cv::UMat& in2, cv::UMat& out)
310 //    {
311 //        out = cv::max(in1, in2);
312 //    }
313 //};
314
315
316 GAPI_GPU_KERNEL(GGPUAbsDiff, cv::gapi::core::GAbsDiff)
317 {
318     static void run(const cv::UMat& in1, const cv::UMat& in2, cv::UMat& out)
319     {
320         cv::absdiff(in1, in2, out);
321     }
322 };
323
324 GAPI_GPU_KERNEL(GGPUAbsDiffC, cv::gapi::core::GAbsDiffC)
325 {
326     static void run(const cv::UMat& in1, const cv::Scalar& in2, cv::UMat& out)
327     {
328         cv::absdiff(in1, in2, out);
329     }
330 };
331
332 GAPI_GPU_KERNEL(GGPUSum, cv::gapi::core::GSum)
333 {
334     static void run(const cv::UMat& in, cv::Scalar& out)
335     {
336         out = cv::sum(in);
337     }
338 };
339
340 GAPI_GPU_KERNEL(GGPUAddW, cv::gapi::core::GAddW)
341 {
342     static void run(const cv::UMat& in1, double alpha, const cv::UMat& in2, double beta, double gamma, int dtype, cv::UMat& out)
343     {
344         cv::addWeighted(in1, alpha, in2, beta, gamma, out, dtype);
345     }
346 };
347
348
349 GAPI_GPU_KERNEL(GGPUNormL1, cv::gapi::core::GNormL1)
350 {
351     static void run(const cv::UMat& in, cv::Scalar& out)
352     {
353         out = cv::norm(in, cv::NORM_L1);
354     }
355 };
356
357 GAPI_GPU_KERNEL(GGPUNormL2, cv::gapi::core::GNormL2)
358 {
359     static void run(const cv::UMat& in, cv::Scalar& out)
360     {
361         out = cv::norm(in, cv::NORM_L2);
362     }
363 };
364
365 GAPI_GPU_KERNEL(GGPUNormInf, cv::gapi::core::GNormInf)
366 {
367     static void run(const cv::UMat& in, cv::Scalar& out)
368     {
369         out = cv::norm(in, cv::NORM_INF);
370     }
371 };
372
373 GAPI_GPU_KERNEL(GGPUIntegral, cv::gapi::core::GIntegral)
374 {
375     static void run(const cv::UMat& in, int sdepth, int sqdepth, cv::UMat& out, cv::UMat& outSq)
376     {
377         cv::integral(in, out, outSq, sdepth, sqdepth);
378     }
379 };
380
381 GAPI_GPU_KERNEL(GGPUThreshold, cv::gapi::core::GThreshold)
382 {
383     static void run(const cv::UMat& in, const cv::Scalar& a, const cv::Scalar& b, int type, cv::UMat& out)
384     {
385         cv::threshold(in, out, a.val[0], b.val[0], type);
386     }
387 };
388
389 GAPI_GPU_KERNEL(GGPUThresholdOT, cv::gapi::core::GThresholdOT)
390 {
391     static void run(const cv::UMat& in, const cv::Scalar& b, int type, cv::UMat& out, cv::Scalar& outScalar)
392     {
393         outScalar = cv::threshold(in, out, b.val[0], b.val[0], type);
394     }
395 };
396
397
398 GAPI_GPU_KERNEL(GGPUInRange, cv::gapi::core::GInRange)
399 {
400     static void run(const cv::UMat& in, const cv::Scalar& low, const cv::Scalar& up, cv::UMat& out)
401     {
402         cv::inRange(in, low, up, out);
403     }
404 };
405
406 GAPI_GPU_KERNEL(GGPUSplit3, cv::gapi::core::GSplit3)
407 {
408     static void run(const cv::UMat& in, cv::UMat &m1, cv::UMat &m2, cv::UMat &m3)
409     {
410         std::vector<cv::UMat> outMats = {m1, m2, m3};
411         cv::split(in, outMats);
412
413         // Write back FIXME: Write a helper or avoid this nonsence completely!
414         m1 = outMats[0];
415         m2 = outMats[1];
416         m3 = outMats[2];
417     }
418 };
419
420 GAPI_GPU_KERNEL(GGPUSplit4, cv::gapi::core::GSplit4)
421 {
422     static void run(const cv::UMat& in, cv::UMat &m1, cv::UMat &m2, cv::UMat &m3, cv::UMat &m4)
423     {
424         std::vector<cv::UMat> outMats = {m1, m2, m3, m4};
425         cv::split(in, outMats);
426
427         // Write back FIXME: Write a helper or avoid this nonsence completely!
428         m1 = outMats[0];
429         m2 = outMats[1];
430         m3 = outMats[2];
431         m4 = outMats[3];
432     }
433 };
434
435 GAPI_GPU_KERNEL(GGPUMerge3, cv::gapi::core::GMerge3)
436 {
437     static void run(const cv::UMat& in1, const cv::UMat& in2, const cv::UMat& in3, cv::UMat &out)
438     {
439         std::vector<cv::UMat> inMats = {in1, in2, in3};
440         cv::merge(inMats, out);
441     }
442 };
443
444 GAPI_GPU_KERNEL(GGPUMerge4, cv::gapi::core::GMerge4)
445 {
446     static void run(const cv::UMat& in1, const cv::UMat& in2, const cv::UMat& in3, const cv::UMat& in4, cv::UMat &out)
447     {
448         std::vector<cv::UMat> inMats = {in1, in2, in3, in4};
449         cv::merge(inMats, out);
450     }
451 };
452
453 GAPI_GPU_KERNEL(GGPUResize, cv::gapi::core::GResize)
454 {
455     static void run(const cv::UMat& in, cv::Size sz, double fx, double fy, int interp, cv::UMat &out)
456     {
457         cv::resize(in, out, sz, fx, fy, interp);
458     }
459 };
460
461 GAPI_GPU_KERNEL(GGPURemap, cv::gapi::core::GRemap)
462 {
463     static void run(const cv::UMat& in, const cv::Mat& x, const cv::Mat& y, int a, int b, cv::Scalar s, cv::UMat& out)
464     {
465         cv::remap(in, out, x, y, a, b, s);
466     }
467 };
468
469 GAPI_GPU_KERNEL(GGPUFlip, cv::gapi::core::GFlip)
470 {
471     static void run(const cv::UMat& in, int code, cv::UMat& out)
472     {
473         cv::flip(in, out, code);
474     }
475 };
476
477 GAPI_GPU_KERNEL(GGPUCrop, cv::gapi::core::GCrop)
478 {
479     static void run(const cv::UMat& in, cv::Rect rect, cv::UMat& out)
480     {
481         cv::UMat(in, rect).copyTo(out);
482     }
483 };
484
485 GAPI_GPU_KERNEL(GGPUConcatHor, cv::gapi::core::GConcatHor)
486 {
487     static void run(const cv::UMat& in1, const cv::UMat& in2, cv::UMat& out)
488     {
489         cv::hconcat(in1, in2, out);
490     }
491 };
492
493 GAPI_GPU_KERNEL(GGPUConcatVert, cv::gapi::core::GConcatVert)
494 {
495     static void run(const cv::UMat& in1, const cv::UMat& in2, cv::UMat& out)
496     {
497         cv::vconcat(in1, in2, out);
498     }
499 };
500
501 GAPI_GPU_KERNEL(GGPULUT, cv::gapi::core::GLUT)
502 {
503     static void run(const cv::UMat& in, const cv::Mat& lut, cv::UMat& out)
504     {
505         cv::LUT(in, lut, out);
506     }
507 };
508
509 GAPI_GPU_KERNEL(GGPUConvertTo, cv::gapi::core::GConvertTo)
510 {
511     static void run(const cv::UMat& in, int rtype, double alpha, double beta, cv::UMat& out)
512     {
513         in.convertTo(out, rtype, alpha, beta);
514     }
515 };
516
517 cv::gapi::GKernelPackage cv::gapi::core::gpu::kernels()
518 {
519     static auto pkg = cv::gapi::kernels
520         <  GGPUAdd
521          , GGPUAddC
522          , GGPUSub
523          , GGPUSubC
524          , GGPUSubRC
525          , GGPUMul
526          , GGPUMulC
527          , GGPUMulCOld
528          , GGPUDiv
529          , GGPUDivC
530          , GGPUDivRC
531          , GGPUMean
532          , GGPUMask
533          , GGPUPolarToCart
534          , GGPUCartToPolar
535          , GGPUCmpGT
536          , GGPUCmpGE
537          , GGPUCmpLE
538          , GGPUCmpLT
539          , GGPUCmpEQ
540          , GGPUCmpNE
541          , GGPUCmpGTScalar
542          , GGPUCmpGEScalar
543          , GGPUCmpLEScalar
544          , GGPUCmpLTScalar
545          , GGPUCmpEQScalar
546          , GGPUCmpNEScalar
547          , GGPUAnd
548          , GGPUAndS
549          , GGPUOr
550          , GGPUOrS
551          , GGPUXor
552          , GGPUXorS
553          , GGPUNot
554          , GGPUSelect
555          //, GGPUMin
556          //, GGPUMax
557          , GGPUAbsDiff
558          , GGPUAbsDiffC
559          , GGPUSum
560          , GGPUAddW
561          , GGPUNormL1
562          , GGPUNormL2
563          , GGPUNormInf
564          , GGPUIntegral
565          , GGPUThreshold
566          , GGPUThresholdOT
567          , GGPUInRange
568          , GGPUSplit3
569          , GGPUSplit4
570          , GGPUResize
571          , GGPUMerge3
572          , GGPUMerge4
573          , GGPURemap
574          , GGPUFlip
575          , GGPUCrop
576          , GGPUConcatHor
577          , GGPUConcatVert
578          , GGPULUT
579          , GGPUConvertTo
580          >();
581     return pkg;
582 }