kis_stroke_benchmark.cpp 16.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  Copyright (c) 2010 Lukáš Tvrdý lukast.dev@gmail.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

19 20
#include <stdlib.h>

21 22 23 24 25 26 27 28
#if defined(_WIN32) || defined(_WIN64)
#define srand48 srand
inline double drand48()
{
    return double(rand()) / RAND_MAX;
}
#endif

29
#include <QTest>
30 31 32 33 34 35 36 37 38 39 40 41 42 43

#include "kis_stroke_benchmark.h"
#include "kis_benchmark_values.h"

#include "kis_paint_device.h"

#include <KoColorSpace.h>
#include <KoColorSpaceRegistry.h>
#include <KoColor.h>

#include <kis_image.h>
#include <kis_layer.h>
#include <kis_paint_layer.h>

44 45
#include <brushengine/kis_paint_information.h>
#include <brushengine/kis_paintop_preset.h>
46 47 48 49

#define GMP_IMAGE_WIDTH 3274
#define GMP_IMAGE_HEIGHT 2067
#include <kis_painter.h>
50
#include <brushengine/kis_paintop_registry.h>
51

52 53 54
//#define SAVE_OUTPUT

static const int LINES = 20;
55
static const int RECTANGLES = 20;
56 57
const QString OUTPUT_FORMAT = ".png";

58 59
void KisStrokeBenchmark::initTestCase()
{
60 61
    m_dataPath = QString(FILES_DATA_DIR) + QDir::separator();
    m_outputPath = QString(FILES_OUTPUT_DIR) + QDir::separator();
62 63

    m_colorSpace = KoColorSpaceRegistry::instance()->rgb8();
64 65 66 67
    m_color = KoColor(m_colorSpace);

    int width = TEST_IMAGE_WIDTH;
    int height = TEST_IMAGE_HEIGHT;
68

69
    m_image = new KisImage(0, width, height, m_colorSpace, "stroke sample image");
70
    m_layer = new KisPaintLayer(m_image, "temporary for stroke sample", OPACITY_OPAQUE_U8, m_colorSpace);
71

72

73 74
    m_painter = new KisPainter(m_layer->paintDevice());
    m_painter->setPaintColor(KoColor(Qt::black, m_colorSpace));
75

76 77 78 79
    // for bezier curve test
    initCurvePoints(width, height);
    // for the lines test
    initLines(width,height);
80 81
    // for the rectangles test
    initRectangles(width, height);
82 83 84 85
}

void KisStrokeBenchmark::init()
{
86 87
    KoColor white(m_colorSpace);
    white.fromQColor(Qt::white);
88 89
    m_layer->paintDevice()->fill(0,0, m_image->width(), m_image->height(),white.data());
}
90 91


92 93
void KisStrokeBenchmark::initCurvePoints(int width, int height)
{
94 95 96 97
    QPointF p1(0                , 7.0 / 12.0 * height);
    QPointF p2(1.0 / 2.0 * width  , 7.0 / 12.0 * height);
    QPointF p3(width - 4.0, height - 4.0);

98 99
    m_c1 = QPointF(1.0 / 4.0 * width, height - 2.0);
    m_c2 = QPointF(3.0 / 4.0 * width, 0);
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    m_pi1 = KisPaintInformation(p1, 0.0);
    m_pi2 = KisPaintInformation(p2, 0.95);
    m_pi3 = KisPaintInformation(p3, 0.0);
}


void KisStrokeBenchmark::initLines(int width, int height)
{
    srand(12345678);
    for (int i = 0; i < LINES; i++){
        qreal sx = rand() / qreal(RAND_MAX - 1);
        qreal sy = rand() / qreal(RAND_MAX - 1);
        m_startPoints.append(QPointF(sx * width,sy * height));
        qreal ex = rand() / qreal(RAND_MAX - 1);
        qreal ey = rand() / qreal(RAND_MAX - 1);
        m_endPoints.append(QPointF(ex * width,ey * height));
117 118 119
    }
}

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
void KisStrokeBenchmark::initRectangles(int width, int height)
{
    qreal margin = 0.5;
    qreal skip = 0.01;

    qreal marginWidth = margin*width;
    qreal marginHeight = margin*height;

    qreal skipWidth = skip*width >= 1 ? skip*width : 1;
    qreal skipHeight = skip*width >= 1 ? skip*width : 1;

    // "concentric" rectangles

    for (int i = 0; i < RECTANGLES; i++){
        QPoint corner1 = QPoint(marginWidth + i*skipWidth, marginHeight + i*skipHeight);
        QPoint corner2 = QPoint(width - marginWidth - i*skipWidth, height - marginHeight - i*skipHeight);

        if(corner1.x() < corner2.x() && corner1.y() < corner2.y()) {
            // if the rectangle is not empty
            m_rectangleLeftLowerCorners.append(corner1);
            m_rectangleRightUpperCorners.append(corner2);
        }
    }
}


