Debian Codegraph @

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

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