Debian Codegraph @

tests/benchmarks/gui/painting/drawtexture/tst_drawtexture.cpp

raw
1 // Copyright (C) 2017 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <qtest.h>
5 #include <QPainter>
6 #include <QImage>
7
8 Q_DECLARE_METATYPE(QImage::Format)
9
10 #define SIZE 400
11
12 class tst_DrawTexture : public QObject
13 {
14 Q_OBJECT
15
16 void paint(bool smooth);
17
18 private slots:
19 void simpleUpscale_data();
20 void simpleUpscale();
21 void simpleUpscaleSmooth_data();
22 void simpleUpscaleSmooth();
23
24 void downscale_data();
25 void downscale();
26 void downscaleSmooth_data();
27 void downscaleSmooth();
28
29 void upscale_data();
30 void upscale();
31 void upscaleSmooth_data();
32 void upscaleSmooth();
33
34 void rotate_data();
35 void rotate();
36 void rotateSmooth_data();
37 void rotateSmooth();
38
39 void perspective_data();
40 void perspective();
41 void perspectiveSmooth_data();
42 void perspectiveSmooth();
43 };
44
45 void tst_DrawTexture::simpleUpscale_data()
46 {
47 QTest::addColumn<QImage::Format>("sourceFormat");
48 QTest::addColumn<QImage::Format>("targetFormat");
49 QTest::addColumn<QTransform>("transform");
50
51
52 QTransform matrix;
53 matrix.scale(1.5, 1.5);
54 QTest::newRow("rgb32 1.5x,1.5x on rgb32") << QImage::Format_RGB32
55 << QImage::Format_RGB32
56 << matrix;
57 QTest::newRow("argb32pm 1.5x,1.5x on argb32pm") << QImage::Format_ARGB32_Premultiplied
58 << QImage::Format_ARGB32_Premultiplied
59 << matrix;
60 QTest::newRow("argb32 1.5x,1.5x on argb32pm") << QImage::Format_ARGB32
61 << QImage::Format_ARGB32_Premultiplied
62 << matrix;
63 QTest::newRow("rgba8888pm 1.5x,1.5x on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
64 << QImage::Format_RGBA8888_Premultiplied
65 << matrix;
66 QTest::newRow("rgb16 1.5x,1.5x on rgb16") << QImage::Format_RGB16
67 << QImage::Format_RGB16
68 << matrix;
69 QTest::newRow("rgb16 1.5x,1.5x on argb32pm") << QImage::Format_RGB16
70 << QImage::Format_ARGB32_Premultiplied
71 << matrix;
72 QTest::newRow("argb32pm 1.5x,1.5x on rgb30") << QImage::Format_ARGB32_Premultiplied
73 << QImage::Format_RGB30
74 << matrix;
75 matrix.reset();
76 matrix.scale(5, 5);
77 QTest::newRow("rgb32 5x,5x on rgb32") << QImage::Format_RGB32
78 << QImage::Format_RGB32
79 << matrix;
80 QTest::newRow("argb32pm 5x,5x on argb32pm") << QImage::Format_ARGB32_Premultiplied
81 << QImage::Format_ARGB32_Premultiplied
82 << matrix;
83 QTest::newRow("argb32 5x,5x on argb32pm") << QImage::Format_ARGB32
84 << QImage::Format_ARGB32_Premultiplied
85 << matrix;
86 QTest::newRow("rgba8888pm 5x,5x on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
87 << QImage::Format_RGBA8888_Premultiplied
88 << matrix;
89 QTest::newRow("rgb16 5x,5x on rgb16") << QImage::Format_RGB16
90 << QImage::Format_RGB16
91 << matrix;
92 QTest::newRow("rgb16 5x,5x on rgb32") << QImage::Format_RGB16
93 << QImage::Format_RGB32
94 << matrix;
95 QTest::newRow("argb32pm 5x,5x on rgb30") << QImage::Format_ARGB32_Premultiplied
96 << QImage::Format_RGB30
97 << matrix;
98 matrix.reset();
99 matrix.translate(0, SIZE);
100 matrix.scale(16, -1);
101 QTest::newRow("rgb32 16x,-1x on rgb32") << QImage::Format_RGB32
102 << QImage::Format_RGB32
103 << matrix;
104 QTest::newRow("argb32pm 16x,-1x on argb32pm") << QImage::Format_ARGB32_Premultiplied
105 << QImage::Format_ARGB32_Premultiplied
106 << matrix;
107 QTest::newRow("argb32 16x,-1x on argb32pm") << QImage::Format_ARGB32
108 << QImage::Format_ARGB32_Premultiplied
109 << matrix;
110 QTest::newRow("rgba8888pm 16x,-1x on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
111 << QImage::Format_RGBA8888_Premultiplied
112 << matrix;
113 QTest::newRow("rgb16 16x,-1x on rgb16") << QImage::Format_RGB16
114 << QImage::Format_RGB16
115 << matrix;
116 QTest::newRow("argb32pm 16x,-1x on rgb30") << QImage::Format_ARGB32_Premultiplied
117 << QImage::Format_RGB30
118 << matrix;
119 }
120
121 void tst_DrawTexture::downscale_data()
122 {
123 QTest::addColumn<QImage::Format>("sourceFormat");
124 QTest::addColumn<QImage::Format>("targetFormat");
125 QTest::addColumn<QTransform>("transform");
126
127
128 QTransform matrix;
129 matrix.translate(SIZE, 0);
130 matrix.scale(-1.5, 1.5);
131 QTest::newRow("rgb32 -1.5x,1.5x on rgb32") << QImage::Format_RGB32
132 << QImage::Format_RGB32
133 << matrix;
134 QTest::newRow("argb32pm -1.5x,1.5x on argb32pm") << QImage::Format_ARGB32_Premultiplied
135 << QImage::Format_ARGB32_Premultiplied
136 << matrix;
137 QTest::newRow("argb32 -1.5x,1.5x on argb32pm") << QImage::Format_ARGB32
138 << QImage::Format_ARGB32_Premultiplied
139 << matrix;
140 QTest::newRow("rgba8888pm -1.5x,1.5x on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
141 << QImage::Format_RGBA8888_Premultiplied
142 << matrix;
143 QTest::newRow("rgb16 -1.5x,1.5x on rgb16") << QImage::Format_RGB16
144 << QImage::Format_RGB16
145 << matrix;
146 QTest::newRow("argb32pm -1.5x,1.5x on rgb30") << QImage::Format_ARGB32_Premultiplied
147 << QImage::Format_RGB30
148 << matrix;
149 matrix.reset();
150 matrix.scale(.5, .5);
151 QTest::newRow("rgb32 .5x,.5x on rgb32") << QImage::Format_RGB32
152 << QImage::Format_RGB32
153 << matrix;
154 QTest::newRow("argb32pm .5x,.5x on argb32pm") << QImage::Format_ARGB32_Premultiplied
155 << QImage::Format_ARGB32_Premultiplied
156 << matrix;
157 QTest::newRow("argb32 .5x,.5x on argb32pm") << QImage::Format_ARGB32
158 << QImage::Format_ARGB32_Premultiplied
159 << matrix;
160 QTest::newRow("rgba8888pm .5x,.5x on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
161 << QImage::Format_RGBA8888_Premultiplied
162 << matrix;
163 QTest::newRow("rgb16 .5x,.5x on rgb16") << QImage::Format_RGB16
164 << QImage::Format_RGB16
165 << matrix;
166 QTest::newRow("argb32pm .5x,.5x on rgb30") << QImage::Format_ARGB32_Premultiplied
167 << QImage::Format_RGB30
168 << matrix;
169 matrix.reset();
170 matrix.scale(.2, 2);
171 QTest::newRow("rgb32 .2x,2x on rgb32") << QImage::Format_RGB32
172 << QImage::Format_RGB32
173 << matrix;
174 QTest::newRow("argb32pm .2x,2x on argb32pm") << QImage::Format_ARGB32_Premultiplied
175 << QImage::Format_ARGB32_Premultiplied
176 << matrix;
177 QTest::newRow("argb32 .2x,2x on argb32pm") << QImage::Format_ARGB32
178 << QImage::Format_ARGB32_Premultiplied
179 << matrix;
180 QTest::newRow("rgba8888pm .2x,2x on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
181 << QImage::Format_RGBA8888_Premultiplied
182 << matrix;
183 QTest::newRow("rgb16 .2x,2x on rgb16") << QImage::Format_RGB16
184 << QImage::Format_RGB16
185 << matrix;
186 QTest::newRow("argb32pm .2x,2x on rgb30") << QImage::Format_ARGB32_Premultiplied
187 << QImage::Format_RGB30
188 << matrix;
189 }
190
191 void tst_DrawTexture::upscale_data()
192 {
193 QTest::addColumn<QImage::Format>("sourceFormat");
194 QTest::addColumn<QImage::Format>("targetFormat");
195 QTest::addColumn<QTransform>("transform");
196
197
198 QTransform matrix;
199 matrix.translate(SIZE, 0);
200 matrix.scale(-8, 8);
201 QTest::newRow("rgb32 -8x,8x on rgb32") << QImage::Format_RGB32
202 << QImage::Format_RGB32
203 << matrix;
204 QTest::newRow("argb32pm -8x,8x on argb32pm") << QImage::Format_ARGB32_Premultiplied
205 << QImage::Format_ARGB32_Premultiplied
206 << matrix;
207 QTest::newRow("argb32 -8x,8x on argb32pm") << QImage::Format_ARGB32
208 << QImage::Format_ARGB32_Premultiplied
209 << matrix;
210 QTest::newRow("rgba8888pm -8x,8x on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
211 << QImage::Format_RGBA8888_Premultiplied
212 << matrix;
213 QTest::newRow("rgb16 -8x,8x on rgb16") << QImage::Format_RGB16
214 << QImage::Format_RGB16
215 << matrix;
216 QTest::newRow("argb32pm -8x,8x on rgb30") << QImage::Format_ARGB32_Premultiplied
217 << QImage::Format_RGB30
218 << matrix;
219 matrix.reset();
220 matrix.translate(SIZE, SIZE);
221 matrix.scale(-10, -10);
222 QTest::newRow("rgb32 -10x,-10x on rgb32") << QImage::Format_RGB32
223 << QImage::Format_RGB32
224 << matrix;
225 QTest::newRow("argb32pm -10x,-10x on argb32pm") << QImage::Format_ARGB32_Premultiplied
226 << QImage::Format_ARGB32_Premultiplied
227 << matrix;
228 QTest::newRow("argb32 -10x,-10x on argb32pm") << QImage::Format_ARGB32
229 << QImage::Format_ARGB32_Premultiplied
230 << matrix;
231 QTest::newRow("rgba8888pm -10x,-10x on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
232 << QImage::Format_RGBA8888_Premultiplied
233 << matrix;
234 QTest::newRow("rgb16 -10x,-10x on rgb16") << QImage::Format_RGB16
235 << QImage::Format_RGB16
236 << matrix;
237 QTest::newRow("argb32pm -10x,-10x on rgb30") << QImage::Format_ARGB32_Premultiplied
238 << QImage::Format_RGB30
239 << matrix;
240
241 matrix.reset();
242 matrix.translate(SIZE, 0);
243 matrix.scale(-1, 16);
244 QTest::newRow("rgb32 -1x,16x on rgb32") << QImage::Format_RGB32
245 << QImage::Format_RGB32
246 << matrix;
247 QTest::newRow("argb32pm -1x,16x on argb32pm") << QImage::Format_ARGB32_Premultiplied
248 << QImage::Format_ARGB32_Premultiplied
249 << matrix;
250 QTest::newRow("argb32 -1x,16x on argb32pm") << QImage::Format_ARGB32
251 << QImage::Format_ARGB32_Premultiplied
252 << matrix;
253 QTest::newRow("rgba8888pm -1x,16x on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
254 << QImage::Format_RGBA8888_Premultiplied
255 << matrix;
256 QTest::newRow("rgb16 -1x,16x on rgb16") << QImage::Format_RGB16
257 << QImage::Format_RGB16
258 << matrix;
259 QTest::newRow("argb32pm -1x,16x on rgb30") << QImage::Format_ARGB32_Premultiplied
260 << QImage::Format_RGB30
261 << matrix;
262 }
263
264 void tst_DrawTexture::rotate_data()
265 {
266 QTest::addColumn<QImage::Format>("sourceFormat");
267 QTest::addColumn<QImage::Format>("targetFormat");
268 QTest::addColumn<QTransform>("transform");
269
270 QTransform matrix;
271 matrix.translate(SIZE/2, SIZE/2);
272 matrix.rotate(-90);
273 matrix.translate(-SIZE/2, -SIZE/2);
274 QTest::newRow("rgb32 -90deg on rgb32") << QImage::Format_RGB32
275 << QImage::Format_RGB32
276 << matrix;
277 QTest::newRow("argb32pm -90deg on argb32pm") << QImage::Format_ARGB32_Premultiplied
278 << QImage::Format_ARGB32_Premultiplied
279 << matrix;
280 QTest::newRow("argb32 -90deg on argb32pm") << QImage::Format_ARGB32
281 << QImage::Format_ARGB32_Premultiplied
282 << matrix;
283 QTest::newRow("rgba8888pm -90deg on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
284 << QImage::Format_RGBA8888_Premultiplied
285 << matrix;
286 QTest::newRow("rgb16 -90deg rgb16") << QImage::Format_RGB16
287 << QImage::Format_RGB16
288 << matrix;
289 QTest::newRow("argb32pm -90deg on rgb30") << QImage::Format_ARGB32_Premultiplied
290 << QImage::Format_RGB30
291 << matrix;
292 matrix.reset();
293 matrix.translate(SIZE/2, SIZE/2);
294 matrix.rotate(45);
295 matrix.translate(-SIZE/2, -SIZE/2);
296 QTest::newRow("rgb32 45deg on rgb32") << QImage::Format_RGB32
297 << QImage::Format_RGB32
298 << matrix;
299 QTest::newRow("argb32pm 45deg on argb32pm") << QImage::Format_ARGB32_Premultiplied
300 << QImage::Format_ARGB32_Premultiplied
301 << matrix;
302 QTest::newRow("argb32 45deg on argb32pm") << QImage::Format_ARGB32
303 << QImage::Format_ARGB32_Premultiplied
304 << matrix;
305 QTest::newRow("rgba8888pm 45deg on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
306 << QImage::Format_RGBA8888_Premultiplied
307 << matrix;
308 QTest::newRow("rgb16 45deg on rgb16") << QImage::Format_RGB16
309 << QImage::Format_RGB16
310 << matrix;
311 QTest::newRow("argb32pm 45deg on rgb30") << QImage::Format_ARGB32_Premultiplied
312 << QImage::Format_RGB30
313 << matrix;
314
315 matrix.reset();
316 matrix.translate(SIZE/2, SIZE/2);
317 matrix.rotate(135);
318 matrix.scale(2, 2);
319 matrix.translate(-SIZE/4, -SIZE/4);
320 QTest::newRow("rgb32 rotate+scale on rgb32") << QImage::Format_RGB32
321 << QImage::Format_RGB32
322 << matrix;
323 QTest::newRow("argb32pm rotate+scale on argb32pm") << QImage::Format_ARGB32_Premultiplied
324 << QImage::Format_ARGB32_Premultiplied
325 << matrix;
326 QTest::newRow("argb32 rotate+scale on argb32pm") << QImage::Format_ARGB32
327 << QImage::Format_ARGB32_Premultiplied
328 << matrix;
329 QTest::newRow("rgba8888pm rotate+scale on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
330 << QImage::Format_RGBA8888_Premultiplied
331 << matrix;
332 QTest::newRow("rgb16 rotate+scale on rgb16") << QImage::Format_RGB16
333 << QImage::Format_RGB16
334 << matrix;
335 QTest::newRow("argb32pm rotate+scale on rgb30") << QImage::Format_ARGB32_Premultiplied
336 << QImage::Format_RGB30
337 << matrix;
338 }
339
340 void tst_DrawTexture::perspective_data()
341 {
342 QTest::addColumn<QImage::Format>("sourceFormat");
343 QTest::addColumn<QImage::Format>("targetFormat");
344 QTest::addColumn<QTransform>("transform");
345
346 QTransform matrix;
347 QPolygonF quad1, quad2;
348 quad1 << QPointF(0.0, 0.0) << QPointF(SIZE,0.0) << QPointF(SIZE,SIZE) << QPointF(0.0,SIZE);
349 quad2 << QPointF(SIZE/6, SIZE/6) << QPointF(SIZE*4/5,SIZE/5) << QPointF(SIZE*4/5,SIZE*5/6) << QPointF(SIZE/5,SIZE*3/5);
350 QTransform::quadToQuad(quad1, quad2, matrix);
351
352 QTest::newRow("rgb32 perspective1 on rgb32") << QImage::Format_RGB32
353 << QImage::Format_RGB32
354 << matrix;
355 QTest::newRow("argb32pm perspective1 on argb32pm") << QImage::Format_ARGB32_Premultiplied
356 << QImage::Format_ARGB32_Premultiplied
357 << matrix;
358 QTest::newRow("argb32 perspective1 on argb32pm") << QImage::Format_ARGB32
359 << QImage::Format_ARGB32_Premultiplied
360 << matrix;
361 QTest::newRow("rgba8888pm perspective1 on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
362 << QImage::Format_RGBA8888_Premultiplied
363 << matrix;
364 QTest::newRow("rgb16 perspective1 on rgb16") << QImage::Format_RGB16
365 << QImage::Format_RGB16
366 << matrix;
367 QTest::newRow("argb32pm perspective1 on rgb30") << QImage::Format_ARGB32_Premultiplied
368 << QImage::Format_RGB30
369 << matrix;
370
371 matrix.reset();
372 quad1.clear(); quad2.clear();
373 quad1 << QPointF(0.0, 0.0) << QPointF(SIZE,0.0) << QPointF(SIZE,SIZE) << QPointF(0.0,SIZE);
374 quad2 << QPointF(0.0, 0.0) << QPointF(SIZE*4/5,SIZE/4) << QPointF(SIZE*4/5,SIZE*3/4) << QPointF(0.0,SIZE);
375 QTransform::quadToQuad(quad1, quad2, matrix);
376
377 QTest::newRow("rgb32 perspective2 on rgb32") << QImage::Format_RGB32
378 << QImage::Format_RGB32
379 << matrix;
380 QTest::newRow("argb32pm perspective2 on argb32pm") << QImage::Format_ARGB32_Premultiplied
381 << QImage::Format_ARGB32_Premultiplied
382 << matrix;
383 QTest::newRow("argb32 perspective2 on argb32pm") << QImage::Format_ARGB32
384 << QImage::Format_ARGB32_Premultiplied
385 << matrix;
386 QTest::newRow("rgba8888pm perspective2 on rgba8888pm") << QImage::Format_RGBA8888_Premultiplied
387 << QImage::Format_RGBA8888_Premultiplied
388 << matrix;
389 QTest::newRow("rgb16 perspective2 on rgb16") << QImage::Format_RGB16
390 << QImage::Format_RGB16
391 << matrix;
392 QTest::newRow("argb32pm perspective2 on rgb30") << QImage::Format_ARGB32_Premultiplied
393 << QImage::Format_RGB30
394 << matrix;
395 }
396
397 void tst_DrawTexture::simpleUpscaleSmooth_data()
398 {
399 simpleUpscale_data();
400 }
401
402 void tst_DrawTexture::downscaleSmooth_data()
403 {
404 downscale_data();
405 }
406
407 void tst_DrawTexture::upscaleSmooth_data()
408 {
409 upscale_data();
410 }
411
412 void tst_DrawTexture::rotateSmooth_data()
413 {
414 rotate_data();
415 }
416
417 void tst_DrawTexture::perspectiveSmooth_data()
418 {
419 perspective_data();
420 }
421
422 static QImage createImage(const QSize &size, QImage::Format format, bool smooth)
423 {
424 QImage base(size, format);
425 base.fill(Qt::transparent);
426 QLinearGradient grad(0.0, 0.0, 1.0, 0.0);
427 grad.setCoordinateMode(QGradient::ObjectBoundingMode);
428 grad.setColorAt(0.0, Qt::red);
429 grad.setColorAt(0.4, Qt::white);
430 grad.setColorAt(0.6, Qt::white);
431 grad.setColorAt(1.0, Qt::blue);
432 QBrush brush(grad);
433 QPainter p(&base);
434 p.setRenderHint(QPainter::Antialiasing, smooth);
435 p.setBrush(brush);
436 p.setPen(Qt::NoPen);
437 p.drawEllipse(0, 0, size.width(), size.height());
438 p.end();
439 return base;
440 }
441
442
443 void tst_DrawTexture::paint(bool smooth)
444 {
445 QFETCH(QImage::Format, sourceFormat);
446 QFETCH(QImage::Format, targetFormat);
447 QFETCH(QTransform, transform);
448
449 QSize size(SIZE, SIZE);
450 QRect rect(QPoint(0,0), size);
451 if (transform.isAffine())
452 rect = transform.inverted().mapRect(rect);
453
454 QImage sourceImage = createImage(rect.size(), sourceFormat, smooth);
455 QImage targetImage(size, targetFormat);
456 targetImage.fill(Qt::gray);
457
458 QPainter p(&targetImage);
459 p.setRenderHints(QPainter::SmoothPixmapTransform, smooth);
460 p.setRenderHints(QPainter::Antialiasing, smooth);
461 p.setWorldTransform(transform, false);
462
463 QBENCHMARK {
464 p.drawImage(0, 0, sourceImage);
465 }
466 // targetImage.save(QString::fromLatin1(QTest::currentTestFunction()) + QChar('_') + QString::fromLatin1(QTest::currentDataTag()) + QStringLiteral(".png"));
467 }
468
469 void tst_DrawTexture::simpleUpscale()
470 {
471 paint(false);
472 }
473
474 void tst_DrawTexture::upscale()
475 {
476 paint(false);
477 }
478
479 void tst_DrawTexture::downscale()
480 {
481 paint(false);
482 }
483
484 void tst_DrawTexture::rotate()
485 {
486 paint(false);
487 }
488
489 void tst_DrawTexture::perspective()
490 {
491 paint(false);
492 }
493
494 void tst_DrawTexture::simpleUpscaleSmooth()
495 {
496 paint(true);
497 }
498
499 void tst_DrawTexture::upscaleSmooth()
500 {
501 paint(true);
502 }
503
504 void tst_DrawTexture::downscaleSmooth()
505 {
506 paint(true);
507 }
508
509 void tst_DrawTexture::rotateSmooth()
510 {
511 paint(true);
512 }
513
514 void tst_DrawTexture::perspectiveSmooth()
515 {
516 paint(true);
517 }
518
519 QTEST_MAIN(tst_DrawTexture)
520
521 #include "tst_drawtexture.moc"