146

147
void KisStrokeBenchmark::cleanupTestCase()
148
{
149
    delete m_painter;
150
}
151

152 153 154 155 156 157 158 159 160 161 162 163
void KisStrokeBenchmark::deformBrush()
{
    QString presetFileName = "deform-default.kpp";
    benchmarkStroke(presetFileName);
}

void KisStrokeBenchmark::deformBrushRL()
{
    QString presetFileName = "deform-default.kpp";
    benchmarkRandomLines(presetFileName);
}

164 165 166 167 168 169 170 171 172 173 174 175 176
void KisStrokeBenchmark::pixelbrush300px()
{
    QString presetFileName = "autobrush_300px.kpp";
    benchmarkStroke(presetFileName);
}

void KisStrokeBenchmark::pixelbrush300pxRL()
{
    QString presetFileName = "autobrush_300px.kpp";
    benchmarkRandomLines(presetFileName);
}


177 178 179 180 181
void KisStrokeBenchmark::sprayPixels()
{
    QString presetFileName = "spray_wu_pixels1.kpp";
    benchmarkStroke(presetFileName);
}
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
void KisStrokeBenchmark::sprayPixelsRL()
{
    QString presetFileName = "spray_wu_pixels1.kpp";
    benchmarkRandomLines(presetFileName);
}


void KisStrokeBenchmark::sprayTexture()
{
    QString presetFileName = "spray_21_textures1.kpp";
    benchmarkStroke(presetFileName);
}


void KisStrokeBenchmark::sprayTextureRL()
{
    QString presetFileName = "spray_21_textures1.kpp";
    benchmarkRandomLines(presetFileName);
}


void KisStrokeBenchmark::spray30px21particles()
{
    QString presetFileName = "spray_30px21rasterParticles.kpp";
    benchmarkStroke(presetFileName);
}

void KisStrokeBenchmark::spray30px21particlesRL()
{
    QString presetFileName = "spray_30px21rasterParticles.kpp";
    benchmarkRandomLines(presetFileName);
}

void KisStrokeBenchmark::sprayPencil()
{
    QString presetFileName = "spray_scaled2rasterParticles.kpp";
    benchmarkStroke(presetFileName);
}

void KisStrokeBenchmark::sprayPencilRL()
{
    QString presetFileName = "spray_scaled2rasterParticles.kpp";
    benchmarkRandomLines(presetFileName);
}

void KisStrokeBenchmark::softbrushDefault30()
{
    QString presetFileName = "softbrush_30px.kpp";
    benchmarkStroke(presetFileName);
}


235 236 237 238 239 240 241
void KisStrokeBenchmark::softbrushCircle30()
{
    QString presetFileName = "softbrush_30px.kpp";
    benchmarkCircle(presetFileName);
}


242 243 244 245 246 247 248 249 250 251 252
void KisStrokeBenchmark::softbrushDefault30RL()
{
    QString presetFileName = "softbrush_30px.kpp";
    benchmarkRandomLines(presetFileName);}


void KisStrokeBenchmark::softbrushFullFeatures30()
{
    QString presetFileName = "softbrush_30px_full.kpp";
    benchmarkStroke(presetFileName);
}
253 254


255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
void KisStrokeBenchmark::softbrushFullFeatures30RL()
{
    QString presetFileName = "softbrush_30px_full.kpp";
    benchmarkRandomLines(presetFileName);
}

void KisStrokeBenchmark::hairy30pxDefault()
{
    QString presetFileName = "hairybrush_thesis30px1.kpp";
    benchmarkStroke(presetFileName);
}

void KisStrokeBenchmark::hairy30pxDefaultRL()
{
    QString presetFileName = "hairybrush_thesis30px1.kpp";
    benchmarkRandomLines(presetFileName);
}

