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 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #include "test_precomp.hpp"
44 
45 #if defined(HAVE_CUDA) && defined(HAVE_OPENGL)
46 
47 #include "opencv2/core/cuda.hpp"
48 #include "opencv2/core/opengl.hpp"
49 #include "opencv2/ts/cuda_test.hpp"
50 
51 namespace opencv_test { namespace {
52 
53 /////////////////////////////////////////////
54 // Buffer
55 
PARAM_TEST_CASE(Buffer,cv::Size,MatType)56 PARAM_TEST_CASE(Buffer, cv::Size, MatType)
57 {
58     static void SetUpTestCase()
59     {
60         cv::namedWindow("test", cv::WINDOW_OPENGL);
61     }
62 
63     static void TearDownTestCase()
64     {
65         cv::destroyAllWindows();
66     }
67 
68     cv::Size size;
69     int type;
70 
71     virtual void SetUp()
72     {
73         size = GET_PARAM(0);
74         type = GET_PARAM(1);
75     }
76 };
77 
CUDA_TEST_P(Buffer,Constructor1)78 CUDA_TEST_P(Buffer, Constructor1)
79 {
80     cv::ogl::Buffer buf(size.height, size.width, type, cv::ogl::Buffer::ARRAY_BUFFER, true);
81 
82     EXPECT_EQ(size.height, buf.rows());
83     EXPECT_EQ(size.width, buf.cols());
84     EXPECT_EQ(type, buf.type());
85 }
86 
CUDA_TEST_P(Buffer,Constructor2)87 CUDA_TEST_P(Buffer, Constructor2)
88 {
89     cv::ogl::Buffer buf(size, type, cv::ogl::Buffer::ARRAY_BUFFER, true);
90 
91     EXPECT_EQ(size.height, buf.rows());
92     EXPECT_EQ(size.width, buf.cols());
93     EXPECT_EQ(type, buf.type());
94 }
95 
CUDA_TEST_P(Buffer,ConstructorFromMat)96 CUDA_TEST_P(Buffer, ConstructorFromMat)
97 {
98     cv::Mat gold = randomMat(size, type);
99 
100     cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
101 
102     cv::Mat bufData;
103     buf.copyTo(bufData);
104 
105     EXPECT_MAT_NEAR(gold, bufData, 0);
106 }
107 
CUDA_TEST_P(Buffer,ConstructorFromGpuMat)108 CUDA_TEST_P(Buffer, ConstructorFromGpuMat)
109 {
110     cv::Mat gold = randomMat(size, type);
111     cv::cuda::GpuMat d_gold(gold);
112 
113     cv::ogl::Buffer buf(d_gold, cv::ogl::Buffer::ARRAY_BUFFER);
114 
115     cv::Mat bufData;
116     buf.copyTo(bufData);
117 
118     EXPECT_MAT_NEAR(gold, bufData, 0);
119 }
120 
CUDA_TEST_P(Buffer,ConstructorFromBuffer)121 CUDA_TEST_P(Buffer, ConstructorFromBuffer)
122 {
123     cv::ogl::Buffer buf_gold(size, type, cv::ogl::Buffer::ARRAY_BUFFER, true);
124 
125     cv::ogl::Buffer buf(buf_gold);
126 
127     EXPECT_EQ(buf_gold.bufId(), buf.bufId());
128     EXPECT_EQ(buf_gold.rows(), buf.rows());
129     EXPECT_EQ(buf_gold.cols(), buf.cols());
130     EXPECT_EQ(buf_gold.type(), buf.type());
131 }
132 
CUDA_TEST_P(Buffer,Create)133 CUDA_TEST_P(Buffer, Create)
134 {
135     cv::ogl::Buffer buf;
136     buf.create(size.height, size.width, type, cv::ogl::Buffer::ARRAY_BUFFER, true);
137 
138     EXPECT_EQ(size.height, buf.rows());
139     EXPECT_EQ(size.width, buf.cols());
140     EXPECT_EQ(type, buf.type());
141 }
142 
CUDA_TEST_P(Buffer,CopyFromMat)143 CUDA_TEST_P(Buffer, CopyFromMat)
144 {
145     cv::Mat gold = randomMat(size, type);
146 
147     cv::ogl::Buffer buf;
148     buf.copyFrom(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
149 
150     cv::Mat bufData;
151     buf.copyTo(bufData);
152 
153     EXPECT_MAT_NEAR(gold, bufData, 0);
154 }
155 
CUDA_TEST_P(Buffer,CopyFromGpuMat)156 CUDA_TEST_P(Buffer, CopyFromGpuMat)
157 {
158     cv::Mat gold = randomMat(size, type);
159     cv::cuda::GpuMat d_gold(gold);
160 
161     cv::ogl::Buffer buf;
162     buf.copyFrom(d_gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
163 
164     cv::Mat bufData;
165     buf.copyTo(bufData);
166 
167     EXPECT_MAT_NEAR(gold, bufData, 0);
168 }
169 
CUDA_TEST_P(Buffer,CopyFromBuffer)170 CUDA_TEST_P(Buffer, CopyFromBuffer)
171 {
172     cv::Mat gold = randomMat(size, type);
173     cv::ogl::Buffer buf_gold(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
174 
175     cv::ogl::Buffer buf;
176     buf.copyFrom(buf_gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
177 
178     EXPECT_NE(buf_gold.bufId(), buf.bufId());
179 
180     cv::Mat bufData;
181     buf.copyTo(bufData);
182 
183     EXPECT_MAT_NEAR(gold, bufData, 0);
184 }
185 
CUDA_TEST_P(Buffer,CopyToGpuMat)186 CUDA_TEST_P(Buffer, CopyToGpuMat)
187 {
188     cv::Mat gold = randomMat(size, type);
189 
190     cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
191 
192     cv::cuda::GpuMat dst;
193     buf.copyTo(dst);
194 
195     EXPECT_MAT_NEAR(gold, dst, 0);
196 }
197 
CUDA_TEST_P(Buffer,CopyToBuffer)198 CUDA_TEST_P(Buffer, CopyToBuffer)
199 {
200     cv::Mat gold = randomMat(size, type);
201 
202     cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
203 
204     cv::ogl::Buffer dst;
205     buf.copyTo(dst);
206     dst.setAutoRelease(true);
207 
208     EXPECT_NE(buf.bufId(), dst.bufId());
209 
210     cv::Mat bufData;
211     dst.copyTo(bufData);
212 
213     EXPECT_MAT_NEAR(gold, bufData, 0);
214 }
215 
CUDA_TEST_P(Buffer,Clone)216 CUDA_TEST_P(Buffer, Clone)
217 {
218     cv::Mat gold = randomMat(size, type);
219 
220     cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
221 
222     cv::ogl::Buffer dst = buf.clone(cv::ogl::Buffer::ARRAY_BUFFER, true);
223 
224     EXPECT_NE(buf.bufId(), dst.bufId());
225 
226     cv::Mat bufData;
227     dst.copyTo(bufData);
228 
229     EXPECT_MAT_NEAR(gold, bufData, 0);
230 }
231 
CUDA_TEST_P(Buffer,MapHostRead)232 CUDA_TEST_P(Buffer, MapHostRead)
233 {
234     cv::Mat gold = randomMat(size, type);
235 
236     cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
237 
238     cv::Mat dst = buf.mapHost(cv::ogl::Buffer::READ_ONLY);
239 
240     EXPECT_MAT_NEAR(gold, dst, 0);
241 
242     buf.unmapHost();
243 }
244 
CUDA_TEST_P(Buffer,MapHostWrite)245 CUDA_TEST_P(Buffer, MapHostWrite)
246 {
247     cv::Mat gold = randomMat(size, type);
248 
249     cv::ogl::Buffer buf(size, type, cv::ogl::Buffer::ARRAY_BUFFER, true);
250 
251     cv::Mat dst = buf.mapHost(cv::ogl::Buffer::WRITE_ONLY);
252     gold.copyTo(dst);
253     buf.unmapHost();
254     dst.release();
255 
256     cv::Mat bufData;
257     buf.copyTo(bufData);
258 
259     EXPECT_MAT_NEAR(gold, bufData, 0);
260 }
261 
CUDA_TEST_P(Buffer,MapDevice)262 CUDA_TEST_P(Buffer, MapDevice)
263 {
264     cv::Mat gold = randomMat(size, type);
265 
266     cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
267 
268     cv::cuda::GpuMat dst = buf.mapDevice();
269 
270     EXPECT_MAT_NEAR(gold, dst, 0);
271 
272     buf.unmapDevice();
273 }
274 
275 INSTANTIATE_TEST_CASE_P(OpenGL, Buffer, testing::Combine(DIFFERENT_SIZES, ALL_TYPES));
276 
277 /////////////////////////////////////////////
278 // Texture2D
279 
PARAM_TEST_CASE(Texture2D,cv::Size,MatType)280 PARAM_TEST_CASE(Texture2D, cv::Size, MatType)
281 {
282     static void SetUpTestCase()
283     {
284         cv::namedWindow("test", cv::WINDOW_OPENGL);
285     }
286 
287     static void TearDownTestCase()
288     {
289         cv::destroyAllWindows();
290     }
291 
292     cv::Size size;
293     int type;
294     int depth;
295     int cn;
296     cv::ogl::Texture2D::Format format;
297 
298     virtual void SetUp()
299     {
300         size = GET_PARAM(0);
301         type = GET_PARAM(1);
302 
303         depth = CV_MAT_DEPTH(type);
304         cn = CV_MAT_CN(type);
305         format = cn == 1 ? cv::ogl::Texture2D::DEPTH_COMPONENT : cn == 3 ? cv::ogl::Texture2D::RGB : cn == 4 ? cv::ogl::Texture2D::RGBA : cv::ogl::Texture2D::NONE;
306     }
307 };
308 
CUDA_TEST_P(Texture2D,Constructor1)309 CUDA_TEST_P(Texture2D, Constructor1)
310 {
311     cv::ogl::Texture2D tex(size.height, size.width, format, true);
312 
313     EXPECT_EQ(size.height, tex.rows());
314     EXPECT_EQ(size.width, tex.cols());
315     EXPECT_EQ(format, tex.format());
316 }
317 
CUDA_TEST_P(Texture2D,Constructor2)318 CUDA_TEST_P(Texture2D, Constructor2)
319 {
320     cv::ogl::Texture2D tex(size, format, true);
321 
322     EXPECT_EQ(size.height, tex.rows());
323     EXPECT_EQ(size.width, tex.cols());
324     EXPECT_EQ(format, tex.format());
325 }
326 
CUDA_TEST_P(Texture2D,ConstructorFromMat)327 CUDA_TEST_P(Texture2D, ConstructorFromMat)
328 {
329     cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
330 
331     cv::ogl::Texture2D tex(gold, true);
332 
333     cv::Mat texData;
334     tex.copyTo(texData, depth);
335 
336     EXPECT_MAT_NEAR(gold, texData, 1e-2);
337 }
338 
CUDA_TEST_P(Texture2D,ConstructorFromGpuMat)339 CUDA_TEST_P(Texture2D, ConstructorFromGpuMat)
340 {
341     cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
342     cv::cuda::GpuMat d_gold(gold);
343 
344     cv::ogl::Texture2D tex(d_gold, true);
345 
346     cv::Mat texData;
347     tex.copyTo(texData, depth);
348 
349     EXPECT_MAT_NEAR(gold, texData, 1e-2);
350 }
351 
CUDA_TEST_P(Texture2D,ConstructorFromBuffer)352 CUDA_TEST_P(Texture2D, ConstructorFromBuffer)
353 {
354     cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
355     cv::ogl::Buffer buf_gold(gold, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true);
356 
357     cv::ogl::Texture2D tex(buf_gold, true);
358 
359     cv::Mat texData;
360     tex.copyTo(texData, depth);
361 
362     EXPECT_MAT_NEAR(gold, texData, 1e-2);
363 }
364 
CUDA_TEST_P(Texture2D,ConstructorFromTexture2D)365 CUDA_TEST_P(Texture2D, ConstructorFromTexture2D)
366 {
367     cv::ogl::Texture2D tex_gold(size, format, true);
368     cv::ogl::Texture2D tex(tex_gold);
369 
370     EXPECT_EQ(tex_gold.texId(), tex.texId());
371     EXPECT_EQ(tex_gold.rows(), tex.rows());
372     EXPECT_EQ(tex_gold.cols(), tex.cols());
373     EXPECT_EQ(tex_gold.format(), tex.format());
374 }
375 
CUDA_TEST_P(Texture2D,Create)376 CUDA_TEST_P(Texture2D, Create)
377 {
378     cv::ogl::Texture2D tex;
379     tex.create(size.height, size.width, format, true);
380 
381     EXPECT_EQ(size.height, tex.rows());
382     EXPECT_EQ(size.width, tex.cols());
383     EXPECT_EQ(format, tex.format());
384 }
385 
CUDA_TEST_P(Texture2D,CopyFromMat)386 CUDA_TEST_P(Texture2D, CopyFromMat)
387 {
388     cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
389 
390     cv::ogl::Texture2D tex;
391     tex.copyFrom(gold, true);
392 
393     cv::Mat texData;
394     tex.copyTo(texData, depth);
395 
396     EXPECT_MAT_NEAR(gold, texData, 1e-2);
397 }
398 
CUDA_TEST_P(Texture2D,CopyFromGpuMat)399 CUDA_TEST_P(Texture2D, CopyFromGpuMat)
400 {
401     cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
402     cv::cuda::GpuMat d_gold(gold);
403 
404     cv::ogl::Texture2D tex;
405     tex.copyFrom(d_gold, true);
406 
407     cv::Mat texData;
408     tex.copyTo(texData, depth);
409 
410     EXPECT_MAT_NEAR(gold, texData, 1e-2);
411 }
412 
CUDA_TEST_P(Texture2D,CopyFromBuffer)413 CUDA_TEST_P(Texture2D, CopyFromBuffer)
414 {
415     cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
416     cv::ogl::Buffer buf_gold(gold, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true);
417 
418     cv::ogl::Texture2D tex;
419     tex.copyFrom(buf_gold, true);
420 
421     cv::Mat texData;
422     tex.copyTo(texData, depth);
423 
424     EXPECT_MAT_NEAR(gold, texData, 1e-2);
425 }
426 
CUDA_TEST_P(Texture2D,CopyToGpuMat)427 CUDA_TEST_P(Texture2D, CopyToGpuMat)
428 {
429     cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
430 
431     cv::ogl::Texture2D tex(gold, true);
432 
433     cv::cuda::GpuMat dst;
434     tex.copyTo(dst, depth);
435 
436     EXPECT_MAT_NEAR(gold, dst, 1e-2);
437 }
438 
CUDA_TEST_P(Texture2D,CopyToBuffer)439 CUDA_TEST_P(Texture2D, CopyToBuffer)
440 {
441     cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
442 
443     cv::ogl::Texture2D tex(gold, true);
444 
445     cv::ogl::Buffer dst;
446     tex.copyTo(dst, depth, true);
447 
448     cv::Mat bufData;
449     dst.copyTo(bufData);
450 
451     EXPECT_MAT_NEAR(gold, bufData, 1e-2);
452 }
453 
454 INSTANTIATE_TEST_CASE_P(OpenGL, Texture2D, testing::Combine(DIFFERENT_SIZES, testing::Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4)));
455 
456 }} // namespace
457 #endif
458