Debian Codegraph @

tests/benchmarks/gui/painting/qpainter/tst_qpainter.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include <qtest.h>
30 #include <QPainter>
31 #include <QPainterPath>
32 #include <QPixmap>
33 #include <QImage>
34 #include <QPaintEngine>
35 #include <QTileRules>
36 #include <qmath.h>
37
38 #include <private/qpixmap_raster_p.h>
39
40 Q_DECLARE_METATYPE(QPainterPath)
41 Q_DECLARE_METATYPE(QPainter::RenderHint)
42 Q_DECLARE_METATYPE(QPainter::CompositionMode)
43 Q_DECLARE_METATYPE(QImage::Format)
44
45 enum PrimitiveType {
46 Primitive_Int_DiagLine,
47 Primitive_Int_VerLine,
48 Primitive_Int_HorLine,
49 Primitive_Int_Rect,
50 Primitive_Int_Ellipse,
51 Primitive_Int_Pie,
52 Primitive_Int_Arc,
53 Primitive_Int_Chord,
54 Primitive_Int_TriPoly,
55 Primitive_Int_RectPoly,
56 Primitive_Int_2RectPoly,
57
58 Primitive_Float_DiagLine,
59 Primitive_Float_VerLine,
60 Primitive_Float_HorLine,
61 Primitive_Float_Rect,
62 Primitive_Float_Ellipse,
63 Primitive_Float_Pie,
64 Primitive_Float_Arc,
65 Primitive_Float_Chord,
66 Primitive_Float_TriPoly,
67 Primitive_Float_RectPoly,
68 Primitive_Float_2RectPoly,
69
70 Primitive_Float_TriPath,
71 Primitive_Float_RectPath,
72 Primitive_Float_2RectPath,
73 Primitive_Float_EllipsePath,
74 Primitive_Last_Primitive
75
76 };
77
78
79 enum StateChanges {
80 ChangePen = 0x0001,
81 ChangeBrush = 0x0002,
82 ChangeClip = 0x0004,
83 ChangeTransform = 0x0008
84 };
85
86
87 struct PrimitiveSet {
88 QRect i_rect;
89 QLine i_line_diag;
90 QLine i_line_ver;
91 QLine i_line_hor;
92 QPolygon i_poly_tri;
93 QPolygon i_poly_2rects;
94 QPolygon i_poly_rect;
95
96 QRectF f_rect;
97 QLineF f_line_diag;
98 QLineF f_line_ver;
99 QLineF f_line_hor;
100 QPolygonF f_poly_tri;
101 QPolygonF f_poly_2rects;
102 QPolygonF f_poly_rect;
103
104 QPainterPath f_path_tri;
105 QPainterPath f_path_2rects;
106 QPainterPath f_path_rect;
107 QPainterPath f_path_ellipse;
108 };
109
110
111 QPixmap rasterPixmap(int width, int height)
112 {
113 QPlatformPixmap *data =
114 new QRasterPlatformPixmap(QPlatformPixmap::PixmapType);
115
116 data->resize(width, height);
117
118 return QPixmap(data);
119 }
120
121 QPixmap rasterPixmap(const QSize &size)
122 {
123 return rasterPixmap(size.width(), size.height());
124 }
125
126 QPixmap rasterPixmap(const QImage &image)
127 {
128 QPlatformPixmap *data =
129 new QRasterPlatformPixmap(QPlatformPixmap::PixmapType);
130
131 data->fromImage(image, Qt::AutoColor | Qt::NoFormatConversion);
132
133 return QPixmap(data);
134 }
135
136
137 class tst_QPainter : public QObject
138 {
139 Q_OBJECT
140
141 public:
142 tst_QPainter()
143 {
144 setupBrushes();
145 createPrimitives();
146 m_surface = surface();
147 }
148
149 private slots:
150 void beginAndEnd();
151
152 void saveRestore_data();
153 void saveRestore();
154
155 void drawLine_data();
156 void drawLine();
157 void drawLine_clipped_data();
158 void drawLine_clipped();
159 void drawLine_antialiased_clipped_data();
160 void drawLine_antialiased_clipped();
161
162 void drawPixmap_data();
163 void drawPixmap();
164
165 void drawImage_data();
166 void drawImage();
167
168 void drawTiledPixmap_data();
169 void drawTiledPixmap();
170
171 void compositionModes_data();
172 void compositionModes();
173
174 void fillPrimitives_10_data() { drawPrimitives_data_helper(false); }
175 void fillPrimitives_100_data() { drawPrimitives_data_helper(false); }
176 void fillPrimitives_1000_data() { drawPrimitives_data_helper(false); }
177 void fillPrimitives_10();
178 void fillPrimitives_100();
179 void fillPrimitives_1000();
180
181 void strokePrimitives_10_data() { drawPrimitives_data_helper(true); }
182 void strokePrimitives_100_data() { drawPrimitives_data_helper(true); }
183 void strokePrimitives_1000_data() { drawPrimitives_data_helper(true); }
184 void strokePrimitives_10();
185 void strokePrimitives_100();
186 void strokePrimitives_1000();
187
188 void drawText_data();
189 void drawText();
190
191 void clipAndFill_data();
192 void clipAndFill();
193
194 void drawRoundedRect();
195 void drawScaledRoundedRect();
196 void drawTransformedRoundedRect();
197
198 void drawScaledAntialiasedRoundedRect_data();
199 void drawTransformedAntialiasedRoundedRect_data();
200 void drawAntialiasedRoundedRect();
201 void drawScaledAntialiasedRoundedRect();
202 void drawTransformedAntialiasedRoundedRect();
203
204 void drawScaledImageRoundedRect_data();
205 void drawTransformedImageRoundedRect_data();
206 void drawImageRoundedRect();
207 void drawScaledImageRoundedRect();
208 void drawTransformedImageRoundedRect();
209
210 void drawScaledBorderPixmapRoundedRect_data();
211 void drawTransformedBorderPixmapRoundedRect_data();
212 void drawBorderPixmapRoundedRect();
213 void drawScaledBorderPixmapRoundedRect();
214 void drawTransformedBorderPixmapRoundedRect();
215
216 void drawTransformedTransparentImage_data();
217 void drawTransformedSemiTransparentImage_data();
218 void drawTransformedFilledImage_data();
219 void drawTransformedTransparentImage();
220 void drawTransformedSemiTransparentImage();
221 void drawTransformedFilledImage();
222
223 private:
224 void setupBrushes();
225 void createPrimitives();
226
227 void drawPrimitives_data_helper(bool fancypens);
228 void drawPixmapImage_data_helper(bool);
229 void fillPrimitives_helper(QPainter *painter, PrimitiveType type, PrimitiveSet *s);
230
231 QTransform transformForAngle(qreal angle);
232
233 QPaintDevice *surface()
234 {
235 m_pixmap = rasterPixmap(1024, 1024);
236 return &m_pixmap;
237 }
238
239
240 QMap<QString, QPen> m_pens;
241 QMap<QString, QBrush> m_brushes;
242
243 PrimitiveSet m_primitives_10;
244 PrimitiveSet m_primitives_100;
245 PrimitiveSet m_primitives_1000;
246
247 QPixmap m_pixmap;
248 QPaintDevice *m_surface;
249 QPainter m_painter;
250
251 };
252
253 void tst_QPainter::createPrimitives()
254 {
255 for (int i=0; i<3; ++i) {
256 PrimitiveSet *ps;
257 int size;
258 switch (i) {
259 case 0:
260 ps = &m_primitives_10;
261 size = 10;
262 break;
263 case 1:
264 ps = &m_primitives_100;
265 size = 100;
266 break;
267 case 2:
268 ps = &m_primitives_1000;
269 size = 1000;
270 break;
271 }
272
273 ps->f_rect = QRectF(0, 0, size, size);
274 ps->f_line_diag = QLineF(0, 0, size, size);
275 ps->f_line_ver = QLineF(10, 0, 10, size);
276 ps->f_line_hor = QLineF(0, 10, size, 10);
277 ps->f_poly_rect = QPolygonF() << QPointF(0, 0)
278 << QPointF(size, 0)
279 << QPointF(size, size)
280 << QPointF(0, size);
281 ps->f_poly_2rects = QPolygonF() << QPointF(0, 0)
282 << QPointF(size * 0.75, 0)
283 << QPointF(size * 0.75, size * 0.75)
284 << QPointF(size * 0.25, size * 0.75)
285 << QPointF(size * 0.25, size * 0.25)
286 << QPointF(size, size * 0.25)
287 << QPointF(size, size)
288 << QPointF(0, size);
289 ps->f_poly_tri = QPolygonF() << QPointF(size / 2.0, 0)
290 << QPointF(0, size)
291 << QPointF(size, size);
292
293 ps->f_path_tri.addPolygon(ps->f_poly_tri);
294 ps->f_path_rect.addRect(ps->f_rect);
295 ps->f_path_2rects.addPolygon(ps->f_poly_2rects);
296 ps->f_path_ellipse.addEllipse(ps->f_rect);
297
298 ps->i_rect = ps->f_rect.toRect();
299 ps->i_line_diag = ps->f_line_diag.toLine();
300 ps->i_line_hor = ps->f_line_hor.toLine();
301 ps->i_line_ver = ps->f_line_ver.toLine();
302 ps->i_poly_tri = ps->f_poly_tri.toPolygon();
303 ps->i_poly_rect = ps->f_poly_rect.toPolygon();
304 ps->i_poly_2rects = ps->f_poly_2rects.toPolygon();
305 }
306 }
307
308 void tst_QPainter::drawLine_data()
309 {
310 QTest::addColumn<QLine>("line");
311 QTest::addColumn<QPen>("pen");
312
313 QVector<QPen> pens;
314 pens << QPen(Qt::black)
315 << QPen(Qt::black, 0, Qt::DashDotLine)
316 << QPen(Qt::black, 4)
317 << QPen(Qt::black, 4, Qt::DashDotLine)
318 << QPen(QColor(255, 0, 0, 200))
319 << QPen(QColor(255, 0, 0, 200), 0, Qt::DashDotLine)
320 << QPen(QColor(255, 0, 0, 200), 4)
321 << QPen(QColor(255, 0, 0, 200), 4, Qt::DashDotLine);
322
323 QStringList penNames;
324 penNames << "black-0"
325 << "black-0-dashdot"
326 << "black-4"
327 << "black-4-dashdot"
328 << "alpha-0"
329 << "alpha-0-dashdot"
330 << "alpha-4"
331 << "alpha-4-dashdot";
332
333 int i = 0;
334 foreach (QPen pen, pens) {
335 const QString s = QString(QLatin1String("%1:%2")).arg(penNames[i]);
336 QTest::newRow(qPrintable(s.arg("horizontal")))
337 << QLine(0, 20, 100, 20) << pen;
338 QTest::newRow(qPrintable(s.arg("vertical:")))
339 << QLine(20, 0, 20, 100) << pen;
340 QTest::newRow(qPrintable(s.arg("0-45:")))
341 << QLine(0, 20, 100, 0) << pen;
342 QTest::newRow(qPrintable(s.arg("45-90:")))
343 << QLine(0, 100, 20, 0) << pen;
344 QTest::newRow(qPrintable(s.arg("90-135:")))
345 << QLine(20, 100, 0, 0) << pen;
346 QTest::newRow(qPrintable(s.arg("135-180:")))
347 << QLine(100, 20, 0, 0) << pen;
348 QTest::newRow(qPrintable(s.arg("180-225:")))
349 << QLine(100, 0, 0, 20) << pen;
350 QTest::newRow(qPrintable(s.arg("225-270:")))
351 << QLine(20, 0, 0, 100) << pen;
352 QTest::newRow(qPrintable(s.arg("270-315:")))
353 << QLine(0, 0, 20, 100) << pen;
354 QTest::newRow(qPrintable(s.arg("315-360:")))
355 << QLine(0, 0, 100, 20) << pen;
356 ++i;
357 }
358 }
359
360 void tst_QPainter::setupBrushes()
361 {
362 // Solid brushes...
363 m_brushes["black-brush"] = QBrush(Qt::black);
364 m_brushes["white-brush"] = QBrush(Qt::white);
365 m_brushes["transparent-brush"] = QBrush(QColor(255, 255, 255, 0));
366 m_brushes["alpha1-brush"] = QBrush(QColor(255, 255, 255, 100));
367 m_brushes["alpha2-brush"] = QBrush(QColor(255, 255, 255, 200));
368
369
370 // Patterns
371 m_brushes["dense1-brush"] = QBrush(Qt::Dense1Pattern);
372 m_brushes["dense2-brush"] = QBrush(Qt::Dense2Pattern);
373 m_brushes["dense3-brush"] = QBrush(Qt::Dense3Pattern);
374 m_brushes["dense4-brush"] = QBrush(Qt::Dense4Pattern);
375 m_brushes["dense5-brush"] = QBrush(Qt::Dense5Pattern);
376 m_brushes["dense6-brush"] = QBrush(Qt::Dense6Pattern);
377 m_brushes["dense7-brush"] = QBrush(Qt::Dense7Pattern);
378 m_brushes["hor-brush"] = QBrush(Qt::HorPattern);
379 m_brushes["ver-brush"] = QBrush(Qt::VerPattern);
380 m_brushes["cross-brush"] = QBrush(Qt::CrossPattern);
381 m_brushes["bdiag-brush"] = QBrush(Qt::BDiagPattern);
382 m_brushes["fdiag-brush"] = QBrush(Qt::FDiagPattern);
383 m_brushes["diagcross-brush"] = QBrush(Qt::DiagCrossPattern);
384
385 // Gradients
386 QGradientStops gradient_white_black;
387 gradient_white_black << QPair<qreal, QColor>(0, QColor(Qt::white));
388 gradient_white_black << QPair<qreal, QColor>(1, QColor(Qt::black));
389
390 QGradientStops gradient_white_black10;
391 for (int i=0; i<10; ++i) {
392 gradient_white_black10 << QPair<qreal, QColor>(i/10.0, QColor(Qt::white));
393 gradient_white_black10 << QPair<qreal, QColor>(i/10.0+0.05, QColor(Qt::black));
394 }
395
396 QGradientStops gradient_white_alpha;
397 gradient_white_alpha << QPair<qreal, QColor>(0, QColor(Qt::white));
398 gradient_white_alpha << QPair<qreal, QColor>(0, QColor(Qt::transparent));
399
400 QGradientStops gradient_white_alpha10;
401 for (int i=0; i<10; ++i) {
402 gradient_white_alpha10 << QPair<qreal, QColor>(i/10.0, QColor(Qt::white));
403 gradient_white_alpha10 << QPair<qreal, QColor>(i/10.0+0.05, QColor(Qt::black));
404 }
405
406
407 for (int j=0; j<4; ++j) {
408 QLinearGradient lg;
409 lg.setStart(0, 0);
410
411 QRadialGradient rg;
412 QConicalGradient cg;
413
414 QGradientStops stops;
415 if (j == 0) stops = gradient_white_black;
416 else if (j == 1) stops = gradient_white_black10;
417 else if (j == 2) stops = gradient_white_alpha;
418 else if (j == 3) stops = gradient_white_alpha10;
419 lg.setStops(stops);
420 rg.setStops(stops);
421 cg.setStops(stops);
422
423 for (int i=0; i<6; ++i) {
424 lg.setSpread((QGradient::Spread) (i % 3));
425 lg.setCoordinateMode((QGradient::CoordinateMode) (j / 3));
426
427 QString name = QString::fromLatin1("-%1%2")
428 .arg(lg.spread())
429 .arg(lg.coordinateMode());
430
431 lg.setFinalStop(100, 0);
432 m_brushes["hor-lingrad-w/b-brush" + name] = QBrush(lg);
433
434 lg.setFinalStop(0, 100);
435 m_brushes["ver-lingrad-w/b-brush" + name] = QBrush(lg);
436
437 lg.setFinalStop(100, 100);
438 m_brushes["diag-lingrad-w/b-brush" + name] = QBrush(lg);
439
440 rg.setRadius(100);
441 rg.setCenter(0, 0);
442 rg.setFocalPoint(50, 50);
443 m_brushes["radgrad-brush" + name] = QBrush(rg);
444
445 cg.setCenter(0, 0);
446 cg.setAngle(40);
447 m_brushes["congrad-brush" + name] = QBrush(cg);
448 }
449 }
450
451 // Set up pens...
452
453
454 // m_pens["black-pen"] = QPen(Qt::black);
455 // m_pens["white-pen"] = QPen(Qt::white);
456 // m_pens["transparent-pen"] = QPen(QColor(255, 255, 255, 0));
457 // m_pens["translucent1-pen"] = QPen(QColor(255, 255, 255, 100));
458 // m_pens["translucent2-pen"] = QPen(QColor(255, 255, 255, 200));
459
460
461
462 }
463
464
465 // void QPainter_Primitives::fillRect_data() {
466
467 // QTest::addColumn<QBrush>("brush");
468 // QTest::addColumn<QSize>("size");
469
470 // for (QMap<QString, QBrush>::const_iterator it = m_brushes.constBegin();
471 // it != m_brushes.constEnd(); ++it) {
472 // for (int w=2; w<1025; w*=2) {
473 // for (int h=2; h<1025; h*=2) {
474 // QTest::newRow(QString("brush=%1; size=[%2,%3]").arg(it.key()).arg(w).arg(h).toLatin1().data())
475 // << *it << QSize(w, h);
476 // }
477 // }
478 // }
479 // }
480
481
482
483
484
485 // void QPainter_Primitives::fillRect()
486 // {
487 // QFETCH(QBrush, brush);
488 // QFETCH(QSize, size);
489
490 // QImage img(1024, 1024, QImage::Format_ARGB32_Premultiplied);
491 // QPainter p(&img);
492 // p.setPen(Qt::NoPen);
493 // p.setBrush(brush);
494 // QRect rect(QPoint(0, 0), size);
495 // QBENCHMARK {
496 // p.drawRect(rect);
497 // }
498 // }
499
500
501
502
503 void tst_QPainter::beginAndEnd()
504 {
505 QPixmap pixmap = rasterPixmap(100, 100);
506
507 QBENCHMARK {
508 QPainter p;
509 p.begin(&pixmap);
510 p.end();
511 }
512 }
513
514 void tst_QPainter::drawLine()
515 {
516 QFETCH(QLine, line);
517 QFETCH(QPen, pen);
518
519 const int offset = 5;
520 QPixmap pixmapUnclipped =
521 rasterPixmap(qMin(line.x1(), line.x2())
522 + 2*offset + qAbs(line.dx()),
523 qMin(line.y1(), line.y2())
524 + 2*offset + qAbs(line.dy()));
525 pixmapUnclipped.fill(Qt::white);
526
527 QPainter p(&pixmapUnclipped);
528 p.translate(offset, offset);
529 p.setPen(pen);
530 p.paintEngine()->syncState();
531
532 QBENCHMARK {
533 p.drawLine(line);
534 }
535
536 p.end();
537
538 }
539
540 void tst_QPainter::drawLine_clipped_data()
541 {
542 drawLine_data();
543 }
544
545 void tst_QPainter::drawLine_clipped()
546 {
547 QFETCH(QLine, line);
548 QFETCH(QPen, pen);
549
550 const int offset = 5;
551 QPixmap pixmapClipped
552 = rasterPixmap(qMin(line.x1(), line.x2())
553 + 2*offset + qAbs(line.dx()),
554 qMin(line.y1(), line.y2())
555 + 2*offset + qAbs(line.dy()));
556
557 const QRect clip = QRect(line.p1(), line.p2()).normalized();
558
559 pixmapClipped.fill(Qt::white);
560 QPainter p(&pixmapClipped);
561 p.translate(offset, offset);
562 p.setClipRect(clip);
563 p.setPen(pen);
564 p.paintEngine()->syncState();
565
566 QBENCHMARK {
567 p.drawLine(line);
568 }
569
570 p.end();
571 }
572
573
574 void tst_QPainter::drawLine_antialiased_clipped_data()
575 {
576 drawLine_data();
577 }
578
579
580 void tst_QPainter::drawLine_antialiased_clipped()
581 {
582 QFETCH(QLine, line);
583 QFETCH(QPen, pen);
584
585 const int offset = 5;
586 QPixmap pixmapClipped
587 = rasterPixmap(qMin(line.x1(), line.x2())
588 + 2*offset + qAbs(line.dx()),
589 qMin(line.y1(), line.y2())
590 + 2*offset + qAbs(line.dy()));
591
592 const QRect clip = QRect(line.p1(), line.p2()).normalized();
593
594 pixmapClipped.fill(Qt::white);
595 QPainter p(&pixmapClipped);
596 p.setRenderHint(QPainter::Antialiasing);
597 p.translate(offset, offset);
598 p.setClipRect(clip);
599 p.setPen(pen);
600 p.paintEngine()->syncState();
601
602 QBENCHMARK {
603 p.drawLine(line);
604 }
605
606 p.end();
607 }
608
609 void tst_QPainter::drawPixmapImage_data_helper(bool pixmaps)
610 {
611 QTest::addColumn<QImage::Format>("sourceFormat");
612 QTest::addColumn<QImage::Format>("targetFormat");
613 QTest::addColumn<QSize>("size");
614 QTest::addColumn<int>("type"); // 0 = circle, 1 = diag line, 2 = solid rect, 3 = alpharect
615
616 QList<QSize> sizes;
617 sizes << QSize(10, 10)
618 << QSize(1000, 1000);
619
620 const char *typeNames[] = {
621 "circle",
622 "line",
623 "solidrect",
624 "alpharect"
625 };
626
627 const char *formatNames[] = {
628 "Invalid",
629 "Mono",
630 "MonoLSB",
631 "Indexed8",
632 "RGB32",
633 "ARGB32",
634 "ARGB32_pm",
635 "RGB16",
636 "ARGB8565_pm",
637 "RGB666",
638 "ARGB6666_pm",
639 "RGB555",
640 "ARGB8555_pm",
641 "RGB888",
642 "RGB444",
643 "ARGB4444_pm",
644 "RGBx8888",
645 "RGBA8888",
646 "RGBA8888_pm",
647 "BGR30",
648 "A2BGR30_pm",
649 "RGB30",
650 "A2RGB30_pm",
651 "Alpha8",
652 "Grayscale8",
653 "RGBx64",
654 "RGBA64",
655 "RGBA64_pm",
656 };
657
658 const QImage::Format pixmapFormats[] = {
659 QImage::Format_RGB32,
660 QImage::Format_ARGB32_Premultiplied,
661 QImage::Format_RGB16,
662 QImage::Format_BGR30,
663 QImage::Format_Invalid
664 };
665
666 const QImage::Format targetImageFormats[] = {
667 QImage::Format_RGB32,
668 QImage::Format_ARGB32,
669 QImage::Format_ARGB32_Premultiplied,
670 QImage::Format_RGB16,
671 QImage::Format_ARGB8565_Premultiplied,
672 QImage::Format_RGBX8888,
673 QImage::Format_RGBA8888_Premultiplied,
674 QImage::Format_BGR30,
675 QImage::Format_A2RGB30_Premultiplied,
676 QImage::Format_Grayscale8,
677 QImage::Format_Invalid
678 };
679
680 const QImage::Format sourceImageFormats[] = {
681 QImage::Format_Indexed8,
682 QImage::Format_RGB32,
683 QImage::Format_ARGB32,
684 QImage::Format_ARGB32_Premultiplied,
685 QImage::Format_RGB16,
686 QImage::Format_RGB888,
687 QImage::Format_RGBX8888,
688 QImage::Format_RGBA8888,
689 QImage::Format_RGB30,
690 QImage::Format_Grayscale8,
691 QImage::Format_Invalid
692 };
693
694 const QImage::Format *targetFormats = pixmaps ? pixmapFormats : targetImageFormats;
695 for (; *targetFormats != QImage::Format_Invalid; ++targetFormats) {
696 const QImage::Format *sourceFormats = pixmaps ? pixmapFormats : sourceImageFormats;
697 for (; *sourceFormats != QImage::Format_Invalid; ++sourceFormats) {
698 for (const QSize &s : qAsConst(sizes)) {
699 for (int type=0; type<=3; ++type) {
700 QString name = QString::fromLatin1("%1 on %2, (%3x%4), %5")
701 .arg(formatNames[*sourceFormats])
702 .arg(formatNames[*targetFormats])
703 .arg(s.width()).arg(s.height())
704 .arg(typeNames[type]);
705 QTest::newRow(name.toLatin1()) << *sourceFormats
706 << *targetFormats
707 << s
708 << type;
709 }
710 }
711 }
712 }
713 }
714
715 static QImage createImage(int type, const QSize &size) {
716 QImage base(size, QImage::Format_ARGB32_Premultiplied);
717 base.fill(0);
718 QPainter p(&base);
719 p.setRenderHint(QPainter::Antialiasing);
720 switch (type) {
721 case 0: // ellipse
722 p.setBrush(Qt::red);
723 p.drawEllipse(0, 0, size.width(), size.height());
724 break;
725 case 1: // line
726 p.drawLine(0, 0, size.width(), size.height());
727 break;
728 case 2:
729 p.fillRect(0, 0, size.width(), size.height(), Qt::red);
730 break;
731 case 3:
732 p.fillRect(0, 0, size.width(), size.height(), QColor(0, 255, 0, 127));
733 break;
734 }
735 p.end();
736 return base;
737 }
738
739
740 void tst_QPainter::drawPixmap_data()
741 {
742 drawPixmapImage_data_helper(true);
743 }
744
745 void tst_QPainter::drawPixmap()
746 {
747 QFETCH(QImage::Format, sourceFormat);
748 QFETCH(QImage::Format, targetFormat);
749 QFETCH(QSize, size);
750 QFETCH(int, type);
751
752 QImage sourceImage = createImage(type, size).convertToFormat(sourceFormat);
753 QImage targetImage(size, targetFormat);
754
755 QPixmap sourcePixmap = rasterPixmap(sourceImage);
756 QPixmap targetPixmap = rasterPixmap(targetImage);
757
758 QPainter p(&targetPixmap);
759
760 QBENCHMARK {
761 p.drawPixmap(0, 0, sourcePixmap);
762 }
763 }
764
765 void tst_QPainter::drawImage_data()
766 {
767 drawPixmapImage_data_helper(false);
768 }
769
770
771 void tst_QPainter::drawImage()
772 {
773 QFETCH(QImage::Format, sourceFormat);
774 QFETCH(QImage::Format, targetFormat);
775 QFETCH(QSize, size);
776 QFETCH(int, type);
777
778 QImage sourceImage = createImage(type, size).convertToFormat(sourceFormat);
779 QImage targetImage(size, targetFormat);
780
781 QPainter p(&targetImage);
782 QBENCHMARK {
783 p.drawImage(0, 0, sourceImage);
784 }
785 }
786
787
788 void tst_QPainter::compositionModes_data()
789 {
790 QTest::addColumn<QPainter::CompositionMode>("mode");
791 QTest::addColumn<QSize>("size");
792 QTest::addColumn<QColor>("color");
793
794 const int n = QPainter::RasterOp_SourceAndNotDestination;
795 for (int i = 0; i <= n; ++i) {
796 QString title("%1:%2");
797 QTest::newRow(qPrintable(title.arg(i).arg("10x10:opaque")))
798 << (QPainter::CompositionMode)(i)
799 << QSize(10, 10) << QColor(255, 0, 0);
800 QTest::newRow(qPrintable(title.arg(i).arg("10x10:!opaque")))
801 << (QPainter::CompositionMode)(i)
802 << QSize(10, 10) << QColor(127, 127, 127, 127);
803 QTest::newRow(qPrintable(title.arg(i).arg("300x300:opaque")))
804 << (QPainter::CompositionMode)(i)
805 << QSize(300, 300) << QColor(255, 0, 0);
806 QTest::newRow(qPrintable(title.arg(i).arg("300x300:!opaque")))
807 << (QPainter::CompositionMode)(i)
808 << QSize(300, 300) << QColor(127, 127, 127, 127);
809 }
810 }
811
812 void tst_QPainter::compositionModes()
813 {
814 QFETCH(QPainter::CompositionMode, mode);
815 QFETCH(QSize, size);
816 QFETCH(QColor, color);
817
818 QPixmap src = rasterPixmap(size);
819 src.fill(color);
820
821 QPixmap dest = rasterPixmap(size);
822 if (mode < QPainter::RasterOp_SourceOrDestination)
823 color.setAlpha(127); // porter-duff needs an alpha channel
824 dest.fill(color);
825
826 QPainter p(&dest);
827 p.setCompositionMode(mode);
828
829 QBENCHMARK {
830 p.drawPixmap(0, 0, src);
831 }
832 }
833
834 void tst_QPainter::drawTiledPixmap_data()
835 {
836 QTest::addColumn<QSize>("srcSize");
837 QTest::addColumn<QSize>("dstSize");
838 QTest::addColumn<QTransform>("transform");
839 QTest::addColumn<QColor>("color");
840 QTest::addColumn<QPainter::RenderHint>("renderHint");
841
842 QTest::newRow("10x10=>20x20")
843 << QSize(10, 10) << QSize(20, 20) << (QTransform())
844 << QColor(Qt::black) << QPainter::RenderHint(0);
845 QTest::newRow("10x10=>20x20, smooth")
846 << QSize(10, 10) << QSize(20, 20) << (QTransform())
847 << QColor(Qt::black) << QPainter::SmoothPixmapTransform;
848 QTest::newRow("10x10=>20x20, !opaque")
849 << QSize(10, 10) << QSize(20, 20) << (QTransform())
850 << QColor(127, 127, 127, 127) << QPainter::RenderHint(0);
851 QTest::newRow("10x10=>20x20, !opaque, smooth")
852 << QSize(10, 10) << QSize(20, 20) << (QTransform())
853 << QColor(127, 127, 127, 127) << QPainter::SmoothPixmapTransform;
854
855 QTest::newRow("10x10=>20x20, rotate(30)")
856 << QSize(10, 10) << QSize(20, 20) << (QTransform().rotate(30))
857 << QColor(Qt::black) << QPainter::RenderHint(0);
858 QTest::newRow("10x10=>20x20, rotate(30), smooth")
859 << QSize(10, 10) << QSize(20, 20) << (QTransform().rotate(30))
860 << QColor(Qt::black) << QPainter::SmoothPixmapTransform;
861 QTest::newRow("10x10=>20x20, rotate(30), !opaque")
862 << QSize(10, 10) << QSize(20, 20) << (QTransform().rotate(30))
863 << QColor(127, 127, 127, 127) << QPainter::RenderHint(0);
864 QTest::newRow("10x10=>20x20, rotate(30), !opaque, smooth")
865 << QSize(10, 10) << QSize(20, 20) << (QTransform().rotate(30))
866 << QColor(127, 127, 127, 127) << QPainter::SmoothPixmapTransform;
867
868 QTest::newRow("100x100=>200x200")
869 << QSize(100, 100) << QSize(200, 200) << (QTransform())
870 << QColor(Qt::black) << QPainter::RenderHint(0);
871 QTest::newRow("100x100=>200x200, smooth")
872 << QSize(100, 100) << QSize(200, 200) << (QTransform())
873 << QColor(Qt::black) << QPainter::SmoothPixmapTransform;
874 QTest::newRow("100x100=>200x200, !opaque")
875 << QSize(100, 100) << QSize(200, 200) << (QTransform())
876 << QColor(127, 127, 127, 127) << QPainter::RenderHint(0);
877 QTest::newRow("100x100=>200x200, !opaque, smooth")
878 << QSize(100, 100) << QSize(200, 200) << (QTransform())
879 << QColor(127, 127, 127, 127) << QPainter::SmoothPixmapTransform;
880
881 QTest::newRow("100x100=>200x200, rotate(30)")
882 << QSize(100, 100) << QSize(200, 200) << (QTransform().rotate(30))
883 << QColor(Qt::black) << QPainter::RenderHint(0);
884 QTest::newRow("100x100=>200x200, rotate(30), smooth")
885 << QSize(100, 100) << QSize(200, 200) << (QTransform().rotate(30))
886 << QColor(Qt::black) << QPainter::SmoothPixmapTransform;
887 QTest::newRow("100x100=>200x200, rotate(30), !opaque")
888 << QSize(100, 100) << QSize(200, 200) << (QTransform().rotate(30))
889 << QColor(127, 127, 127, 127) << QPainter::RenderHint(0);
890 QTest::newRow("100x100=>200x200, rotate(30), !opaque, smooth")
891 << QSize(100, 100) << QSize(200, 200) << (QTransform().rotate(30))
892 << QColor(127, 127, 127, 127) << QPainter::SmoothPixmapTransform;
893 }
894
895 void tst_QPainter::drawTiledPixmap()
896 {
897 QFETCH(QSize, srcSize);
898 QFETCH(QSize, dstSize);
899 QFETCH(QTransform, transform);
900 QFETCH(QColor, color);
901 QFETCH(QPainter::RenderHint, renderHint);
902
903 QPixmap src = rasterPixmap(srcSize);
904 src.fill(color);
905
906 const QRect dstRect = transform.mapRect(QRect(QPoint(), dstSize));
907 QPixmap dst = rasterPixmap(dstRect.right() + 5, dstRect.bottom() + 5);
908 QPainter p(&dst);
909 p.setTransform(transform);
910 p.setRenderHint(renderHint);
911
912 QBENCHMARK {
913 p.drawTiledPixmap(QRect(QPoint(), dstSize), src);
914 }
915 }
916
917 void tst_QPainter::fillPrimitives_helper(QPainter *p, PrimitiveType type, PrimitiveSet *s)
918 {
919 p->paintEngine()->syncState();
920
921 switch (type) {
922 case Primitive_Int_DiagLine: QBENCHMARK { p->drawLine(s->i_line_diag); } break;
923 case Primitive_Int_VerLine: QBENCHMARK { p->drawLine(s->i_line_ver); } break;
924 case Primitive_Int_HorLine: QBENCHMARK { p->drawLine(s->i_line_hor); } break;
925 case Primitive_Int_Rect: QBENCHMARK { p->drawRect(s->i_rect); } break;
926 case Primitive_Int_Ellipse: QBENCHMARK { p->drawEllipse(s->i_rect); } break;
927 case Primitive_Int_Pie: QBENCHMARK { p->drawPie(s->i_rect, 45*16, 270*16); } break;
928 case Primitive_Int_Arc: QBENCHMARK { p->drawArc(s->i_rect, 45*16, 270*16); } break;
929 case Primitive_Int_Chord: QBENCHMARK { p->drawChord(s->i_rect, 45*16, 270*16); } break;
930 case Primitive_Int_TriPoly: QBENCHMARK { p->drawPolygon(s->i_poly_tri); } break;
931 case Primitive_Int_RectPoly: QBENCHMARK { p->drawPolygon(s->i_poly_rect); } break;
932 case Primitive_Int_2RectPoly: QBENCHMARK { p->drawPolygon(s->i_poly_2rects); } break;
933
934 case Primitive_Float_DiagLine: QBENCHMARK { p->drawLine(s->f_line_diag); } break;
935 case Primitive_Float_VerLine: QBENCHMARK { p->drawLine(s->f_line_ver); } break;
936 case Primitive_Float_HorLine: QBENCHMARK { p->drawLine(s->f_line_hor); } break;
937 case Primitive_Float_Rect: QBENCHMARK { p->drawRect(s->f_rect); } break;
938 case Primitive_Float_Ellipse: QBENCHMARK { p->drawEllipse(s->f_rect); } break;
939 case Primitive_Float_Pie: QBENCHMARK { p->drawPie(s->f_rect, 45*16, 270*16); } break;
940 case Primitive_Float_Arc: QBENCHMARK { p->drawArc(s->f_rect, 45*16, 270*16); } break;
941 case Primitive_Float_Chord: QBENCHMARK { p->drawChord(s->f_rect, 45*16, 270*16); } break;
942 case Primitive_Float_TriPoly: QBENCHMARK { p->drawPolygon(s->f_poly_tri); } break;
943 case Primitive_Float_RectPoly: QBENCHMARK { p->drawPolygon(s->f_poly_rect); } break;
944 case Primitive_Float_2RectPoly: QBENCHMARK { p->drawPolygon(s->f_poly_2rects); } break;
945
946 case Primitive_Float_TriPath: QBENCHMARK { p->drawPath(s->f_path_tri); } break;
947 case Primitive_Float_RectPath: QBENCHMARK { p->drawPath(s->f_path_rect); } break;
948 case Primitive_Float_2RectPath: QBENCHMARK { p->drawPath(s->f_path_2rects); } break;
949 case Primitive_Float_EllipsePath: QBENCHMARK { p->drawPath(s->f_path_ellipse); } break;
950 case Primitive_Last_Primitive: break;
951 }
952 }
953
954 void tst_QPainter::drawPrimitives_data_helper(bool fancypens)
955 {
956 QTest::addColumn<int>("type");
957 QTest::addColumn<bool>("aa");
958 QTest::addColumn<bool>("dash");
959 QTest::addColumn<int>("width");
960
961 const char * const names[] = {
962 "IDLine",
963 "IVLine",
964 "IHLine",
965 "IRect",
966 "IElli",
967 "IPie",
968 "IArc",
969 "IChord",
970 "ITriPol",
971 "IRectPol",
972 "I2RectPol",
973 "FDLine",
974 "FVLine",
975 "FHLine",
976 "FRect",
977 "FElli",
978 "FPie",
979 "FArc",
980 "FChord",
981 "FTriPol",
982 "FRectPol",
983 "F2RectPol",
984 "FTriPa",
985 "FRectPa",
986 "F2RectPa",
987 "FElliPa"
988 };
989
990 if (fancypens) {
991 for (int dash=0; dash<2; ++dash) {
992 for (int width=0; width<=4; width+=4) {
993 for (int aa=0; aa<2; ++aa) {
994 for (int type=0; type<Primitive_Last_Primitive; ++type) {
995 QString name = QString::fromLatin1(names[type]);
996
997 if (aa) name += " aa";
998 if (dash) name += " dotted";
999 if (width) name += QString::fromLatin1(" width=%1").arg(width);
1000
1001 QTest::newRow(name.toLatin1()) << type << (bool) aa << (bool) dash << width;
1002 }
1003 }
1004 }
1005 }
1006 } else {
1007 for (int aa=0; aa<2; ++aa) {
1008 for (int type=0; type<Primitive_Last_Primitive; ++type) {
1009 QString name = QString::fromLatin1(names[type]);
1010 if (aa) name += " aa";
1011 QTest::newRow(name.toLatin1()) << type << (bool) aa;
1012 }
1013 }
1014 }
1015 }
1016
1017
1018 void tst_QPainter::fillPrimitives_10()
1019 {
1020 QFETCH(int, type);
1021 QFETCH(bool, aa);
1022 QPainter p(m_surface);
1023 p.setPen(Qt::NoPen);
1024 p.setBrush(Qt::red);
1025 p.setRenderHint(QPainter::Antialiasing, aa);
1026 fillPrimitives_helper(&p, (PrimitiveType) type, &m_primitives_10);
1027 }
1028
1029
1030 void tst_QPainter::fillPrimitives_100()
1031 {
1032 QFETCH(int, type);
1033 QFETCH(bool, aa);
1034 QPainter p(m_surface);
1035 p.setPen(Qt::NoPen);
1036 p.setBrush(Qt::red);
1037 p.setRenderHint(QPainter::Antialiasing, aa);
1038 fillPrimitives_helper(&p, (PrimitiveType) type, &m_primitives_100);
1039 }
1040
1041
1042 void tst_QPainter::fillPrimitives_1000()
1043 {
1044 QFETCH(int, type);
1045 QFETCH(bool, aa);
1046 QPainter p(m_surface);
1047 p.setPen(Qt::NoPen);
1048 p.setBrush(Qt::red);
1049 p.setRenderHint(QPainter::Antialiasing, aa);
1050 fillPrimitives_helper(&p, (PrimitiveType) type, &m_primitives_1000);
1051 }
1052
1053 void tst_QPainter::strokePrimitives_10()
1054 {
1055 QFETCH(int, type);
1056 QFETCH(bool, aa);
1057 QFETCH(bool, dash);
1058 QFETCH(int, width);
1059 QPainter p(m_surface);
1060 p.setPen(QPen(Qt::red, width, dash ? Qt::DashLine : Qt::SolidLine));
1061 p.setBrush(Qt::NoBrush);
1062 p.setRenderHint(QPainter::Antialiasing, aa);
1063 fillPrimitives_helper(&p, (PrimitiveType) type, &m_primitives_10);
1064 }
1065
1066 void tst_QPainter::strokePrimitives_100()
1067 {
1068 QFETCH(int, type);
1069 QFETCH(bool, aa);
1070 QFETCH(bool, dash);
1071 QFETCH(int, width);
1072 QPainter p(m_surface);
1073 p.setPen(QPen(Qt::red, width, dash ? Qt::DashLine : Qt::SolidLine));
1074 p.setBrush(Qt::NoBrush);
1075 p.setRenderHint(QPainter::Antialiasing, aa);
1076 fillPrimitives_helper(&p, (PrimitiveType) type, &m_primitives_100);
1077 }
1078
1079 void tst_QPainter::strokePrimitives_1000()
1080 {
1081 QFETCH(int, type);
1082 QFETCH(bool, aa);
1083 QFETCH(bool, dash);
1084 QFETCH(int, width);
1085 QPainter p(m_surface);
1086 p.setPen(QPen(Qt::red, width, dash ? Qt::DashLine : Qt::SolidLine));
1087 p.setBrush(Qt::NoBrush);
1088 p.setRenderHint(QPainter::Antialiasing, aa);
1089 fillPrimitives_helper(&p, (PrimitiveType) type, &m_primitives_1000);
1090 }
1091
1092 void tst_QPainter::drawText_data()
1093 {
1094 QTest::addColumn<QString>("text");
1095
1096 QTest::newRow("a") << QString::fromLatin1("a");
1097 QTest::newRow("ab") << QString::fromLatin1("ab");
1098 QTest::newRow("abc") << QString::fromLatin1("abc");
1099 QTest::newRow("abcd") << QString::fromLatin1("abcd");
1100 QTest::newRow("abcde") << QString::fromLatin1("abcde");
1101 QTest::newRow("abcdef") << QString::fromLatin1("abcdef");
1102 QTest::newRow("abcdefg") << QString::fromLatin1("abcdefg");
1103 }
1104
1105 void tst_QPainter::drawText()
1106 {
1107 QFETCH(QString, text);
1108
1109 QPainter p(m_surface);
1110
1111 QBENCHMARK {
1112 p.drawText(QPointF(5, 5), text);
1113 }
1114 }
1115
1116 void tst_QPainter::saveRestore_data()
1117 {
1118 QTest::addColumn<int>("change");
1119
1120 for (int i=0; i<16; ++i) {
1121 QString change = "change=";
1122 if (i == 0) change += " none";
1123 if (i & ChangePen) change += " pen";
1124 if (i & ChangeBrush) change += " brush";
1125 if (i & ChangeClip) change += " clip";
1126 if (i & ChangeTransform) change += " xform";
1127
1128 QTest::newRow(change.toLatin1()) << i;
1129 }
1130 }
1131
1132 void tst_QPainter::saveRestore()
1133 {
1134 QFETCH(int, change);
1135
1136 QPen pen(Qt::blue);
1137 QBrush brush(Qt::green);
1138 QRect r(100, 100, 100, 20);
1139
1140 QPainter p(m_surface);
1141
1142 p.setPen(Qt::NoPen);
1143 p.setBrush(Qt::NoBrush);
1144
1145 QBENCHMARK {
1146 p.save();
1147 if (change & ChangePen) { p.setPen(pen); p.setPen(Qt::NoPen); }
1148 if (change & ChangeBrush) { p.setBrush(brush); p.setBrush(Qt::NoBrush); }
1149 if (change & ChangeClip) p.setClipRect(r);
1150 if (change & ChangeTransform) { p.scale(3, 5); p.scale(1/3.0, 1/5.0); }
1151 p.drawRect(0, 0, 1, 1);
1152 p.restore();
1153 };
1154 }
1155
1156 enum ClipType {
1157 RectClipType,
1158 RectPathClipType,
1159 RectRegionClipType,
1160 RegionClipType,
1161 PathClipType
1162 };
1163
1164 void tst_QPainter::clipAndFill_data()
1165 {
1166 QTest::addColumn<int>("type");
1167
1168 QTest::newRow("rect") << (int) RectClipType;
1169 QTest::newRow("rectpath") << (int) RectPathClipType;
1170 QTest::newRow("rectregion") << (int) RectRegionClipType;
1171 QTest::newRow("ellipseRegion") << (int) RegionClipType;
1172 QTest::newRow("ellipsePath") << (int) PathClipType;
1173 }
1174
1175
1176 void tst_QPainter::clipAndFill()
1177 {
1178 QFETCH(int, type);
1179
1180 QRegion region;
1181 QPainterPath path;
1182 QRectF rect;
1183
1184 switch (type) {
1185 case RectClipType:
1186 rect = QRectF(100, 100, 100, 100);
1187 break;
1188 case RectPathClipType:
1189 path.addRect(100, 100, 100, 100);
1190 break;
1191 case RectRegionClipType:
1192 region = QRegion(100, 100, 100, 100);
1193 break;
1194 case RegionClipType:
1195 region = QRegion(100, 100, 100, 100, QRegion::Ellipse);
1196 break;
1197 case PathClipType:
1198 path.addEllipse(100, 100, 100, 100);
1199 break;
1200 }
1201
1202 QPainter p(m_surface);
1203
1204 p.setPen(Qt::NoPen);
1205 p.setBrush(Qt::red);
1206
1207 QBENCHMARK {
1208 if (type == RectClipType)
1209 p.setClipRect(rect);
1210 else if (type == RectPathClipType || type == PathClipType)
1211 p.setClipPath(path);
1212 else
1213 p.setClipRegion(region);
1214 p.drawRect(110, 110, 10, 10);
1215 }
1216 }
1217
1218 QTransform tst_QPainter::transformForAngle(qreal angle)
1219 {
1220 const qreal inv_dist_to_plane = 1. / 1024.;
1221
1222 QTransform transform;
1223
1224 QTransform rotTrans;
1225 rotTrans.translate(-40, 0);
1226 QTransform rotTrans2;
1227 rotTrans2.translate(40, 0);
1228
1229 qreal rad = qDegreesToRadians(angle);
1230 qreal c = ::cos(rad);
1231 qreal s = ::sin(rad);
1232
1233 qreal x = 0;
1234 qreal y = 80;
1235 qreal z = 0;
1236
1237 qreal len = x * x + y * y + z * z;
1238 if (len != 1.) {
1239 len = ::sqrt(len);
1240 x /= len;
1241 y /= len;
1242 z /= len;
1243 }
1244
1245 QTransform rot(x*x*(1-c)+c, x*y*(1-c)-z*s, x*z*(1-c)+y*s*inv_dist_to_plane,
1246 y*x*(1-c)+z*s, y*y*(1-c)+c, y*z*(1-c)-x*s*inv_dist_to_plane,
1247 0, 0, 1);
1248
1249 transform *= rotTrans;
1250 transform *= rot;
1251 transform *= rotTrans2;
1252
1253 return transform;
1254 }
1255
1256 void tst_QPainter::drawRoundedRect()
1257 {
1258 QImage surface(100, 100, QImage::Format_RGB16);
1259 surface.fill(QColor(255,255,255).rgb());
1260 QPainter p(&surface);
1261
1262 p.setPen(QPen(Qt::black, 1));
1263 p.setBrush(Qt::red);
1264
1265 QBENCHMARK {
1266 p.drawRoundedRect(QRectF(.5, .5, 80, 80), 10, 10);
1267 }
1268 }
1269
1270 void tst_QPainter::drawScaledRoundedRect()
1271 {
1272 QImage surface(400, 400, QImage::Format_RGB16);
1273 surface.fill(QColor(255,255,255).rgb());
1274 QPainter p(&surface);
1275
1276 p.setPen(QPen(Qt::black, 1));
1277 p.setBrush(Qt::red);
1278 p.scale(3, 3);
1279
1280 QBENCHMARK {
1281 p.drawRoundedRect(10, 10, 80, 80, 10, 10);
1282 }
1283 }
1284
1285 void tst_QPainter::drawTransformedRoundedRect()
1286 {
1287 QImage surface(400, 400, QImage::Format_RGB16);
1288 surface.fill(QColor(255,255,255).rgb());
1289 QPainter p(&surface);
1290
1291 p.setPen(QPen(Qt::black, 1));
1292 p.setBrush(Qt::red);
1293
1294 QBENCHMARK {
1295 p.setWorldTransform(QTransform(0.956957, 0, 0.000704124, 0, 1, 0, 16.141, 0, 0.735953));
1296 p.drawRoundedRect(100, 100, 80, 80, 10, 10);
1297 }
1298 }
1299
1300 void tst_QPainter::drawAntialiasedRoundedRect()
1301 {
1302 QImage surface(100, 100, QImage::Format_RGB16);
1303 surface.fill(QColor(255,255,255).rgb());
1304 QPainter p(&surface);
1305
1306 p.setRenderHint(QPainter::Antialiasing, true);
1307 p.setPen(QPen(Qt::black, 1));
1308 p.setBrush(Qt::red);
1309
1310 QBENCHMARK {
1311 p.drawRoundedRect(QRectF(.5, .5, 80, 80), 10, 10);
1312 }
1313 }
1314
1315 void tst_QPainter::drawScaledAntialiasedRoundedRect_data()
1316 {
1317 QTest::addColumn<float>("scale");
1318
1319 for (float i = 0; i < 3; i += .1f)
1320 QTest::newRow(QString(QLatin1String("scale=%1")).arg(i).toLatin1()) << i;
1321 }
1322
1323 void tst_QPainter::drawScaledAntialiasedRoundedRect()
1324 {
1325 QFETCH(float, scale);
1326
1327 QImage surface(400, 400, QImage::Format_RGB16);
1328 surface.fill(QColor(255,255,255).rgb());
1329 QPainter p(&surface);
1330
1331 p.setRenderHint(QPainter::Antialiasing, true);
1332 p.setPen(QPen(Qt::black, 1));
1333 p.setBrush(Qt::red);
1334 p.scale(scale, scale);
1335
1336 QBENCHMARK {
1337 p.drawRoundedRect(10, 10, 80, 80, 10, 10);
1338 }
1339 }
1340
1341 void tst_QPainter::drawTransformedAntialiasedRoundedRect_data()
1342 {
1343 QTest::addColumn<QTransform>("transform");
1344
1345 for (float angle = 0; angle < 360; angle += 10)
1346 QTest::newRow(QString(QLatin1String("angle=%1")).arg(angle).toLatin1()) << transformForAngle(angle);
1347 }
1348
1349 void tst_QPainter::drawTransformedAntialiasedRoundedRect()
1350 {
1351 QFETCH(QTransform, transform);
1352
1353 QImage surface(400, 400, QImage::Format_RGB16);
1354 surface.fill(QColor(255,255,255).rgb());
1355 QPainter p(&surface);
1356
1357 p.setRenderHint(QPainter::Antialiasing, true);
1358 p.setPen(QPen(Qt::black, 1));
1359 p.setBrush(Qt::red);
1360
1361 QBENCHMARK {
1362 p.setWorldTransform(transform);
1363 p.drawRoundedRect(100, 100, 80, 80, 10, 10);
1364 }
1365 }
1366
1367 void tst_QPainter::drawImageRoundedRect()
1368 {
1369 //setup image
1370 const int radius = 10;
1371 QImage rectImage(81, 81, QImage::Format_ARGB32_Premultiplied);
1372 rectImage.fill(0);
1373 QPainter rp(&rectImage);
1374 rp.setRenderHint(QPainter::Antialiasing);
1375 rp.setPen(Qt::black);
1376 rp.setBrush(Qt::red);
1377 rp.drawRoundedRect(QRectF(.5, .5, 80, 80), radius, radius);
1378
1379 //setup surface
1380 QImage surface(100, 100, QImage::Format_RGB16);
1381 surface.fill(QColor(255,255,255).rgb());
1382 QPainter p(&surface);
1383
1384 QBENCHMARK {
1385 p.drawImage(0,0, rectImage);
1386 }
1387 }
1388
1389 void tst_QPainter::drawScaledImageRoundedRect_data()
1390 {
1391 QTest::addColumn<int>("imageType");
1392
1393 QTest::newRow("imagetype=ARGB32_Pre") << (int)QImage::Format_ARGB32_Premultiplied;
1394 QTest::newRow("imagetype=ARGB8565_Pre") << (int)QImage::Format_ARGB8565_Premultiplied;
1395 }
1396
1397 void tst_QPainter::drawScaledImageRoundedRect()
1398 {
1399 QFETCH(int, imageType);
1400
1401 //setup image
1402 const int radius = 10;
1403 QImage rectImage(81, 81, (QImage::Format)imageType);
1404 rectImage.fill(0);
1405 QPainter rp(&rectImage);
1406 rp.setRenderHint(QPainter::Antialiasing);
1407 rp.setPen(Qt::black);
1408 rp.setBrush(Qt::red);
1409 rp.drawRoundedRect(QRectF(.5, .5, 80, 80), radius, radius);
1410
1411 //setup surface
1412 QImage surface(400, 400, QImage::Format_RGB16);
1413 surface.fill(QColor(255,255,255).rgb());
1414 QPainter p(&surface);
1415 p.scale(3, 3);
1416
1417 QBENCHMARK {
1418 p.drawImage(0,0, rectImage);
1419 }
1420 }
1421
1422 void tst_QPainter::drawTransformedImageRoundedRect_data()
1423 {
1424 QTest::addColumn<int>("imageType");
1425
1426 QTest::newRow("imagetype=ARGB32_Pre") << (int)QImage::Format_ARGB32_Premultiplied;
1427 QTest::newRow("imagetype=ARGB8565_Pre") << (int)QImage::Format_ARGB8565_Premultiplied;
1428 }
1429
1430 void tst_QPainter::drawTransformedImageRoundedRect()
1431 {
1432 QFETCH(int, imageType);
1433
1434 //setup image
1435 const int radius = 10;
1436 QImage rectImage(81, 81, (QImage::Format)imageType);
1437 rectImage.fill(0);
1438 QPainter rp(&rectImage);
1439 rp.setRenderHint(QPainter::Antialiasing);
1440 rp.setPen(Qt::black);
1441 rp.setBrush(Qt::red);
1442 rp.drawRoundedRect(QRectF(.5, .5, 80, 80), radius, radius);
1443
1444 //setup surface
1445 QImage surface(400, 400, QImage::Format_RGB16);
1446 surface.fill(QColor(255,255,255).rgb());
1447 QPainter p(&surface);
1448
1449 QBENCHMARK {
1450 p.setWorldTransform(QTransform(0.956957, 0, 0.000704124, 0, 1, 0, 16.141, 0, 0.735953));
1451 p.drawImage(100,100, rectImage);
1452 }
1453 }
1454
1455 //code from QDeclarativeRectangle for drawing rounded rects
1456 void tst_QPainter::drawBorderPixmapRoundedRect()
1457 {
1458 //setup image
1459 const int pw = 1;
1460 const int radius = 10;
1461 QImage rectImage(radius*2 + 3 + pw*2, radius*2 + 3 + pw*2, QImage::Format_ARGB32_Premultiplied);
1462 rectImage.fill(0);
1463 QPainter rp(&rectImage);
1464 rp.setRenderHint(QPainter::Antialiasing);
1465 rp.setPen(Qt::black);
1466 rp.setBrush(Qt::red);
1467 if (pw%2)
1468 rp.drawRoundedRect(QRectF(qreal(pw)/2+1, qreal(pw)/2+1, rectImage.width()-(pw+1), rectImage.height()-(pw+1)), radius, radius);
1469 else
1470 rp.drawRoundedRect(QRectF(qreal(pw)/2, qreal(pw)/2, rectImage.width()-pw, rectImage.height()-pw), radius, radius);
1471 QPixmap rectPixmap = rasterPixmap(rectImage);
1472
1473 //setup surface
1474 QImage surface(100, 100, QImage::Format_RGB16);
1475 surface.fill(QColor(255,255,255).rgb());
1476 QPainter p(&surface);
1477
1478 QBENCHMARK {
1479 const int pw = 2;
1480 int width = 80;
1481 int height = 80;
1482
1483 int xOffset = (rectPixmap.width()-1)/2;
1484 int yOffset = (rectPixmap.height()-1)/2;
1485 Q_ASSERT(rectPixmap.width() == 2*xOffset + 1);
1486 Q_ASSERT(rectPixmap.height() == 2*yOffset + 1);
1487
1488 QMargins margins(xOffset, yOffset, xOffset, yOffset);
1489 QTileRules rules(Qt::StretchTile, Qt::StretchTile);
1490 //NOTE: even though our item may have qreal-based width and height, qDrawBorderPixmap only supports QRects
1491 qDrawBorderPixmap(&p, QRect(-pw/2, -pw/2, width+pw, height+pw), margins, rectPixmap, rectPixmap.rect(), margins, rules);
1492 }
1493 }
1494
1495 void tst_QPainter::drawScaledBorderPixmapRoundedRect_data()
1496 {
1497 QTest::addColumn<float>("scale");
1498 QTest::addColumn<int>("imageType");
1499
1500 for (float i = 0; i < 3; i += .1f)
1501 QTest::newRow(QString(QLatin1String("scale=%1; imagetype=ARGB32_Pre")).arg(i).toLatin1()) << i << (int)QImage::Format_ARGB32_Premultiplied;
1502 //for (float i = 0; i < 3; i += .1)
1503 // QTest::newRow(QString(QLatin1String("scale=%1; imagetype=ARGB8565_Pre")).arg(i).toLatin1()) << i << (int)QImage::Format_ARGB8565_Premultiplied;
1504 }
1505
1506 //code from QDeclarativeRectangle for drawing rounded rects
1507 void tst_QPainter::drawScaledBorderPixmapRoundedRect()
1508 {
1509 QFETCH(float, scale);
1510 QFETCH(int, imageType);
1511
1512 //setup image
1513 const int pw = 1;
1514 const int radius = 10;
1515 QImage rectImage(radius*2 + 3 + pw*2, radius*2 + 3 + pw*2, (QImage::Format)imageType);
1516 rectImage.fill(0);
1517 QPainter rp(&rectImage);
1518 rp.setRenderHint(QPainter::Antialiasing);
1519 rp.setPen(Qt::black);
1520 rp.setBrush(Qt::red);
1521 if (pw%2)
1522 rp.drawRoundedRect(QRectF(qreal(pw)/2+1, qreal(pw)/2+1, rectImage.width()-(pw+1), rectImage.height()-(pw+1)), radius, radius);
1523 else
1524 rp.drawRoundedRect(QRectF(qreal(pw)/2, qreal(pw)/2, rectImage.width()-pw, rectImage.height()-pw), radius, radius);
1525
1526 QPixmap rectPixmap = rasterPixmap(rectImage);
1527
1528 //setup surface
1529 QImage surface(400, 400, QImage::Format_RGB16);
1530 surface.fill(QColor(255,255,255).rgb());
1531 QPainter p(&surface);
1532 p.scale(scale, scale);
1533
1534 QBENCHMARK {
1535 const int pw = 2;
1536 int width = 80;
1537 int height = 80;
1538
1539 int xOffset = (rectPixmap.width()-1)/2;
1540 int yOffset = (rectPixmap.height()-1)/2;
1541 Q_ASSERT(rectPixmap.width() == 2*xOffset + 1);
1542 Q_ASSERT(rectPixmap.height() == 2*yOffset + 1);
1543
1544 QMargins margins(xOffset, yOffset, xOffset, yOffset);
1545 QTileRules rules(Qt::StretchTile, Qt::StretchTile);
1546 qDrawBorderPixmap(&p, QRect(-pw/2, -pw/2, width+pw, height+pw), margins, rectPixmap, rectPixmap.rect(), margins, rules);
1547 }
1548 }
1549
1550 void tst_QPainter::drawTransformedBorderPixmapRoundedRect_data()
1551 {
1552 QTest::addColumn<QTransform>("transform");
1553 QTest::addColumn<int>("imageType");
1554
1555 for (float angle = 0; angle < 360; angle += 10)
1556 QTest::newRow(QString(QLatin1String("angle=%1; imagetype=ARGB32_Pre")).arg(angle).toLatin1()) << transformForAngle(angle) << (int)QImage::Format_ARGB32_Premultiplied;
1557 //for (float angle = 0; angle < 360; angle += 10)
1558 // QTest::newRow(QString(QLatin1String("angle=%1; imagetype=ARGB8565_Pre")).arg(angle).toLatin1()) << transformForAngle(angle) << (int)QImage::Format_ARGB8565_Premultiplied;
1559
1560 }
1561
1562 //code from QDeclarativeRectangle for drawing rounded rects
1563 void tst_QPainter::drawTransformedBorderPixmapRoundedRect()
1564 {
1565 QFETCH(QTransform, transform);
1566 QFETCH(int, imageType);
1567
1568 //setup image
1569 const int pw = 1;
1570 const int radius = 10;
1571 QImage rectImage(radius*2 + 3 + pw*2, radius*2 + 3 + pw*2, (QImage::Format)imageType);
1572 rectImage.fill(0);
1573 QPainter rp(&rectImage);
1574 rp.setRenderHint(QPainter::Antialiasing);
1575 rp.setPen(Qt::black);
1576 rp.setBrush(Qt::red);
1577 if (pw%2)
1578 rp.drawRoundedRect(QRectF(qreal(pw)/2+1, qreal(pw)/2+1, rectImage.width()-(pw+1), rectImage.height()-(pw+1)), radius, radius);
1579 else
1580 rp.drawRoundedRect(QRectF(qreal(pw)/2, qreal(pw)/2, rectImage.width()-pw, rectImage.height()-pw), radius, radius);
1581
1582 QPixmap rectPixmap = rasterPixmap(rectImage);
1583
1584 //setup surface
1585 QImage surface(400, 400, QImage::Format_RGB16);
1586 surface.fill(QColor(255,255,255).rgb());
1587 QPainter p(&surface);
1588
1589 QBENCHMARK {
1590 p.setWorldTransform(transform);
1591 const int pw = 2;
1592 int width = 80;
1593 int height = 80;
1594
1595 int xOffset = (rectPixmap.width()-1)/2;
1596 int yOffset = (rectPixmap.height()-1)/2;
1597 Q_ASSERT(rectPixmap.width() == 2*xOffset + 1);
1598 Q_ASSERT(rectPixmap.height() == 2*yOffset + 1);
1599
1600 QMargins margins(xOffset, yOffset, xOffset, yOffset);
1601 QTileRules rules(Qt::StretchTile, Qt::StretchTile);
1602 qDrawBorderPixmap(&p, QRect(-pw/2, -pw/2, width+pw, height+pw), margins, rectPixmap, rectPixmap.rect(), margins, rules);
1603 }
1604 }
1605
1606 void tst_QPainter::drawTransformedTransparentImage_data()
1607 {
1608 QTest::addColumn<int>("imageType");
1609
1610 QTest::newRow("imagetype=ARGB32_Pre") << (int)QImage::Format_ARGB32_Premultiplied;
1611 QTest::newRow("imagetype=ARGB8565_Pre") << (int)QImage::Format_ARGB8565_Premultiplied;
1612 }
1613
1614 void tst_QPainter::drawTransformedTransparentImage()
1615 {
1616 QFETCH(int, imageType);
1617
1618 //setup image
1619 QImage transImage(200, 200, (QImage::Format)imageType);
1620 transImage.fill(0);
1621
1622 //setup surface
1623 QImage surface(200, 200, QImage::Format_RGB16);
1624 surface.fill(QColor(255,255,255).rgb());
1625 QPainter p(&surface);
1626
1627 QBENCHMARK {
1628 p.setWorldTransform(QTransform(0.956957, 0, 0.000704124, 0, 1, 0, 16.141, 0, 0.735953));
1629 p.drawImage(0,0, transImage);
1630 }
1631 }
1632
1633 void tst_QPainter::drawTransformedSemiTransparentImage_data()
1634 {
1635 QTest::addColumn<int>("imageType");
1636
1637 QTest::newRow("imagetype=ARGB32_Pre") << (int)QImage::Format_ARGB32_Premultiplied;
1638 QTest::newRow("imagetype=ARGB8565_Pre") << (int)QImage::Format_ARGB8565_Premultiplied;
1639 }
1640
1641 void tst_QPainter::drawTransformedSemiTransparentImage()
1642 {
1643 QFETCH(int, imageType);
1644
1645 //setup image
1646 QImage transImage(200, 200, (QImage::Format)imageType);
1647 transImage.fill(QColor(0,0,0, 128).rgba());
1648
1649 //setup surface
1650 QImage surface(200, 200, QImage::Format_RGB16);
1651 surface.fill(QColor(255,255,255).rgb());
1652 QPainter p(&surface);
1653
1654 QBENCHMARK {
1655 p.setWorldTransform(QTransform(0.956957, 0, 0.000704124, 0, 1, 0, 16.141, 0, 0.735953));
1656 p.drawImage(0,0, transImage);
1657 }
1658 }
1659
1660 void tst_QPainter::drawTransformedFilledImage_data()
1661 {
1662 QTest::addColumn<int>("imageType");
1663
1664 QTest::newRow("imagetype=ARGB32_Pre") << (int)QImage::Format_ARGB32_Premultiplied;
1665 QTest::newRow("imagetype=ARGB8565_Pre") << (int)QImage::Format_ARGB8565_Premultiplied;
1666 }
1667
1668 void tst_QPainter::drawTransformedFilledImage()
1669 {
1670 QFETCH(int, imageType);
1671
1672 //setup image
1673 QImage filledImage(200, 200, (QImage::Format)imageType);
1674 filledImage.fill(QColor(0,0,0).rgb());
1675
1676 //setup surface
1677 QImage surface(200, 200, QImage::Format_RGB16);
1678 surface.fill(QColor(255,255,255).rgb());
1679 QPainter p(&surface);
1680
1681 QBENCHMARK {
1682 p.setWorldTransform(QTransform(0.956957, 0, 0.000704124, 0, 1, 0, 16.141, 0, 0.735953));
1683 p.drawImage(0,0, filledImage);
1684 }
1685 }
1686
1687
1688 QTEST_MAIN(tst_QPainter)
1689
1690 #include "tst_qpainter.moc"