void KisStrokeBenchmark::hairy30pxAntiAlias()
{
    QString presetFileName = "hairybrush_thesis30px_antialiasing1.kpp";
    benchmarkStroke(presetFileName);
}

void KisStrokeBenchmark::hairy30pxAntiAliasRL()
{
    QString presetFileName = "hairybrush_thesis30px_antialiasing1.kpp";
    benchmarkRandomLines(presetFileName);
}

void KisStrokeBenchmark::hairy30px30density()
{
    QString presetFileName = "hairybrush_thesis30px_density301.kpp";
    benchmarkStroke(presetFileName);
}

void KisStrokeBenchmark::hairy30px30densityRL()
{
    QString presetFileName = "hairybrush_thesis30px_density301.kpp";
    benchmarkRandomLines(presetFileName);
}

void KisStrokeBenchmark::hairy30InkDepletion()
{
    QString presetFileName = "hairy30inkDepletion1.kpp";
    benchmarkStroke(presetFileName);

}

void KisStrokeBenchmark::hairy30InkDepletionRL()
{
    QString presetFileName = "hairy30inkDepletion1.kpp";
    benchmarkRandomLines(presetFileName);
}


void KisStrokeBenchmark::softbrushOpacity()
{
    QString presetFileName = "softbrush_opacity1.kpp";
    benchmarkLine(presetFileName);
}

void KisStrokeBenchmark::softbrushSoftness()
{
    QString presetFileName = "softbrush_softness1.kpp";
    benchmarkLine(presetFileName);
}

323 324 325 326 327 328 329 330 331 332 333
void KisStrokeBenchmark::dynabrush()
{
    QString presetFileName = "dyna301.kpp";
    benchmarkLine(presetFileName);
}

void KisStrokeBenchmark::dynabrushRL()
{
    QString presetFileName = "dyna301.kpp";
    benchmarkRandomLines(presetFileName);
}
334

335 336 337 338 339 340
void KisStrokeBenchmark::experimental()
{
    QString presetFileName = "experimental.kpp";
    benchmarkStroke(presetFileName);
}

341 342 343 344 345 346
void KisStrokeBenchmark::experimentalCircle()
{
    QString presetFileName = "experimental.kpp";
    benchmarkCircle(presetFileName);
}

347 348 349 350 351 352 353 354 355 356 357 358
void KisStrokeBenchmark::colorsmudge()
{
    QString presetFileName = "colorsmudge.kpp";
    benchmarkStroke(presetFileName);
}

