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