| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#ifndef BENCHMARKTESTS_H |
| 5 |
#define BENCHMARKTESTS_H |
| 6 |
|
| 7 |
#include <QApplication> |
| 8 |
#include <QTextDocument> |
| 9 |
#include <QTextLayout> |
| 10 |
#include <QFontMetrics> |
| 11 |
#include <QDebug> |
| 12 |
#include <QStaticText> |
| 13 |
#include <QPainter> |
| 14 |
#include <QPainterPath> |
| 15 |
#include <QRandomGenerator> |
| 16 |
|
| 17 |
class Benchmark |
| 18 |
{ |
| 19 |
public: |
| 20 |
virtual ~Benchmark() {} |
| 21 |
|
| 22 |
Benchmark(const QSize &size) |
| 23 |
: m_size(size) |
| 24 |
{ |
| 25 |
for (int i=0; i<16; ++i) { |
| 26 |
m_colors[i] = QColor::fromRgbF((QRandomGenerator::global()->bounded(4)) / 3.0, |
| 27 |
(QRandomGenerator::global()->bounded(4)) / 3.0, |
| 28 |
(QRandomGenerator::global()->bounded(4)) / 3.0, |
| 29 |
1); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
virtual void draw(QPainter *p, const QRect &rect, int iteration) = 0; |
| 34 |
virtual QString name() const = 0; |
| 35 |
|
| 36 |
inline const QSize &size() const |
| 37 |
{ |
| 38 |
return m_size; |
| 39 |
} |
| 40 |
virtual void begin(QPainter *, int iterations = 1) { Q_UNUSED(iterations); } |
| 41 |
virtual void end(QPainter *) { } |
| 42 |
|
| 43 |
inline const QColor &randomColor(int i) { return m_colors[i % 16]; } |
| 44 |
|
| 45 |
protected: |
| 46 |
QColor m_colors[16]; |
| 47 |
QSize m_size; |
| 48 |
}; |
| 49 |
|
| 50 |
class PaintingRectAdjuster |
| 51 |
{ |
| 52 |
public: |
| 53 |
PaintingRectAdjuster() |
| 54 |
: m_benchmark(0), |
| 55 |
m_bounds(), |
| 56 |
m_screen_filled(false) |
| 57 |
{ |
| 58 |
} |
| 59 |
|
| 60 |
const QRect &newPaintingRect() { |
| 61 |
m_rect.translate(m_rect.width(), 0); |
| 62 |
|
| 63 |
if (m_rect.right() > m_bounds.width()) { |
| 64 |
m_rect.moveLeft(m_bounds.left()); |
| 65 |
m_rect.translate(0,m_rect.height()); |
| 66 |
if (m_rect.bottom() > m_bounds.height()) { |
| 67 |
m_screen_filled = true; |
| 68 |
m_rect.moveTo(m_bounds.topLeft()); |
| 69 |
} |
| 70 |
} |
| 71 |
return m_rect; |
| 72 |
} |
| 73 |
|
| 74 |
inline bool isScreenFilled() const |
| 75 |
{ return m_screen_filled; } |
| 76 |
|
| 77 |
void reset(const QRect &bounds) |
| 78 |
{ |
| 79 |
m_bounds = bounds; |
| 80 |
m_rect.moveTo(m_bounds.topLeft()); |
| 81 |
m_rect = QRect(m_bounds.topLeft(),m_benchmark->size()); |
| 82 |
m_rect.translate(-m_rect.width(),0); |
| 83 |
m_screen_filled = false; |
| 84 |
} |
| 85 |
|
| 86 |
inline void setNewBenchmark( Benchmark *benchmark ) |
| 87 |
{ |
| 88 |
m_benchmark = benchmark; |
| 89 |
} |
| 90 |
|
| 91 |
protected: |
| 92 |
Benchmark *m_benchmark; |
| 93 |
QRect m_rect; |
| 94 |
QRect m_bounds; |
| 95 |
bool m_screen_filled; |
| 96 |
}; |
| 97 |
|
| 98 |
class FillRectBenchmark : public Benchmark |
| 99 |
{ |
| 100 |
public: |
| 101 |
FillRectBenchmark(int size) |
| 102 |
: Benchmark(QSize(size, size)) |
| 103 |
{ |
| 104 |
} |
| 105 |
|
| 106 |
void draw(QPainter *p, const QRect &rect, int iterationCount) override |
| 107 |
{ |
| 108 |
p->fillRect(rect, randomColor(iterationCount)); |
| 109 |
} |
| 110 |
|
| 111 |
QString name() const override |
| 112 |
{ |
| 113 |
return QString::fromLatin1("fillRect(%1)").arg(m_size.width()); |
| 114 |
} |
| 115 |
}; |
| 116 |
|
| 117 |
class ImageFillRectBenchmark : public Benchmark |
| 118 |
{ |
| 119 |
public: |
| 120 |
ImageFillRectBenchmark(int size) |
| 121 |
: Benchmark(QSize(size, size)) |
| 122 |
{ |
| 123 |
int s = QRandomGenerator::global()->bounded(24) + 8; |
| 124 |
m_content = QImage(s, s, QImage::Format_ARGB32_Premultiplied); |
| 125 |
QPainter p(&m_content); |
| 126 |
p.fillRect(0, 0, s, s, Qt::white); |
| 127 |
p.fillRect(s/2, 0, s/2, s/2, Qt::gray); |
| 128 |
p.fillRect(0, s/2, s/2, s/2, Qt::gray); |
| 129 |
p.end(); |
| 130 |
|
| 131 |
m_brush = QBrush(m_content); |
| 132 |
} |
| 133 |
|
| 134 |
void draw(QPainter *p, const QRect &rect, int) override { p->fillRect(rect, m_brush); } |
| 135 |
|
| 136 |
QString name() const override |
| 137 |
{ |
| 138 |
return QString::fromLatin1("fillRect with image(%1)").arg(m_size.width()); |
| 139 |
} |
| 140 |
|
| 141 |
private: |
| 142 |
QImage m_content; |
| 143 |
QBrush m_brush; |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
class DrawRectBenchmark : public Benchmark |
| 148 |
{ |
| 149 |
public: |
| 150 |
DrawRectBenchmark(int size) |
| 151 |
: Benchmark(QSize(size, size)) |
| 152 |
{ |
| 153 |
} |
| 154 |
|
| 155 |
void begin(QPainter *p, int) override |
| 156 |
{ |
| 157 |
p->setPen(Qt::NoPen); |
| 158 |
p->setBrush(randomColor(m_size.width())); |
| 159 |
} |
| 160 |
|
| 161 |
void draw(QPainter *p, const QRect &rect, int) override { p->drawRect(rect); } |
| 162 |
|
| 163 |
QString name() const override |
| 164 |
{ |
| 165 |
return QString::fromLatin1("drawRect(%1)").arg(m_size.width()); |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
class DrawRectWithBrushChangeBenchmark : public Benchmark |
| 171 |
{ |
| 172 |
public: |
| 173 |
DrawRectWithBrushChangeBenchmark(int size) |
| 174 |
: Benchmark(QSize(size, size)) |
| 175 |
{ |
| 176 |
} |
| 177 |
|
| 178 |
void begin(QPainter *p, int) override { p->setPen(Qt::NoPen); } |
| 179 |
|
| 180 |
void draw(QPainter *p, const QRect &rect, int i) override |
| 181 |
{ |
| 182 |
p->setBrush(randomColor(i)); |
| 183 |
p->drawRect(rect); |
| 184 |
} |
| 185 |
|
| 186 |
QString name() const override |
| 187 |
{ |
| 188 |
return QString::fromLatin1("drawRect with brushchange(%1)").arg(m_size.width()); |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
class RoundRectBenchmark : public Benchmark |
| 193 |
{ |
| 194 |
public: |
| 195 |
RoundRectBenchmark(int size) |
| 196 |
: Benchmark(QSize(size, size)) |
| 197 |
{ |
| 198 |
m_roundness = size / 4.; |
| 199 |
} |
| 200 |
|
| 201 |
void begin(QPainter *p, int) override |
| 202 |
{ |
| 203 |
p->setPen(Qt::NoPen); |
| 204 |
p->setBrush(Qt::red); |
| 205 |
} |
| 206 |
|
| 207 |
void draw(QPainter *p, const QRect &rect, int) override |
| 208 |
{ |
| 209 |
p->drawRoundedRect(rect, m_roundness, m_roundness); |
| 210 |
} |
| 211 |
|
| 212 |
QString name() const override |
| 213 |
{ |
| 214 |
return QString::fromLatin1("drawRoundedRect(%1)").arg(m_size.width()); |
| 215 |
} |
| 216 |
|
| 217 |
qreal m_roundness; |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
class ArcsBenchmark : public Benchmark |
| 222 |
{ |
| 223 |
public: |
| 224 |
enum Type { |
| 225 |
Stroked = 0x0001, |
| 226 |
Filled = 0x0002, |
| 227 |
|
| 228 |
ArcShape = 0x0010, |
| 229 |
ChordShape = 0x0020, |
| 230 |
PieShape = 0x0040, |
| 231 |
CircleShape = 0x0080, |
| 232 |
Shapes = 0x00f0 |
| 233 |
|
| 234 |
}; |
| 235 |
|
| 236 |
ArcsBenchmark(int size, uint type) |
| 237 |
: Benchmark(QSize(size, size)), |
| 238 |
m_type(type) |
| 239 |
{ |
| 240 |
} |
| 241 |
|
| 242 |
void begin(QPainter *p, int) override |
| 243 |
{ |
| 244 |
if (m_type & Stroked) |
| 245 |
p->setPen(Qt::black); |
| 246 |
else |
| 247 |
p->setPen(Qt::NoPen); |
| 248 |
|
| 249 |
if (m_type & Filled) |
| 250 |
p->setBrush(Qt::red); |
| 251 |
else |
| 252 |
p->setBrush(Qt::NoBrush); |
| 253 |
} |
| 254 |
|
| 255 |
void draw(QPainter *p, const QRect &rect, int) override |
| 256 |
{ |
| 257 |
switch (m_type & Shapes) { |
| 258 |
case ArcShape: |
| 259 |
p->drawArc(rect, 45*16, 120*16); |
| 260 |
break; |
| 261 |
case ChordShape: |
| 262 |
p->drawChord(rect, 45*16, 120*16); |
| 263 |
break; |
| 264 |
case PieShape: |
| 265 |
p->drawPie(rect, 45*16, 120*16); |
| 266 |
break; |
| 267 |
case CircleShape: |
| 268 |
p->drawEllipse(rect); |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
QString name() const override |
| 274 |
{ |
| 275 |
QString fillStroke; |
| 276 |
|
| 277 |
if ((m_type & (Stroked|Filled)) == (Stroked|Filled)) { |
| 278 |
fillStroke = QLatin1String("Fill & Outline"); |
| 279 |
} else if (m_type & Stroked) { |
| 280 |
fillStroke = QLatin1String("Outline"); |
| 281 |
} else if (m_type & Filled) { |
| 282 |
fillStroke = QLatin1String("Fill"); |
| 283 |
} |
| 284 |
|
| 285 |
QString shape; |
| 286 |
if (m_type & PieShape) shape = QLatin1String("drawPie"); |
| 287 |
else if (m_type & ChordShape) shape = QLatin1String("drawChord"); |
| 288 |
else if (m_type & ArcShape) shape = QLatin1String("drawArc"); |
| 289 |
else if (m_type & CircleShape) shape = QLatin1String("drawEllipse"); |
| 290 |
|
| 291 |
return QString::fromLatin1("%1(%2) %3").arg(shape).arg(m_size.width()).arg(fillStroke); |
| 292 |
} |
| 293 |
|
| 294 |
uint m_type; |
| 295 |
}; |
| 296 |
|
| 297 |
|
| 298 |
class DrawScaledImage : public Benchmark |
| 299 |
{ |
| 300 |
public: |
| 301 |
DrawScaledImage(const QImage &image, qreal scale, bool asPixmap) |
| 302 |
: Benchmark(QSize(image.width(), image.height())), |
| 303 |
m_image(image), |
| 304 |
m_type(asPixmap ? "Pixmap" : "Image"), |
| 305 |
m_scale(scale), |
| 306 |
m_as_pixmap(asPixmap) |
| 307 |
{ |
| 308 |
m_pixmap = QPixmap::fromImage(m_image); |
| 309 |
} |
| 310 |
DrawScaledImage(const QString& type, const QPixmap &pixmap, qreal scale) |
| 311 |
: Benchmark(QSize(pixmap.width(), pixmap.height())), |
| 312 |
m_type(type), |
| 313 |
m_scale(scale), |
| 314 |
m_as_pixmap(true), |
| 315 |
m_pixmap(pixmap) |
| 316 |
{ |
| 317 |
} |
| 318 |
|
| 319 |
void begin(QPainter *p, int) override { p->scale(m_scale, m_scale); } |
| 320 |
|
| 321 |
void draw(QPainter *p, const QRect &rect, int) override |
| 322 |
{ |
| 323 |
if (m_as_pixmap) |
| 324 |
p->drawPixmap(rect.topLeft(), m_pixmap); |
| 325 |
else |
| 326 |
p->drawImage(rect.topLeft(), m_image); |
| 327 |
} |
| 328 |
|
| 329 |
QString name() const override |
| 330 |
{ |
| 331 |
return QString::fromLatin1("draw%4(%1) at scale=%2, depth=%3") |
| 332 |
.arg(m_size.width()) |
| 333 |
.arg(m_scale) |
| 334 |
.arg(m_as_pixmap ? m_pixmap.depth() : m_image.depth()) |
| 335 |
.arg(m_type); |
| 336 |
} |
| 337 |
|
| 338 |
private: |
| 339 |
QImage m_image; |
| 340 |
QString m_type; |
| 341 |
qreal m_scale; |
| 342 |
bool m_as_pixmap; |
| 343 |
QPixmap m_pixmap; |
| 344 |
}; |
| 345 |
|
| 346 |
class DrawTransformedImage : public Benchmark |
| 347 |
{ |
| 348 |
public: |
| 349 |
DrawTransformedImage(const QImage &image, bool asPixmap) |
| 350 |
: Benchmark(QSize(image.width(), image.height())), |
| 351 |
m_image(image), |
| 352 |
m_type(asPixmap ? "Pixmap" : "Image"), |
| 353 |
m_as_pixmap(asPixmap) |
| 354 |
{ |
| 355 |
m_pixmap = QPixmap::fromImage(m_image); |
| 356 |
} |
| 357 |
DrawTransformedImage(const QString& type, const QPixmap &pixmap) |
| 358 |
: Benchmark(QSize(pixmap.width(), pixmap.height())), |
| 359 |
m_type(type), |
| 360 |
m_as_pixmap(true), |
| 361 |
m_pixmap(pixmap) |
| 362 |
{ |
| 363 |
} |
| 364 |
|
| 365 |
void draw(QPainter *p, const QRect &rect, int) override |
| 366 |
{ |
| 367 |
QTransform oldTransform = p->transform(); |
| 368 |
p->translate(0.5 * rect.width() + rect.left(), 0.5 * rect.height() + rect.top()); |
| 369 |
p->shear(0.25, 0.0); |
| 370 |
p->rotate(5.0); |
| 371 |
if (m_as_pixmap) |
| 372 |
p->drawPixmap(-0.5 * rect.width(), -0.5 * rect.height(), m_pixmap); |
| 373 |
else |
| 374 |
p->drawImage(-0.5 * rect.width(), -0.5 * rect.height(), m_image); |
| 375 |
p->setTransform(oldTransform); |
| 376 |
} |
| 377 |
|
| 378 |
QString name() const override |
| 379 |
{ |
| 380 |
return QString::fromLatin1("draw%3(%1) w/transform, depth=%2") |
| 381 |
.arg(m_size.width()) |
| 382 |
.arg(m_as_pixmap ? m_pixmap.depth() : m_image.depth()) |
| 383 |
.arg(m_type); |
| 384 |
} |
| 385 |
|
| 386 |
private: |
| 387 |
QImage m_image; |
| 388 |
QString m_type; |
| 389 |
bool m_as_pixmap; |
| 390 |
QPixmap m_pixmap; |
| 391 |
}; |
| 392 |
|
| 393 |
|
| 394 |
class DrawImage : public Benchmark |
| 395 |
{ |
| 396 |
public: |
| 397 |
DrawImage(const QImage &image, bool asPixmap) |
| 398 |
: Benchmark(QSize(image.width(), image.height())), |
| 399 |
m_image(image), |
| 400 |
m_type(asPixmap ? "Pixmap" : "Image"), |
| 401 |
m_as_pixmap(asPixmap) |
| 402 |
{ |
| 403 |
m_pixmap = QPixmap::fromImage(image); |
| 404 |
} |
| 405 |
DrawImage(const QString& type, const QPixmap &pixmap) |
| 406 |
: Benchmark(QSize(pixmap.width(), pixmap.height())), |
| 407 |
m_type(type), |
| 408 |
m_as_pixmap(true), |
| 409 |
m_pixmap(pixmap) |
| 410 |
{ |
| 411 |
} |
| 412 |
|
| 413 |
void draw(QPainter *p, const QRect &rect, int) override |
| 414 |
{ |
| 415 |
if (m_as_pixmap) |
| 416 |
p->drawPixmap(rect.topLeft(), m_pixmap); |
| 417 |
else |
| 418 |
p->drawImage(rect.topLeft(), m_image); |
| 419 |
} |
| 420 |
|
| 421 |
QString name() const override |
| 422 |
{ |
| 423 |
return QString::fromLatin1("draw%2(%1), depth=%3") |
| 424 |
.arg(m_size.width()) |
| 425 |
.arg(m_type) |
| 426 |
.arg(m_as_pixmap ? m_pixmap.depth() : m_image.depth()); |
| 427 |
} |
| 428 |
|
| 429 |
private: |
| 430 |
QImage m_image; |
| 431 |
QString m_type; |
| 432 |
bool m_as_pixmap; |
| 433 |
QPixmap m_pixmap; |
| 434 |
}; |
| 435 |
|
| 436 |
|
| 437 |
class DrawText : public Benchmark |
| 438 |
{ |
| 439 |
public: |
| 440 |
enum Mode { |
| 441 |
PainterMode, |
| 442 |
PainterQPointMode, |
| 443 |
LayoutMode, |
| 444 |
DocumentMode, |
| 445 |
PixmapMode, |
| 446 |
StaticTextMode, |
| 447 |
StaticTextWithMaximumSizeMode, |
| 448 |
StaticTextBackendOptimizations |
| 449 |
}; |
| 450 |
|
| 451 |
DrawText(const QString &text, Mode mode) |
| 452 |
: Benchmark(QSize()), m_mode(mode), m_text(text), m_document(text), m_layout(text) |
| 453 |
{ |
| 454 |
} |
| 455 |
|
| 456 |
void begin(QPainter *p, int iterations) override |
| 457 |
{ |
| 458 |
m_staticTexts.clear(); |
| 459 |
m_currentStaticText = 0; |
| 460 |
m_pixmaps.clear(); |
| 461 |
m_currentPixmap = 0; |
| 462 |
QRect m_bounds = QRect(0,0,p->device()->width(), p->device()->height()); |
| 463 |
switch (m_mode) { |
| 464 |
case PainterMode: |
| 465 |
m_size = (p->boundingRect(m_bounds, 0, m_text)).size(); |
| 466 |
// m_rect = m_rect.translated(-m_rect.topLeft()); |
| 467 |
break; |
| 468 |
case DocumentMode: |
| 469 |
m_size = QSize(m_document.size().toSize()); |
| 470 |
break; |
| 471 |
case PixmapMode: |
| 472 |
for (int i=0; i<4; ++i) { |
| 473 |
m_size = (p->boundingRect(m_bounds, 0, m_text)).size(); |
| 474 |
QPixmap pixmap = QPixmap(m_size); |
| 475 |
pixmap.fill(Qt::transparent); |
| 476 |
{ |
| 477 |
QPainter p(&pixmap); |
| 478 |
p.drawText(pixmap.rect(), m_text); |
| 479 |
} |
| 480 |
m_pixmaps.append(pixmap); |
| 481 |
} |
| 482 |
break; |
| 483 |
|
| 484 |
case LayoutMode: { |
| 485 |
QRect r = p->boundingRect(m_bounds, 0, m_text); |
| 486 |
QStringList lines = m_text.split('\n'); |
| 487 |
int height = 0; |
| 488 |
int leading = p->fontMetrics().leading(); |
| 489 |
m_layout.beginLayout(); |
| 490 |
for (int i=0; i<lines.size(); ++i) { |
| 491 |
QTextLine textLine = m_layout.createLine(); |
| 492 |
if (textLine.isValid()) { |
| 493 |
textLine.setLineWidth(r.width()); |
| 494 |
textLine.setPosition(QPointF(0, height)); |
| 495 |
height += leading + textLine.height(); |
| 496 |
} |
| 497 |
} |
| 498 |
m_layout.endLayout(); |
| 499 |
m_layout.setCacheEnabled(true); |
| 500 |
m_size = m_layout.boundingRect().toRect().size(); |
| 501 |
break; } |
| 502 |
|
| 503 |
case StaticTextWithMaximumSizeMode: { |
| 504 |
QStaticText staticText; |
| 505 |
m_size = (p->boundingRect(m_bounds, 0, m_text)).size(); |
| 506 |
staticText.setTextWidth(m_size.width() + 10); |
| 507 |
staticText.setText(m_text); |
| 508 |
staticText.prepare(p->transform(), p->font()); |
| 509 |
m_staticTexts.append(staticText); |
| 510 |
break; |
| 511 |
} |
| 512 |
case StaticTextBackendOptimizations: { |
| 513 |
m_size = (p->boundingRect(m_bounds, 0, m_text)).size(); |
| 514 |
for (int i=0; i<iterations; ++i) { |
| 515 |
QStaticText staticText; |
| 516 |
staticText.setPerformanceHint(QStaticText::AggressiveCaching); |
| 517 |
staticText.setTextWidth(m_size.width() + 10); |
| 518 |
staticText.setText(m_text); |
| 519 |
staticText.prepare(p->transform(), p->font()); |
| 520 |
m_staticTexts.append(staticText); |
| 521 |
} |
| 522 |
|
| 523 |
break; |
| 524 |
} |
| 525 |
case StaticTextMode: { |
| 526 |
QStaticText staticText; |
| 527 |
staticText.setText(m_text); |
| 528 |
staticText.prepare(p->transform(), p->font()); |
| 529 |
m_staticTexts.append(staticText); |
| 530 |
|
| 531 |
QFontMetrics fm(p->font()); |
| 532 |
m_size = QSize(fm.horizontalAdvance(m_text, m_text.size()), fm.height()); |
| 533 |
|
| 534 |
break; |
| 535 |
} |
| 536 |
case PainterQPointMode: { |
| 537 |
QFontMetrics fm(p->font()); |
| 538 |
m_size = QSize(fm.horizontalAdvance(m_text, m_text.size()), fm.height()); |
| 539 |
break; |
| 540 |
} |
| 541 |
|
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
void draw(QPainter *p, const QRect &rect, int) override |
| 546 |
{ |
| 547 |
switch (m_mode) { |
| 548 |
case PainterMode: |
| 549 |
p->drawText(rect, 0, m_text); |
| 550 |
break; |
| 551 |
case PainterQPointMode: |
| 552 |
p->drawText(rect.topLeft(), m_text); |
| 553 |
break; |
| 554 |
case PixmapMode: |
| 555 |
p->drawPixmap(rect.topLeft(), m_pixmaps.at(m_currentPixmap)); |
| 556 |
m_currentPixmap = (m_currentPixmap + 1) % m_pixmaps.size(); |
| 557 |
break; |
| 558 |
case DocumentMode: |
| 559 |
p->translate(rect.topLeft()); |
| 560 |
m_document.drawContents(p); |
| 561 |
p->translate(-rect.topLeft()); |
| 562 |
break; |
| 563 |
case LayoutMode: |
| 564 |
m_layout.draw(p, rect.topLeft()); |
| 565 |
break; |
| 566 |
case StaticTextWithMaximumSizeMode: |
| 567 |
case StaticTextMode: |
| 568 |
p->drawStaticText(rect.topLeft(), m_staticTexts.at(0)); |
| 569 |
break; |
| 570 |
case StaticTextBackendOptimizations: |
| 571 |
p->drawStaticText(rect.topLeft(), m_staticTexts.at(m_currentStaticText)); |
| 572 |
m_currentStaticText = (m_currentStaticText + 1) % m_staticTexts.size(); |
| 573 |
break; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
QString name() const override |
| 578 |
{ |
| 579 |
int letters = m_text.size(); |
| 580 |
int lines = m_text.count('\n'); |
| 581 |
if (lines == 0) |
| 582 |
lines = 1; |
| 583 |
QString type; |
| 584 |
switch (m_mode) { |
| 585 |
case PainterMode: type = "drawText(rect)"; break; |
| 586 |
case PainterQPointMode: type = "drawText(point)"; break; |
| 587 |
case LayoutMode: type = "layout.draw()"; break; |
| 588 |
case DocumentMode: type = "doc.drawContents()"; break; |
| 589 |
case PixmapMode: type = "pixmap cached text"; break; |
| 590 |
case StaticTextMode: type = "drawStaticText()"; break; |
| 591 |
case StaticTextWithMaximumSizeMode: type = "drawStaticText() w/ maxsize"; break; |
| 592 |
case StaticTextBackendOptimizations: type = "drawStaticText() w/ backend optimizations"; break; |
| 593 |
} |
| 594 |
|
| 595 |
return QString::fromLatin1("%3, len=%1, lines=%2") |
| 596 |
.arg(letters) |
| 597 |
.arg(lines) |
| 598 |
.arg(type); |
| 599 |
} |
| 600 |
|
| 601 |
private: |
| 602 |
Mode m_mode; |
| 603 |
QString m_text; |
| 604 |
QTextDocument m_document; |
| 605 |
QTextLayout m_layout; |
| 606 |
|
| 607 |
QList<QPixmap> m_pixmaps; |
| 608 |
int m_currentPixmap; |
| 609 |
|
| 610 |
int m_currentStaticText; |
| 611 |
QList<QStaticText> m_staticTexts; |
| 612 |
}; |
| 613 |
|
| 614 |
class ClippedDrawRectBenchmark : public Benchmark |
| 615 |
{ |
| 616 |
public: |
| 617 |
enum ClipType { |
| 618 |
RectClip, |
| 619 |
TwoRectRegionClip, |
| 620 |
EllipseRegionClip, |
| 621 |
TwoRectPathClip, |
| 622 |
EllipsePathClip, |
| 623 |
AAEllipsePathClip, |
| 624 |
EllipseRegionThenRectClip, |
| 625 |
EllipsePathThenRectClip |
| 626 |
}; |
| 627 |
|
| 628 |
ClippedDrawRectBenchmark(int size, ClipType type) |
| 629 |
: Benchmark(QSize(size, size)), m_type(type) |
| 630 |
{ |
| 631 |
} |
| 632 |
|
| 633 |
void begin(QPainter *p, int) override |
| 634 |
{ |
| 635 |
QRect m_bounds = QRect(0,0,p->device()->width(), p->device()->height()); |
| 636 |
p->setPen(Qt::NoPen); |
| 637 |
p->setBrush(Qt::red); |
| 638 |
|
| 639 |
switch (m_type) { |
| 640 |
case RectClip: |
| 641 |
p->setClipRect(m_bounds.adjusted(1, 1, -1, -1)); |
| 642 |
break; |
| 643 |
case TwoRectRegionClip: |
| 644 |
p->setClipRegion(QRegion(m_bounds.adjusted(0, 0, -1, -1)) |
| 645 |
| QRegion(m_bounds.adjusted(1, 1, 0, 0))); |
| 646 |
break; |
| 647 |
case EllipseRegionClip: |
| 648 |
p->setClipRegion(QRegion(m_bounds, QRegion::Ellipse)); |
| 649 |
break; |
| 650 |
case TwoRectPathClip: |
| 651 |
{ |
| 652 |
QPainterPath path; |
| 653 |
path.addRect(m_bounds.adjusted(0, 0, -1, -1)); |
| 654 |
path.addRect(m_bounds.adjusted(1, 1, 0, 0)); |
| 655 |
path.setFillRule(Qt::WindingFill); |
| 656 |
p->setClipPath(path); |
| 657 |
} |
| 658 |
break; |
| 659 |
case EllipsePathClip: |
| 660 |
{ |
| 661 |
QPainterPath path; |
| 662 |
path.addEllipse(m_bounds); |
| 663 |
p->setClipPath(path); |
| 664 |
} |
| 665 |
break; |
| 666 |
case AAEllipsePathClip: |
| 667 |
{ |
| 668 |
QPainterPath path; |
| 669 |
path.addEllipse(m_bounds); |
| 670 |
p->setRenderHint(QPainter::Antialiasing); |
| 671 |
p->setClipPath(path); |
| 672 |
p->setRenderHint(QPainter::Antialiasing, false); |
| 673 |
} |
| 674 |
break; |
| 675 |
case EllipseRegionThenRectClip: |
| 676 |
p->setClipRegion(QRegion(m_bounds, QRegion::Ellipse)); |
| 677 |
p->setClipRegion(QRegion(m_bounds.width() / 4, |
| 678 |
m_bounds.height() / 4, |
| 679 |
m_bounds.width() / 2, |
| 680 |
m_bounds.height() / 2), Qt::IntersectClip); |
| 681 |
break; |
| 682 |
case EllipsePathThenRectClip: |
| 683 |
{ |
| 684 |
QPainterPath path; |
| 685 |
path.addEllipse(m_bounds); |
| 686 |
p->setClipPath(path); |
| 687 |
p->setClipRegion(QRegion(m_bounds.width() / 4, |
| 688 |
m_bounds.height() / 4, |
| 689 |
m_bounds.width() / 2, |
| 690 |
m_bounds.height() / 2), Qt::IntersectClip); |
| 691 |
} |
| 692 |
break; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
void draw(QPainter *p, const QRect &rect, int) override { p->drawRect(rect); } |
| 697 |
|
| 698 |
QString name() const override |
| 699 |
{ |
| 700 |
QString namedType; |
| 701 |
switch (m_type) { |
| 702 |
case RectClip: |
| 703 |
namedType = "rect"; |
| 704 |
break; |
| 705 |
case TwoRectRegionClip: |
| 706 |
namedType = "two-rect-region"; |
| 707 |
break; |
| 708 |
case EllipseRegionClip: |
| 709 |
namedType = "ellipse-region"; |
| 710 |
break; |
| 711 |
case TwoRectPathClip: |
| 712 |
namedType = "two-rect-path"; |
| 713 |
break; |
| 714 |
case EllipsePathClip: |
| 715 |
namedType = "ellipse-path"; |
| 716 |
break; |
| 717 |
case AAEllipsePathClip: |
| 718 |
namedType = "aa-ellipse-path"; |
| 719 |
break; |
| 720 |
case EllipseRegionThenRectClip: |
| 721 |
namedType = "ellipseregion&rect"; |
| 722 |
break; |
| 723 |
case EllipsePathThenRectClip: |
| 724 |
namedType = "ellipsepath&rect"; |
| 725 |
break; |
| 726 |
} |
| 727 |
return QString::fromLatin1("%1-clipped-drawRect(%2)").arg(namedType).arg(m_size.width()); |
| 728 |
} |
| 729 |
|
| 730 |
ClipType m_type; |
| 731 |
}; |
| 732 |
|
| 733 |
class LinesBenchmark : public Benchmark |
| 734 |
{ |
| 735 |
public: |
| 736 |
enum LineType { |
| 737 |
Horizontal_Integer, |
| 738 |
Diagonal_Integer, |
| 739 |
Vertical_Integer, |
| 740 |
Horizontal_Float, |
| 741 |
Diagonal_Float, |
| 742 |
Vertical_Float |
| 743 |
}; |
| 744 |
|
| 745 |
LinesBenchmark(int length, LineType type) |
| 746 |
: Benchmark(QSize(qAbs(length), qAbs(length))), |
| 747 |
m_type(type), |
| 748 |
m_length(length) |
| 749 |
{ |
| 750 |
|
| 751 |
} |
| 752 |
|
| 753 |
void draw(QPainter *p, const QRect &rect, int) override |
| 754 |
{ |
| 755 |
switch (m_type) { |
| 756 |
case Horizontal_Integer: |
| 757 |
p->drawLine(QLine(rect.x(), rect.y(), rect.x() + m_length, rect.y())); |
| 758 |
break; |
| 759 |
case Diagonal_Integer: |
| 760 |
p->drawLine(QLine(rect.x(), rect.y(), rect.x() + m_length, rect.y() + m_length)); |
| 761 |
break; |
| 762 |
case Vertical_Integer: |
| 763 |
p->drawLine(QLine(rect.x() + 4, rect.y(), rect.x() + 4, rect.y() + m_length)); |
| 764 |
break; |
| 765 |
case Horizontal_Float: |
| 766 |
p->drawLine(QLineF(rect.x(), rect.y(), rect.x() + m_length, rect.y())); |
| 767 |
break; |
| 768 |
case Diagonal_Float: |
| 769 |
p->drawLine(QLineF(rect.x(), rect.y(), rect.x() + m_length, rect.y() + m_length)); |
| 770 |
break; |
| 771 |
case Vertical_Float: |
| 772 |
p->drawLine(QLineF(rect.x() + 4, rect.y(), rect.x() + 4, rect.y() + m_length)); |
| 773 |
break; |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
QString name() const override |
| 778 |
{ |
| 779 |
const char *names[] = { |
| 780 |
"Hor_I", |
| 781 |
"Diag_I", |
| 782 |
"Ver_I", |
| 783 |
"Hor_F", |
| 784 |
"Diag_F", |
| 785 |
"Ver_F" |
| 786 |
}; |
| 787 |
return QString::fromLatin1("drawLine(size=%1,type=%2)").arg(m_length).arg(names[m_type]); |
| 788 |
} |
| 789 |
|
| 790 |
LineType m_type; |
| 791 |
int m_length; |
| 792 |
}; |
| 793 |
|
| 794 |
#endif // BENCHMARKTESTS_H |