void KisStrokeBenchmark::colorsmudgeRL()
{
    QString presetFileName = "colorsmudge.kpp";
    benchmarkStroke(presetFileName);
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

void KisStrokeBenchmark::roundMarker()
{
    // Quick Brush engine ( b) Basic - 1 brush, size 40px)
    QString presetFileName = "roundmarker40px.kpp";
    benchmarkStroke(presetFileName);
}

void KisStrokeBenchmark::roundMarkerRandomLines()
{
    // Quick Brush engine ( b) Basic - 1 brush, size 40px)
    QString presetFileName = "roundmarker40px.kpp";
    benchmarkRandomLines(presetFileName);
}

void KisStrokeBenchmark::roundMarkerRectangle()
{
    // Quick Brush engine ( b) Basic - 1 brush, size 40px)
    QString presetFileName = "roundmarker40px.kpp";
    benchmarkStroke(presetFileName);
}


void KisStrokeBenchmark::roundMarkerHalfPixel()
{
    // Quick Brush engine ( b) Basic - 1 brush, size 0.5px)
    QString presetFileName = "roundmarker05px.kpp";
    benchmarkStroke(presetFileName);
}

void KisStrokeBenchmark::roundMarkerRandomLinesHalfPixel()
{
    // Quick Brush engine ( b) Basic - 1 brush, size 0.5px)
    QString presetFileName = "roundmarker05px.kpp";
    benchmarkRandomLines(presetFileName);
}

void KisStrokeBenchmark::roundMarkerRectangleHalfPixel()
{
    // Quick Brush engine ( b) Basic - 1 brush, size 0.5px)
    QString presetFileName = "roundmarker05px.kpp";
    benchmarkStroke(presetFileName);
}



405 406 407 408 409 410 411 412 413
/*
void KisStrokeBenchmark::predefinedBrush()
{
    QString presetFileName = "deevad-slow-brush1.kpp";
    benchmarkLine(presetFileName);
}

void KisStrokeBenchmark::predefinedBrushRL()
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
414

415
    QString presetFileName = "deevad-slow-brush1.kpp";
Boudewijn Rempt's avatar
Boudewijn Rempt committed
416

417 418 419 420 421 422
    KisPaintOpPresetSP preset = new KisPaintOpPreset(m_dataPath + presetFileName);
    preset->load();
    preset->settings()->setNode(m_layer);
    m_painter->setPaintOpPreset(preset, m_image);

    sleep(3);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437
    QBENCHMARK{
        for (int i = 0; i < LINES; i++){
            KisPaintInformation pi1(m_startPoints[i], 0.0);
            KisPaintInformation pi2(m_endPoints[i], 1.0);
            m_painter->paintLine(pi1, pi2);
        }
    }

    //m_layer->paintDevice()->convertToQImage(0).save(m_outputPath + presetFileName + "_randomLines" + OUTPUT_FORMAT);

}
*/

inline void KisStrokeBenchmark::benchmarkLine(QString presetFileName)
438 439
{
    KisPaintOpPresetSP preset = new KisPaintOpPreset(m_dataPath + presetFileName);
440
    preset->load();
441
    m_painter->setPaintOpPreset(preset, m_layer, m_image);
442

443 444
    QPointF startPoint(0.10 * TEST_IMAGE_WIDTH, 0.5 * TEST_IMAGE_HEIGHT);
    QPointF endPoint(0.90 * TEST_IMAGE_WIDTH, 0.5 * TEST_IMAGE_HEIGHT);
445

446
    KisDistanceInformation currentDistance;
447 448
    KisPaintInformation pi1(startPoint, 0.0);
    KisPaintInformation pi2(endPoint, 1.0);
449

450
    QBENCHMARK{
451
        m_painter->paintLine(pi1, pi2, &currentDistance);
452
    }
453

454 455 456
#ifdef SAVE_OUTPUT
    m_layer->paintDevice()->convertToQImage(0).save(m_outputPath + presetFileName + "_line" + OUTPUT_FORMAT);
#endif
457

458 459
}

460 461
void KisStrokeBenchmark::benchmarkCircle(QString presetFileName)
{
462
    dbgKrita << "(circle)preset : " << presetFileName;
463 464 465

    KisPaintOpPresetSP preset = new KisPaintOpPreset(m_dataPath + presetFileName);
    if (!preset->load()){
466
        dbgKrita << "Preset was not loaded";
467 468 469
        return;
    }

470
    m_painter->setPaintOpPreset(preset, m_layer, m_image);
471 472 473 474 475 476 477 478 479 480 481 482 483

QBENCHMARK{

    qreal radius = 300;
    qreal randomOffset = 300 * 0.4;
    int rounds = 20;
    int steps = 20;
    qreal step = 1.0 / steps;

    QPointF center(m_image->width() * 0.5, m_image->height() * 0.5);
    QPointF first(center.x()+radius,center.y());

    srand48(0);
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    for (int k = 0; k < rounds; k++){
        KisDistanceInformation currentDistance;
        m_painter->paintLine(center, first, &currentDistance);
        QPointF prev = first;
        for (int i = 1; i < steps; i++) {
            qreal cx = cos(i * step * 2 * M_PI);
            qreal cy = sin(i * step * 2 * M_PI);

            cx *= (radius + drand48() * randomOffset);
            cy *= (radius + drand48() * randomOffset);

            cx += center.x();
            cy += center.y();

            m_painter->paintLine(prev, QPointF(cx,cy), &currentDistance);
            prev = QPointF(cx,cy);
        }
        m_painter->paintLine(prev, first, &currentDistance);
502 503 504 505 506 507 508 509 510 511
    }
}

#ifdef SAVE_OUTPUT
    m_layer->paintDevice()->convertToQImage(0).save(m_outputPath + presetFileName + "_circle" + OUTPUT_FORMAT);
#endif

}


512

513
void KisStrokeBenchmark::benchmarkRandomLines(QString presetFileName)
514 515
{
    KisPaintOpPresetSP preset = new KisPaintOpPreset(m_dataPath + presetFileName);
516 517
    bool loadedOk = preset->load();
    if (!loadedOk){
518
        dbgKrita << "The preset was not loaded correctly. Done.";
519 520
        return;
    }else{
521
        dbgKrita << "preset : " << presetFileName;
522 523
    }

524
    m_painter->setPaintOpPreset(preset, m_layer, m_image);
525

526
    QBENCHMARK{
527
        KisDistanceInformation currentDistance;
528
        for (int i = 0; i < LINES; i++){
529 530
            KisPaintInformation pi1(m_startPoints[i], 0.0);
            KisPaintInformation pi2(m_endPoints[i], 1.0);
531
            m_painter->paintLine(pi1, pi2, &currentDistance);
532 533 534
        }
    }

535 536 537 538 539
#ifdef SAVE_OUTPUT
    m_layer->paintDevice()->convertToQImage(0).save(m_outputPath + presetFileName + "_randomLines" + OUTPUT_FORMAT);
#endif
}

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570


void KisStrokeBenchmark::benchmarkRectangle(QString presetFileName)
{
    KisPaintOpPresetSP preset = new KisPaintOpPreset(m_dataPath + presetFileName);
    bool loadedOk = preset->load();
    if (!loadedOk){
        dbgKrita << "The preset was not loaded correctly. Done.";
        return;
    }else{
        dbgKrita << "preset : " << presetFileName;
    }
    m_painter->setPaintOpPreset(preset, m_layer, m_image);

    int rectangleNumber = m_rectangleLeftLowerCorners.size(); // see initRectangles

    QBENCHMARK{
        KisDistanceInformation currentDistance;
        for (int i = 0; i < rectangleNumber; i++){
            QPainterPath path;
            QRect rect = QRect(m_rectangleLeftLowerCorners[i], m_rectangleRightUpperCorners[i]);
            path.addRect(rect);
            m_painter->paintPainterPath(path);
        }
    }

#ifdef SAVE_OUTPUT
    m_layer->paintDevice()->convertToQImage(0).save(m_outputPath + presetFileName + "_rectangle" + OUTPUT_FORMAT);
#endif
}

571 572 573
void KisStrokeBenchmark::benchmarkStroke(QString presetFileName)
{
    KisPaintOpPresetSP preset = new KisPaintOpPreset(m_dataPath + presetFileName);
574 575
    bool loadedOk = preset->load();
    if (!loadedOk){
576
        dbgKrita << "The preset was not loaded correctly. Done.";
577
        return;
578
    } else {
579
        dbgKrita << "preset : " << presetFileName;
580 581
    }

582
    m_painter->setPaintOpPreset(preset, m_layer, m_image);
583

584
    QBENCHMARK{
585 586 587
        KisDistanceInformation currentDistance;
        m_painter->paintBezierCurve(m_pi1, m_c1, m_c1, m_pi2, &currentDistance);
        m_painter->paintBezierCurve(m_pi2, m_c2, m_c2, m_pi3, &currentDistance);
588
    }
589

590
#ifdef SAVE_OUTPUT
591
    dbgKrita << "Saving output " << m_outputPath + presetFileName + ".png";
592 593 594 595 596 597 598 599 600 601
    m_layer->paintDevice()->convertToQImage(0).save(m_outputPath + presetFileName + OUTPUT_FORMAT);
#endif
}

static const int COUNT = 1000000;
void KisStrokeBenchmark::benchmarkRand48()
{
QBENCHMARK
    {
        for (int i = 0 ; i < COUNT; i++){
602 603
            double result = drand48();
            Q_UNUSED(result);
604 605
        }
    }
606 607
}

608 609
void KisStrokeBenchmark::benchmarkRand()
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
610
    float j;
611 612
    QBENCHMARK{
        for (int i = 0 ; i < COUNT; i++){
Boudewijn Rempt's avatar
Boudewijn Rempt committed
613
            j = rand() / (float)RAND_MAX;
614 615
        }
    }
616
    Q_UNUSED(j);
617 618
}

619 620 621 622 623 624 625 626 627 628 629 630 631 632
void KisStrokeBenchmark::becnhmarkPresetCloning()
{
    QString presetFileName = "spray_21_textures1.kpp";
    KisPaintOpPresetSP preset = new KisPaintOpPreset(m_dataPath + presetFileName);
    bool loadedOk = preset->load();
    KIS_ASSERT_RECOVER_RETURN(loadedOk);
    KIS_ASSERT_RECOVER_RETURN(preset->settings());

    QBENCHMARK {
        KisPaintOpPresetSP other = preset->clone();
        other->settings()->setPaintOpOpacity(0.3);
    }
}

633

634
QTEST_MAIN(KisStrokeBenchmark)