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